blob: 0a5dc9211fec3dbe8fc70cf51392d65e55ecbead [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 }
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 Andreeva0d4e412012-04-09 15:06:40 +030088 protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
Andrey Andreev8ae24c52012-01-16 13:05:23 +020089 {
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 Andreevea09a8a2012-04-06 20:50:07 +030098 $sql .= $this->db->escape_identifiers($table).' (';
Andrey Andreev8ae24c52012-01-16 13:05:23 +020099 $current_field_count = 0;
100
Andrey Andreevea09a8a2012-04-06 20:50:07 +0300101 foreach ($fields as $field => $attributes)
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200102 {
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 Andreevcb9f3612012-01-26 02:06:48 +0200114 $sql .= "\n\t".$this->db->protect_identifiers($field)
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200115 .' '.$attributes['TYPE']
Andrey Andreevfe3428a2012-01-26 14:47:29 +0200116 .( ! empty($attributes['CONSTRAINT']) ? '('.$attributes['CONSTRAINT'].')' : '')
117 .(( ! empty($attributes['UNSIGNED']) && $attributes['UNSIGNED'] === TRUE) ? ' UNSIGNED' : '')
Andrey Andreev79f67542012-01-26 14:15:53 +0200118 .(isset($attributes['DEFAULT']) ? ' DEFAULT \''.$attributes['DEFAULT'].'\'' : '')
Andrey Andreevfe3428a2012-01-26 14:47:29 +0200119 .(( ! empty($attributes['NULL']) && $attributes['NULL'] === TRUE) ? ' NULL' : ' NOT NULL')
120 .(( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE) ? ' AUTO_INCREMENT' : '');
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200121 }
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 Andreevcb9f3612012-01-26 02:06:48 +0200132 $primary_keys = $this->db->protect_identifiers($primary_keys);
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200133 $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 Andreevcb9f3612012-01-26 02:06:48 +0200142 $key = $this->db->protect_identifiers($key);
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200143 }
144 else
145 {
Andrey Andreevcb9f3612012-01-26 02:06:48 +0200146 $key = array($this->db->protect_identifiers($key));
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200147 }
148
149 $sql .= ",\n\tUNIQUE (".implode(', ', $key).')';
150 }
151 }
152
153 return $sql."\n)";
154 }
155
156 // --------------------------------------------------------------------
157
158 /**
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200159 * 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 Andreeva0d4e412012-04-09 15:06:40 +0300173 protected function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200174 {
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 Andreevcb9f3612012-01-26 02:06:48 +0200185 return 'ALTER TABLE '.$this->db->protect_identifiers($table).' '.$alter_type.' '.$this->db->protect_identifiers($column_name)
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200186 .' '.$column_definition
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100187 .($default_value !== '' ? ' DEFAULT '.$default_value : '')
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200188 // 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 Andreev8ae24c52012-01-16 13:05:23 +0200192}
193
194/* End of file sqlite3_forge.php */
Andrey Andreevf944d3b2012-03-20 22:12:55 +0200195/* Location: ./system/database/drivers/sqlite3/sqlite3_forge.php */