PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/unsupported/lib/ez_sql_oracleTNS.php

http://github.com/jv2222/ezSQL
PHP | 472 lines | 323 code | 33 blank | 116 comment | 28 complexity | 3b6c20b48b1c266e224d67eb7a7fb4ad MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. class ezSQL_oracleTNS extends ezSQLcore
  3. {
  4. /**
  5. * ezSQL error strings - Oracle 8 and 9
  6. * @var array
  7. */
  8. private $_ezsql_oracle_str = array
  9. (
  10. 1 => 'Require $dbuser, $dbpassword and $dbname to connect to a database server',
  11. 2 => 'ezSQL auto created the following Oracle sequence'
  12. );
  13. /**
  14. * Database user name
  15. * @var string
  16. */
  17. private $_dbuser;
  18. /**
  19. * Database password for the given user
  20. * @var string
  21. */
  22. private $_dbpassword;
  23. /**
  24. * Database server name or IP address
  25. * @var string
  26. */
  27. private $_host;
  28. /**
  29. * TCP port for the database connection on the specified server
  30. * @var integer
  31. */
  32. private $_port;
  33. /**
  34. * The service name
  35. * @var string
  36. */
  37. private $_serviceName;
  38. /**
  39. * The connection string
  40. * @var string
  41. */
  42. private $_tns;
  43. /**
  44. * The Oracle NLS_LANG character set for the connection
  45. * Default: Empty string
  46. * @var string
  47. */
  48. private $_characterSet;
  49. /**
  50. * Use oci_pconnect instead of oci_connect to have connection pooling
  51. * enabled with PHP
  52. * @var boolean
  53. */
  54. private $_pooling;
  55. /**
  56. * Show errors
  57. * @var boolean Default is true
  58. */
  59. public $show_errors = true;
  60. /**
  61. * Constructor - allow the user to perform a qucik connect at the same time
  62. * as initialising the ezSQL_oracleTNS class
  63. *
  64. * @param string $host The server name or the IP address of the server
  65. * @param integer $port The TCP port of the server
  66. * @param string $serviceName The service name
  67. * @param string $dbuser The database user name
  68. * Default is empty string
  69. * @param string $dbpassword The database users password
  70. * Default is empty string
  71. * @param string $characterSet The Oracle NLS_LANG character string
  72. * Default is empty string
  73. * @param boolean $pooling Use connection pooling with pconnect instead of
  74. * connect
  75. * Default is false
  76. * @throws Exception Requires Orcle OCI Lib and ez_sql_core.php
  77. */
  78. public function __construct($host, $port, $serviceName, $dbuser='', $dbpassword='', $characterSet='', $pooling=false) {
  79. if ( ! function_exists ('OCILogon') ) {
  80. throw new Exception('<b>Fatal Error:</b> ezSQL_oracleTNS requires Oracle OCI Lib to be compiled and/or linked in to the PHP engine');
  81. }
  82. if ( ! class_exists ('ezSQLcore') ) {
  83. throw new Exception('<b>Fatal Error:</b> ezSQL_oracle8_9 requires ezSQLcore (ez_sql_core.php) to be included/loaded before it can be used');
  84. }
  85. parent::__construct();
  86. // Turn on track errors
  87. ini_set('track_errors',1);
  88. $this->_dbuser = $dbuser;
  89. $this->_dbpassword = $dbpassword;
  90. $this->_host = $host;
  91. $this->_port = $port;
  92. $this->_serviceName = $serviceName;
  93. $this->_characterSet = $characterSet;
  94. $this->setTNS();
  95. $this->_pooling = $pooling;
  96. global $_ezOracleTNS;
  97. $_ezOracleTNS = $this;
  98. } // __construct
  99. /**
  100. * Try to connect to Oracle database server
  101. *
  102. * @param string $dbuser The database user name
  103. * Default is empty string
  104. * @param string $dbpassword The database users password
  105. * Default is empty string
  106. * @return boolean
  107. */
  108. public function connect($dbuser='', $dbpassword='') {
  109. $this->_connected = false;
  110. if (empty($dbuser)) {
  111. $dbuser = $this->_dbuser;
  112. }
  113. if (empty($dbpassword)) {
  114. $dbpassword = $this->_dbpassword;
  115. }
  116. // Must have a user and a password
  117. if ( ! $dbuser || ! $dbpassword) {
  118. $this->register_error($this->_ezsql_oracle_str[1] . ' in ' . __FILE__ . ' on line ' . __LINE__);
  119. $this->show_errors ? trigger_error($this->_ezsql_oracle_str[1], E_USER_WARNING) : null;
  120. }
  121. // Try to establish the server database handle
  122. else {
  123. if ($this->_pooling) {
  124. $this->_pconnect($dbuser, $dbpassword);
  125. } else {
  126. $this->_connect($dbuser, $dbpassword);
  127. }
  128. }
  129. return $this->_connected;
  130. } // connect
  131. /**
  132. * Try to connect to Oracle database server with connection pooling
  133. *
  134. * @param string $dbuser The database user name
  135. * Default is empty string
  136. * @param string $dbpassword The database users password
  137. * Default is empty string
  138. * @return boolean
  139. */
  140. public function pconnect($dbuser='', $dbpassword='') {
  141. $this->_connected = false;
  142. if (empty($dbuser)) {
  143. $dbuser = $this->_dbuser;
  144. }
  145. if (empty($dbpassword)) {
  146. $dbpassword = $this->_dbpassword;
  147. }
  148. // Must have a user and a password
  149. if ( ! $dbuser || ! $dbpassword) {
  150. $this->register_error($this->_ezsql_oracle_str[1] . ' in ' . __FILE__ . ' on line ' . __LINE__);
  151. $this->show_errors ? trigger_error($this->_ezsql_oracle_str[1], E_USER_WARNING) : null;
  152. }
  153. // Try to establish the server database handle
  154. else {
  155. $this->_pconnect($dbuser, $dbpassword);
  156. }
  157. return $this->_connected;
  158. } // pconnect
  159. /**
  160. * Try to connect to Oracle database server without connection pooling
  161. *
  162. * @param string $dbuser The database user name
  163. * Default is empty string
  164. * @param string $dbpassword The database users password
  165. * Default is empty string
  166. */
  167. private function _connect($dbuser='', $dbpassword='') {
  168. if ( ! empty($this->_characterSet) ) {
  169. if ( ! $this->dbh = @oci_connect($dbuser, $dbpassword, $this->_tns, $this->_characterSet) ) {
  170. $this->register_error($php_errormsg);
  171. $this->show_errors ? trigger_error($php_errormsg,E_USER_WARNING) : null;
  172. } else {
  173. $this->_dbuser = $dbuser;
  174. $this->_dbpassword = $dbpassword;
  175. $this->_connected = true;
  176. }
  177. } else {
  178. if ( ! $this->dbh = @oci_connect($dbuser, $dbpassword, $this->_tns) ) {
  179. $this->register_error($php_errormsg);
  180. $this->show_errors ? trigger_error($php_errormsg,E_USER_WARNING) : null;
  181. } else {
  182. $this->_dbuser = $dbuser;
  183. $this->_dbpassword = $dbpassword;
  184. $this->_connected = true;
  185. }
  186. }
  187. }
  188. /**
  189. * Try to connect to Oracle database server with connection pooling
  190. *
  191. * @param string $dbuser The database user name
  192. * Default is empty string
  193. * @param string $dbpassword The database users password
  194. * Default is empty string
  195. */
  196. private function _pconnect($dbuser='', $dbpassword='') {
  197. if ( ! empty($this->_characterSet) ) {
  198. if ( ! $this->dbh = @oci_pconnect($dbuser, $dbpassword, $this->_tns, $this->_characterSet) ) {
  199. $this->register_error($php_errormsg);
  200. $this->show_errors ? trigger_error($php_errormsg,E_USER_WARNING) : null;
  201. } else {
  202. $this->_dbuser = $dbuser;
  203. $this->_dbpassword = $dbpassword;
  204. $this->_connected = true;
  205. }
  206. } else {
  207. if ( ! $this->dbh = @oci_pconnect($dbuser, $dbpassword, $this->_tns) ) {
  208. $this->register_error($php_errormsg);
  209. $this->show_errors ? trigger_error($php_errormsg,E_USER_WARNING) : null;
  210. } else {
  211. $this->_dbuser = $dbuser;
  212. $this->_dbpassword = $dbpassword;
  213. $this->_connected = true;
  214. }
  215. }
  216. } // _connect
  217. /**
  218. * In the case of Oracle quick_connect is not really needed because std.
  219. * connect already does what quick connect does - but for the sake of
  220. * consistency it has been included
  221. *
  222. * @param string $dbuser The database user name
  223. * Default is empty string
  224. * @param string $dbpassword The database users password
  225. * Default is empty string
  226. * @return boolean
  227. */
  228. public function quick_connect($dbuser='', $dbpassword='') {
  229. return $this->connect($dbuser, $dbpassword);
  230. } // quick_connect
  231. /**
  232. * Format a Oracle string correctly for safe Oracle insert
  233. *
  234. * @param string $str
  235. * @return string
  236. */
  237. public function escape($str) {
  238. if ( !isset($str) ) return '';
  239. if ( is_numeric($str) ) return $str;
  240. $non_displayables = array(
  241. '/%0[0-8bcef]/', // url encoded 00-08, 11, 12, 14, 15
  242. '/%1[0-9a-f]/', // url encoded 16-31
  243. '/[\x00-\x08]/', // 00-08
  244. '/\x0b/', // 11
  245. '/\x0c/', // 12
  246. '/[\x0e-\x1f]/' // 14-31
  247. );
  248. foreach ( $non_displayables as $regex )
  249. $str = preg_replace( $regex, '', $str );
  250. $search = array("\\", "\x00", "\n", "\r", "'", '"', "\x1a");
  251. $replace = array("\\\\","\\0","\\n", "\\r", "\'", '\"', "\\Z");
  252. return str_replace($search, $replace, $str);
  253. } // escape
  254. /**
  255. * Return Oracle specific system date syntax
  256. * i.e. Oracle: SYSDATE Mysql: NOW()
  257. *
  258. * @return string
  259. */
  260. public function sysdate() {
  261. return 'SYSDATE';
  262. } // sysdate
  263. /**********************************************************************
  264. * These special Oracle functions make sure that even if your test
  265. * pattern is '' it will still match records that are null if
  266. * you don't use these funcs then oracle will return no results
  267. * if $user = ''; even if there were records that = ''
  268. *
  269. * SELECT * FROM USERS WHERE USER = ".$db->is_equal_str($user)."
  270. */
  271. /**
  272. * Returns an escaped equal string
  273. *
  274. * @param string $str
  275. * @return string
  276. */
  277. public function is_equal_str($str='') {
  278. return ($str=='' ? 'IS NULL' : "= '" . $this->escape($str) . "'");
  279. } // is_equal_str
  280. /**
  281. * Returns an equal string for integer values
  282. *
  283. * @param string $int
  284. * @return string
  285. */
  286. public function is_equal_int($int) {
  287. return ($int=='' ? 'IS NULL': '= ' . $int);
  288. } // is_equal_int
  289. /**
  290. * Another oracle specific function - if you have set up a sequence this
  291. * function returns the next ID from that sequence
  292. * If the sequence is not defined, the sequence is created by this method.
  293. * Though be shure, that you use the correct sequence name not to end in
  294. * more than one sequence for a primary key of a table.
  295. *
  296. * @param string $seq_name Name of the sequenze
  297. * @return string
  298. */
  299. public function insert_id($seq_name) {
  300. $return_val = $this->get_var("SELECT $seq_name.nextVal id FROM Dual");
  301. // If no return value then try to create the sequence
  302. if ( ! $return_val ) {
  303. $this->query("CREATE SEQUENCE $seq_name maxValue 9999999999 INCREMENT BY 1 START WITH 1 CACHE 20 CYCLE");
  304. $return_val = $this->get_var("SELECT $seq_name.nextVal id FROM Dual");
  305. $this->register_error($this->_ezsql_oracle_str[2] . ": $seq_name");
  306. $this->show_errors ? trigger_error($this->_ezsql_oracle_str[2] . ": $seq_name", E_USER_NOTICE) : null;
  307. }
  308. return $return_val;
  309. } // insert_id
  310. /**
  311. * An alias for insert_id using the original Oracle function name.
  312. *
  313. * @param string $seq_name Name of the sequenze
  314. * @return string
  315. */
  316. public function nextVal($seq_name) {
  317. return $this->insert_id($seq_name);
  318. } // nextVal
  319. /**
  320. * Perform Oracle query and try to determine result value
  321. *
  322. * @param string $query
  323. * @return object
  324. */
  325. public function query($query) {
  326. $return_value = 0;
  327. // Flush cached values..
  328. $this->flush();
  329. // Log how the function was called
  330. $this->func_call = "\$db->query(\"$query\")";
  331. // Keep track of the last query for debug..
  332. $this->last_query = $query;
  333. $this->num_queries++;
  334. // Use core file cache function
  335. if ( $cache = $this->get_cache($query) ) {
  336. return $cache;
  337. }
  338. // If there is no existing database connection then try to connect
  339. if ( ! isset($this->dbh) || ! $this->dbh ) {
  340. $this->connect($this->_dbuser, $this->_dbpassword);
  341. }
  342. // Parses the query and returns a statement..
  343. if ( ! $stmt = OCIParse($this->dbh, $query)) {
  344. $error = OCIError($this->dbh);
  345. $this->register_error($error['message']);
  346. $this->show_errors ? trigger_error($error['message'], E_USER_WARNING) : null;
  347. return false;
  348. } elseif ( ! $this->result = OCIExecute($stmt)) {
  349. // Execut the query..
  350. $error = OCIError($stmt);
  351. $this->register_error($error['message']);
  352. $this->show_errors ? trigger_error($error['message'], E_USER_WARNING) : null;
  353. return false;
  354. }
  355. // If query was an insert
  356. $is_insert = false;
  357. if ( preg_match('/^(insert|delete|update|create) /i', $query) ) {
  358. $is_insert = true;
  359. // num afected rows
  360. $this->_affectedRows = @OCIRowCount($stmt);
  361. $return_value = $this->_affectedRows;
  362. } else {
  363. // If query was a select
  364. // Get column information
  365. if ( $num_cols = @OCINumCols($stmt) ) {
  366. // Fetch the column meta data
  367. for ( $i = 1; $i <= $num_cols; $i++ ) {
  368. $this->col_info[($i-1)]->name = @OCIColumnName($stmt, $i);
  369. $this->col_info[($i-1)]->type = @OCIColumnType($stmt, $i);
  370. $this->col_info[($i-1)]->size = @OCIColumnSize($stmt, $i);
  371. }
  372. }
  373. // If there are any results then get them
  374. if ($this->num_rows = @OCIFetchStatement($stmt, $results)) {
  375. // Convert results into object orientated results..
  376. // Due to Oracle strange return structure - loop through columns
  377. foreach ( $results as $col_title => $col_contents ) {
  378. $row_num=0;
  379. // Then - loop through rows
  380. foreach ( $col_contents as $col_content ) {
  381. $this->last_result[$row_num]->{$col_title} = $col_content;
  382. $row_num++;
  383. }
  384. }
  385. }
  386. // Num result rows
  387. $return_value = $this->num_rows;
  388. }
  389. // Disk caching of queries
  390. $this->store_cache($query, $is_insert);
  391. // If debug ALL queries
  392. $this->trace || $this->debug_all ? $this->debug() : null;
  393. return $return_value;
  394. } // query
  395. /**
  396. * Close the database connection
  397. */
  398. public function disconnect() {
  399. if ( $this->dbh ) {
  400. oci_close($this->dbh);
  401. $this->_connected = false;
  402. }
  403. } // disconnect
  404. /**
  405. * Sets the TNS variable with all relevant connection informations
  406. */
  407. private function setTNS() {
  408. $this->_tns = "(DESCRIPTION =
  409. (ADDRESS=(PROTOCOL = TCP)(HOST = $this->_host)(PORT = $this->_port))
  410. (CONNECT_DATA=(SERVER = DEDICATED)(SERVICE_NAME = $this->_serviceName)))";
  411. } // setTNS
  412. } // ezSQL_oracle8_9