-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathMVCBr.FDMongoDB.pas
414 lines (365 loc) · 10.5 KB
/
MVCBr.FDMongoDB.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
unit MVCBr.FDMongoDB;
interface
uses System.Classes, System.SysUtils, Data.DB,
MVCBr.MongoModel,
FireDAC.DatS, FireDAC.Stan.Intf,
FireDAC.Stan.Option, FireDAC.Comp.Client,
FireDAC.Comp.Dataset;
type
TMVCBrMongoConnection = class(TComponent)
private
FParams: IMongoModel;
FPort: integer;
FDatabase: string;
FPassword: string;
FHost: string;
FUserName: string;
procedure SetParams(const Value: IMongoModel);
procedure SetDatabase(const Value: string);
procedure SetHost(const Value: string);
procedure SetPassword(const Value: string);
procedure SetPort(const Value: integer);
procedure SetUserName(const Value: string);
function GetActive: boolean;
procedure SetActive(const Value: boolean);
public
function Default: TMVCBrMongoConnection;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
Property Params: IMongoModel read FParams write SetParams;
function RunCommand(ACommand: IJSONDocument): IJSONDocument;
published
property Active: boolean read GetActive write SetActive default false;
property Host: string read FHost write SetHost;
property Port: integer read FPort write SetPort default 27017;
property Database: string read FDatabase write SetDatabase;
property UserName: string read FUserName write SetUserName;
property Password: string read FPassword write SetPassword;
end;
TMVCBrMongoDataset = class(TFDCustomMemTable)
private
FConnection: TMVCBrMongoConnection;
FLoading: boolean;
FCollectionName: string;
FKeyFields: string;
procedure SetConnection(const Value: TMVCBrMongoConnection);
procedure SetLoading(const Value: boolean);
procedure SetCollectionName(const Value: string);
procedure checkNil;
procedure SetKeyFields(const Value: string);
function GetKeyFieldsDoc(AFields: TFields): IJSONDocument;
constructor Create(AOwner: TComponent); override;
procedure DoUpdateRecord(ASender: TDataSet; ARequest: TFDUpdateRequest;
var AAction: TFDErrorAction; AOptions: TFDUpdateRowOptions);
protected
procedure Notification(AComponent: TComponent;
Operation: TOperation); override;
procedure SetActive(Value: boolean); override;
public
function Open(AWhereArray: array of Variant; AProcBeforePost: TProc = nil)
: integer; overload;
function OpenWithCommand(ACommand: IJSONDocument;
AProcBeforePost: TProc = nil): integer; overload;
procedure Open; reintroduce; overload;
function Insert: boolean;
function Update(const AWhereJson: array of Variant;
AUpsert: boolean = false; AMUltiUpdate: boolean = false): boolean;
function Post(const AWhereJson: Array of Variant): boolean;
function Delete(const AWhereJson: Array of Variant): boolean;
property Loading: boolean read FLoading write SetLoading;
{ TODO:
function UpdatesPending: boolean;
procedure ClearChanges;
procedure CancelUpdates; reintroduce;
procedure MergeChangeLog; reintroduce;
property Changes: TJSONArray read GetChanges;
}
published
property CollectionName: string read FCollectionName
write SetCollectionName;
property KeyFields: string read FKeyFields write SetKeyFields;
property Connection: TMVCBrMongoConnection read FConnection
write SetConnection;
property FieldDefs;
property Active;
property Filter;
property Filtered;
property FormatOptions;
property AutoCalcFields;
property Adapter;
property DataSetField;
property DetailFields;
property FieldOptions;
property MasterFields;
property MasterSource;
property UpdateOptions;
property LocalSQL;
end;
Procedure Register;
implementation
uses System.JSON, Data.DB.Helper, Variants;
Procedure Register;
begin
RegisterComponents('MVCBr', [TMVCBrMongoConnection, TMVCBrMongoDataset]);
end;
{ TMVCBrMongoDataset }
procedure TMVCBrMongoDataset.checkNil;
begin
assert(Connection <> nil, 'Can not do anything without a connection');
end;
constructor TMVCBrMongoDataset.Create(AOwner: TComponent);
begin
inherited;
OnUpdateRecord := DoUpdateRecord;
end;
function TMVCBrMongoDataset.Delete(const AWhereJson: array of Variant): boolean;
begin
checkNil;
end;
procedure TMVCBrMongoDataset.DoUpdateRecord(ASender: TDataSet;
ARequest: TFDUpdateRequest; var AAction: TFDErrorAction;
AOptions: TFDUpdateRowOptions);
begin
AAction := TFDErrorAction.eaApplied;
if FLoading then
exit;
AAction := TFDErrorAction.eaFail;
checkNil;
case ARequest of
arInsert:
Connection.Params.Insert(FCollectionName,
mongoJSON(ASender.fields.ToJson));
arUpdate:
Connection.Params.Update(FCollectionName, GetKeyFieldsDoc(ASender.fields),
mongoJSON(ASender.fields.ToJson), false, false);
arDelete:
Connection.Params.Delete(FCollectionName,
GetKeyFieldsDoc(ASender.fields), true);
end;
AAction := TFDErrorAction.eaApplied;
end;
function TMVCBrMongoDataset.GetKeyFieldsDoc(AFields: TFields): IJSONDocument;
var
str: TStringList;
s: string;
begin
assert(FKeyFields <> '', 'Cant create Where with no KeyFields');
result := mongoJSON();
str := TStringList.Create;
try
str.Delimiter := ';';
str.DelimitedText := FKeyFields;
for s in str do
begin
result.Item[s] := AFields.FieldByName(s).OldValue;
end;
finally
str.free;
end;
end;
function TMVCBrMongoDataset.Insert: boolean;
begin
result := false;
checkNil;
Connection.Params.Insert(FCollectionName, mongoJSON(fields.ToJson));
result := true;
end;
procedure TMVCBrMongoDataset.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited;
if assigned(FConnection) and FConnection.Equals(AComponent) and
(Operation = TOperation.opRemove) then
FConnection := nil;
end;
procedure TMVCBrMongoDataset.Open;
begin
self.Open([]);
end;
function TMVCBrMongoDataset.OpenWithCommand(ACommand: IJSONDocument;
AProcBeforePost: TProc = nil): integer;
var
j, jp: TJsonObject;
p: TJsonPair;
v: TJSonValue;
rst: TJsonObject;
arr: TJSonValue;
s: string;
doc: IJSONDocArray;
begin
checkNil;
FLoading := true;
try
if Active then
EmptyDataSet;
j := TJsonObject.Create as TJsonObject;
try
if VarIsNull(ACommand.Item['find']) then
j.addPair('find', FCollectionName);
jp := TJsonObject.ParseJSONValue(ACommand.ToString) as TJsonObject;
try
for p in jp do
begin
j.addPair(TJsonPair.Create(p.JsonString, p.JsonValue));
end;
s := j.ToJson;
rst := TJsonObject.ParseJSONValue
(Connection.Params.RunCommand(mongoJSON(s)).ToString) as TJsonObject;
try
arr := rst.GetValue('cursor');
begin
doc := TJSONDocument.NewDocArray;
for v in arr.GetValue<TJsonArray>('firstBatch') do
begin
doc.AddJson(v.ToString);
end;
result := Connection.Params.This.FillDataset(doc, self, nil);
end;
finally
rst.free;
end;
finally
// jp.free;
end;
finally
j.free;
end;
finally
FLoading := false;
end;
end;
function TMVCBrMongoDataset.Open(AWhereArray: array of Variant;
AProcBeforePost: TProc = nil): integer;
var
AJson: IJSONDocument;
begin
checkNil;
FLoading := true;
try
if Active then
EmptyDataSet;
AJson := mongoJSON(AWhereArray);
result := Connection.Params.GetDataset(FCollectionName, AJson, self,
AProcBeforePost);
finally
FLoading := false;
end;
end;
function TMVCBrMongoDataset.Post(const AWhereJson: array of Variant): boolean;
begin
checkNil;
result := self.Update(AWhereJson, true, false);
end;
procedure TMVCBrMongoDataset.SetActive(Value: boolean);
begin
inherited;
if Value and (not FLoading) and (csDesigning in ComponentState) then
begin
try
OpenWithCommand(mongoJSON(['limit', 10]));
except
end;
end;
end;
procedure TMVCBrMongoDataset.SetCollectionName(const Value: string);
begin
FCollectionName := Value;
end;
procedure TMVCBrMongoDataset.SetConnection(const Value: TMVCBrMongoConnection);
begin
FConnection := Value;
end;
procedure TMVCBrMongoDataset.SetKeyFields(const Value: string);
begin
FKeyFields := Value;
end;
procedure TMVCBrMongoDataset.SetLoading(const Value: boolean);
begin
FLoading := Value;
end;
function TMVCBrMongoDataset.Update(const AWhereJson: array of Variant;
AUpsert: boolean = false; AMUltiUpdate: boolean = false): boolean;
begin
result := false;
checkNil;
Connection.Params.Update(FCollectionName, mongoJSON(AWhereJson),
mongoJSON(fields.ToJson), AUpsert, AMUltiUpdate);
result := false;
end;
{ TMVCBrMongoConnection }
constructor TMVCBrMongoConnection.Create(AOwner: TComponent);
begin
inherited;
FPort := 27017;
FHost := 'localhost';
FDatabase := 'mvcbrDB';
end;
function TMVCBrMongoConnection.Default: TMVCBrMongoConnection;
begin
result := self;
if not assigned(FParams) then
begin
FParams := TMongoModelFactory.Create;
FParams.SetPort(FPort);
FParams.SetHost(FHost);
FParams.SetPassword(FPassword);
FParams.SetUserName(FUserName);
FParams.SetDatabase(FDatabase);
end;
end;
destructor TMVCBrMongoConnection.Destroy;
begin
FParams := nil;
inherited;
end;
function TMVCBrMongoConnection.GetActive: boolean;
begin
result := Default.Params.Active;
end;
function TMVCBrMongoConnection.RunCommand(ACommand: IJSONDocument)
: IJSONDocument;
begin
result := Default.Params.RunCommand(ACommand);
end;
procedure TMVCBrMongoConnection.SetActive(const Value: boolean);
begin
Default;
FParams.Active := Value;
end;
procedure TMVCBrMongoConnection.SetDatabase(const Value: string);
begin
FDatabase := Value;
Default.Params.Database(Value);
end;
procedure TMVCBrMongoConnection.SetHost(const Value: string);
begin
FHost := Value;
Default.Params.Host(Value);
end;
procedure TMVCBrMongoConnection.SetParams(const Value: IMongoModel);
begin
with Default do
begin
Params.Database(Value.GetDatabase);
Params.Host(Value.GetHost);
Params.Password(Value.GetPassword);
Params.Port(Value.GetPort);
Params.UserName(Value.GetUserName);
end;
end;
procedure TMVCBrMongoConnection.SetPassword(const Value: string);
begin
FPassword := Value;
Default.Params.Password(Value);
end;
procedure TMVCBrMongoConnection.SetPort(const Value: integer);
begin
FPort := Value;
Default.Params.Port(Value);
end;
procedure TMVCBrMongoConnection.SetUserName(const Value: string);
begin
FUserName := Value;
Default.Params.UserName(Value);
end;
end.