pdo_sqlite_driver.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. * PDO 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_pdo_sqlite_driver extends CI_DB_pdo_driver {
  53. /**
  54. * Sub-driver
  55. *
  56. * @var string
  57. */
  58. public $subdriver = 'sqlite';
  59. // --------------------------------------------------------------------
  60. /**
  61. * ORDER BY random keyword
  62. *
  63. * @var array
  64. */
  65. protected $_random_keyword = array('RANDOM()', 'RANDOM()');
  66. // --------------------------------------------------------------------
  67. /**
  68. * Class constructor
  69. *
  70. * Builds the DSN if not already set.
  71. *
  72. * @param array $params
  73. * @return void
  74. */
  75. public function __construct($params)
  76. {
  77. parent::__construct($params);
  78. if (empty($this->dsn))
  79. {
  80. $this->dsn = 'sqlite:';
  81. if (empty($this->database) && empty($this->hostname))
  82. {
  83. $this->database = ':memory:';
  84. }
  85. $this->database = empty($this->database) ? $this->hostname : $this->database;
  86. }
  87. }
  88. // --------------------------------------------------------------------
  89. /**
  90. * Show table query
  91. *
  92. * Generates a platform-specific query string so that the table names can be fetched
  93. *
  94. * @param bool $prefix_limit
  95. * @return string
  96. */
  97. protected function _list_tables($prefix_limit = FALSE)
  98. {
  99. $sql = 'SELECT "NAME" FROM "SQLITE_MASTER" WHERE "TYPE" = \'table\'';
  100. if ($prefix_limit === TRUE && $this->dbprefix !== '')
  101. {
  102. return $sql.' AND "NAME" LIKE \''.$this->escape_like_str($this->dbprefix)."%' "
  103. .sprintf($this->_like_escape_str, $this->_like_escape_chr);
  104. }
  105. return $sql;
  106. }
  107. // --------------------------------------------------------------------
  108. /**
  109. * Fetch Field Names
  110. *
  111. * @param string $table Table name
  112. * @return array
  113. */
  114. public function list_fields($table)
  115. {
  116. if (($result = $this->query('PRAGMA TABLE_INFO('.$this->protect_identifiers($table, TRUE, NULL, FALSE).')')) === FALSE)
  117. {
  118. return FALSE;
  119. }
  120. $fields = array();
  121. foreach ($result->result_array() as $row)
  122. {
  123. $fields[] = $row['name'];
  124. }
  125. return $fields;
  126. }
  127. // --------------------------------------------------------------------
  128. /**
  129. * Returns an object with field data
  130. *
  131. * @param string $table
  132. * @return array
  133. */
  134. public function field_data($table)
  135. {
  136. if (($query = $this->query('PRAGMA TABLE_INFO('.$this->protect_identifiers($table, TRUE, NULL, FALSE).')')) === FALSE)
  137. {
  138. return FALSE;
  139. }
  140. $query = $query->result_array();
  141. if (empty($query))
  142. {
  143. return FALSE;
  144. }
  145. $retval = array();
  146. for ($i = 0, $c = count($query); $i < $c; $i++)
  147. {
  148. $retval[$i] = new stdClass();
  149. $retval[$i]->name = $query[$i]['name'];
  150. $retval[$i]->type = $query[$i]['type'];
  151. $retval[$i]->max_length = NULL;
  152. $retval[$i]->default = $query[$i]['dflt_value'];
  153. $retval[$i]->primary_key = isset($query[$i]['pk']) ? (int) $query[$i]['pk'] : 0;
  154. }
  155. return $retval;
  156. }
  157. // --------------------------------------------------------------------
  158. /**
  159. * Replace statement
  160. *
  161. * @param string $table Table name
  162. * @param array $keys INSERT keys
  163. * @param array $values INSERT values
  164. * @return string
  165. */
  166. protected function _replace($table, $keys, $values)
  167. {
  168. return 'INSERT OR '.parent::_replace($table, $keys, $values);
  169. }
  170. // --------------------------------------------------------------------
  171. /**
  172. * Truncate statement
  173. *
  174. * Generates a platform-specific truncate string from the supplied data
  175. *
  176. * If the database does not support the TRUNCATE statement,
  177. * then this method maps to 'DELETE FROM table'
  178. *
  179. * @param string $table
  180. * @return string
  181. */
  182. protected function _truncate($table)
  183. {
  184. return 'DELETE FROM '.$table;
  185. }
  186. }