adodb-postgres7.inc.php 10 KB

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