PageRenderTime 45ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/php/extlib/ezsql/ezsql_oracle.php

http://github.com/openmelody/melody
PHP | 543 lines | 322 code | 113 blank | 108 comment | 36 complexity | 0282982cd780bbd6ac23d1b38e5301bc MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, LGPL-2.1
  1. <?php
  2. // ==================================================================
  3. // Author: Justin Vincent (justin@visunet.ie)
  4. // Web: http://php.justinvincent.com
  5. // Name: ezSQL
  6. // Desc: Class to make it very easy to deal with Oracle8 database connections.
  7. //
  8. // !! IMPORTANT !!
  9. //
  10. // Please send me a mail telling me what you think of ezSQL
  11. // and what your using it for!! Cheers. [ justin@visunet.ie ]
  12. //
  13. // ==================================================================
  14. // User Settings -- CHANGE HERE
  15. define("EZSQL_DB_USER", ""); // <-- oracle db user
  16. define("EZSQL_DB_PASSWORD", ""); // <-- oracle db password
  17. define("EZSQL_DB_NAME", ""); // <-- oracle db name
  18. // ==================================================================
  19. // ezSQL Constants
  20. define("EZSQL_VERSION","1.26");
  21. define("OBJECT","OBJECT",true);
  22. define("ARRAY_A","ARRAY_A",true);
  23. define("ARRAY_N","ARRAY_N",true);
  24. // ==================================================================
  25. // The Main Class
  26. class ezsql
  27. {
  28. var $trace = false; // same as $debug_all
  29. var $debug_all = false; // same as $trace
  30. var $show_errors = false;
  31. var $num_queries = 0;
  32. var $last_query;
  33. var $col_info;
  34. var $debug_called;
  35. var $vardump_called;
  36. // ==================================================================
  37. // DB Constructor - connects to the server and selects a database
  38. function db($dbuser, $dbpassword, $dbname, $dbhost = '', $dbport = '')
  39. {
  40. $dbport = $dbport ? ':' . $dbport : '';
  41. $dbname = $dbhost ? "//$dbhost$dbport/$dbname" : $dbname;
  42. $this->dbh = @OCILogon($dbuser, $dbpassword, $dbname);
  43. if ( ! $this->dbh )
  44. {
  45. $this->print_error("Error","<ol><b>Error establishing a database connection!</b><li>Are you sure you have the correct user/password?<li>Are you sure that you have typed the correct database instance name?<li>Are you sure that the database server is running?</ol>");
  46. }
  47. else
  48. {
  49. // Remember these values for the select function
  50. $this->dbuser = $dbuser;
  51. $this->dbpassword = $dbpassword;
  52. $this->dbname = $dbname;
  53. }
  54. }
  55. // ==================================================================
  56. // Select a DB instance (if another one needs to be selected)
  57. function select($db)
  58. {
  59. $this->db($this->dbuser, $this->dbpassword, $dbname);
  60. }
  61. // ====================================================================
  62. // Format a string correctly for safe insert under all PHP conditions
  63. function escape($str)
  64. {
  65. return str_replace("'","''",str_replace("''","'",stripslashes($str)));
  66. }
  67. // ==================================================================
  68. // Print SQL/DB error.
  69. function print_error($title = "SQL/DB Error", $str = "")
  70. {
  71. // All erros go to the global error array $EZSQL_ERROR..
  72. global $EZSQL_ERROR;
  73. // If no error string then assume an oracle error is required
  74. if ( !$str )
  75. {
  76. $error = OCIError();
  77. $str = $error["message"] . "-" . $error["code"];
  78. }
  79. // Log this error to the global array..
  80. $EZSQL_ERROR[] = array
  81. (
  82. "query" => $this->last_query,
  83. "error_str" => $str
  84. );
  85. // Is error output turned on or not..
  86. if ( $this->show_errors )
  87. {
  88. // If there is an error then take note of it
  89. print "<blockquote><font face=arial size=2 color=ff0000>";
  90. print "<b>$title --</b> ";
  91. print "[<font color=000077>$str</font>]";
  92. print "</font></blockquote>";
  93. }
  94. else
  95. {
  96. return false;
  97. }
  98. }
  99. // ==================================================================
  100. // These special Oracle functions make sure that even if your test
  101. // pattern is '' it will still match records that are null if
  102. // you don't use these funcs then oracle will return no results
  103. // if $user = ''; even if there were records that = ''
  104. //
  105. // SELECT * FROM USERS WHERE USER = ".$db->is_equal_str($user)."
  106. function is_equal_str($str='')
  107. {
  108. return ($str==''?'IS NULL':"= '".$this->escape($str)."'");
  109. }
  110. function is_equal_int($int)
  111. {
  112. return ($int==''?'IS NULL':'= '.$int);
  113. }
  114. // ==================================================================
  115. // If you have set up a sequence this function returns the
  116. // next ID from that sequence
  117. function insert_id($seq_name)
  118. {
  119. return $this->get_var("SELECT $seq_name.nextVal id FROM Dual");
  120. }
  121. function sysdate()
  122. {
  123. return "SYSDATE";
  124. }
  125. // ==================================================================
  126. // Turn error handling on or off..
  127. function show_errors()
  128. {
  129. $this->show_errors = true;
  130. }
  131. function hide_errors()
  132. {
  133. $this->show_errors = false;
  134. }
  135. // ==================================================================
  136. // Kill cached query results
  137. function flush()
  138. {
  139. // Get rid of these
  140. $this->last_result = null;
  141. $this->col_info = null;
  142. $this->last_query = null;
  143. }
  144. // ==================================================================
  145. // Basic Query - see docs for more detail
  146. function query($query)
  147. {
  148. // For reg expressions
  149. $query = trim($query);
  150. $return_value = 0;
  151. // Flush cached values..
  152. $this->flush();
  153. // Log how the function was called
  154. $this->func_call = "\$db->query(\"$query\")";
  155. // Keep track of the last query for debug..
  156. $this->last_query = $query;
  157. // Parses the query and returns a statement..
  158. if ( ! $stmt = OCIParse($this->dbh, $query))
  159. {
  160. $this->print_error("Last Query",$query);
  161. }
  162. // Execut the query..
  163. elseif ( ! $this->result = OCIExecute($stmt))
  164. {
  165. $this->print_error("Last Query",$query);
  166. }
  167. $this->num_queries++;
  168. // If query was an insert
  169. if ( preg_match('/^(insert|delete|update|create)\s+/i', $query) )
  170. {
  171. // num afected rows
  172. $return_value = $this->rows_affected = OCIRowCount($stmt);
  173. }
  174. // If query was a select
  175. else
  176. {
  177. // Get column information
  178. if ( $num_cols = @OCINumCols($stmt) )
  179. {
  180. // Fetch the column meta data
  181. for ( $i = 1; $i <= $num_cols; $i++ )
  182. {
  183. $this->col_info[($i-1)]->name = OCIColumnName($stmt,$i);
  184. $this->col_info[($i-1)]->type = OCIColumnType($stmt,$i);
  185. $this->col_info[($i-1)]->size = OCIColumnSize($stmt,$i);
  186. }
  187. }
  188. // If there are any results then get them
  189. if ($this->num_rows = @OCIFetchStatement($stmt,$results))
  190. {
  191. // Convert results into object orientated results..
  192. // Due to Oracle strange return structure - loop through columns
  193. foreach ( $results as $col_title => $col_contents )
  194. {
  195. $row_num=0;
  196. // then - loop through rows
  197. foreach ( $col_contents as $col_content )
  198. {
  199. $this->last_result[$row_num]->{$col_title} = $col_content;
  200. $row_num++;
  201. }
  202. }
  203. }
  204. // num result rows
  205. $return_value = $this->num_rows;
  206. }
  207. // If debug ALL queries
  208. $this->trace || $this->debug_all ? $this->debug() : null ;
  209. return $return_value;
  210. }
  211. // ==================================================================
  212. // Get one variable from the DB - see docs for more detail
  213. function get_var($query=null,$x=0,$y=0)
  214. {
  215. // Log how the function was called
  216. $this->func_call = "\$db->get_var(\"$query\",$x,$y)";
  217. // If there is a query then perform it if not then use cached results..
  218. if ( $query )
  219. {
  220. $this->query($query);
  221. }
  222. // Extract var out of cached results based x,y vals
  223. if ( $this->last_result[$y] )
  224. {
  225. $values = array_values(get_object_vars($this->last_result[$y]));
  226. }
  227. // If there is a value return it else return null
  228. return (isset($values[$x]) && $values[$x]!=='')?$values[$x]:null;
  229. }
  230. // ==================================================================
  231. // Get one row from the DB - see docs for more detail
  232. function get_row($query=null,$output=OBJECT,$y=0)
  233. {
  234. // Log how the function was called
  235. $this->func_call = "\$db->get_row(\"$query\",$output,$y)";
  236. // If there is a query then perform it if not then use cached results..
  237. if ( $query )
  238. {
  239. $this->query($query);
  240. }
  241. // If the output is an object then return object using the row offset..
  242. if ( $output == OBJECT )
  243. {
  244. return $this->last_result[$y]?$this->last_result[$y]:null;
  245. }
  246. // If the output is an associative array then return row as such..
  247. elseif ( $output == ARRAY_A )
  248. {
  249. return $this->last_result[$y]?get_object_vars($this->last_result[$y]):null;
  250. }
  251. // If the output is an numerical array then return row as such..
  252. elseif ( $output == ARRAY_N )
  253. {
  254. return $this->last_result[$y]?array_values(get_object_vars($this->last_result[$y])):null;
  255. }
  256. // If invalid output type was specified..
  257. else
  258. {
  259. $this->print_error(" \$db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N");
  260. }
  261. }
  262. // ==================================================================
  263. // Function to get 1 column from the cached result set based in X index
  264. // se docs for usage and info
  265. function get_col($query=null,$x=0)
  266. {
  267. // If there is a query then perform it if not then use cached results..
  268. if ( $query )
  269. {
  270. $this->query($query);
  271. }
  272. // Extract the column values
  273. for ( $i=0; $i < count($this->last_result); $i++ )
  274. {
  275. $new_array[$i] = $this->get_var(null,$x,$i);
  276. }
  277. return $new_array;
  278. }
  279. // ==================================================================
  280. // Return the the query as a result set - see docs for more details
  281. function get_results($query=null, $output = OBJECT)
  282. {
  283. // Log how the function was called
  284. $this->func_call = "\$db->get_results(\"$query\", $output)";
  285. // If there is a query then perform it if not then use cached results..
  286. if ( $query )
  287. {
  288. $this->query($query);
  289. }
  290. // Send back array of objects. Each row is an object
  291. if ( $output == OBJECT )
  292. {
  293. return $this->last_result;
  294. }
  295. elseif ( $output == ARRAY_A || $output == ARRAY_N )
  296. {
  297. if ( $this->last_result )
  298. {
  299. $i=0;
  300. foreach( $this->last_result as $row )
  301. {
  302. $new_array[$i] = get_object_vars($row);
  303. if ( $output == ARRAY_N )
  304. {
  305. $new_array[$i] = array_values($new_array[$i]);
  306. }
  307. $i++;
  308. }
  309. return $new_array;
  310. }
  311. else
  312. {
  313. return null;
  314. }
  315. }
  316. }
  317. // ==================================================================
  318. // Function to get column meta data info pertaining to the last query
  319. // see docs for more info and usage
  320. function get_col_info($info_type="name",$col_offset=-1)
  321. {
  322. if ( $this->col_info )
  323. {
  324. if ( $col_offset == -1 )
  325. {
  326. $i=0;
  327. foreach($this->col_info as $col )
  328. {
  329. $new_array[$i] = $col->{$info_type};
  330. $i++;
  331. }
  332. return $new_array;
  333. }
  334. else
  335. {
  336. return $this->col_info[$col_offset]->{$info_type};
  337. }
  338. }
  339. }
  340. // ==================================================================
  341. // Dumps the contents of any input variable to screen in a nicely
  342. // formatted and easy to understand way - any type: Object, Var or Array
  343. function vardump($mixed='')
  344. {
  345. echo "<p><table><tr><td bgcolor=ffffff><blockquote><font color=000090>";
  346. echo "<pre><font face=arial>";
  347. if ( ! $this->vardump_called )
  348. {
  349. echo "<font color=800080><b>ezSQL</b> (v".EZSQL_VERSION.") <b>Variable Dump..</b></font>\n\n";
  350. }
  351. $var_type = gettype ($mixed);
  352. print_r(($mixed?$mixed:"<font color=red>No Value / False</font>"));
  353. echo "\n\n<b>Type:</b> " . ucfirst($var_type) . "\n";
  354. echo "<b>Last Query</b> [$this->num_queries]<b>:</b> ".($this->last_query?$this->last_query:"NULL")."\n";
  355. echo "<b>Last Function Call:</b> " . ($this->func_call?$this->func_call:"None")."\n";
  356. echo "<b>Last Rows Returned:</b> ".count($this->last_result)."\n";
  357. echo "</font></pre></font></blockquote></td></tr></table>".$this->donation();
  358. echo "\n<hr size=1 noshade color=dddddd>";
  359. $this->vardump_called = true;
  360. }
  361. // Alias for the above function
  362. function dumpvar($mixed)
  363. {
  364. $this->vardump($mixed);
  365. }
  366. // ==================================================================
  367. // Displays the last query string that was sent to the database & a
  368. // table listing results (if there were any).
  369. // (abstracted into a seperate file to save server overhead).
  370. function debug()
  371. {
  372. echo "<blockquote>";
  373. // Only show ezSQL credits once..
  374. if ( ! $this->debug_called )
  375. {
  376. echo "<font color=800080 face=arial size=2><b>ezSQL</b> (v".EZSQL_VERSION.") <b>Debug..</b></font><p>\n";
  377. }
  378. echo "<font face=arial size=2 color=000099><b>Query</b> [$this->num_queries] <b>--</b> ";
  379. echo "[<font color=000000><b>$this->last_query</b></font>]</font><p>";
  380. echo "<font face=arial size=2 color=000099><b>Query Result..</b></font>";
  381. echo "<blockquote>";
  382. if ( $this->col_info )
  383. {
  384. // =====================================================
  385. // Results top rows
  386. echo "<table cellpadding=5 cellspacing=1 bgcolor=555555>";
  387. echo "<tr bgcolor=eeeeee><td nowrap valign=bottom><font color=555599 face=arial size=2><b>(row)</b></font></td>";
  388. for ( $i=0; $i < count($this->col_info); $i++ )
  389. {
  390. echo "<td nowrap align=left valign=top><font size=1 color=555599 face=arial>{$this->col_info[$i]->type} {$this->col_info[$i]->max_length}</font><br><span style='font-family: arial; font-size: 10pt; font-weight: bold;'>{$this->col_info[$i]->name}</span></td>";
  391. }
  392. echo "</tr>";
  393. // ======================================================
  394. // print main results
  395. if ( $this->last_result )
  396. {
  397. $i=0;
  398. foreach ( $this->get_results(null,ARRAY_N) as $one_row )
  399. {
  400. $i++;
  401. echo "<tr bgcolor=ffffff><td bgcolor=eeeeee nowrap align=middle><font size=2 color=555599 face=arial>$i</font></td>";
  402. foreach ( $one_row as $item )
  403. {
  404. echo "<td nowrap><font face=arial size=2>$item</font></td>";
  405. }
  406. echo "</tr>";
  407. }
  408. } // if last result
  409. else
  410. {
  411. echo "<tr bgcolor=ffffff><td colspan=".(count($this->col_info)+1)."><font face=arial size=2>No Results</font></td></tr>";
  412. }
  413. echo "</table>";
  414. } // if col_info
  415. else
  416. {
  417. echo "<font face=arial size=2>No Results</font>";
  418. }
  419. echo "</blockquote></blockquote>".$this->donation()."<hr noshade color=dddddd size=1>";
  420. $this->debug_called = true;
  421. }
  422. // =======================================================
  423. // Naughty little function to ask for some remuniration!
  424. function donation()
  425. {
  426. return "<font size=1 face=arial color=000000>If ezSQL has helped <a href=\"https://www.paypal.com/xclick/business=justin%40justinvincent.com&item_name=ezSQL&no_note=1&tax=0\" style=\"color: 0000CC;\">make a donation!?</a> &nbsp;&nbsp;[ go on! you know you want to! ]</font>";
  427. }
  428. }
  429. # $db = new db(EZSQL_DB_USER, EZSQL_DB_PASSWORD, EZSQL_DB_NAME );
  430. ?>