-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathql_pgsql.qpp
More file actions
604 lines (437 loc) · 20.2 KB
/
ql_pgsql.qpp
File metadata and controls
604 lines (437 loc) · 20.2 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
/* -*- mode: c++; indent-tabs-mode: nil -*- */
/*
ql_pgsql.qpp
PostgreSQL DBI Interface to Qore DBI layer
Qore Programming Language
Copyright 2003 - 2026 Qore Technologies, s.r.o.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "pgsql.h"
#include "QorePGConnection.h"
#ifndef PG_TYPE_RELTYPE_OID
#define PG_TYPE_RELTYPE_OID 0
#endif
#ifndef PG_ATTRIBUTE_RELTYPE_OID
#define PG_ATTRIBUTE_RELTYPE_OID 0
#endif
#ifndef PG_PROC_RELTYPE_OID
#define PG_PROC_RELTYPE_OID 0
#endif
#ifndef PG_CLASS_RELTYPE_OID
#define PG_CLASS_RELTYPE_OID 0
#endif
#ifdef HAVE_PQLIBVERSION
#define PGSQL_HAVE_GETCLIENTVERSION 1
#else
#define PGSQL_HAVE_GETCLIENTVERSION 0
#endif
/** @defgroup pgsql_option_constants Option Constangs for the pgsql Module
*/
///@{
namespace Qore::Pgsql;
//! This constant indicates if the pgsql driver supports the getClientVersion method or not
/** This method is only supported if compiled with a version of the PostgreSQL client library
implementing the PQlibVersion() function, which was introduced in PostgreSQL 9.1.
*/
const PGSQL_HAVE_GETCLIENTVERSION = bool(PGSQL_HAVE_GETCLIENTVERSION);
///@}
/** @defgroup pgsql_bind_constants Type Constants for pgsql_bind()
*/
///@{
namespace Qore::Pgsql;
//! defines a bind for the \c BOOLOID type
const PG_TYPE_BOOL = BOOLOID;
//! defines a bind for the \c BYTEAOID type
const PG_TYPE_BYTEA = BYTEAOID;
//! defines a bind for the \c CHAROID type
const PG_TYPE_CHAR = CHAROID;
//! defines a bind for the \c NAMEOID type
const PG_TYPE_NAME = NAMEOID;
//! defines a bind for the \c INT8OID type
const PG_TYPE_INT8 = INT8OID;
//! defines a bind for the \c INT2OID type
const PG_TYPE_INT2 = INT2OID;
//! defines a bind for the \c INT2VECTOROID type
const PG_TYPE_INT2VECTOR = INT2VECTOROID;
//! defines a bind for the \c INT4OID type
const PG_TYPE_INT4 = INT4OID;
//! defines a bind for the \c REGPROCOID type
const PG_TYPE_REGPROC = REGPROCOID;
//! defines a bind for the \c TEXTOID type
const PG_TYPE_TEXT = TEXTOID;
//! defines a bind for the \c OIDOID type
const PG_TYPE_OID = OIDOID;
//! defines a bind for the \c TIDOID type
const PG_TYPE_TID = TIDOID;
//! defines a bind for the \c XIDOID type
const PG_TYPE_XID = XIDOID;
//! defines a bind for the \c CIDOID type
const PG_TYPE_CID = CIDOID;
//! defines a bind for the \c OIDVECTOROID type
const PG_TYPE_VECTOROID = OIDVECTOROID;
//! defines a bind for the \c PG_TYPE_RELTYPE_OID type
/** if this value is not defined by the PostgreSQL installation at
compile time, then its value will be 0
*/
const PG_TYPE_TYPE_RELTYPE = PG_TYPE_RELTYPE_OID;
//! defines a bind for the \c PG_ATTRIBUTE_RELTYPE_OID type
/** if this value is not defined by the PostgreSQL installation at
compile time, then its value will be 0
*/
const PG_TYPE_ATTRIBUTE_RELTYPE = PG_ATTRIBUTE_RELTYPE_OID;
//! defines a bind for the \c PG_PROC_RELTYPE_OID type
/** if this value is not defined by the PostgreSQL installation at
compile time, then its value will be 0
*/
const PG_TYPE_PROC_RELTYPE = PG_PROC_RELTYPE_OID;
//! defines a bind for the \c PG_CLASS_RELTYPE_OID type
/** if this value is not defined by the PostgreSQL installation at
compile time, then its value will be 0
*/
const PG_TYPE_CLASS_RELTYPE = PG_CLASS_RELTYPE_OID;
//! defines a bind for the \c POINTOID type
const PG_TYPE_POINT = POINTOID;
//! defines a bind for the \c LSEGOID type
const PG_TYPE_LSEG = LSEGOID;
//! defines a bind for the \c PATHOID type
const PG_TYPE_PATH = PATHOID;
//! defines a bind for the \c BOXOID type
const PG_TYPE_BOX = BOXOID;
//! defines a bind for the \c POLYGONOID type
const PG_TYPE_POLYGON = POLYGONOID;
//! defines a bind for the \c LINEOID type
const PG_TYPE_LINE = LINEOID;
//! defines a bind for the \c FLOAT4OID type
const PG_TYPE_FLOAT4 = FLOAT4OID;
//! defines a bind for the \c FLOAT8OID type
const PG_TYPE_FLOAT8 = FLOAT8OID;
//! defines a bind for the \c ABSTIMEOID type
const PG_TYPE_ABSTIME = ABSTIMEOID;
//! defines a bind for the \c RELTIMEOID type
const PG_TYPE_RELTIME = RELTIMEOID;
//! defines a bind for the \c TINTERVALOID type
const PG_TYPE_TINTERVAL = TINTERVALOID;
//! defines a bind for the \c UNKNOWNOID type
const PG_TYPE_UNKNOWN = UNKNOWNOID;
//! defines a bind for the \c CIRCLEOID type
const PG_TYPE_CIRCLE = CIRCLEOID;
//! defines a bind for the \c CASHOID type
const PG_TYPE_CASH = CASHOID;
//! defines a bind for the \c MACADDROID type
const PG_TYPE_MACADDR = MACADDROID;
//! defines a bind for the \c INETOID type
const PG_TYPE_INET = INETOID;
//! defines a bind for the \c CIDROID type
const PG_TYPE_CIDR = CIDROID;
//! defines a bind for the \c ACLITEMOID type
const PG_TYPE_ACLITEM = ACLITEMOID;
//! defines a bind for the \c BPCHAROID type
const PG_TYPE_BPCHAR = BPCHAROID;
//! defines a bind for the \c VARCHAROID type
const PG_TYPE_VARCHAR = VARCHAROID;
//! defines a bind for the \c DATEOID type
const PG_TYPE_DATE = DATEOID;
//! defines a bind for the \c TIMEOID type
const PG_TYPE_TIME = TIMEOID;
//! defines a bind for the \c TIMESTAMPOID type
const PG_TYPE_TIMESTAMP = TIMESTAMPOID;
//! defines a bind for the \c TIMESTAMPTZOID type
const PG_TYPE_TIMESTAMPTZ = TIMESTAMPTZOID;
//! defines a bind for the \c INTERVALOID type
const PG_TYPE_INTERVAL = INTERVALOID;
//! defines a bind for the \c TIMETZOID type
const PG_TYPE_TIMETZ = TIMETZOID;
//! defines a bind for the \c BITOID type
const PG_TYPE_BIT = BITOID;
//! defines a bind for the \c VARBITOID type
const PG_TYPE_VARBIT = VARBITOID;
//! defines a bind for the \c NUMERICOID type
const PG_TYPE_NUMERIC = NUMERICOID;
//! defines a bind for the \c REFCURSOROID type
const PG_TYPE_REFCURSOR = REFCURSOROID;
//! defines a bind for the \c REGPROCEDUREOID type
const PG_TYPE_REGPROCEDURE = REGPROCEDUREOID;
//! defines a bind for the \c REGOPEROID type
const PG_TYPE_REGOPER = REGOPEROID;
//! defines a bind for the \c REGOPERATOROID type
const PG_TYPE_REGOPERATOR = REGOPERATOROID;
//! defines a bind for the \c REGCLASSOID type
const PG_TYPE_REGCLASS = REGCLASSOID;
//! defines a bind for the \c REGTYPEOID type
const PG_TYPE_REGTYPE = REGTYPEOID;
//! defines a bind for the \c RECORDOID type
const PG_TYPE_RECORD = RECORDOID;
//! defines a bind for the \c CSTRINGOID type
const PG_TYPE_CSTRING = CSTRINGOID;
//! defines a bind for the \c ANYOID type
const PG_TYPE_ANY = ANYOID;
//! defines a bind for the \c VOIDOID type
const PG_TYPE_VOID = VOIDOID;
//! defines a bind for the \c TRIGGEROID type
const PG_TYPE_TRIGGER = TRIGGEROID;
//! defines a bind for the \c LANGUAGE_HANDLEROID type
const PG_TYPE_LANGUAGE_HANDLER = LANGUAGE_HANDLEROID;
//! defines a bind for the \c INTERNALOID type
const PG_TYPE_INTERNAL = INTERNALOID;
//! defines a bind for the \c OPAQUEOID type
const PG_TYPE_OPAQUE = OPAQUEOID;
//! defines a bind for the \c ANYELEMENTOID type
const PG_TYPE_ANYELEMENT = ANYELEMENTOID;
//! defines a bind for the \c XML type
const PG_TYPE_XML = XMLOID;
//! defines a bind for the \c JSON type
const PG_TYPE_JSON = JSONOID;
//! defines a bind for the \c JSONB type
const PG_TYPE_JSONB = JSONBOID;
//! defines a bind for the \c QPGT_INT4ARRAYOID type
const PG_TYPE_INT4ARRAY = QPGT_INT4ARRAYOID;
//! defines a bind for the \c QPGT_CIRCLEARRAYOID type
const PG_TYPE_CIRCLEARRAY = QPGT_CIRCLEARRAYOID;
//! defines a bind for the \c QPGT_MONEYARRAYOID type
const PG_TYPE_MONEYARRAY = QPGT_MONEYARRAYOID;
//! defines a bind for the \c QPGT_BOOLARRAYOID type
const PG_TYPE_BOOLARRAY = QPGT_BOOLARRAYOID;
//! defines a bind for the \c QPGT_BYTEAARRAYOID type
const PG_TYPE_BYTEAARRAY = QPGT_BYTEAARRAYOID;
//! defines a bind for the \c QPGT_NAMEARRAYOID type
const PG_TYPE_NAMEARRAY = QPGT_NAMEARRAYOID;
//! defines a bind for the \c QPGT_INT2ARRAYOID type
const PG_TYPE_INT2ARRAY = QPGT_INT2ARRAYOID;
//! defines a bind for the \c QPGT_TEXTARRAYOID type
const PG_TYPE_TEXTARRAY = QPGT_TEXTARRAYOID;
//! defines a bind for the \c QPGT_OIDARRAYOID type
const PG_TYPE_OIDARRAY = QPGT_OIDARRAYOID;
//! defines a bind for the \c QPGT_TIDARRAYOID type
const PG_TYPE_TIDARRAY = QPGT_TIDARRAYOID;
//! defines a bind for the \c QPGT_XIDARRAYOID type
const PG_TYPE_XIDARRAY = QPGT_XIDARRAYOID;
//! defines a bind for the \c QPGT_CIDARRAYOID type
const PG_TYPE_CIDARRAY = QPGT_CIDARRAYOID;
//! defines a bind for the \c QPGT_BPCHARARRAYOID type
const PG_TYPE_BPCHARARRAY = QPGT_BPCHARARRAYOID;
//! defines a bind for the \c QPGT_VARCHARARRAYOID type
const PG_TYPE_VARCHARARRAY = QPGT_VARCHARARRAYOID;
//! defines a bind for the \c QPGT_INT8ARRAYOID type
const PG_TYPE_INT8ARRAY = QPGT_INT8ARRAYOID;
//! defines a bind for the \c QPGT_POINTARRAYOID type
const PG_TYPE_POINTARRAY = QPGT_POINTARRAYOID;
//! defines a bind for the \c QPGT_LSEGARRAYOID type
const PG_TYPE_LSEGARRAY = QPGT_LSEGARRAYOID;
//! defines a bind for the \c QPGT_PATHARRAYOID type
const PG_TYPE_PATHARRAY = QPGT_PATHARRAYOID;
//! defines a bind for the \c QPGT_BOXARRAYOID type
const PG_TYPE_BOXARRAY = QPGT_BOXARRAYOID;
//! defines a bind for the \c QPGT_FLOAT4ARRAYOID type
const PG_TYPE_FLOAT4ARRAY = QPGT_FLOAT4ARRAYOID;
//! defines a bind for the \c QPGT_FLOAT8ARRAYOID type
const PG_TYPE_FLOAT8ARRAY = QPGT_FLOAT8ARRAYOID;
//! defines a bind for the \c QPGT_ABSTIMEARRAYOID type
const PG_TYPE_ABSTIMEARRAY = QPGT_ABSTIMEARRAYOID;
//! defines a bind for the \c QPGT_RELTIMEARRAYOID type
const PG_TYPE_RELTIMEARRAY = QPGT_RELTIMEARRAYOID;
//! defines a bind for the \c QPGT_TINTERVALARRAYOID type
const PG_TYPE_TINTERVALARRAY = QPGT_TINTERVALARRAYOID;
//! defines a bind for the \c QPGT_POLYGONARRAYOID type
const PG_TYPE_POLYGONARRAY = QPGT_POLYGONARRAYOID;
//! defines a bind for the \c QPGT_MACADDRARRAYOID type
const PG_TYPE_MACADDRARRAY = QPGT_MACADDRARRAYOID;
//! defines a bind for the \c QPGT_INETARRAYOID type
const PG_TYPE_INETARRAY = QPGT_INETARRAYOID;
//! defines a bind for the \c QPGT_CIDRARRAYOID type
const PG_TYPE_CIDRARRAY = QPGT_CIDRARRAYOID;
//! defines a bind for the \c QPGT_TIMESTAMPARRAYOID type
const PG_TYPE_TIMESTAMPARRAY = QPGT_TIMESTAMPARRAYOID;
//! defines a bind for the \c QPGT_DATEARRAYOID type
const PG_TYPE_DATEARRAY = QPGT_DATEARRAYOID;
//! defines a bind for the \c QPGT_TIMEARRAYOID type
const PG_TYPE_TIMEARRAY = QPGT_TIMEARRAYOID;
//! defines a bind for the \c QPGT_TIMESTAMPTZARRAYOID type
const PG_TYPE_TIMESTAMPTZARRAY = QPGT_TIMESTAMPTZARRAYOID;
//! defines a bind for the \c QPGT_INTERVALARRAYOID type
const PG_TYPE_INTERVALARRAY = QPGT_INTERVALARRAYOID;
//! defines a bind for the \c QPGT_NUMERICARRAYOID type
const PG_TYPE_NUMERICARRAY = QPGT_NUMERICARRAYOID;
//! defines a bind for the \c QPGT_TIMETZARRAYOID type
const PG_TYPE_TIMETZARRAY = QPGT_TIMETZARRAYOID;
//! defines a bind for the \c QPGT_BITARRAYOID type
const PG_TYPE_BITARRAY = QPGT_BITARRAYOID;
//! defines a bind for the \c QPGT_VARBITARRAYOID type
const PG_TYPE_VARBITARRAY = QPGT_VARBITARRAYOID;
//! defines a bind for the \c ANYARRAYOID type
const PG_TYPE_ANYARRAY = ANYARRAYOID;
//! defines a bind for the \c XMLARRAY type
const PG_TYPE_XMLARRAY = XMLARRAYOID;
//! defines a bind for the \c JSONARRAY type
const PG_TYPE_JSONARRAY = JSONOID;
//! defines a bind for the \c JSONBARRAY type
const PG_TYPE_JSONBARRAY = JSONBARRAYOID;
//! defines a bind for the \c UUID type
const PG_TYPE_UUID = UUIDOID;
//! defines a bind for the \c UUIDARRAY type
const PG_TYPE_UUIDARRAY = QPGT_UUIDARRAYOID;
///@}
/** @defgroup pgsql_functions PostgreSQL Functions
*/
///@{
namespace Qore::Pgsql;
//! Creates a hash data structure understood by the pgsql DBI driver when binding values in SQL queries that allows programmers to directly specify the PostgreSQL data type for the bind
/** Use the @ref pgsql_bind_constants to specify the PostgreSQL data type for the bind. If the value to bind is \c NOTHING or \c NULL, a \c NULL will be bound as the value, regardless of the PostgreSQL type code provided as the first argument.
@param type the @ref pgsql_bind_constants "type code" for the PostgreSQL type to bind
@param value the value to bind
@returns The hash returned by the function encodes the desired PostgreSQL type for the bind and the value for binding in the \c "^pgtype^" and \c "^value^" keys
@par Example:
@code
# we use pgsql_bind() to bind a money type by value, otherwise the server would raise an error
my float $amount = 400.56;
my *hash $results = $db.select("select * from table where amount > %v", pgsql_bind(PG_TYPE_CASH, $amount));
@endcode
@throw PGSQL-BIND-ERROR if the type argument is 0 this exception is raised
*/
hash pgsql_bind(softint type, auto value) [flags=RET_VALUE_ONLY] {
if (!type) {
xsink->raiseException("PGSQL-BIND-ERROR", "expecting OID (type number) as first parameter to pgsql_bind()");
return QoreValue();
}
QoreHashNode *h = new QoreHashNode;
h->setKeyValue("^pgtype^", type, xsink);
h->setKeyValue("^value^", value.refSelf(), xsink);
return h;
}
//! Creates a data structure understood by the pgsql DBI driver when binding array values in SQL queries
/** @param value the list to bind as an array
@return A hash that allows the value to be bound as an array
@since pgsql 2.4
*/
hash pgsql_bind_array(*softlist<auto> value) [flags=CONSTANT] {
QoreHashNode *h = new QoreHashNode;
h->setKeyValue("^pgarray^", true, xsink);
h->setKeyValue("^value^", value ? value->refSelf() : nullptr, xsink);
return h;
}
//! Creates a hash data structure for binding a UUID value
/** Use this function to explicitly bind a UUID string value to a PostgreSQL UUID column.
@param value the UUID string value to bind (format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)
@returns The hash returned by the function encodes the UUID type for binding
@par Example:
@code
# bind a UUID value
db.exec("insert into users (id) values (%v)", pgsql_bind_uuid("550e8400-e29b-41d4-a716-446655440000"));
@endcode
@since pgsql 3.3
*/
hash pgsql_bind_uuid(string value) [flags=RET_VALUE_ONLY] {
QoreHashNode *h = new QoreHashNode;
h->setKeyValue("^pgtype^", (int64)UUIDOID, xsink);
h->setKeyValue("^value^", value->refSelf(), xsink);
return h;
}
//! Creates a hash data structure for binding a vector value (pgvector extension)
/** Use this function to explicitly bind a list of float values as a PostgreSQL
\c vector column (requires the pgvector extension).
@param value the list of float values representing the vector
@returns The hash returned by the function encodes the vector type for binding
@par Example:
@code
# bind a vector value
db.exec("insert into items (embedding) values (%v)", pgsql_bind_vector((1.5, 2.3, 4.1)));
@endcode
@note The vector type OID is resolved dynamically at bind time from the connection's
type cache. If pgvector is not installed, an error will be raised.
@since pgsql 3.4
*/
hash<auto> pgsql_bind_vector(*softlist<softfloat> value) [flags=RET_VALUE_ONLY] {
QoreHashNode* h = new QoreHashNode(autoTypeInfo);
h->setKeyValue("^pgtype^", new QoreStringNode("vector"), xsink);
h->setKeyValue("^value^", value && value->size() ? value->refSelf() : nullptr, xsink);
return h;
}
///@}
/** @defgroup pgsql_schema_constants Schema Introspection SQL Constants
These constants provide SQL queries for introspecting the PostgreSQL database schema.
Use these with Datasource::selectRows() to get schema information.
*/
///@{
namespace Qore::Pgsql;
//! SQL query to list all user tables in the current schema
/** Returns columns: table_name, table_type
@par Example:
@code
list<auto> tables = db.selectRows(PGSQL_SQL_LIST_TABLES);
@endcode
@since pgsql 3.3
*/
const PGSQL_SQL_LIST_TABLES = "SELECT table_name, table_type FROM information_schema.tables WHERE table_schema = current_schema() AND table_type = 'BASE TABLE' ORDER BY table_name";
//! SQL query to list all schemas in the database
/** Returns columns: schema_name, schema_owner
@par Example:
@code
list<auto> schemas = db.selectRows(PGSQL_SQL_LIST_SCHEMAS);
@endcode
@since pgsql 3.3
*/
const PGSQL_SQL_LIST_SCHEMAS = "SELECT schema_name, schema_owner FROM information_schema.schemata WHERE schema_name NOT LIKE 'pg_%' AND schema_name != 'information_schema' ORDER BY schema_name";
//! SQL query template to get columns for a specific table (use %v placeholder for table name)
/** Returns columns: column_name, data_type, is_nullable, column_default, character_maximum_length, numeric_precision
@par Example:
@code
list<auto> columns = db.selectRows(PGSQL_SQL_TABLE_COLUMNS, "my_table");
@endcode
@since pgsql 3.3
*/
const PGSQL_SQL_TABLE_COLUMNS = "SELECT column_name, data_type, is_nullable, column_default, character_maximum_length, numeric_precision, numeric_scale FROM information_schema.columns WHERE table_schema = current_schema() AND table_name = %v ORDER BY ordinal_position";
//! SQL query template to get indexes for a specific table (use %v placeholder for table name)
/** Returns columns: index_name, is_unique, is_primary, column_names
@par Example:
@code
list<auto> indexes = db.selectRows(PGSQL_SQL_TABLE_INDEXES, "my_table");
@endcode
@since pgsql 3.3
*/
const PGSQL_SQL_TABLE_INDEXES = "SELECT i.relname as index_name, ix.indisunique as is_unique, ix.indisprimary as is_primary, array_agg(a.attname ORDER BY array_position(ix.indkey, a.attnum)) as column_names FROM pg_class t JOIN pg_index ix ON t.oid = ix.indrelid JOIN pg_class i ON i.oid = ix.indexrelid JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = ANY(ix.indkey) WHERE t.relname = %v AND t.relkind = 'r' GROUP BY i.relname, ix.indisunique, ix.indisprimary ORDER BY i.relname";
//! SQL query template to get foreign keys for a specific table (use %v placeholder for table name)
/** Returns columns: constraint_name, column_name, foreign_table, foreign_column
@par Example:
@code
list<auto> fks = db.selectRows(PGSQL_SQL_TABLE_FOREIGN_KEYS, "my_table");
@endcode
@since pgsql 3.3
*/
const PGSQL_SQL_TABLE_FOREIGN_KEYS = "SELECT tc.constraint_name, kcu.column_name, ccu.table_name AS foreign_table, ccu.column_name AS foreign_column FROM information_schema.table_constraints AS tc JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name AND tc.table_schema = kcu.table_schema JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name AND ccu.table_schema = tc.table_schema WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_name = %v";
//! SQL query to list all functions in the current schema
/** Returns columns: function_name, result_type, argument_types
@par Example:
@code
list<auto> functions = db.selectRows(PGSQL_SQL_LIST_FUNCTIONS);
@endcode
@since pgsql 3.3
*/
const PGSQL_SQL_LIST_FUNCTIONS = "SELECT p.proname as function_name, pg_get_function_result(p.oid) as result_type, pg_get_function_arguments(p.oid) as argument_types FROM pg_proc p JOIN pg_namespace n ON p.pronamespace = n.oid WHERE n.nspname = current_schema() ORDER BY p.proname";
//! SQL query to list all sequences in the current schema
/** Returns columns: sequence_name, data_type, start_value, increment
@par Example:
@code
list<auto> sequences = db.selectRows(PGSQL_SQL_LIST_SEQUENCES);
@endcode
@since pgsql 3.3
*/
const PGSQL_SQL_LIST_SEQUENCES = "SELECT sequence_name, data_type, start_value, increment FROM information_schema.sequences WHERE sequence_schema = current_schema() ORDER BY sequence_name";
//! SQL query to list all views in the current schema
/** Returns columns: view_name, view_definition
@par Example:
@code
list<auto> views = db.selectRows(PGSQL_SQL_LIST_VIEWS);
@endcode
@since pgsql 3.3
*/
const PGSQL_SQL_LIST_VIEWS = "SELECT table_name as view_name, view_definition FROM information_schema.views WHERE table_schema = current_schema() ORDER BY table_name";
///@}