-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathADOTools.pas
411 lines (368 loc) · 10.1 KB
/
ADOTools.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
unit ADOTools;
{
ADOTools: thin ADODB wrapper.
https://github.com/stijnsanders/DataLank
}
interface
uses SysUtils, ADODB_TLB;
type
TADOLink=class(TObject)
private
FConnection:Connection;
public
constructor Create(const ConnectionString: WideString);
destructor Destroy; override;
function SingleValue(const SQL: WideString;
const Values: array of Variant): Variant;
function Execute(const SQL: WideString;
const Values: array of Variant): integer;
function Insert(const TableName: WideString; const Values: array of OleVariant;
const PKFieldName: WideString=''): int64;
procedure Update(const TableName: WideString; const Values:array of OleVariant);
property Connection:Connection read FConnection;
end;
TADOResult=class(TObject)
private
FRecordSet:Recordset;
FFirstRead:boolean;
function GetValue(Idx:OleVariant):OleVariant;
function IsEof:boolean;
public
constructor Create(Connection: TADOLink; const SQL: WideString;
const Values: array of Variant); overload;
constructor Create(Recordset: Recordset); overload;
destructor Destroy; override;
procedure Reset;
procedure CheckResultSet;//use with multiple resultsets (e.g. when calling stored procedure)
function Read:boolean;
property Fields[Idx:OleVariant]:OleVariant read GetValue; default;
property EOF: boolean read IsEof;
function GetInt(const Idx:OleVariant):integer;
function GetStr(const Idx:OleVariant):WideString;
function GetDate(const Idx:OleVariant):TDateTime;
function IsNull(const Idx:OleVariant):boolean;
function GetDefault(const Idx,Default:OleVariant):OleVariant;
end;
EFieldNotFound=class(Exception);
ESingleValueFailed=class(Exception);
implementation
uses Variants, Classes, ComObj;
procedure CmdParameters(Cmd:Command;const Values:array of Variant);
var
i:integer;
vt:TVarType;
begin
for i:=0 to Length(Values)-1 do
begin
vt:=VarType(Values[i]);
if (vt=varNull) or (vt=varString) or (vt=varOleStr) then
cmd.Parameters.Append(cmd.CreateParameter('',adVariant,adParamInput,0,Values[i]))
else
cmd.Parameters.Append(cmd.CreateParameter('',vt,adParamInput,0,Values[i]));
end;
end;
function ErrInfo(const QueryName: AnsiString; const Values: array of Variant):AnsiString;
var
i,l:integer;
begin
l:=Length(Values);
Result:='';
if l>0 then
begin
Result:=VarToStr(Values[0]);
for i:=1 to l-1 do Result:=Result+','+VarToStr(Values[i]);
end;
Result:=#13#10'"'+QueryName+'"['+Result+']';
end;
{ TADOResult }
constructor TADOResult.Create(Connection: TADOLink; const SQL: WideString;
const Values: array of Variant);
var
cmd:Command;
begin
inherited Create;
//FRecordSet:=Session.DbCon.Execute(,v,adCmdText);
FFirstRead:=true;
cmd:=CoCommand.Create;
try
cmd.CommandType:=adCmdText;
cmd.CommandText:=SQL;
cmd.Set_ActiveConnection(Connection.FConnection);
CmdParameters(cmd,Values);
FRecordset:=CoRecordset.Create;
FRecordset.CursorLocation:=adUseClient;
FRecordset.Open(cmd,EmptyParam,adOpenStatic,adLockReadOnly,0);
except
on e:Exception do
begin
e.Message:=e.Message+ErrInfo(SQL,Values);
raise;
end;
end;
end;
constructor TADOResult.Create(Recordset: Recordset);
begin
inherited Create;
FFirstRead:=true;
FRecordSet:=Recordset;//Clone?
end;
destructor TADOResult.Destroy;
begin
//FRecordSet.Close;
FRecordSet:=nil;
inherited;
end;
procedure TADOResult.Reset;
begin
FFirstRead:=true;
FRecordSet.MoveFirst;
end;
function TADOResult.GetInt(const Idx: OleVariant): integer;
var
v:OleVariant;
begin
try
//v:=FRecordSet.Fields[Idx].Value;
v:=FRecordSet.Collect[idx];
except
on e:EOleException do
if cardinal(e.ErrorCode)=$800A0CC1 then
raise EFieldNotFound.Create('GetInt: Field not found: '+VarToStr(Idx))
else
begin
//if cardinal(e.ErrorCode)=$80004005 then Connection.Close;
raise;
end;
end;
if VarIsNull(v) then Result:=0 else Result:=v;
end;
function TADOResult.GetStr(const Idx: OleVariant): WideString;
begin
try
//Result:=VarToWideStr(FRecordSet.Fields[Idx].Value);
Result:=VarToWideStr(FRecordSet.Collect[Idx]);
except
on e:EOleException do
if cardinal(e.ErrorCode)=$800A0CC1 then
raise EFieldNotFound.Create('GetStr: Field not found: '+VarToStr(Idx))
else
begin
//if cardinal(e.ErrorCode)=$80004005 then Connection.Close;
raise;
end;
end;
end;
function TADOResult.GetDate(const Idx: OleVariant): TDateTime;
var
v:OleVariant;
begin
try
//v:=FRecordSet.Fields[Idx].Value;
v:=FRecordSet.Collect[Idx];
except
on e:EOleException do
if cardinal(e.ErrorCode)=$800A0CC1 then
raise EFieldNotFound.Create('GetDate: Field not found: '+VarToStr(Idx))
else
begin
//if cardinal(e.ErrorCode)=$80004005 then Connection.Close;
raise;
end;
end;
if VarIsNull(v) then
Result:=0 //Now?
else
Result:=VarToDateTime(v);
end;
function TADOResult.GetValue(Idx: OleVariant): OleVariant;
begin
try
//Result:=FRecordSet.Fields[Idx].Value;
Result:=FRecordSet.Collect[Idx];
except
on e:EOleException do
if cardinal(e.ErrorCode)=$800A0CC1 then
raise EFieldNotFound.Create('Field not found: '+VarToStr(Idx))
else
begin
//if cardinal(e.ErrorCode)=$80004005 then Connection.Close;
raise;
end;
end;
end;
function TADOResult.GetDefault(const Idx,Default: OleVariant): OleVariant;
begin
if FRecordSet.EOF then Result:=Default else Result:=GetValue(Idx);
end;
function TADOResult.IsNull(const Idx: OleVariant): boolean;
begin
try
//Result:=VarIsNull(FRecordSet.Fields[Idx].Value);
Result:=VarIsNull(FRecordSet.Collect[Idx]);
except
on e:EOleException do
begin
if cardinal(e.ErrorCode)=$800A0CC1 then
raise EFieldNotFound.Create('IsNull: Field not found: '+VarToStr(Idx))
else
begin
//if cardinal(e.ErrorCode)=$80004005 then Connection.Close;
raise;
end;
Result:=true;//counter warning
end;
end;
end;
function TADOResult.IsEof: boolean;
begin
Result:=FRecordSet.EOF;
end;
function TADOResult.Read: boolean;
begin
if (FRecordSet=nil) or FRecordSet.EOF then Result:=false else
begin
if FFirstRead then FFirstRead:=false else FRecordSet.MoveNext;
Result:=not(FRecordSet.EOF);
end;
end;
procedure TADOResult.CheckResultSet;
var
v:OleVariant;
begin
while (FRecordSet<>nil) and (FRecordSet.State=adStateClosed) do
FRecordSet:=FRecordSet.NextRecordset(v);
FFirstRead:=true;
end;
{ TADOLink }
constructor TADOLink.Create(const ConnectionString: WideString);
begin
inherited Create;
FConnection:=CoConnection.Create;
FConnection.Open(ConnectionString,'','',0);
end;
destructor TADOLink.Destroy;
begin
try
if FConnection<>nil then FConnection.Close;
except
//ignore
end;
FConnection:=nil;
inherited;
end;
function TADOLink.SingleValue(const SQL: WideString;
const Values: array of Variant): Variant;
var
cmd:Command;
rs:Recordset;
v:OleVariant;
begin
try
cmd:=CoCommand.Create;
cmd.CommandType:=adCmdText;
cmd.CommandText:=SQL;
cmd.Set_ActiveConnection(FConnection);
CmdParameters(cmd,Values);
rs:=cmd.Execute(v,EmptyParam,0);
if rs.EOF then
raise ESingleValueFailed.Create('SingleValue did not result a value "'+SQL+'"')
else
begin
Result:=rs.Fields[0].Value;
rs.MoveNext;
if not rs.EOF then
raise ESingleValueFailed.Create('SingleValue resulted in more than one value "'+SQL+'"')
end;
except
on e:Exception do
begin
e.Message:=e.Message+ErrInfo(SQL,Values);
raise;
end;
end;
end;
function TADOLink.Execute(const SQL: WideString;
const Values: array of Variant): integer;
var
cmd:Command;
v:OleVariant;
begin
try
cmd:=CoCommand.Create;
cmd.CommandType:=adCmdText;
cmd.CommandText:=SQL;
cmd.Set_ActiveConnection(FConnection);
CmdParameters(cmd,Values);
cmd.Execute(v,EmptyParam,0);//rs:=
//while (rs<>nil) and (rs.State=adStateClosed) do rs:=rs.NextRecordset(v);
Result:=v;
except
on e:Exception do
begin
e.Message:=e.Message+ErrInfo(SQL,Values);
raise;
end;
end;
end;
function TADOLink.Insert(const TableName: WideString;
const Values: array of OleVariant; const PKFieldName: WideString=''): int64;
var
rs:Recordset;
i,l:integer;
begin
l:=Length(Values);
if (l mod 2)<>0 then
raise Exception.Create('TADOLink.Insert expects pairs of name-value');
rs:=CoRecordset.Create;
//TODO: adCmdTable and find PK? first col?
rs.Open(
'SELECT * FROM ['+TableName+'] WHERE 0=1',
FConnection,
adOpenKeyset,//?
adLockOptimistic,//adLockPessimistic?
adCmdText);
rs.AddNew(EmptyParam,EmptyParam);
for i:=0 to (l div 2)-1 do
rs.Fields[Values[i*2]].Value:=Values[i*2+1];
if PKFieldName='' then
Result:=rs.Fields[0].Value //assert primary key
else
Result:=rs.Fields[PKFieldName].Value;
rs.Update(EmptyParam,EmptyParam);
rs:=nil;
end;
procedure TADOLink.Update(const TableName: WideString;
const Values: array of OleVariant);
var
cmd:Command;
rs:Recordset;
i,l:integer;
vt:TVarType;
begin
l:=Length(Values);
if (l mod 2)<>0 then
raise Exception.Create('TADOLink.Insert expects pairs of name-value');
cmd:=CoCommand.Create;
cmd.CommandType:=adCmdText;
cmd.CommandText:='SELECT * FROM ['+TableName+'] WHERE ['+VarToStr(Values[0])+']=?';
cmd.Set_ActiveConnection(FConnection);
vt:=VarType(Values[1]);
if (vt=varNull) or (vt=varString) or (vt=varOleStr) then
cmd.Parameters.Append(cmd.CreateParameter('',adVariant,adParamInput,0,Values[1]))
else
cmd.Parameters.Append(cmd.CreateParameter('',vt,adParamInput,0,Values[1]));
rs:=CoRecordset.Create;
//TODO: adCmdTable and find PK? first col?
rs.Open(
cmd,
FConnection,
adOpenKeyset,//?
adLockOptimistic,//adLockPessimistic?
integer(adCmdUnspecified));
for i:=2 to (l div 2)-1 do
rs.Fields[Values[i*2]].Value:=Values[i*2+1];
rs.Update(EmptyParam,EmptyParam);
rs:=nil;
cmd:=nil;
end;
end.