-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMSJet.pas
More file actions
2957 lines (2605 loc) · 76.2 KB
/
MSJet.pas
File metadata and controls
2957 lines (2605 loc) · 76.2 KB
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
unit MSJet;
{$WARN UNSAFE_TYPE OFF}
{$WARN UNSAFE_CODE OFF}
{$WARN UNSAFE_CAST OFF}
interface
{$ALIGN 4}
uses System.Types, Winapi.Windows, System.Classes, System.SysUtils, System.Variants, System.Generics.Collections,
MSJet.Consts, MSJet.API;
type
TMSJetObject = class;
TMSJetSession = class;
TMSJetTable = class;
{$region 'Base classes'}
TMSJetItemState = (itsUnmodified, itsNew, itsModified, itsDelete, itsDeleted);
TMSJetObjectClass = class of TMSJetObject;
TMSJetObjectBaseList = class;
TMSJetObject = class(TPersistent)
private
FName: AnsiString;
FItemState: TMSJetItemState;
FList: TMSJetObjectBaseList;
function GetName: string;
function GetIndex: Integer;
procedure SetIndex(const Value: Integer);
protected
property List: TMSJetObjectBaseList read FList;
public
destructor Destroy; override;
property Index: Integer read GetIndex write SetIndex;
property ItemState: TMSJetItemState read FItemState write FItemState;
published
property Name: string read GetName;
end;
TMSJetObjectBaseList = class(TPersistent)
private
FList: TList;
protected
function DoRemove(Item: TMSJetObject): Integer; virtual; abstract;
end;
TMSJetObjectList<TItem: TMSJetObject> = class(TMSJetObjectBaseList)
private
FItems: TDictionary<AnsiString, TItem>;
function GetCount: Integer; inline;
function Get(Index: Integer): TItem; inline;
procedure Put(Index: Integer; const Value: TItem); inline;
protected
function CreateItem(const Name: string): TItem; virtual; abstract;
function DoRemove(Item: TMSJetObject): Integer; override;
public
constructor Create;
destructor Destroy; override;
function Add(const Name: string): TItem;
function ByName(const Name: string): TItem; inline;
procedure Clear;
procedure Delete(Index: Integer);
function Find(const Name: string): TItem; inline;
function FindOrAdd(const Name: string): TItem;
function Exists(const Name: string): Boolean;
function IndexOf(const Name: string): Integer;
function Insert(Index: Integer; const Name: string): TItem;
function Remove(Item: TItem): Integer; inline;
property Count: Integer read GetCount;
property Items[Index: Integer]: TItem read Get write Put; default;
end;
TMSJetObjectInfo = class(TMSJetObject)
private
FContainer: string;
FFlags: ULONG;
FOptions: ULONG;
published
property Container: string read FContainer write FContainer;
property Options: ULONG read FOptions write FOptions;
property Flags: ULONG read FFlags write FFlags;
end;
TMSJetObjectInfoList = class(TMSJetObjectList<TMSJetObjectInfo>)
protected
function CreateItem(const Name: string): TMSJetObjectInfo; override;
end;
TMSJetMetaObject = class(TMSJetObject)
private
FHandle: ULONG;
FOriginalName: string;
function GetRealName: string; inline;
protected
procedure SetHandle(const Value: JET_COLUMNID); virtual;
protected
procedure DoDbDelete(const Name: AnsiString); virtual; abstract;
procedure DoDbCreate(const Name: AnsiString); virtual; abstract;
procedure DoDbRename(const OldName, NewName: AnsiString); virtual; abstract;
procedure DoDbUpdate(const Name: AnsiString); virtual; abstract;
procedure DoDbReload(const Name: AnsiString); virtual; abstract;
public
procedure DbDelete;
procedure DbCreate;
procedure DbRename(const NewName: string);
procedure DbUpdate;
procedure DbReload;
property Handle: ULONG read FHandle write SetHandle;
property OriginalName: string read FOriginalName write FOriginalName;
property RealName: string read GetRealName;
end;
TMSJetMetaObjectList<TItem: TMSJetMetaObject> = class(TMSJetObjectList<TItem>)
public
procedure Reload; virtual; abstract;
procedure Apply;
end;
TMSJetTableItem = class(TMSJetMetaObject)
private
FTable: TMSJetTable;
protected
public
constructor Create(Table: TMSJetTable); virtual;
property Table: TMSJetTable read FTable;
end;
TMSJetTableItemList<TItem: TMSJetTableItem> = class(TMSJetMetaObjectList<TItem>)
private
FTable: TMSJetTable;
public
constructor Create(Table: TMSJetTable);
property Table: TMSJetTable read FTable;
end;
{$endregion}
{$region 'Jet'}
TMSJetRecoveryOption = (
RecoveryWithoutUndo = JET_bitRecoveryWithoutUndo,
TruncateLogsAfterRecovery = JET_bitTruncateLogsAfterRecovery,
ReplayMissingMapEntryDB = JET_bitReplayMissingMapEntryDB,
LogStreamMustExist = JET_bitLogStreamMustExist,
ReplayIgnoreLostLogs= JET_bitReplayIgnoreLostLogs
);
TMSJetRecoveryOptions = set of TMSJetRecoveryOption;
TMSJetInstance = class(TPersistent)
private
FName: AnsiString;
FHandle: JET_INSTANCE;
FOwnHandle: Boolean;
FPUCW: Word;
FMaxVerPages: Cardinal;
FCircularLog: Boolean;
FRecoveryOptions: TMSJetRecoveryOptions;
function GetName: string;
protected
constructor Create; overload;
procedure BeginCall;
procedure EndCall;
public
constructor Create(const Name: string); overload;
constructor Create(const Handle: JET_INSTANCE); overload;
destructor Destroy; override;
procedure Open;
procedure Close;
function CreateSession: TMSJetSession;
property Name: string read GetName;
property MaxVerPages: Cardinal read FMaxVerPages write FMaxVerPages;
property Handle: JET_INSTANCE read FHandle;
property CircularLog: Boolean read FCircularLog write FCircularLog default True;
property RecoveryOptions: TMSJetRecoveryOptions read FRecoveryOptions write FRecoveryOptions;
end;
TMSJetSession = class(TPersistent)
private
FInstance: TMSJetInstance;
FHandle: JET_SESID;
public
constructor Create(Instance: TMSJetInstance); overload;
constructor Create(Handle: JET_SESID); overload;
destructor Destroy; override;
procedure BeginTransaction;
procedure Commit;
procedure Rollback;
property Instance: TMSJetInstance read FInstance;
property Handle: JET_SESID read FHandle;
end;
TMSJetDefragmentOption = (
BatchStart = JET_bitDefragmentBatchStart,
BatchStop = JET_bitDefragmentBatchStop,
AvailSpaceTreesOnly = JET_bitDefragmentAvailSpaceTreesOnly
);
TMSJetDefragmentOptions = set of TMSJetDefragmentOption;
TMSJetDatabase = class(TPersistent)
private
FSession: TMSJetSession;
FHandle: JET_DBID;
FFileName: AnsiString;
FOwnHandle: Boolean;
function GetFileName: string;
procedure SetFileName(const Value: string);
procedure SetHandle(const Value: JET_DBID);
public
constructor Create(Session: TMSJetSession);
destructor Destroy; override;
function Select(const TableName, IndexName: string; const Keys: array of Variant): TMSJetTable;
procedure CreateDatabase;
procedure Open;
procedure Close;
procedure ReadTableList(Items: TMSJetObjectInfoList);
procedure Defragment(Passes, Seconds: Cardinal; const Options: TMSJetDefragmentOptions = [BatchStart]);
property FileName: string read GetFileName write SetFileName;
property Session: TMSJetSession read FSession;
property Handle: JET_DBID read FHandle write SetHandle;
end;
{$endregion}
{$region 'Columns'}
TMSJetColumnType = (
jetBit = JET_coltypBit,
jetUByte = JET_coltypUnsignedByte,
jetShort = JET_coltypShort,
jetLong = JET_coltypLong,
jetCurrency = JET_coltypCurrency,
jetSingle = JET_coltypIEEESingle,
jetDouble = JET_coltypIEEEDouble,
jetDateTime = JET_coltypDateTime,
jetBinary = JET_coltypBinary,
jetText = JET_coltypText,
jetLongBinary = JET_coltypLongBinary,
jetLongText = JET_coltypLongText,
jetSLV = JET_coltypSLV,
jetULong = JET_coltypUnsignedLong,
jetLongLong = JET_coltypLongLong,
jetGUID = JET_coltypGUID,
jetUShort = JET_coltypUnsignedShort
);
TMSJetColumnOption = (
jetColumnFixed,
jetColumnTagged,
jetColumnNotNULL,
jetColumnVersion,
jetColumnAutoincrement,
jetColumnUpdatable,
jetColumnTTKey,
jetColumnTTDescending,
jetColumnMultiValued,
jetColumnEscrowUpdate,
jetColumnUnversioned,
jetDeleteOnZero,
jetColumnMaybeNull,
jetColumnFinalize,
jetColumnUserDefinedDefault
);
TMSJetColumnOptions = set of TMSJetColumnOption;
EMSJetColumnTypeError = class(EMSJetError)
public
constructor Create(const ColumnName: string; DataType: TMSJetColumnType);
end;
TMSJetColumn = class(TMSJetTableItem)
private
FDataType: TMSJetColumnType;
FSize: Cardinal;
FOptions: TMSJetColumnOptions;
FDisplayName: string;
function GetAsBoolean: Boolean;
function GetAsCurrency: Currency;
function GetAsDateTime: TDateTime;
function GetAsFloat: Double;
function GetAsInt64: Int64;
function GetAsInteger: Integer;
function GetAsSingle: Single;
function GetAsString: string;
function GetIsNull: Boolean;
function GetValue: Variant;
procedure SetAsString(const Value: string);
procedure SetAsInteger(const Value: Integer);
procedure SetAsBoolean(const Value: Boolean);
procedure SetAsCurrency(const Value: Currency);
procedure SetAsDateTime(const Value: TDateTime);
procedure SetAsFloat(const Value: Double);
procedure SetAsInt64(const Value: Int64);
procedure SetAsSingle(const Value: Single);
procedure SetIsNull(const Value: Boolean);
procedure SetValue(const Value: Variant);
function GetAsRaw: RawByteString;
procedure SetAsRaw(const Value: RawByteString);
procedure SetDisplayName(const Value: string);
protected
procedure SetHandle(const Value: JET_COLUMNID); override;
protected
procedure DoDbDelete(const Name: AnsiString); override;
procedure DoDbCreate(const Name: AnsiString); override;
procedure DoDbRename(const OldName, NewName: AnsiString); override;
procedure DoDbUpdate(const Name: AnsiString); override;
procedure DoDbReload(const Name: AnsiString); override;
public
constructor Create(Table: TMSJetTable); override;
procedure AssignTo(Dest: TPersistent); override;
function Retrieve(var Value): ULONG;
procedure Update(const P: Pointer; Size: Cardinal);
property AsBoolean: Boolean read GetAsBoolean write SetAsBoolean;
property AsCurrency: Currency read GetAsCurrency write SetAsCurrency;
property AsDateTime: TDateTime read GetAsDateTime write SetAsDateTime;
property AsFloat: Double read GetAsFloat write SetAsFloat;
property AsInt64: Int64 read GetAsInt64 write SetAsInt64;
property AsInteger: Integer read GetAsInteger write SetAsInteger;
property AsSingle: Single read GetAsSingle write SetAsSingle;
property AsString: string read GetAsString write SetAsString;
property AsRaw: RawByteString read GetAsRaw write SetAsRaw;
property IsNull: Boolean read GetIsNull write SetIsNull;
property Value: Variant read GetValue write SetValue;
published
property DataType: TMSJetColumnType read FDataType write FDataType;
property DisplayName: string read FDisplayName write SetDisplayName;
property Options: TMSJetColumnOptions read FOptions write FOptions;
property Size: Cardinal read FSize write FSize;
end;
TMSJetColumnList = class(TMSJetTableItemList<TMSJetColumn>)
protected
function CreateItem(const Name: string): TMSJetColumn; override;
public
function Add(const Name: string; DataType: TMSJetColumnType; Size: Cardinal = 0; Options: TMSJetColumnOptions = []): TMSJetColumn; overload;
procedure Reload; override;
end;
{$endregion}
{$region 'Index'}
TMSJetIndexOption = (
jetIndexUnique,
jetIndexPrimary,
jetIndexDisallowNull,
jetIndexIgnoreNull,
jetIndexIgnoreAnyNull,
jetIndexIgnoreFirstNull,
jetIndexLazyFlush,
jetIndexEmpty,
jetIndexUnversioned,
jetIndexSortNullsHigh,
jetIndexUnicode,
jetIndexTuples,
jetIndexTupleLimits,
jetIndexCrossProduct,
jetIndexKeyMost,
jetIndexDisallowTruncation,
jetIndexNestedTable
);
TMSJetIndexOptions = set of TMSJetIndexOption;
TMSJetIndex = class(TMSJetTableItem)
private
FFields: string;
FKey: AnsiString;
FDensity: Cardinal;
FOptions: TMSJetIndexOptions;
FColumns: TList;
procedure SetFields(const Value: string);
function GetColumn(const Index: Integer): TMSJetColumn; inline;
function GetColumnCount: Integer; inline;
protected
procedure DoDbDelete(const Name: AnsiString); override;
procedure DoDbCreate(const Name: AnsiString); override;
procedure DoDbRename(const OldName, NewName: AnsiString); override;
procedure DoDbUpdate(const Name: AnsiString); override;
procedure DoDbReload(const Name: AnsiString); override;
public
constructor Create(Table: TMSJetTable); override;
destructor Destroy; override;
procedure AssignTo(Dest: TPersistent); override;
property Key: AnsiString read FKey;
property ColumnCount: Integer read GetColumnCount;
property Columns[const Index: Integer]: TMSJetColumn read GetColumn;
published
property Fields: string read FFields write SetFields;
property Options: TMSJetIndexOptions read FOptions write FOptions;
property Density: Cardinal read FDensity write FDensity;
end;
TMSJetIndexList = class(TMSJetTableItemList<TMSJetIndex>)
protected
function CreateItem(const Name: string): TMSJetIndex; override;
public
function Add(const Name, Columns: string; Options: TMSJetIndexOptions = []): TMSJetIndex; overload;
procedure Reload; override;
function FindByKey(const Key: string): TMSJetIndex;
end;
{$endregion}
{$region 'Table'}
TMSJetMove = (
jetMoveFirst = JET_MoveFirst,
jetMovePreious = JET_MovePrevious,
jetMoveNext = JET_MoveNext,
jetMoeLast = JET_MoveLast
);
TMSJetKeyOption = (
jetNewKey,
jetStrLimit,
jetSubStrLimit,
jetNormalizedKey,
jetKeyDataZeroLength,
jetFullColumnStartLimit,
jetFullColumnEndLimit,
jetPartialColumnStartLimit,
jetPartialColumnEndLimit
);
TMSJetKeyOptions = set of TMSJetKeyOption;
TMSJetSeekOption = (
jetSeekEQ = JET_bitSeekEQ,
jetSeekLT = JET_bitSeekLT,
jetSeekLE = JET_bitSeekLE,
jetSeekGE = JET_bitSeekGE,
jetSeekGT = JET_bitSeekGT
);
TMSJetSeekOptions = set of TMSJetSeekOption;
JetBookmark = interface
['{03580CAE-6536-4F51-B440-F5BA939277C9}']
function Go: Boolean;
end;
TJetBookmark = class(TInterfacedObject, JetBookmark)
private
FData: Pointer;
FSize: ULONG;
FSecondaryData: Pointer;
FSecondarySize: ULONG;
FTable: TMSJetTable;
protected
function Go: Boolean;
public
constructor Create(Table: TMSJetTable; const Data: Pointer; const Size: ULONG; const SecondaryData: Pointer; const SecondarySize: ULONG);
destructor Destroy; override;
end;
TMSJetJoin = class(TPersistent)
private
FTable: TMSJetTable;
FParentKey: string;
FParent: TMSJetTable;
FIsSync: Boolean;
FColumns: TArray<string>;
function GetValue(const Name: string): Variant;
public
constructor Create(Parent: TMSJetTable; const TableName, Key, ParentKey: string);
destructor Destroy; override;
function Sync(IsSync: Boolean): Boolean;
property Table: TMSJetTable read FTable;
property Parent: TMSJetTable read FParent;
property ParentKey: string read FParentKey;
property IsSync: Boolean read FIsSync;
property Columns: TArray<string> read FColumns write FColumns;
property Value[const Name: string]: Variant read GetValue; default;
end;
TMSJetTableFilter = reference to function(const Table: TMSJetTable): Boolean;
TMSJetTable = class(TMSJetMetaObject)
private
FColumns: TMSJetColumnList;
FDatabase: TMSJetDatabase;
FDensity: Cardinal;
FIndexes: TMSJetIndexList;
FIndexName: AnsiString;
FIsInsert: Boolean;
FJoins: TDictionary<string, TMSJetJoin>;
FKey: Pointer;
FKeySize: ULONG;
FPages: Cardinal;
FSeekKeys: array of Variant;
FSeekOption: TMSJetSeekOption;
FSeekSetRange: Boolean;
function GetActive: Boolean;
function GetColumn(const ColumnName: string): TMSJetColumn;
function GetIndexName: string;
function GetJoinCount: Integer;
function GetJoins: TArray<TMSJetJoin>;
function GetLink(const ParentKey: string): TMSJetJoin;
procedure SetColumns(const Value: TMSJetColumnList);
procedure SetIndexes(const Value: TMSJetIndexList);
procedure SetIndexName(const Value: string);
protected
function GetColumnText(id: JET_COLUMNID): string;
procedure MakeKey(Column: TMSJetColumn; const Value: Variant; Options: TMSJetKeyOptions);
function CreatePrimaryBookmark: JetBookmark;
function CreateSecondaryBookmark: JetBookmark;
procedure SyncCursor(HasCursor: Boolean);
protected
procedure DoDbDelete(const Name: AnsiString); override;
procedure DoDbCreate(const Name: AnsiString); override;
procedure DoDbRename(const OldName, NewName: AnsiString); override;
procedure DoDbUpdate(const Name: AnsiString); override;
procedure DoDbReload(const Name: AnsiString); override;
property IsInsert: Boolean read FIsInsert;
public
constructor Create(Database: TMSJetDatabase; const Name: string); overload;
constructor Create(Database: TMSJetDatabase; Handle: JET_TABLEID); overload;
destructor Destroy; override;
procedure Open;
procedure Close;
function First: Boolean; overload;
function First(const Filter: TMSJetTableFilter): Boolean; overload;
function Last: Boolean; overload;
function Last(const Filter: TMSJetTableFilter): Boolean; overload;
function Next: Boolean; overload;
function Next(const Filter: TMSJetTableFilter): Boolean; overload;
function Previous: Boolean; overload;
function Previous(const Filter: TMSJetTableFilter): Boolean; overload;
procedure Insert;
procedure Edit;
procedure Cancel;
procedure Post;
procedure Delete;
function AddColumn(const Name: string; DataType: TMSJetColumnType; Size: ULONG = 0; Options: TMSJetColumnOptions = []): TMSJetColumn;
function Seek(const Keys: array of Variant; Option: TMSJetSeekOption = jetSeekEQ; SetRange: Boolean = False; const Filter: TMSJetTableFilter = nil): Boolean;
procedure ClearRange;
function RetrieveKey: Boolean;
function GoToKey: Boolean;
function CreateBookmark: JetBookmark;
function GotoBookmark(const Bookmark: JetBookmark): Boolean;
function Join(const TableName, Key, ParentKey: string): TMSJetJoin;
property Active: Boolean read GetActive;
property IndexName: string read GetIndexName write SetIndexName;
property Cols[const ColumnName: string]: TMSJetColumn read GetColumn; default;
property Link[const ParentKey: string]: TMSJetJoin read GetLink;
property Joins: TArray<TMSJetJoin> read GetJoins;
property JoinCount: Integer read GetJoinCount;
published
property Database: TMSJetDatabase read FDatabase;
property Columns: TMSJetColumnList read FColumns write SetColumns;
property Indexes: TMSJetIndexList read FIndexes write SetIndexes;
property Pages: Cardinal read FPages write FPages;
property Density: Cardinal read FDensity write FDensity;
end;
TMSJetTableList = class(TMSJetMetaObjectList<TMSJetTable>)
private
FDatabase: TMSJetDatabase;
protected
function CreateItem(const Name: string): TMSJetTable; override;
public
constructor Create(Database: TMSJetDatabase);
property Database: TMSJetDatabase read FDatabase;
end;
{$endregion}
{$region 'Utils'}
const
MSJetTypeNames: array[TMSJetColumnType] of string = (
'Bit',
'UByte',
'Short',
'Long',
'Currency',
'Single',
'Double',
'DateTime',
'Binary',
'Text',
'LongBinary',
'LongText',
'SLV',
'ULong',
'LongLong',
'GUID',
'UShort'
);
MSJetColumnOptionNames: array[TMSJetColumnOption] of string = (
'Fixed',
'Tagged',
'NotNULL',
'Version',
'Autoincrement',
'Updatable',
'TTKey',
'TTDescending',
'MultiValued',
'EscrowUpdate',
'Unversioned',
'DeleteOnZero',
'MaybeNull',
'Finalize',
'UserDefinedDefault'
);
MSJetIndexOptionNames: array[TMSJetIndexOption] of string = (
'Unique',
'Primary',
'DisallowNull',
'IgnoreNull',
'IgnoreAnyNull',
'IgnoreFirstNull',
'LazyFlush',
'Empty',
'Unversioned',
'SortNullsHigh',
'Unicode',
'Tuples',
'TupleLimits',
'CrossProduct',
'KeyMost',
'DisallowTruncation',
'NestedTable'
);
function SetToInt(const Value): Cardinal;
procedure IntToSet(Value: Cardinal; out Flags);
{$endregion}
const
ColumnTypeNames: array[TMSJetColumnType] of string = (
'Bit',
'UByte',
'Short',
'Long',
'Currency',
'Single',
'Double',
'DateTime',
'Binary',
'Text',
'LongBinary',
'LongText',
'SLV',
'ULong',
'LongLong',
'GUID',
'UShort'
);
function ColumnTypeToName(const DataType: TMSJetColumnType): string; inline;
function NameToColumnType(const Name: string): TMSJetColumnType; inline;
function NameToColumnOption(const Name: string): TMSJetColumnOption; inline;
function NameToColumnOptions(const Name: string): TMSJetColumnOptions;
function ColumnOptionToName(const Option: TMSJetColumnOption): string; inline;
function ColumnOptionsToName(const Options: TMSJetColumnOptions): string;
function NameToIndexOption(const Name: string): TMSJetIndexOption; inline;
function NameToIndexOptions(const Text: string): TMSJetIndexOptions;
function IndexOptionToName(const Option: TMSJetIndexOption): string; inline;
function IndexOptionsToName(const Options: TMSJetIndexOptions): string;
resourcestring
SJetInvalidColumnType = 'Invalid column type "%s"';
SJetInvalidOption = 'Invalid option "%"';
SJetItemByNameNotFound = 'Item with name "%s" not foud';
SJetIndexForColumnNotFound = 'Index for column "%s" not found';
implementation
uses StrUtils;
function ColumnTypeToName(const DataType: TMSJetColumnType): string;
begin
Result := ColumnTypeNames[DataType];
end;
function NameToColumnType(const Name: string): TMSJetColumnType;
var
I: TMSJetColumnType;
begin
for I := Low(TMSJetColumnType) to High(TMSJetColumnType) do
if ColumnTypeNames[I] = Name then
begin
Result := I;
Exit;
end;
raise EMSJetError.CreateResFmt(@SJetInvalidColumnType, [Name]);
end;
function NameToColumnOption(const Name: string): TMSJetColumnOption;
begin
if Name = 'Fixed' then
Result := jetColumnFixed
else if Name = 'Tagged' then
Result := jetColumnTagged
else if Name = 'NotNull' then
Result := jetColumnNotNULL
else if Name = 'Versioned' then
Result := jetColumnVersion
else if Name = 'AutoIncrement' then
Result := jetColumnAutoincrement
else if Name = 'Updatable' then
Result := jetColumnUpdatable
else
raise EMSJetError.CreateResFmt(@SJetInvalidOption, [Name]);
end;
function NameToColumnOptions(const Name: string): TMSJetColumnOptions;
var
OptionList: TStringDynArray;
Option: string;
begin
Result := [];
OptionList := SplitString(Name, ',');
if OptionList <> nil then
for Option in OptionList do
Include(Result, NameToColumnOption(Trim(Option)));
end;
function ColumnOptionToName(const Option: TMSJetColumnOption): string;
begin
case Option of
jetColumnFixed:
Result := 'Fixed';
jetColumnTagged:
Result := 'Tagged';
jetColumnNotNULL:
Result := 'NotNull';
jetColumnVersion:
Result := 'Versioned';
jetColumnAutoincrement:
Result := 'AutoIncrement';
jetColumnUpdatable:
Result := 'Updatable';
else
Result := '';
end;
end;
function ColumnOptionsToName(const Options: TMSJetColumnOptions): string;
var
Option: TMSJetColumnOption;
Name: string;
begin
Result := '';
for Option := Low(TMSJetColumnOption) to High(TMSJetColumnOption) do
begin
if Option in Options then
begin
Name := ColumnOptionToName(Option);
if Name <> '' then
begin
if Result = '' then
Result := Name
else
Result := Result + ', ' + Name;
end;
end;
end;
end;
function NameToIndexOption(const Name: string): TMSJetIndexOption;
begin
if Name = 'Unique' then
Result := jetIndexUnique
else if Name = 'Primary' then
Result := jetIndexPrimary
else if Name = 'DisallowNull' then
Result := jetIndexDisallowNull
else if Name = 'IgnoreNull' then
Result := jetIndexIgnoreNull
else if Name = 'IgnoreAnyNull' then
Result := jetIndexIgnoreAnyNull
else if Name = 'IgnoreFirstNull' then
Result := jetIndexIgnoreFirstNull
else if Name = 'LazyFlush' then
Result := jetIndexLazyFlush
else if Name = 'Empty' then
Result := jetIndexEmpty
else if Name = 'SortNullsHigh' then
Result := jetIndexSortNullsHigh
else
raise EMSJetError.CreateResFmt(@SJetInvalidOption, [Name]);
end;
function NameToIndexOptions(const Text: string): TMSJetIndexOptions;
var
OptionList: TStringDynArray;
Option: string;
begin
Result := [];
OptionList := SplitString(Text, ',');
if OptionList <> nil then
for Option in OptionList do
Include(Result, NameToIndexOption(Trim(Option)));
end;
function IndexOptionToName(const Option: TMSJetIndexOption): string;
begin
case Option of
jetIndexUnique:
Result := 'Unique';
jetIndexPrimary:
Result := 'Primary';
jetIndexDisallowNull:
Result := 'DisallowNull';
jetIndexIgnoreNull:
Result := 'IgnoreNull';
jetIndexIgnoreAnyNull:
Result := 'IgnoreAnyNull';
jetIndexIgnoreFirstNull:
Result := 'IgnoreFirstNull';
jetIndexLazyFlush:
Result := 'LazyFlush';
jetIndexEmpty:
Result := 'Empty';
jetIndexSortNullsHigh:
Result := 'SortNullsHigh';
else
Result := '';
end;
end;
function IndexOptionsToName(const Options: TMSJetIndexOptions): string;
var
Option: TMSJetIndexOption;
Name: string;
begin
Result := '';
for Option := Low(TMSJetIndexOption) to High(TMSJetIndexOption) do
begin
if Option in Options then
begin
Name := IndexOptionToName(Option);
if Name <> '' then
begin
if Result = '' then
Result := Name
else
Result := Result + ', ' + Name;
end;
end;
end;
end;
function SetToInt(const Value): Cardinal;
begin
Result := PDWord(@Value)^;
end;
procedure IntToSet(Value: Cardinal; out Flags);
begin
PDWord(@Flags)^ := Value;
end;
{ EMSJetColumnTypeError }
constructor EMSJetColumnTypeError.Create(const ColumnName: string; DataType: TMSJetColumnType);
begin
inherited Create('Could not access column "' + ColumnName + '" as "' + MSJetTypeNames[DataType] + '"');
end;
{ TMSJetObject }
destructor TMSJetObject.Destroy;
begin
if FList <> nil then
FList.DoRemove(Self);
inherited Destroy;
end;
function TMSJetObject.GetIndex: Integer;
begin
Result := List.FList.IndexOf(Self);
end;
function TMSJetObject.GetName: string;
begin
Result := string(FName);
end;
procedure TMSJetObject.SetIndex(const Value: Integer);
begin
List.FList.Move(Index, Value);
end;
{ TMSJetObjectList<TItem> }
constructor TMSJetObjectList<TItem>.Create;
begin
inherited Create;
FList := TList.Create;
FItems := TDictionary<AnsiString, TItem>.Create;
end;
destructor TMSJetObjectList<TItem>.Destroy;
begin
Clear;
FreeAndNil(FList);
FreeAndNil(FItems);
inherited;
end;
procedure TMSJetObjectList<TItem>.Clear;
var
I: Integer;
begin
for I := 0 to FList.Count - 1 do
begin
Items[I].Flist := nil;
Items[I].Free;
end;
FList.Clear;
FItems.Clear;
end;
function TMSJetObjectList<TItem>.IndexOf(const Name: string): Integer;
var
I: Integer;
begin
for I := 0 to Count - 1 do
if Items[I].Name = Name then
begin
Result := I;
Exit;
end;
Result := -1;
end;
function TMSJetObjectList<TItem>.Find(const Name: string): TItem;
begin
if not FItems.TryGetValue(AnsiString(Name), Result) then
Result := nil;
end;
function TMSJetObjectList<TItem>.Exists(const Name: string): Boolean;
var
Item: TItem;
begin
Result := FItems.TryGetValue(AnsiString(Name), Item);
end;
function TMSJetObjectList<TItem>.ByName(const Name: string): TItem;
begin
Result := Find(Name);
if Result = nil then
raise EMSJetError.CreateResFmt(@SJetItemByNameNotFound, [Name]);
end;
function TMSJetObjectList<TItem>.FindOrAdd(const Name: string): TItem;
begin
Result := Find(Name);
if Result = nil then
Result := Add(Name);
end;
function TMSJetObjectList<TItem>.Add(const Name: string): TItem;
begin
Result := CreateItem(Name);
Result.ItemState := itsNew;
Result.FList := Self;
FList.Add(Pointer(Result));
FItems.Add(AnsiString(Name), Result);
end;
function TMSJetObjectList<TItem>.Insert(Index: Integer; const Name: string): TItem;
begin
Result := CreateItem(Name);
Result.ItemState := itsNew;
Result.FList := Self;
FList.Insert(Index, Pointer(Result));
FItems.Add(AnsiString(Name), Result);
end;
procedure TMSJetObjectList<TItem>.Delete(Index: Integer);
begin
FItems.Remove(AnsiString(Items[Index].Name));
Items[Index].FList := nil;
Items[Index].Free;
FList.Delete(Index);
end;
function TMSJetObjectList<TItem>.DoRemove(Item: TMSJetObject): Integer;
begin
Result := FList.IndexOf(Pointer(Item));
if Result >= 0 then
begin
FItems.Remove(AnsiString(Items[Result].Name));
FList.Delete(Result);
end;
end;