-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLibPQData.pas
977 lines (911 loc) · 23 KB
/
LibPQData.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
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
unit LibPQData;
{
LibPQData: thin LibPQ wrapper to connect to a PostgreSQL server.
https://github.com/stijnsanders/DataLank
ATTENTION:
Include following files in the folder that contains the executable,
or in a folder included in the default DLL search path.
They are provided with the Windows PostgreSQL server install.
libpq.dll
libssl-1_1.dll
libcrypto-1_1.dll
libiconv-2.dll
libintl-8.dll
}
interface
//debugging: prevent step-into from debugging TQueryResult calls:
{$D-}
{$L-}
{$DEFINE LIBPQDATA_TRANSFORMQM}
uses SysUtils, LibPQ;
type
TPostgresConnection=class(TObject)
private
FDB:PGConn;
procedure Exec(const SQL:UTF8String);
public
constructor Create(const ConnectionInfo: WideString);
destructor Destroy; override;
procedure BeginTrans;
procedure CommitTrans;
procedure RollbackTrans;
function Execute(const SQL: WideString;
const Values: array of Variant): integer;
function Insert(const TableName: UTF8String; const Values: array of Variant;
const PKFieldName: UTF8String=''): int64;
procedure Update(const TableName: UTF8String; const Values:array of Variant);
end;
TPostgresCommand=class(TObject)
private
FFirstRead:boolean;
function GetValue(Idx:Variant):Variant;
function IsEof:boolean;
function GetCount:integer;
protected
FDB:PGConn;
FRecordSet:PGResult;
FTuple:integer;
public
constructor Create(Connection: TPostgresConnection; const SQL: WideString;
const Values: array of Variant);
destructor Destroy; override;
procedure Reset;
function Read:boolean;
property Fields[Idx:Variant]:Variant read GetValue; default;
property EOF: boolean read IsEof;
property Count: integer read GetCount;
function GetInt(const Idx:Variant):integer;
function GetStr(const Idx:Variant):WideString;
function GetDate(const Idx:Variant):TDateTime;
function GetInterval(const Idx:Variant):TDateTime;
function IsNull(const Idx:Variant):boolean;
end;
EPostgres=class(Exception);
EQueryResultError=class(Exception);
function RefCursor(const CursorName:WideString):Variant;
implementation
uses Variants;
//hardcoded object ID's (defined by \include\server\catalog\pg_type.h)
const
Oid_bool = 16; //boolean, 'true'/'false'
Oid_bytea = 17; //variable-length string, binary values escaped
Oid_int8 = 20; //~18 digit integer, 8-byte storage
Oid_int2 = 21; //-32 thousand to 32 thousand, 2-byte storage
Oid_int4 = 23; //-2 billion to 2 billion integer, 4-byte storage
Oid_text = 25; //variable-length string, no limit specified
Oid_xml = 142; //XML content
Oid_float4 = 700; //single-precision floating point number, 4-byte storage
Oid_float8 = 701; //double-precision floating point number, 8-byte storage
Oid_unknown = 705; //(used with varNull below)
Oid_money = 790; //monetary amounts, $d,ddd.cc
Oid_bpchar = 1042; //char(length), blank-padded string, fixed storage length
Oid_varchar = 1043; //varchar(length), non-blank-padded string, variable storage length
Oid_date = 1082; //date
Oid_time = 1083; //time of day
Oid_timestamp = 1114; //date and time
Oid_timestamptz = 1184; //date and time with time zone
Oid_interval = 1186;
Oid_numeric = 1700; //numeric(precision, decimal), arbitrary precision number
Oid_refcursor = 1790; //reference to cursor (portal name)
Oid_uuid = 2950; //UUID datatype
var
RefCursorCatch:Variant;//see initialization
function RefCursor(const CursorName:WideString):Variant;
begin
//assert: caller does transaction!
//package a bespoke array with a reference to secret fixed thing,
//see AddParam that check this when VarType=varArray or varVariant
Result:=VarArrayCreate([0,1],varVariant);
Result[0]:=VarArrayRef(RefCursorCatch);
Result[1]:=CursorName;
end;
function AddParam(const v: Variant; var vt: Oid; var vs: UTF8String;
var vv: pointer; var vl: integer; var vf: integer): boolean;
var
ods:Char;
rds:PChar;
d:TDateTime;
const
NullStr:AnsiString=#0;
begin
rds:=@{$IF Declared(FormatSettings)}FormatSettings.{$IFEND}DecimalSeparator;
Result:=true;//default
//TODO: varArray
case VarType(v) of
varEmpty,varNull:
begin
vt:=Oid_unknown;
vs:='';
vv:=nil;
vl:=0;
vf:=0;
end;
varSmallint,varShortInt,varByte,varWord:
begin
vt:=Oid_int2;
vs:=UTF8String(VarToStr(v));//IntToStr?
vv:=@vs[1];
vl:=Length(vs);
vf:=0;
end;
varInteger,varLongWord:
begin
vt:=Oid_int4;
vs:=UTF8String(VarToStr(v));//IntToStr?
vv:=@vs[1];
vl:=Length(vs);
vf:=0;
end;
varInt64,$15{varUInt64}:
begin
vt:=Oid_int8;
vs:=UTF8Encode(VarToWideStr(v));//IntToStr64?
vv:=@vs[1];
vl:=Length(vs);
vf:=0;
end;
varSingle:
begin
ods:=rds^;
rds^:='.';
try
vt:=Oid_float4;
vs:=UTF8String(FloatToStr(v));
vv:=@vs[1];
vl:=Length(vs);
vf:=0;
finally
rds^:=ods;
end;
end;
varDouble,$E{varDecimal}:
begin
ods:=rds^;
rds^:='.';
try
vt:=Oid_float8;
vs:=UTF8String(FloatToStr(v));
vv:=@vs[1];
vl:=Length(vs);
vf:=0;
finally
rds^:=ods;
end;
end;
varCurrency:
begin
ods:=rds^;
rds^:='.';
try
vt:=Oid_money;
vs:=UTF8String(FloatToStr(v));
vv:=@vs[1];
vl:=Length(vs);
vf:=0;
finally
rds^:=ods;
end;
end;
varDate:
begin
d:=VarToDateTime(v);
vt:=Oid_timestamp;//?
if d=0.0 then
begin
vs:='';
vv:=nil;
vl:=0;
end
else
begin
vs:=UTF8String(FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz',d));
vv:=@vs[1];
vl:=Length(vs);
end;
vf:=0;
end;
varOleStr,varString,$0102{varUString}:
begin
vt:=Oid_varchar;//?Oid_text?
vs:=UTF8Encode(VarToWideStr(v));
if vs='' then vv:=@NullStr[1] else vv:=@vs[1];
vl:=Length(vs);
vf:=0;
end;
{
varDispatch,varUnknown:
begin
//check is XML doc?
vt:=Oid_xml;
vs:=UTF8Encode((IUnknown(v) as IXMLDOMNode).xml);
vv:=@vs[1];
vl:=Length(vs);
vf:=0;
end;
}
varBoolean:
begin
vt:=Oid_bool;
if v then vs:='t' else vs:='f';
vv:=@vs[1];
vl:=1;//Length(vs);
vf:=0;
end;
//varVariant...
//varRecord?
varStrArg:
begin
vt:=Oid_uuid;
vs:=UTF8Encode(VarToWideStr(v));
vv:=@vs[1];
vl:=Length(vs);
vf:=0;
end;
//varObject?
//varUStrArg?
//varAny?
//varUString?
varArray or varVariant:
if (VarArrayLowBound(v,1)=0) and (VarArrayHighBound(v,1)=1)
and (TVarData(v[0]).VPointer=@TVarData(RefCursorCatch).VArray) then
begin
vt:=Oid_refcursor;
vs:=UTF8Encode(VarToWideStr(v[1]));
vv:=@vs[1];
vl:=Length(vs);
vf:=0;
end
else
Result:=false;
else
Result:=false;
end;
end;
{$IF not Declared(UTF8ToWideString)}
function UTF8ToWideString(const s: UTF8String): WideString;
begin
Result:=UTF8Decode(s);
end;
{$IFEND}
function PrepSQL(const SQL: UTF8String): PAnsiChar;
var
s:UTF8String;
i,j,k,l:integer;
begin
{$IFDEF LIBPQDATA_TRANSFORMQM}
i:=1;
j:=1;
k:=0;
l:=Length(SQL);
SetLength(s,l*2);
while (i<=l) do
begin
while (i<=l) and (SQL[i]<>'?') and (SQL[i]<>'''') do
begin
s[j]:=SQL[i];
inc(i);
inc(j);
end;
if i<=l then
if SQL[i]='''' then
begin
s[j]:=SQL[i];//''''
inc(i);
inc(j);
while (i<=l) and (SQL[i]<>'''') do
begin
s[j]:=SQL[i];
inc(i);
inc(j);
end;
s[j]:=SQL[i];//''''
inc(i);
inc(j);
end
else
begin
//SQL[i]='?'
s[j]:='$';
inc(i);
inc(j);
inc(k);
if k<10 then
begin
s[j]:=AnsiChar(k or $30);
inc(j);
end
else
if k<100 then
begin
s[j]:=AnsiChar((k div 10) or $30);
inc(j);
s[j]:=AnsiChar((k mod 10) or $30);
inc(j);
end
else
raise EPostgres.Create('Maximum number of question marks exceeded');
end;
end;
SetLength(s,j-1);
Result:=@s[1];
{$ELSE}
Result:=@SQL[1];
{$ENDIF}
end;
procedure SendQuery(DB: PGConn; const SQL: UTF8String;
const Values: array of Variant);
var
i:integer;
pn:integer;
pt:array of Oid;
ps:array of UTF8String;
pv:array of pointer;
pl:array of integer;
pf:array of integer;
begin
pn:=Length(Values);
if pn=0 then
begin
if PQsendQuery(DB,PrepSQL(SQL))=0 then
raise EPostgres.Create(UTF8ToWideString(PQerrorMessage(DB)));
end
else
begin
SetLength(pt,pn);
SetLength(ps,pn);
SetLength(pv,pn);
SetLength(pl,pn);
SetLength(pf,pn);
for i:=0 to pn-1 do
if not AddParam(Values[i],pt[i],ps[i],pv[i],pl[i],pf[i]) then
raise Exception.Create('Unsupported Parameter Type: #'+IntToStr(i+1));
if PQsendQueryParams(DB,PrepSQL(SQL),pn,@pt[0],@pv[0],@pl[0],@pf[0],0)=0 then
raise EPostgres.Create(UTF8ToWideString(PQerrorMessage(DB)));
end;
end;
{ TPostgresConnection }
constructor TPostgresConnection.Create(const ConnectionInfo: WideString);
var
s,e:UTF8String;
begin
inherited Create;
s:=UTF8Encode(ConnectionInfo);
FDB:=PQconnectdb(@s[1]);
if FDB.Handle=nil then
raise EPostgres.Create('Connect failed');
e:=PQerrorMessage(FDB);
if e<>'' then
raise EPostgres.Create(UTF8ToWideString(e));
end;
destructor TPostgresConnection.Destroy;
begin
if FDB.Handle<>nil then
begin
PQfinish(FDB);
FDB.Handle:=nil;
end;
inherited;
end;
procedure TPostgresConnection.Exec(const SQL: UTF8String);
var
r:PGResult;
e:UTF8String;
begin
r:=PQexec(FDB,@SQL[1]);
if r.Handle=nil then
raise EPostgres.Create('Exec error '+UTF8ToWideString(PQerrorMessage(FDB)));
try
e:=PQresultErrorMessage(r);
if e<>'' then
raise EPostgres.Create(UTF8ToWideString(e));
finally
PQclear(r);
end;
end;
procedure TPostgresConnection.BeginTrans;
begin
Exec('begin');
//TODO: support savepoints
end;
procedure TPostgresConnection.CommitTrans;
begin
Exec('commit');
end;
procedure TPostgresConnection.RollbackTrans;
begin
Exec('rollback');
end;
function TPostgresConnection.Execute(const SQL: WideString;
const Values: array of Variant): integer;
var
r:PGResult;
s,e:UTF8String;
i:integer;
begin
try
SendQuery(FDB,UTF8Encode(SQL),Values);
Result:=0;//see below
r:=PQgetResult(FDB);
if r.Handle=nil then
e:=PQerrorMessage(FDB)
else
e:=PQresultErrorMessage(r);
if e<>'' then
raise EPostgres.Create(UTF8ToWideString(e));
while r.Handle<>nil do
begin
s:=PQcmdTuples(r);
if s<>'' then
if TryStrToInt(string(s),i) then inc(Result,i) else
raise EPostgres.Create('Unexpected Tuples Response: "'+
UTF8ToWideString(s)+'"');
PQclear(r);
r:=PQgetResult(FDB);
if r.Handle<>nil then
begin
e:=PQresultErrorMessage(r);
if e<>'' then
raise EPostgres.Create(UTF8ToWideString(e));
end;
end;
except
on e:Exception do
begin
r:=PQgetResult(FDB);
while r.Handle<>nil do
begin
PQclear(r);
r:=PQgetResult(FDB);
end;
raise;
end;
end;
end;
function TPostgresConnection.Insert(const TableName: UTF8String;
const Values: array of Variant; const PKFieldName: UTF8String=''): int64;
var
r:PGResult;
i,l:integer;
pn:integer;
pt:array of Oid;
ps:array of UTF8String;
pv:array of pointer;
pl:array of integer;
pf:array of integer;
sql1,sql2,e:UTF8String;
begin
sql1:='';
sql2:='';
l:=Length(Values);
if (l and 1)<>0 then
raise EQueryResultError.Create('Insert('''+string(TableName)+''') requires an even number of values');
pn:=l div 2;
SetLength(pt,pn);
SetLength(ps,pn);
SetLength(pv,pn);
SetLength(pl,pn);
SetLength(pf,pn);
pn:=0;//re-count, see below
i:=1;
while i<l do
begin
if not VarIsNull(Values[i]) then
begin
sql1:=sql1+','+UTF8Encode(VarToWideStr(Values[i-1]));
if not AddParam(Values[i],pt[pn],ps[pn],pv[pn],pl[pn],pf[pn]) then
raise Exception.Create('Unsupported Parameter Type: TableName="'+string(TableName)+'" #'+IntToStr((i div 2)+1));
inc(pn);
sql2:=sql2+',$'+UTF8String(IntToStr(pn));
end;
inc(i,2);
end;
//TODO: check TableName,Values[i*2] on sql-safe!
sql1[1]:='(';
sql2[1]:='(';
if PKFieldName='' then
sql2:=sql2+')'
else
sql2:=sql2+') returning '+PKFieldName;
sql1:='insert into "'+TableName+'" '+sql1+') values '+sql2;
if PQsendQueryParams(FDB,@sql1[1],pn,@pt[0],@pv[0],@pl[0],@pf[0],0)=0 then
raise EPostgres.Create(UTF8ToWideString(PQerrorMessage(FDB)));
r:=PQgetResult(FDB);
if r.Handle=nil then
e:=PQerrorMessage(FDB)
else
e:=PQresultErrorMessage(r);
if e<>'' then
raise EPostgres.Create(UTF8ToWideString(e));
if PQntuples(r)=0 then
Result:=-1
else
begin
e:=PQgetvalue(r,0,0);
if e='' then Result:=-1 else Result:=StrToInt64(string(e));
end;
while r.Handle<>nil do
begin
PQclear(r);
r:=PQgetResult(FDB);
end;
end;
procedure TPostgresConnection.Update(const TableName: UTF8String; const Values: array of Variant);
var
r:PGResult;
i,l:integer;
pn:integer;
pt:array of Oid;
ps:array of UTF8String;
pv:array of pointer;
pl:array of integer;
pf:array of integer;
sql1,sql2,e:UTF8String;
begin
sql1:='';
sql2:='';
l:=Length(Values);
if (l and 1)<>0 then
raise EQueryResultError.Create('Update('''+string(TableName)+''') requires an even number of values');
pn:=l div 2;
SetLength(pt,pn);
SetLength(ps,pn);
SetLength(pv,pn);
SetLength(pl,pn);
SetLength(pf,pn);
pn:=0;//re-count, see below
i:=1;
while i<l do
begin
if not VarIsNull(Values[i]) then
begin
if not AddParam(Values[i],pt[pn],ps[pn],pv[pn],pl[pn],pf[pn]) then
raise Exception.Create('Unsupported Parameter Type: TableName="'+string(TableName)+'" #'+IntToStr((i div 2)+1));
inc(pn);
if pn=1 then
sql2:=' where '+UTF8Encode(VarToWideStr(Values[i-1]))+'=$1'//'+IntToStr(i)
else
sql1:=sql1+','+UTF8Encode(VarToWideStr(Values[i-1]))+'=$'+UTF8String(IntToStr(pn));
end;
inc(i,2);
end;
sql1[1]:=' ';
sql1:='update "'+TableName+'" set'+sql1+sql2;
if PQsendQueryParams(FDB,@sql1[1],pn,@pt[0],@pv[0],@pl[0],@pf[0],0)=0 then
raise EPostgres.Create(UTF8ToWideString(PQerrorMessage(FDB)));
r:=PQgetResult(FDB);
if r.Handle=nil then
e:=PQerrorMessage(FDB)
else
e:=PQresultErrorMessage(r);
if e<>'' then
raise EPostgres.Create(UTF8ToWideString(e));
while r.Handle<>nil do
begin
PQclear(r);
r:=PQgetResult(FDB);
end;
end;
{ TPostgresCommand }
constructor TPostgresCommand.Create(Connection: TPostgresConnection;
const SQL: WideString; const Values: array of Variant);
var
e:UTF8String;
r:PGResult;
begin
inherited Create;
//TODO: check PQisbusy?
try
FDB:=Connection.FDB;
SendQuery(FDB,UTF8Encode(SQL),Values);
//PQsetSingleRowMode(QueryDbConLive); //TODO!!
FTuple:=0;
FRecordSet:=PQgetResult(FDB);
if FRecordSet.Handle=nil then
e:=PQerrorMessage(FDB)
else
e:=PQresultErrorMessage(FRecordSet);
if e<>'' then
raise EPostgres.Create(UTF8ToWideString(e));
FFirstRead:=true;
except
if FRecordSet.Handle<>nil then
PQclear(FRecordSet);
r:=PQgetResult(FDB);
while r.Handle<>nil do
begin
PQclear(r);
r:=PQgetResult(FDB);
end;
raise;
end;
end;
destructor TPostgresCommand.Destroy;
begin
while FRecordSet.Handle<>nil do
begin
PQclear(FRecordSet);
FRecordSet:=PQgetResult(FDB);
end;
inherited;
end;
function TPostgresCommand.Read: boolean;
begin
if (FRecordSet.Handle=nil) or (PQntuples(FRecordSet)=FTuple) then Result:=false else
begin
if FFirstRead then FFirstRead:=false else
begin
{if streaming then
begin
if FRecordSet<>nil then PQclear(FRecordSet);
FRecordSet:=PQgetResult(FRecordSet);
end
else
}
inc(FTuple);
end;
Result:=not((FRecordSet.Handle=nil) or (PQntuples(FRecordSet)=FTuple));
end;
end;
procedure TPostgresCommand.Reset;
begin
FFirstRead:=true;
if FTuple<>0 then FTuple:=0;
end;
function TPostgresCommand.GetInt(const Idx: Variant): integer;
var
i:integer;
s:UTF8String;
begin
if IsEOF then raise EQueryResultError.Create('Reading past EOF.');
if VarIsNumeric(Idx) then i:=Idx else
begin
s:=UTF8String(VarToStr(Idx));
i:=PQfnumber(FRecordSet,@s[1]);
end;
if i=-1 then
raise EQueryResultError.Create('GetInt: Field not found: '+VarToStr(Idx));
if PQgetisnull(FRecordSet,FTuple,i)=0 then
Result:=StrToInt(string(PQgetvalue(FRecordSet,FTuple,i)))
else
Result:=0;//?
end;
function TPostgresCommand.GetStr(const Idx: Variant): WideString;
var
i:integer;
s:UTF8String;
begin
if IsEOF then raise EQueryResultError.Create('Reading past EOF.');
if VarIsNumeric(Idx) then i:=Idx else
begin
s:=UTF8String(VarToStr(Idx));
i:=PQfnumber(FRecordSet,@s[1]);
end;
if i=-1 then
raise EQueryResultError.Create('GetStr: Field not found: '+VarToStr(Idx));
if PQgetisnull(FRecordSet,FTuple,i)=0 then
Result:=UTF8ToWideString(PQgetvalue(FRecordSet,FTuple,i))
else
Result:='';//?
end;
function TPostgresCommand.GetDate(const Idx: Variant): TDateTime;
var
i,l,f:integer;
dy,dm,dd,th,tm,ts,tz:word;
s:UTF8String;
function Next:word;
begin
Result:=0;
while (i<=l) and (s[i] in ['0'..'9']) do
begin
Result:=Result*10+(byte(s[i]) and $F);
inc(i);
end;
end;
begin
if IsEOF then raise EQueryResultError.Create('Reading past EOF.');
if VarIsNumeric(Idx) then i:=Idx else
begin
s:=UTF8String(VarToStr(Idx));
i:=PQfnumber(FRecordSet,@s[1]);
end;
if i=-1 then
raise EQueryResultError.Create('GetDate: Field not found: '+VarToStr(Idx));
if PQgetisnull(FRecordSet,FTuple,i)=0 then
begin
s:=PQgetvalue(FRecordSet,FTuple,i);
i:=1;
l:=Length(s);
dy:=Next;
inc(i);//'-'
dm:=Next;
inc(i);//'-'
dd:=Next;
inc(i);//' '
th:=Next;
inc(i);//':'
tm:=Next;
inc(i);//':'
ts:=Next;
inc(i);//'.'
tz:=0;//Next;//more precision than milliseconds here, encode floating:
f:=24*60*60;
Result:=0.0;
while (i<=l) and (s[i] in ['0'..'9']) do
begin
f:=f*10;
Result:=Result+(byte(s[i]) and $F)/f;
inc(i);
end;
//assert i>l
Result:=EncodeDate(dy,dm,dd)+EncodeTime(th,tm,ts,tz)+Result;
end
else
Result:=0;//Now?
end;
function TPostgresCommand.GetInterval(const Idx: Variant): TDateTime;
var
i,dd,l,f:integer;
th,tm,ts,tz:word;
s:UTF8String;
function Next:word;
begin
Result:=0;
while (i<=l) and (s[i] in ['0'..'9']) do
begin
Result:=Result*10+(byte(s[i]) and $F);
inc(i);
end;
end;
begin
if IsEOF then raise EQueryResultError.Create('Reading past EOF.');
if VarIsNumeric(Idx) then i:=Idx else
begin
s:=UTF8String(VarToStr(Idx));
i:=PQfnumber(FRecordSet,@s[1]);
end;
if i=-1 then
raise EQueryResultError.Create('GetDate: Field not found: '+VarToStr(Idx));
if PQgetisnull(FRecordSet,FTuple,i)=0 then
begin
s:=PQgetvalue(FRecordSet,FTuple,i);
i:=1;
l:=Length(s);
dd:=Next;
if s[i]=' ' then//' days '
begin
inc(i);
if (i<=l) and (s[i]='d') then inc(i) else i:=l+1;
if (i<=l) and (s[i]='a') then inc(i) else i:=l+1;
if (i<=l) and (s[i]='y') then inc(i) else i:=l+1;
if (i<=l) and (s[i]='s') then inc(i) else i:=l+1;
if (i<=l) and (s[i]=' ') then inc(i) else i:=l+1;
th:=Next;
end
else
begin
th:=dd;
dd:=0;
end;
while th>=24 do
begin
inc(dd);
dec(th,24);
end;
inc(i);//':';
tm:=Next;
inc(i);//':'
ts:=Next;
inc(i);//'.'
tz:=0;//Next;//more precision than milliseconds here, encode floating:
f:=24*60*60;
Result:=0.0;
while (i<=l) and (s[i] in ['0'..'9']) do
begin
f:=f*10;
Result:=Result+(byte(s[i]) and $F)/f;
inc(i);
end;
//assert i>l
Result:=dd+EncodeTime(th,tm,ts,tz)+Result;
end
else
Result:=0;
end;
function TPostgresCommand.GetValue(Idx: Variant): Variant;
var
i:integer;
s:UTF8String;
ods:char;
rds:PChar;
begin
if IsEOF then raise EQueryResultError.Create('Reading past EOF.');
rds:=@{$IF Declared(FormatSettings)}FormatSettings.{$IFEND}DecimalSeparator;
if VarIsNumeric(Idx) then i:=Idx else
begin
s:=UTF8String(VarToStr(Idx));
i:=PQfnumber(FRecordSet,@s[1]);
end;
if i=-1 then
raise EQueryResultError.Create('Field not found: '+VarToStr(Idx));
if PQgetisnull(FRecordSet,FTuple,i)=0 then
begin
s:=PQgetvalue(FRecordSet,FTuple,i);
case PQftype(FRecordset,i) of
Oid_bool:Result:=s='t';
//Oid_bytea
Oid_int8:Result:=StrToInt64(string(s));
Oid_int2:Result:=Word(StrToInt(string(s)));
Oid_int4:Result:=StrToInt(string(s));
{
Oid_xml:
begin
d:=CreateComObject(CLASS_DOMDocument60) as DOMDocument60;
d.async:=false;
d.preserveWhiteSpace:=true;//?
if not d.loadXML(UTF8ToWideString(s)) then
raise EQueryResultError.Create('Field holds invalid XML: '+VarToStr(Idx)+' '+d.parseError.reason);
Result:=d;
end;
}
Oid_float4,Oid_float8,Oid_numeric:
begin
ods:=rds^;
rds^:='.';
try
Result:=StrToFloat(string(s));
finally
rds^:=ods;
end;
end;
Oid_money:
begin
ods:=rds^;
rds^:='.';
try
Result:=StrToCurr(string(s));
finally
rds^:=ods;
end;
end;
Oid_bpchar,Oid_varchar,Oid_text:Result:=UTF8ToWideString(s);
//Oid_date
//Oid_time
Oid_timestamp:Result:=GetDate(Idx);
Oid_interval:Result:=GetInterval(Idx);
//Oid_timestamptz
//Oid_uuid
else
raise EQueryResultError.Create('Unsupported result type oid='+
IntToStr(PQftype(FRecordset,i))+': '+VarToStr(Idx));
end;
end
else
Result:=Null;
end;
function TPostgresCommand.IsNull(const Idx: Variant): boolean;
var
i:integer;
s:UTF8String;
begin
if IsEOF then raise EQueryResultError.Create('Reading past EOF.');
if VarIsNumeric(Idx) then i:=Idx else
begin
s:=UTF8String(VarToStr(Idx));
i:=PQfnumber(FRecordSet,@s[1]);
end;
if i=-1 then
raise EQueryResultError.Create('IsNull: Field not found: '+VarToStr(Idx));
Result:=PQgetisnull(FRecordSet,FTuple,i)<>0;
end;
function TPostgresCommand.IsEof: boolean;
begin
Result:=(FRecordSet.Handle=nil) or (PQntuples(FRecordSet)=FTuple);
end;
function TPostgresCommand.GetCount: integer;
begin
if FRecordSet.Handle=nil then Result:=-1 else Result:=PQntuples(FRecordSet);
end;
initialization
//something fixed invalid, see function RefCursor
RefCursorCatch:=VarArrayCreate([0,0],varError);
end.