adodb-pdo_pgsql.inc.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /*
  3. @version v5.20.17 31-Mar-2020
  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 8.
  10. */
  11. class ADODB_pdo_pgsql extends ADODB_pdo {
  12. var $metaDatabasesSQL = "select datname from pg_database where datname not in ('template0','template1') order by 1";
  13. var $metaTablesSQL = "select tablename,'T' from pg_tables where tablename not like 'pg\_%'
  14. and tablename not in ('sql_features', 'sql_implementation_info', 'sql_languages',
  15. 'sql_packages', 'sql_sizing', 'sql_sizing_profiles')
  16. union
  17. select viewname,'V' from pg_views where viewname not like 'pg\_%'";
  18. //"select tablename from pg_tables where tablename not like 'pg_%' order by 1";
  19. var $isoDates = true; // accepts dates in ISO format
  20. var $sysDate = "CURRENT_DATE";
  21. var $sysTimeStamp = "CURRENT_TIMESTAMP";
  22. var $blobEncodeType = 'C';
  23. var $metaColumnsSQL = "SELECT a.attname,t.typname,a.attlen,a.atttypmod,a.attnotnull,a.atthasdef,a.attnum
  24. FROM pg_class c, pg_attribute a,pg_type t
  25. WHERE relkind in ('r','v') AND (c.relname='%s' or c.relname = lower('%s')) and a.attname not like '....%%'
  26. AND a.attnum > 0 AND a.atttypid = t.oid AND a.attrelid = c.oid ORDER BY a.attnum";
  27. // used when schema defined
  28. var $metaColumnsSQL1 = "SELECT a.attname, t.typname, a.attlen, a.atttypmod, a.attnotnull, a.atthasdef, a.attnum
  29. FROM pg_class c, pg_attribute a, pg_type t, pg_namespace n
  30. WHERE relkind in ('r','v') AND (c.relname='%s' or c.relname = lower('%s'))
  31. and c.relnamespace=n.oid and n.nspname='%s'
  32. and a.attname not like '....%%' AND a.attnum > 0
  33. AND a.atttypid = t.oid AND a.attrelid = c.oid ORDER BY a.attnum";
  34. // get primary key etc -- from Freek Dijkstra
  35. var $metaKeySQL = "SELECT ic.relname AS index_name, a.attname AS column_name,i.indisunique AS unique_key, i.indisprimary AS primary_key
  36. 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'";
  37. var $hasAffectedRows = true;
  38. var $hasLimit = false; // set to true for pgsql 7 only. support pgsql/mysql SELECT * FROM TABLE LIMIT 10
  39. // below suggested by Freek Dijkstra
  40. var $true = 't'; // string that represents TRUE for a database
  41. var $false = 'f'; // string that represents FALSE for a database
  42. var $fmtDate = "'Y-m-d'"; // used by DBDate() as the default date format used by the database
  43. var $fmtTimeStamp = "'Y-m-d G:i:s'"; // used by DBTimeStamp as the default timestamp fmt.
  44. var $hasMoveFirst = true;
  45. var $hasGenID = true;
  46. var $_genIDSQL = "SELECT NEXTVAL('%s')";
  47. var $_genSeqSQL = "CREATE SEQUENCE %s START %s";
  48. var $_dropSeqSQL = "DROP SEQUENCE %s";
  49. 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";
  50. var $random = 'random()'; /// random function
  51. var $concat_operator='||';
  52. function _init($parentDriver)
  53. {
  54. $parentDriver->hasTransactions = false; ## <<< BUG IN PDO pgsql driver
  55. $parentDriver->hasInsertID = true;
  56. $parentDriver->_nestedSQL = true;
  57. }
  58. function ServerInfo()
  59. {
  60. $arr['description'] = ADOConnection::GetOne("select version()");
  61. $arr['version'] = ADOConnection::_findvers($arr['description']);
  62. return $arr;
  63. }
  64. function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0)
  65. {
  66. $nrows = (int) $nrows;
  67. $offset = (int) $offset;
  68. $offsetStr = ($offset >= 0) ? " OFFSET $offset" : '';
  69. $limitStr = ($nrows >= 0) ? " LIMIT $nrows" : '';
  70. if ($secs2cache)
  71. $rs = $this->CacheExecute($secs2cache,$sql."$limitStr$offsetStr",$inputarr);
  72. else
  73. $rs = $this->Execute($sql."$limitStr$offsetStr",$inputarr);
  74. return $rs;
  75. }
  76. function MetaTables($ttype=false,$showSchema=false,$mask=false)
  77. {
  78. $info = $this->ServerInfo();
  79. if ($info['version'] >= 7.3) {
  80. $this->metaTablesSQL = "select tablename,'T' from pg_tables where tablename not like 'pg\_%'
  81. and schemaname not in ( 'pg_catalog','information_schema')
  82. union
  83. select viewname,'V' from pg_views where viewname not like 'pg\_%' and schemaname not in ( 'pg_catalog','information_schema') ";
  84. }
  85. if ($mask) {
  86. $save = $this->metaTablesSQL;
  87. $mask = $this->qstr(strtolower($mask));
  88. if ($info['version']>=7.3)
  89. $this->metaTablesSQL = "
  90. select tablename,'T' from pg_tables where tablename like $mask and schemaname not in ( 'pg_catalog','information_schema')
  91. union
  92. select viewname,'V' from pg_views where viewname like $mask and schemaname not in ( 'pg_catalog','information_schema') ";
  93. else
  94. $this->metaTablesSQL = "
  95. select tablename,'T' from pg_tables where tablename like $mask
  96. union
  97. select viewname,'V' from pg_views where viewname like $mask";
  98. }
  99. $ret = ADOConnection::MetaTables($ttype,$showSchema);
  100. if ($mask) {
  101. $this->metaTablesSQL = $save;
  102. }
  103. return $ret;
  104. }
  105. function MetaColumns($table,$normalize=true)
  106. {
  107. global $ADODB_FETCH_MODE;
  108. $schema = false;
  109. $this->_findschema($table,$schema);
  110. if ($normalize) $table = strtolower($table);
  111. $save = $ADODB_FETCH_MODE;
  112. $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
  113. if ($this->fetchMode !== false) $savem = $this->SetFetchMode(false);
  114. if ($schema) $rs = $this->Execute(sprintf($this->metaColumnsSQL1,$table,$table,$schema));
  115. else $rs = $this->Execute(sprintf($this->metaColumnsSQL,$table,$table));
  116. if (isset($savem)) $this->SetFetchMode($savem);
  117. $ADODB_FETCH_MODE = $save;
  118. if ($rs === false) {
  119. $false = false;
  120. return $false;
  121. }
  122. if (!empty($this->metaKeySQL)) {
  123. // If we want the primary keys, we have to issue a separate query
  124. // Of course, a modified version of the metaColumnsSQL query using a
  125. // LEFT JOIN would have been much more elegant, but postgres does
  126. // not support OUTER JOINS. So here is the clumsy way.
  127. $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
  128. $rskey = $this->Execute(sprintf($this->metaKeySQL,($table)));
  129. // fetch all result in once for performance.
  130. $keys = $rskey->GetArray();
  131. if (isset($savem)) $this->SetFetchMode($savem);
  132. $ADODB_FETCH_MODE = $save;
  133. $rskey->Close();
  134. unset($rskey);
  135. }
  136. $rsdefa = array();
  137. if (!empty($this->metaDefaultsSQL)) {
  138. $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
  139. $sql = sprintf($this->metaDefaultsSQL, ($table));
  140. $rsdef = $this->Execute($sql);
  141. if (isset($savem)) $this->SetFetchMode($savem);
  142. $ADODB_FETCH_MODE = $save;
  143. if ($rsdef) {
  144. while (!$rsdef->EOF) {
  145. $num = $rsdef->fields['num'];
  146. $s = $rsdef->fields['def'];
  147. if (strpos($s,'::')===false && substr($s, 0, 1) == "'") { /* quoted strings hack... for now... fixme */
  148. $s = substr($s, 1);
  149. $s = substr($s, 0, strlen($s) - 1);
  150. }
  151. $rsdefa[$num] = $s;
  152. $rsdef->MoveNext();
  153. }
  154. } else {
  155. ADOConnection::outp( "==> SQL => " . $sql);
  156. }
  157. unset($rsdef);
  158. }
  159. $retarr = array();
  160. while (!$rs->EOF) {
  161. $fld = new ADOFieldObject();
  162. $fld->name = $rs->fields[0];
  163. $fld->type = $rs->fields[1];
  164. $fld->max_length = $rs->fields[2];
  165. if ($fld->max_length <= 0) $fld->max_length = $rs->fields[3]-4;
  166. if ($fld->max_length <= 0) $fld->max_length = -1;
  167. if ($fld->type == 'numeric') {
  168. $fld->scale = $fld->max_length & 0xFFFF;
  169. $fld->max_length >>= 16;
  170. }
  171. // dannym
  172. // 5 hasdefault; 6 num-of-column
  173. $fld->has_default = ($rs->fields[5] == 't');
  174. if ($fld->has_default) {
  175. $fld->default_value = $rsdefa[$rs->fields[6]];
  176. }
  177. //Freek
  178. if ($rs->fields[4] == $this->true) {
  179. $fld->not_null = true;
  180. }
  181. // Freek
  182. if (is_array($keys)) {
  183. foreach($keys as $key) {
  184. if ($fld->name == $key['column_name'] AND $key['primary_key'] == $this->true)
  185. $fld->primary_key = true;
  186. if ($fld->name == $key['column_name'] AND $key['unique_key'] == $this->true)
  187. $fld->unique = true; // What name is more compatible?
  188. }
  189. }
  190. if ($ADODB_FETCH_MODE == ADODB_FETCH_NUM) $retarr[] = $fld;
  191. else $retarr[($normalize) ? strtoupper($fld->name) : $fld->name] = $fld;
  192. $rs->MoveNext();
  193. }
  194. $rs->Close();
  195. if (empty($retarr)) {
  196. $false = false;
  197. return $false;
  198. } else return $retarr;
  199. }
  200. }