/system/database/drivers/sqlite/sqlite_forge.php

https://github.com/bwghughes/houghandco · PHP · 234 lines · 117 code · 32 blank · 85 comment · 25 complexity · 2784d6bc002a7805c0368e3a12d05300 MD5 · raw file

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