adodb-oracle.inc.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. Latest version is available at http://adodb.org/
  10. Oracle data driver. Requires Oracle client. Works on Windows and Unix and Oracle 7.
  11. If you are using Oracle 8 or later, use the oci8 driver which is much better and more reliable.
  12. */
  13. // security - hide paths
  14. if (!defined('ADODB_DIR')) die();
  15. class ADODB_oracle extends ADOConnection {
  16. var $databaseType = "oracle";
  17. var $replaceQuote = "''"; // string to use to replace quotes
  18. var $concat_operator='||';
  19. var $_curs;
  20. var $_initdate = true; // init date to YYYY-MM-DD
  21. var $metaTablesSQL = 'select table_name from cat';
  22. var $metaColumnsSQL = "select cname,coltype,width from col where tname='%s' order by colno";
  23. var $sysDate = "TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM-DD'),'YYYY-MM-DD')";
  24. var $sysTimeStamp = 'SYSDATE';
  25. var $connectSID = true;
  26. function __construct()
  27. {
  28. }
  29. // format and return date string in database date format
  30. function DBDate($d, $isfld = false)
  31. {
  32. if (is_string($d)) $d = ADORecordSet::UnixDate($d);
  33. if (is_object($d)) $ds = $d->format($this->fmtDate);
  34. else $ds = adodb_date($this->fmtDate,$d);
  35. return 'TO_DATE('.$ds.",'YYYY-MM-DD')";
  36. }
  37. // format and return date string in database timestamp format
  38. function DBTimeStamp($ts, $isfld = false)
  39. {
  40. if (is_string($ts)) $ts = ADORecordSet::UnixTimeStamp($ts);
  41. if (is_object($ts)) $ds = $ts->format($this->fmtDate);
  42. else $ds = adodb_date($this->fmtTimeStamp,$ts);
  43. return 'TO_DATE('.$ds.",'RRRR-MM-DD, HH:MI:SS AM')";
  44. }
  45. function BindDate($d)
  46. {
  47. $d = ADOConnection::DBDate($d);
  48. if (strncmp($d,"'",1)) return $d;
  49. return substr($d,1,strlen($d)-2);
  50. }
  51. function BindTimeStamp($d)
  52. {
  53. $d = ADOConnection::DBTimeStamp($d);
  54. if (strncmp($d,"'",1)) return $d;
  55. return substr($d,1,strlen($d)-2);
  56. }
  57. function BeginTrans()
  58. {
  59. $this->autoCommit = false;
  60. ora_commitoff($this->_connectionID);
  61. return true;
  62. }
  63. function CommitTrans($ok=true)
  64. {
  65. if (!$ok) return $this->RollbackTrans();
  66. $ret = ora_commit($this->_connectionID);
  67. ora_commiton($this->_connectionID);
  68. return $ret;
  69. }
  70. function RollbackTrans()
  71. {
  72. $ret = ora_rollback($this->_connectionID);
  73. ora_commiton($this->_connectionID);
  74. return $ret;
  75. }
  76. /* there seems to be a bug in the oracle extension -- always returns ORA-00000 - no error */
  77. function ErrorMsg()
  78. {
  79. if ($this->_errorMsg !== false) return $this->_errorMsg;
  80. if (is_resource($this->_curs)) $this->_errorMsg = @ora_error($this->_curs);
  81. if (empty($this->_errorMsg)) $this->_errorMsg = @ora_error($this->_connectionID);
  82. return $this->_errorMsg;
  83. }
  84. function ErrorNo()
  85. {
  86. if ($this->_errorCode !== false) return $this->_errorCode;
  87. if (is_resource($this->_curs)) $this->_errorCode = @ora_errorcode($this->_curs);
  88. if (empty($this->_errorCode)) $this->_errorCode = @ora_errorcode($this->_connectionID);
  89. return $this->_errorCode;
  90. }
  91. // returns true or false
  92. function _connect($argHostname, $argUsername, $argPassword, $argDatabasename, $mode=0)
  93. {
  94. if (!function_exists('ora_plogon')) return null;
  95. // <G. Giunta 2003/03/03/> Reset error messages before connecting
  96. $this->_errorMsg = false;
  97. $this->_errorCode = false;
  98. // G. Giunta 2003/08/13 - This looks danegrously suspicious: why should we want to set
  99. // the oracle home to the host name of remote DB?
  100. // if ($argHostname) putenv("ORACLE_HOME=$argHostname");
  101. if($argHostname) { // code copied from version submitted for oci8 by Jorma Tuomainen <jorma.tuomainen@ppoy.fi>
  102. if (empty($argDatabasename)) $argDatabasename = $argHostname;
  103. else {
  104. if(strpos($argHostname,":")) {
  105. $argHostinfo=explode(":",$argHostname);
  106. $argHostname=$argHostinfo[0];
  107. $argHostport=$argHostinfo[1];
  108. } else {
  109. $argHostport="1521";
  110. }
  111. if ($this->connectSID) {
  112. $argDatabasename="(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=".$argHostname
  113. .")(PORT=$argHostport))(CONNECT_DATA=(SID=$argDatabasename)))";
  114. } else
  115. $argDatabasename="(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=".$argHostname
  116. .")(PORT=$argHostport))(CONNECT_DATA=(SERVICE_NAME=$argDatabasename)))";
  117. }
  118. }
  119. if ($argDatabasename) $argUsername .= "@$argDatabasename";
  120. //if ($argHostname) print "<p>Connect: 1st argument should be left blank for $this->databaseType</p>";
  121. if ($mode == 1)
  122. $this->_connectionID = ora_plogon($argUsername,$argPassword);
  123. else
  124. $this->_connectionID = ora_logon($argUsername,$argPassword);
  125. if ($this->_connectionID === false) return false;
  126. if ($this->autoCommit) ora_commiton($this->_connectionID);
  127. if ($this->_initdate) {
  128. $rs = $this->_query("ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD'");
  129. if ($rs) ora_close($rs);
  130. }
  131. return true;
  132. }
  133. // returns true or false
  134. function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
  135. {
  136. return $this->_connect($argHostname, $argUsername, $argPassword, $argDatabasename, 1);
  137. }
  138. // returns query ID if successful, otherwise false
  139. function _query($sql,$inputarr=false)
  140. {
  141. // <G. Giunta 2003/03/03/> Reset error messages before executing
  142. $this->_errorMsg = false;
  143. $this->_errorCode = false;
  144. $curs = ora_open($this->_connectionID);
  145. if ($curs === false) return false;
  146. $this->_curs = $curs;
  147. if (!ora_parse($curs,$sql)) return false;
  148. if (ora_exec($curs)) return $curs;
  149. // <G. Giunta 2004/03/03> before we close the cursor, we have to store the error message
  150. // that we can obtain ONLY from the cursor (and not from the connection)
  151. $this->_errorCode = @ora_errorcode($curs);
  152. $this->_errorMsg = @ora_error($curs);
  153. // </G. Giunta 2004/03/03>
  154. @ora_close($curs);
  155. return false;
  156. }
  157. // returns true or false
  158. function _close()
  159. {
  160. return @ora_logoff($this->_connectionID);
  161. }
  162. }
  163. /*--------------------------------------------------------------------------------------
  164. Class Name: Recordset
  165. --------------------------------------------------------------------------------------*/
  166. class ADORecordset_oracle extends ADORecordSet {
  167. var $databaseType = "oracle";
  168. var $bind = false;
  169. function __construct($queryID,$mode=false)
  170. {
  171. if ($mode === false) {
  172. global $ADODB_FETCH_MODE;
  173. $mode = $ADODB_FETCH_MODE;
  174. }
  175. $this->fetchMode = $mode;
  176. $this->_queryID = $queryID;
  177. $this->_inited = true;
  178. $this->fields = array();
  179. if ($queryID) {
  180. $this->_currentRow = 0;
  181. $this->EOF = !$this->_fetch();
  182. @$this->_initrs();
  183. } else {
  184. $this->_numOfRows = 0;
  185. $this->_numOfFields = 0;
  186. $this->EOF = true;
  187. }
  188. return $this->_queryID;
  189. }
  190. /* Returns: an object containing field information.
  191. Get column information in the Recordset object. fetchField() can be used in order to obtain information about
  192. fields in a certain query result. If the field offset isn't specified, the next field that wasn't yet retrieved by
  193. fetchField() is retrieved. */
  194. function FetchField($fieldOffset = -1)
  195. {
  196. $fld = new ADOFieldObject;
  197. $fld->name = ora_columnname($this->_queryID, $fieldOffset);
  198. $fld->type = ora_columntype($this->_queryID, $fieldOffset);
  199. $fld->max_length = ora_columnsize($this->_queryID, $fieldOffset);
  200. return $fld;
  201. }
  202. /* Use associative array to get fields array */
  203. function Fields($colname)
  204. {
  205. if (!$this->bind) {
  206. $this->bind = array();
  207. for ($i=0; $i < $this->_numOfFields; $i++) {
  208. $o = $this->FetchField($i);
  209. $this->bind[strtoupper($o->name)] = $i;
  210. }
  211. }
  212. return $this->fields[$this->bind[strtoupper($colname)]];
  213. }
  214. function _initrs()
  215. {
  216. $this->_numOfRows = -1;
  217. $this->_numOfFields = @ora_numcols($this->_queryID);
  218. }
  219. function _seek($row)
  220. {
  221. return false;
  222. }
  223. function _fetch($ignore_fields=false) {
  224. // should remove call by reference, but ora_fetch_into requires it in 4.0.3pl1
  225. if ($this->fetchMode & ADODB_FETCH_ASSOC)
  226. return @ora_fetch_into($this->_queryID,$this->fields,ORA_FETCHINTO_NULLS|ORA_FETCHINTO_ASSOC);
  227. else
  228. return @ora_fetch_into($this->_queryID,$this->fields,ORA_FETCHINTO_NULLS);
  229. }
  230. /* close() only needs to be called if you are worried about using too much memory while your script
  231. is running. All associated result memory for the specified result identifier will automatically be freed. */
  232. function _close()
  233. {
  234. return @ora_close($this->_queryID);
  235. }
  236. function MetaType($t, $len = -1, $fieldobj = false)
  237. {
  238. if (is_object($t)) {
  239. $fieldobj = $t;
  240. $t = $fieldobj->type;
  241. $len = $fieldobj->max_length;
  242. }
  243. switch (strtoupper($t)) {
  244. case 'VARCHAR':
  245. case 'VARCHAR2':
  246. case 'CHAR':
  247. case 'VARBINARY':
  248. case 'BINARY':
  249. if ($len <= $this->blobSize) return 'C';
  250. case 'LONG':
  251. case 'LONG VARCHAR':
  252. case 'CLOB':
  253. return 'X';
  254. case 'LONG RAW':
  255. case 'LONG VARBINARY':
  256. case 'BLOB':
  257. return 'B';
  258. case 'DATE': return 'D';
  259. //case 'T': return 'T';
  260. case 'BIT': return 'L';
  261. case 'INT':
  262. case 'SMALLINT':
  263. case 'INTEGER': return 'I';
  264. default: return 'N';
  265. }
  266. }
  267. }