PageRenderTime 34ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/system/database/drivers/mysql/mysql_driver.php

https://gitlab.com/Japang-Jawara/jawara-penilaian
PHP | 516 lines | 197 code | 73 blank | 246 comment | 22 complexity | c9a4d46e0cb41674d9f4e2ea28b3d304 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 - 2019, 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. (https://ellislab.com/)
  32. * @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
  33. * @license https://opensource.org/licenses/MIT MIT License
  34. * @link https://codeigniter.com
  35. * @since Version 1.0.0
  36. * @filesource
  37. */
  38. defined('BASEPATH') OR exit('No direct script access allowed');
  39. /**
  40. * MySQL 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 https://codeigniter.com/user_guide/database/
  51. */
  52. class CI_DB_mysql_driver extends CI_DB {
  53. /**
  54. * Database driver
  55. *
  56. * @var string
  57. */
  58. public $dbdriver = 'mysql';
  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;
  83. // --------------------------------------------------------------------
  84. /**
  85. * Identifier escape character
  86. *
  87. * @var string
  88. */
  89. protected $_escape_char = '`';
  90. // --------------------------------------------------------------------
  91. /**
  92. * Class constructor
  93. *
  94. * @param array $params
  95. * @return void
  96. */
  97. public function __construct($params)
  98. {
  99. parent::__construct($params);
  100. if ( ! empty($this->port))
  101. {
  102. $this->hostname .= ':'.$this->port;
  103. }
  104. }
  105. // --------------------------------------------------------------------
  106. /**
  107. * Non-persistent database connection
  108. *
  109. * @param bool $persistent
  110. * @return resource
  111. */
  112. public function db_connect($persistent = FALSE)
  113. {
  114. $client_flags = ($this->compress === FALSE) ? 0 : MYSQL_CLIENT_COMPRESS;
  115. if ($this->encrypt === TRUE)
  116. {
  117. $client_flags = $client_flags | MYSQL_CLIENT_SSL;
  118. }
  119. // Error suppression is necessary mostly due to PHP 5.5+ issuing E_DEPRECATED messages
  120. $this->conn_id = ($persistent === TRUE)
  121. ? mysql_pconnect($this->hostname, $this->username, $this->password, $client_flags)
  122. : mysql_connect($this->hostname, $this->username, $this->password, TRUE, $client_flags);
  123. // ----------------------------------------------------------------
  124. // Select the DB... assuming a database name is specified in the config file
  125. if ($this->database !== '' && ! $this->db_select())
  126. {
  127. log_message('error', 'Unable to select database: '.$this->database);
  128. return ($this->db_debug === TRUE)
  129. ? $this->display_error('db_unable_to_select', $this->database)
  130. : FALSE;
  131. }
  132. if (isset($this->stricton) && is_resource($this->conn_id))
  133. {
  134. if ($this->stricton)
  135. {
  136. $this->simple_query('SET SESSION sql_mode = CONCAT(@@sql_mode, ",", "STRICT_ALL_TABLES")');
  137. }
  138. else
  139. {
  140. $this->simple_query(
  141. 'SET SESSION sql_mode =
  142. REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
  143. @@sql_mode,
  144. "STRICT_ALL_TABLES,", ""),
  145. ",STRICT_ALL_TABLES", ""),
  146. "STRICT_ALL_TABLES", ""),
  147. "STRICT_TRANS_TABLES,", ""),
  148. ",STRICT_TRANS_TABLES", ""),
  149. "STRICT_TRANS_TABLES", "")'
  150. );
  151. }
  152. }
  153. return $this->conn_id;
  154. }
  155. // --------------------------------------------------------------------
  156. /**
  157. * Reconnect
  158. *
  159. * Keep / reestablish the db connection if no queries have been
  160. * sent for a length of time exceeding the server's idle timeout
  161. *
  162. * @return void
  163. */
  164. public function reconnect()
  165. {
  166. if (mysql_ping($this->conn_id) === FALSE)
  167. {
  168. $this->conn_id = FALSE;
  169. }
  170. }
  171. // --------------------------------------------------------------------
  172. /**
  173. * Select the database
  174. *
  175. * @param string $database
  176. * @return bool
  177. */
  178. public function db_select($database = '')
  179. {
  180. if ($database === '')
  181. {
  182. $database = $this->database;
  183. }
  184. if (mysql_select_db($database, $this->conn_id))
  185. {
  186. $this->database = $database;
  187. $this->data_cache = array();
  188. return TRUE;
  189. }
  190. return FALSE;
  191. }
  192. // --------------------------------------------------------------------
  193. /**
  194. * Set client character set
  195. *
  196. * @param string $charset
  197. * @return bool
  198. */
  199. protected function _db_set_charset($charset)
  200. {
  201. return mysql_set_charset($charset, $this->conn_id);
  202. }
  203. // --------------------------------------------------------------------
  204. /**
  205. * Database version number
  206. *
  207. * @return string
  208. */
  209. public function version()
  210. {
  211. if (isset($this->data_cache['version']))
  212. {
  213. return $this->data_cache['version'];
  214. }
  215. if ( ! $this->conn_id OR ($version = mysql_get_server_info($this->conn_id)) === FALSE)
  216. {
  217. return FALSE;
  218. }
  219. return $this->data_cache['version'] = $version;
  220. }
  221. // --------------------------------------------------------------------
  222. /**
  223. * Execute the query
  224. *
  225. * @param string $sql an SQL query
  226. * @return mixed
  227. */
  228. protected function _execute($sql)
  229. {
  230. return mysql_query($this->_prep_query($sql), $this->conn_id);
  231. }
  232. // --------------------------------------------------------------------
  233. /**
  234. * Prep the query
  235. *
  236. * If needed, each database adapter can prep the query string
  237. *
  238. * @param string $sql an SQL query
  239. * @return string
  240. */
  241. protected function _prep_query($sql)
  242. {
  243. // mysql_affected_rows() returns 0 for "DELETE FROM TABLE" queries. This hack
  244. // modifies the query so that it a proper number of affected rows is returned.
  245. if ($this->delete_hack === TRUE && preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
  246. {
  247. return trim($sql).' WHERE 1=1';
  248. }
  249. return $sql;
  250. }
  251. // --------------------------------------------------------------------
  252. /**
  253. * Begin Transaction
  254. *
  255. * @return bool
  256. */
  257. protected function _trans_begin()
  258. {
  259. $this->simple_query('SET AUTOCOMMIT=0');
  260. return $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
  261. }
  262. // --------------------------------------------------------------------
  263. /**
  264. * Commit Transaction
  265. *
  266. * @return bool
  267. */
  268. protected function _trans_commit()
  269. {
  270. if ($this->simple_query('COMMIT'))
  271. {
  272. $this->simple_query('SET AUTOCOMMIT=1');
  273. return TRUE;
  274. }
  275. return FALSE;
  276. }
  277. // --------------------------------------------------------------------
  278. /**
  279. * Rollback Transaction
  280. *
  281. * @return bool
  282. */
  283. protected function _trans_rollback()
  284. {
  285. if ($this->simple_query('ROLLBACK'))
  286. {
  287. $this->simple_query('SET AUTOCOMMIT=1');
  288. return TRUE;
  289. }
  290. return FALSE;
  291. }
  292. // --------------------------------------------------------------------
  293. /**
  294. * Platform-dependent string escape
  295. *
  296. * @param string
  297. * @return string
  298. */
  299. protected function _escape_str($str)
  300. {
  301. return mysql_real_escape_string($str, $this->conn_id);
  302. }
  303. // --------------------------------------------------------------------
  304. /**
  305. * Affected Rows
  306. *
  307. * @return int
  308. */
  309. public function affected_rows()
  310. {
  311. return mysql_affected_rows($this->conn_id);
  312. }
  313. // --------------------------------------------------------------------
  314. /**
  315. * Insert ID
  316. *
  317. * @return int
  318. */
  319. public function insert_id()
  320. {
  321. return mysql_insert_id($this->conn_id);
  322. }
  323. // --------------------------------------------------------------------
  324. /**
  325. * List table query
  326. *
  327. * Generates a platform-specific query string so that the table names can be fetched
  328. *
  329. * @param bool $prefix_limit
  330. * @return string
  331. */
  332. protected function _list_tables($prefix_limit = FALSE)
  333. {
  334. $sql = 'SHOW TABLES FROM '.$this->_escape_char.$this->database.$this->_escape_char;
  335. if ($prefix_limit !== FALSE && $this->dbprefix !== '')
  336. {
  337. return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
  338. }
  339. return $sql;
  340. }
  341. // --------------------------------------------------------------------
  342. /**
  343. * Show column query
  344. *
  345. * Generates a platform-specific query string so that the column names can be fetched
  346. *
  347. * @param string $table
  348. * @return string
  349. */
  350. protected function _list_columns($table = '')
  351. {
  352. return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
  353. }
  354. // --------------------------------------------------------------------
  355. /**
  356. * Returns an object with field data
  357. *
  358. * @param string $table
  359. * @return array
  360. */
  361. public function field_data($table)
  362. {
  363. if (($query = $this->query('SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE))) === FALSE)
  364. {
  365. return FALSE;
  366. }
  367. $query = $query->result_object();
  368. $retval = array();
  369. for ($i = 0, $c = count($query); $i < $c; $i++)
  370. {
  371. $retval[$i] = new stdClass();
  372. $retval[$i]->name = $query[$i]->Field;
  373. sscanf($query[$i]->Type, '%[a-z](%d)',
  374. $retval[$i]->type,
  375. $retval[$i]->max_length
  376. );
  377. $retval[$i]->default = $query[$i]->Default;
  378. $retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI');
  379. }
  380. return $retval;
  381. }
  382. // --------------------------------------------------------------------
  383. /**
  384. * Error
  385. *
  386. * Returns an array containing code and message of the last
  387. * database error that has occurred.
  388. *
  389. * @return array
  390. */
  391. public function error()
  392. {
  393. return array('code' => mysql_errno($this->conn_id), 'message' => mysql_error($this->conn_id));
  394. }
  395. // --------------------------------------------------------------------
  396. /**
  397. * FROM tables
  398. *
  399. * Groups tables in FROM clauses if needed, so there is no confusion
  400. * about operator precedence.
  401. *
  402. * @return string
  403. */
  404. protected function _from_tables()
  405. {
  406. if ( ! empty($this->qb_join) && count($this->qb_from) > 1)
  407. {
  408. return '('.implode(', ', $this->qb_from).')';
  409. }
  410. return implode(', ', $this->qb_from);
  411. }
  412. // --------------------------------------------------------------------
  413. /**
  414. * Close DB Connection
  415. *
  416. * @return void
  417. */
  418. protected function _close()
  419. {
  420. // Error suppression to avoid annoying E_WARNINGs in cases
  421. // where the connection has already been closed for some reason.
  422. @mysql_close($this->conn_id);
  423. }
  424. // --------------------------------------------------------------------
  425. /**
  426. * Insert_on_duplicate_update_batch statement
  427. *
  428. * Generates a platform-specific insert string from the supplied data
  429. * MODIFIED to include ON DUPLICATE UPDATE
  430. *
  431. * @access public
  432. * @param string the table name
  433. * @param array the insert keys
  434. * @param array the insert values
  435. * @return string
  436. */
  437. function _insert_on_duplicate_update_batch($table, $keys, $values)
  438. {
  439. foreach($keys as $key)
  440. $update_fields[] = $key.'=VALUES('.$key.')';
  441. return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES ".implode(', ', $values)." ON DUPLICATE KEY UPDATE ".implode(', ', $update_fields);
  442. }
  443. }