PageRenderTime 59ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/shared/ez_sql_core_2.1_debughack_0.2alpha.php

https://github.com/OmegaVersion/ezSQL
PHP | 524 lines | 259 code | 110 blank | 155 comment | 31 complexity | ead06bb9492aeaeca55ae9bbdd86208d MD5 | raw file
  1. <?php
  2. // =================================================================
  3. // == TJH == To provide optional return value as opposed to simple echo
  4. // == TJH == of the $db->vardump and $db->debug functions
  5. // == TJH == Helpful for assigning the output to a var for handling in situations like template
  6. // == TJH == engines where you want the debugging output rendered in a particular location.
  7. // == TJH == This latest version 0.2 alpha includes a modification that allows
  8. // == TJH == the original dump and debug behaviours to be maintained by default
  9. // == TJH == and hopefully be backward compatible with previous ezSQL versions
  10. // == TJH == USAGE: $ezdump = print_r($db->vardump($result),true);
  11. // == TJH == USAGE: $ezconsole = print_r($db->console,true);
  12. // =========== n.b. for TBS template engine users ==============================
  13. // === TJH === This is hacked to enable an ezSQL pop-up debug console from a TBS template page
  14. // === TJH === The following steps need to be taken:
  15. // === TJH === (1) Set $db->debug_all = true; // in your .php file
  16. // === TJH === and $db->debug_echo = false; // in your .php file
  17. // === TJH === (2) Add the following javascript to top of your html
  18. /*
  19. <ezdebugconsole>
  20. [onload_1;block=ezdebugconsole;when [var.db.debug_all]=1]
  21. <SCRIPT LANGUAGE="JavaScript">
  22. if(self.name == ''){var title = 'Console';}
  23. else{var title = 'Console_' + self.name;}
  24. newWindow = window.open("",title.value,"width=550,height=650,resizable,scrollbars=yes");
  25. newWindow.document.write("<HTML><HEAD><TITLE>ezSQL Debug [var..script_name;htmlconv=js]</TITLE></HEAD><BODY bgcolor=#e8e8e8>");
  26. // newWindow.document.write("<b>Debug for [var..script_name;htmlconv=js]</b><BR />");
  27. newWindow.document.write("<table border=0 width='100%'>");
  28. newWindow.document.write("[var.ezdebug;htmlconv=js]");
  29. newWindow.document.write("</body>\n</html>\n");
  30. </script>
  31. </ezdebugconsole>
  32. */
  33. // === TJH === (3) debug data is called with $db->console
  34. // === TJH === Use something like
  35. // === TJH === $ezdebug = print_r($db->console,true);
  36. // === TJH === to stuff the debug data into a PHP var
  37. // === TJH ===
  38. // === TJH === n.b. Don't forget to slurp the slug of javascript
  39. // === TJH === at the top of the .html template page
  40. // === TJH === you'll need to hack it if you're going to
  41. // === TJH === use it other than with TBS tempalte engine.
  42. // === TJH ===
  43. // === TJH === Search this file for "TJH" comments to find changes
  44. // === TJH === You can contact TJH via http://tomhenry.us/
  45. // =================================================================
  46. /**********************************************************************
  47. * Author: Justin Vincent (jv@jvmultimedia.com)
  48. * Web...: http://twitter.com/justinvincent
  49. * Name..: ezSQL
  50. * Desc..: ezSQL Core module - database abstraction library to make
  51. * it very easy to deal with databases.
  52. *
  53. */
  54. /**********************************************************************
  55. * ezSQL Constants
  56. */
  57. define('EZSQL_VERSION','2.1-console'); // === TJH === added an identifying flag to the version number
  58. define('OBJECT','OBJECT',true);
  59. define('ARRAY_A','ARRAY_A',true);
  60. define('ARRAY_N','ARRAY_N',true);
  61. define('EZSQL_CORE_ERROR','ezSQLcore can not be used by itself (it is designed for use by database specific modules).');
  62. /**********************************************************************
  63. * Core class containg common functions to manipulate query result
  64. * sets once returned
  65. */
  66. class ezSQLcore
  67. {
  68. var $trace = false; // same as $debug_all
  69. var $debug_all = false; // same as $trace
  70. // === TJH ===
  71. var $debug_echo = true; // == TJH == // default now needed for echo of debug function
  72. var $debug_called = false;
  73. var $vardump_called = false;
  74. var $show_errors = true;
  75. var $num_queries = 0;
  76. var $last_query = null;
  77. var $last_error = null;
  78. var $col_info = null;
  79. var $captured_errors = array();
  80. /**********************************************************************
  81. * Constructor
  82. */
  83. function ezSQLcore()
  84. {
  85. }
  86. /**********************************************************************
  87. * Connect to DB - over-ridden by specific DB class
  88. */
  89. function connect()
  90. {
  91. die(EZSQL_CORE_ERROR);
  92. }
  93. /**********************************************************************
  94. * Select DB - over-ridden by specific DB class
  95. */
  96. function select()
  97. {
  98. die(EZSQL_CORE_ERROR);
  99. }
  100. /**********************************************************************
  101. * Basic Query - over-ridden by specific DB class
  102. */
  103. function query()
  104. {
  105. die(EZSQL_CORE_ERROR);
  106. }
  107. /**********************************************************************
  108. * Format a string correctly for safe insert - over-ridden by specific
  109. * DB class
  110. */
  111. function escape()
  112. {
  113. die(EZSQL_CORE_ERROR);
  114. }
  115. /**********************************************************************
  116. * Return database specific system date syntax
  117. * i.e. Oracle: SYSDATE Mysql: NOW()
  118. */
  119. function sysdate()
  120. {
  121. die(EZSQL_CORE_ERROR);
  122. }
  123. /**********************************************************************
  124. * Print SQL/DB error - over-ridden by specific DB class
  125. */
  126. function register_error($err_str)
  127. {
  128. // Keep track of last error
  129. $this->last_error = $err_str;
  130. // Capture all errors to an error array no matter what happens
  131. $this->captured_errors[] = array
  132. (
  133. 'error_str' => $err_str,
  134. 'query' => $this->last_query
  135. );
  136. }
  137. /**********************************************************************
  138. * Turn error handling on or off..
  139. */
  140. function show_errors()
  141. {
  142. $this->show_errors = true;
  143. }
  144. function hide_errors()
  145. {
  146. $this->show_errors = false;
  147. }
  148. /**********************************************************************
  149. * Kill cached query results
  150. */
  151. function flush()
  152. {
  153. // Get rid of these
  154. $this->last_result = null;
  155. $this->col_info = null;
  156. $this->last_query = null;
  157. $this->from_disk_cache = false;
  158. }
  159. /**********************************************************************
  160. * Get one variable from the DB - see docs for more detail
  161. */
  162. function get_var($query=null,$x=0,$y=0)
  163. {
  164. // Log how the function was called
  165. $this->func_call = "\$db->get_var(\"$query\",$x,$y)";
  166. // If there is a query then perform it if not then use cached results..
  167. if ( $query )
  168. {
  169. $this->query($query);
  170. }
  171. // Extract var out of cached results based x,y vals
  172. if ( $this->last_result[$y] )
  173. {
  174. $values = array_values(get_object_vars($this->last_result[$y]));
  175. }
  176. // If there is a value return it else return null
  177. return (isset($values[$x]) && $values[$x]!=='')?$values[$x]:null;
  178. }
  179. /**********************************************************************
  180. * Get one row from the DB - see docs for more detail
  181. */
  182. function get_row($query=null,$output=OBJECT,$y=0)
  183. {
  184. // Log how the function was called
  185. $this->func_call = "\$db->get_row(\"$query\",$output,$y)";
  186. // If there is a query then perform it if not then use cached results..
  187. if ( $query )
  188. {
  189. $this->query($query);
  190. }
  191. // If the output is an object then return object using the row offset..
  192. if ( $output == OBJECT )
  193. {
  194. return $this->last_result[$y]?$this->last_result[$y]:null;
  195. }
  196. // If the output is an associative array then return row as such..
  197. elseif ( $output == ARRAY_A )
  198. {
  199. return $this->last_result[$y]?get_object_vars($this->last_result[$y]):null;
  200. }
  201. // If the output is an numerical array then return row as such..
  202. elseif ( $output == ARRAY_N )
  203. {
  204. return $this->last_result[$y]?array_values(get_object_vars($this->last_result[$y])):null;
  205. }
  206. // If invalid output type was specified..
  207. else
  208. {
  209. $this->print_error(" \$db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N");
  210. }
  211. }
  212. /**********************************************************************
  213. * Function to get 1 column from the cached result set based in X index
  214. * see docs for usage and info
  215. */
  216. function get_col($query=null,$x=0)
  217. {
  218. // If there is a query then perform it if not then use cached results..
  219. if ( $query )
  220. {
  221. $this->query($query);
  222. }
  223. // Extract the column values
  224. for ( $i=0; $i < count($this->last_result); $i++ )
  225. {
  226. $new_array[$i] = $this->get_var(null,$x,$i);
  227. }
  228. return $new_array;
  229. }
  230. /**********************************************************************
  231. * Return the the query as a result set - see docs for more details
  232. */
  233. function get_results($query=null, $output = OBJECT)
  234. {
  235. // Log how the function was called
  236. $this->func_call = "\$db->get_results(\"$query\", $output)";
  237. // If there is a query then perform it if not then use cached results..
  238. if ( $query )
  239. {
  240. $this->query($query);
  241. }
  242. // Send back array of objects. Each row is an object
  243. if ( $output == OBJECT )
  244. {
  245. return $this->last_result;
  246. }
  247. elseif ( $output == ARRAY_A || $output == ARRAY_N )
  248. {
  249. if ( $this->last_result )
  250. {
  251. $i=0;
  252. foreach( $this->last_result as $row )
  253. {
  254. $new_array[$i] = get_object_vars($row);
  255. if ( $output == ARRAY_N )
  256. {
  257. $new_array[$i] = array_values($new_array[$i]);
  258. }
  259. $i++;
  260. }
  261. return $new_array;
  262. }
  263. else
  264. {
  265. return array();
  266. }
  267. }
  268. }
  269. /**********************************************************************
  270. * Function to get column meta data info pertaining to the last query
  271. * see docs for more info and usage
  272. */
  273. function get_col_info($info_type="name",$col_offset=-1)
  274. {
  275. if ( $this->col_info )
  276. {
  277. if ( $col_offset == -1 )
  278. {
  279. $i=0;
  280. foreach($this->col_info as $col )
  281. {
  282. $new_array[$i] = $col->{$info_type};
  283. $i++;
  284. }
  285. return $new_array;
  286. }
  287. else
  288. {
  289. return $this->col_info[$col_offset]->{$info_type};
  290. }
  291. }
  292. }
  293. /**********************************************************************
  294. * Dumps the contents of any input variable to screen in a nicely
  295. * formatted and easy to understand way - any type: Object, Var or Array
  296. */
  297. // === TJH === This is hacked to OPTIONALLY generate a "$return_var"
  298. // === TJH === must also set $db->debug_echo = false; in your script to override default behaviour
  299. // === TJH === instead of a simple "echo" to the current screen (DEFAULT)
  300. // === TJH === USAGE: $ezdebug = print_r($db->vardump($result),true);
  301. function vardump($mixed='')
  302. {
  303. $return_var .= "<p><table><tr><td bgcolor=ffffff><blockquote><font color=000090>";
  304. $return_var .= "<pre><font face=arial>";
  305. if ( ! $this->vardump_called )
  306. {
  307. $return_var .= "<font color=800080><b>ezSQL</b> (v".EZSQL_VERSION.") <b>Variable Dump..</b></font>\n\n";
  308. }
  309. $var_type = gettype ($mixed);
  310. $return_var .= print_r(($mixed?$mixed:"<font color=red>No Value / False</font>"),true);
  311. $return_var .= "\n\n<b>Type:</b> " . ucfirst($var_type) . "\n";
  312. $return_var .= "<b>Last Query</b> [$this->num_queries]<b>:</b> ".($this->last_query?$this->last_query:"NULL")."\n";
  313. $return_var .= "<b>Last Function Call:</b> " . ($this->func_call?$this->func_call:"None")."\n";
  314. $return_var .= "<b>Last Rows Returned:</b> ".count($this->last_result)."\n";
  315. $return_var .= "</font></pre></font></blockquote></td></tr></table>".$this->donation();
  316. $return_var .= "\n<hr size=1 noshade color=dddddd>";
  317. $this->vardump_called = true;
  318. if($this->debug_echo){
  319. echo $return_var;
  320. }
  321. return $return_var;
  322. }
  323. /**********************************************************************
  324. * Alias for the above function
  325. */
  326. function dumpvar($mixed)
  327. {
  328. $this->vardump($mixed);
  329. }
  330. /**********************************************************************
  331. * Displays the last query string that was sent to the database & a
  332. * table listing results (if there were any).
  333. * (abstracted into a seperate file to save server overhead).
  334. */
  335. // === TJH === The debug() function is now hacked to OPTIOANLLY create a return result
  336. // === TJH === that can be called as a variable, just changed all "echo"s to "$this->console .= "
  337. // === TJH === this is accessed with "$db->console" obviously
  338. // === TJH === n.b. you must also set $db->debug_echo = false; to override default behaviour
  339. function debug($debug_echo) // === TJH === set a default for function to be able to switch "echo" on/off
  340. {
  341. //$this->console .= "<blockquote>"; // === TJH == commented out to change output formatting slightly
  342. // Only show ezSQL credits once..
  343. if ( ! $this->debug_called )
  344. {
  345. $this->console .= "<font color=800080 face=arial size=2><b>ezSQL</b> (v".EZSQL_VERSION.") <b>Debug..</b></font><p>\n";
  346. }
  347. if ( $this->last_error )
  348. {
  349. $this->console .= "<font face=arial size=2 color=000099><b>Last Error --</b> [<font color=000000><b>$this->last_error</b></font>]<p>";
  350. }
  351. if ( $this->from_disk_cache )
  352. {
  353. $this->console .= "<font face=arial size=2 color=000099><b>Results retrieved from disk cache</b></font><p>";
  354. }
  355. $this->console .= "<font face=arial size=2 color=000099><b>Query</b> [$this->num_queries] <b>--</b> ";
  356. $this->console .= "[<font color=000000><b>$this->last_query</b></font>]</font><p>";
  357. $this->console .= "<font face=arial size=2 color=000099><b>Query Result..</b></font>";
  358. $this->console .= "<blockquote>";
  359. if ( $this->col_info )
  360. {
  361. // =====================================================
  362. // Results top rows
  363. $this->console .= "<table cellpadding=5 cellspacing=1 bgcolor=555555>";
  364. $this->console .= "<tr bgcolor=eeeeee><td nowrap valign=bottom><font color=555599 face=arial size=2><b>(row)</b></font></td>";
  365. for ( $i=0; $i < count($this->col_info); $i++ )
  366. {
  367. $this->console .= "<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>";
  368. }
  369. $this->console .= "</tr>";
  370. // ======================================================
  371. // print main results
  372. if ( $this->last_result )
  373. {
  374. $i=0;
  375. foreach ( $this->get_results(null,ARRAY_N) as $one_row )
  376. {
  377. $i++;
  378. $this->console .= "<tr bgcolor=ffffff><td bgcolor=eeeeee nowrap align=middle><font size=2 color=555599 face=arial>$i</font></td>";
  379. foreach ( $one_row as $item )
  380. {
  381. $this->console .= "<td nowrap><font face=arial size=2>$item</font></td>";
  382. }
  383. $this->console .= "</tr>";
  384. }
  385. } // if last result
  386. else
  387. {
  388. $this->console .= "<tr bgcolor=ffffff><td colspan=".(count($this->col_info)+1)."><font face=arial size=2>No Results</font></td></tr>";
  389. }
  390. $this->console .= "</table>";
  391. } // if col_info
  392. else
  393. {
  394. $this->console .= "<font face=arial size=2>No Results</font>";
  395. }
  396. $this->console .= "</blockquote></blockquote>";
  397. $this->console .= $this->donation();
  398. $this->console .= "<hr noshade color=dddddd size=1>";
  399. // == TJH == more -- to try to make backward compatible with a default param that defaults to echo
  400. if($this->debug_echo){
  401. echo $this->console;
  402. }
  403. $this->debug_called = true;
  404. //echo "Something tested"; // == TJH == just some breadcrumbs for testing
  405. }
  406. /**********************************************************************
  407. * Naughty little function to ask for some remuniration!
  408. */
  409. function donation()
  410. {
  411. 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>";
  412. }
  413. }
  414. ?>