forked from synopse/mORMot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SynDBMidasVCL.pas
516 lines (466 loc) · 17.4 KB
/
SynDBMidasVCL.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
/// fill a VCL TClientDataset from SynDB data access
// - this unit is a part of the freeware Synopse framework,
// licensed under a MPL/GPL/LGPL tri-license; version 1.18
unit SynDBMidasVCL;
{
This file is part of Synopse framework.
Synopse framework. Copyright (c) Arnaud Bouchez
Synopse Informatique - https://synopse.info
*** BEGIN LICENSE BLOCK *****
Version: MPL 1.1/GPL 2.0/LGPL 2.1
The contents of this file are subject to the Mozilla Public License Version
1.1 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
for the specific language governing rights and limitations under the License.
The Original Code is Synopse mORMot framework.
The Initial Developer of the Original Code is Arnaud Bouchez.
Portions created by the Initial Developer are Copyright (c)
the Initial Developer. All Rights Reserved.
Contributor(s):
- Alfred Glaenzer (alf)
- mingda
- Murat Ak
Alternatively, the contents of this file may be used under the terms of
either the GNU General Public License Version 2 or later (the "GPL"), or
the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
in which case the provisions of the GPL or the LGPL are applicable instead
of those above. If you wish to allow use of your version of this file only
under the terms of either the GPL or the LGPL, and not to allow others to
use your version of this file under the terms of the MPL, indicate your
decision by deleting the provisions above and replace them with the notice
and other provisions required by the GPL or the LGPL. If you do not delete
the provisions above, a recipient may use your version of this file under
the terms of any one of the MPL, the GPL or the LGPL.
***** END LICENSE BLOCK *****
}
{$I Synopse.inc} // define HASINLINE CPU32 CPU64 OWNNORMTOUPPER
interface
uses
{$ifdef ISDELPHIXE2}
System.SysUtils,
{$else}
SysUtils,
{$endif}
Classes,
{$ifndef DELPHI5OROLDER}
Variants,
{$ifndef FPC}
MidasLib,
{$endif}
{$endif}
SynCommons,
SynDB, SynDBVCL,
DB,
{$ifdef FPC}
BufDataset
{$else}
Contnrs,
DBClient,
Provider,
SqlConst
{$endif};
{$ifdef FPC}
type
/// FPC's pure pascal in-memory buffer is used instead of TClientDataSet
TClientDataSet = TBufDataset;
/// wrapper functions will use FPC's pure pascal in-memory buffer
TSynDBDataSet = TBufDataset;
{$else FPC}
type
/// a TClientDataSet which allows to apply updates on a SynDB connection
// - typical usage may be for instance over a SynDBRemote connection:
// ! props := TSQLDBWinHTTPConnectionProperties.Create(....);
// ! ds := TSynDBDataSet.Create(MainForm);
// ! ds.CommandText := 'select * from people';
// ! ds.Open;
// ! // ... use ds as usual, including modifications
// ! ds.ApplyUpdates(0);
TSynDBDataSet = class(TCustomClientDataSet)
protected
fDataSet: TSynDBSQLDataSet;
fProvider: TDataSetProvider;
fIgnoreColumnDataSize: boolean;
function GetConnection: TSQLDBConnectionProperties; virtual;
procedure SetConnection(Value: TSQLDBConnectionProperties); virtual;
// from TDataSet
procedure OpenCursor(InfoQuery: Boolean); override;
{$ifdef ISDELPHI2007ANDUP}
// from IProviderSupport
function PSGetCommandText: string; override;
{$endif}
public
/// initialize the instance
constructor Create(AOwner: TComponent); override;
procedure FetchParams;
/// initialize the internal TDataSet from a SynDB TSQLDBStatement result set
// - the supplied TSQLDBStatement can then be freed by the caller, since
// a private binary copy will be owned by this instance (in fDataSet.Data)
procedure From(Statement: TSQLDBStatement; MaxRowCount: cardinal=0);
/// if field sizes should be left unset, allowing further filling with
// any data length
// - by default, ColumnDataSize would be computed from the supplied data,
// unless you set IgnoreColumnDataSize=true to set the value to 0 (and
// force e.g. SynDBVCL TSynBinaryDataSet.InternalInitFieldDefs define the
// field as ftDefaultMemo)
property IgnoreColumnDataSize: boolean read fIgnoreColumnDataSize write fIgnoreColumnDataSize;
published
property CommandText;
property Active;
property Aggregates;
property AggregatesActive;
property AutoCalcFields;
property Constraints;
property DisableStringTrim;
property FileName;
property Filter;
property Filtered;
property FilterOptions;
property FieldDefs;
property IndexDefs;
property IndexFieldNames;
property IndexName;
property FetchOnDemand;
property MasterFields;
property MasterSource;
property ObjectView;
property PacketRecords;
property Params;
property ReadOnly;
property StoreDefs;
property BeforeOpen;
property AfterOpen;
property BeforeClose;
property AfterClose;
property BeforeInsert;
property AfterInsert;
property BeforeEdit;
property AfterEdit;
property BeforePost;
property AfterPost;
property BeforeCancel;
property AfterCancel;
property BeforeDelete;
property AfterDelete;
property BeforeScroll;
property AfterScroll;
property BeforeRefresh;
property AfterRefresh;
property OnCalcFields;
property OnDeleteError;
property OnEditError;
property OnFilterRecord;
property OnNewRecord;
property OnPostError;
property OnReconcileError;
property BeforeApplyUpdates;
property AfterApplyUpdates;
property BeforeGetRecords;
property AfterGetRecords;
property BeforeRowRequest;
property AfterRowRequest;
property BeforeExecute;
property AfterExecute;
property BeforeGetParams;
property AfterGetParams;
/// the associated SynDB connection
property Connection: TSQLDBConnectionProperties read GetConnection write SetConnection;
/// the associated SynDB TDataSet, used to retrieve and update data
property DataSet: TSynDBSQLDataSet read fDataSet;
end;
{$endif FPC}
/// fetch a SynDB TQuery result set into a new VCL TClientDataSet
// - if aMaxRowCount>0, will return up to the specified number of rows
// - current implementation will return a TClientDataSet instance, created from
// the supplied TQuery content
// - for better speed with Delphi older than Delphi 2009 Update 3, it is
// recommended to use http://andy.jgknet.de/blog/bugfix-units/midas-speed-fix-12
// - if you need a read/only TDataSet, you should better not use this function
// but ToDataSet() as defined in SynDBVCL which is much faster and uses
// much less resources
function ToClientDataSet(aOwner: TComponent; aStatement: SynDB.TQuery;
aMaxRowCount: integer=0): TSynDBDataSet; overload;
{$ifndef FPC}
/// fetch a SynDB TSQLDBStatement result set into a new VCL TClientDataSet
// - if aMaxRowCount>0, will return up to the specified number of rows
// - current implementation will return a TClientDataSet instance, created from
// the supplied TSQLDBStatement content
// - for better speed with Delphi older than Delphi 2009 Update 3, it is
// recommended to use http://andy.jgknet.de/blog/bugfix-units/midas-speed-fix-12
// - if you need a read/only TDataSet, you should better not use this function
// but ToDataSet() function as defined in SynDBVCL which is much faster and uses
// much less resources
function ToClientDataSet(aOwner: TComponent; aStatement: TSQLDBStatement;
aMaxRowCount: integer=0): TSynDBDataSet; overload;
{$endif FPC}
/// fetch a SynDB ISQLDBRows result set into a new VCL TClientDataSet
// - this overloaded function can use directly a result of the
// TSQLDBConnectionProperties.Execute() method, as such:
// ! ds1.DataSet := ToClientDataSet(self,props.Execute('select * from table',[]));
function ToClientDataSet(aOwner: TComponent; aStatement: ISQLDBRows;
aMaxRowCount: integer=0): TSynDBDataSet; overload;
type
/// how ToClientDataSet functions will
// fill the TClientDataSet instance
TClientDataSetMode = (cdsNew, cdsAppend, cdsReplace);
/// fetch a SynDB TQuery result into an existing VCL TClientDataSet
// - if aMaxRowCount>0, will return up to the specified number of rows
// - current implementation will fill an existing TClientDataSet instance, from
// the supplied TQuery content
// - for better speed with Delphi older than Delphi 2009 Update 3, it is
// recommended to use http://andy.jgknet.de/blog/bugfix-units/midas-speed-fix-12
function ToClientDataSet(aDataSet: TClientDataSet; aStatement: SynDB.TQuery;
aMaxRowCount: integer=0; aMode: TClientDataSetMode=cdsReplace; aLogChange: boolean=false): boolean; overload;
/// fetch a SynDB TSQLDBStatement result into an existing VCL TClientDataSet
// - if aMaxRowCount>0, will return up to the specified number of rows
// - current implementation will fill an existing TClientDataSet instance, from
// the supplied TSQLDBStatement content
// - for better speed with Delphi older than Delphi 2009 Update 3, it is
// recommended to use http://andy.jgknet.de/blog/bugfix-units/midas-speed-fix-12
function ToClientDataSet(aDataSet: TClientDataSet; aStatement: TSQLDBStatement;
aMaxRowCount: integer=0; aMode: TClientDataSetMode=cdsReplace; aLogChange: boolean=false): boolean; overload;
implementation
var
GlobalDataSetCount: integer;
function ToClientDataSet(aDataSet: TClientDataSet; aStatement: SynDB.TQuery;
aMaxRowCount: integer; aMode: TClientDataSetMode; aLogChange: boolean): boolean;
begin
if aStatement=nil then
result := false else
result := ToClientDataSet(aDataSet,aStatement.PreparedSQLDBStatement.Instance,aMaxRowCount);
end;
function ToClientDataSet(aOwner: TComponent; aStatement: SynDB.TQuery;
aMaxRowCount: integer): TSynDBDataSet;
begin
if aStatement=nil then
result := nil else
result := ToClientDataSet(aOwner,aStatement.PreparedSQLDBStatement.Instance,aMaxRowCount);
end;
{$ifndef FPC}
function ToClientDataSet(aOwner: TComponent; aStatement: TSQLDBStatement;
aMaxRowCount: integer): TSynDBDataSet;
begin
result := TSynDBDataSet.Create(aOwner);
try
result.Name := 'SynDBDS'+IntToStr(GlobalDataSetCount); // unique name
inc(GlobalDataSetCount);
if aStatement<>nil then
result.From(aStatement,aMaxRowCount);
except
on Exception do
FreeAndNil(result);
end;
end;
{$endif FPC}
function ToClientDataSet(aOwner: TComponent; aStatement: ISQLDBRows;
aMaxRowCount: integer=0): TSynDBDataSet; overload;
begin
if aStatement=nil then
result := nil else
result := ToClientDataSet(aOwner,aStatement.Instance,aMaxRowCount);
end;
function ToClientDataSet(aDataSet: TClientDataSet; aStatement: TSQLDBStatement;
aMaxRowCount: integer; aMode: TClientDataSetMode; aLogChange: boolean): boolean; overload;
var Source: TSynBinaryDataSet;
Columns: array of record
Field: DB.TField;
WasReadOnly: boolean;
OnChange: TFieldNotifyEvent;
end;
Previous: record
Active: Boolean;
ReadOnly: Boolean;
LogChanges: Boolean;
AfterScroll: TDataSetNotifyEvent;
end;
row, f: integer;
SourceStream,DestStream: TStream;
begin
result := false;
if (aDataSet=nil) or (aStatement=nil) then
exit;
fillchar(Previous,sizeof(Previous),0);
if aDataSet.Active then begin
Previous.Active := true;
{$ifndef FPC}
Previous.LogChanges := aDataSet.LogChanges;
{$endif}
Previous.ReadOnly := aDataSet.ReadOnly;
Previous.AfterScroll := aDataSet.AfterScroll;
aDataSet.AfterScroll := nil;
aDataSet.ReadOnly := false;
aDataSet.DisableControls;
end;
if aMode=cdsReplace then begin
{$ifndef FPC}
if Previous.LogChanges then
aDataSet.LogChanges := false;
aDataSet.EmptyDataSet;
{$else}
aDataSet.MergeChangeLog;
aDataSet.Close;
aDataSet.Open;
{$endif}
end;
Source := TSynBinaryDataSet.Create(nil);
try
// load all data content into optimized in-memory buffer
Source.From(aStatement,aMaxRowCount);
Source.Open;
// handle columns
SetLength(Columns,Source.DataAccess.ColumnCount);
if aMode=cdsNew then begin
for f := 0 to high(Columns) do
with Source.Fields[f] do
aDataSet.FieldDefs.Add(FieldName,DataType,Size);
aDataSet.CreateDataSet;
for f := 0 to high(Columns) do
Columns[f].Field := aDataSet.FieldByName(Source.Fields[f].FieldName);
end else
for f := 0 to high(Columns) do
with Columns[f] do begin
Field := aDataSet.FieldByName(Source.Fields[f].FieldName);
if Field.ReadOnly then begin
WasReadOnly := true;
Field.ReadOnly := false;
end;
OnChange := Field.OnChange;
Field.OnChange := nil;
end;
// append data
{$ifndef FPC}
aDataSet.LogChanges := aLogChange;
{$endif}
for row := 0 to Source.DataAccess.DataRowCount-1 do begin
Source.DataAccess.GotoRow(row,true);
aDataSet.Append;
for f := 0 to high(Columns) do
with Columns[f] do
if Field<>nil then
if Source.DataAccess.ColumnNull(f) then
Field.Clear else
case Field.DataType of
ftBoolean:
Field.AsBoolean := Source.DataAccess.ColumnInt(f)<>0;
ftSmallint, ftInteger, ftWord:
Field.AsInteger := Source.DataAccess.ColumnInt(f);
ftLargeint:
TLargeintField(Field).Value := Source.DataAccess.ColumnInt(f);
ftFloat, DB.ftCurrency:
Field.AsFloat := Source.DataAccess.ColumnDouble(f);
DB.ftDate,ftDateTime,ftTime:
Field.AsDateTime := Source.DataAccess.ColumnDateTime(f);
ftString:
Field.AsString := Source.DataAccess.ColumnString(f);
ftWideString:
TWideStringField(Field).Value := UTF8ToSynUnicode(Source.DataAccess.ColumnUTF8(f));
{$ifdef ISDELPHI2007ANDUP}
ftWideMemo,
{$endif}
ftMemo, DB.ftBlob: begin
SourceStream := Source.GetBlobStream(Source.Fields[f],row);
if SourceStream=nil then
Field.Clear else
try
DestStream := aDataSet.CreateBlobStream(Field,bmWrite);
try
DestStream.CopyFrom(SourceStream,0);
finally
DestStream.Free;
end;
finally
SourceStream.Free;
end;
end;
else raise EDatabaseError.CreateFmt('Invalid Source.ColumnType for %s)',
[Field.FieldName]);
end;
aDataSet.Post;
end;
aDataSet.First;
result := true;
finally
Source.Free;
if Previous.Active then begin
{$ifndef FPC}
aDataSet.LogChanges := Previous.LogChanges;
{$endif}
aDataSet.ReadOnly := Previous.ReadOnly;
aDataSet.AfterScroll := Previous.AfterScroll;
if Assigned(Previous.AfterScroll) then
Previous.AfterScroll(aDataSet);
aDataSet.EnableControls;
end;
if aMode<>cdsNew then begin
for f := 0 to high(Columns) do
with Columns[f] do
if Field<>nil then begin
Field.ReadOnly := WasReadOnly;
Field.OnChange := OnChange;
end;
end;
end;
end;
{$ifndef FPC}
{ TSynDBDataSet }
constructor TSynDBDataSet.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
fProvider := TDataSetProvider.Create(Self);
fProvider.Name := 'InternalProvider'; { Do not localize }
fProvider.SetSubComponent(True);
fProvider.Options := fProvider.Options+[poAllowCommandText];
SetProvider(fProvider);
fDataSet := TSynDBSQLDataSet.Create(Self);
fDataSet.Name := 'InternalDataSet'; { Do not localize }
fDataSet.SetSubComponent(True);
fProvider.DataSet := fDataSet;
end;
procedure TSynDBDataSet.From(Statement: TSQLDBStatement; MaxRowCount: cardinal);
begin
fDataSet.From(Statement,MaxRowCount,fIgnoreColumnDataSize);
fDataSet.CommandText := ''; // ensure no SQL execution
Open;
fDataSet.CommandText := UTF8ToString(Statement.SQL); // assign it AFTER Open
end;
procedure TSynDBDataSet.FetchParams;
begin
if not HasAppServer and Assigned(FProvider) then
SetProvider(FProvider);
inherited FetchParams;
end;
procedure TSynDBDataSet.OpenCursor(InfoQuery: Boolean);
begin
if Assigned(fProvider) then
SetProvider(fProvider);
if fProvider.DataSet=self then
raise ESQLDBException.Create(SCircularProvider);
inherited OpenCursor(InfoQuery);
end;
{$ifdef ISDELPHI2007ANDUP}
function TSynDBDataSet.PSGetCommandText: string;
{$ifdef ISDELPHIXE3}
var IP: IProviderSupportNG;
begin
if Supports(fDataSet, IProviderSupportNG, IP) then
{$else}
var IP: IProviderSupport;
begin
if Supports(fDataSet, IProviderSupport, IP) then
{$endif}
result := IP.PSGetCommandText else
result := CommandText;
end;
{$endif ISDELPHI2007ANDUP}
function TSynDBDataSet.GetConnection: TSQLDBConnectionProperties;
begin
result := fDataSet.Connection;
end;
procedure TSynDBDataSet.SetConnection(Value: TSQLDBConnectionProperties);
begin
fDataSet.Connection := Value;
end;
{$endif FPC}
end.