-
Notifications
You must be signed in to change notification settings - Fork 267
/
adodb-postgres64.inc.php
1167 lines (1009 loc) · 32 KB
/
adodb-postgres64.inc.php
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
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* ADOdb PostgreSQL 6.4 driver
*
* This file is part of ADOdb, a Database Abstraction Layer library for PHP.
*
* @package ADOdb
* @link https://adodb.org Project's web site and documentation
* @link https://github.com/ADOdb/ADOdb Source code and issue tracker
*
* The ADOdb Library is dual-licensed, released under both the BSD 3-Clause
* and the GNU Lesser General Public Licence (LGPL) v2.1 or, at your option,
* any later version. This means you can use it in proprietary products.
* See the LICENSE.md file distributed with this source code for details.
* @license BSD-3-Clause
* @license LGPL-2.1-or-later
*
* @copyright 2000-2013 John Lim
* @copyright 2014 Damien Regad, Mark Newnham and the ADOdb community
*/
// security - hide paths
if (!defined('ADODB_DIR')) die();
class ADODB_postgres64 extends ADOConnection{
var $databaseType = 'postgres64';
var $dataProvider = 'postgres';
var $hasInsertID = true;
/** @var PgSql\Connection|resource|false */
var $_resultid = false;
var $concat_operator='||';
var $metaDatabasesSQL = "select datname from pg_database where datname not in ('template0','template1') order by 1";
var $metaTablesSQL = "select tablename,'T' from pg_tables where tablename not like 'pg\_%'
and tablename not in ('sql_features', 'sql_implementation_info', 'sql_languages',
'sql_packages', 'sql_sizing', 'sql_sizing_profiles')
union
select viewname,'V' from pg_views where viewname not like 'pg\_%'";
//"select tablename from pg_tables where tablename not like 'pg_%' order by 1";
var $isoDates = true; // accepts dates in ISO format
var $sysDate = "CURRENT_DATE";
var $sysTimeStamp = "CURRENT_TIMESTAMP";
var $blobEncodeType = 'C';
var $metaColumnsSQL = "SELECT a.attname,t.typname,a.attlen,a.atttypmod,a.attnotnull,a.atthasdef,a.attnum
FROM pg_class c, pg_attribute a,pg_type t
WHERE relkind in ('r','v') AND (c.relname='%s' or c.relname = lower('%s')) and a.attname not like '....%%'
AND a.attnum > 0 AND a.atttypid = t.oid AND a.attrelid = c.oid ORDER BY a.attnum";
// used when schema defined
var $metaColumnsSQL1 = "SELECT a.attname, t.typname, a.attlen, a.atttypmod, a.attnotnull, a.atthasdef, a.attnum
FROM pg_class c, pg_attribute a, pg_type t, pg_namespace n
WHERE relkind in ('r','v') AND (c.relname='%s' or c.relname = lower('%s'))
and c.relnamespace=n.oid and n.nspname='%s'
and a.attname not like '....%%' AND a.attnum > 0
AND a.atttypid = t.oid AND a.attrelid = c.oid ORDER BY a.attnum";
// get primary key etc -- from Freek Dijkstra
var $metaKeySQL = "SELECT ic.relname AS index_name, a.attname AS column_name,i.indisunique AS unique_key, i.indisprimary AS primary_key
FROM pg_class bc, pg_class ic, pg_index i, pg_attribute a
WHERE bc.oid = i.indrelid AND ic.oid = i.indexrelid
AND (i.indkey[0] = a.attnum OR i.indkey[1] = a.attnum OR i.indkey[2] = a.attnum OR i.indkey[3] = a.attnum OR i.indkey[4] = a.attnum OR i.indkey[5] = a.attnum OR i.indkey[6] = a.attnum OR i.indkey[7] = a.attnum)
AND a.attrelid = bc.oid AND bc.relname = '%s'";
var $hasAffectedRows = true;
var $hasLimit = false; // set to true for pgsql 7 only. support pgsql/mysql SELECT * FROM TABLE LIMIT 10
// below suggested by Freek Dijkstra
var $true = 'TRUE'; // string that represents TRUE for a database
var $false = 'FALSE'; // string that represents FALSE for a database
var $fmtDate = "'Y-m-d'"; // used by DBDate() as the default date format used by the database
var $fmtTimeStamp = "'Y-m-d H:i:s'"; // used by DBTimeStamp as the default timestamp fmt.
var $hasMoveFirst = true;
var $hasGenID = true;
var $_genIDSQL = "SELECT NEXTVAL('%s')";
var $_genSeqSQL = "CREATE SEQUENCE %s START %s";
var $_dropSeqSQL = "DROP SEQUENCE %s";
var $metaDefaultsSQL = "SELECT d.adnum as num, d.adsrc as def from pg_attrdef d, pg_class c where d.adrelid=c.oid and c.relname='%s' order by d.adnum";
var $random = 'random()'; /// random function
var $autoRollback = true; // apparently pgsql does not autorollback properly before php 4.3.4
// http://bugs.php.net/bug.php?id=25404
var $uniqueIisR = true;
var $_bindInputArray = false; // requires postgresql 7.3+ and ability to modify database
var $disableBlobs = false; // set to true to disable blob checking, resulting in 2-5% improvement in performance.
/** @var int $_pnum Number of the last assigned query parameter {@see param()} */
var $_pnum = 0;
var $version;
var $_nestedSQL = false;
// The last (fmtTimeStamp is not entirely correct:
// PostgreSQL also has support for time zones,
// and writes these time in this format: "2001-03-01 18:59:26+02".
// There is no code for the "+02" time zone information, so I just left that out.
// I'm not familiar enough with both ADODB as well as Postgres
// to know what the concequences are. The other values are correct (wheren't in 0.94)
// -- Freek Dijkstra
/**
* Retrieve Server information.
* In addition to server version and description, the function also returns
* the client version.
* @param bool $detailed If true, retrieve detailed version string (executes
* a SQL query) in addition to the version number
* @return array|bool Server info or false if version could not be retrieved
* e.g. if there is no active connection
*/
function ServerInfo($detailed = true)
{
if (empty($this->version['version'])) {
// We don't have a connection, so we can't retrieve server info
if (!$this->_connectionID) {
return false;
}
$version = pg_version($this->_connectionID);
$this->version = array(
// If PHP has been compiled with PostgreSQL 7.3 or lower, then
// server version is not set so we use pg_parameter_status()
// which includes logic to obtain values server_version
'version' => isset($version['server'])
? $version['server']
: pg_parameter_status($this->_connectionID, 'server_version'),
'client' => $version['client'],
'description' => null,
);
}
if ($detailed && $this->version['description'] === null) {
$this->version['description'] = $this->GetOne('select version()');
}
return $this->version;
}
function IfNull( $field, $ifNull )
{
return " coalesce($field, $ifNull) ";
}
// get the last id - never tested
function pg_insert_id($tablename,$fieldname)
{
$result=pg_query($this->_connectionID, 'SELECT last_value FROM '. $tablename .'_'. $fieldname .'_seq');
if ($result) {
$arr = @pg_fetch_row($result,0);
pg_free_result($result);
if (isset($arr[0])) return $arr[0];
}
return false;
}
/**
* Warning from http://www.php.net/manual/function.pg-getlastoid.php:
* Using a OID as a unique identifier is not generally wise.
* Unless you are very careful, you might end up with a tuple having
* a different OID if a database must be reloaded.
*
* @inheritDoc
*/
protected function _insertID($table = '', $column = '')
{
if ($this->_resultid === false) return false;
$oid = pg_last_oid($this->_resultid);
// to really return the id, we need the table and column-name, else we can only return the oid != id
return empty($table) || empty($column) ? $oid : $this->GetOne("SELECT $column FROM $table WHERE oid=".(int)$oid);
}
function _affectedrows()
{
if ($this->_resultid === false) return false;
return pg_affected_rows($this->_resultid);
}
/**
* @return bool
*/
function BeginTrans()
{
if ($this->transOff) return true;
$this->transCnt += 1;
return pg_query($this->_connectionID, 'begin '.$this->_transmode);
}
function RowLock($tables,$where,$col='1 as adodbignore')
{
if (!$this->transCnt) $this->BeginTrans();
return $this->GetOne("select $col from $tables where $where for update");
}
// returns true/false.
function CommitTrans($ok=true)
{
if ($this->transOff) return true;
if (!$ok) return $this->RollbackTrans();
$this->transCnt -= 1;
return pg_query($this->_connectionID, 'commit');
}
// returns true/false
function RollbackTrans()
{
if ($this->transOff) return true;
$this->transCnt -= 1;
return pg_query($this->_connectionID, 'rollback');
}
function MetaTables($ttype=false,$showSchema=false,$mask=false)
{
$info = $this->ServerInfo();
if ($info['version'] >= 7.3) {
$this->metaTablesSQL = "
select table_name,'T' from information_schema.tables where table_schema not in ( 'pg_catalog','information_schema')
union
select table_name,'V' from information_schema.views where table_schema not in ( 'pg_catalog','information_schema') ";
}
if ($mask) {
$save = $this->metaTablesSQL;
$mask = $this->qstr(strtolower($mask));
if ($info['version']>=7.3)
$this->metaTablesSQL = "
select table_name,'T' from information_schema.tables where table_name like $mask and table_schema not in ( 'pg_catalog','information_schema')
union
select table_name,'V' from information_schema.views where table_name like $mask and table_schema not in ( 'pg_catalog','information_schema') ";
else
$this->metaTablesSQL = "
select tablename,'T' from pg_tables where tablename like $mask
union
select viewname,'V' from pg_views where viewname like $mask";
}
$ret = ADOConnection::MetaTables($ttype,$showSchema);
if ($mask) {
$this->metaTablesSQL = $save;
}
return $ret;
}
/**
* Quotes a string to be sent to the database.
*
* Relies on pg_escape_string()
* @link https://adodb.org/dokuwiki/doku.php?id=v5:reference:connection:qstr
*
* @param string $s The string to quote
* @param bool $magic_quotes This param is not used since 5.21.0.
* It remains for backwards compatibility.
*
* @return string Quoted string
*/
function qStr($s, $magic_quotes=false)
{
if (is_bool($s)) {
return $s ? 'true' : 'false';
}
if ($this->_connectionID) {
return "'" . pg_escape_string($this->_connectionID, $s) . "'";
} else {
// Fall back to emulated escaping when there is no database connection.
// Avoids errors when using setSessionVariables() in the load balancer.
return parent::qStr( $s );
}
}
// Format date column in sql string given an input format that understands Y M D
function SQLDate($fmt, $col=false)
{
if (!$col) $col = $this->sysTimeStamp;
$s = 'TO_CHAR('.$col.",'";
$len = strlen($fmt);
for ($i=0; $i < $len; $i++) {
$ch = $fmt[$i];
switch($ch) {
case 'Y':
case 'y':
$s .= 'YYYY';
break;
case 'Q':
case 'q':
$s .= 'Q';
break;
case 'M':
$s .= 'Mon';
break;
case 'm':
$s .= 'MM';
break;
case 'D':
case 'd':
$s .= 'DD';
break;
case 'H':
$s.= 'HH24';
break;
case 'h':
$s .= 'HH';
break;
case 'i':
$s .= 'MI';
break;
case 's':
$s .= 'SS';
break;
case 'a':
case 'A':
$s .= 'AM';
break;
case 'w':
$s .= 'D';
break;
case 'l':
$s .= 'DAY';
break;
case 'W':
$s .= 'WW';
break;
default:
// handle escape characters...
if ($ch == '\\') {
$i++;
$ch = substr($fmt,$i,1);
}
if (strpos('-/.:;, ',$ch) !== false) $s .= $ch;
else $s .= '"'.$ch.'"';
}
}
return $s. "')";
}
/*
* Load a Large Object from a file
* - the procedure stores the object id in the table and imports the object using
* postgres proprietary blob handling routines
*
* contributed by Mattia Rossi mattia@technologist.com
* modified for safe mode by juraj chlebec
*/
function UpdateBlobFile($table,$column,$path,$where,$blobtype='BLOB')
{
pg_query($this->_connectionID, 'begin');
$fd = fopen($path,'r');
$contents = fread($fd,filesize($path));
fclose($fd);
$oid = pg_lo_create($this->_connectionID);
$handle = pg_lo_open($this->_connectionID, $oid, 'w');
pg_lo_write($handle, $contents);
pg_lo_close($handle);
// $oid = pg_lo_import ($path);
pg_query($this->_connectionID, 'commit');
$rs = ADOConnection::UpdateBlob($table,$column,$oid,$where,$blobtype);
$rez = !empty($rs);
return $rez;
}
/*
* Deletes/Unlinks a Blob from the database, otherwise it
* will be left behind
*
* Returns TRUE on success or FALSE on failure.
*
* contributed by Todd Rogers todd#windfox.net
*/
function BlobDelete( $blob )
{
pg_query($this->_connectionID, 'begin');
$result = @pg_lo_unlink($this->_connectionID, $blob);
pg_query($this->_connectionID, 'commit');
return( $result );
}
/*
Heuristic - not guaranteed to work.
*/
function GuessOID($oid)
{
if (strlen($oid)>16) return false;
return is_numeric($oid);
}
/*
* If an OID is detected, then we use pg_lo_* to open the oid file and read the
* real blob from the db using the oid supplied as a parameter. If you are storing
* blobs using bytea, we autodetect and process it so this function is not needed.
*
* contributed by Mattia Rossi mattia@technologist.com
*
* see http://www.postgresql.org/idocs/index.php?largeobjects.html
*
* Since adodb 4.54, this returns the blob, instead of sending it to stdout. Also
* added maxsize parameter, which defaults to $db->maxblobsize if not defined.
*/
function BlobDecode($blob,$maxsize=false,$hastrans=true)
{
if (!$this->GuessOID($blob)) return $blob;
if ($hastrans) pg_query($this->_connectionID,'begin');
$fd = @pg_lo_open($this->_connectionID,$blob,'r');
if ($fd === false) {
if ($hastrans) pg_query($this->_connectionID,'commit');
return $blob;
}
if (!$maxsize) $maxsize = $this->maxblobsize;
$realblob = @pg_lo_read($fd,$maxsize);
@pg_lo_close($fd);
if ($hastrans) pg_query($this->_connectionID,'commit');
return $realblob;
}
/**
* Encode binary value prior to DB storage.
*
* See https://www.postgresql.org/docs/current/static/datatype-binary.html
*
* NOTE: SQL string literals (input strings) must be preceded with two
* backslashes due to the fact that they must pass through two parsers in
* the PostgreSQL backend.
*
* @param string $blob
*/
function BlobEncode($blob)
{
return pg_escape_bytea($this->_connectionID, $blob);
}
// assumes bytea for blob, and varchar for clob
function UpdateBlob($table,$column,$val,$where,$blobtype='BLOB')
{
if ($blobtype == 'CLOB') {
return $this->Execute("UPDATE $table SET $column=" . $this->qstr($val) . " WHERE $where");
}
// do not use bind params which uses qstr(), as blobencode() already quotes data
return $this->Execute("UPDATE $table SET $column='".$this->BlobEncode($val)."'::bytea WHERE $where");
}
function OffsetDate($dayFraction,$date=false)
{
if (!$date) $date = $this->sysDate;
else if (strncmp($date,"'",1) == 0) {
$len = strlen($date);
if (10 <= $len && $len <= 12) $date = 'date '.$date;
else $date = 'timestamp '.$date;
}
return "($date+interval'".($dayFraction * 1440)." minutes')";
#return "($date+interval'$dayFraction days')";
}
/**
* Generate the SQL to retrieve MetaColumns data
* @param string $table Table name
* @param string $schema Schema name (can be blank)
* @return string SQL statement to execute
*/
protected function _generateMetaColumnsSQL($table, $schema)
{
if ($schema) {
return sprintf($this->metaColumnsSQL1, $table, $table, $schema);
}
else {
return sprintf($this->metaColumnsSQL, $table, $table, $schema);
}
}
// for schema support, pass in the $table param "$schema.$tabname".
// converts field names to lowercase, $upper is ignored
// see PHPLens Issue No: 14018 for more info
function MetaColumns($table,$normalize=true)
{
global $ADODB_FETCH_MODE;
$schema = false;
$false = false;
$this->_findschema($table,$schema);
if ($normalize) $table = strtolower($table);
$save = $ADODB_FETCH_MODE;
$ADODB_FETCH_MODE = ADODB_FETCH_NUM;
if ($this->fetchMode !== false) $savem = $this->SetFetchMode(false);
$rs = $this->Execute($this->_generateMetaColumnsSQL($table, $schema));
if (isset($savem)) $this->SetFetchMode($savem);
$ADODB_FETCH_MODE = $save;
if ($rs === false) {
return $false;
}
if (!empty($this->metaKeySQL)) {
// If we want the primary keys, we have to issue a separate query
// Of course, a modified version of the metaColumnsSQL query using a
// LEFT JOIN would have been much more elegant, but postgres does
// not support OUTER JOINS. So here is the clumsy way.
$ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
$rskey = $this->Execute(sprintf($this->metaKeySQL,($table)));
// fetch all result in once for performance.
$keys = $rskey->GetArray();
if (isset($savem)) $this->SetFetchMode($savem);
$ADODB_FETCH_MODE = $save;
$rskey->Close();
unset($rskey);
}
$rsdefa = array();
if (!empty($this->metaDefaultsSQL)) {
$ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
$sql = sprintf($this->metaDefaultsSQL, ($table));
$rsdef = $this->Execute($sql);
if (isset($savem)) $this->SetFetchMode($savem);
$ADODB_FETCH_MODE = $save;
if ($rsdef) {
while (!$rsdef->EOF) {
$num = $rsdef->fields['num'];
$s = $rsdef->fields['def'];
if (strpos($s,'::')===false && substr($s, 0, 1) == "'") { /* quoted strings hack... for now... fixme */
$s = substr($s, 1);
$s = substr($s, 0, strlen($s) - 1);
}
$rsdefa[$num] = $s;
$rsdef->MoveNext();
}
} else {
ADOConnection::outp( "==> SQL => " . $sql);
}
unset($rsdef);
}
$retarr = array();
while (!$rs->EOF) {
$fld = new ADOFieldObject();
$fld->name = $rs->fields[0];
$fld->type = $rs->fields[1];
$fld->max_length = $rs->fields[2];
$fld->attnum = $rs->fields[6];
if ($fld->max_length <= 0) $fld->max_length = $rs->fields[3]-4;
if ($fld->max_length <= 0) $fld->max_length = -1;
if ($fld->type == 'numeric') {
$fld->scale = $fld->max_length & 0xFFFF;
$fld->max_length >>= 16;
}
// dannym
// 5 hasdefault; 6 num-of-column
$fld->has_default = ($rs->fields[5] == 't');
if ($fld->has_default) {
$fld->default_value = $rsdefa[$rs->fields[6]];
}
//Freek
$fld->not_null = $rs->fields[4] == 't';
// Freek
if (is_array($keys)) {
foreach($keys as $key) {
if ($fld->name == $key['column_name'] AND $key['primary_key'] == 't')
$fld->primary_key = true;
if ($fld->name == $key['column_name'] AND $key['unique_key'] == 't')
$fld->unique = true; // What name is more compatible?
}
}
if ($ADODB_FETCH_MODE == ADODB_FETCH_NUM) $retarr[] = $fld;
else $retarr[($normalize) ? strtoupper($fld->name) : $fld->name] = $fld;
$rs->MoveNext();
}
$rs->Close();
if (empty($retarr))
return $false;
else
return $retarr;
}
function param($name, $type='C')
{
if (!$name) {
// Reset parameter number if $name is falsy
$this->_pnum = 0;
if ($name === false) {
// and don't return placeholder if false (see #380)
return '';
}
}
return '$' . ++$this->_pnum;
}
function MetaIndexes ($table, $primary = FALSE, $owner = false)
{
global $ADODB_FETCH_MODE;
$schema = false;
$this->_findschema($table,$schema);
if ($schema) { // requires pgsql 7.3+ - pg_namespace used.
$sql = '
SELECT c.relname as "Name", i.indisunique as "Unique", i.indkey as "Columns"
FROM pg_catalog.pg_class c
JOIN pg_catalog.pg_index i ON i.indexrelid=c.oid
JOIN pg_catalog.pg_class c2 ON c2.oid=i.indrelid
,pg_namespace n
WHERE (c2.relname=\'%s\' or c2.relname=lower(\'%s\'))
and c.relnamespace=c2.relnamespace
and c.relnamespace=n.oid
and n.nspname=\'%s\'';
} else {
$sql = '
SELECT c.relname as "Name", i.indisunique as "Unique", i.indkey as "Columns"
FROM pg_catalog.pg_class c
JOIN pg_catalog.pg_index i ON i.indexrelid=c.oid
JOIN pg_catalog.pg_class c2 ON c2.oid=i.indrelid
WHERE (c2.relname=\'%s\' or c2.relname=lower(\'%s\'))';
}
if ($primary == FALSE) {
$sql .= ' AND i.indisprimary=false;';
}
$save = $ADODB_FETCH_MODE;
$ADODB_FETCH_MODE = ADODB_FETCH_NUM;
if ($this->fetchMode !== FALSE) {
$savem = $this->SetFetchMode(FALSE);
}
$rs = $this->Execute(sprintf($sql,$table,$table,$schema));
if (isset($savem)) {
$this->SetFetchMode($savem);
}
$ADODB_FETCH_MODE = $save;
if (!is_object($rs)) {
$false = false;
return $false;
}
// Get column names indexed by attnum so we can lookup the index key
$col_names = $this->MetaColumnNames($table,true,true);
$indexes = array();
while ($row = $rs->FetchRow()) {
$columns = array();
foreach (explode(' ', $row[2]) as $col) {
// When index attribute (pg_index.indkey) is an expression, $col == 0
// @see https://www.postgresql.org/docs/current/catalog-pg-index.html
// so there is no matching column name - set it to null (see #940).
$columns[] = $col_names[$col] ?? null;
}
$indexes[$row[0]] = array(
'unique' => ($row[1] == 't'),
'columns' => $columns
);
}
return $indexes;
}
/**
* Connect to a database.
*
* Examples:
* $db->Connect("host=host1 user=user1 password=secret port=4341");
* $db->Connect('host1:4341', 'user1', 'secret');
*
* @param string $str pg_connect() Connection string or Hostname[:port]
* @param string $user (Optional) The username to connect as.
* @param string $pwd (Optional) The password to connect with.
* @param string $db (Optional) The name of the database to start in when connected.
* @param int $ctype Connection type
* @return bool|null True if connected successfully, false if connection failed, or
* null if the PostgreSQL extension is not loaded.
*/
function _connect($str, $user='', $pwd='', $db='', $ctype=0)
{
if (!function_exists('pg_connect')) {
return null;
}
$this->_errorMsg = false;
// If $user, $pwd and $db are all null, then $str is a pg_connect()
// connection string. Otherwise we expect it to be a hostname,
// with optional port separated by ':'
if ($user || $pwd || $db) {
// Hostname & port
if ($str) {
$host = explode(':', $str);
if ($host[0]) {
$conn['host'] = $host[0];
}
if (isset($host[1])) {
$conn['port'] = (int)$host[1];
} elseif (!empty($this->port)) {
$conn['port'] = $this->port;
}
}
$conn['user'] = $user;
$conn['password'] = $pwd;
// @TODO not sure why we default to 'template1', pg_connect() uses the username when dbname is empty
$conn['dbname'] = $db ?: 'template1';
// Generate connection string
$str = '';
foreach ($conn as $param => $value) {
// Escaping single quotes and backslashes per pg_connect() documentation
$str .= $param . "='" . addcslashes($value, "'\\") . "' ";
}
}
if ($ctype === 1) { // persistent
$this->_connectionID = pg_pconnect($str);
} else {
if ($ctype === -1) { // nconnect, we trick pgsql ext by changing the connection str
static $ncnt;
if (empty($ncnt)) $ncnt = 1;
else $ncnt += 1;
$str .= str_repeat(' ',$ncnt);
}
$this->_connectionID = pg_connect($str);
}
if ($this->_connectionID === false) return false;
$this->Execute("set datestyle='ISO'");
$info = $this->ServerInfo(false);
if (version_compare($info['version'], '7.1', '>=')) {
$this->_nestedSQL = true;
}
# PostgreSQL 9.0 changed the default output for bytea from 'escape' to 'hex'
# PHP does not handle 'hex' properly ('x74657374' is returned as 't657374')
# https://bugs.php.net/bug.php?id=59831 states this is in fact not a bug,
# so we manually set bytea_output
if (version_compare($info['version'], '9.0', '>=')
&& version_compare($info['client'], '9.2', '<')
) {
$this->Execute('set bytea_output=escape');
}
return true;
}
function _nconnect($argHostname, $argUsername, $argPassword, $argDatabaseName)
{
return $this->_connect($argHostname, $argUsername, $argPassword, $argDatabaseName,-1);
}
// returns true or false
//
// examples:
// $db->PConnect("host=host1 user=user1 password=secret port=4341");
// $db->PConnect('host1','user1','secret');
function _pconnect($str,$user='',$pwd='',$db='')
{
return $this->_connect($str,$user,$pwd,$db,1);
}
// returns queryID or false
function _query($sql,$inputarr=false)
{
$this->_pnum = 0;
$this->_errorMsg = false;
if ($inputarr) {
/*
It appears that PREPARE/EXECUTE is slower for many queries.
For query executed 1000 times:
"select id,firstname,lastname from adoxyz
where firstname not like ? and lastname not like ? and id = ?"
with plan = 1.51861286163 secs
no plan = 1.26903700829 secs
*/
$plan = 'P'.md5($sql);
$execp = '';
foreach($inputarr as $v) {
if ($execp) $execp .= ',';
if (is_string($v)) {
if (strncmp($v,"'",1) !== 0) $execp .= $this->qstr($v);
} else {
$execp .= $v;
}
}
if ($execp) $exsql = "EXECUTE $plan ($execp)";
else $exsql = "EXECUTE $plan";
$rez = @pg_query($this->_connectionID, $exsql);
if (!$rez) {
# Perhaps plan does not exist? Prepare/compile plan.
$params = '';
foreach($inputarr as $v) {
if ($params) $params .= ',';
if (is_string($v)) {
$params .= 'VARCHAR';
} else if (is_integer($v)) {
$params .= 'INTEGER';
} else {
$params .= "REAL";
}
}
$sqlarr = explode('?',$sql);
//print_r($sqlarr);
$sql = '';
$i = 1;
foreach($sqlarr as $v) {
$sql .= $v.' $'.$i;
$i++;
}
$s = "PREPARE $plan ($params) AS ".substr($sql,0,strlen($sql)-2);
//adodb_pr($s);
$rez = pg_query($this->_connectionID, $s);
//echo $this->ErrorMsg();
}
if ($rez)
$rez = pg_query($this->_connectionID, $exsql);
} else {
//adodb_backtrace();
$rez = pg_query($this->_connectionID, $sql);
}
// check if no data returned, then no need to create real recordset
if ($rez && pg_num_fields($rez) <= 0) {
if ($this->_resultid !== false) {
pg_free_result($this->_resultid);
}
$this->_resultid = $rez;
return true;
}
return $rez;
}
function _errconnect()
{
if (defined('DB_ERROR_CONNECT_FAILED')) return DB_ERROR_CONNECT_FAILED;
else return 'Database connection failed';
}
/* Returns: the last error message from previous database operation */
function ErrorMsg()
{
if ($this->_errorMsg !== false) {
return $this->_errorMsg;
}
if (!empty($this->_resultid)) {
$this->_errorMsg = @pg_result_error($this->_resultid);
if ($this->_errorMsg) {
return $this->_errorMsg;
}
}
if (!empty($this->_connectionID)) {
$this->_errorMsg = @pg_last_error($this->_connectionID);
} else {
$this->_errorMsg = $this->_errconnect();
}
return $this->_errorMsg;
}
function ErrorNo()
{
$e = $this->ErrorMsg();
if (strlen($e)) {
return ADOConnection::MetaError($e);
}
return 0;
}
// returns true or false
function _close()
{
if ($this->transCnt) $this->RollbackTrans();
if ($this->_resultid) {
@pg_free_result($this->_resultid);
$this->_resultid = false;
}
@pg_close($this->_connectionID);
$this->_connectionID = false;
return true;
}
/*
* Maximum size of C field
*/
function CharMax()
{
return 1000000000; // should be 1 Gb?
}
/*
* Maximum size of X field
*/
function TextMax()
{
return 1000000000; // should be 1 Gb?
}
}
/*--------------------------------------------------------------------------------------
Class Name: Recordset
--------------------------------------------------------------------------------------*/
class ADORecordSet_postgres64 extends ADORecordSet{
var $_blobArr;
var $databaseType = "postgres64";
var $canSeek = true;
function __construct($queryID, $mode=false)
{
if ($mode === false) {
global $ADODB_FETCH_MODE;
$mode = $ADODB_FETCH_MODE;
}
switch ($mode)
{
case ADODB_FETCH_NUM: $this->fetchMode = PGSQL_NUM; break;
case ADODB_FETCH_ASSOC:$this->fetchMode = PGSQL_ASSOC; break;
case ADODB_FETCH_DEFAULT:
case ADODB_FETCH_BOTH:
default: $this->fetchMode = PGSQL_BOTH; break;
}
$this->adodbFetchMode = $mode;
// Parent's constructor
parent::__construct($queryID);
}
function GetRowAssoc($upper = ADODB_ASSOC_CASE)
{
if ($this->fetchMode == PGSQL_ASSOC && $upper == ADODB_ASSOC_CASE_LOWER) {
return $this->fields;
}
$row = ADORecordSet::GetRowAssoc($upper);
return $row;
}
function _initRS()
{
global $ADODB_COUNTRECS;
$qid = $this->_queryID;
$this->_numOfRows = ($ADODB_COUNTRECS)? @pg_num_rows($qid):-1;
$this->_numOfFields = @pg_num_fields($qid);
// cache types for blob decode check
// apparently pg_field_type actually performs an sql query on the database to get the type.
if (empty($this->connection->noBlobs))
for ($i=0, $max = $this->_numOfFields; $i < $max; $i++) {
if (pg_field_type($qid,$i) == 'bytea') {
$this->_blobArr[$i] = pg_field_name($qid,$i);
}
}
}
function fields($colname)
{
if ($this->fetchMode != PGSQL_NUM) {
return @$this->fields[$colname];
}
if (!$this->bind) {
$this->bind = array();
for ($i=0; $i < $this->_numOfFields; $i++) {
$o = $this->FetchField($i);
$this->bind[strtoupper($o->name)] = $i;