/halogy/database/drivers/sqlite/sqlite_driver.php

https://bitbucket.org/haloweb/halogy-1.0/ · PHP · 657 lines · 250 code · 105 blank · 302 comment · 30 complexity · 1a5ec4dd7662ff9ad4ed28696d36caa3 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 ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2008 - 2009, EllisLab, Inc.
  10. * @license http://codeigniter.com/user_guide/license.html
  11. * @link http://codeigniter.com
  12. * @since Version 1.0
  13. * @filesource
  14. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * SQLite 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 ExpressionEngine Dev Team
  27. * @link http://codeigniter.com/user_guide/database/
  28. */
  29. class CI_DB_sqlite_driver extends CI_DB {
  30. var $dbdriver = 'sqlite';
  31. // The character used to escape with - not needed for SQLite
  32. var $_escape_char = '';
  33. // clause and character used for LIKE escape sequences
  34. var $_like_escape_str = " ESCAPE '%s' ";
  35. var $_like_escape_chr = '!';
  36. /**
  37. * The syntax to count rows is slightly different across different
  38. * database engines, so this string appears in each driver and is
  39. * used for the count_all() and count_all_results() functions.
  40. */
  41. var $_count_string = "SELECT COUNT(*) AS ";
  42. var $_random_keyword = ' Random()'; // database specific random keyword
  43. /**
  44. * Non-persistent database connection
  45. *
  46. * @access private called by the base class
  47. * @return resource
  48. */
  49. function db_connect()
  50. {
  51. if ( ! $conn_id = @sqlite_open($this->database, FILE_WRITE_MODE, $error))
  52. {
  53. log_message('error', $error);
  54. if ($this->db_debug)
  55. {
  56. $this->display_error($error, '', TRUE);
  57. }
  58. return FALSE;
  59. }
  60. return $conn_id;
  61. }
  62. // --------------------------------------------------------------------
  63. /**
  64. * Persistent database connection
  65. *
  66. * @access private called by the base class
  67. * @return resource
  68. */
  69. function db_pconnect()
  70. {
  71. if ( ! $conn_id = @sqlite_popen($this->database, FILE_WRITE_MODE, $error))
  72. {
  73. log_message('error', $error);
  74. if ($this->db_debug)
  75. {
  76. $this->display_error($error, '', TRUE);
  77. }
  78. return FALSE;
  79. }
  80. return $conn_id;
  81. }
  82. // --------------------------------------------------------------------
  83. /**
  84. * Reconnect
  85. *
  86. * Keep / reestablish the db connection if no queries have been
  87. * sent for a length of time exceeding the server's idle timeout
  88. *
  89. * @access public
  90. * @return void
  91. */
  92. function reconnect()
  93. {
  94. // not implemented in SQLite
  95. }
  96. // --------------------------------------------------------------------
  97. /**
  98. * Select the database
  99. *
  100. * @access private called by the base class
  101. * @return resource
  102. */
  103. function db_select()
  104. {
  105. return TRUE;
  106. }
  107. // --------------------------------------------------------------------
  108. /**
  109. * Set client character set
  110. *
  111. * @access public
  112. * @param string
  113. * @param string
  114. * @return resource
  115. */
  116. function db_set_charset($charset, $collation)
  117. {
  118. // @todo - add support if needed
  119. return TRUE;
  120. }
  121. // --------------------------------------------------------------------
  122. /**
  123. * Version number query string
  124. *
  125. * @access public
  126. * @return string
  127. */
  128. function _version()
  129. {
  130. return sqlite_libversion();
  131. }
  132. // --------------------------------------------------------------------
  133. /**
  134. * Execute the query
  135. *
  136. * @access private called by the base class
  137. * @param string an SQL query
  138. * @return resource
  139. */
  140. function _execute($sql)
  141. {
  142. $sql = $this->_prep_query($sql);
  143. return @sqlite_query($this->conn_id, $sql);
  144. }
  145. // --------------------------------------------------------------------
  146. /**
  147. * Prep the query
  148. *
  149. * If needed, each database adapter can prep the query string
  150. *
  151. * @access private called by execute()
  152. * @param string an SQL query
  153. * @return string
  154. */
  155. function _prep_query($sql)
  156. {
  157. return $sql;
  158. }
  159. // --------------------------------------------------------------------
  160. /**
  161. * Begin Transaction
  162. *
  163. * @access public
  164. * @return bool
  165. */
  166. function trans_begin($test_mode = FALSE)
  167. {
  168. if ( ! $this->trans_enabled)
  169. {
  170. return TRUE;
  171. }
  172. // When transactions are nested we only begin/commit/rollback the outermost ones
  173. if ($this->_trans_depth > 0)
  174. {
  175. return TRUE;
  176. }
  177. // Reset the transaction failure flag.
  178. // If the $test_mode flag is set to TRUE transactions will be rolled back
  179. // even if the queries produce a successful result.
  180. $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
  181. $this->simple_query('BEGIN TRANSACTION');
  182. return TRUE;
  183. }
  184. // --------------------------------------------------------------------
  185. /**
  186. * Commit Transaction
  187. *
  188. * @access public
  189. * @return bool
  190. */
  191. function trans_commit()
  192. {
  193. if ( ! $this->trans_enabled)
  194. {
  195. return TRUE;
  196. }
  197. // When transactions are nested we only begin/commit/rollback the outermost ones
  198. if ($this->_trans_depth > 0)
  199. {
  200. return TRUE;
  201. }
  202. $this->simple_query('COMMIT');
  203. return TRUE;
  204. }
  205. // --------------------------------------------------------------------
  206. /**
  207. * Rollback Transaction
  208. *
  209. * @access public
  210. * @return bool
  211. */
  212. function trans_rollback()
  213. {
  214. if ( ! $this->trans_enabled)
  215. {
  216. return TRUE;
  217. }
  218. // When transactions are nested we only begin/commit/rollback the outermost ones
  219. if ($this->_trans_depth > 0)
  220. {
  221. return TRUE;
  222. }
  223. $this->simple_query('ROLLBACK');
  224. return TRUE;
  225. }
  226. // --------------------------------------------------------------------
  227. /**
  228. * Escape String
  229. *
  230. * @access public
  231. * @param string
  232. * @param bool whether or not the string will be used in a LIKE condition
  233. * @return string
  234. */
  235. function escape_str($str, $like = FALSE)
  236. {
  237. if (is_array($str))
  238. {
  239. foreach($str as $key => $val)
  240. {
  241. $str[$key] = $this->escape_str($val, $like);
  242. }
  243. return $str;
  244. }
  245. $str = sqlite_escape_string($str);
  246. // escape LIKE condition wildcards
  247. if ($like === TRUE)
  248. {
  249. $str = str_replace( array('%', '_', $this->_like_escape_chr),
  250. array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr),
  251. $str);
  252. }
  253. return $str;
  254. }
  255. // --------------------------------------------------------------------
  256. /**
  257. * Affected Rows
  258. *
  259. * @access public
  260. * @return integer
  261. */
  262. function affected_rows()
  263. {
  264. return sqlite_changes($this->conn_id);
  265. }
  266. // --------------------------------------------------------------------
  267. /**
  268. * Insert ID
  269. *
  270. * @access public
  271. * @return integer
  272. */
  273. function insert_id()
  274. {
  275. return @sqlite_last_insert_rowid($this->conn_id);
  276. }
  277. // --------------------------------------------------------------------
  278. /**
  279. * "Count All" query
  280. *
  281. * Generates a platform-specific query string that counts all records in
  282. * the specified database
  283. *
  284. * @access public
  285. * @param string
  286. * @return string
  287. */
  288. function count_all($table = '')
  289. {
  290. if ($table == '')
  291. {
  292. return 0;
  293. }
  294. $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
  295. if ($query->num_rows() == 0)
  296. {
  297. return 0;
  298. }
  299. $row = $query->row();
  300. return (int) $row->numrows;
  301. }
  302. // --------------------------------------------------------------------
  303. /**
  304. * List table query
  305. *
  306. * Generates a platform-specific query string so that the table names can be fetched
  307. *
  308. * @access private
  309. * @param boolean
  310. * @return string
  311. */
  312. function _list_tables($prefix_limit = FALSE)
  313. {
  314. $sql = "SELECT name from sqlite_master WHERE type='table'";
  315. if ($prefix_limit !== FALSE AND $this->dbprefix != '')
  316. {
  317. $sql .= " AND 'name' LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_char);
  318. }
  319. return $sql;
  320. }
  321. // --------------------------------------------------------------------
  322. /**
  323. * Show column query
  324. *
  325. * Generates a platform-specific query string so that the column names can be fetched
  326. *
  327. * @access public
  328. * @param string the table name
  329. * @return string
  330. */
  331. function _list_columns($table = '')
  332. {
  333. // Not supported
  334. return FALSE;
  335. }
  336. // --------------------------------------------------------------------
  337. /**
  338. * Field data query
  339. *
  340. * Generates a platform-specific query so that the column data can be retrieved
  341. *
  342. * @access public
  343. * @param string the table name
  344. * @return object
  345. */
  346. function _field_data($table)
  347. {
  348. return "SELECT * FROM ".$table." LIMIT 1";
  349. }
  350. // --------------------------------------------------------------------
  351. /**
  352. * The error message string
  353. *
  354. * @access private
  355. * @return string
  356. */
  357. function _error_message()
  358. {
  359. return sqlite_error_string(sqlite_last_error($this->conn_id));
  360. }
  361. // --------------------------------------------------------------------
  362. /**
  363. * The error message number
  364. *
  365. * @access private
  366. * @return integer
  367. */
  368. function _error_number()
  369. {
  370. return sqlite_last_error($this->conn_id);
  371. }
  372. // --------------------------------------------------------------------
  373. /**
  374. * Escape the SQL Identifiers
  375. *
  376. * This function escapes column and table names
  377. *
  378. * @access private
  379. * @param string
  380. * @return string
  381. */
  382. function _escape_identifiers($item)
  383. {
  384. if ($this->_escape_char == '')
  385. {
  386. return $item;
  387. }
  388. foreach ($this->_reserved_identifiers as $id)
  389. {
  390. if (strpos($item, '.'.$id) !== FALSE)
  391. {
  392. $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
  393. // remove duplicates if the user already included the escape
  394. return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
  395. }
  396. }
  397. if (strpos($item, '.') !== FALSE)
  398. {
  399. $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
  400. }
  401. else
  402. {
  403. $str = $this->_escape_char.$item.$this->_escape_char;
  404. }
  405. // remove duplicates if the user already included the escape
  406. return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
  407. }
  408. // --------------------------------------------------------------------
  409. /**
  410. * From Tables
  411. *
  412. * This function implicitly groups FROM tables so there is no confusion
  413. * about operator precedence in harmony with SQL standards
  414. *
  415. * @access public
  416. * @param type
  417. * @return type
  418. */
  419. function _from_tables($tables)
  420. {
  421. if ( ! is_array($tables))
  422. {
  423. $tables = array($tables);
  424. }
  425. return '('.implode(', ', $tables).')';
  426. }
  427. // --------------------------------------------------------------------
  428. /**
  429. * Insert statement
  430. *
  431. * Generates a platform-specific insert string from the supplied data
  432. *
  433. * @access public
  434. * @param string the table name
  435. * @param array the insert keys
  436. * @param array the insert values
  437. * @return string
  438. */
  439. function _insert($table, $keys, $values)
  440. {
  441. return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
  442. }
  443. // --------------------------------------------------------------------
  444. /**
  445. * Update statement
  446. *
  447. * Generates a platform-specific update string from the supplied data
  448. *
  449. * @access public
  450. * @param string the table name
  451. * @param array the update data
  452. * @param array the where clause
  453. * @param array the orderby clause
  454. * @param array the limit clause
  455. * @return string
  456. */
  457. function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
  458. {
  459. foreach($values as $key => $val)
  460. {
  461. $valstr[] = $key." = ".$val;
  462. }
  463. $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
  464. $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
  465. $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
  466. $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
  467. $sql .= $orderby.$limit;
  468. return $sql;
  469. }
  470. // --------------------------------------------------------------------
  471. /**
  472. * Truncate statement
  473. *
  474. * Generates a platform-specific truncate string from the supplied data
  475. * If the database does not support the truncate() command
  476. * This function maps to "DELETE FROM table"
  477. *
  478. * @access public
  479. * @param string the table name
  480. * @return string
  481. */
  482. function _truncate($table)
  483. {
  484. return $this->_delete($table);
  485. }
  486. // --------------------------------------------------------------------
  487. /**
  488. * Delete statement
  489. *
  490. * Generates a platform-specific delete string from the supplied data
  491. *
  492. * @access public
  493. * @param string the table name
  494. * @param array the where clause
  495. * @param string the limit clause
  496. * @return string
  497. */
  498. function _delete($table, $where = array(), $like = array(), $limit = FALSE)
  499. {
  500. $conditions = '';
  501. if (count($where) > 0 OR count($like) > 0)
  502. {
  503. $conditions = "\nWHERE ";
  504. $conditions .= implode("\n", $this->ar_where);
  505. if (count($where) > 0 && count($like) > 0)
  506. {
  507. $conditions .= " AND ";
  508. }
  509. $conditions .= implode("\n", $like);
  510. }
  511. $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
  512. return "DELETE FROM ".$table.$conditions.$limit;
  513. }
  514. // --------------------------------------------------------------------
  515. /**
  516. * Limit string
  517. *
  518. * Generates a platform-specific LIMIT clause
  519. *
  520. * @access public
  521. * @param string the sql query string
  522. * @param integer the number of rows to limit the query to
  523. * @param integer the offset value
  524. * @return string
  525. */
  526. function _limit($sql, $limit, $offset)
  527. {
  528. if ($offset == 0)
  529. {
  530. $offset = '';
  531. }
  532. else
  533. {
  534. $offset .= ", ";
  535. }
  536. return $sql."LIMIT ".$offset.$limit;
  537. }
  538. // --------------------------------------------------------------------
  539. /**
  540. * Close DB Connection
  541. *
  542. * @access public
  543. * @param resource
  544. * @return void
  545. */
  546. function _close($conn_id)
  547. {
  548. @sqlite_close($conn_id);
  549. }
  550. }
  551. /* End of file sqlite_driver.php */
  552. /* Location: ./system/database/drivers/sqlite/sqlite_driver.php */