PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/concreteOLD/libraries/3rdparty/adodb/drivers/adodb-pdo_pgsql.inc.php

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