PageRenderTime 23ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/firestats/lib/ezsql/sqlite/ez_sql_sqlite.php

https://github.com/alx/blogsfera
PHP | 207 lines | 100 code | 45 blank | 62 comment | 13 complexity | 9ae23d1477b06d6f6e49b9044706c8d8 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0
  1. <?php
  2. /**********************************************************************
  3. * Author: Justin Vincent (justin@visunet.ie)
  4. * Web...: http://php.justinvincent.com
  5. * Name..: ezSQL_sqlite
  6. * Desc..: SQLite component (part of ezSQL databse abstraction library)
  7. *
  8. */
  9. /**********************************************************************
  10. * ezSQL error strings - SQLite
  11. */
  12. $ezsql_sqlite_str = array
  13. (
  14. 1 => 'Require $dbpath and $dbname to open an SQLite database'
  15. );
  16. /**********************************************************************
  17. * ezSQL Database specific class - SQLite
  18. */
  19. if ( ! function_exists ('sqlite_open') ) die('<b>Fatal Error:</b> ezSQL_sqlite requires SQLite Lib to be compiled and or linked in to the PHP engine');
  20. if ( ! class_exists ('ezSQLcore') ) die('<b>Fatal Error:</b> ezSQL_sqlite requires ezSQLcore (ez_sql_core.php) to be included/loaded before it can be used');
  21. class ezSQL_sqlite extends ezSQLcore
  22. {
  23. /**********************************************************************
  24. * Constructor - allow the user to perform a qucik connect at the
  25. * same time as initialising the ezSQL_sqlite class
  26. */
  27. function ezSQL_sqlite($dbpath='', $dbname='')
  28. {
  29. // Turn on track errors
  30. ini_set('track_errors',1);
  31. if ( $dbpath && $dbname )
  32. {
  33. $this->connect($dbpath, $dbname);
  34. }
  35. }
  36. /**********************************************************************
  37. * Try to connect to SQLite database server
  38. */
  39. function connect($dbpath='', $dbname='')
  40. {
  41. global $ezsql_sqlite_str; $return_val = false;
  42. // Must have a user and a password
  43. if ( ! $dbpath || ! $dbname )
  44. {
  45. $this->register_error($ezsql_sqlite_str[1].' in '.__FILE__.' on line '.__LINE__);
  46. $this->show_errors ? trigger_error($ezsql_sqlite_str[1],E_USER_WARNING) : null;
  47. }
  48. // Try to establish the server database handle
  49. else if ( ! $this->dbh = @sqlite_open($dbpath.$dbname) )
  50. {
  51. $this->register_error($php_errormsg);
  52. $this->show_errors ? trigger_error($php_errormsg,E_USER_WARNING) : null;
  53. }
  54. else
  55. $return_val = true;
  56. return $return_val;
  57. }
  58. /**********************************************************************
  59. * In the case of SQLite quick_connect is not really needed
  60. * because std. connect already does what quick connect does -
  61. * but for the sake of consistency it has been included
  62. */
  63. function quick_connect($dbpath='', $dbname='')
  64. {
  65. return $this->connect($dbpath, $dbname);
  66. }
  67. /**********************************************************************
  68. * No real equivalent of mySQL select in SQLite
  69. * once again, function included for the sake of consistency
  70. */
  71. function select($dbpath='', $dbname='')
  72. {
  73. return $this->connect($dbpath, $dbname);
  74. }
  75. /**********************************************************************
  76. * Format a SQLite string correctly for safe SQLite insert
  77. * (no mater if magic quotes are on or not)
  78. */
  79. function escape($str)
  80. {
  81. return sqlite_escape_string(stripslashes(preg_replace("/[\r\n]/",'',$str)));
  82. }
  83. /**********************************************************************
  84. * Return SQLite specific system date syntax
  85. * i.e. Oracle: SYSDATE Mysql: NOW()
  86. */
  87. function sysdate()
  88. {
  89. return 'now';
  90. }
  91. /**********************************************************************
  92. * Perform SQLite query and try to detirmin result value
  93. */
  94. // ==================================================================
  95. // Basic Query - see docs for more detail
  96. function query($query)
  97. {
  98. // For reg expressions
  99. $query = str_replace("/[\n\r]/",'',trim($query));
  100. // initialise return
  101. $return_val = 0;
  102. // Flush cached values..
  103. $this->flush();
  104. // Log how the function was called
  105. $this->func_call = "\$db->query(\"$query\")";
  106. // Keep track of the last query for debug..
  107. $this->last_query = $query;
  108. // Perform the query via std mysql_query function..
  109. $this->result = @sqlite_query($this->dbh,$query);
  110. $this->num_queries++;
  111. // If there is an error then take note of it..
  112. if (@sqlite_last_error($this->dbh))
  113. {
  114. $err_str = sqlite_error_string (sqlite_last_error($this->dbh));
  115. $this->register_error($err_str);
  116. $this->show_errors ? trigger_error($err_str,E_USER_WARNING) : null;
  117. return false;
  118. }
  119. // Query was an insert, delete, update, replace
  120. if ( preg_match("/^(insert|delete|update|replace)\s+/i",$query) )
  121. {
  122. $this->rows_affected = @sqlite_changes($this->dbh);
  123. // Take note of the insert_id
  124. if ( preg_match("/^(insert|replace)\s+/i",$query) )
  125. {
  126. $this->insert_id = @sqlite_last_insert_rowid($this->dbh);
  127. }
  128. // Return number fo rows affected
  129. $return_val = $this->rows_affected;
  130. }
  131. // Query was an select
  132. else
  133. {
  134. // Take note of column info
  135. $i=0;
  136. while ($i < @sqlite_num_fields($this->result))
  137. {
  138. $this->col_info[$i]->name = sqlite_field_name ( $this->result, $i);
  139. $this->col_info[$i]->type = null;
  140. $this->col_info[$i]->max_length = null;
  141. $i++;
  142. }
  143. // Store Query Results
  144. $num_rows=0;
  145. while ($row = @sqlite_fetch_array($this->result,SQLITE_ASSOC) )
  146. {
  147. // Store relults as an objects within main array
  148. $obj= (object) $row; //convert to object
  149. $this->last_result[$num_rows] = $obj;
  150. $num_rows++;
  151. }
  152. // Log number of rows the query returned
  153. $this->num_rows = $num_rows;
  154. // Return number of rows selected
  155. $return_val = $this->num_rows;
  156. }
  157. // If debug ALL queries
  158. $this->trace||$this->debug_all ? $this->debug() : null ;
  159. return $return_val;
  160. }
  161. }
  162. ?>