sqlite3_driver.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 - 2019, 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 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
  33. * @license https://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. if (($result = $this->query('PRAGMA TABLE_INFO('.$this->protect_identifiers($table, TRUE, NULL, FALSE).')')) === FALSE)
  206. {
  207. return FALSE;
  208. }
  209. $fields = array();
  210. foreach ($result->result_array() as $row)
  211. {
  212. $fields[] = $row['name'];
  213. }
  214. return $fields;
  215. }
  216. // --------------------------------------------------------------------
  217. /**
  218. * Returns an object with field data
  219. *
  220. * @param string $table
  221. * @return array
  222. */
  223. public function field_data($table)
  224. {
  225. if (($query = $this->query('PRAGMA TABLE_INFO('.$this->protect_identifiers($table, TRUE, NULL, FALSE).')')) === FALSE)
  226. {
  227. return FALSE;
  228. }
  229. $query = $query->result_array();
  230. if (empty($query))
  231. {
  232. return FALSE;
  233. }
  234. $retval = array();
  235. for ($i = 0, $c = count($query); $i < $c; $i++)
  236. {
  237. $retval[$i] = new stdClass();
  238. $retval[$i]->name = $query[$i]['name'];
  239. $retval[$i]->type = $query[$i]['type'];
  240. $retval[$i]->max_length = NULL;
  241. $retval[$i]->default = $query[$i]['dflt_value'];
  242. $retval[$i]->primary_key = isset($query[$i]['pk']) ? (int) $query[$i]['pk'] : 0;
  243. }
  244. return $retval;
  245. }
  246. // --------------------------------------------------------------------
  247. /**
  248. * Error
  249. *
  250. * Returns an array containing code and message of the last
  251. * database error that has occurred.
  252. *
  253. * @return array
  254. */
  255. public function error()
  256. {
  257. return array('code' => $this->conn_id->lastErrorCode(), 'message' => $this->conn_id->lastErrorMsg());
  258. }
  259. // --------------------------------------------------------------------
  260. /**
  261. * Replace statement
  262. *
  263. * Generates a platform-specific replace string from the supplied data
  264. *
  265. * @param string $table Table name
  266. * @param array $keys INSERT keys
  267. * @param array $values INSERT values
  268. * @return string
  269. */
  270. protected function _replace($table, $keys, $values)
  271. {
  272. return 'INSERT OR '.parent::_replace($table, $keys, $values);
  273. }
  274. // --------------------------------------------------------------------
  275. /**
  276. * Truncate statement
  277. *
  278. * Generates a platform-specific truncate string from the supplied data
  279. *
  280. * If the database does not support the TRUNCATE statement,
  281. * then this method maps to 'DELETE FROM table'
  282. *
  283. * @param string $table
  284. * @return string
  285. */
  286. protected function _truncate($table)
  287. {
  288. return 'DELETE FROM '.$table;
  289. }
  290. // --------------------------------------------------------------------
  291. /**
  292. * Close DB Connection
  293. *
  294. * @return void
  295. */
  296. protected function _close()
  297. {
  298. $this->conn_id->close();
  299. }
  300. }