PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/unsupported/lib/ez_sql_cubrid.php

http://github.com/jv2222/ezSQL
PHP | 262 lines | 149 code | 53 blank | 60 comment | 20 complexity | 2aa01b201c8775f91cd43ed8249c837b MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. global $ezSQL_cubrid_str;
  3. $ezSQL_cubrid_str = array
  4. (
  5. 1 => 'Require $dbuser and $dbname to connect to a database server',
  6. 2 => 'Error establishing CUBRID database connection. Correct user/password? Correct hostname? Correct database name and port ? Database server running?'
  7. );
  8. /**********************************************************************
  9. * ezSQL Database specific class - CUBRID
  10. */
  11. if ( ! function_exists ('cubrid_connect') ) die('<b>Fatal Error:</b> ezSQL_cubrid requires CUBRID PHP Driver to be compiled and or linked in to the PHP engine');
  12. if ( ! class_exists ('ezSQLcore') ) die('<b>Fatal Error:</b> ezSQL_cubrid requires ezSQLcore (ez_sql_core.php) to be included/loaded before it can be used');
  13. class ezSQL_cubrid extends ezSQLcore
  14. {
  15. var $dbuser = false;
  16. var $dbpassword = false;
  17. var $dbname = false;
  18. var $dbhost = false;
  19. var $dbport = false;
  20. var $rows_affected = false;
  21. /**********************************************************************
  22. * Constructor - allow the user to perform a quick connect at the
  23. * same time as initializing the ezSQL_cubrid class
  24. */
  25. function __construct($dbuser='', $dbpassword='', $dbname='', $dbhost='localhost', $dbport=33000)
  26. {
  27. $this->dbuser = $dbuser;
  28. $this->dbpassword = $dbpassword;
  29. $this->dbname = $dbname;
  30. $this->dbhost = $dbhost;
  31. $this->dbport = $dbport;
  32. global $_ezCubrid;
  33. $_ezCubrid = $this;
  34. }
  35. /**********************************************************************
  36. * In the case of CUBRID quick_connect is not really needed
  37. * because std. connect already does what quick connect does -
  38. * but for the sake of consistency it has been included
  39. */
  40. function quick_connect($dbuser='', $dbpassword='', $dbname='', $dbhost='localhost', $dbport=33000)
  41. {
  42. return $this->connect($dbuser='', $dbpassword='', $dbname='', $dbhost='localhost', $dbport=33000);
  43. }
  44. /**********************************************************************
  45. * Try to connect to CUBRID database server
  46. */
  47. function connect($dbuser='', $dbpassword='', $dbname='', $dbhost='localhost', $dbport=33000)
  48. {
  49. global $ezSQL_cubrid_str; $return_val = false;
  50. $this->_connected = false;
  51. // Keep track of how long the DB takes to connect
  52. $this->timer_start('db_connect_time');
  53. // Must have a user and a password
  54. if ( ! $dbuser || ! $dbname )
  55. {
  56. $this->register_error($ezSQL_cubrid_str[1].' in '.__FILE__.' on line '.__LINE__);
  57. $this->show_errors ? trigger_error($ezSQL_cubrid_str[1],E_USER_WARNING) : null;
  58. }
  59. // Try to establish the server database handle
  60. else if ( ! $this->dbh = @cubrid_connect($dbhost,$dbport,$dbname,$dbuser,$dbpassword) )
  61. {
  62. $this->register_error($ezSQL_cubrid_str[2].' in '.__FILE__.' on line '.__LINE__);
  63. $this->show_errors ? trigger_error($ezSQL_cubrid_str[2],E_USER_WARNING) : null;
  64. }
  65. else
  66. {
  67. $this->dbuser = $dbuser;
  68. $this->dbpassword = $dbpassword;
  69. $this->dbhost = $dbhost;
  70. $this->dbname = $dbname;
  71. $this->dbport = $dbport;
  72. $return_val = true;
  73. $this->_connected = true;
  74. $this->conn_queries = 0;
  75. }
  76. return $return_val;
  77. }
  78. /**********************************************************************
  79. * Format a CUBRID string correctly for safe CUBRID insert
  80. * (no mater if magic quotes are on or not)
  81. */
  82. function escape($str)
  83. {
  84. // If there is no existing database connection then try to connect
  85. if ( ! isset($this->dbh) || ! $this->dbh )
  86. {
  87. $this->connect($this->dbuser, $this->dbpassword, $this->dbhost);
  88. }
  89. return cubrid_real_escape_string(stripslashes($str));
  90. }
  91. /**********************************************************************
  92. * Return CUBRID specific system date syntax
  93. * i.e. Oracle: SYSDATE Mysql/CUBRID: NOW()
  94. */
  95. function sysdate()
  96. {
  97. return 'NOW()';
  98. }
  99. /**********************************************************************
  100. * Perform CUBRID query and try to determine result value
  101. */
  102. function query($query)
  103. {
  104. // This keeps the connection alive for very long running scripts
  105. if ( $this->count(false) >= 500 )
  106. {
  107. $this->disconnect();
  108. $this->connect($this->dbuser,$this->dbpassword,$this->dbname,$this->dbhost,$this->dbport);
  109. }
  110. // Initialise return
  111. $return_val = 0;
  112. // Flush cached values..
  113. $this->flush();
  114. // For reg expressions
  115. $query = trim($query);
  116. // Log how the function was called
  117. $this->func_call = "\$db->query(\"$query\")";
  118. // Keep track of the last query for debug..
  119. $this->last_query = $query;
  120. // Count how many queries there have been
  121. $this->count(true, true);
  122. // Start timer
  123. $this->timer_start($this->num_queries);
  124. // Use core file cache function
  125. if ( $cache = $this->get_cache($query) )
  126. {
  127. // Keep tack of how long all queries have taken
  128. $this->timer_update_global($this->num_queries);
  129. // Trace all queries
  130. if ( $this->use_trace_log )
  131. {
  132. $this->trace_log[] = $this->debug(false);
  133. }
  134. return $cache;
  135. }
  136. // If there is no existing database connection then try to connect
  137. if ( ! isset($this->dbh) || ! $this->dbh )
  138. {
  139. $this->connect($this->dbuser, $this->dbpassword, $this->dbname, $this->dbhost, $this->dbport);
  140. }
  141. // Perform the query via std cubrid_query function..
  142. $this->result = @cubrid_query($query,$this->dbh);
  143. // If there is an error then take note of it..
  144. if ( $str = @cubrid_error($this->dbh) )
  145. {
  146. $this->register_error($str);
  147. $this->show_errors ? trigger_error($str,E_USER_WARNING) : null;
  148. return false;
  149. }
  150. // Query was an insert, delete, update, replace
  151. if ( preg_match("/^(insert|delete|update|replace|truncate|drop|create|alter)\s+/i",$query) )
  152. {
  153. $is_insert = true;
  154. $this->rows_affected = @cubrid_affected_rows($this->dbh);
  155. // Take note of the insert_id
  156. if ( preg_match("/^(insert|replace)\s+/i",$query) )
  157. {
  158. $this->insert_id = @cubrid_insert_id($this->dbh);
  159. }
  160. // Return number fo rows affected
  161. $return_val = $this->rows_affected;
  162. }
  163. // Query was a select
  164. else
  165. {
  166. $is_insert = false;
  167. // Take note of column info
  168. $i=0;
  169. while ($i < @cubrid_num_fields($this->result))
  170. {
  171. $this->col_info[$i] = @cubrid_fetch_field($this->result);
  172. $i++;
  173. }
  174. // Store Query Results
  175. $num_rows=0;
  176. while ( $row = @cubrid_fetch_object($this->result) )
  177. {
  178. // Store relults as an objects within main array
  179. $this->last_result[$num_rows] = $row;
  180. $num_rows++;
  181. }
  182. @cubrid_free_result($this->result);
  183. // Log number of rows the query returned
  184. $this->num_rows = $num_rows;
  185. // Return number of rows selected
  186. $return_val = $this->num_rows;
  187. }
  188. // disk caching of queries
  189. $this->store_cache($query,$is_insert);
  190. // If debug ALL queries
  191. $this->trace || $this->debug_all ? $this->debug() : null ;
  192. // Keep tack of how long all queries have taken
  193. $this->timer_update_global($this->num_queries);
  194. // Trace all queries
  195. if ( $this->use_trace_log )
  196. {
  197. $this->trace_log[] = $this->debug(false);
  198. }
  199. return $return_val;
  200. }
  201. /**********************************************************************
  202. * Close the active CUBRID connection
  203. */
  204. function disconnect()
  205. {
  206. $this->conn_queries = 0;
  207. @cubrid_close($this->dbh);
  208. }
  209. }