You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

330 lines
7.8 KiB

7 years ago
  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 - 2017, 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 - 2017, British Columbia Institute of Technology (http://bcit.ca/)
  33. * @license http://opensource.org/licenses/MIT MIT License
  34. * @link https://codeigniter.com
  35. * @since Version 1.3.0
  36. * @filesource
  37. */
  38. defined('BASEPATH') OR exit('No direct script access allowed');
  39. /**
  40. * SQLite 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_sqlite_driver extends CI_DB {
  53. /**
  54. * Database driver
  55. *
  56. * @var string
  57. */
  58. public $dbdriver = 'sqlite';
  59. // --------------------------------------------------------------------
  60. /**
  61. * ORDER BY random keyword
  62. *
  63. * @var array
  64. */
  65. protected $_random_keyword = array('RANDOM()', 'RANDOM()');
  66. // --------------------------------------------------------------------
  67. /**
  68. * Non-persistent database connection
  69. *
  70. * @param bool $persistent
  71. * @return resource
  72. */
  73. public function db_connect($persistent = FALSE)
  74. {
  75. $error = NULL;
  76. $conn_id = ($persistent === TRUE)
  77. ? sqlite_popen($this->database, 0666, $error)
  78. : sqlite_open($this->database, 0666, $error);
  79. isset($error) && log_message('error', $error);
  80. return $conn_id;
  81. }
  82. // --------------------------------------------------------------------
  83. /**
  84. * Database version number
  85. *
  86. * @return string
  87. */
  88. public function version()
  89. {
  90. return isset($this->data_cache['version'])
  91. ? $this->data_cache['version']
  92. : $this->data_cache['version'] = sqlite_libversion();
  93. }
  94. // --------------------------------------------------------------------
  95. /**
  96. * Execute the query
  97. *
  98. * @param string $sql an SQL query
  99. * @return resource
  100. */
  101. protected function _execute($sql)
  102. {
  103. return $this->is_write_type($sql)
  104. ? sqlite_exec($this->conn_id, $sql)
  105. : sqlite_query($this->conn_id, $sql);
  106. }
  107. // --------------------------------------------------------------------
  108. /**
  109. * Begin Transaction
  110. *
  111. * @return bool
  112. */
  113. protected function _trans_begin()
  114. {
  115. return $this->simple_query('BEGIN TRANSACTION');
  116. }
  117. // --------------------------------------------------------------------
  118. /**
  119. * Commit Transaction
  120. *
  121. * @return bool
  122. */
  123. protected function _trans_commit()
  124. {
  125. return $this->simple_query('COMMIT');
  126. }
  127. // --------------------------------------------------------------------
  128. /**
  129. * Rollback Transaction
  130. *
  131. * @return bool
  132. */
  133. protected function _trans_rollback()
  134. {
  135. return $this->simple_query('ROLLBACK');
  136. }
  137. // --------------------------------------------------------------------
  138. /**
  139. * Platform-dependant string escape
  140. *
  141. * @param string
  142. * @return string
  143. */
  144. protected function _escape_str($str)
  145. {
  146. return sqlite_escape_string($str);
  147. }
  148. // --------------------------------------------------------------------
  149. /**
  150. * Affected Rows
  151. *
  152. * @return int
  153. */
  154. public function affected_rows()
  155. {
  156. return sqlite_changes($this->conn_id);
  157. }
  158. // --------------------------------------------------------------------
  159. /**
  160. * Insert ID
  161. *
  162. * @return int
  163. */
  164. public function insert_id()
  165. {
  166. return sqlite_last_insert_rowid($this->conn_id);
  167. }
  168. // --------------------------------------------------------------------
  169. /**
  170. * List table query
  171. *
  172. * Generates a platform-specific query string so that the table names can be fetched
  173. *
  174. * @param bool $prefix_limit
  175. * @return string
  176. */
  177. protected function _list_tables($prefix_limit = FALSE)
  178. {
  179. $sql = "SELECT name FROM sqlite_master WHERE type='table'";
  180. if ($prefix_limit !== FALSE && $this->dbprefix != '')
  181. {
  182. return $sql." AND 'name' LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr);
  183. }
  184. return $sql;
  185. }
  186. // --------------------------------------------------------------------
  187. /**
  188. * Show column query
  189. *
  190. * Generates a platform-specific query string so that the column names can be fetched
  191. *
  192. * @param string $table
  193. * @return bool
  194. */
  195. protected function _list_columns($table = '')
  196. {
  197. // Not supported
  198. return FALSE;
  199. }
  200. // --------------------------------------------------------------------
  201. /**
  202. * Returns an object with field data
  203. *
  204. * @param string $table
  205. * @return array
  206. */
  207. public function field_data($table)
  208. {
  209. if (($query = $this->query('PRAGMA TABLE_INFO('.$this->protect_identifiers($table, TRUE, NULL, FALSE).')')) === FALSE)
  210. {
  211. return FALSE;
  212. }
  213. $query = $query->result_array();
  214. if (empty($query))
  215. {
  216. return FALSE;
  217. }
  218. $retval = array();
  219. for ($i = 0, $c = count($query); $i < $c; $i++)
  220. {
  221. $retval[$i] = new stdClass();
  222. $retval[$i]->name = $query[$i]['name'];
  223. $retval[$i]->type = $query[$i]['type'];
  224. $retval[$i]->max_length = NULL;
  225. $retval[$i]->default = $query[$i]['dflt_value'];
  226. $retval[$i]->primary_key = isset($query[$i]['pk']) ? (int) $query[$i]['pk'] : 0;
  227. }
  228. return $retval;
  229. }
  230. // --------------------------------------------------------------------
  231. /**
  232. * Error
  233. *
  234. * Returns an array containing code and message of the last
  235. * database error that has occured.
  236. *
  237. * @return array
  238. */
  239. public function error()
  240. {
  241. $error = array('code' => sqlite_last_error($this->conn_id));
  242. $error['message'] = sqlite_error_string($error['code']);
  243. return $error;
  244. }
  245. // --------------------------------------------------------------------
  246. /**
  247. * Replace statement
  248. *
  249. * Generates a platform-specific replace string from the supplied data
  250. *
  251. * @param string $table Table name
  252. * @param array $keys INSERT keys
  253. * @param array $values INSERT values
  254. * @return string
  255. */
  256. protected function _replace($table, $keys, $values)
  257. {
  258. return 'INSERT OR '.parent::_replace($table, $keys, $values);
  259. }
  260. // --------------------------------------------------------------------
  261. /**
  262. * Truncate statement
  263. *
  264. * Generates a platform-specific truncate string from the supplied data
  265. *
  266. * If the database does not support the TRUNCATE statement,
  267. * then this function maps to 'DELETE FROM table'
  268. *
  269. * @param string $table
  270. * @return string
  271. */
  272. protected function _truncate($table)
  273. {
  274. return 'DELETE FROM '.$table;
  275. }
  276. // --------------------------------------------------------------------
  277. /**
  278. * Close DB Connection
  279. *
  280. * @return void
  281. */
  282. protected function _close()
  283. {
  284. sqlite_close($this->conn_id);
  285. }
  286. }