Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 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 | * NOTICE OF LICENSE |
| 8 | * |
| 9 | * Licensed under the Open Software License version 3.0 |
| 10 | * |
| 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
Andrey Andreev | f20fb98 | 2012-01-24 15:26:01 +0200 | [diff] [blame] | 12 | * bundled with this package in the files license.txt / license.rst. It is |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 13 | * also available through the world wide web at this URL: |
| 14 | * http://opensource.org/licenses/OSL-3.0 |
| 15 | * If you did not receive a copy of the license and are unable to obtain it |
| 16 | * through the world wide web, please send an email to |
| 17 | * licensing@ellislab.com so we can send you a copy immediately. |
| 18 | * |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 19 | * @package CodeIgniter |
| 20 | * @author EllisLab Dev Team |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
| 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 25 | * @filesource |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * SQLite3 Forge Class |
| 30 | * |
| 31 | * @category Database |
| 32 | * @author Andrey Andreev |
| 33 | * @link http://codeigniter.com/user_guide/database/ |
| 34 | */ |
| 35 | class CI_DB_sqlite3_forge extends CI_DB_forge { |
| 36 | |
| 37 | /** |
| 38 | * Create database |
| 39 | * |
| 40 | * @param string the database name |
| 41 | * @return bool |
| 42 | */ |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 43 | public function create_database($db_name = '') |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 44 | { |
| 45 | // In SQLite, a database is created when you connect to the database. |
| 46 | // We'll return TRUE so that an error isn't generated |
| 47 | return TRUE; |
| 48 | } |
| 49 | |
| 50 | // -------------------------------------------------------------------- |
| 51 | |
| 52 | /** |
| 53 | * Drop database |
| 54 | * |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 55 | * @param string the database name (ignored) |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 56 | * @return bool |
| 57 | */ |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 58 | public function drop_database($db_name = '') |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 59 | { |
| 60 | // In SQLite, a database is dropped when we delete a file |
| 61 | if (@file_exists($this->db->database)) |
| 62 | { |
| 63 | // We need to close the pseudo-connection first |
| 64 | $this->db->close(); |
| 65 | if ( ! @unlink($this->db->database)) |
| 66 | { |
| 67 | return $this->db->db_debug ? $this->db->display_error('db_unable_to_drop') : FALSE; |
| 68 | } |
| 69 | |
| 70 | return TRUE; |
| 71 | } |
| 72 | |
| 73 | return $this->db->db_debug ? $this->db->display_error('db_unable_to_drop') : FALSE; |
| 74 | } |
| 75 | |
| 76 | // -------------------------------------------------------------------- |
| 77 | |
| 78 | /** |
| 79 | * Create Table |
| 80 | * |
| 81 | * @param string the table name |
| 82 | * @param array the fields |
| 83 | * @param mixed primary key(s) |
| 84 | * @param mixed key(s) |
| 85 | * @param bool should 'IF NOT EXISTS' be added to the SQL |
| 86 | * @return bool |
| 87 | */ |
Andrey Andreev | a0d4e41 | 2012-04-09 15:06:40 +0300 | [diff] [blame] | 88 | protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists) |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 89 | { |
| 90 | $sql = 'CREATE TABLE '; |
| 91 | |
| 92 | // IF NOT EXISTS added to SQLite in 3.3.0 |
| 93 | if ($if_not_exists === TRUE && version_compare($this->db->version(), '3.3.0', '>=') === TRUE) |
| 94 | { |
| 95 | $sql .= 'IF NOT EXISTS '; |
| 96 | } |
| 97 | |
Andrey Andreev | ea09a8a | 2012-04-06 20:50:07 +0300 | [diff] [blame] | 98 | $sql .= $this->db->escape_identifiers($table).' ('; |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 99 | $current_field_count = 0; |
| 100 | |
Andrey Andreev | ea09a8a | 2012-04-06 20:50:07 +0300 | [diff] [blame] | 101 | foreach ($fields as $field => $attributes) |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 102 | { |
| 103 | // Numeric field names aren't allowed in databases, so if the key is |
| 104 | // numeric, we know it was assigned by PHP and the developer manually |
| 105 | // entered the field information, so we'll simply add it to the list |
| 106 | if (is_numeric($field)) |
| 107 | { |
| 108 | $sql .= "\n\t".$attributes; |
| 109 | } |
| 110 | else |
| 111 | { |
| 112 | $attributes = array_change_key_case($attributes, CASE_UPPER); |
| 113 | |
Andrey Andreev | cb9f361 | 2012-01-26 02:06:48 +0200 | [diff] [blame] | 114 | $sql .= "\n\t".$this->db->protect_identifiers($field) |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 115 | .' '.$attributes['TYPE'] |
Andrey Andreev | fe3428a | 2012-01-26 14:47:29 +0200 | [diff] [blame] | 116 | .( ! empty($attributes['CONSTRAINT']) ? '('.$attributes['CONSTRAINT'].')' : '') |
| 117 | .(( ! empty($attributes['UNSIGNED']) && $attributes['UNSIGNED'] === TRUE) ? ' UNSIGNED' : '') |
Andrey Andreev | 79f6754 | 2012-01-26 14:15:53 +0200 | [diff] [blame] | 118 | .(isset($attributes['DEFAULT']) ? ' DEFAULT \''.$attributes['DEFAULT'].'\'' : '') |
Andrey Andreev | fe3428a | 2012-01-26 14:47:29 +0200 | [diff] [blame] | 119 | .(( ! empty($attributes['NULL']) && $attributes['NULL'] === TRUE) ? ' NULL' : ' NOT NULL') |
| 120 | .(( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE) ? ' AUTO_INCREMENT' : ''); |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | // don't add a comma on the end of the last field |
| 124 | if (++$current_field_count < count($fields)) |
| 125 | { |
| 126 | $sql .= ','; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | if (count($primary_keys) > 0) |
| 131 | { |
Andrey Andreev | cb9f361 | 2012-01-26 02:06:48 +0200 | [diff] [blame] | 132 | $primary_keys = $this->db->protect_identifiers($primary_keys); |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 133 | $sql .= ",\n\tPRIMARY KEY (".implode(', ', $primary_keys).')'; |
| 134 | } |
| 135 | |
| 136 | if (is_array($keys) && count($keys) > 0) |
| 137 | { |
| 138 | foreach ($keys as $key) |
| 139 | { |
| 140 | if (is_array($key)) |
| 141 | { |
Andrey Andreev | cb9f361 | 2012-01-26 02:06:48 +0200 | [diff] [blame] | 142 | $key = $this->db->protect_identifiers($key); |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 143 | } |
| 144 | else |
| 145 | { |
Andrey Andreev | cb9f361 | 2012-01-26 02:06:48 +0200 | [diff] [blame] | 146 | $key = array($this->db->protect_identifiers($key)); |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | $sql .= ",\n\tUNIQUE (".implode(', ', $key).')'; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | return $sql."\n)"; |
| 154 | } |
| 155 | |
| 156 | // -------------------------------------------------------------------- |
| 157 | |
| 158 | /** |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 159 | * Alter table query |
| 160 | * |
| 161 | * Generates a platform-specific query so that a table can be altered |
| 162 | * Called by add_column(), drop_column(), and column_alter(), |
| 163 | * |
| 164 | * @param string the ALTER type (ADD, DROP, CHANGE) |
| 165 | * @param string the column name |
| 166 | * @param string the table name |
| 167 | * @param string the column definition |
| 168 | * @param string the default value |
| 169 | * @param bool should 'NOT NULL' be added |
| 170 | * @param string the field after which we should add the new field |
| 171 | * @return string |
| 172 | */ |
Andrey Andreev | a0d4e41 | 2012-04-09 15:06:40 +0300 | [diff] [blame] | 173 | protected function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '') |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 174 | { |
| 175 | /* SQLite only supports adding new columns and it does |
| 176 | * NOT support the AFTER statement. Each new column will |
| 177 | * be added as the last one in the table. |
| 178 | */ |
| 179 | if ($alter_type !== 'ADD COLUMN') |
| 180 | { |
| 181 | // Not supported |
| 182 | return FALSE; |
| 183 | } |
| 184 | |
Andrey Andreev | cb9f361 | 2012-01-26 02:06:48 +0200 | [diff] [blame] | 185 | return 'ALTER TABLE '.$this->db->protect_identifiers($table).' '.$alter_type.' '.$this->db->protect_identifiers($column_name) |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 186 | .' '.$column_definition |
| 187 | .($default_value != '' ? ' DEFAULT '.$default_value : '') |
| 188 | // If NOT NULL is specified, the field must have a DEFAULT value other than NULL |
| 189 | .(($null !== NULL && $default_value !== 'NULL') ? ' NOT NULL' : ' NULL'); |
| 190 | } |
| 191 | |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | /* End of file sqlite3_forge.php */ |
Andrey Andreev | f944d3b | 2012-03-20 22:12:55 +0200 | [diff] [blame] | 195 | /* Location: ./system/database/drivers/sqlite3/sqlite3_forge.php */ |