Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

   1  <?php
   2  /*
   3   @version   v5.21.0  2021-02-27
   4   @copyright (c) 2000-2013 John Lim (jlim#natsoft.com). All rights reserved.
   5   @copyright (c) 2014      Damien Regad, Mark Newnham and the ADOdb community
   6    Released under both BSD license and Lesser GPL library license.
   7    Whenever there is any discrepancy between the two licenses,
   8    the BSD license will take precedence.
   9    Set tabs to 4.
  10  
  11    Postgres7 support.
  12    28 Feb 2001: Currently indicate that we support LIMIT
  13    01 Dec 2001: dannym added support for default values
  14  */
  15  
  16  // security - hide paths
  17  if (!defined('ADODB_DIR')) die();
  18  
  19  include_once(ADODB_DIR."/drivers/adodb-postgres64.inc.php");
  20  
  21  class ADODB_postgres7 extends ADODB_postgres64 {
  22  	 var $databaseType = 'postgres7';
  23  	 var $hasLimit = true;	 // set to true for pgsql 6.5+ only. support pgsql/mysql SELECT * FROM TABLE LIMIT 10
  24  	 var $ansiOuter = true;
  25  	 var $charSet = true; //set to true for Postgres 7 and above - PG client supports encodings
  26  
  27  	 // Richard 3/18/2012 - Modified SQL to return SERIAL type correctly AS old driver no longer return SERIAL as data type.
  28  	 var $metaColumnsSQL = "
  29  	 	 SELECT
  30  	 	 	 a.attname,
  31  	 	 	 CASE
  32  	 	 	 	 WHEN x.sequence_name != ''
  33  	 	 	 	 THEN 'SERIAL'
  34  	 	 	 	 ELSE t.typname
  35  	 	 	 END AS typname,
  36  	 	 	 a.attlen, a.atttypmod, a.attnotnull, a.atthasdef, a.attnum
  37  	 	 FROM
  38  	 	 	 pg_class c,
  39  	 	 	 pg_attribute a
  40  	 	 JOIN pg_type t ON a.atttypid = t.oid
  41  	 	 LEFT JOIN (
  42  	 	 	 SELECT
  43  	 	 	 	 c.relname as sequence_name,
  44  	 	 	 	 c1.relname as related_table,
  45  	 	 	 	 a.attname as related_column
  46  	 	 	 FROM pg_class c
  47  	 	 	 JOIN pg_depend d ON d.objid = c.oid
  48  	 	 	 LEFT JOIN pg_class c1 ON d.refobjid = c1.oid
  49  	 	 	 LEFT JOIN pg_attribute a ON (d.refobjid, d.refobjsubid) = (a.attrelid, a.attnum)
  50  	 	 	 WHERE c.relkind = 'S' AND c1.relname = '%s'
  51  	 	 ) x ON x.related_column= a.attname
  52  	 	 WHERE
  53  	 	 	 c.relkind in ('r','v')
  54  	 	 	 AND (c.relname='%s' or c.relname = lower('%s'))
  55  	 	 	 AND a.attname not like '....%%'
  56  	 	 	 AND a.attnum > 0
  57  	 	 	 AND a.attrelid = c.oid
  58  	 	 ORDER BY
  59  	 	 	 a.attnum";
  60  
  61  	 // used when schema defined
  62  	 var $metaColumnsSQL1 = "
  63  	 	 SELECT
  64  	 	 	 a.attname,
  65  	 	 	 CASE
  66  	 	 	 	 WHEN x.sequence_name != ''
  67  	 	 	 	 THEN 'SERIAL'
  68  	 	 	 	 ELSE t.typname
  69  	 	 	 END AS typname,
  70  	 	 	 a.attlen, a.atttypmod, a.attnotnull, a.atthasdef, a.attnum
  71  	 	 FROM
  72  	 	 	 pg_class c,
  73  	 	 	 pg_namespace n,
  74  	 	 	 pg_attribute a
  75  	 	 JOIN pg_type t ON a.atttypid = t.oid
  76  	 	 LEFT JOIN (
  77  	 	 	 SELECT
  78  	 	 	 	 c.relname as sequence_name,
  79  	 	 	 	 c1.relname as related_table,
  80  	 	 	 	 a.attname as related_column
  81  	 	 	 FROM pg_class c
  82  	 	 	 JOIN pg_depend d ON d.objid = c.oid
  83  	 	 	 LEFT JOIN pg_class c1 ON d.refobjid = c1.oid
  84  	 	 	 LEFT JOIN pg_attribute a ON (d.refobjid, d.refobjsubid) = (a.attrelid, a.attnum)
  85  	 	 	 WHERE c.relkind = 'S' AND c1.relname = '%s'
  86  	 	 ) x ON x.related_column= a.attname
  87  	 	 WHERE
  88  	 	 	 c.relkind in ('r','v')
  89  	 	 	 AND (c.relname='%s' or c.relname = lower('%s'))
  90  	 	 	 AND c.relnamespace=n.oid and n.nspname='%s'
  91  	 	 	 AND a.attname not like '....%%'
  92  	 	 	 AND a.attnum > 0
  93  	 	 	 AND a.atttypid = t.oid
  94  	 	 	 AND a.attrelid = c.oid
  95  	 	 ORDER BY a.attnum";
  96  
  97  
  98  	function __construct()
  99  	 {
 100  	 	 parent::__construct();
 101  	 	 if (ADODB_ASSOC_CASE !== ADODB_ASSOC_CASE_NATIVE) {
 102  	 	 	 $this->rsPrefix .= 'assoc_';
 103  	 	 }
 104  	 	 $this->_bindInputArray = true;
 105  	 }
 106  
 107  
 108  	 // the following should be compat with postgresql 7.2,
 109  	 // which makes obsolete the LIMIT limit,offset syntax
 110  	function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0)
 111  	 {
 112  	 	 $nrows = (int) $nrows;
 113  	 	 $offset = (int) $offset;
 114  	 	 $offsetStr = ($offset >= 0) ? " OFFSET ".((integer)$offset) : '';
 115  	 	 $limitStr  = ($nrows >= 0)  ? " LIMIT ".((integer)$nrows) : '';
 116  	 	 if ($secs2cache)
 117  	 	 	 $rs = $this->CacheExecute($secs2cache,$sql."$limitStr$offsetStr",$inputarr);
 118  	 	 else
 119  	 	 	 $rs = $this->Execute($sql."$limitStr$offsetStr",$inputarr);
 120  
 121  	 	 return $rs;
 122  	 }
 123  
 124  	 /*
 125  	 function Prepare($sql)
 126  	 {
 127  	 	 $info = $this->ServerInfo();
 128  	 	 if ($info['version']>=7.3) {
 129  	 	 	 return array($sql,false);
 130  	 	 }
 131  	 	 return $sql;
 132  	 }
 133  	 */
 134  
 135  	 /**
 136  	  * Generate the SQL to retrieve MetaColumns data
 137  	  * @param string $table Table name
 138  	  * @param string $schema Schema name (can be blank)
 139  	  * @return string SQL statement to execute
 140  	  */
 141  	protected function _generateMetaColumnsSQL($table, $schema)
 142  	 {
 143  	 	 if ($schema) {
 144  	 	 	 return sprintf($this->metaColumnsSQL1, $table, $table, $table, $schema);
 145  	 	 }
 146  	 	 else {
 147  	 	 	 return sprintf($this->metaColumnsSQL, $table, $table, $schema);
 148  	 	 }
 149  	 }
 150  
 151  	 /**
 152  	  * @returns assoc array where keys are tables, and values are foreign keys
 153  	  */
 154  	function MetaForeignKeys($table, $owner=false, $upper=false)
 155  	 {
 156  	 	 # Regex isolates the 2 terms between parenthesis using subexpressions
 157  	 	 $regex = '^.*\((.*)\).*\((.*)\).*$';
 158  	 	 $sql="
 159  	 	 	 SELECT
 160  	 	 	 	 lookup_table,
 161  	 	 	 	 regexp_replace(consrc, '$regex', '\\2') AS lookup_field,
 162  	 	 	 	 dep_table,
 163  	 	 	 	 regexp_replace(consrc, '$regex', '\\1') AS dep_field
 164  	 	 	 FROM (
 165  	 	 	 	 SELECT
 166  	 	 	 	 	 pg_get_constraintdef(c.oid) AS consrc,
 167  	 	 	 	 	 t.relname AS dep_table,
 168  	 	 	 	 	 ft.relname AS lookup_table
 169  	 	 	 	 FROM pg_constraint c
 170  	 	 	 	 JOIN pg_class t ON (t.oid = c.conrelid)
 171  	 	 	 	 JOIN pg_class ft ON (ft.oid = c.confrelid)
 172  	 	 	 	 JOIN pg_namespace nft ON (nft.oid = ft.relnamespace)
 173  	 	 	 	 LEFT JOIN pg_description ds ON (ds.objoid = c.oid)
 174  	 	 	 	 JOIN pg_namespace n ON (n.oid = t.relnamespace)
 175  	 	 	 	 WHERE c.contype = 'f'::\"char\"
 176  	 	 	 	 ORDER BY t.relname, n.nspname, c.conname, c.oid
 177  	 	 	 	 ) constraints
 178  	 	 	 WHERE
 179  	 	 	 	 dep_table='".strtolower($table)."'
 180  	 	 	 ORDER BY
 181  	 	 	 	 lookup_table,
 182  	 	 	 	 dep_table,
 183  	 	 	 	 dep_field";
 184  	 	 $rs = $this->Execute($sql);
 185  
 186  	 	 if (!$rs || $rs->EOF) return false;
 187  
 188  	 	 $a = array();
 189  	 	 while (!$rs->EOF) {
 190  	 	 	 if ($upper) {
 191  	 	 	 	 $a[strtoupper($rs->Fields('lookup_table'))][] = strtoupper(str_replace('"','',$rs->Fields('dep_field').'='.$rs->Fields('lookup_field')));
 192  	 	 	 } else {
 193  	 	 	 	 $a[$rs->Fields('lookup_table')][] = str_replace('"','',$rs->Fields('dep_field').'='.$rs->Fields('lookup_field'));
 194  	 	 	 }
 195  	 	 	 $rs->MoveNext();
 196  	 	 }
 197  
 198  	 	 return $a;
 199  
 200  	 }
 201  
 202  	 // from  Edward Jaramilla, improved version - works on pg 7.4
 203  	function _old_MetaForeignKeys($table, $owner=false, $upper=false)
 204  	 {
 205  	 	 $sql = 'SELECT t.tgargs as args
 206  	 	 FROM
 207  	 	 pg_trigger t,pg_class c,pg_proc p
 208  	 	 WHERE
 209  	 	 t.tgenabled AND
 210  	 	 t.tgrelid = c.oid AND
 211  	 	 t.tgfoid = p.oid AND
 212  	 	 p.proname = \'RI_FKey_check_ins\' AND
 213  	 	 c.relname = \''.strtolower($table).'\'
 214  	 	 ORDER BY
 215  	 	 	 t.tgrelid';
 216  
 217  	 	 $rs = $this->Execute($sql);
 218  
 219  	 	 if (!$rs || $rs->EOF) return false;
 220  
 221  	 	 $arr = $rs->GetArray();
 222  	 	 $a = array();
 223  	 	 foreach($arr as $v) {
 224  	 	 	 $data = explode(chr(0), $v['args']);
 225  	 	 	 $size = count($data)-1; //-1 because the last node is empty
 226  	 	 	 for($i = 4; $i < $size; $i++) {
 227  	 	 	 	 if ($upper)
 228  	 	 	 	 	 $a[strtoupper($data[2])][] = strtoupper($data[$i].'='.$data[++$i]);
 229  	 	 	 	 else
 230  	 	 	 	 	 $a[$data[2]][] = $data[$i].'='.$data[++$i];
 231  	 	 	 }
 232  	 	 }
 233  	 	 return $a;
 234  	 }
 235  
 236  	function _query($sql,$inputarr=false)
 237  	 {
 238  	 	 if (! $this->_bindInputArray) {
 239  	 	 	 // We don't have native support for parameterized queries, so let's emulate it at the parent
 240  	 	 	 return ADODB_postgres64::_query($sql, $inputarr);
 241  	 	 }
 242  
 243  	 	 $this->_pnum = 0;
 244  	 	 $this->_errorMsg = false;
 245  	 	 // -- added Cristiano da Cunha Duarte
 246  	 	 if ($inputarr) {
 247  	 	 	 $sqlarr = explode('?',trim($sql));
 248  	 	 	 $sql = '';
 249  	 	 	 $i = 1;
 250  	 	 	 $last = sizeof($sqlarr)-1;
 251  	 	 	 foreach($sqlarr as $v) {
 252  	 	 	 	 if ($last < $i) $sql .= $v;
 253  	 	 	 	 else $sql .= $v.' $'.$i;
 254  	 	 	 	 $i++;
 255  	 	 	 }
 256  
 257  	 	 	 $rez = pg_query_params($this->_connectionID,$sql, $inputarr);
 258  	 	 } else {
 259  	 	 	 $rez = pg_query($this->_connectionID,$sql);
 260  	 	 }
 261  	 	 // check if no data returned, then no need to create real recordset
 262  	 	 if ($rez && pg_num_fields($rez) <= 0) {
 263  	 	 	 if (is_resource($this->_resultid) && get_resource_type($this->_resultid) === 'pgsql result') {
 264  	 	 	 	 pg_free_result($this->_resultid);
 265  	 	 	 }
 266  	 	 	 $this->_resultid = $rez;
 267  	 	 	 return true;
 268  	 	 }
 269  	 	 return $rez;
 270  	 }
 271  
 272  	 // this is a set of functions for managing client encoding - very important if the encodings
 273  	 // of your database and your output target (i.e. HTML) don't match
 274  	 //for instance, you may have UNICODE database and server it on-site as WIN1251 etc.
 275  	 // GetCharSet - get the name of the character set the client is using now
 276  	 // the functions should work with Postgres 7.0 and above, the set of charsets supported
 277  	 // depends on compile flags of postgres distribution - if no charsets were compiled into the server
 278  	 // it will return 'SQL_ANSI' always
 279  	function GetCharSet()
 280  	 {
 281  	 	 //we will use ADO's builtin property charSet
 282  	 	 $this->charSet = @pg_client_encoding($this->_connectionID);
 283  	 	 if (!$this->charSet) {
 284  	 	 	 return false;
 285  	 	 } else {
 286  	 	 	 return $this->charSet;
 287  	 	 }
 288  	 }
 289  
 290  	 // SetCharSet - switch the client encoding
 291  	function SetCharSet($charset_name)
 292  	 {
 293  	 	 $this->GetCharSet();
 294  	 	 if ($this->charSet !== $charset_name) {
 295  	 	 	 $if = pg_set_client_encoding($this->_connectionID, $charset_name);
 296  	 	 	 if ($if == "0" & $this->GetCharSet() == $charset_name) {
 297  	 	 	 	 return true;
 298  	 	 	 } else return false;
 299  	 	 } else return true;
 300  	 }
 301  
 302  }
 303  
 304  /*--------------------------------------------------------------------------------------
 305  	 Class Name: Recordset
 306  --------------------------------------------------------------------------------------*/
 307  
 308  class ADORecordSet_postgres7 extends ADORecordSet_postgres64{
 309  
 310  	 var $databaseType = "postgres7";
 311  
 312  	 // 10% speedup to move MoveNext to child class
 313  	function MoveNext()
 314  	 {
 315  	 	 if (!$this->EOF) {
 316  	 	 	 $this->_currentRow++;
 317  	 	 	 if ($this->_numOfRows < 0 || $this->_numOfRows > $this->_currentRow) {
 318  	 	 	 	 $this->fields = @pg_fetch_array($this->_queryID,$this->_currentRow,$this->fetchMode);
 319  
 320  	 	 	 	 if (is_array($this->fields)) {
 321  	 	 	 	 	 if ($this->fields && isset($this->_blobArr)) $this->_fixblobs();
 322  	 	 	 	 	 return true;
 323  	 	 	 	 }
 324  	 	 	 }
 325  	 	 	 $this->fields = false;
 326  	 	 	 $this->EOF = true;
 327  	 	 }
 328  	 	 return false;
 329  	 }
 330  
 331  }
 332  
 333  class ADORecordSet_assoc_postgres7 extends ADORecordSet_postgres64{
 334  
 335  	 var $databaseType = "postgres7";
 336  
 337  
 338  	function _fetch()
 339  	 {
 340  	 	 if ($this->_currentRow >= $this->_numOfRows && $this->_numOfRows >= 0) {
 341  	 	 	 return false;
 342  	 	 }
 343  
 344  	 	 $this->fields = @pg_fetch_array($this->_queryID,$this->_currentRow,$this->fetchMode);
 345  
 346  	 	 if ($this->fields) {
 347  	 	 	 if (isset($this->_blobArr)) $this->_fixblobs();
 348  	 	 	 $this->_updatefields();
 349  	 	 }
 350  
 351  	 	 return (is_array($this->fields));
 352  	 }
 353  
 354  	function MoveNext()
 355  	 {
 356  	 	 if (!$this->EOF) {
 357  	 	 	 $this->_currentRow++;
 358  	 	 	 if ($this->_numOfRows < 0 || $this->_numOfRows > $this->_currentRow) {
 359  	 	 	 	 $this->fields = @pg_fetch_array($this->_queryID,$this->_currentRow,$this->fetchMode);
 360  
 361  	 	 	 	 if (is_array($this->fields)) {
 362  	 	 	 	 	 if ($this->fields) {
 363  	 	 	 	 	 	 if (isset($this->_blobArr)) $this->_fixblobs();
 364  
 365  	 	 	 	 	 	 $this->_updatefields();
 366  	 	 	 	 	 }
 367  	 	 	 	 	 return true;
 368  	 	 	 	 }
 369  	 	 	 }
 370  
 371  
 372  	 	 	 $this->fields = false;
 373  	 	 	 $this->EOF = true;
 374  	 	 }
 375  	 	 return false;
 376  	 }
 377  }