PageRenderTime 95ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/system/database/drivers/mysqli/mysqli_driver.php

https://gitlab.com/relacilia/cakra
PHP | 503 lines | 210 code | 69 blank | 224 comment | 27 complexity | 98221205dae033cb4461722be5323709 MD5 | raw file
  1. <?php
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP
  6. *
  7. * This content is released under the MIT License (MIT)
  8. *
  9. * Copyright (c) 2014 - 2015, British Columbia Institute of Technology
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in
  19. * all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. * THE SOFTWARE.
  28. *
  29. * @package CodeIgniter
  30. * @author EllisLab Dev Team
  31. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
  32. * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
  33. * @license http://opensource.org/licenses/MIT MIT License
  34. * @link http://codeigniter.com
  35. * @since Version 1.3.0
  36. * @filesource
  37. */
  38. defined('BASEPATH') OR exit('No direct script access allowed');
  39. /**
  40. * MySQLi Database Adapter Class
  41. *
  42. * Note: _DB is an extender class that the app controller
  43. * creates dynamically based on whether the query builder
  44. * class is being used or not.
  45. *
  46. * @package CodeIgniter
  47. * @subpackage Drivers
  48. * @category Database
  49. * @author EllisLab Dev Team
  50. * @link http://codeigniter.com/user_guide/database/
  51. */
  52. class CI_DB_mysqli_driver extends CI_DB {
  53. /**
  54. * Database driver
  55. *
  56. * @var string
  57. */
  58. public $dbdriver = 'mysqli';
  59. /**
  60. * Compression flag
  61. *
  62. * @var bool
  63. */
  64. public $compress = FALSE;
  65. /**
  66. * DELETE hack flag
  67. *
  68. * Whether to use the MySQL "delete hack" which allows the number
  69. * of affected rows to be shown. Uses a preg_replace when enabled,
  70. * adding a bit more processing to all queries.
  71. *
  72. * @var bool
  73. */
  74. public $delete_hack = TRUE;
  75. /**
  76. * Strict ON flag
  77. *
  78. * Whether we're running in strict SQL mode.
  79. *
  80. * @var bool
  81. */
  82. public $stricton = FALSE;
  83. // --------------------------------------------------------------------
  84. /**
  85. * Identifier escape character
  86. *
  87. * @var string
  88. */
  89. protected $_escape_char = '`';
  90. // --------------------------------------------------------------------
  91. /**
  92. * Database connection
  93. *
  94. * @param bool $persistent
  95. * @return object
  96. */
  97. public function db_connect($persistent = FALSE)
  98. {
  99. // Do we have a socket path?
  100. if ($this->hostname[0] === '/')
  101. {
  102. $hostname = NULL;
  103. $port = NULL;
  104. $socket = $this->hostname;
  105. }
  106. else
  107. {
  108. // Persistent connection support was added in PHP 5.3.0
  109. $hostname = ($persistent === TRUE && is_php('5.3'))
  110. ? 'p:'.$this->hostname : $this->hostname;
  111. $port = empty($this->port) ? NULL : $this->port;
  112. $socket = NULL;
  113. }
  114. $client_flags = ($this->compress === TRUE) ? MYSQLI_CLIENT_COMPRESS : 0;
  115. $mysqli = mysqli_init();
  116. $mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 10);
  117. if ($this->stricton)
  118. {
  119. $mysqli->options(MYSQLI_INIT_COMMAND, 'SET SESSION sql_mode="STRICT_ALL_TABLES"');
  120. }
  121. if (is_array($this->encrypt))
  122. {
  123. $ssl = array();
  124. empty($this->encrypt['ssl_key']) OR $ssl['key'] = $this->encrypt['ssl_key'];
  125. empty($this->encrypt['ssl_cert']) OR $ssl['cert'] = $this->encrypt['ssl_cert'];
  126. empty($this->encrypt['ssl_ca']) OR $ssl['ca'] = $this->encrypt['ssl_ca'];
  127. empty($this->encrypt['ssl_capath']) OR $ssl['capath'] = $this->encrypt['ssl_capath'];
  128. empty($this->encrypt['ssl_cipher']) OR $ssl['cipher'] = $this->encrypt['ssl_cipher'];
  129. if ( ! empty($ssl))
  130. {
  131. if ( ! empty($this->encrypt['ssl_verify']) && defined('MYSQLI_OPT_SSL_VERIFY_SERVER_CERT'))
  132. {
  133. $mysqli->options(MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, TRUE);
  134. }
  135. $client_flags |= MYSQLI_CLIENT_SSL;
  136. $mysqli->ssl_set(
  137. isset($ssl['key']) ? $ssl['key'] : NULL,
  138. isset($ssl['cert']) ? $ssl['cert'] : NULL,
  139. isset($ssl['ca']) ? $ssl['ca'] : NULL,
  140. isset($ssl['capath']) ? $ssl['capath'] : NULL,
  141. isset($ssl['cipher']) ? $ssl['cipher'] : NULL
  142. );
  143. }
  144. }
  145. if ($mysqli->real_connect($hostname, $this->username, $this->password, $this->database, $port, $socket, $client_flags))
  146. {
  147. // Prior to version 5.7.3, MySQL silently downgrades to an unencrypted connection if SSL setup fails
  148. if (
  149. ($client_flags & MYSQLI_CLIENT_SSL)
  150. && version_compare($mysqli->client_info, '5.7.3', '<=')
  151. && empty($mysqli->query("SHOW STATUS LIKE 'ssl_cipher'")->fetch_object()->Value)
  152. )
  153. {
  154. $mysqli->close();
  155. $message = 'MySQLi was configured for an SSL connection, but got an unencrypted connection instead!';
  156. log_message('error', $message);
  157. return ($this->db->db_debug) ? $this->db->display_error($message, '', TRUE) : FALSE;
  158. }
  159. return $mysqli;
  160. }
  161. return FALSE;
  162. }
  163. // --------------------------------------------------------------------
  164. /**
  165. * Reconnect
  166. *
  167. * Keep / reestablish the db connection if no queries have been
  168. * sent for a length of time exceeding the server's idle timeout
  169. *
  170. * @return void
  171. */
  172. public function reconnect()
  173. {
  174. if ($this->conn_id !== FALSE && $this->conn_id->ping() === FALSE)
  175. {
  176. $this->conn_id = FALSE;
  177. }
  178. }
  179. // --------------------------------------------------------------------
  180. /**
  181. * Select the database
  182. *
  183. * @param string $database
  184. * @return bool
  185. */
  186. public function db_select($database = '')
  187. {
  188. if ($database === '')
  189. {
  190. $database = $this->database;
  191. }
  192. if ($this->conn_id->select_db($database))
  193. {
  194. $this->database = $database;
  195. return TRUE;
  196. }
  197. return FALSE;
  198. }
  199. // --------------------------------------------------------------------
  200. /**
  201. * Set client character set
  202. *
  203. * @param string $charset
  204. * @return bool
  205. */
  206. protected function _db_set_charset($charset)
  207. {
  208. return $this->conn_id->set_charset($charset);
  209. }
  210. // --------------------------------------------------------------------
  211. /**
  212. * Database version number
  213. *
  214. * @return string
  215. */
  216. public function version()
  217. {
  218. if (isset($this->data_cache['version']))
  219. {
  220. return $this->data_cache['version'];
  221. }
  222. return $this->data_cache['version'] = $this->conn_id->server_info;
  223. }
  224. // --------------------------------------------------------------------
  225. /**
  226. * Execute the query
  227. *
  228. * @param string $sql an SQL query
  229. * @return mixed
  230. */
  231. protected function _execute($sql)
  232. {
  233. return $this->conn_id->query($this->_prep_query($sql));
  234. }
  235. // --------------------------------------------------------------------
  236. /**
  237. * Prep the query
  238. *
  239. * If needed, each database adapter can prep the query string
  240. *
  241. * @param string $sql an SQL query
  242. * @return string
  243. */
  244. protected function _prep_query($sql)
  245. {
  246. // mysqli_affected_rows() returns 0 for "DELETE FROM TABLE" queries. This hack
  247. // modifies the query so that it a proper number of affected rows is returned.
  248. if ($this->delete_hack === TRUE && preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
  249. {
  250. return trim($sql).' WHERE 1=1';
  251. }
  252. return $sql;
  253. }
  254. // --------------------------------------------------------------------
  255. /**
  256. * Begin Transaction
  257. *
  258. * @return bool
  259. */
  260. protected function _trans_begin()
  261. {
  262. $this->conn_id->autocommit(FALSE);
  263. return is_php('5.5')
  264. ? $this->conn_id->begin_transaction()
  265. : $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
  266. }
  267. // --------------------------------------------------------------------
  268. /**
  269. * Commit Transaction
  270. *
  271. * @return bool
  272. */
  273. protected function _trans_commit()
  274. {
  275. if ($this->conn_id->commit())
  276. {
  277. $this->conn_id->autocommit(TRUE);
  278. return TRUE;
  279. }
  280. return FALSE;
  281. }
  282. // --------------------------------------------------------------------
  283. /**
  284. * Rollback Transaction
  285. *
  286. * @return bool
  287. */
  288. protected function _trans_rollback()
  289. {
  290. if ($this->conn_id->rollback())
  291. {
  292. $this->conn_id->autocommit(TRUE);
  293. return TRUE;
  294. }
  295. return FALSE;
  296. }
  297. // --------------------------------------------------------------------
  298. /**
  299. * Platform-dependant string escape
  300. *
  301. * @param string
  302. * @return string
  303. */
  304. protected function _escape_str($str)
  305. {
  306. return $this->conn_id->real_escape_string($str);
  307. }
  308. // --------------------------------------------------------------------
  309. /**
  310. * Affected Rows
  311. *
  312. * @return int
  313. */
  314. public function affected_rows()
  315. {
  316. return $this->conn_id->affected_rows;
  317. }
  318. // --------------------------------------------------------------------
  319. /**
  320. * Insert ID
  321. *
  322. * @return int
  323. */
  324. public function insert_id()
  325. {
  326. return $this->conn_id->insert_id;
  327. }
  328. // --------------------------------------------------------------------
  329. /**
  330. * List table query
  331. *
  332. * Generates a platform-specific query string so that the table names can be fetched
  333. *
  334. * @param bool $prefix_limit
  335. * @return string
  336. */
  337. protected function _list_tables($prefix_limit = FALSE)
  338. {
  339. $sql = 'SHOW TABLES FROM '.$this->escape_identifiers($this->database);
  340. if ($prefix_limit !== FALSE && $this->dbprefix !== '')
  341. {
  342. return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
  343. }
  344. return $sql;
  345. }
  346. // --------------------------------------------------------------------
  347. /**
  348. * Show column query
  349. *
  350. * Generates a platform-specific query string so that the column names can be fetched
  351. *
  352. * @param string $table
  353. * @return string
  354. */
  355. protected function _list_columns($table = '')
  356. {
  357. return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
  358. }
  359. // --------------------------------------------------------------------
  360. /**
  361. * Returns an object with field data
  362. *
  363. * @param string $table
  364. * @return array
  365. */
  366. public function field_data($table)
  367. {
  368. if (($query = $this->query('SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE))) === FALSE)
  369. {
  370. return FALSE;
  371. }
  372. $query = $query->result_object();
  373. $retval = array();
  374. for ($i = 0, $c = count($query); $i < $c; $i++)
  375. {
  376. $retval[$i] = new stdClass();
  377. $retval[$i]->name = $query[$i]->Field;
  378. sscanf($query[$i]->Type, '%[a-z](%d)',
  379. $retval[$i]->type,
  380. $retval[$i]->max_length
  381. );
  382. $retval[$i]->default = $query[$i]->Default;
  383. $retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI');
  384. }
  385. return $retval;
  386. }
  387. // --------------------------------------------------------------------
  388. /**
  389. * Error
  390. *
  391. * Returns an array containing code and message of the last
  392. * database error that has occurred.
  393. *
  394. * @return array
  395. */
  396. public function error()
  397. {
  398. if ( ! empty($this->conn_id->connect_errno))
  399. {
  400. return array(
  401. 'code' => $this->conn_id->connect_errno,
  402. 'message' => is_php('5.2.9') ? $this->conn_id->connect_error : mysqli_connect_error()
  403. );
  404. }
  405. return array('code' => $this->conn_id->errno, 'message' => $this->conn_id->error);
  406. }
  407. // --------------------------------------------------------------------
  408. /**
  409. * FROM tables
  410. *
  411. * Groups tables in FROM clauses if needed, so there is no confusion
  412. * about operator precedence.
  413. *
  414. * @return string
  415. */
  416. protected function _from_tables()
  417. {
  418. if ( ! empty($this->qb_join) && count($this->qb_from) > 1)
  419. {
  420. return '('.implode(', ', $this->qb_from).')';
  421. }
  422. return implode(', ', $this->qb_from);
  423. }
  424. // --------------------------------------------------------------------
  425. /**
  426. * Close DB Connection
  427. *
  428. * @return void
  429. */
  430. protected function _close()
  431. {
  432. $this->conn_id->close();
  433. }
  434. }