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 | } |
Andrey Andreev | 5d28176 | 2012-06-11 22:05:40 +0300 | [diff] [blame^] | 69 | elseif ( ! empty($this->db->data_cache['db_names'])) |
| 70 | { |
| 71 | $key = array_search(strtolower($this->db->database), array_map('strtolower', $this->db->data_cache['db_names']), TRUE); |
| 72 | if ($key !== FALSE) |
| 73 | { |
| 74 | unset($this->db->data_cache['db_names'][$key]); |
| 75 | } |
| 76 | } |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 77 | |
| 78 | return TRUE; |
| 79 | } |
| 80 | |
| 81 | return $this->db->db_debug ? $this->db->display_error('db_unable_to_drop') : FALSE; |
| 82 | } |
| 83 | |
| 84 | // -------------------------------------------------------------------- |
| 85 | |
| 86 | /** |
| 87 | * Create Table |
| 88 | * |
| 89 | * @param string the table name |
| 90 | * @param array the fields |
| 91 | * @param mixed primary key(s) |
| 92 | * @param mixed key(s) |
| 93 | * @param bool should 'IF NOT EXISTS' be added to the SQL |
| 94 | * @return bool |
| 95 | */ |
Andrey Andreev | a0d4e41 | 2012-04-09 15:06:40 +0300 | [diff] [blame] | 96 | protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists) |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 97 | { |
| 98 | $sql = 'CREATE TABLE '; |
| 99 | |
| 100 | // IF NOT EXISTS added to SQLite in 3.3.0 |
| 101 | if ($if_not_exists === TRUE && version_compare($this->db->version(), '3.3.0', '>=') === TRUE) |
| 102 | { |
| 103 | $sql .= 'IF NOT EXISTS '; |
| 104 | } |
| 105 | |
Andrey Andreev | ea09a8a | 2012-04-06 20:50:07 +0300 | [diff] [blame] | 106 | $sql .= $this->db->escape_identifiers($table).' ('; |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 107 | $current_field_count = 0; |
| 108 | |
Andrey Andreev | ea09a8a | 2012-04-06 20:50:07 +0300 | [diff] [blame] | 109 | foreach ($fields as $field => $attributes) |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 110 | { |
| 111 | // Numeric field names aren't allowed in databases, so if the key is |
| 112 | // numeric, we know it was assigned by PHP and the developer manually |
| 113 | // entered the field information, so we'll simply add it to the list |
| 114 | if (is_numeric($field)) |
| 115 | { |
| 116 | $sql .= "\n\t".$attributes; |
| 117 | } |
| 118 | else |
| 119 | { |
| 120 | $attributes = array_change_key_case($attributes, CASE_UPPER); |
| 121 | |
Andrey Andreev | 5ef194d | 2012-06-08 01:12:54 +0300 | [diff] [blame] | 122 | $sql .= "\n\t".$this->db->escape_identifiers($field).' '.$attributes['TYPE']; |
| 123 | |
| 124 | empty($attributes['CONSTRAINT']) OR $sql .= '('.$attributes['CONSTRAINT'].')'; |
| 125 | |
| 126 | if ( ! empty($attributes['UNSIGNED']) && $attributes['UNSIGNED'] === TRUE) |
| 127 | { |
| 128 | $sql .= ' UNSIGNED'; |
| 129 | } |
| 130 | |
| 131 | if (isset($attributes['DEFAULT'])) |
| 132 | { |
| 133 | $sql .= " DEFAULT '".$attributes['DEFAULT']."'"; |
| 134 | } |
| 135 | |
| 136 | $sql .= ( ! empty($attributes['NULL']) && $attributes['NULL'] === TRUE) |
| 137 | ? ' NULL' : ' NOT NULL'; |
| 138 | |
| 139 | if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE) |
| 140 | { |
| 141 | $sql .= ' AUTO_INCREMENT'; |
| 142 | } |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | // don't add a comma on the end of the last field |
| 146 | if (++$current_field_count < count($fields)) |
| 147 | { |
| 148 | $sql .= ','; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | if (count($primary_keys) > 0) |
| 153 | { |
Andrey Andreev | 9637b40 | 2012-06-08 15:39:24 +0300 | [diff] [blame] | 154 | $sql .= ",\n\tPRIMARY KEY (".implode(', ', $this->db->escape_identifiers($primary_keys)).')'; |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | if (is_array($keys) && count($keys) > 0) |
| 158 | { |
| 159 | foreach ($keys as $key) |
| 160 | { |
Andrey Andreev | 5ef194d | 2012-06-08 01:12:54 +0300 | [diff] [blame] | 161 | $key = is_array($key) |
Andrey Andreev | 9637b40 | 2012-06-08 15:39:24 +0300 | [diff] [blame] | 162 | ? $this->db->escape_identifiers($key) |
Andrey Andreev | 5ef194d | 2012-06-08 01:12:54 +0300 | [diff] [blame] | 163 | : array($this->db->escape_identifiers($key)); |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 164 | |
| 165 | $sql .= ",\n\tUNIQUE (".implode(', ', $key).')'; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | return $sql."\n)"; |
| 170 | } |
| 171 | |
| 172 | // -------------------------------------------------------------------- |
| 173 | |
| 174 | /** |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 175 | * Alter table query |
| 176 | * |
| 177 | * Generates a platform-specific query so that a table can be altered |
| 178 | * Called by add_column(), drop_column(), and column_alter(), |
| 179 | * |
| 180 | * @param string the ALTER type (ADD, DROP, CHANGE) |
| 181 | * @param string the column name |
| 182 | * @param string the table name |
| 183 | * @param string the column definition |
| 184 | * @param string the default value |
| 185 | * @param bool should 'NOT NULL' be added |
| 186 | * @param string the field after which we should add the new field |
| 187 | * @return string |
| 188 | */ |
Andrey Andreev | a0d4e41 | 2012-04-09 15:06:40 +0300 | [diff] [blame] | 189 | 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] | 190 | { |
| 191 | /* SQLite only supports adding new columns and it does |
| 192 | * NOT support the AFTER statement. Each new column will |
| 193 | * be added as the last one in the table. |
| 194 | */ |
| 195 | if ($alter_type !== 'ADD COLUMN') |
| 196 | { |
| 197 | // Not supported |
| 198 | return FALSE; |
| 199 | } |
| 200 | |
Andrey Andreev | 5ef194d | 2012-06-08 01:12:54 +0300 | [diff] [blame] | 201 | return 'ALTER TABLE '.$this->db->escape_identifiers($table).' '.$alter_type.' '.$this->db->escape_identifiers($column_name) |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 202 | .' '.$column_definition |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 203 | .($default_value !== '' ? ' DEFAULT '.$default_value : '') |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 204 | // If NOT NULL is specified, the field must have a DEFAULT value other than NULL |
| 205 | .(($null !== NULL && $default_value !== 'NULL') ? ' NOT NULL' : ' NULL'); |
| 206 | } |
| 207 | |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /* End of file sqlite3_forge.php */ |
Andrey Andreev | f944d3b | 2012-03-20 22:12:55 +0200 | [diff] [blame] | 211 | /* Location: ./system/database/drivers/sqlite3/sqlite3_forge.php */ |