-
-
Notifications
You must be signed in to change notification settings - Fork 563
Expand file tree
/
Copy pathdbstructures.postgresql.pas
More file actions
751 lines (736 loc) · 27.1 KB
/
dbstructures.postgresql.pas
File metadata and controls
751 lines (736 loc) · 27.1 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
unit dbstructures.postgresql;
interface
uses
dbstructures, StrUtils;
type
// PostgreSQL structures
TPQConnectStatus = (CONNECTION_OK, CONNECTION_BAD, CONNECTION_STARTED, CONNECTION_MADE, CONNECTION_AWAITING_RESPONSE, CONNECTION_AUTH_OK, CONNECTION_SETENV, CONNECTION_SSL_STARTUP, CONNECTION_NEEDED);
PPGconn = Pointer;
PPGresult = Pointer;
POid = Cardinal; // Object ID is a fundamental type in Postgres.
TPostgreSQLLib = class(TDbLib)
PQconnectdb: function(const ConnInfo: PAnsiChar): PPGconn cdecl;
PQerrorMessage: function(const Handle: PPGconn): PAnsiChar cdecl;
PQresultErrorMessage: function(const Result: PPGresult): PAnsiChar cdecl;
PQresultErrorField: function(const Result: PPGresult; fieldcode: Integer): PAnsiChar;
PQfinish: procedure(const Handle: PPGconn);
PQstatus: function(const Handle: PPGconn): TPQConnectStatus cdecl;
PQsendQuery: function(const Handle: PPGconn; command: PAnsiChar): Integer cdecl;
PQgetResult: function(const Handle: PPGconn): PPGresult cdecl;
PQbackendPID: function(const Handle: PPGconn): Integer cdecl;
PQcmdTuples: function(Result: PPGresult): PAnsiChar; cdecl;
PQntuples: function(Result: PPGresult): Integer; cdecl;
PQclear: procedure(Result: PPGresult); cdecl;
PQnfields: function(Result: PPGresult): Integer; cdecl;
PQfname: function(const Result: PPGresult; column_number: Integer): PAnsiChar; cdecl;
PQftype: function(const Result: PPGresult; column_number: Integer): POid; cdecl;
PQftable: function(const Result: PPGresult; column_number: Integer): POid; cdecl;
PQgetvalue: function(const Result: PPGresult; row_number: Integer; column_number: Integer): PAnsiChar; cdecl;
PQgetlength: function(const Result: PPGresult; row_number: Integer; column_number: Integer): Integer; cdecl;
PQgetisnull: function(const Result: PPGresult; row_number: Integer; column_number: Integer): Integer; cdecl;
PQlibVersion: function(): Integer; cdecl;
protected
procedure AssignProcedures; override;
end;
TPostgreSQLProvider = class(TSqlProvider)
public
function GetSql(AId: TQueryId): string; override;
end;
const InvalidOid: POid = 0;
var
PostgreSQLDatatypes: Array[0..39] of TDBDatatype =
(
(
Index: dbdtUnknown;
NativeTypes: '99999';
Name: 'UNKNOWN';
Description: 'Unknown data type';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: False;
Category: dtcOther;
),
(
Index: dbdtSmallint;
NativeTypes: '21';
Name: 'SMALLINT';
Names: 'smallint|int2';
Description: 'Small-range integer. Range: -32768 to +32767. Storage Size: 2 Bytes.';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: False;
ValueMustMatch: '^\d{1,5}$';
Category: dtcInteger;
),
(
Index: dbdtInt;
// 26 = oid, 28 = xid
NativeTypes: '23|26|28';
Name: 'INTEGER';
Names: 'integer|int4|int|oid|xid';
Description: 'Typical choice for integer. Range: -2147483648 to +2147483647. Storage Size: 4 Bytes.';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: False;
ValueMustMatch: '^\d{1,10}$';
Category: dtcInteger;
),
(
Index: dbdtBigint;
NativeTypes: '20';
Name: 'BIGINT';
Names: 'bigint|int8';
Description: 'Large-range integer. Range: -9223372036854775808 to 9223372036854775807. Storage Size: 8 Bytes.';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: False;
ValueMustMatch: '^\d{1,19}$';
Category: dtcInteger;
),
(
Index: dbdtSerial;
Name: 'SERIAL';
Names: 'serial|serial4';
Description: 'Autoincrementing integer. Range: 1 to 2147483647. Storage Size: 4 Bytes.';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: False;
Category: dtcInteger;
),
(
Index: dbdtBigSerial;
Name: 'BIGSERIAL';
Names: 'bigserial|serial8';
Description: 'Large autoincrementing integer. Range: 1 to 9223372036854775807. Storage Size: 8 Bytes.';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: False;
Category: dtcInteger;
),
(
Index: dbdtVarBit;
NativeTypes: '1562';
Name: 'BIT VARYING';
Names: 'bit varying|varbit';
Description: 'Variable-length bit string.';
HasLength: True;
RequiresLength: False;
HasBinary: False;
HasDefault: True;
LoadPart: False;
Category: dtcInteger;
),
(
Index: dbdtBit;
NativeTypes: '1560';
Name: 'BIT';
Names: 'bit';
Description: 'Fixed-length bit string.';
HasLength: True;
RequiresLength: False;
HasBinary: False;
HasDefault: True;
LoadPart: False;
Category: dtcInteger;
),
(
Index: dbdtNumeric;
NativeTypes: '1700';
Name: 'NUMERIC';
Names: 'numeric|float8|decimal';
Description: 'User-specified precision, exact. Range: no limit. Storage Size: variable.';
HasLength: True;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: False;
Category: dtcReal;
),
(
Index: dbdtReal;
NativeTypes: '700';
Name: 'REAL';
Names: 'real|float4';
Description: 'Variable-precision, inexact. Range: 6 decimal digits precision. Storage Size: 4 Bytes.';
HasLength: True;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: False;
Category: dtcReal;
),
(
Index: dbdtDoublePrecision;
NativeTypes: '701|1700';
Name: 'DOUBLE PRECISION';
Names: 'double precision|float8';
Description: 'Variable-precision, inexact. Range: 15 decimal digits precision. Storage Size: 8 Bytes.';
HasLength: True;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: False;
Category: dtcReal;
),
(
Index: dbdtChar;
NativeTypes: '18|1042';
Name: 'CHAR';
Names: 'CHARACTER';
Description: 'Fixed-length, blank padded.';
HasLength: True;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: True;
Category: dtcText;
),
(
Index: dbdtVarchar;
NativeTypes: '18|19|24|1043|1043';
Name: 'VARCHAR';
Names: 'char|bpchar|varchar|name|enum|character varying';
Description: 'Variable-length with limit.';
HasLength: True;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: True;
Category: dtcText;
),
(
Index: dbdtText;
NativeTypes: '25|22|30|143|629|651|719|791|1000|1028|1040|1041|1115|1182|1183|1185|1187|1231|1263|1270|1561|1563|2201|2207|2211|2949|2951|3643|3644|3645|3735|3770';
Name: 'TEXT';
Names: 'text|int2vector|oidvector|bool';
Description: 'Variable unlimited length.';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: True;
Category: dtcText;
),
(
Index: dbdtCiText;
NativeTypes: '?';
Name: 'CITEXT';
Names: 'citext';
Description: 'A case-insensitive character string type. Essentially, it internally calls lower when comparing values. Otherwise, it behaves almost exactly like text.';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: True;
Category: dtcText;
),
(
Index: dbdtCidr;
NativeTypes: '650';
Name: 'CIDR';
Names: 'cidr';
Description: 'IPv4 and IPv6 networks. Storage size: 7 or 19 bytes';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: False;
Category: dtcText;
),
(
Index: dbdtInet;
NativeTypes: '869';
Name: 'INET';
Names: 'inet';
Description: 'IPv4 and IPv6 hosts and networks. Storage size: 7 or 19 bytes';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: False;
Category: dtcText;
),
(
Index: dbdtMacaddr;
NativeTypes: '829';
Name: 'MACADDR';
Names: 'macaddr';
Description: 'MAC addresses. Storage size: 6 bytes';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: False;
Category: dtcText;
),
(
Index: dbdtMoney;
NativeTypes: '790';
Name: 'MONEY';
Description: 'Currency amount. Range: -92233720368547758.08 to +92233720368547758.07. Storage Size: 8 Bytes.';
HasLength: True;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: False;
Category: dtcText;
),
(
Index: dbdtDate;
NativeTypes: '1082';
Name: 'DATE';
Description: 'Calendar date (year, month, day).';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: False;
Format: 'yyyy-mm-dd';
Category: dtcTemporal;
),
(
Index: dbdtTime;
NativeTypes: '1083';
Name: 'TIME';
Description: 'Time of day.';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: False;
Format: 'hh:nn:ss';
Category: dtcTemporal;
),
(
Index: dbdtDatetime;
NativeTypes: '1082|1114|702';
Name: 'TIMESTAMP';
Names: 'timestamp|datetime|abstime|timestamp without time zone';
Description: 'Date and time without timezone, e.g. "2020-06-27 16:24:41".';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: False;
Format: 'yyyy-mm-dd hh:nn:ss';
Category: dtcTemporal;
),
(
Index: dbdtDatetime2;
NativeTypes: '1184';
Name: 'TIMESTAMPTZ';
Names: 'timestamptz|timestamp with time zone';
Description: 'Date and time with time zone, e.g. "2020-06-27 16:24:41+02".';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: False;
Format: 'yyyy-mm-dd hh:nn:ss';
Category: dtcTemporal;
),
(
Index: dbdtDate;
NativeTypes: '1082';
Name: 'DATE';
Description: 'Calendar date (year, month, day).';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: False;
Format: 'yyyy-mm-dd';
Category: dtcTemporal;
),
(
Index: dbdtInterval;
NativeTypes: '1186';
Name: 'INTERVAL';
Description: 'time interval from -178000000 years to 178000000 years';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: False;
Format: 'yyyy-mm-dd hh:nn:ss';
Category: dtcTemporal;
),
(
Index: dbdtBlob;
NativeTypes: '17';
Name: 'BYTEA';
Description: 'Binary data ("byte array").';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: True;
Category: dtcBinary;
),
(
Index: dbdtPoint;
NativeTypes: '600';
Name: 'POINT';
Description: 'Point on a plane (x,y). Storage size: 16 bytes.';
HasLength: True;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: False;
Category: dtcSpatial;
),
(
Index: dbdtLinestring;
NativeTypes: '628';
Name: 'LINE';
Description: 'Infinite line ((x1,y1),(x2,y2)). Storage size: 32 bytes.';
HasLength: True;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: False;
Category: dtcSpatial;
),
(
Index: dbdtLineSegment;
NativeTypes: '601';
Name: 'LSEG';
Description: 'Finite line segment ((x1,y1),(x2,y2)). Storage size: 32 bytes.';
HasLength: True;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: False;
Category: dtcSpatial;
),
(
Index: dbdtBox;
NativeTypes: '603';
Name: 'BOX';
Description: 'Rectangular box ((x1,y1),(x2,y2)). Storage size: 32 bytes.';
HasLength: True;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: False;
Category: dtcSpatial;
),
(
Index: dbdtPath;
NativeTypes: '602';
Name: 'PATH';
Description: 'Closed path (similar to polygon) ((x1,y1),...). Storage size: 16+16n bytes.';
HasLength: True;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: False;
Category: dtcSpatial;
),
(
Index: dbdtPolygon;
NativeTypes: '604';
Name: 'POLYGON';
Description: 'Closed path (similar to polygon) ((x1,y1),...). Storage size: 40+16n bytes.';
HasLength: True;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: False;
Category: dtcSpatial;
),
(
Index: dbdtCircle;
NativeTypes: '718';
Name: 'CIRCLE';
Description: 'Circle <(x,y),r> (center point and radius). Storage size: 24 bytes.';
HasLength: True;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: False;
Category: dtcSpatial;
),
(
Index: dbdtBool;
NativeTypes: '16';
Name: 'BOOLEAN';
Names: 'boolean|bool';
Description: 'State of true or false. Storage size: 1 byte.';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: False;
ValueMustMatch: '^(true|false)$';
Category: dtcOther;
),
(
Index: dbdtRegClass;
NativeTypes: '2205';
Name: 'REGCLASS';
Names: 'regclass';
Description: 'Relation name';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: False;
Category: dtcOther;
),
(
Index: dbdtRegProc;
NativeTypes: '24';
Name: 'REGPROC';
Names: 'regproc|regprocedure';
Description: 'Function name';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: False;
Category: dtcOther;
),
(
Index: dbdtEnum;
NativeTypes: 'e';
Name: 'ENUM';
Names: '';
Description: 'A list of quoted labels, each of which must be less than NAMEDATALEN bytes long (64 bytes in a standard PostgreSQL build)';
HasLength: True; // Enables the Length/set field in table editor
RequiresLength: True;
HasBinary: False;
HasDefault: True;
LoadPart: False;
Category: dtcOther;
),
(
Index: dbdtJson;
NativeTypes: '114';
Name: 'JSON';
Names: 'json';
Description: 'JavaScript Object Notation data';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: False;
Category: dtcText;
),
(
Index: dbdtJsonB;
NativeTypes: '3802';
Name: 'JSONB';
Names: 'jsonb';
Description: 'JavaScript Object Notation data in a binary form';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: False;
Category: dtcText;
),
(
Index: dbdtUniqueidentifier;
NativeTypes: '2950';
Name: 'UUID';
Names: 'uuid';
Description: 'The data type uuid stores Universally Unique Identifiers (UUID) as defined by RFC 4122, ISO/IEC 9834-8:2005, and related standards.';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: False;
ValueMustMatch: '^\{?[a-f0-9]{8}-?[a-f0-9]{4}-?[a-f0-9]{4}-?[a-f0-9]{4}-?[a-f0-9]{12}\}?$';
Category: dtcText;
)
);
implementation
procedure TPostgreSQLLib.AssignProcedures;
begin
AssignProc(@PQconnectdb, 'PQconnectdb');
AssignProc(@PQerrorMessage, 'PQerrorMessage');
AssignProc(@PQresultErrorMessage, 'PQresultErrorMessage');
AssignProc(@PQresultErrorField, 'PQresultErrorField');
AssignProc(@PQfinish, 'PQfinish');
AssignProc(@PQstatus, 'PQstatus');
AssignProc(@PQsendQuery, 'PQsendQuery');
AssignProc(@PQgetResult, 'PQgetResult');
AssignProc(@PQbackendPID, 'PQbackendPID');
AssignProc(@PQcmdTuples, 'PQcmdTuples');
AssignProc(@PQntuples, 'PQntuples');
AssignProc(@PQclear, 'PQclear');
AssignProc(@PQnfields, 'PQnfields');
AssignProc(@PQfname, 'PQfname');
AssignProc(@PQftype, 'PQftype');
AssignProc(@PQftable, 'PQftable');
AssignProc(@PQgetvalue, 'PQgetvalue');
AssignProc(@PQgetlength, 'PQgetlength');
AssignProc(@PQgetisnull, 'PQgetisnull');
AssignProc(@PQlibVersion, 'PQlibVersion');
end;
{ TPostgreSQLProvider }
function TPostgreSQLProvider.GetSql(AId: TQueryId): string;
begin
case AId of
qDatabaseDrop: Result := 'DROP SCHEMA %s';
qRenameTable: Result := 'ALTER TABLE %s RENAME TO %s';
qRenameView: Result := 'ALTER VIEW %s RENAME TO %s';
qCurrentUserHost: Result := 'SELECT CURRENT_USER';
qLikeCompare: Result := '%s ILIKE %s';
qAddColumn: Result := 'ADD %s';
qChangeColumn: Result := 'ALTER COLUMN %s %s';
qRenameColumn: Result := 'RENAME COLUMN %s TO %s';
qForeignKeyEventAction: Result := 'RESTRICT,CASCADE,SET NULL,NO ACTION,SET DEFAULT';
qSessionVariables: Result := 'SHOW ALL';
qGlobalVariables: Result := 'SHOW ALL';
qISSchemaCol: Result := '%s_schema';
qUSEQuery: Result := 'SET search_path TO %s';
qKillQuery: Result := 'SELECT pg_cancel_backend(%d)';
qKillProcess: Result := 'SELECT pg_cancel_backend(%d)';
qFuncLength: Result := 'LENGTH';
qFuncCeil: Result := 'CEIL';
qFuncLeft: Result := 'SUBSTRING(%s, 1, %d)';
qFuncNow: Result := 'NOW()';
qFuncLastAutoIncNumber: Result := 'LASTVAL()';
qLockedTables: Result := '';
qDisableForeignKeyChecks: Result := '';
qEnableForeignKeyChecks: Result := '';
qForeignKeyDrop: Result := 'DROP CONSTRAINT %s';
// This uses pg_attribute.attgenerated, which only exists starting in PostgreSQL 12
qGetTableColumns: Result := IfThen(
FServerVersion >= 120000,
'SELECT ' +
' n.nspname AS table_schema, ' +
' c.relname AS table_name, ' +
' a.attname AS column_name, ' +
' a.attnum AS ordinal_position, ' +
' pg_catalog.format_type(a.atttypid, a.atttypmod) AS data_type, ' +
// YES/NO like information_schema.is_nullable
' CASE ' +
' WHEN a.attnotnull THEN ''NO'' ' +
' ELSE ''YES'' ' +
' END AS is_nullable, ' +
// Character maximum length (in characters)
' CASE ' +
' WHEN (bt.typcategory = ''S'' OR (bt.oid IS NULL AND t.typcategory = ''S'')) ' +
' AND a.atttypmod <> -1 ' +
' THEN a.atttypmod - 4 ' +
' ELSE NULL ' +
' END AS character_maximum_length, ' +
// Numeric precision / scale (NULL for non-numeric)
' CASE ' +
' WHEN (bt.typcategory IN (''N'',''F'')) OR (bt.oid IS NULL AND t.typcategory IN (''N'',''F'')) ' +
' THEN ' +
' CASE ' +
' WHEN a.atttypmod = -1 THEN NULL ' +
' ELSE ((a.atttypmod - 4) >> 16)::integer ' +
' END ' +
' END AS numeric_precision, ' +
' CASE ' +
' WHEN (bt.typcategory IN (''N'',''F'')) OR (bt.oid IS NULL AND t.typcategory IN (''N'',''F'')) ' +
' THEN ' +
' CASE ' +
' WHEN a.atttypmod = -1 THEN NULL ' +
' ELSE ((a.atttypmod - 4) & 65535)::integer ' +
' END ' +
' END AS numeric_scale, ' +
// Datetime precision (for time/timestamp/interval)
' CASE ' +
' WHEN (bt.typcategory = ''D'' OR (bt.oid IS NULL AND t.typcategory = ''D'')) ' +
' AND a.atttypmod <> -1 ' +
' THEN a.atttypmod ' +
' ELSE NULL ' +
' END AS datetime_precision, ' +
// Character set name: PostgreSQL has one per DB; mimic information_schema
' CASE ' +
' WHEN (bt.typcategory = ''S'' OR (bt.oid IS NULL AND t.typcategory = ''S'')) ' +
' THEN current_database() ' +
' ELSE NULL ' +
' END AS character_set_name, ' +
// Collation name for collatable columns
' CASE ' +
' WHEN (bt.typcategory = ''S'' OR (bt.oid IS NULL AND t.typcategory = ''S'')) ' +
' THEN ' +
' CASE ' +
' WHEN a.attcollation <> t.typcollation ' +
' THEN coll.collname ' +
' ELSE NULL ' +
' END ' +
' ELSE NULL ' +
' END AS collation_name, ' +
// Default expression for non-generated columns
' CASE ' +
' WHEN a.attgenerated = '''' AND a.atthasdef ' +
' THEN pg_get_expr(ad.adbin, ad.adrelid) ' +
' ELSE NULL ' +
' END AS column_default, ' +
// Generation expression for generated columns
' CASE ' +
' WHEN a.attgenerated <> '''' AND a.atthasdef ' +
' THEN pg_get_expr(ad.adbin, ad.adrelid) ' +
' ELSE NULL ' +
' END AS generation_expression, ' +
' d.description AS column_comment ' +
'FROM pg_catalog.pg_class AS c ' +
'JOIN pg_catalog.pg_namespace AS n ON n.oid = c.relnamespace ' +
'JOIN pg_catalog.pg_attribute AS a ON a.attrelid = c.oid ' +
'JOIN pg_catalog.pg_type AS t ON t.oid = a.atttypid ' +
'LEFT JOIN pg_catalog.pg_type AS bt ON bt.oid = t.typbasetype ' +
'LEFT JOIN pg_catalog.pg_attrdef AS ad ' +
' ON ad.adrelid = a.attrelid ' +
' AND ad.adnum = a.attnum ' +
'LEFT JOIN pg_catalog.pg_description AS d ' +
' ON d.objoid = a.attrelid ' +
' AND d.objsubid = a.attnum ' +
'LEFT JOIN pg_catalog.pg_collation AS coll ' +
' ON coll.oid = a.attcollation ' +
'WHERE n.nspname = %s ' +
' AND a.attnum > 0 ' +
' AND NOT a.attisdropped ' +
' AND c.relname = %s ' +
'ORDER BY ordinal_position',
'' // ServerVersion < 12
);
qGetCharsets: Result := 'SELECT DISTINCT pg_encoding_to_char(enc) AS "Charset" FROM '+
'(SELECT conforencoding AS enc FROM pg_catalog.pg_conversion '+
' UNION '+
' SELECT contoencoding AS enc FROM pg_catalog.pg_conversion) AS x';
qGetRowCountApprox: Result := 'SELECT reltuples::bigint FROM pg_class'+
' LEFT JOIN pg_namespace ON pg_namespace.oid = pg_class.relnamespace'+
' WHERE pg_class.relkind=''r'''+
' AND pg_namespace.nspname=:EscapedDatabase'+
' AND pg_class.relname=:EscapedName';
qGetEnumTypes: Result := IfThen(
FServerVersion >= 90000,
'SELECT ' +
' n.nspname AS enum_schema, ' +
' t.typname AS enum_name, ' +
' string_agg(e.enumlabel, ''|'' ORDER BY e.enumsortorder) AS enum_labels ' +
'FROM pg_type AS t ' +
'JOIN pg_enum AS e ' +
' ON t.oid = e.enumtypid ' +
'JOIN pg_namespace AS n ' +
' ON n.oid = t.typnamespace ' +
'WHERE t.typtype = ''e'' ' +
'GROUP BY n.nspname, t.typname ' +
'ORDER BY UPPER(t.typname)',
'' // ServerVersion < 9
);
qAutoInc: Result := 'SERIAL';
else Result := inherited;
end;
end;
end.