pdo_sqlite_forge.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 Forge Class
  41. *
  42. * @category Database
  43. * @author EllisLab Dev Team
  44. * @link https://codeigniter.com/user_guide/database/
  45. */
  46. class CI_DB_pdo_sqlite_forge extends CI_DB_pdo_forge {
  47. /**
  48. * CREATE TABLE IF statement
  49. *
  50. * @var string
  51. */
  52. protected $_create_table_if = 'CREATE TABLE IF NOT EXISTS';
  53. /**
  54. * DROP TABLE IF statement
  55. *
  56. * @var string
  57. */
  58. protected $_drop_table_if = 'DROP TABLE IF EXISTS';
  59. /**
  60. * UNSIGNED support
  61. *
  62. * @var bool|array
  63. */
  64. protected $_unsigned = FALSE;
  65. /**
  66. * NULL value representation in CREATE/ALTER TABLE statements
  67. *
  68. * @var string
  69. */
  70. protected $_null = 'NULL';
  71. // --------------------------------------------------------------------
  72. /**
  73. * Class constructor
  74. *
  75. * @param object &$db Database object
  76. * @return void
  77. */
  78. public function __construct(&$db)
  79. {
  80. parent::__construct($db);
  81. if (version_compare($this->db->version(), '3.3', '<'))
  82. {
  83. $this->_create_table_if = FALSE;
  84. $this->_drop_table_if = FALSE;
  85. }
  86. }
  87. // --------------------------------------------------------------------
  88. /**
  89. * Create database
  90. *
  91. * @param string $db_name (ignored)
  92. * @return bool
  93. */
  94. public function create_database($db_name)
  95. {
  96. // In SQLite, a database is created when you connect to the database.
  97. // We'll return TRUE so that an error isn't generated
  98. return TRUE;
  99. }
  100. // --------------------------------------------------------------------
  101. /**
  102. * Drop database
  103. *
  104. * @param string $db_name (ignored)
  105. * @return bool
  106. */
  107. public function drop_database($db_name)
  108. {
  109. // In SQLite, a database is dropped when we delete a file
  110. if (file_exists($this->db->database))
  111. {
  112. // We need to close the pseudo-connection first
  113. $this->db->close();
  114. if ( ! @unlink($this->db->database))
  115. {
  116. return $this->db->db_debug ? $this->db->display_error('db_unable_to_drop') : FALSE;
  117. }
  118. elseif ( ! empty($this->db->data_cache['db_names']))
  119. {
  120. $key = array_search(strtolower($this->db->database), array_map('strtolower', $this->db->data_cache['db_names']), TRUE);
  121. if ($key !== FALSE)
  122. {
  123. unset($this->db->data_cache['db_names'][$key]);
  124. }
  125. }
  126. return TRUE;
  127. }
  128. return $this->db->db_debug ? $this->db->display_error('db_unable_to_drop') : FALSE;
  129. }
  130. // --------------------------------------------------------------------
  131. /**
  132. * ALTER TABLE
  133. *
  134. * @param string $alter_type ALTER type
  135. * @param string $table Table name
  136. * @param mixed $field Column definition
  137. * @return string|string[]
  138. */
  139. protected function _alter_table($alter_type, $table, $field)
  140. {
  141. if ($alter_type === 'DROP' OR $alter_type === 'CHANGE')
  142. {
  143. // drop_column():
  144. // BEGIN TRANSACTION;
  145. // CREATE TEMPORARY TABLE t1_backup(a,b);
  146. // INSERT INTO t1_backup SELECT a,b FROM t1;
  147. // DROP TABLE t1;
  148. // CREATE TABLE t1(a,b);
  149. // INSERT INTO t1 SELECT a,b FROM t1_backup;
  150. // DROP TABLE t1_backup;
  151. // COMMIT;
  152. return FALSE;
  153. }
  154. return parent::_alter_table($alter_type, $table, $field);
  155. }
  156. // --------------------------------------------------------------------
  157. /**
  158. * Process column
  159. *
  160. * @param array $field
  161. * @return string
  162. */
  163. protected function _process_column($field)
  164. {
  165. return $this->db->escape_identifiers($field['name'])
  166. .' '.$field['type']
  167. .$field['auto_increment']
  168. .$field['null']
  169. .$field['unique']
  170. .$field['default'];
  171. }
  172. // --------------------------------------------------------------------
  173. /**
  174. * Field attribute TYPE
  175. *
  176. * Performs a data type mapping between different databases.
  177. *
  178. * @param array &$attributes
  179. * @return void
  180. */
  181. protected function _attr_type(&$attributes)
  182. {
  183. switch (strtoupper($attributes['TYPE']))
  184. {
  185. case 'ENUM':
  186. case 'SET':
  187. $attributes['TYPE'] = 'TEXT';
  188. return;
  189. default: return;
  190. }
  191. }
  192. // --------------------------------------------------------------------
  193. /**
  194. * Field attribute AUTO_INCREMENT
  195. *
  196. * @param array &$attributes
  197. * @param array &$field
  198. * @return void
  199. */
  200. protected function _attr_auto_increment(&$attributes, &$field)
  201. {
  202. if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE && stripos($field['type'], 'int') !== FALSE)
  203. {
  204. $field['type'] = 'INTEGER PRIMARY KEY';
  205. $field['default'] = '';
  206. $field['null'] = '';
  207. $field['unique'] = '';
  208. $field['auto_increment'] = ' AUTOINCREMENT';
  209. $this->primary_keys = array();
  210. }
  211. }
  212. }