PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/system/database/drivers/pdo/pdo_driver.php

https://gitlab.com/ablu/invertika-backup-web
PHP | 610 lines | 262 code | 78 blank | 270 comment | 24 complexity | 74f72a6147237810528980bdb1528e9f MD5 | raw file
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * Code Igniter
  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, pMachine, 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. * PDO Database Adapter Class
  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 Dready
  27. * @link http://dready.jexiste.fr/dotclear/
  28. */
  29. class CI_DB_pdo_driver extends CI_DB
  30. {
  31. var $class_version = '0.1';
  32. // this is a hack: missing random keyword...
  33. // this line is not part of the official ci distribution: added 2009-08-27
  34. // by exceptionfault
  35. var $_random_keyword = ' Random()'; // database specific random keyword
  36. /**
  37. * Non-persistent database connection
  38. *
  39. * @access private called by the base class
  40. * @return resource
  41. */
  42. function db_connect()
  43. {
  44. $conn_id = false;
  45. try
  46. {
  47. $conn_id = new PDO ($this->database, $this->username, $this->password);
  48. log_message('debug', 'connecting ' . $this->database);
  49. }
  50. catch (PDOException $e)
  51. {
  52. log_message('error', $e->getMessage());
  53. if ($this->db_debug)
  54. {
  55. $this->display_error($e->getMessage(), '', TRUE);
  56. }
  57. }
  58. log_message('debug', print_r($conn_id, true));
  59. if ($conn_id)
  60. {
  61. log_message('debug', 'connection ok');
  62. }
  63. return $conn_id;
  64. }
  65. /**
  66. * Persistent database connection
  67. *
  68. * @access private, called by the base class
  69. * @return resource
  70. */
  71. function db_pconnect()
  72. {
  73. try
  74. {
  75. $conn_id = new PDO ($this->database, $this->username, $this->password, array(PDO::ATTR_PERSISTENT => true));
  76. }
  77. catch (PDOException $e)
  78. {
  79. log_message('error', $e->getMessage());
  80. if ($this->db_debug)
  81. {
  82. $this->display_error($e->getMessage(), '', TRUE);
  83. }
  84. }
  85. return $conn_id;
  86. }
  87. /**
  88. * Select the database
  89. *
  90. * @access private called by the base class
  91. * @return resource
  92. */
  93. function db_select()
  94. {
  95. return TRUE;
  96. }
  97. /**
  98. * Execute the query
  99. *
  100. * @access private, called by the base class
  101. * @param string an SQL query
  102. * @return resource
  103. */
  104. function _execute($sql)
  105. {
  106. $sql = $this->_prep_query($sql);
  107. log_message('debug', 'SQL : ' . $sql);
  108. return @$this->conn_id->query($sql); // Do we really need to use error supression here? :(
  109. }
  110. /**
  111. * Prep the query
  112. *
  113. * If needed, each database adapter can prep the query string
  114. *
  115. * @access private called by execute()
  116. * @param string an SQL query
  117. * @return string
  118. */
  119. function &_prep_query($sql)
  120. {
  121. return $sql;
  122. }
  123. /**
  124. * "Smart" Escape String
  125. *
  126. * Escapes data based on type
  127. * Sets boolean and null types
  128. *
  129. * @access public
  130. * @param string
  131. * @return integer
  132. */
  133. function escape($str)
  134. {
  135. switch (gettype($str))
  136. {
  137. case 'string':
  138. $str = $this->escape_str($str);
  139. break;
  140. case 'boolean':
  141. $str = ($str === FALSE) ? 0 : 1;
  142. break;
  143. default:
  144. $str = ($str === NULL) ? 'NULL' : $str;
  145. break;
  146. }
  147. return $str;
  148. }
  149. /**
  150. * Escape String
  151. *
  152. * @access public
  153. * @param string
  154. * @return string
  155. */
  156. function escape_str($str)
  157. {
  158. if (get_magic_quotes_gpc())
  159. {
  160. $str = stripslashes($str);
  161. }
  162. return $this->conn_id->quote($str);
  163. }
  164. /**
  165. * Close DB Connection
  166. *
  167. * @access public
  168. * @param resource
  169. * @return void
  170. */
  171. function destroy($conn_id)
  172. {
  173. $conn_id = null;
  174. }
  175. /**
  176. * Insert ID
  177. *
  178. * @access public
  179. * @return integer
  180. */
  181. function insert_id()
  182. {
  183. return @$this->conn_id->lastInsertId();
  184. }
  185. /**
  186. * "Count All" query
  187. *
  188. * Generates a platform-specific query string that counts all records in
  189. * the specified database
  190. *
  191. * @access public
  192. * @param string
  193. * @return string
  194. */
  195. function count_all($table = '')
  196. {
  197. if ($table == '')
  198. {
  199. return '0';
  200. }
  201. $query = $this->query('SELECT COUNT(*) AS numrows FROM `' . $table . '`');
  202. if ($query->num_rows() == 0)
  203. {
  204. return '0';
  205. }
  206. $row = $query->row();
  207. return $row->numrows;
  208. }
  209. /**
  210. * The error message string
  211. *
  212. * @access private
  213. * @return string
  214. */
  215. function _error_message()
  216. {
  217. $infos = $this->conn_id->errorInfo();
  218. return $infos[2];
  219. }
  220. /**
  221. * The error message number
  222. *
  223. * @access private
  224. * @return integer
  225. */
  226. function _error_number()
  227. {
  228. $infos = $this->conn_id->errorInfo();
  229. return $infos[1];
  230. }
  231. /**
  232. * Version number query string
  233. *
  234. * @access public
  235. * @return string
  236. */
  237. function version()
  238. {
  239. return $this->conn_id->getAttribute(constant("PDO::ATTR_SERVER_VERSION"));
  240. }
  241. /**
  242. * Escape Table Name
  243. *
  244. * This function adds backticks if the table name has a period
  245. * in it. Some DBs will get cranky unless periods are escaped
  246. *
  247. * @access public
  248. * @param string the table name
  249. * @return string
  250. */
  251. function escape_table($table)
  252. {
  253. if (stristr($table, '.'))
  254. {
  255. $table = preg_replace("/\./", '`.`', $table);
  256. }
  257. return $table;
  258. }
  259. /**
  260. * Field data query
  261. *
  262. * Generates a platform-specific query so that the column data can be retrieved
  263. *
  264. * @access public
  265. * @param string the table name
  266. * @return object
  267. */
  268. function _field_data($table)
  269. {
  270. $sql = 'SELECT * FROM ' . $this->escape_table($table) . ' LIMIT 1';
  271. $query = $this->query($sql);
  272. return $query->field_data();
  273. }
  274. /**
  275. * Insert statement
  276. *
  277. * Generates a platform-specific insert string from the supplied data
  278. *
  279. * @access public
  280. * @param string the table name
  281. * @param array the insert keys
  282. * @param array the insert values
  283. * @return string
  284. */
  285. function _insert($table, $keys, $values)
  286. {
  287. return "INSERT INTO " . $this->escape_table($table) . " (" . implode(', ', $keys) . ") VALUES (" . implode(', ', $values) . ")";
  288. }
  289. /**
  290. * Update statement
  291. *
  292. * Generates a platform-specific update string from the supplied data
  293. *
  294. * @access public
  295. * @param string the table name
  296. * @param array the update data
  297. * @param array the where clause
  298. * @return string
  299. */
  300. function _update($table, $values, $where)
  301. {
  302. foreach ($values as $key => $val)
  303. {
  304. $valstr[] = $key." = ".$val;
  305. }
  306. return "UPDATE " . $this->escape_table($table) . " SET " . implode(', ', $valstr) . " WHERE " . implode(" ", $where);
  307. }
  308. /**
  309. * Delete statement
  310. *
  311. * Generates a platform-specific delete string from the supplied data
  312. *
  313. * @access public
  314. * @param string the table name
  315. * @param array the where clause
  316. * @return string
  317. */
  318. function _delete($table, $where)
  319. {
  320. return "DELETE FROM " . $this->escape_table($table) . " WHERE " . implode(" ", $where);
  321. }
  322. /**
  323. * Show table query
  324. *
  325. * Generates a platform-specific query string so that the table names can be fetched
  326. *
  327. * @access public
  328. * @return string
  329. */
  330. function _show_tables()
  331. {
  332. return "SELECT name from sqlite_master WHERE type='table'";
  333. }
  334. /**
  335. * Show columnn query
  336. *
  337. * Generates a platform-specific query string so that the column names can be fetched
  338. *
  339. * @access public
  340. * @param string the table name
  341. * @return string
  342. */
  343. function _show_columns($table = '')
  344. {
  345. // Not supported
  346. return FALSE;
  347. }
  348. /**
  349. * Limit string
  350. *
  351. * Generates a platform-specific LIMIT clause
  352. *
  353. * @access public
  354. * @param string the sql query string
  355. * @param integer the number of rows to limit the query to
  356. * @param integer the offset value
  357. * @return string
  358. */
  359. function _limit($sql, $limit, $offset)
  360. {
  361. if ($offset == 0)
  362. {
  363. $offset = '';
  364. }
  365. else
  366. {
  367. $offset .= ', ';
  368. }
  369. return $sql . "LIMIT " . $offset . $limit;
  370. }
  371. // -----------------------------------------------------------------
  372. /**
  373. * Set client character set
  374. *
  375. * @access public
  376. * @param string
  377. * @param string
  378. * @return resource
  379. */
  380. function db_set_charset($charset, $collation)
  381. {
  382. // TODO - add support if needed
  383. return TRUE;
  384. }
  385. // --------------------------------------------------------------------
  386. /**
  387. * Protect Identifiers
  388. *
  389. * This function adds backticks if appropriate based on db type
  390. *
  391. * @access private
  392. * @param mixed the item to escape
  393. * @param boolean only affect the first word
  394. * @return mixed the item with backticks
  395. */
  396. function _protect_identifiers($item, $first_word_only = FALSE)
  397. {
  398. if (is_array($item))
  399. {
  400. $escaped_array = array();
  401. foreach($item as $k=>$v)
  402. {
  403. $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v, $first_word_only);
  404. }
  405. return $escaped_array;
  406. }
  407. // This function may get "item1 item2" as a string, and so
  408. // we may need "item1 item2" and not "item1 item2"
  409. if (ctype_alnum($item) === FALSE)
  410. {
  411. if (strpos($item, '.') !== FALSE)
  412. {
  413. $aliased_tables = implode(".",$this->ar_aliased_tables).'.';
  414. $table_name = substr($item, 0, strpos($item, '.')+1);
  415. $item = (strpos($aliased_tables, $table_name) !== FALSE) ? $item = $item : $this->dbprefix.$item;
  416. }
  417. // This function may get "field >= 1", and need it to return "field >= 1"
  418. $lbound = ($first_word_only === TRUE) ? '' : '|\s|\(';
  419. $item = preg_replace('/(^'.$lbound.')([\w\d\-\_]+?)(\s|\)|$)/iS', '$1$2$3', $item);
  420. }
  421. else
  422. {
  423. return "{$item}";
  424. }
  425. $exceptions = array('AS', '/', '-', '%', '+', '*', 'OR', 'IS');
  426. foreach ($exceptions as $exception)
  427. {
  428. if (stristr($item, " {$exception} ") !== FALSE)
  429. {
  430. $item = preg_replace('/ ('.preg_quote($exception).') /i', ' $1 ', $item);
  431. }
  432. }
  433. return $item;
  434. }
  435. // --------------------------------------------------------------------
  436. /**
  437. * From Tables
  438. *
  439. * This function implicitly groups FROM tables so there is no confusion
  440. * about operator precedence in harmony with SQL standards
  441. *
  442. * @access public
  443. * @param type
  444. * @return type
  445. */
  446. function _from_tables($tables)
  447. {
  448. if ( ! is_array($tables))
  449. {
  450. $tables = array($tables);
  451. }
  452. return '('.implode(', ', $tables).')';
  453. }
  454. // --------------------------------------------------------------------
  455. /**
  456. * Rollback Transaction
  457. *
  458. * @access public
  459. * @return bool
  460. */
  461. function trans_rollback()
  462. {
  463. if ( ! $this->trans_enabled)
  464. {
  465. return TRUE;
  466. }
  467. // When transactions are nested we only begin/commit/rollback the outermost ones
  468. if ($this->_trans_depth > 0)
  469. {
  470. return TRUE;
  471. }
  472. $this->simple_query('ROLLBACK');
  473. return TRUE;
  474. }
  475. /**
  476. * Begin Transaction
  477. *
  478. * @access public
  479. * @return bool
  480. */
  481. function trans_begin($test_mode = FALSE)
  482. {
  483. if ( ! $this->trans_enabled)
  484. {
  485. return TRUE;
  486. }
  487. // When transactions are nested we only begin/commit/rollback the outermost ones
  488. if ($this->_trans_depth > 0)
  489. {
  490. return TRUE;
  491. }
  492. // Reset the transaction failure flag.
  493. // If the $test_mode flag is set to TRUE transactions will be rolled back
  494. // even if the queries produce a successful result.
  495. $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
  496. $this->simple_query('BEGIN TRANSACTION');
  497. return TRUE;
  498. }
  499. /**
  500. * Commit Transaction
  501. *
  502. * @access public
  503. * @return bool
  504. */
  505. function trans_commit()
  506. {
  507. if ( ! $this->trans_enabled)
  508. {
  509. return TRUE;
  510. }
  511. // When transactions are nested we only begin/commit/rollback the outermost ones
  512. if ($this->_trans_depth > 0)
  513. {
  514. return TRUE;
  515. }
  516. $this->simple_query('COMMIT');
  517. return TRUE;
  518. }
  519. // --------------------------------------------------------------------
  520. /**
  521. * Close DB Connection
  522. *
  523. * @access public
  524. * @param resource
  525. * @return void
  526. */
  527. function _close($conn_id)
  528. {
  529. return true;
  530. }
  531. }
  532. ?>