blob: e1dd3fa91fb1d9e5bd60a2ffb577582d33abf3c5 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Andrey Andreev8ae24c52012-01-16 13:05:23 +02002/**
3 * CodeIgniter
4 *
vlakofffe832492012-07-13 20:40:06 +02005 * An open source application development framework for PHP 5.2.4 or newer
Andrey Andreev8ae24c52012-01-16 13:05:23 +02006 *
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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Andrey Andreev8ae24c52012-01-16 13:05:23 +020028
29/**
30 * SQLite3 Forge Class
31 *
32 * @category Database
33 * @author Andrey Andreev
34 * @link http://codeigniter.com/user_guide/database/
35 */
36class CI_DB_sqlite3_forge extends CI_DB_forge {
37
38 /**
39 * Create database
40 *
41 * @param string the database name
42 * @return bool
43 */
Andrey Andreevd947eba2012-04-09 14:58:28 +030044 public function create_database($db_name = '')
Andrey Andreev8ae24c52012-01-16 13:05:23 +020045 {
46 // In SQLite, a database is created when you connect to the database.
47 // We'll return TRUE so that an error isn't generated
48 return TRUE;
49 }
50
51 // --------------------------------------------------------------------
52
53 /**
54 * Drop database
55 *
Andrey Andreevd947eba2012-04-09 14:58:28 +030056 * @param string the database name (ignored)
Andrey Andreev8ae24c52012-01-16 13:05:23 +020057 * @return bool
58 */
Andrey Andreevd947eba2012-04-09 14:58:28 +030059 public function drop_database($db_name = '')
Andrey Andreev8ae24c52012-01-16 13:05:23 +020060 {
61 // In SQLite, a database is dropped when we delete a file
62 if (@file_exists($this->db->database))
63 {
64 // We need to close the pseudo-connection first
65 $this->db->close();
66 if ( ! @unlink($this->db->database))
67 {
68 return $this->db->db_debug ? $this->db->display_error('db_unable_to_drop') : FALSE;
69 }
Andrey Andreev5d281762012-06-11 22:05:40 +030070 elseif ( ! empty($this->db->data_cache['db_names']))
71 {
72 $key = array_search(strtolower($this->db->database), array_map('strtolower', $this->db->data_cache['db_names']), TRUE);
73 if ($key !== FALSE)
74 {
75 unset($this->db->data_cache['db_names'][$key]);
76 }
77 }
Andrey Andreev8ae24c52012-01-16 13:05:23 +020078
79 return TRUE;
80 }
81
82 return $this->db->db_debug ? $this->db->display_error('db_unable_to_drop') : FALSE;
83 }
84
85 // --------------------------------------------------------------------
86
87 /**
88 * Create Table
89 *
90 * @param string the table name
91 * @param array the fields
92 * @param mixed primary key(s)
93 * @param mixed key(s)
94 * @param bool should 'IF NOT EXISTS' be added to the SQL
95 * @return bool
96 */
Andrey Andreeva0d4e412012-04-09 15:06:40 +030097 protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
Andrey Andreev8ae24c52012-01-16 13:05:23 +020098 {
99 $sql = 'CREATE TABLE ';
100
101 // IF NOT EXISTS added to SQLite in 3.3.0
102 if ($if_not_exists === TRUE && version_compare($this->db->version(), '3.3.0', '>=') === TRUE)
103 {
104 $sql .= 'IF NOT EXISTS ';
105 }
106
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300107 $sql .= $this->db->escape_identifiers($table).' (';
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200108 $current_field_count = 0;
109
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300110 foreach ($fields as $field => $attributes)
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200111 {
112 // Numeric field names aren't allowed in databases, so if the key is
113 // numeric, we know it was assigned by PHP and the developer manually
114 // entered the field information, so we'll simply add it to the list
115 if (is_numeric($field))
116 {
117 $sql .= "\n\t".$attributes;
118 }
119 else
120 {
121 $attributes = array_change_key_case($attributes, CASE_UPPER);
122
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300123 $sql .= "\n\t".$this->db->escape_identifiers($field).' '.$attributes['TYPE'];
124
125 empty($attributes['CONSTRAINT']) OR $sql .= '('.$attributes['CONSTRAINT'].')';
126
127 if ( ! empty($attributes['UNSIGNED']) && $attributes['UNSIGNED'] === TRUE)
128 {
129 $sql .= ' UNSIGNED';
130 }
131
132 if (isset($attributes['DEFAULT']))
133 {
134 $sql .= " DEFAULT '".$attributes['DEFAULT']."'";
135 }
136
137 $sql .= ( ! empty($attributes['NULL']) && $attributes['NULL'] === TRUE)
138 ? ' NULL' : ' NOT NULL';
139
140 if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE)
141 {
142 $sql .= ' AUTO_INCREMENT';
143 }
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200144 }
145
146 // don't add a comma on the end of the last field
147 if (++$current_field_count < count($fields))
148 {
149 $sql .= ',';
150 }
151 }
152
153 if (count($primary_keys) > 0)
154 {
Andrey Andreev9637b402012-06-08 15:39:24 +0300155 $sql .= ",\n\tPRIMARY KEY (".implode(', ', $this->db->escape_identifiers($primary_keys)).')';
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200156 }
157
158 if (is_array($keys) && count($keys) > 0)
159 {
160 foreach ($keys as $key)
161 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300162 $key = is_array($key)
Andrey Andreev9637b402012-06-08 15:39:24 +0300163 ? $this->db->escape_identifiers($key)
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300164 : array($this->db->escape_identifiers($key));
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200165
166 $sql .= ",\n\tUNIQUE (".implode(', ', $key).')';
167 }
168 }
169
170 return $sql."\n)";
171 }
172
173 // --------------------------------------------------------------------
174
175 /**
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200176 * Alter table query
177 *
178 * Generates a platform-specific query so that a table can be altered
179 * Called by add_column(), drop_column(), and column_alter(),
180 *
181 * @param string the ALTER type (ADD, DROP, CHANGE)
182 * @param string the column name
183 * @param string the table name
184 * @param string the column definition
185 * @param string the default value
186 * @param bool should 'NOT NULL' be added
187 * @param string the field after which we should add the new field
188 * @return string
189 */
Andrey Andreeva0d4e412012-04-09 15:06:40 +0300190 protected function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200191 {
192 /* SQLite only supports adding new columns and it does
193 * NOT support the AFTER statement. Each new column will
194 * be added as the last one in the table.
195 */
196 if ($alter_type !== 'ADD COLUMN')
197 {
198 // Not supported
199 return FALSE;
200 }
201
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300202 return 'ALTER TABLE '.$this->db->escape_identifiers($table).' '.$alter_type.' '.$this->db->escape_identifiers($column_name)
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200203 .' '.$column_definition
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100204 .($default_value !== '' ? ' DEFAULT '.$default_value : '')
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200205 // If NOT NULL is specified, the field must have a DEFAULT value other than NULL
206 .(($null !== NULL && $default_value !== 'NULL') ? ' NOT NULL' : ' NULL');
207 }
208
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200209}
210
211/* End of file sqlite3_forge.php */
Andrey Andreevf944d3b2012-03-20 22:12:55 +0200212/* Location: ./system/database/drivers/sqlite3/sqlite3_forge.php */