sqlite_forge.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.1.6 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author EllisLab Dev Team
  9. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc.
  10. * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
  11. * @license http://codeigniter.com/user_guide/license.html
  12. * @link http://codeigniter.com
  13. * @since Version 1.0
  14. * @filesource
  15. */
  16. // ------------------------------------------------------------------------
  17. /**
  18. * SQLite Forge Class
  19. *
  20. * @category Database
  21. * @author EllisLab Dev Team
  22. * @link http://codeigniter.com/user_guide/database/
  23. */
  24. class CI_DB_sqlite_forge extends CI_DB_forge {
  25. /**
  26. * Create database
  27. *
  28. * @access public
  29. * @param string the database name
  30. * @return bool
  31. */
  32. function _create_database()
  33. {
  34. // In SQLite, a database is created when you connect to the database.
  35. // We'll return TRUE so that an error isn't generated
  36. return TRUE;
  37. }
  38. // --------------------------------------------------------------------
  39. /**
  40. * Drop database
  41. *
  42. * @access private
  43. * @param string the database name
  44. * @return bool
  45. */
  46. function _drop_database($name)
  47. {
  48. if ( ! @file_exists($this->db->database) OR ! @unlink($this->db->database))
  49. {
  50. if ($this->db->db_debug)
  51. {
  52. return $this->db->display_error('db_unable_to_drop');
  53. }
  54. return FALSE;
  55. }
  56. return TRUE;
  57. }
  58. // --------------------------------------------------------------------
  59. /**
  60. * Create Table
  61. *
  62. * @access private
  63. * @param string the table name
  64. * @param array the fields
  65. * @param mixed primary key(s)
  66. * @param mixed key(s)
  67. * @param boolean should 'IF NOT EXISTS' be added to the SQL
  68. * @return bool
  69. */
  70. function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
  71. {
  72. $sql = 'CREATE TABLE ';
  73. // IF NOT EXISTS added to SQLite in 3.3.0
  74. if ($if_not_exists === TRUE && version_compare($this->db->_version(), '3.3.0', '>=') === TRUE)
  75. {
  76. $sql .= 'IF NOT EXISTS ';
  77. }
  78. $sql .= $this->db->_escape_identifiers($table)."(";
  79. $current_field_count = 0;
  80. foreach ($fields as $field=>$attributes)
  81. {
  82. // Numeric field names aren't allowed in databases, so if the key is
  83. // numeric, we know it was assigned by PHP and the developer manually
  84. // entered the field information, so we'll simply add it to the list
  85. if (is_numeric($field))
  86. {
  87. $sql .= "\n\t$attributes";
  88. }
  89. else
  90. {
  91. $attributes = array_change_key_case($attributes, CASE_UPPER);
  92. $sql .= "\n\t".$this->db->_protect_identifiers($field);
  93. $sql .= ' '.$attributes['TYPE'];
  94. if (array_key_exists('CONSTRAINT', $attributes))
  95. {
  96. $sql .= '('.$attributes['CONSTRAINT'].')';
  97. }
  98. if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
  99. {
  100. $sql .= ' UNSIGNED';
  101. }
  102. if (array_key_exists('DEFAULT', $attributes))
  103. {
  104. $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
  105. }
  106. if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
  107. {
  108. $sql .= ' NULL';
  109. }
  110. else
  111. {
  112. $sql .= ' NOT NULL';
  113. }
  114. if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
  115. {
  116. $sql .= ' AUTO_INCREMENT';
  117. }
  118. }
  119. // don't add a comma on the end of the last field
  120. if (++$current_field_count < count($fields))
  121. {
  122. $sql .= ',';
  123. }
  124. }
  125. if (count($primary_keys) > 0)
  126. {
  127. $primary_keys = $this->db->_protect_identifiers($primary_keys);
  128. $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";
  129. }
  130. if (is_array($keys) && count($keys) > 0)
  131. {
  132. foreach ($keys as $key)
  133. {
  134. if (is_array($key))
  135. {
  136. $key = $this->db->_protect_identifiers($key);
  137. }
  138. else
  139. {
  140. $key = array($this->db->_protect_identifiers($key));
  141. }
  142. $sql .= ",\n\tUNIQUE (" . implode(', ', $key) . ")";
  143. }
  144. }
  145. $sql .= "\n)";
  146. return $sql;
  147. }
  148. // --------------------------------------------------------------------
  149. /**
  150. * Drop Table
  151. *
  152. * Unsupported feature in SQLite
  153. *
  154. * @access private
  155. * @return bool
  156. */
  157. function _drop_table($table)
  158. {
  159. if ($this->db->db_debug)
  160. {
  161. return $this->db->display_error('db_unsuported_feature');
  162. }
  163. return array();
  164. }
  165. // --------------------------------------------------------------------
  166. /**
  167. * Alter table query
  168. *
  169. * Generates a platform-specific query so that a table can be altered
  170. * Called by add_column(), drop_column(), and column_alter(),
  171. *
  172. * @access private
  173. * @param string the ALTER type (ADD, DROP, CHANGE)
  174. * @param string the column name
  175. * @param string the table name
  176. * @param string the column definition
  177. * @param string the default value
  178. * @param boolean should 'NOT NULL' be added
  179. * @param string the field after which we should add the new field
  180. * @return object
  181. */
  182. function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
  183. {
  184. $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ".$this->db->_protect_identifiers($column_name);
  185. // DROP has everything it needs now.
  186. if ($alter_type == 'DROP')
  187. {
  188. // SQLite does not support dropping columns
  189. // http://www.sqlite.org/omitted.html
  190. // http://www.sqlite.org/faq.html#q11
  191. return FALSE;
  192. }
  193. $sql .= " $column_definition";
  194. if ($default_value != '')
  195. {
  196. $sql .= " DEFAULT \"$default_value\"";
  197. }
  198. if ($null === NULL)
  199. {
  200. $sql .= ' NULL';
  201. }
  202. else
  203. {
  204. $sql .= ' NOT NULL';
  205. }
  206. if ($after_field != '')
  207. {
  208. $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);
  209. }
  210. return $sql;
  211. }
  212. // --------------------------------------------------------------------
  213. /**
  214. * Rename a table
  215. *
  216. * Generates a platform-specific query so that a table can be renamed
  217. *
  218. * @access private
  219. * @param string the old table name
  220. * @param string the new table name
  221. * @return string
  222. */
  223. function _rename_table($table_name, $new_table_name)
  224. {
  225. $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name)." RENAME TO ".$this->db->_protect_identifiers($new_table_name);
  226. return $sql;
  227. }
  228. }
  229. /* End of file sqlite_forge.php */
  230. /* Location: ./system/database/drivers/sqlite/sqlite_forge.php */