-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathREST.TMS.myCloudDataRestClient.Data.pas
More file actions
2936 lines (2600 loc) · 83.7 KB
/
REST.TMS.myCloudDataRestClient.Data.pas
File metadata and controls
2936 lines (2600 loc) · 83.7 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
{ *************************************************************************** }
{ TMS myCloudData REST Client data structures }
{ for Delphi & C++Builder }
{ }
{ written by TMS Software }
{ copyright © 2016 }
{ Email : info@tmssoftware.com }
{ Web : http://www.tmssoftware.com }
{ }
{ The source code is given as is. The author is not responsible }
{ for any possible damage done due to the use of this code. }
{ The component can be freely used in any application. The complete }
{ source code remains property of the author and may not be distributed, }
{ published, given or sold in any form as such. No parts of the source }
{ code can be included in any other component or application without }
{ written authorization of the author. }
{ *************************************************************************** }
unit REST.TMS.myCloudDataRestClient.Data;
interface
{$M+}
uses
Classes,
SysUtils,
System.Variants,
System.Generics.Collections,
System.JSON,
REST.Client,
REST.HttpClient;
type
// The actual Client
TCustomMyCloudDataRESTClient = class;
// Model base classes
TmyCloudDataModelBase = class;
TmyCloudDataModelCollectionBase<T: class> = class;
// Model classes
TmyCloudDataBlob = class;
TmyCloudDataEntities = class;
TmyCloudDataEntity = class;
TmyCloudDataEntityFilter = class;
TmyCloudDataEntityFilters = class;
TmyCloudDataEntityQuery = class;
TmyCloudDataEntityQueryResult = class;
TmyCloudDataEntitySorting = class;
TmyCloudDataEntitySortingCollection = class;
TmyCloudDataField = class;
TmyCloudDataFieldMetaData = class;
TmyCloudDataFields = class;
TmyCloudDataLookupData = class;
TmyCloudDataLookupDataCollection = class;
TmyCloudDataTable = class;
TmyCloudDataPermissions = class;
TmyCloudDataTables = class;
TmyCloudDataTableShare = class;
TmyCloudDataTableShares = class;
TmyCloudDataUser = class;
{ ------------------------------------------------------------------------- }
TCustomMyCloudDataRESTClient = class(TRESTClient)
private
FRequest: TRESTRequest;
FTables: TmyCloudDataTables;
function GetTables: TmyCloudDataTables;
protected
FCurrentUser: TmyCloudDataUser;
FIsConnected: Boolean;
function GetTable(ATableId: integer): TmyCloudDataTable;
property Request: TRESTRequest read FRequest;
function GetHttpClient: TRESTHTTP;
procedure EnsureConnectedState; virtual;
procedure HandleMyCloudDataErrorResponse(AResponse: TCustomRESTResponse);
procedure FreeLocalData;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property IsConnected: Boolean read FIsConnected;
property CurrentUser: TmyCloudDataUser read FCurrentUser;
property Tables: TmyCloudDataTables read GetTables;
end;
TmyCloudDataModelBase = class abstract(TPersistent)
private
FOwner: TCustomMyCloudDataRESTClient;
public
constructor Create(AOwner: TCustomMyCloudDataRESTClient); virtual;
property Owner: TCustomMyCloudDataRESTClient read FOwner;
end;
TmyCloudDataUserType = (utFree, utSubscription, utAdmin);
TmyCloudDataUser = class(TPersistent)
private
FUserId: integer;
FFirstName: string;
FLastName: string;
FEmail: string;
FCompany: string;
FUserType: TmyCloudDataUserType;
FPermissions: TmyCloudDataPermissions;
public
destructor Destroy; override;
function CanUseBlobFields: Boolean;
function FromJSON(ASource: string): Boolean;
function UserTypeAsString: string;
property Company: string read FCompany;
property Email: string read FEmail;
property FirstName: string read FFirstName;
property Lastname: string read FLastName;
property Permission: TmyCloudDataPermissions read FPermissions;
property UserId: integer read FUserId;
property UserType: TmyCloudDataUserType read FUserType;
end;
TmyCloudDataModelCollectionBase<T: class> = class abstract(TmyCloudDataModelBase)
private
FItems: TObjectList<T>;
function GetItem(i: integer): T;
function GetItemCount: integer;
function GetItems: TObjectList<T>;
protected
function LoadItems: TObjectList<T>; virtual;
property Items: TObjectList<T> read GetItems;
public
constructor Create(AOwner: TCustomMyCloudDataRESTClient); reintroduce; virtual;
destructor Destroy; override;
function GetEnumerator: TEnumerator<T>;
procedure Reset; virtual;
property Item[i: integer]: T read GetItem; default;
property Count: integer read GetItemCount;
end;
TmyCloudDataFieldDataType = (ftInteger, ftFloat, ftWideString, ftBoolean, ftDateTime, ftDate, ftTime, ftBlob);
TmyCloudDataFieldMetaTypedField = (tfNone, tfRadioButton, tfComboBox, tfCheckBox);
TmyCloudDataFieldMetaData = class(TObject)
private
FEnabled: Boolean;
FLabelText: string;
FLookupTable: int64;
FMask: string;
FWidth: integer;
FMaximum: double;
FOrder: integer;
FMinimumDate: TDatetime;
FMaximumDate: TDatetime;
FLookupKeyField: string;
FLookupField: string;
FVisible: Boolean;
FDescription: string;
FMinimum: double;
FTypedValues: TDictionary<string, string>;
FTypedField: TmyCloudDataFieldMetaTypedField;
FDefaultValue: string;
FRequired: Boolean;
public
constructor Create;
destructor Destroy; override;
function Equals(Obj: TObject): Boolean; override;
property DefaultValue: string read FDefaultValue write FDefaultValue;
property Description: string read FDescription write FDescription;
property Enabled: Boolean read FEnabled write FEnabled;
property LabelText: string read FLabelText write FLabelText;
property LookupField: string read FLookupField write FLookupField;
property LookupKeyField: string read FLookupKeyField write FLookupKeyField;
property LookupTable: int64 read FLookupTable write FLookupTable;
property Mask: string read FMask write FMask;
property Maximum: double read FMaximum write FMaximum;
property MaximumDate: TDatetime read FMaximumDate write FMaximumDate;
property Minimum: double read FMinimum write FMinimum;
property MinimumDate: TDatetime read FMinimumDate write FMinimumDate;
property Order: integer read FOrder write FOrder;
property Required: Boolean read FRequired write FRequired;
property TypedField: TmyCloudDataFieldMetaTypedField read FTypedField write FTypedField;
property TypedValues: TDictionary<string, string> read FTypedValues write FTypedValues;
property Visible: Boolean read FVisible write FVisible;
property Width: integer read FWidth write FWidth;
end;
TmyCloudDataField = class(TObject)
private
FTableID: integer;
FFieldName: string;
FOriginalFieldName: string;
FDataType: TmyCloudDataFieldDataType;
FIsNullable: Boolean;
FMaxFieldSize: integer;
FMetaData: TmyCloudDataFieldMetaData;
function CheckForExtraMetaData: Boolean;
function StringToDataType(AString: string): TmyCloudDataFieldDataType; virtual;
function DataTypeToString(ADataType: TmyCloudDataFieldDataType): string; virtual;
protected
function FromJSON(AJSONString: string): Boolean;
function ToJSON(AIsNew: Boolean): string;
function ToMetaDataJSON: string;
public
constructor Create(ATableId: integer);
destructor Destroy; override;
function CompareBasicFields(AOtherField: TmyCloudDataField): Boolean;
function CompareExtraMetaData(AOtherField: TmyCloudDataField): Boolean;
property DataType: TmyCloudDataFieldDataType read FDataType write FDataType;
property FieldName: string read FFieldName write FFieldName;
property HasExtraMetaData: Boolean read CheckForExtraMetaData;
property IsNullable: Boolean read FIsNullable write FIsNullable;
property MaxFieldSize: integer read FMaxFieldSize write FMaxFieldSize;
property MetaData: TmyCloudDataFieldMetaData read FMetaData write FMetaData;
property OriginalFieldName: string read FOriginalFieldName;
property TableID: integer read FTableID;
end;
TmyCloudDataFields = class(TmyCloudDataModelCollectionBase<TmyCloudDataField>)
private
FTableID: integer;
protected
function LoadItems: TObjectList<TmyCloudDataField>; override;
function TryParseJSON(AJSONString: string; out AResult: TObjectList<TmyCloudDataField>): Boolean;
public
constructor Create(AOwner: TCustomMyCloudDataRESTClient; ATableId: integer); reintroduce; overload;
function Add(AFieldName: string; ADataType: TmyCloudDataFieldDataType): TmyCloudDataField; overload;
function Add(AFieldName: string; ADataType: TmyCloudDataFieldDataType; AFieldSize: integer): TmyCloudDataField; overload;
function AddOrUpdate(AFieldName: string; ADataType: TmyCloudDataFieldDataType): TmyCloudDataField; overload;
function AddOrUpdate(AFieldName: string; ADataType: TmyCloudDataFieldDataType; AFieldSize: integer): TmyCloudDataField; overload;
function GetByName(AFieldName: string): TmyCloudDataField;
function GetByOriginalName(AOriginalFieldName: string): TmyCloudDataField;
procedure Save;
property TableID: integer read FTableID;
end;
TmyCloudDataBlob = class(TmyCloudDataModelBase)
private
FHasData: Boolean;
FUrl: string;
protected
function GetStream: TStream;
procedure SetStream(AStream: TStream);
function FromJSON(AJSONString: string): Boolean;
public
function AsString: string;
function ToFile(AFileName: string): Boolean;
procedure FromFile(AFileName: string);
property HasData: Boolean read FHasData;
property Stream: TStream read GetStream write SetStream;
end;
TmyCloudDataJSonHelper = class
public
class function TryGetJSonProperty<T>(AJSonValue: TJSONValue; APath: string; ADefaultValue: T): T; overload;
class function TryGetJSonProperty<T>(AJSonValue: TJSONValue; APath: string): T; overload;
class function TryParseBooleanFieldValue(ASource: string; out AResult: Variant): Boolean;
class function TryParseDateTimeFieldValue(ASource: string; out AResult: Variant): Boolean;
class function TryParseFieldValue(ASource: string; ADataType: TmyCloudDataFieldDataType; out AResult: Variant): Boolean;
class function TryParseFloatFieldValue(ASource: string; out AResult: Variant): Boolean;
class function TryParseIntegerFieldValue(ASource: string; out AResult: Variant): Boolean;
class function TrySerializeBooleanFieldValue(AValue: Variant; out AStringResult: string): Boolean;
class function TrySerializeDateTimeFieldValue(AValue: Variant; out AStringResult: string): Boolean;
class function TrySerializeFieldValue(AValue: Variant; ADataType: TmyCloudDataFieldDataType; out AStringResult: string): Boolean;
class function TrySerializeFloatFieldValue(AValue: Variant; out AStringResult: string): Boolean;
class function TrySerializeIntegerFieldValue(AValue: Variant; out AStringResult: string): Boolean;
class function TrySerializeStringFieldValue(AValue: Variant; out AStringResult: string): Boolean;
class function IntToZStr(i, l: integer): string;
end;
TmyCloudDataEntity = class(TmyCloudDataModelBase)
private
FBlobValues: TObjectDictionary<string, TmyCloudDataBlob>;
FEntityId: int64;
FFields: TmyCloudDataFields;
FHasUnsavedChanges: Boolean;
FTableID: integer;
FValues: TDictionary<string, Variant>;
protected
function GetIsNew: Boolean;
protected
function FromJSON(AJSONString: string; AFieldCollection: TmyCloudDataFields): Boolean;
function ToJSON: string;
public
constructor Create(AOwner: TCustomMyCloudDataRESTClient; ATableId: integer; AFieldSet: TmyCloudDataFields); reintroduce; overload;
destructor Destroy; override;
function GetBlobField(AFieldName: string): TmyCloudDataBlob;
function GetValue(AFieldName: string): Variant;
function GetValueAsString(AFieldName: string): string;
function TrySetValueFromString(AFieldName: string; AValue: string): Boolean;
procedure SetValue(AFieldName: string; AValue: Variant);
property HasUnsavedChanges: Boolean read FHasUnsavedChanges;
property ID: int64 read FEntityId;
property IsNew: Boolean read GetIsNew;
property TableID: integer read FTableID;
end;
TmyCloudDataEntities = class(TmyCloudDataModelCollectionBase<TmyCloudDataEntity>)
private
FTableID: integer;
FFieldCollection: TmyCloudDataFields;
FRemovedEntities: TList<int64>;
protected
function FromJSON(AJSONString: string): Boolean;
public
constructor Create(AOwner: TCustomMyCloudDataRESTClient; ATableId: integer; AFieldCollection: TmyCloudDataFields); reintroduce; overload;
destructor Destroy; override;
function CreateEntity: TmyCloudDataEntity;
function GetEntity(AEntityId: int64): TmyCloudDataEntity;
procedure RemoveEntity(AEntityId: int64);
procedure Save;
property TableID: integer read FTableID;
end;
TComparisonOperator = (coEqual, coNotEqual, coLike, coGreater, coGreaterOrEqual, coLess, coLessOrEqual, coStartsWith, coEndsWith, coNull,
coNotNull);
TLogicalOperator = (loAnd, loOr, loNone);
TmyCloudDataEntityFilter = class
private
FFieldName: string;
FValue: Variant;
FComparisonOperator: TComparisonOperator;
FLogicalOperator: TLogicalOperator;
function OperatorAsString(AOperator: TComparisonOperator): string; overload;
function OperatorAsString(AOperator: TLogicalOperator): string; overload;
protected
function ToJSON: TJSONObject;
public
property ComparisonOperator: TComparisonOperator read FComparisonOperator write FComparisonOperator;
property FieldName: string read FFieldName write FFieldName;
property LogicalOperator: TLogicalOperator read FLogicalOperator write FLogicalOperator;
property Value: Variant read FValue write FValue;
end;
TmyCloudDataEntityFilters = class(TObjectList<TmyCloudDataEntityFilter>)
public
function Add(AFieldName: string; AValue: Variant; AComparisonOperator: TComparisonOperator = coEqual; ALogicalOperator: TLogicalOperator = loAnd)
: integer; overload;
end;
TSortDirection = (soAscending, soDescending);
TmyCloudDataEntitySorting = class
private
FFieldName: string;
FSortOrder: TSortDirection;
function SortOrderAsString(ASortOrder: TSortDirection): string;
protected
function ToJSON: TJSONObject;
public
property FieldName: string read FFieldName write FFieldName;
property SortDirection: TSortDirection read FSortOrder write FSortOrder;
end;
TmyCloudDataEntitySortingCollection = class(TObjectList<TmyCloudDataEntitySorting>)
public
function Add(AFieldName: string; AOrder: TSortDirection): integer; overload;
end;
TmyCloudDataEntityQuery = class
private
FTableID: integer;
FPageSize: integer;
FPageIndex: integer;
FFilters: TmyCloudDataEntityFilters;
FSorting: TmyCloudDataEntitySortingCollection;
FFields: TList<string>;
function GetIsPaged: Boolean;
protected
function ToJSON: string;
public
constructor Create(ATableId: integer);
destructor Destroy; override;
procedure Reset;
property Fields: TList<string> read FFields write FFields;
property Filters: TmyCloudDataEntityFilters read FFilters write FFilters;
property IsPaged: Boolean read GetIsPaged;
property PageIndex: integer read FPageIndex write FPageIndex;
property PageSize: integer read FPageSize write FPageSize;
property Sorting: TmyCloudDataEntitySortingCollection read FSorting write FSorting;
property TableID: integer read FTableID;
end;
TmyCloudDataEntityQueryResult = class(TObjectList<TmyCloudDataEntity>)
private
FPageSize: integer;
FPageIndex: integer;
FTotalResults: integer;
FResults: TmyCloudDataEntities;
published
destructor Destroy; override;
property Entities: TmyCloudDataEntities read FResults write FResults;
property PageSize: integer read FPageSize write FPageSize;
property PageIndex: integer read FPageIndex write FPageIndex;
property TotalResults: integer read FTotalResults write FTotalResults;
end;
TmyCloudDataPermission = (pCreate, pRead, pUpdate, pDelete);
TmyCloudDataPermissions = class(TList<TmyCloudDataPermission>)
protected
procedure SetFromString(ASource: string);
public
function ToString: string; override;
function IsSameAs(AOther: TmyCloudDataPermissions): Boolean;
class function FromString(ASource: string): TmyCloudDataPermissions;
class function ReadOnly: TmyCloudDataPermissions;
class function ReadWrite: TmyCloudDataPermissions;
class function None: TmyCloudDataPermissions;
end;
TmyCloudDataTableShare = class
private
FEmail: string;
FPermissions: TmyCloudDataPermissions;
FTableID: integer;
FHasUnchangedChanges: Boolean;
procedure SetPermissions(const Value: TmyCloudDataPermissions);
public
constructor Create(ATableId: integer; AEmail: string; APermissions: TmyCloudDataPermissions; IsNew: Boolean = True);
published
property Email: string read FEmail;
property HasUnsavedChanges: Boolean read FHasUnchangedChanges;
property Permissions: TmyCloudDataPermissions read FPermissions write SetPermissions;
property TableID: integer read FTableID;
end;
TmyCloudDataTableShares = class(TmyCloudDataModelCollectionBase<TmyCloudDataTableShare>)
private
FTableID: integer;
protected
function LoadItems: TObjectList<TmyCloudDataTableShare>; override;
function TryParseJSON(AJSONString: string; out AResult: TObjectList<TmyCloudDataTableShare>): Boolean;
public
constructor Create(AOwner: TCustomMyCloudDataRESTClient; ATableId: integer); reintroduce; overload;
function GetShare(AEmail: string): TmyCloudDataTableShare;
procedure RemoveShare(AEmail: string);
procedure Save;
procedure SetShare(AEmail: string; APermissions: TmyCloudDataPermissions);
end;
TmyCloudDataLookupData = class(TDictionary<Variant, Variant>)
end;
TmyCloudDataLookupDataCollection = class(TObjectDictionary<string, TmyCloudDataLookupData>)
end;
/// <summary>
/// This class represents a single table on the myCloudData.net service
/// </summary>
TmyCloudDataTable = class(TmyCloudDataModelBase)
private
FEntities: TmyCloudDataEntities;
FEntityQuery: TmyCloudDataEntityQuery;
FFields: TmyCloudDataFields;
FFilters: TmyCloudDataEntityFilters;
FIsOwner: Boolean;
FLookupData: TmyCloudDataLookupDataCollection;
FOriginalTableName: string;
FOwnerID: integer;
FPageIndex: integer;
FPageSize: integer;
FPermissions: string;
FShares: TmyCloudDataTableShares;
FSorting: TmyCloudDataEntitySortingCollection;
FTableID: integer;
FTableName: string;
function GetEntities: TmyCloudDataEntities;
function GetFields: TmyCloudDataFields;
function GetLookupData: TmyCloudDataLookupDataCollection;
protected
function ToJSON: string;
public
constructor Create(AOwner: TCustomMyCloudDataRESTClient); override;
destructor Destroy; override;
function ExecuteQuery(AQuery: TmyCloudDataEntityQuery): TmyCloudDataEntityQueryResult;
function FromJSON(AJSONContent: string): Boolean;
function GetLookupDataForField(AFieldName: string): TmyCloudDataLookupData;
function Query: Boolean;
property Entities: TmyCloudDataEntities read GetEntities;
property Fields: TmyCloudDataFields read GetFields;
property Filters: TmyCloudDataEntityFilters read FFilters write FFilters;
property IsOwner: Boolean read FIsOwner write FIsOwner;
property LookupData: TmyCloudDataLookupDataCollection read GetLookupData;
property OwnerID: integer read FOwnerID;
property PageIndex: integer read FPageIndex write FPageIndex;
property PageSize: integer read FPageSize write FPageSize;
property Permissions: string read FPermissions;
property Shares: TmyCloudDataTableShares read FShares write FShares;
property Sorting: TmyCloudDataEntitySortingCollection read FSorting write FSorting;
property TableID: integer read FTableID;
property TableName: string read FTableName write FTableName;
end;
TmyCloudDataTables = class(TmyCloudDataModelCollectionBase<TmyCloudDataTable>)
protected
function TryParseJSON(AJSONString: string; out AResult: TObjectList<TmyCloudDataTable>): Boolean;
function LoadItems: TObjectList<TmyCloudDataTable>; override;
public
function CreateTable(ATableName: string): TmyCloudDataTable;
function GetOrCreateTable(ATableName: string): TmyCloudDataTable;
function GetTableByName(ATableName: string): TmyCloudDataTable;
function GetTableByID(ATableId: integer): TmyCloudDataTable;
procedure RemoveTable(ATableId: integer);
end;
implementation
uses
StrUtils,
System.JSON.Readers,
System.JSON.Types,
REST.Types,
REST.Utils;
function TmyCloudDataField.CheckForExtraMetaData: Boolean;
var
LEmptyMetaData: TmyCloudDataFieldMetaData;
begin
LEmptyMetaData := TmyCloudDataFieldMetaData.Create;
try
Result := MetaData.Equals(LEmptyMetaData);
finally
LEmptyMetaData.Free;
end;
end;
{ TmyCloudDataModelBase }
constructor TmyCloudDataModelBase.Create(AOwner: TCustomMyCloudDataRESTClient);
begin
FOwner := AOwner;
end;
{ TmyCloudDataEntity }
constructor TmyCloudDataEntity.Create(AOwner: TCustomMyCloudDataRESTClient; ATableId: integer; AFieldSet: TmyCloudDataFields);
begin
inherited Create(AOwner);
FTableID := ATableId;
FFields := AFieldSet;
FValues := TDictionary<string, Variant>.Create;
FBlobValues := TObjectDictionary<string, TmyCloudDataBlob>.Create([doOwnsValues]);
end;
destructor TmyCloudDataEntity.Destroy;
begin
if Assigned(FValues) then
FValues.Free;
if Assigned(FBlobValues) then
FBlobValues.Free;
inherited;
end;
function TmyCloudDataEntity.FromJSON(AJSONString: string; AFieldCollection: TmyCloudDataFields): Boolean;
var
LObject: TJSONValue;
LField: TmyCloudDataField;
LFieldValue: Variant;
LBlobFieldValue: TmyCloudDataBlob;
LFieldStringValue: string;
LFieldJSONValue: TJSONValue;
begin
Result := False;
LObject := TJSONObject.ParseJSONValue(AJSONString);
try
FEntityId := TmyCloudDataJSonHelper.TryGetJSonProperty<integer>(LObject, '_ID', 0);
if FEntityId > 0 then
begin
FValues.Clear;
for LField in AFieldCollection do
begin
if not(LField.DataType = ftBlob) then
begin
if LObject.TryGetValue<string>(LField.FieldName, LFieldStringValue) then
begin
if TmyCloudDataJSonHelper.TryParseFieldValue(LFieldStringValue, LField.DataType, LFieldValue) then
begin
FValues.Add(LField.FieldName, LFieldValue);
end;
end;
end
else
begin
if LObject.TryGetValue<TJSONValue>(LField.FieldName, LFieldJSONValue) then
begin
LBlobFieldValue := TmyCloudDataBlob.Create(Owner);
if not LBlobFieldValue.FromJSON(LFieldJSONValue.ToJSON) then
begin
LBlobFieldValue.FHasData := False;
LBlobFieldValue.FUrl := '?tableid=' + FTableID.ToString + '&id=' + FEntityId.ToString + '&fieldname=' + LField.FieldName;
end;
FBlobValues.Add(LField.FieldName, LBlobFieldValue);
end;
end;
end;
Result := True;
end;
finally
LObject.Free;
end;
end;
function TmyCloudDataEntity.GetBlobField(AFieldName: string): TmyCloudDataBlob;
begin
Result := nil;
if FBlobValues.ContainsKey(AFieldName) then
begin
Result := FBlobValues[AFieldName];
end;
end;
function TmyCloudDataEntity.GetIsNew: Boolean;
begin
Result := (ID = 0);
end;
function TmyCloudDataEntity.GetValue(AFieldName: string): Variant;
begin
Result := default (Variant);
if FValues.ContainsKey(AFieldName) then
begin
Result := FValues[AFieldName];
end;
end;
function TmyCloudDataEntity.GetValueAsString(AFieldName: string): string;
begin
Result := GetValue(AFieldName);
end;
procedure TmyCloudDataEntity.SetValue(AFieldName: string; AValue: Variant);
begin
FValues.AddOrSetValue(AFieldName, AValue);
FHasUnsavedChanges := True;
end;
function TmyCloudDataEntity.ToJSON: string;
var
LJSONResult: TJSONObject;
LFieldValuesList: TJSONObject;
LField: TmyCloudDataField;
LFieldValue: string;
begin
LJSONResult := TJSONObject.Create;
LFieldValuesList := TJSONObject.Create;
try
LJSONResult.AddPair('tableid', TableID.ToString);
if not IsNew then
begin
LFieldValuesList.AddPair('_ID', ID.ToString);
end;
if (FValues.Count > 0) then
begin
for LField in FFields do
begin
if (FValues.ContainsKey(LField.FieldName)) then
begin
LFieldValue := '';
if (TmyCloudDataJSonHelper.TrySerializeFieldValue(FValues[LField.FieldName], LField.DataType, LFieldValue)) then
begin
LFieldValuesList.AddPair(LField.FieldName, LFieldValue);
end;
end;
end;
if (LFieldValuesList.Count > 0) then
begin
LJSONResult.AddPair('fields', LFieldValuesList);
end;
end;
Result := LJSONResult.ToString;
finally
LJSONResult.Free;
end;
end;
class function TmyCloudDataJSonHelper.TryParseBooleanFieldValue(ASource: string; out AResult: Variant): Boolean;
var
LBoolValue: Boolean;
begin
AResult := default (Variant);
Result := Boolean.TryToParse(ASource, LBoolValue);
if Result then
begin
AResult := LBoolValue;
end;
end;
class function TmyCloudDataJSonHelper.TryParseDateTimeFieldValue(ASource: string; out AResult: Variant): Boolean;
var
da, mo, ye, ho, mi, se: Word;
err: integer;
LDateResult: TDatetime;
begin
try
Val(Copy(ASource, 1, 4), ye, err);
Val(Copy(ASource, 6, 2), mo, err);
Val(Copy(ASource, 9, 2), da, err);
Val(Copy(ASource, 12, 2), ho, err);
Val(Copy(ASource, 15, 2), mi, err);
Val(Copy(ASource, 18, 2), se, err);
if ye < 100 then
ye := 100;
if mo < 1 then
mo := 1;
if da < 1 then
da := 1;
LDateResult := EncodeDate(ye, mo, da) + EncodeTime(ho, mi, se, 0);
AResult := LDateResult;
Result := True;
except
on E: Exception do
Result := False;
end;
end;
class function TmyCloudDataJSonHelper.TryParseFieldValue(ASource: string; ADataType: TmyCloudDataFieldDataType; out AResult: Variant): Boolean;
begin
Result := False;
case ADataType of
ftInteger:
begin
Result := TryParseIntegerFieldValue(ASource, AResult);
end;
ftFloat:
begin
Result := TryParseFloatFieldValue(ASource, AResult);
end;
ftBoolean:
begin
Result := TryParseBooleanFieldValue(ASource, AResult);
end;
ftWideString:
begin
Result := True;
AResult := ASource;
end;
ftDateTime, ftDate, ftTime:
begin
Result := TryParseDateTimeFieldValue(ASource, AResult);
end;
end;
end;
class function TmyCloudDataJSonHelper.TryParseFloatFieldValue(ASource: string; out AResult: Variant): Boolean;
var
LInt64Value: int64;
begin
Result := int64.TryParse(ASource, LInt64Value);
if Result then
begin
AResult := LInt64Value;
end;
end;
class function TmyCloudDataJSonHelper.TryParseIntegerFieldValue(ASource: string; out AResult: Variant): Boolean;
var
LIntValue: integer;
begin
Result := integer.TryParse(ASource, LIntValue);
if Result then
begin
AResult := LIntValue;
end;
end;
class function TmyCloudDataJSonHelper.TrySerializeBooleanFieldValue(AValue: Variant; out AStringResult: string): Boolean;
var
LValue: Boolean;
begin
AStringResult := '';
Result := False;
try
LValue := AValue;
if LValue then
begin
AStringResult := 'true';
Result := True;
end
else
begin
AStringResult := 'false';
Result := True;
end;
except
on E: Exception do
// nothing
end;
end;
class function TmyCloudDataJSonHelper.TrySerializeDateTimeFieldValue(AValue: Variant; out AStringResult: string): Boolean;
var
da, mo, ye, ho, mi, se, ms: Word;
LDateTimeValue: TDatetime;
begin
try
LDateTimeValue := AValue;
DecodeDate(LDateTimeValue, ye, mo, da);
DecodeTime(LDateTimeValue, ho, mi, se, ms);
AStringResult := IntToZStr(ye, 4) + '-' + IntToZStr(mo, 2) + '-' + IntToZStr(da, 2) + 'T' + IntToZStr(ho, 2) + ':' + IntToZStr(mi, 2) + ':' +
IntToZStr(se, 2) + '.000Z';
Result := True;
except
on E: Exception do
Result := False;
end;
end;
class function TmyCloudDataJSonHelper.IntToZStr(i, l: integer): string;
var
Res: string;
begin
Res := IntToStr(i);
while Length(Res) < l do
begin
Res := '0' + Res;
end;
Result := Res;
end;
class function TmyCloudDataJSonHelper.TrySerializeFieldValue(AValue: Variant; ADataType: TmyCloudDataFieldDataType;
out AStringResult: string): Boolean;
begin
Result := False;
case ADataType of
ftInteger:
Result := TrySerializeIntegerFieldValue(AValue, AStringResult);
ftFloat:
Result := TrySerializeFloatFieldValue(AValue, AStringResult);
ftWideString:
Result := TrySerializeStringFieldValue(AValue, AStringResult);
ftBoolean:
Result := TrySerializeBooleanFieldValue(AValue, AStringResult);
ftDateTime, ftDate, ftTime:
Result := TrySerializeDateTimeFieldValue(AValue, AStringResult);
end;
end;
class function TmyCloudDataJSonHelper.TrySerializeFloatFieldValue(AValue: Variant; out AStringResult: string): Boolean;
begin
try
AStringResult := FloatToStr(AValue);
Result := True;
except
on E: Exception do
Result := False;
end;
end;
class function TmyCloudDataJSonHelper.TrySerializeIntegerFieldValue(AValue: Variant; out AStringResult: string): Boolean;
begin
try
AStringResult := IntToStr(AValue);
Result := True;
except
on E: Exception do
Result := False;
end;
end;
class function TmyCloudDataJSonHelper.TrySerializeStringFieldValue(AValue: Variant; out AStringResult: string): Boolean;
begin
try
Result := True;
AStringResult := StringReplace(AValue, '\', '\\', [rfReplaceAll]);
AStringResult := StringReplace(AStringResult, '"', '\"', [rfReplaceAll]);
AStringResult := StringReplace(AStringResult, #9, '\t', [rfReplaceAll]);
AStringResult := StringReplace(AStringResult, #$A, '\n', [rfReplaceAll]);
AStringResult := StringReplace(AStringResult, #$D, '\r', [rfReplaceAll]);
except
on E: Exception do
Result := False;
end;
end;
function TmyCloudDataEntity.TrySetValueFromString(AFieldName, AValue: string): Boolean;
var
LField: TmyCloudDataField;
LValue: Variant;
begin
Result := False;
for LField in FFields do
begin
if LField.FFieldName = AFieldName then
begin
if not(LField.DataType = ftBlob) then
begin
Result := TmyCloudDataJSonHelper.TryParseFieldValue(AValue, LField.DataType, LValue);
if Result then
begin
SetValue(AFieldName, LValue);
Result := True;
end;
end;
Exit;
end;
end;
end;
{ TmyCloudDataEntityQuery }
constructor TmyCloudDataEntityQuery.Create(ATableId: integer);
begin
FTableID := ATableId;
FFilters := TmyCloudDataEntityFilters.Create;
FSorting := TmyCloudDataEntitySortingCollection.Create;
FFields := TList<string>.Create;
end;
destructor TmyCloudDataEntityQuery.Destroy;
begin
inherited;
if Assigned(FFilters) then
FFilters.Free;
if Assigned(FSorting) then
FSorting.Free;
if Assigned(FFields) then
FFields.Free;
end;
function TmyCloudDataEntityQuery.GetIsPaged: Boolean;
begin
Result := (PageSize > 0) and (PageIndex >= 0);
end;
procedure TmyCloudDataEntityQuery.Reset;
begin
FFilters.Clear;
FSorting.Clear;
FFields.Clear;
FPageSize := 0;
FPageIndex := 0;
end;
function TmyCloudDataEntityQuery.ToJSON: string;
var
LJSONResult: TJSONObject;
LFilter: TmyCloudDataEntityFilter;
LFiltersArray: TJSONArray;
LSorting: TmyCloudDataEntitySorting;
LSortingArray: TJSONArray;
LField: string;
LFieldsFilter: string;
begin
LJSONResult := TJSONObject.Create;
try
LJSONResult.AddPair('tableid', IntToStr(TableID));
if (IsPaged) then
begin
LJSONResult.AddPair('pagesize', IntToStr(PageSize));
LJSONResult.AddPair('pageindex', IntToStr(PageIndex));
end;
if (Filters.Count > 0) then
begin
LFiltersArray := TJSONArray.Create;
for LFilter in Filters do
begin
LFiltersArray.Add(LFilter.ToJSON);
end;
LJSONResult.AddPair('filters', LFiltersArray);
end;
if (Sorting.Count > 0) then
begin
LSortingArray := TJSONArray.Create;
for LSorting in Sorting do
begin
LSortingArray.Add(LSorting.ToJSON);
end;
LJSONResult.AddPair('sorting', LSortingArray);
end;
if (Fields.Count > 0) then
begin
LFieldsFilter := '';
for LField in Fields do
begin
LFieldsFilter := LFieldsFilter + LField + ',';
end;
LJSONResult.AddPair('fields', LFieldsFilter.TrimRight([',']));
end;
Result := LJSONResult.ToString;
finally
LJSONResult.Free;
end;
end;
{ TmyCloudDataTable }
constructor TmyCloudDataTable.Create(AOwner: TCustomMyCloudDataRESTClient);