blob: 036b6073f6c7706fecbb785fd9d9655c2ef0a80c [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreevd9038782012-01-26 12:38:49 +02008 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02009 * Copyright (c) 2014, British Columbia Institute of Technology
Andrey Andreevd9038782012-01-26 12:38:49 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020032 * @copyright Copyright (c) 2014, British Columbia Institute of Technology (http://bcit.ca/)
33 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @since Version 1.3.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * SQLite Forge Class
42 *
43 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050044 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000045 * @link http://codeigniter.com/user_guide/database/
46 */
47class CI_DB_sqlite_forge extends CI_DB_forge {
48
49 /**
Andrey Andreeva287a342012-11-05 23:19:59 +020050 * CREATE TABLE IF statement
51 *
52 * @var string
53 */
54 protected $_create_table_if = FALSE;
55
56 /**
57 * UNSIGNED support
58 *
59 * @var bool|array
60 */
61 protected $_unsigned = FALSE;
62
63 /**
64 * NULL value representation in CREATE/ALTER TABLE statements
65 *
66 * @var string
67 */
68 protected $_null = 'NULL';
69
Zachary Flower9b512282014-11-02 21:51:19 -070070 /**
71 * COMMENT value representation in CREATE/ALTER TABLE statements
72 *
73 * @var string
74 */
75 protected $_comment = FALSE;
76
Andrey Andreeva287a342012-11-05 23:19:59 +020077 // --------------------------------------------------------------------
78
79 /**
Derek Allard2067d1a2008-11-13 22:59:24 +000080 * Create database
81 *
Andrey Andreeva287a342012-11-05 23:19:59 +020082 * @param string $db_name (ignored)
Derek Allard2067d1a2008-11-13 22:59:24 +000083 * @return bool
84 */
Andrey Andreevd947eba2012-04-09 14:58:28 +030085 public function create_database($db_name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +000086 {
87 // In SQLite, a database is created when you connect to the database.
88 // We'll return TRUE so that an error isn't generated
89 return TRUE;
90 }
91
92 // --------------------------------------------------------------------
93
94 /**
95 * Drop database
96 *
Andrey Andreeva287a342012-11-05 23:19:59 +020097 * @param string $db_name (ignored)
Derek Allard2067d1a2008-11-13 22:59:24 +000098 * @return bool
99 */
Andrey Andreevd947eba2012-04-09 14:58:28 +0300100 public function drop_database($db_name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 {
Andrey Andreev382b5132014-02-26 18:41:59 +0200102 if ( ! file_exists($this->db->database) OR ! @unlink($this->db->database))
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200104 return ($this->db->db_debug) ? $this->db->display_error('db_unable_to_drop') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 }
Andrey Andreev5d281762012-06-11 22:05:40 +0300106 elseif ( ! empty($this->db->data_cache['db_names']))
107 {
108 $key = array_search(strtolower($this->db->database), array_map('strtolower', $this->db->data_cache['db_names']), TRUE);
109 if ($key !== FALSE)
110 {
111 unset($this->db->data_cache['db_names'][$key]);
112 }
113 }
Andrey Andreevd9038782012-01-26 12:38:49 +0200114
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 return TRUE;
116 }
Andrey Andreev8480f7c2012-03-20 16:55:05 +0200117
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 // --------------------------------------------------------------------
119
120 /**
Andrey Andreeva287a342012-11-05 23:19:59 +0200121 * ALTER TABLE
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 *
Andrey Andreeva287a342012-11-05 23:19:59 +0200123 * @todo implement drop_column(), modify_column()
124 * @param string $alter_type ALTER type
125 * @param string $table Table name
126 * @param mixed $field Column definition
127 * @return string|string[]
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 */
Andrey Andreeva287a342012-11-05 23:19:59 +0200129 protected function _alter_table($alter_type, $table, $field)
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200131 if ($alter_type === 'DROP' OR $alter_type === 'CHANGE')
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200133 // drop_column():
134 // BEGIN TRANSACTION;
135 // CREATE TEMPORARY TABLE t1_backup(a,b);
136 // INSERT INTO t1_backup SELECT a,b FROM t1;
137 // DROP TABLE t1;
138 // CREATE TABLE t1(a,b);
139 // INSERT INTO t1 SELECT a,b FROM t1_backup;
140 // DROP TABLE t1_backup;
141 // COMMIT;
142
143 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 }
Barry Mienydd671972010-10-04 16:33:58 +0200145
Andrey Andreeva287a342012-11-05 23:19:59 +0200146 return parent::_alter_table($alter_type, $table, $field);
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 }
148
149 // --------------------------------------------------------------------
150
151 /**
Andrey Andreeva287a342012-11-05 23:19:59 +0200152 * Process column
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 *
Andrey Andreeva287a342012-11-05 23:19:59 +0200154 * @param array $field
Andrey Andreevd9038782012-01-26 12:38:49 +0200155 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 */
Andrey Andreeva287a342012-11-05 23:19:59 +0200157 protected function _process_column($field)
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200159 return $this->db->escape_identifiers($field['name'])
160 .' '.$field['type']
161 .$field['auto_increment']
162 .$field['null']
163 .$field['unique']
164 .$field['default'];
165 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000166
Andrey Andreeva287a342012-11-05 23:19:59 +0200167 // --------------------------------------------------------------------
168
169 /**
170 * Field attribute TYPE
171 *
172 * Performs a data type mapping between different databases.
173 *
174 * @param array &$attributes
175 * @return void
176 */
177 protected function _attr_type(&$attributes)
178 {
179 switch (strtoupper($attributes['TYPE']))
180 {
181 case 'ENUM':
182 case 'SET':
183 $attributes['TYPE'] = 'TEXT';
184 return;
185 default: return;
186 }
187 }
188
189 // --------------------------------------------------------------------
190
191 /**
192 * Field attribute AUTO_INCREMENT
193 *
194 * @param array &$attributes
195 * @param array &$field
196 * @return void
197 */
198 protected function _attr_auto_increment(&$attributes, &$field)
199 {
200 if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE && stripos($field['type'], 'int') !== FALSE)
201 {
202 $field['type'] = 'INTEGER PRIMARY KEY';
203 $field['default'] = '';
204 $field['null'] = '';
205 $field['unique'] = '';
206 $field['auto_increment'] = ' AUTOINCREMENT';
207
208 $this->primary_keys = array();
209 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 }
211
Derek Allard2067d1a2008-11-13 22:59:24 +0000212}
213
214/* End of file sqlite_forge.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400215/* Location: ./system/database/drivers/sqlite/sqlite_forge.php */