PageRenderTime 25ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/vj-include/class.ezsql.core.php

http://vanilla-journal.googlecode.com/
PHP | 372 lines | 183 code | 93 blank | 96 comment | 32 complexity | de60f66441a1b7e6fde9a9b8ea113731 MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0
  1. <?php
  2. /**********************************************************************
  3. * Author: Justin Vincent (justin@visunet.ie)
  4. * Web...: http://php.justinvincent.com
  5. * Name..: ezSQL
  6. * Desc..: ezSQL Core module - database abstraction library to make
  7. * it very easy to deal with databases.
  8. *
  9. */
  10. /**********************************************************************
  11. * ezSQL Constants
  12. */
  13. define('EZSQL_VERSION','2.0');
  14. define('OBJECT','OBJECT',true);
  15. define('ARRAY_A','ARRAY_A',true);
  16. define('ARRAY_N','ARRAY_N',true);
  17. define('EZSQL_CORE_ERROR','ezSQLcore can not be used by itself (it is designed for use by database specific modules).');
  18. /**********************************************************************
  19. * Core class containg common functions to manipulate query result
  20. * sets once returned
  21. */
  22. class ezSQLcore
  23. {
  24. var $trace = false; // same as $debug_all
  25. var $debug_all = false; // same as $trace
  26. var $debug_called = false;
  27. var $vardump_called = false;
  28. var $show_errors = false;
  29. var $num_queries = 0;
  30. var $last_query = null;
  31. var $last_error = null;
  32. var $col_info = null;
  33. var $captured_errors = array();
  34. /**********************************************************************
  35. * Constructor
  36. */
  37. function ezSQLcore() { }
  38. /**********************************************************************
  39. * Connect to DB - over-ridden by specific DB class
  40. */
  41. function connect() { die(EZSQL_CORE_ERROR); }
  42. /**********************************************************************
  43. * Select DB - over-ridden by specific DB class
  44. */
  45. function select() { die(EZSQL_CORE_ERROR); }
  46. /**********************************************************************
  47. * Basic Query - over-ridden by specific DB class
  48. */
  49. function query() { die(EZSQL_CORE_ERROR); }
  50. /**********************************************************************
  51. * Format a string correctly for safe insert - over-ridden by specific
  52. * DB class
  53. */
  54. function escape() { die(EZSQL_CORE_ERROR); }
  55. /**********************************************************************
  56. * Return database specific system date syntax
  57. * i.e. Oracle: SYSDATE Mysql: NOW()
  58. */
  59. function sysdate() { die(EZSQL_CORE_ERROR); }
  60. /**********************************************************************
  61. * Print SQL/DB error - over-ridden by specific DB class
  62. */
  63. function register_error($err_str) {
  64. // Keep track of last error
  65. $this->last_error = $err_str;
  66. // Capture all errors to an error array no matter what happens
  67. $this->captured_errors[] = array
  68. (
  69. 'error_str' => $err_str,
  70. 'query' => $this->last_query
  71. );
  72. }
  73. /**********************************************************************
  74. * Turn error handling on or off..
  75. */
  76. function show_errors() {
  77. $this->show_errors = true;
  78. }
  79. function hide_errors() {
  80. $this->show_errors = false;
  81. }
  82. /**********************************************************************
  83. * Kill cached query results
  84. */
  85. function flush() {
  86. // Get rid of these
  87. $this->last_result = null;
  88. $this->col_info = null;
  89. $this->last_query = null;
  90. }
  91. /**********************************************************************
  92. * Get one variable from the DB - see docs for more detail
  93. */
  94. function get_var($query=null,$x=0,$y=0) {
  95. // Log how the function was called
  96. $this->func_call = "\$db->get_var(\"$query\",$x,$y)";
  97. // If there is a query then perform it if not then use cached results..
  98. if ( $query ) { $this->query($query); }
  99. // Extract var out of cached results based x,y vals
  100. if ( $this->last_result[$y] ) {
  101. $values = array_values(get_object_vars($this->last_result[$y]));
  102. }
  103. // If there is a value return it else return null
  104. return (isset($values[$x]) && $values[$x]!=='')?$values[$x]:null;
  105. }
  106. /**********************************************************************
  107. * Get one row from the DB - see docs for more detail
  108. */
  109. function get_row($query=null,$output=OBJECT,$y=0) {
  110. // Log how the function was called
  111. $this->func_call = "\$db->get_row(\"$query\",$output,$y)";
  112. // If there is a query then perform it if not then use cached results..
  113. if ( $query ) {
  114. $this->query($query);
  115. }
  116. // If the output is an object then return object using the row offset..
  117. if ( $output == OBJECT ) {
  118. return $this->last_result[$y]?$this->last_result[$y]:null;
  119. }
  120. // If the output is an associative array then return row as such..
  121. elseif ( $output == ARRAY_A ) {
  122. return $this->last_result[$y]?get_object_vars($this->last_result[$y]):null;
  123. }
  124. // If the output is an numerical array then return row as such..
  125. elseif ( $output == ARRAY_N ) {
  126. return $this->last_result[$y]?array_values(get_object_vars($this->last_result[$y])):null;
  127. }
  128. // If invalid output type was specified..
  129. else
  130. {
  131. $this->print_error(" \$db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N");
  132. }
  133. }
  134. /**********************************************************************
  135. * Function to get 1 column from the cached result set based in X index
  136. * see docs for usage and info
  137. */
  138. function get_col($query=null,$x=0) {
  139. // If there is a query then perform it if not then use cached results..
  140. if ( $query ) { $this->query($query); }
  141. // Extract the column values
  142. for ( $i=0; $i < count($this->last_result); $i++ ) {
  143. $new_array[$i] = $this->get_var(null,$x,$i);
  144. }
  145. return $new_array;
  146. }
  147. /**********************************************************************
  148. * Return the the query as a result set - see docs for more details
  149. */
  150. function get_results($query=null, $output = OBJECT) {
  151. // Log how the function was called
  152. $this->func_call = "\$db->get_results(\"$query\", $output)";
  153. // If there is a query then perform it if not then use cached results..
  154. if ( $query ) { $this->query($query); }
  155. // Send back array of objects. Each row is an object
  156. if ( $output == OBJECT ) {
  157. return $this->last_result;
  158. }
  159. elseif ( $output == ARRAY_A || $output == ARRAY_N ) {
  160. if ( $this->last_result ) {
  161. $i=0;
  162. foreach( $this->last_result as $row ) {
  163. $new_array[$i] = get_object_vars($row);
  164. if ( $output == ARRAY_N ) {
  165. $new_array[$i] = array_values($new_array[$i]);
  166. }
  167. $i++;
  168. }
  169. return $new_array;
  170. }
  171. else {
  172. return null;
  173. }
  174. }
  175. }
  176. /**********************************************************************
  177. * Function to get column meta data info pertaining to the last query
  178. * see docs for more info and usage
  179. */
  180. function get_col_info($info_type="name",$col_offset=-1) {
  181. if ( $this->col_info ) {
  182. if ( $col_offset == -1 ) {
  183. $i=0;
  184. foreach($this->col_info as $col ) {
  185. $new_array[$i] = $col->{$info_type};
  186. $i++;
  187. }
  188. return $new_array;
  189. }
  190. else {
  191. return $this->col_info[$col_offset]->{$info_type};
  192. }
  193. }
  194. }
  195. /**********************************************************************
  196. * Dumps the contents of any input variable to screen in a nicely
  197. * formatted and easy to understand way - any type: Object, Var or Array
  198. */
  199. function vardump($mixed='') {
  200. echo "<p><table><tr><td bgcolor=ffffff><blockquote><font color=000090>";
  201. echo "<pre><font face=arial>";
  202. if ( ! $this->vardump_called ) {
  203. echo "<font color=800080><b>ezSQL</b> (v".EZSQL_VERSION.") <b>Variable Dump..</b></font>\n\n";
  204. }
  205. $var_type = gettype ($mixed);
  206. print_r(($mixed?$mixed:"<font color=red>No Value / False</font>"));
  207. echo "\n\n<b>Type:</b> " . ucfirst($var_type) . "\n";
  208. echo "<b>Last Query</b> [$this->num_queries]<b>:</b> ".($this->last_query?$this->last_query:"NULL")."\n";
  209. echo "<b>Last Function Call:</b> " . ($this->func_call?$this->func_call:"None")."\n";
  210. echo "<b>Last Rows Returned:</b> ".count($this->last_result)."\n";
  211. echo "</font></pre></font></blockquote></td></tr></table>";
  212. echo "\n<hr size=1 noshade color=dddddd>";
  213. $this->vardump_called = true;
  214. }
  215. /**********************************************************************
  216. * Alias for the above function
  217. */
  218. function dumpvar($mixed) { $this->vardump($mixed); }
  219. /**********************************************************************
  220. * Displays the last query string that was sent to the database & a
  221. * table listing results (if there were any).
  222. * (abstracted into a seperate file to save server overhead).
  223. */
  224. function debug() {
  225. echo "<blockquote>";
  226. // Only show ezSQL credits once..
  227. if ( ! $this->debug_called ) {
  228. echo "<font color=800080 face=arial size=2><b>ezSQL</b> (v".EZSQL_VERSION.") <b>Debug..</b></font><p>\n";
  229. }
  230. if ( $this->last_error ) {
  231. echo "<font face=arial size=2 color=000099><b>Last Error --</b> [<font color=000000><b>$this->last_error</b></font>]<p>";
  232. }
  233. echo "<font face=arial size=2 color=000099><b>Query</b> [$this->num_queries] <b>--</b> ";
  234. echo "[<font color=000000><b>$this->last_query</b></font>]</font><p>";
  235. echo "<font face=arial size=2 color=000099><b>Query Result..</b></font>";
  236. echo "<blockquote>";
  237. if ( $this->col_info ) {
  238. // =====================================================
  239. // Results top rows
  240. echo "<table cellpadding=5 cellspacing=1 bgcolor=555555>";
  241. echo "<tr bgcolor=eeeeee><td nowrap valign=bottom><font color=555599 face=arial size=2><b>(row)</b></font></td>";
  242. for ( $i=0; $i < count($this->col_info); $i++ )
  243. {
  244. 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>";
  245. }
  246. echo "</tr>";
  247. // ======================================================
  248. // print main results
  249. if ( $this->last_result )
  250. {
  251. $i=0;
  252. foreach ( $this->get_results(null,ARRAY_N) as $one_row )
  253. {
  254. $i++;
  255. echo "<tr bgcolor=ffffff><td bgcolor=eeeeee nowrap align=middle><font size=2 color=555599 face=arial>$i</font></td>";
  256. foreach ( $one_row as $item )
  257. {
  258. echo "<td nowrap><font face=arial size=2>$item</font></td>";
  259. }
  260. echo "</tr>";
  261. }
  262. } // if last result
  263. else {
  264. echo "<tr bgcolor=ffffff><td colspan=".(count($this->col_info)+1)."><font face=arial size=2>No Results</font></td></tr>";
  265. }
  266. echo "</table>";
  267. } // if col_info
  268. else {
  269. echo "<font face=arial size=2>No Results</font>";
  270. }
  271. echo "</blockquote></blockquote><hr noshade color=dddddd size=1>";
  272. $this->debug_called = true;
  273. }
  274. function reset_error() {
  275. $this->captured_errors = array();
  276. $this->last_error = "";
  277. }
  278. }
  279. ?>