blob: 7ba0f7bab790a6198edc66d9eab663dbe31efd32 [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 /**
Andrey Andreeva287a342012-11-05 23:19:59 +020039 * UNSIGNED support
40 *
41 * @var bool|array
42 */
43 protected $_unsigned = FALSE;
44
45 /**
46 * NULL value representation in CREATE/ALTER TABLE statements
47 *
48 * @var string
49 */
50 protected $_null = 'NULL';
51
52 // --------------------------------------------------------------------
53
54 /**
55 * Class constructor
56 *
57 * @return void
58 */
59 public function __construct()
60 {
61 parent::__construct();
62
63 if (version_compare($this->db->version(), '3.3', '<'))
64 {
65 $this->create_table_if = FALSE;
66 }
67 }
68
69 // --------------------------------------------------------------------
70
71 /**
Andrey Andreev8ae24c52012-01-16 13:05:23 +020072 * Create database
73 *
Andrey Andreeva287a342012-11-05 23:19:59 +020074 * @param string $db_name
Andrey Andreev8ae24c52012-01-16 13:05:23 +020075 * @return bool
76 */
Andrey Andreevd947eba2012-04-09 14:58:28 +030077 public function create_database($db_name = '')
Andrey Andreev8ae24c52012-01-16 13:05:23 +020078 {
79 // In SQLite, a database is created when you connect to the database.
80 // We'll return TRUE so that an error isn't generated
81 return TRUE;
82 }
83
84 // --------------------------------------------------------------------
85
86 /**
87 * Drop database
88 *
Andrey Andreeva287a342012-11-05 23:19:59 +020089 * @param string $db_name (ignored)
Andrey Andreev8ae24c52012-01-16 13:05:23 +020090 * @return bool
91 */
Andrey Andreevd947eba2012-04-09 14:58:28 +030092 public function drop_database($db_name = '')
Andrey Andreev8ae24c52012-01-16 13:05:23 +020093 {
94 // In SQLite, a database is dropped when we delete a file
95 if (@file_exists($this->db->database))
96 {
97 // We need to close the pseudo-connection first
98 $this->db->close();
99 if ( ! @unlink($this->db->database))
100 {
101 return $this->db->db_debug ? $this->db->display_error('db_unable_to_drop') : FALSE;
102 }
Andrey Andreev5d281762012-06-11 22:05:40 +0300103 elseif ( ! empty($this->db->data_cache['db_names']))
104 {
105 $key = array_search(strtolower($this->db->database), array_map('strtolower', $this->db->data_cache['db_names']), TRUE);
106 if ($key !== FALSE)
107 {
108 unset($this->db->data_cache['db_names'][$key]);
109 }
110 }
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200111
112 return TRUE;
113 }
114
115 return $this->db->db_debug ? $this->db->display_error('db_unable_to_drop') : FALSE;
116 }
117
118 // --------------------------------------------------------------------
119
120 /**
Andrey Andreeva287a342012-11-05 23:19:59 +0200121 * ALTER TABLE
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200122 *
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[]
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200128 */
Andrey Andreeva287a342012-11-05 23:19:59 +0200129 protected function _alter_table($alter_type, $table, $field)
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200130 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200131 if ($alter_type === 'DROP' OR $alter_type === 'CHANGE')
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200132 {
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;
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200144 }
145
Andrey Andreeva287a342012-11-05 23:19:59 +0200146 return parent::_alter_table($alter_type, $table, $field);
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200147 }
148
149 // --------------------------------------------------------------------
150
151 /**
Andrey Andreeva287a342012-11-05 23:19:59 +0200152 * Process column
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200153 *
Andrey Andreeva287a342012-11-05 23:19:59 +0200154 * @param array $field
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200155 * @return string
156 */
Andrey Andreeva287a342012-11-05 23:19:59 +0200157 protected function _process_column($field)
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200158 {
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 }
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200166
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 }
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200210 }
211
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200212}
213
214/* End of file sqlite3_forge.php */
Andrey Andreevf944d3b2012-03-20 22:12:55 +0200215/* Location: ./system/database/drivers/sqlite3/sqlite3_forge.php */