PageRenderTime 121ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Modifications Since 08/www/events/system/database/drivers/mysqli/mysqli_driver.php

https://github.com/holsinger/openfloor
PHP | 478 lines | 160 code | 69 blank | 249 comment | 15 complexity | 99847d2845906176af6355e6e80893d4 MD5 | raw file
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 4.3.2 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author Rick Ellis
  9. * @copyright Copyright (c) 2006, EllisLab, Inc.
  10. * @license http://www.codeignitor.com/user_guide/license.html
  11. * @link http://www.codeigniter.com
  12. * @since Version 1.0
  13. * @filesource
  14. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * MySQLi Database Adapter Class - MySQLi only works with PHP 5
  18. *
  19. * Note: _DB is an extender class that the app controller
  20. * creates dynamically based on whether the active record
  21. * class is being used or not.
  22. *
  23. * @package CodeIgniter
  24. * @subpackage Drivers
  25. * @category Database
  26. * @author Rick Ellis
  27. * @link http://www.codeigniter.com/user_guide/database/
  28. */
  29. class CI_DB_mysqli_driver extends CI_DB {
  30. /**
  31. * Whether to use the MySQL "delete hack" which allows the number
  32. * of affected rows to be shown. Uses a preg_replace when enabled,
  33. * adding a bit more processing to all queries.
  34. */
  35. var $delete_hack = TRUE;
  36. // --------------------------------------------------------------------
  37. /**
  38. * Non-persistent database connection
  39. *
  40. * @access private called by the base class
  41. * @return resource
  42. */
  43. function db_connect()
  44. {
  45. return @mysqli_connect($this->hostname, $this->username, $this->password);
  46. }
  47. // --------------------------------------------------------------------
  48. /**
  49. * Persistent database connection
  50. *
  51. * @access private called by the base class
  52. * @return resource
  53. */
  54. function db_pconnect()
  55. {
  56. return $this->db_connect();
  57. }
  58. // --------------------------------------------------------------------
  59. /**
  60. * Select the database
  61. *
  62. * @access private called by the base class
  63. * @return resource
  64. */
  65. function db_select()
  66. {
  67. return @mysqli_select_db($this->conn_id, $this->database);
  68. }
  69. // --------------------------------------------------------------------
  70. /**
  71. * Version number query string
  72. *
  73. * @access public
  74. * @return string
  75. */
  76. function _version()
  77. {
  78. return "SELECT version() AS ver";
  79. }
  80. // --------------------------------------------------------------------
  81. /**
  82. * Execute the query
  83. *
  84. * @access private called by the base class
  85. * @param string an SQL query
  86. * @return resource
  87. */
  88. function _execute($sql)
  89. {
  90. $sql = $this->_prep_query($sql);
  91. $result = @mysqli_query($this->conn_id, $sql);
  92. return $result;
  93. }
  94. // --------------------------------------------------------------------
  95. /**
  96. * Prep the query
  97. *
  98. * If needed, each database adapter can prep the query string
  99. *
  100. * @access private called by execute()
  101. * @param string an SQL query
  102. * @return string
  103. */
  104. function _prep_query($sql)
  105. {
  106. // "DELETE FROM TABLE" returns 0 affected rows This hack modifies
  107. // the query so that it returns the number of affected rows
  108. if ($this->delete_hack === TRUE)
  109. {
  110. if (preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
  111. {
  112. $sql = preg_replace("/^\s*DELETE\s+FROM\s+(\S+)\s*$/", "DELETE FROM \\1 WHERE 1=1", $sql);
  113. }
  114. }
  115. return $sql;
  116. }
  117. // --------------------------------------------------------------------
  118. /**
  119. * Begin Transaction
  120. *
  121. * @access public
  122. * @return bool
  123. */
  124. function trans_begin($test_mode = FALSE)
  125. {
  126. if ( ! $this->trans_enabled)
  127. {
  128. return TRUE;
  129. }
  130. // When transactions are nested we only begin/commit/rollback the outermost ones
  131. if ($this->_trans_depth > 0)
  132. {
  133. return TRUE;
  134. }
  135. // Reset the transaction failure flag.
  136. // If the $test_mode flag is set to TRUE transactions will be rolled back
  137. // even if the queries produce a successful result.
  138. $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
  139. $this->simple_query('SET AUTOCOMMIT=0');
  140. $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
  141. return TRUE;
  142. }
  143. // --------------------------------------------------------------------
  144. /**
  145. * Commit Transaction
  146. *
  147. * @access public
  148. * @return bool
  149. */
  150. function trans_commit()
  151. {
  152. if ( ! $this->trans_enabled)
  153. {
  154. return TRUE;
  155. }
  156. // When transactions are nested we only begin/commit/rollback the outermost ones
  157. if ($this->_trans_depth > 0)
  158. {
  159. return TRUE;
  160. }
  161. $this->simple_query('COMMIT');
  162. $this->simple_query('SET AUTOCOMMIT=1');
  163. return TRUE;
  164. }
  165. // --------------------------------------------------------------------
  166. /**
  167. * Rollback Transaction
  168. *
  169. * @access public
  170. * @return bool
  171. */
  172. function trans_rollback()
  173. {
  174. if ( ! $this->trans_enabled)
  175. {
  176. return TRUE;
  177. }
  178. // When transactions are nested we only begin/commit/rollback the outermost ones
  179. if ($this->_trans_depth > 0)
  180. {
  181. return TRUE;
  182. }
  183. $this->simple_query('ROLLBACK');
  184. $this->simple_query('SET AUTOCOMMIT=1');
  185. return TRUE;
  186. }
  187. // --------------------------------------------------------------------
  188. /**
  189. * Escape String
  190. *
  191. * @access public
  192. * @param string
  193. * @return string
  194. */
  195. function escape_str($str)
  196. {
  197. return mysqli_real_escape_string($this->conn_id, $str);
  198. }
  199. // --------------------------------------------------------------------
  200. /**
  201. * Affected Rows
  202. *
  203. * @access public
  204. * @return integer
  205. */
  206. function affected_rows()
  207. {
  208. return @mysqli_affected_rows($this->conn_id);
  209. }
  210. // --------------------------------------------------------------------
  211. /**
  212. * Insert ID
  213. *
  214. * @access public
  215. * @return integer
  216. */
  217. function insert_id()
  218. {
  219. return @mysqli_insert_id($this->conn_id);
  220. }
  221. // --------------------------------------------------------------------
  222. /**
  223. * "Count All" query
  224. *
  225. * Generates a platform-specific query string that counts all records in
  226. * the specified database
  227. *
  228. * @access public
  229. * @param string
  230. * @return string
  231. */
  232. function count_all($table = '')
  233. {
  234. if ($table == '')
  235. return '0';
  236. $query = $this->query("SELECT COUNT(*) AS numrows FROM `".$this->dbprefix.$table."`");
  237. if ($query->num_rows() == 0)
  238. return '0';
  239. $row = $query->row();
  240. return $row->numrows;
  241. }
  242. // --------------------------------------------------------------------
  243. /**
  244. * List table query
  245. *
  246. * Generates a platform-specific query string so that the table names can be fetched
  247. *
  248. * @access private
  249. * @return string
  250. */
  251. function _list_tables()
  252. {
  253. return "SHOW TABLES FROM `".$this->database."`";
  254. }
  255. // --------------------------------------------------------------------
  256. /**
  257. * Show column query
  258. *
  259. * Generates a platform-specific query string so that the column names can be fetched
  260. *
  261. * @access public
  262. * @param string the table name
  263. * @return string
  264. */
  265. function _list_columns($table = '')
  266. {
  267. return "SHOW COLUMNS FROM ".$this->_escape_table($table);
  268. }
  269. // --------------------------------------------------------------------
  270. /**
  271. * Field data query
  272. *
  273. * Generates a platform-specific query so that the column data can be retrieved
  274. *
  275. * @access public
  276. * @param string the table name
  277. * @return object
  278. */
  279. function _field_data($table)
  280. {
  281. return "SELECT * FROM ".$this->_escape_table($table)." LIMIT 1";
  282. }
  283. // --------------------------------------------------------------------
  284. /**
  285. * The error message string
  286. *
  287. * @access private
  288. * @return string
  289. */
  290. function _error_message()
  291. {
  292. return mysqli_error($this->conn_id);
  293. }
  294. // --------------------------------------------------------------------
  295. /**
  296. * The error message number
  297. *
  298. * @access private
  299. * @return integer
  300. */
  301. function _error_number()
  302. {
  303. return mysqli_errno($this->conn_id);
  304. }
  305. // --------------------------------------------------------------------
  306. /**
  307. * Escape Table Name
  308. *
  309. * This function adds backticks if the table name has a period
  310. * in it. Some DBs will get cranky unless periods are escaped
  311. *
  312. * @access private
  313. * @param string the table name
  314. * @return string
  315. */
  316. function _escape_table($table)
  317. {
  318. if (stristr($table, '.'))
  319. {
  320. $table = preg_replace("/\./", "`.`", $table);
  321. }
  322. return $table;
  323. }
  324. // --------------------------------------------------------------------
  325. /**
  326. * Insert statement
  327. *
  328. * Generates a platform-specific insert string from the supplied data
  329. *
  330. * @access public
  331. * @param string the table name
  332. * @param array the insert keys
  333. * @param array the insert values
  334. * @return string
  335. */
  336. function _insert($table, $keys, $values)
  337. {
  338. return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
  339. }
  340. // --------------------------------------------------------------------
  341. /**
  342. * Update statement
  343. *
  344. * Generates a platform-specific update string from the supplied data
  345. *
  346. * @access public
  347. * @param string the table name
  348. * @param array the update data
  349. * @param array the where clause
  350. * @return string
  351. */
  352. function _update($table, $values, $where)
  353. {
  354. foreach($values as $key => $val)
  355. {
  356. $valstr[] = $key." = ".$val;
  357. }
  358. return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
  359. }
  360. // --------------------------------------------------------------------
  361. /**
  362. * Delete statement
  363. *
  364. * Generates a platform-specific delete string from the supplied data
  365. *
  366. * @access public
  367. * @param string the table name
  368. * @param array the where clause
  369. * @return string
  370. */
  371. function _delete($table, $where)
  372. {
  373. return "DELETE FROM ".$this->_escape_table($table)." WHERE ".implode(" ", $where);
  374. }
  375. // --------------------------------------------------------------------
  376. /**
  377. * Limit string
  378. *
  379. * Generates a platform-specific LIMIT clause
  380. *
  381. * @access public
  382. * @param string the sql query string
  383. * @param integer the number of rows to limit the query to
  384. * @param integer the offset value
  385. * @return string
  386. */
  387. function _limit($sql, $limit, $offset)
  388. {
  389. $sql .= "LIMIT ".$limit;
  390. if ($offset > 0)
  391. {
  392. $sql .= " OFFSET ".$offset;
  393. }
  394. return $sql;
  395. }
  396. // --------------------------------------------------------------------
  397. /**
  398. * Close DB Connection
  399. *
  400. * @access public
  401. * @param resource
  402. * @return void
  403. */
  404. function _close($conn_id)
  405. {
  406. @mysqli_close($conn_id);
  407. }
  408. }
  409. ?>