blob: 6a76ba929a20c802ac82ed8ff9e864334de36e75 [file] [log] [blame]
Andrey Andreev8ae24c52012-01-16 13:05:23 +02001<?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 Andreevf20fb982012-01-24 15:26:01 +020012 * bundled with this package in the files license.txt / license.rst. It is
Andrey Andreev8ae24c52012-01-16 13:05:23 +020013 * 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 Andreevd947eba2012-04-09 14:58:28 +030019 * @package CodeIgniter
20 * @author EllisLab Dev Team
Andrey Andreev8ae24c52012-01-16 13:05:23 +020021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevd947eba2012-04-09 14:58:28 +030022 * @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 Andreev8ae24c52012-01-16 13:05:23 +020025 * @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 */
35class 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 Andreevd947eba2012-04-09 14:58:28 +030043 public function create_database($db_name = '')
Andrey Andreev8ae24c52012-01-16 13:05:23 +020044 {
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 Andreevd947eba2012-04-09 14:58:28 +030055 * @param string the database name (ignored)
Andrey Andreev8ae24c52012-01-16 13:05:23 +020056 * @return bool
57 */
Andrey Andreevd947eba2012-04-09 14:58:28 +030058 public function drop_database($db_name = '')
Andrey Andreev8ae24c52012-01-16 13:05:23 +020059 {
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 Andreev5d281762012-06-11 22:05:40 +030069 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 Andreev8ae24c52012-01-16 13:05:23 +020077
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 Andreeva0d4e412012-04-09 15:06:40 +030096 protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
Andrey Andreev8ae24c52012-01-16 13:05:23 +020097 {
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 Andreevea09a8a2012-04-06 20:50:07 +0300106 $sql .= $this->db->escape_identifiers($table).' (';
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200107 $current_field_count = 0;
108
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300109 foreach ($fields as $field => $attributes)
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200110 {
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 Andreev5ef194d2012-06-08 01:12:54 +0300122 $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 Andreev8ae24c52012-01-16 13:05:23 +0200143 }
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 Andreev9637b402012-06-08 15:39:24 +0300154 $sql .= ",\n\tPRIMARY KEY (".implode(', ', $this->db->escape_identifiers($primary_keys)).')';
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200155 }
156
157 if (is_array($keys) && count($keys) > 0)
158 {
159 foreach ($keys as $key)
160 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300161 $key = is_array($key)
Andrey Andreev9637b402012-06-08 15:39:24 +0300162 ? $this->db->escape_identifiers($key)
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300163 : array($this->db->escape_identifiers($key));
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200164
165 $sql .= ",\n\tUNIQUE (".implode(', ', $key).')';
166 }
167 }
168
169 return $sql."\n)";
170 }
171
172 // --------------------------------------------------------------------
173
174 /**
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200175 * 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 Andreeva0d4e412012-04-09 15:06:40 +0300189 protected function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200190 {
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 Andreev5ef194d2012-06-08 01:12:54 +0300201 return 'ALTER TABLE '.$this->db->escape_identifiers($table).' '.$alter_type.' '.$this->db->escape_identifiers($column_name)
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200202 .' '.$column_definition
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100203 .($default_value !== '' ? ' DEFAULT '.$default_value : '')
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200204 // 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 Andreev8ae24c52012-01-16 13:05:23 +0200208}
209
210/* End of file sqlite3_forge.php */
Andrey Andreevf944d3b2012-03-20 22:12:55 +0200211/* Location: ./system/database/drivers/sqlite3/sqlite3_forge.php */