adodb-oci8po.inc.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. /*
  3. @version v5.20.17 31-Mar-2020
  4. @copyright (c) 2000-2013 John Lim. 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. Latest version is available at http://adodb.org/
  10. Portable version of oci8 driver, to make it more similar to other database drivers.
  11. The main differences are
  12. 1. that the OCI_ASSOC names are in lowercase instead of uppercase.
  13. 2. bind variables are mapped using ? instead of :<bindvar>
  14. Should some emulation of RecordCount() be implemented?
  15. */
  16. // security - hide paths
  17. if (!defined('ADODB_DIR')) die();
  18. include_once(ADODB_DIR.'/drivers/adodb-oci8.inc.php');
  19. class ADODB_oci8po extends ADODB_oci8 {
  20. var $databaseType = 'oci8po';
  21. var $dataProvider = 'oci8';
  22. var $metaColumnsSQL = "select lower(cname),coltype,width, SCALE, PRECISION, NULLS, DEFAULTVAL from col where tname='%s' order by colno"; //changed by smondino@users.sourceforge. net
  23. var $metaTablesSQL = "select lower(table_name),table_type from cat where table_type in ('TABLE','VIEW')";
  24. function __construct()
  25. {
  26. $this->_hasOCIFetchStatement = ADODB_PHPVER >= 0x4200;
  27. # oci8po does not support adodb extension: adodb_movenext()
  28. }
  29. function Param($name,$type='C')
  30. {
  31. return '?';
  32. }
  33. function Prepare($sql,$cursor=false)
  34. {
  35. $sqlarr = explode('?',$sql);
  36. $sql = $sqlarr[0];
  37. for ($i = 1, $max = sizeof($sqlarr); $i < $max; $i++) {
  38. $sql .= ':'.($i-1) . $sqlarr[$i];
  39. }
  40. return ADODB_oci8::Prepare($sql,$cursor);
  41. }
  42. function Execute($sql,$inputarr=false)
  43. {
  44. return ADOConnection::Execute($sql,$inputarr);
  45. }
  46. /**
  47. * The optimizations performed by ADODB_oci8::SelectLimit() are not
  48. * compatible with the oci8po driver, so we rely on the slower method
  49. * from the base class.
  50. * We can't properly handle prepared statements either due to preprocessing
  51. * of query parameters, so we treat them as regular SQL statements.
  52. */
  53. function SelectLimit($sql, $nrows=-1, $offset=-1, $inputarr=false, $secs2cache=0)
  54. {
  55. if(is_array($sql)) {
  56. // $sql = $sql[0];
  57. }
  58. return ADOConnection::SelectLimit($sql, $nrows, $offset, $inputarr, $secs2cache);
  59. }
  60. // emulate handling of parameters ? ?, replacing with :bind0 :bind1
  61. function _query($sql,$inputarr=false)
  62. {
  63. if (is_array($inputarr)) {
  64. $i = 0;
  65. if (is_array($sql)) {
  66. foreach($inputarr as $v) {
  67. $arr['bind'.$i++] = $v;
  68. }
  69. } else {
  70. $sql = $this->extractBinds($sql,$inputarr);
  71. }
  72. }
  73. return ADODB_oci8::_query($sql,$inputarr);
  74. }
  75. /**
  76. * Replaces compatibility bind markers with oracle ones and returns a
  77. * valid sql statement
  78. *
  79. * This replaces a regexp based section of code that has been subject
  80. * to numerous tweaks, as more extreme test cases have appeared. This
  81. * is now done this like this to help maintainability and avoid the
  82. * need to rely on regexp experienced maintainers
  83. *
  84. * @param string $sql The sql statement
  85. * @param string[] $inputarr The bind array
  86. *
  87. * @return string The modified statement
  88. */
  89. final private function extractBinds($sql,$inputarr)
  90. {
  91. $inString = false;
  92. $escaped = 0;
  93. $sqlLength = strlen($sql) - 1;
  94. $newSql = '';
  95. $bindCount = 0;
  96. /*
  97. * inputarr is the passed in bind list, which is associative, but
  98. * we only want the keys here
  99. */
  100. $inputKeys = array_keys($inputarr);
  101. for ($i=0;$i<=$sqlLength;$i++)
  102. {
  103. /*
  104. * find the next character of the string
  105. */
  106. $c = $sql{$i};
  107. if ($c == "'" && !$inString && $escaped==0)
  108. /*
  109. * Found the start of a string inside the statement
  110. */
  111. $inString = true;
  112. elseif ($c == "\\" && $escaped==0)
  113. /*
  114. * The next character will be escaped
  115. */
  116. $escaped = 1;
  117. elseif ($c == "'" && $inString && $escaped==0)
  118. /*
  119. * We found the end of the string
  120. */
  121. $inString = false;
  122. if ($escaped == 2)
  123. $escaped = 0;
  124. if ($escaped==0 && !$inString && $c == '?')
  125. /*
  126. * We found a bind symbol, replace it with the oracle equivalent
  127. */
  128. $newSql .= ':' . $inputKeys[$bindCount++];
  129. else
  130. /*
  131. * Add the current character the pile
  132. */
  133. $newSql .= $c;
  134. if ($escaped == 1)
  135. /*
  136. * We have just found an escape character, make sure we ignore the
  137. * next one that comes along, it might be a ' character
  138. */
  139. $escaped = 2;
  140. }
  141. return $newSql;
  142. }
  143. }
  144. /*--------------------------------------------------------------------------------------
  145. Class Name: Recordset
  146. --------------------------------------------------------------------------------------*/
  147. class ADORecordset_oci8po extends ADORecordset_oci8 {
  148. var $databaseType = 'oci8po';
  149. function __construct($queryID,$mode=false)
  150. {
  151. parent::__construct($queryID,$mode);
  152. }
  153. function Fields($colname)
  154. {
  155. if ($this->fetchMode & OCI_ASSOC) return $this->fields[$colname];
  156. if (!$this->bind) {
  157. $this->bind = array();
  158. for ($i=0; $i < $this->_numOfFields; $i++) {
  159. $o = $this->FetchField($i);
  160. $this->bind[strtoupper($o->name)] = $i;
  161. }
  162. }
  163. return $this->fields[$this->bind[strtoupper($colname)]];
  164. }
  165. // lowercase field names...
  166. function _FetchField($fieldOffset = -1)
  167. {
  168. $fld = new ADOFieldObject;
  169. $fieldOffset += 1;
  170. $fld->name = OCIcolumnname($this->_queryID, $fieldOffset);
  171. if (ADODB_ASSOC_CASE == ADODB_ASSOC_CASE_LOWER) {
  172. $fld->name = strtolower($fld->name);
  173. }
  174. $fld->type = OCIcolumntype($this->_queryID, $fieldOffset);
  175. $fld->max_length = OCIcolumnsize($this->_queryID, $fieldOffset);
  176. if ($fld->type == 'NUMBER') {
  177. $sc = OCIColumnScale($this->_queryID, $fieldOffset);
  178. if ($sc == 0) {
  179. $fld->type = 'INT';
  180. }
  181. }
  182. return $fld;
  183. }
  184. // 10% speedup to move MoveNext to child class
  185. function MoveNext()
  186. {
  187. $ret = @oci_fetch_array($this->_queryID,$this->fetchMode);
  188. if($ret !== false) {
  189. global $ADODB_ANSI_PADDING_OFF;
  190. $this->fields = $ret;
  191. $this->_currentRow++;
  192. $this->_updatefields();
  193. if (!empty($ADODB_ANSI_PADDING_OFF)) {
  194. foreach($this->fields as $k => $v) {
  195. if (is_string($v)) $this->fields[$k] = rtrim($v);
  196. }
  197. }
  198. return true;
  199. }
  200. if (!$this->EOF) {
  201. $this->EOF = true;
  202. $this->_currentRow++;
  203. }
  204. return false;
  205. }
  206. /* Optimize SelectLimit() by using OCIFetch() instead of OCIFetchInto() */
  207. function GetArrayLimit($nrows,$offset=-1)
  208. {
  209. if ($offset <= 0) {
  210. $arr = $this->GetArray($nrows);
  211. return $arr;
  212. }
  213. for ($i=1; $i < $offset; $i++)
  214. if (!@OCIFetch($this->_queryID)) {
  215. $arr = array();
  216. return $arr;
  217. }
  218. $ret = @oci_fetch_array($this->_queryID,$this->fetchMode);
  219. if ($ret === false) {
  220. $arr = array();
  221. return $arr;
  222. }
  223. $this->fields = $ret;
  224. $this->_updatefields();
  225. $results = array();
  226. $cnt = 0;
  227. while (!$this->EOF && $nrows != $cnt) {
  228. $results[$cnt++] = $this->fields;
  229. $this->MoveNext();
  230. }
  231. return $results;
  232. }
  233. function _fetch()
  234. {
  235. global $ADODB_ANSI_PADDING_OFF;
  236. $ret = @oci_fetch_array($this->_queryID,$this->fetchMode);
  237. if ($ret) {
  238. $this->fields = $ret;
  239. $this->_updatefields();
  240. if (!empty($ADODB_ANSI_PADDING_OFF)) {
  241. foreach($this->fields as $k => $v) {
  242. if (is_string($v)) $this->fields[$k] = rtrim($v);
  243. }
  244. }
  245. }
  246. return $ret !== false;
  247. }
  248. }