PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/sp_1/system/database/drivers/sqlite3/sqlite3_driver.php

https://gitlab.com/rezaul007/Hospital-information-bank
PHP | 360 lines | 125 code | 46 blank | 189 comment | 10 complexity | c0aaca51e053c2caec85ac79966ce5c6 MD5 | raw file
  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 - 2015, 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. (http://ellislab.com/)
  32. * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
  33. * @license http://opensource.org/licenses/MIT MIT License
  34. * @link http://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 http://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. * @param bool $test_mode
  124. * @return bool
  125. */
  126. public function trans_begin($test_mode = FALSE)
  127. {
  128. // When transactions are nested we only begin/commit/rollback the outermost ones
  129. if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
  130. {
  131. return TRUE;
  132. }
  133. // Reset the transaction failure flag.
  134. // If the $test_mode flag is set to TRUE transactions will be rolled back
  135. // even if the queries produce a successful result.
  136. $this->_trans_failure = ($test_mode === TRUE);
  137. return $this->conn_id->exec('BEGIN TRANSACTION');
  138. }
  139. // --------------------------------------------------------------------
  140. /**
  141. * Commit Transaction
  142. *
  143. * @return bool
  144. */
  145. public function trans_commit()
  146. {
  147. // When transactions are nested we only begin/commit/rollback the outermost ones
  148. if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
  149. {
  150. return TRUE;
  151. }
  152. return $this->conn_id->exec('END TRANSACTION');
  153. }
  154. // --------------------------------------------------------------------
  155. /**
  156. * Rollback Transaction
  157. *
  158. * @return bool
  159. */
  160. public function trans_rollback()
  161. {
  162. // When transactions are nested we only begin/commit/rollback the outermost ones
  163. if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
  164. {
  165. return TRUE;
  166. }
  167. return $this->conn_id->exec('ROLLBACK');
  168. }
  169. // --------------------------------------------------------------------
  170. /**
  171. * Platform-dependant string escape
  172. *
  173. * @param string
  174. * @return string
  175. */
  176. protected function _escape_str($str)
  177. {
  178. return $this->conn_id->escapeString($str);
  179. }
  180. // --------------------------------------------------------------------
  181. /**
  182. * Affected Rows
  183. *
  184. * @return int
  185. */
  186. public function affected_rows()
  187. {
  188. return $this->conn_id->changes();
  189. }
  190. // --------------------------------------------------------------------
  191. /**
  192. * Insert ID
  193. *
  194. * @return int
  195. */
  196. public function insert_id()
  197. {
  198. return $this->conn_id->lastInsertRowID();
  199. }
  200. // --------------------------------------------------------------------
  201. /**
  202. * Show table query
  203. *
  204. * Generates a platform-specific query string so that the table names can be fetched
  205. *
  206. * @param bool $prefix_limit
  207. * @return string
  208. */
  209. protected function _list_tables($prefix_limit = FALSE)
  210. {
  211. return 'SELECT "NAME" FROM "SQLITE_MASTER" WHERE "TYPE" = \'table\''
  212. .(($prefix_limit !== FALSE && $this->dbprefix != '')
  213. ? ' AND "NAME" LIKE \''.$this->escape_like_str($this->dbprefix).'%\' '.sprintf($this->_like_escape_str, $this->_like_escape_chr)
  214. : '');
  215. }
  216. // --------------------------------------------------------------------
  217. /**
  218. * Show column query
  219. *
  220. * Generates a platform-specific query string so that the column names can be fetched
  221. *
  222. * @param string $table
  223. * @return string
  224. */
  225. protected function _list_columns($table = '')
  226. {
  227. // Not supported
  228. return FALSE;
  229. }
  230. // --------------------------------------------------------------------
  231. /**
  232. * Returns an object with field data
  233. *
  234. * @param string $table
  235. * @return array
  236. */
  237. public function field_data($table)
  238. {
  239. if (($query = $this->query('PRAGMA TABLE_INFO('.$this->protect_identifiers($table, TRUE, NULL, FALSE).')')) === FALSE)
  240. {
  241. return FALSE;
  242. }
  243. $query = $query->result_array();
  244. if (empty($query))
  245. {
  246. return FALSE;
  247. }
  248. $retval = array();
  249. for ($i = 0, $c = count($query); $i < $c; $i++)
  250. {
  251. $retval[$i] = new stdClass();
  252. $retval[$i]->name = $query[$i]['name'];
  253. $retval[$i]->type = $query[$i]['type'];
  254. $retval[$i]->max_length = NULL;
  255. $retval[$i]->default = $query[$i]['dflt_value'];
  256. $retval[$i]->primary_key = isset($query[$i]['pk']) ? (int) $query[$i]['pk'] : 0;
  257. }
  258. return $retval;
  259. }
  260. // --------------------------------------------------------------------
  261. /**
  262. * Error
  263. *
  264. * Returns an array containing code and message of the last
  265. * database error that has occured.
  266. *
  267. * @return array
  268. */
  269. public function error()
  270. {
  271. return array('code' => $this->conn_id->lastErrorCode(), 'message' => $this->conn_id->lastErrorMsg());
  272. }
  273. // --------------------------------------------------------------------
  274. /**
  275. * Replace statement
  276. *
  277. * Generates a platform-specific replace string from the supplied data
  278. *
  279. * @param string $table Table name
  280. * @param array $keys INSERT keys
  281. * @param array $values INSERT values
  282. * @return string
  283. */
  284. protected function _replace($table, $keys, $values)
  285. {
  286. return 'INSERT OR '.parent::_replace($table, $keys, $values);
  287. }
  288. // --------------------------------------------------------------------
  289. /**
  290. * Truncate statement
  291. *
  292. * Generates a platform-specific truncate string from the supplied data
  293. *
  294. * If the database does not support the TRUNCATE statement,
  295. * then this method maps to 'DELETE FROM table'
  296. *
  297. * @param string $table
  298. * @return string
  299. */
  300. protected function _truncate($table)
  301. {
  302. return 'DELETE FROM '.$table;
  303. }
  304. // --------------------------------------------------------------------
  305. /**
  306. * Close DB Connection
  307. *
  308. * @return void
  309. */
  310. protected function _close()
  311. {
  312. $this->conn_id->close();
  313. }
  314. }