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.

219 lines
5.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 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. // Is there a cached result?
  117. if (isset($this->data_cache['field_names'][$table]))
  118. {
  119. return $this->data_cache['field_names'][$table];
  120. }
  121. if (($result = $this->query('PRAGMA TABLE_INFO('.$this->protect_identifiers($table, TRUE, NULL, FALSE).')')) === FALSE)
  122. {
  123. return FALSE;
  124. }
  125. $this->data_cache['field_names'][$table] = array();
  126. foreach ($result->result_array() as $row)
  127. {
  128. $this->data_cache['field_names'][$table][] = $row['name'];
  129. }
  130. return $this->data_cache['field_names'][$table];
  131. }
  132. // --------------------------------------------------------------------
  133. /**
  134. * Returns an object with field data
  135. *
  136. * @param string $table
  137. * @return array
  138. */
  139. public function field_data($table)
  140. {
  141. if (($query = $this->query('PRAGMA TABLE_INFO('.$this->protect_identifiers($table, TRUE, NULL, FALSE).')')) === FALSE)
  142. {
  143. return FALSE;
  144. }
  145. $query = $query->result_array();
  146. if (empty($query))
  147. {
  148. return FALSE;
  149. }
  150. $retval = array();
  151. for ($i = 0, $c = count($query); $i < $c; $i++)
  152. {
  153. $retval[$i] = new stdClass();
  154. $retval[$i]->name = $query[$i]['name'];
  155. $retval[$i]->type = $query[$i]['type'];
  156. $retval[$i]->max_length = NULL;
  157. $retval[$i]->default = $query[$i]['dflt_value'];
  158. $retval[$i]->primary_key = isset($query[$i]['pk']) ? (int) $query[$i]['pk'] : 0;
  159. }
  160. return $retval;
  161. }
  162. // --------------------------------------------------------------------
  163. /**
  164. * Replace statement
  165. *
  166. * @param string $table Table name
  167. * @param array $keys INSERT keys
  168. * @param array $values INSERT values
  169. * @return string
  170. */
  171. protected function _replace($table, $keys, $values)
  172. {
  173. return 'INSERT OR '.parent::_replace($table, $keys, $values);
  174. }
  175. // --------------------------------------------------------------------
  176. /**
  177. * Truncate statement
  178. *
  179. * Generates a platform-specific truncate string from the supplied data
  180. *
  181. * If the database does not support the TRUNCATE statement,
  182. * then this method maps to 'DELETE FROM table'
  183. *
  184. * @param string $table
  185. * @return string
  186. */
  187. protected function _truncate($table)
  188. {
  189. return 'DELETE FROM '.$table;
  190. }
  191. }