-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDataPackageUnit.pas
2269 lines (2080 loc) · 62.5 KB
/
DataPackageUnit.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{*******************************************************}
{ }
{ 通用内存数据集控件 }
{ }
{ 功能:实现如TDataSet一样的内存数据集控制,支持Blob数据}
{*******************************************************}
//////////////////////////////////////////////////////////////////////////////////
/// ///
/// miniframe开源Web框架技术群:821855479 如果加不了群,请联系QQ:3123827806 ///
/// 网页制作器网址:https://wyeditor.com ///
/// 源码及demo下载:https://wyeditor.com/miniframe/ ///
/// ///
//////////////////////////////////////////////////////////////////////////////////
{$ifdef fpc}
{$MODE Delphi}
{$endif}
{.$INCLUDE Test.inc}
{$ifdef clientdotnet}
{$else}
unit DataPackageUnit;
interface
uses
SysUtils, Classes, db, Generics.Collections, Generics.Defaults, TypInfo{,
IdGlobalProtocols};
type
THjhMemoryDataSet = class;
TMFieldType = TFieldType{(mftUnknown, mftString, mftInteger,
mftBoolean, mftFloat, mftCurrency, mftDate, mftTime, mftDateTime,
mftBytes, mftBlob, mftWideString, mftVariant)};
PInfo = ^TInfo;
TInfo = packed record
{$IFDEF MSWINDOWS}
FieldName: string[100];
{$else}
FieldName: string;
{$ENDIF}
RecordNo: integer;
Size: integer;
end;
PSQLInfo = ^TSQLInfo;
TSQLInfo = packed record
{$IFDEF MSWINDOWS}
TableName: string[100];
{$else}
TableName: string;
{$endif}
Size: integer;
end;
TMFields = class;
EIndexOutBands = class(Exception)
public
constructor Create;reintroduce;
end;
ENotBlobError = class(Exception)
public
constructor Create;reintroduce;
end;
EBlobError = class(Exception)
public
constructor Create;reintroduce;
end;
TMField = class(TComponent)
private
FFieldName: string;
FMDataType: TMFieldType;
FCurPos: Integer;
FSlList, FTmpSL: TStringList;
FParentFields: TMFields;
function GetAsBoolean: Boolean;
function GetAsCurrency: Currency;
function GetAsDateTime: TDateTime;
function GetAsFloat: Double;
function GetAsInteger: Longint;
function GetAsString: string;
function GetAsVariant: Variant;
function GetFieldNo: Integer;
function GetIsNull: Boolean;
function GetStringValue: string;
procedure SetStringValue(Value: string);
procedure SetAsBoolean(const Value: Boolean);
procedure SetAsCurrency(const Value: Currency);
procedure SetAsDateTime(const Value: TDateTime);
procedure SetAsFloat(const Value: Double);
procedure SetAsInteger(const Value: Longint);
procedure SetAsString(const Value: string);
procedure SetAsVariant(const Value: Variant);
function GetAsDate: TDate;
function GetAsTime: TTime;
procedure SetAsDate(const Value: TDate);
procedure SetAsDTime(const Value: TTime);
function GetCommaText: string;
procedure SetCommaText(const Value: string);
function GetText: string;
procedure SetText(const Value: string);
function GetStringValueEx(CurPos: integer): string;
function BlobLoadFromFile_ToBase64(const FileName: string; var ErrStr: string): boolean;
function BlobLoadFromStream_ToBase64(Stream: TMemoryStream; var ErrStr: string): boolean;
public
function GetCommaTextEx: string;
function IndexOf(Value: string): integer;
constructor Create(AOwer: TComponent);override;
destructor Destroy; override;
procedure Clear;
function AsStringEx(Row: integer): string; //2020-08-28
function BlobLoadFromFile(const FileName: string; var ErrStr: string): boolean;
function BlobLoadFromStream(Stream: TMemoryStream; var ErrStr: string): boolean;
function BlobSaveToFile(const FileName: string; var ErrStr: string): boolean;
function BlobSaveToStream(Stream: TStream; var ErrStr: string): boolean;
published
property IsNull: Boolean read GetIsNull;
property DataType: TMFieldType read FMDataType write FMDataType;
property FieldName: string read FFieldName write FFieldName;
property ParentFields: TMFields read FParentFields write FParentFields;
property CurPos: integer read FCurPos write FCurPos default 0;
property FieldNo: Integer read GetFieldNo;
property CommaText: string read GetCommaText write SetCommaText;
property Text: string read GetText write SetText;
property AsVariant: Variant read GetAsVariant write SetAsVariant;
property AsBoolean: Boolean read GetAsBoolean write SetAsBoolean;
property AsCurrency: Currency read GetAsCurrency write SetAsCurrency;
property AsDateTime: TDateTime read GetAsDateTime write SetAsDateTime;
property AsDate: TDate read GetAsDate write SetAsDate;
property AsTime: TTime read GetAsTime write SetAsDTime;
property AsFloat: Double read GetAsFloat write SetAsFloat;
property AsInteger: Longint read GetAsInteger write SetAsInteger;
property AsString: string read GetAsString write SetAsString;
end;
TMFields = class(TObject)
private
FList: TList<TMField>;
FOwer: TComponent;
FParentMds: THjhMemoryDataSet;
function GetFieldList: string;
procedure SetFieldList(const Value: string);
protected
function GetCount: Integer;
function GetField(Index: Integer): TMField;
procedure SetField(Index: Integer; Value: TMField);
public
constructor Create(AOwer: TComponent);
destructor Destroy; override;
procedure Add(const FieldName: string; DataType: TMFieldType = ftString);
procedure Clear;
function FindField(const FieldName: string): TMField;
function FieldByName(const FieldName: string): TMField;
function FieldByNumber(FieldNo: Integer): TMField;
function IndexOf(Field: TMField): Integer;
procedure Remove(Field: TMField);
procedure Delete(FieldName: string);
{$ifdef fpc}
published
{$else}
public
{$endif}
property Fields[Index: Integer]: TMField read GetField write SetField; default;
//通过字段列表(以逗号分隔)传入,自动增加表头, 对于字段类型为数值型的要在名称后面加'*'标识
//如:CORP_ID, WZ_ID, MEAS_ID, WZM_CONVRATE*, WZM_TS //在WZM_CONVRATE后加*表示WZM_CONVRATE为数值型
published
property Count: Integer read GetCount;
property FieldList: string read GetFieldList write SetFieldList;
end;
THjhMemoryDataSet = class(TComponent)
private
FFields: TMFields;
FBof, FEof, FNeedProcessBlob, FNeedSQLScript: boolean;
FAbout: string;
FSl, SQLScriptSl: TStringList;
FTmpSL: TStringList;
FDataSet: TDataSet;
FTableName, FAliasName: string;
FHint, FDataString: string;
FBlobToBase64: boolean;
function GetRecordCount: integer;
function GetItems(index: integer): TMFields;
function GetCurRecordPos: integer;
procedure SetCurRecordPos(const Value: integer);
procedure Save;
procedure SaveSQLScript(Stream: TStream);
procedure SaveBlob(Stream: TStream);
procedure Load;
procedure LoadBlob(Stream: TStream);
procedure LoadSQLScript(Stream: TStream);
function GetRecords(index: integer; FieldName: string): Variant;
function GetCommaText: string;
function GetCommaTextEx: string;
procedure SetCommaText(const Value: string);
function GetData: OleVariant;
procedure SetData(const Value: OleVariant);
procedure MyAddField;
procedure OpenDataEx;
procedure SetBlobToBase64(const Value: boolean);
public
NeedSingleQuotes: boolean; //2018-02-26 数据添加单引号
constructor Create(AOwer: TComponent); override;
destructor Destroy; override;
procedure Clear; //清空数据和字段
procedure ClearData;//清空数据
function FieldByName(const FieldName: string): TMField; //通过字段名获取字段对象
function FindField(const FieldName: string): TMField; //通过字段名查找字段对象
function V(const FieldName: string): string; //通过字段名读取一项值,返回串
procedure S(const FieldName, Value: string); //通过字段名设置对应的值
procedure Append; //添加一行
procedure Insert;overload; //在当前位置插入一行
procedure Insert(Pos: integer);overload; //在指定位置插入一行
procedure Delete;overload; //删除当前行
procedure Delete(Pos: integer);overload; //删除指定下标的行
procedure First; //移到最前记录
procedure Prior; //移到前一记录
procedure Next; //移到下一记录
procedure Last; //移到最后记录
procedure Go(const Pos: integer); //移到指定行
function SaveToFile(const FileName: string; var ErrStr: string): boolean; //保存到文件
function SaveToStream(Stream: TStream; var ErrStr: string): boolean; //保存到流
function LoadFromFile(const FileName: string; var ErrStr: string): boolean; //从文件中加载
function LoadFromStream(Stream: TStream; var ErrStr: string): boolean; //从流中加载
procedure Open; //取字段名和数据
procedure OpenFields; //只读取字段,建表头
procedure OpenData; //只从数据集中取数据
procedure AddCurRow(MemoryStream: TMemoryStream); //添加一到mds中
procedure Post;
function IsEmpty: boolean; //判断数据集是不是空
procedure CopyOneRecord(SourMds: THjhMemoryDataset; NeedAppend: boolean = true); overload; //从SourMds复制数据集
procedure CopyOneRecord(SourCds: TDataset); overload; //从SourCds复制数据集
procedure CopyToOneRecord(TargetCds: TDataset); //把当前数据集复制到
procedure SetCommaTextEx(const Value: string);
//2022-05-12 add 通过文件后缀取适合base64的文件类型头信息
//class function GetFileMIMEType(FileName: string): string;
//class function GetFileMIMEType_Base64Head(FileName: string): string;
{$ifdef fpc}
published
{$else}
public
{$endif}
property Items[index: integer]: TMFields read GetItems; //获取指定下标的列
property Records[index: integer; FieldName: string]: Variant read GetRecords; //获取指定下标行指定的字段的值,这种方法获取多线程安全
protected
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
published
property Bof: boolean read FBof; //是否最前面
property Eof: boolean read FEof; //是否最后面
property CurRecordPos: integer read GetCurRecordPos write SetCurRecordPos; //当前行的位置
property Fields: TMFields read FFields write FFields; //字段对象集合
property RecordCount: integer read GetRecordCount; //数据集记录数
property CommaText: string read GetCommaText write SetCommaText; //将数据集转为字符串
property CommaTextEx: string read GetCommaTextEx write SetCommaText;
property Data: Olevariant read GetData write SetData; //将数据集转为Olevariant
property About: string read FAbout write FAbout;
property NeedProcessBlob: boolean read FNeedProcessBlob write FNeedProcessBlob default true; //是否需要处理Blob字段
property DataSet: TDataSet read FDataSet write FDataSet default nil; //绑定DataSet
property NeedSQLScript: boolean read FNeedSQLScript write FNeedSQLScript default false;
property AliasName: string read FAliasName write FAliasName;
property TableName: string read FTableName write FTableName;
property Hint: string read FHint write FHint;
property DataString: string read FDataString write FDataString;
property BlobToBase64: boolean read FBlobToBase64 write SetBlobToBase64; //2022-05-12 add //是否把Blob数据转Base64存贮
end;
//var
//MIMEMap: TIdMIMETable;
implementation
uses {dbtables,}PubSysUnit, Variants;
{ THjhMemoryDataSet }
function GetTableSQLScript(aTableName, aAliasName:string;
SQLIncludeIndexes: boolean = true;SQLAutoWrap:Boolean = true;
SQLSeparator:string = ''): string;
var
//Query1: TQuery ;
i:Integer ;
S,S1,DTn : String ;
Enterkey : string ;
begin
if SQLAutoWrap then Enterkey := #13#10
else Enterkey := '' ;
result := '';
{Query1:= TQuery.Create(nil);
try
Query1.Close;
Query1.SQl.Clear;
Query1.DatabaseName := aAliasName;
Query1.SQL.text := 'select FieldName=left(a.name,255) ,'
+' DataTypeName=left(b.name,255),'
+' DataLen=a.length,'
+' CanNull=columnproperty(object_id("'+aTableName+'"),a.name,"AllowsNull"),'
+' DefaultValue=(select left(text,20) from syscomments where id= a.cdefault) '
+' from syscolumns a,systypes b '
+' where a.id=object_id("'+aTableName+'") '
+' and a.xtype=b.xtype'
+' order by a.colid';
Query1.Open ;
s := 'Create Table [dbo].['+aTableName+'](' ;
While not Query1.Eof do
begin
if Pos(' ['+trim(Query1 .FieldByName('FieldName').AsString)+'] ',s) > 0 then
begin
Query1.Next;
Continue;
end;
DTn := trim(Query1.FieldByName('DataTypeName').AsString) ;
S := s +EnterKey+' ['+trim(Query1 .FieldByName('FieldName').AsString)+'] ['
+DTn+'] ';
if (Pos('char',LowerCase(DTn))>0)or(Pos('binary',LowerCase(DTn))>0) then
S := S+'('+Trim(Query1.FieldByName('DataLen').AsString)+') ';
if (pos('numeric',LowerCase(DTn))>0) then
S := S+'(5,0) ';
if Query1.FieldByName('CanNull').AsInteger = 0 then
S := S+' Not Null '
else S := S + ' Null ';
S1 := Query1.FieldByName('DefaultValue').AsString ;
i := Pos('(',s1) ;
if i>0 then
S := S + ' Default '+ S1;//Copy(s1,i+1,pos(')',s1)-i-1);hjh改为这句
S := S +' ,';
Query1.next ;
end;
S[Length(S)] := ')' ;
if SQLSeparator<>'' then S := S+Enterkey+SQLSeparator+' '+EnterKey
else S := S+EnterKey;
if SQLIncludeIndexes then
//包含索引
begin
Query1.Close;
Query1.SQL.Text :='select IndexName=left(a.name,255), '
+' ColName=left(Col_name(object_id("'+aTableName+'"),b.colid),255),'
+' IndexOrder=b.keyno,'
+' IsUnique=indexproperty(object_id("'+aTableName+'"),a.name,"IsUnique")'
+' from sysindexes a,sysindexkeys b '
+' where a.id=object_id("'+aTableName+'") and a.id=b.id '
+' and left(a.name,1)<>''_'' '
+' and a.indid=b.indid and a.used>0 '
+' order by a.name,b.colid ';
Query1.Open ;
S1 := '';
while not Query1.Eof do
begin
i := Query1.FieldByName('IsUnique').AsInteger ;
if S1<>Query1.FieldByName('IndexName').AsString then
begin
S1 := Query1.FieldByName('IndexName').AsString ;
if i=1 then
S := S +EnterKey+' Alter table '+aTableName+' add constraint '+
Query1.FieldByName('IndexName').AsString+' UNIQUE CLUSTERED ('
else
S := S +EnterKey+' Create Index '+
Query1.FieldByName('IndexName').AsString+' On '+ATableName+'(';
end;
S := S+Query1.FieldByName('ColName').AsString+',';
Query1.next ;
if Query1.Eof or(S1<>Query1.FieldByName('IndexName').AsString) then
begin
S[length(S)] := ')';
if SQLSeparator<>'' then S := S+Enterkey+SQLSeparator+' '+EnterKey
else S := S+EnterKey;
end;
end;
end;
finally
Query1.free;
end; }
Result := s;
end;
procedure THjhMemoryDataSet.Append;
var
lp: integer;
begin
for lp := 0 to Fields.Count - 1 do
begin
Fields.Fields[lp].FSlList.Add('');
Fields.Fields[lp].CurPos := Fields.Fields[lp].FSlList.Count - 1;
end;
FBof := false;
FEof := false;
end;
procedure THjhMemoryDataSet.Insert;
var
lp: integer;
begin
for lp := 0 to Fields.Count - 1 do
begin
if Fields.Fields[lp].FSlList.Count < 1 then
begin
Fields.Fields[lp].FSlList.Add('');
Fields.Fields[lp].CurPos := 0;
end else
Fields.Fields[lp].FSlList.Insert(Fields.Fields[lp].CurPos, '');
end;
FBof := false;
FEof := false;
end;
procedure THjhMemoryDataSet.Delete;
var
lp: integer;
begin
if RecordCount < 1 then exit;
for lp := 0 to Fields.Count - 1 do
begin
Fields.Fields[lp].FSlList.Delete(Fields.Fields[lp].CurPos);
if Fields.Fields[lp].CurPos > Fields.Fields[lp].FSlList.Count - 1 then
Fields.Fields[lp].CurPos := Fields.Fields[lp].CurPos - 1;
if Fields.Fields[lp].FSlList.Count = 0 then Fields.Fields[lp].CurPos := -1;
end;
if Fields.Count > 0 then
if Fields.Fields[0].CurPos < 1 then
FBof := true;
if (Fields.Fields[0].CurPos < 1)and(RecordCount < 1) then
FEof := true;
if Fields.Count > 0 then
if Fields.Fields[0].CurPos >= Fields.Count - 1 then
FEof := true;
end;
procedure THjhMemoryDataSet.Clear;
begin
FBof := true;
FEof := true;
CurRecordPos := -1;
Fields.Clear;
ClearData;
end;
constructor THjhMemoryDataSet.Create(AOwer: TComponent);
begin
inherited Create(AOwer);
FBlobToBase64 := false;
//{$ifdef XE10}
//NeedSingleQuotes := true; //2018-02-26 add
//{$ELSE}
NeedSingleQuotes := false; //2018-02-26 add
//{$ENDIF}
FFields := TMFields.Create(AOwer);
FFields.FParentMds := Self;
FHint := '';
FDataString := '';
FBof := true;
FEof := true;
FNeedProcessBlob := true;
FNeedSQLScript := false;
FDataSet := nil;
FTmpSL := TStringList.Create;
end;
destructor THjhMemoryDataSet.Destroy;
begin
if not Assigned(Self) then exit;
FFields.Clear;
FFields.Free;
if Assigned(FSl) then
FSl.Free;
if Assigned(SQLScriptSl) then
SQLScriptSl.Free;
FTmpSL.Free;
inherited;
end;
function THjhMemoryDataSet.FieldByName(const FieldName: string): TMField;
begin
Result := FindField(FieldName);
if Result = nil then raise Exception.Create(Name + '不存在的字段名['+FieldName+']');
end;
function THjhMemoryDataSet.FindField(const FieldName: string): TMField;
begin
Result := FFields.FindField(FieldName);
end;
function THjhMemoryDataSet.GetRecordCount: integer;
begin
Result := -1;
if FFields.Count < 1 then exit;
//hjh mod Result := TMField(FFields.FList.Items[0]).FSlList.Count;
Result := TMField(FFields.FList[0]).FSlList.Count;
end;
procedure THjhMemoryDataSet.First;
var
lp: integer;
begin
if RecordCount < 1 then
begin
FBof := true;
FEof := true;
exit;
end;
for lp := 0 to FFields.Count - 1 do
FFields.Fields[lp].CurPos := 0;
if RecordCount < 1 then
begin
FBof := true;
FEof := true;
end else
begin
FBof := false;
FEof := false;
end;
end;
procedure THjhMemoryDataSet.Last;
var
lp: integer;
begin
if RecordCount < 1 then exit;
for lp := 0 to FFields.Count - 1 do
FFields.Fields[lp].CurPos := FFields.Fields[lp].FSlList.Count - 1;
if RecordCount < 1 then
begin
FBof := true;
FEof := true;
end else
begin
FBof := false;
FEof := false;
end;
end;
procedure THjhMemoryDataSet.Next;
var
lp: integer;
begin
if RecordCount < 1 then
begin
FEof := true;
exit;
end;
FBof := false;
if Fields.Fields[0].CurPos = FFields.Fields[0].FSlList.Count - 1 then
FEof := true;
for lp := 0 to FFields.Count - 1 do
begin
FFields.Fields[lp].CurPos := FFields.Fields[lp].CurPos + 1;
if FFields.Fields[lp].CurPos > FFields.Fields[lp].FSlList.Count - 1 then
FFields.Fields[lp].CurPos := FFields.Fields[lp].FSlList.Count - 1;
end;
end;
procedure THjhMemoryDataSet.Prior;
var
lp: integer;
begin
if RecordCount < 1 then exit;
if Fields.Count > 0 then
FEof := false;
if Fields.Fields[0].CurPos = 0 then
FBof := true;
for lp := 0 to FFields.Count - 1 do
begin
FFields.Fields[lp].CurPos := FFields.Fields[lp].CurPos - 1;
if FFields.Fields[lp].CurPos < 0 then FFields.Fields[lp].CurPos := 0;
end;
end;
function THjhMemoryDataSet.GetItems(index: integer): TMFields;
var
lp: integer;
begin
Result := FFields;
for lp := 0 to Result.Count - 1 do
TMField(Result.Fields[lp]).CurPos := index;
end;
function THjhMemoryDataSet.GetCurRecordPos: integer;
begin
Result := -1;
if FFields.Count < 1 then exit;
Result := FFields.Fields[0].CurPos;
end;
procedure THjhMemoryDataSet.SetCurRecordPos(const Value: integer);
var
lp: integer;
begin
for lp := 0 to FFields.Count - 1 do
FFields.Fields[lp].CurPos := Value;
end;
procedure THjhMemoryDataSet.Delete(Pos: integer);
begin
CurRecordPos := Pos;
Delete;
end;
procedure THjhMemoryDataSet.Insert(Pos: integer);
begin
CurRecordPos := Pos;
Insert;
end;
procedure THjhMemoryDataSet.Go(const Pos: integer);
begin
CurRecordPos := Pos;
end;
procedure THjhMemoryDataSet.S(const FieldName, Value: string);
var
Field: TMField;
begin
Field := FindField(FieldName);
if Field <> nil then Field.AsString := Value;
end;
procedure THjhMemoryDataSet.Save;
var
pt: PTypeInfo;
function RepaceType(FieldType: TMFieldType):string;
begin
Result := '';
pt := TypeInfo(TMFieldType);
Result := GetEnumName(pt, Ord(FieldType));
{case FieldType of
mftUnknown: Result := 'mftUnknown';
mftString: Result := 'mftString';
mftInteger: Result := 'mftInteger';
mftBoolean: Result := 'mftBoolean';
mftFloat: Result := 'mftFloat';
mftCurrency: Result := 'mftCurrency';
mftDate: Result := 'mftDate';
mftTime: Result := 'mftTime';
mftDateTime: Result := 'mftDateTime';
mftBytes: Result := 'mftBytes';
mftBlob: Result := 'mftBlob';
mftWideString: Result := 'mftWideString';
mftVariant: Result := 'mftVariant';
end;}
end;
procedure MySaveField;
var
i: integer;
tmpStr: string;
begin
tmpStr := '';
for i := 0 to FFields.Count - 1 do
tmpStr := tmpStr + #2 + FFields.Fields[i].FieldName + #2 +
RepaceType(FFields.Fields[i].DataType);
if Length(tmpStr) > 1 then
System.Delete(tmpStr,1,1);
FSl.Add(tmpStr);
end;
procedure MySaveData;
var
i, k: integer;
SL, SL2: TStringList;
begin
SL := TStringList.Create;
try
SL2 := TStringList.Create;
try
for i := 0 to RecordCount - 1 do
begin
SL.Clear;
for k := 0 to FFields.Count - 1 do
begin
SL2.Text := Items[i].Fields[k].AsString;
SL.Add(SL2.CommaText);
end;
FSl.Add(SL.CommaText);
end;
finally
SL2.Free;
end;
finally
SL.Free;
end;
end;
begin
if not Assigned(FSl) then FSl := TStringList.Create
else FSl.Clear;
MySaveField;
MySaveData;
end;
procedure THjhMemoryDataSet.Load;
var
TmpSl: TStringList;
function RepaceType(FieldType: string): TMFieldType;
var
pt: PTypeInfo;
p: PTypeData;
I: integer;
begin
Result := ftString;
pt := TypeInfo(TMFieldType);
FieldType := trim(FieldType);
Result := ftString;
p := GetTypeData(TypeInfo(TMFieldType));
for I := p.MinValue to p.MaxValue do
if LowerCase(GetEnumName(pt,I)) = LowerCase(trim(FieldType)) then
begin
Result := TMFieldType(I);
break;
end;
{ //2022-05-11 add
if FieldType = 'mftUnknown' then Result := mftUnknown else
if FieldType = 'mftString' then Result := mftString else
if FieldType = 'mftInteger' then Result := mftInteger else
if FieldType = 'mftBoolean' then Result := mftBoolean else
if FieldType = 'mftFloat' then Result := mftFloat else
if FieldType = 'mftCurrency'then Result := mftCurrency else
if FieldType = 'mftDate' then Result := mftDate else
if FieldType = 'mftTime' then Result := mftTime else
if FieldType = 'mftDateTime'then Result := mftDateTime else
if FieldType = 'mftBytes' then Result := mftBytes else
if FieldType = 'mftBlob' then Result := mftBlob else
if FieldType = 'mftWideString' then Result := mftWideString else
if FieldType = 'mftVariant' then Result := mftVariant else
Result := mftString;}
end;
procedure MyLoadField;
var
TmpStr, Oth: string;
i: integer;
begin
TmpStr := FSl[0];
TmpStr := trim(TmpStr);
if TmpStr = '' then exit;
for i := 1 to length(TmpStr) do
if TmpStr[i] = #2 then TmpStr[i] := #13;
TmpSl.Text := TmpStr;
FFields.Clear;
for i := 0 to TmpSl.Count div 2 - 1 do
begin
if Pos(',' + TmpSl[2*i] + ',', Oth) < 1 then //2022-05-07 add 过滤
FFields.Add(TmpSl[2*i], RepaceType(TmpSl[2*i + 1]));
Oth := Oth + ',' + TmpSl[2*i] + ','; //2022-05-07 add 过滤
end;
end;
procedure MyLoadData;
var
i, j: integer;
TmpSl2: TStringlist;
begin
TmpSl2 := TStringlist.Create;
try
for i := 1 to FSl.Count - 1 do
begin
TmpSl.Clear;
TmpSl.CommaText := FSl[i];
Append;
//2022-05-17 mod for j := 0 to TmpSl.Count - 1 do
for j := 0 to FFields.Count - 1 do
begin
try
TmpSl2.CommaText := TmpSl[j];
//2017-11-09 mod FieldByName(FFields.Fields[j].FieldName).SetStringValue(TmpSl2.Text);
FieldByName(FFields.Fields[j].FieldName).SetStringValue(trim(TmpSl2.Text));
except
end;
end;
end;
finally
TmpSl2.Free;
end;
end;
begin
//{$ifdef XE10}
//NeedSingleQuotes := true; //2018-02-26 add
//{$ELSE}
NeedSingleQuotes := false; //2018-02-26 add
//{$ENDIF}
if (not Assigned(FSl)) or (FSl.Count < 1) then exit;
TmpSl := TStringList.Create;
try
MyLoadField;
MyLoadData;
finally
TmpSl.Free;
end;
end;
procedure THjhMemoryDataSet.SaveBlob(Stream: TStream);
var
Info: PInfo;
lp, Size: integer;
procedure SaveOneColBlob;
var
i: integer;
Ms: TMemoryStream;
begin
Size := SizeOf(TInfo);
Info^.FieldName := FFields.Fields[lp].FieldName;
for i := 0 to RecordCount - 1 do
begin
if trim(Items[i].FieldByName(Info^.FieldName).AsString) <> '' then
begin
Info^.RecordNo := i;
Ms := TMemoryStream(Items[i].FieldByName(Info^.FieldName).AsInteger);
Info^.Size := Ms.Size;
Stream.WriteBuffer(Info^, Size);
Ms.Position := 0;
//2010-02-02 mod Stream.CopyFrom(Ms, Info^.Size);
Stream.WriteBuffer(Ms.Memory^, Info^.Size);
end;
end;
end;
begin
New(Info);
try
for lp := 0 to FFields.Count - 1 do
begin
if FFields.Fields[lp].DataType = ftBlob then
begin
SaveOneColBlob;
end;
end;
finally
Dispose(PInfo(Info));
end;
end;
procedure THjhMemoryDataSet.SaveSQLScript(Stream: TStream);
var
SQLInfo: PSQLInfo;
Size: integer;
MemoryStream: TMemoryStream;
begin
New(SQLInfo);
try
Size := SizeOf(TSQLInfo);
SQLInfo^.TableName := '';
SQLInfo^.Size := 0;
if not (FNeedSQLScript and Assigned(SQLScriptSl)) then
begin
Stream.WriteBuffer(SQLInfo^, Size);
end else
begin
SQLInfo^.TableName := FTableName;
MemoryStream := TMemoryStream.Create;
try
SQLScriptSl.SaveToStream(MemoryStream);
SQLInfo^.Size := MemoryStream.Size;
SQLInfo^.TableName := FTableName;
Stream.WriteBuffer(SQLInfo^, Size);
MemoryStream.Position := 0;
Stream.CopyFrom(MemoryStream, SQLInfo^.Size);
finally
MemoryStream.Free;
end;
end;
finally
Dispose(PSQLInfo(SQLInfo));
end;
end;
function THjhMemoryDataSet.SaveToFile(const FileName: string; var ErrStr: string): boolean;
var
FileStream: TFileStream;
MemoryStream: TMemoryStream;
aSize: integer;
begin
//if Encoding = nil then Encoding := TEncoding.UTF8; //2020-08-23 add
Result := false;
try
Save;
if not FNeedProcessBlob then
FSl.SaveToFile(FileName, TEncoding.UTF8)
else begin
FileStream := TFileStream.Create(FileName, fmCreate);
try
MemoryStream := TMemoryStream.Create;
try
FSl.SaveToStream(MemoryStream, TEncoding.UTF8);
aSize := MemoryStream.Size;
FileStream.Write(aSize, SizeOf(aSize));
MemoryStream.Position := 0;
FileStream.CopyFrom(MemoryStream, aSize);
SaveSQLScript(FileStream);
if not BlobToBase64 then
SaveBlob(FileStream);
finally
MemoryStream.Free;
end;
finally
FileStream.Free;
end;
end;
except
on e: exception do
begin
ErrStr := e.Message;
exit;
end;
end;
Result := true;
end;
function THjhMemoryDataSet.SaveToStream(Stream: TStream; var ErrStr: string): boolean;
var
MemoryStream: TMemoryStream;
aSize: integer;
begin
Result := false;
try
Save;
if not FNeedProcessBlob then
FSl.SaveToStream(Stream)
else
begin
MemoryStream := TMemoryStream.Create;
try
FSl.SaveToStream(MemoryStream);
aSize := MemoryStream.Size;
Stream.Write(aSize, SizeOf(aSize));
MemoryStream.Position := 0;
//2010-02-02 mod Stream.CopyFrom(MemoryStream, aSize);
Stream.WriteBuffer(MemoryStream.Memory^, aSize);
SaveSQLScript(Stream);
SaveBlob(Stream);
finally
MemoryStream.Free;
end;
end;
except
on e: exception do
begin
ErrStr := e.Message;
exit;
end;
end;
Result := true;
end;
procedure THjhMemoryDataSet.LoadBlob(Stream: TStream);
var
Info: PInfo;
Size: integer;
MemoryStream: TMemoryStream;
begin
if Stream.Position >= Stream.Size then exit;
New(Info);
try
Size := SizeOf(TInfo);
while Stream.Position < Stream.Size do
begin
FillChar(Info^, Size, 0);
Stream.ReadBuffer(Info^, Size);
MemoryStream := TMemoryStream.Create;
if Info^.Size > 0 then
begin
//2010-02-02 mod
//MemoryStream.CopyFrom(Stream, Info^.Size);
MemoryStream.Size := Info^.Size;
Stream.ReadBuffer(MemoryStream.Memory^, Info^.Size);
Items[Info^.RecordNo].FieldByName(Info^.FieldName).
SetStringValue(IntToStr(LongInt(MemoryStream)));
end;
end;
finally
Dispose(PInfo(Info));
end;
end;