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.

205 lines
5.3 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 1.3.0
  36. * @filesource
  37. */
  38. defined('BASEPATH') OR exit('No direct script access allowed');
  39. /**
  40. * 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_sqlite_forge extends CI_DB_forge {
  47. /**
  48. * CREATE TABLE IF statement
  49. *
  50. * @var string
  51. */
  52. protected $_create_table_if = FALSE;
  53. /**
  54. * UNSIGNED support
  55. *
  56. * @var bool|array
  57. */
  58. protected $_unsigned = FALSE;
  59. /**
  60. * NULL value representation in CREATE/ALTER TABLE statements
  61. *
  62. * @var string
  63. */
  64. protected $_null = 'NULL';
  65. // --------------------------------------------------------------------
  66. /**
  67. * Create database
  68. *
  69. * @param string $db_name (ignored)
  70. * @return bool
  71. */
  72. public function create_database($db_name)
  73. {
  74. // In SQLite, a database is created when you connect to the database.
  75. // We'll return TRUE so that an error isn't generated
  76. return TRUE;
  77. }
  78. // --------------------------------------------------------------------
  79. /**
  80. * Drop database
  81. *
  82. * @param string $db_name (ignored)
  83. * @return bool
  84. */
  85. public function drop_database($db_name)
  86. {
  87. if ( ! file_exists($this->db->database) OR ! @unlink($this->db->database))
  88. {
  89. return ($this->db->db_debug) ? $this->db->display_error('db_unable_to_drop') : FALSE;
  90. }
  91. elseif ( ! empty($this->db->data_cache['db_names']))
  92. {
  93. $key = array_search(strtolower($this->db->database), array_map('strtolower', $this->db->data_cache['db_names']), TRUE);
  94. if ($key !== FALSE)
  95. {
  96. unset($this->db->data_cache['db_names'][$key]);
  97. }
  98. }
  99. return TRUE;
  100. }
  101. // --------------------------------------------------------------------
  102. /**
  103. * ALTER TABLE
  104. *
  105. * @todo implement drop_column(), modify_column()
  106. * @param string $alter_type ALTER type
  107. * @param string $table Table name
  108. * @param mixed $field Column definition
  109. * @return string|string[]
  110. */
  111. protected function _alter_table($alter_type, $table, $field)
  112. {
  113. if ($alter_type === 'DROP' OR $alter_type === 'CHANGE')
  114. {
  115. // drop_column():
  116. // BEGIN TRANSACTION;
  117. // CREATE TEMPORARY TABLE t1_backup(a,b);
  118. // INSERT INTO t1_backup SELECT a,b FROM t1;
  119. // DROP TABLE t1;
  120. // CREATE TABLE t1(a,b);
  121. // INSERT INTO t1 SELECT a,b FROM t1_backup;
  122. // DROP TABLE t1_backup;
  123. // COMMIT;
  124. return FALSE;
  125. }
  126. return parent::_alter_table($alter_type, $table, $field);
  127. }
  128. // --------------------------------------------------------------------
  129. /**
  130. * Process column
  131. *
  132. * @param array $field
  133. * @return string
  134. */
  135. protected function _process_column($field)
  136. {
  137. return $this->db->escape_identifiers($field['name'])
  138. .' '.$field['type']
  139. .$field['auto_increment']
  140. .$field['null']
  141. .$field['unique']
  142. .$field['default'];
  143. }
  144. // --------------------------------------------------------------------
  145. /**
  146. * Field attribute TYPE
  147. *
  148. * Performs a data type mapping between different databases.
  149. *
  150. * @param array &$attributes
  151. * @return void
  152. */
  153. protected function _attr_type(&$attributes)
  154. {
  155. switch (strtoupper($attributes['TYPE']))
  156. {
  157. case 'ENUM':
  158. case 'SET':
  159. $attributes['TYPE'] = 'TEXT';
  160. return;
  161. default: return;
  162. }
  163. }
  164. // --------------------------------------------------------------------
  165. /**
  166. * Field attribute AUTO_INCREMENT
  167. *
  168. * @param array &$attributes
  169. * @param array &$field
  170. * @return void
  171. */
  172. protected function _attr_auto_increment(&$attributes, &$field)
  173. {
  174. if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE && stripos($field['type'], 'int') !== FALSE)
  175. {
  176. $field['type'] = 'INTEGER PRIMARY KEY';
  177. $field['default'] = '';
  178. $field['null'] = '';
  179. $field['unique'] = '';
  180. $field['auto_increment'] = ' AUTOINCREMENT';
  181. $this->primary_keys = array();
  182. }
  183. }
  184. }