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.

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