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.

350 lines
8.4 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 3.0.0
  36. * @filesource
  37. */
  38. defined('BASEPATH') OR exit('No direct script access allowed');
  39. /**
  40. * SQLite3 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 Andrey Andreev
  50. * @link https://codeigniter.com/user_guide/database/
  51. */
  52. class CI_DB_sqlite3_driver extends CI_DB {
  53. /**
  54. * Database driver
  55. *
  56. * @var string
  57. */
  58. public $dbdriver = 'sqlite3';
  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 SQLite3
  72. */
  73. public function db_connect($persistent = FALSE)
  74. {
  75. if ($persistent)
  76. {
  77. log_message('debug', 'SQLite3 doesn\'t support persistent connections');
  78. }
  79. try
  80. {
  81. return ( ! $this->password)
  82. ? new SQLite3($this->database)
  83. : new SQLite3($this->database, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE, $this->password);
  84. }
  85. catch (Exception $e)
  86. {
  87. return FALSE;
  88. }
  89. }
  90. // --------------------------------------------------------------------
  91. /**
  92. * Database version number
  93. *
  94. * @return string
  95. */
  96. public function version()
  97. {
  98. if (isset($this->data_cache['version']))
  99. {
  100. return $this->data_cache['version'];
  101. }
  102. $version = SQLite3::version();
  103. return $this->data_cache['version'] = $version['versionString'];
  104. }
  105. // --------------------------------------------------------------------
  106. /**
  107. * Execute the query
  108. *
  109. * @todo Implement use of SQLite3::querySingle(), if needed
  110. * @param string $sql
  111. * @return mixed SQLite3Result object or bool
  112. */
  113. protected function _execute($sql)
  114. {
  115. return $this->is_write_type($sql)
  116. ? $this->conn_id->exec($sql)
  117. : $this->conn_id->query($sql);
  118. }
  119. // --------------------------------------------------------------------
  120. /**
  121. * Begin Transaction
  122. *
  123. * @return bool
  124. */
  125. protected function _trans_begin()
  126. {
  127. return $this->conn_id->exec('BEGIN TRANSACTION');
  128. }
  129. // --------------------------------------------------------------------
  130. /**
  131. * Commit Transaction
  132. *
  133. * @return bool
  134. */
  135. protected function _trans_commit()
  136. {
  137. return $this->conn_id->exec('END TRANSACTION');
  138. }
  139. // --------------------------------------------------------------------
  140. /**
  141. * Rollback Transaction
  142. *
  143. * @return bool
  144. */
  145. protected function _trans_rollback()
  146. {
  147. return $this->conn_id->exec('ROLLBACK');
  148. }
  149. // --------------------------------------------------------------------
  150. /**
  151. * Platform-dependent string escape
  152. *
  153. * @param string
  154. * @return string
  155. */
  156. protected function _escape_str($str)
  157. {
  158. return $this->conn_id->escapeString($str);
  159. }
  160. // --------------------------------------------------------------------
  161. /**
  162. * Affected Rows
  163. *
  164. * @return int
  165. */
  166. public function affected_rows()
  167. {
  168. return $this->conn_id->changes();
  169. }
  170. // --------------------------------------------------------------------
  171. /**
  172. * Insert ID
  173. *
  174. * @return int
  175. */
  176. public function insert_id()
  177. {
  178. return $this->conn_id->lastInsertRowID();
  179. }
  180. // --------------------------------------------------------------------
  181. /**
  182. * Show table query
  183. *
  184. * Generates a platform-specific query string so that the table names can be fetched
  185. *
  186. * @param bool $prefix_limit
  187. * @return string
  188. */
  189. protected function _list_tables($prefix_limit = FALSE)
  190. {
  191. return 'SELECT "NAME" FROM "SQLITE_MASTER" WHERE "TYPE" = \'table\''
  192. .(($prefix_limit !== FALSE && $this->dbprefix != '')
  193. ? ' AND "NAME" LIKE \''.$this->escape_like_str($this->dbprefix).'%\' '.sprintf($this->_like_escape_str, $this->_like_escape_chr)
  194. : '');
  195. }
  196. // --------------------------------------------------------------------
  197. /**
  198. * Fetch Field Names
  199. *
  200. * @param string $table Table name
  201. * @return array
  202. */
  203. public function list_fields($table)
  204. {
  205. // Is there a cached result?
  206. if (isset($this->data_cache['field_names'][$table]))
  207. {
  208. return $this->data_cache['field_names'][$table];
  209. }
  210. if (($result = $this->query('PRAGMA TABLE_INFO('.$this->protect_identifiers($table, TRUE, NULL, FALSE).')')) === FALSE)
  211. {
  212. return FALSE;
  213. }
  214. $this->data_cache['field_names'][$table] = array();
  215. foreach ($result->result_array() as $row)
  216. {
  217. $this->data_cache['field_names'][$table][] = $row['name'];
  218. }
  219. return $this->data_cache['field_names'][$table];
  220. }
  221. // --------------------------------------------------------------------
  222. /**
  223. * Returns an object with field data
  224. *
  225. * @param string $table
  226. * @return array
  227. */
  228. public function field_data($table)
  229. {
  230. if (($query = $this->query('PRAGMA TABLE_INFO('.$this->protect_identifiers($table, TRUE, NULL, FALSE).')')) === FALSE)
  231. {
  232. return FALSE;
  233. }
  234. $query = $query->result_array();
  235. if (empty($query))
  236. {
  237. return FALSE;
  238. }
  239. $retval = array();
  240. for ($i = 0, $c = count($query); $i < $c; $i++)
  241. {
  242. $retval[$i] = new stdClass();
  243. $retval[$i]->name = $query[$i]['name'];
  244. $retval[$i]->type = $query[$i]['type'];
  245. $retval[$i]->max_length = NULL;
  246. $retval[$i]->default = $query[$i]['dflt_value'];
  247. $retval[$i]->primary_key = isset($query[$i]['pk']) ? (int) $query[$i]['pk'] : 0;
  248. }
  249. return $retval;
  250. }
  251. // --------------------------------------------------------------------
  252. /**
  253. * Error
  254. *
  255. * Returns an array containing code and message of the last
  256. * database error that has occurred.
  257. *
  258. * @return array
  259. */
  260. public function error()
  261. {
  262. return array('code' => $this->conn_id->lastErrorCode(), 'message' => $this->conn_id->lastErrorMsg());
  263. }
  264. // --------------------------------------------------------------------
  265. /**
  266. * Replace statement
  267. *
  268. * Generates a platform-specific replace string from the supplied data
  269. *
  270. * @param string $table Table name
  271. * @param array $keys INSERT keys
  272. * @param array $values INSERT values
  273. * @return string
  274. */
  275. protected function _replace($table, $keys, $values)
  276. {
  277. return 'INSERT OR '.parent::_replace($table, $keys, $values);
  278. }
  279. // --------------------------------------------------------------------
  280. /**
  281. * Truncate statement
  282. *
  283. * Generates a platform-specific truncate string from the supplied data
  284. *
  285. * If the database does not support the TRUNCATE statement,
  286. * then this method maps to 'DELETE FROM table'
  287. *
  288. * @param string $table
  289. * @return string
  290. */
  291. protected function _truncate($table)
  292. {
  293. return 'DELETE FROM '.$table;
  294. }
  295. // --------------------------------------------------------------------
  296. /**
  297. * Close DB Connection
  298. *
  299. * @return void
  300. */
  301. protected function _close()
  302. {
  303. $this->conn_id->close();
  304. }
  305. }