PageRenderTime 26ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/appsys/database/drivers/sqlite/sqlite_driver.php

https://gitlab.com/venenux/codeigniterpower
PHP | 660 lines | 254 code | 104 blank | 302 comment | 30 complexity | b563d5cdc1f0c79dc1b661d5463eaf47 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 5.1.6 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2008 - 2011, 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. $this->_reset_select();
  301. return (int) $row->numrows;
  302. }
  303. // --------------------------------------------------------------------
  304. /**
  305. * List table query
  306. *
  307. * Generates a platform-specific query string so that the table names can be fetched
  308. *
  309. * @access private
  310. * @param boolean
  311. * @return string
  312. */
  313. function _list_tables($prefix_limit = FALSE)
  314. {
  315. $sql = "SELECT name from sqlite_master WHERE type='table'";
  316. if ($prefix_limit !== FALSE AND $this->dbprefix != '')
  317. {
  318. $sql .= " AND 'name' LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr);
  319. }
  320. return $sql;
  321. }
  322. // --------------------------------------------------------------------
  323. /**
  324. * Show column query
  325. *
  326. * Generates a platform-specific query string so that the column names can be fetched
  327. *
  328. * @access public
  329. * @param string the table name
  330. * @return string
  331. */
  332. function _list_columns($table = '')
  333. {
  334. // Not supported
  335. return FALSE;
  336. }
  337. // --------------------------------------------------------------------
  338. /**
  339. * Field data query
  340. *
  341. * Generates a platform-specific query so that the column data can be retrieved
  342. *
  343. * @access public
  344. * @param string the table name
  345. * @return object
  346. */
  347. function _field_data($table)
  348. {
  349. return "SELECT * FROM ".$table." LIMIT 1";
  350. }
  351. // --------------------------------------------------------------------
  352. /**
  353. * The error message string
  354. *
  355. * @access private
  356. * @return string
  357. */
  358. function _error_message()
  359. {
  360. return sqlite_error_string(sqlite_last_error($this->conn_id));
  361. }
  362. // --------------------------------------------------------------------
  363. /**
  364. * The error message number
  365. *
  366. * @access private
  367. * @return integer
  368. */
  369. function _error_number()
  370. {
  371. return sqlite_last_error($this->conn_id);
  372. }
  373. // --------------------------------------------------------------------
  374. /**
  375. * Escape the SQL Identifiers
  376. *
  377. * This function escapes column and table names
  378. *
  379. * @access private
  380. * @param string
  381. * @return string
  382. */
  383. function _escape_identifiers($item)
  384. {
  385. if ($this->_escape_char == '')
  386. {
  387. return $item;
  388. }
  389. foreach ($this->_reserved_identifiers as $id)
  390. {
  391. if (strpos($item, '.'.$id) !== FALSE)
  392. {
  393. $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
  394. // remove duplicates if the user already included the escape
  395. return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
  396. }
  397. }
  398. if (strpos($item, '.') !== FALSE)
  399. {
  400. $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
  401. }
  402. else
  403. {
  404. $str = $this->_escape_char.$item.$this->_escape_char;
  405. }
  406. // remove duplicates if the user already included the escape
  407. return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
  408. }
  409. // --------------------------------------------------------------------
  410. /**
  411. * From Tables
  412. *
  413. * This function implicitly groups FROM tables so there is no confusion
  414. * about operator precedence in harmony with SQL standards
  415. *
  416. * @access public
  417. * @param type
  418. * @return type
  419. */
  420. function _from_tables($tables)
  421. {
  422. if ( ! is_array($tables))
  423. {
  424. return strstr($tables, ',') ? '('.$tables.')' : $tables; // PICCORO retorna una tbla solo si no es array
  425. }
  426. else
  427. {
  428. return count($tables) > 1 ? '('.implode(', ', $tables).')' : end($tables);
  429. }
  430. }
  431. // --------------------------------------------------------------------
  432. /**
  433. * Insert statement
  434. *
  435. * Generates a platform-specific insert string from the supplied data
  436. *
  437. * @access public
  438. * @param string the table name
  439. * @param array the insert keys
  440. * @param array the insert values
  441. * @return string
  442. */
  443. function _insert($table, $keys, $values)
  444. {
  445. return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
  446. }
  447. // --------------------------------------------------------------------
  448. /**
  449. * Update statement
  450. *
  451. * Generates a platform-specific update string from the supplied data
  452. *
  453. * @access public
  454. * @param string the table name
  455. * @param array the update data
  456. * @param array the where clause
  457. * @param array the orderby clause
  458. * @param array the limit clause
  459. * @return string
  460. */
  461. function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
  462. {
  463. foreach ($values as $key => $val)
  464. {
  465. $valstr[] = $key." = ".$val;
  466. }
  467. $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
  468. $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
  469. $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
  470. $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
  471. $sql .= $orderby.$limit;
  472. return $sql;
  473. }
  474. // --------------------------------------------------------------------
  475. /**
  476. * Truncate statement
  477. *
  478. * Generates a platform-specific truncate string from the supplied data
  479. * If the database does not support the truncate() command
  480. * This function maps to "DELETE FROM table"
  481. *
  482. * @access public
  483. * @param string the table name
  484. * @return string
  485. */
  486. function _truncate($table)
  487. {
  488. return $this->_delete($table);
  489. }
  490. // --------------------------------------------------------------------
  491. /**
  492. * Delete statement
  493. *
  494. * Generates a platform-specific delete string from the supplied data
  495. *
  496. * @access public
  497. * @param string the table name
  498. * @param array the where clause
  499. * @param string the limit clause
  500. * @return string
  501. */
  502. function _delete($table, $where = array(), $like = array(), $limit = FALSE)
  503. {
  504. $conditions = '';
  505. if (count($where) > 0 OR count($like) > 0)
  506. {
  507. $conditions = "\nWHERE ";
  508. $conditions .= implode("\n", $this->ar_where);
  509. if (count($where) > 0 && count($like) > 0)
  510. {
  511. $conditions .= " AND ";
  512. }
  513. $conditions .= implode("\n", $like);
  514. }
  515. $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
  516. return "DELETE FROM ".$table.$conditions.$limit;
  517. }
  518. // --------------------------------------------------------------------
  519. /**
  520. * Limit string
  521. *
  522. * Generates a platform-specific LIMIT clause
  523. *
  524. * @access public
  525. * @param string the sql query string
  526. * @param integer the number of rows to limit the query to
  527. * @param integer the offset value
  528. * @return string
  529. */
  530. function _limit($sql, $limit, $offset)
  531. {
  532. if ($offset == 0)
  533. {
  534. $offset = '';
  535. }
  536. else
  537. {
  538. $offset .= ", ";
  539. }
  540. return $sql."LIMIT ".$offset.$limit;
  541. }
  542. // --------------------------------------------------------------------
  543. /**
  544. * Close DB Connection
  545. *
  546. * @access public
  547. * @param resource
  548. * @return void
  549. */
  550. function _close($conn_id)
  551. {
  552. @sqlite_close($conn_id);
  553. }
  554. }
  555. /* End of file sqlite_driver.php */
  556. /* Location: ./system/database/drivers/sqlite/sqlite_driver.php */