blob: d79d15afd6e2f98accc8c1af3c8d908e34655fed [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
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, 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 *
Andrey Andreeveaa60c72012-11-06 01:11:22 +020057 * @param object &$db Database object
Andrey Andreeva287a342012-11-05 23:19:59 +020058 * @return void
59 */
Andrey Andreeveaa60c72012-11-06 01:11:22 +020060 public function __construct(&$db)
Andrey Andreeva287a342012-11-05 23:19:59 +020061 {
Andrey Andreeveaa60c72012-11-06 01:11:22 +020062 parent::__construct($db);
Andrey Andreeva287a342012-11-05 23:19:59 +020063
64 if (version_compare($this->db->version(), '3.3', '<'))
65 {
66 $this->create_table_if = FALSE;
67 }
68 }
69
70 // --------------------------------------------------------------------
71
72 /**
Andrey Andreev8ae24c52012-01-16 13:05:23 +020073 * Create database
74 *
Andrey Andreeva287a342012-11-05 23:19:59 +020075 * @param string $db_name
Andrey Andreev8ae24c52012-01-16 13:05:23 +020076 * @return bool
77 */
Andrey Andreevd947eba2012-04-09 14:58:28 +030078 public function create_database($db_name = '')
Andrey Andreev8ae24c52012-01-16 13:05:23 +020079 {
80 // In SQLite, a database is created when you connect to the database.
81 // We'll return TRUE so that an error isn't generated
82 return TRUE;
83 }
84
85 // --------------------------------------------------------------------
86
87 /**
88 * Drop database
89 *
Andrey Andreeva287a342012-11-05 23:19:59 +020090 * @param string $db_name (ignored)
Andrey Andreev8ae24c52012-01-16 13:05:23 +020091 * @return bool
92 */
Andrey Andreevd947eba2012-04-09 14:58:28 +030093 public function drop_database($db_name = '')
Andrey Andreev8ae24c52012-01-16 13:05:23 +020094 {
95 // In SQLite, a database is dropped when we delete a file
Andrey Andreev382b5132014-02-26 18:41:59 +020096 if (file_exists($this->db->database))
Andrey Andreev8ae24c52012-01-16 13:05:23 +020097 {
98 // We need to close the pseudo-connection first
99 $this->db->close();
100 if ( ! @unlink($this->db->database))
101 {
102 return $this->db->db_debug ? $this->db->display_error('db_unable_to_drop') : FALSE;
103 }
Andrey Andreev5d281762012-06-11 22:05:40 +0300104 elseif ( ! empty($this->db->data_cache['db_names']))
105 {
106 $key = array_search(strtolower($this->db->database), array_map('strtolower', $this->db->data_cache['db_names']), TRUE);
107 if ($key !== FALSE)
108 {
109 unset($this->db->data_cache['db_names'][$key]);
110 }
111 }
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200112
113 return TRUE;
114 }
115
116 return $this->db->db_debug ? $this->db->display_error('db_unable_to_drop') : FALSE;
117 }
118
119 // --------------------------------------------------------------------
120
121 /**
Andrey Andreeva287a342012-11-05 23:19:59 +0200122 * ALTER TABLE
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200123 *
Andrey Andreeva287a342012-11-05 23:19:59 +0200124 * @todo implement drop_column(), modify_column()
125 * @param string $alter_type ALTER type
126 * @param string $table Table name
127 * @param mixed $field Column definition
128 * @return string|string[]
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200129 */
Andrey Andreeva287a342012-11-05 23:19:59 +0200130 protected function _alter_table($alter_type, $table, $field)
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200131 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200132 if ($alter_type === 'DROP' OR $alter_type === 'CHANGE')
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200133 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200134 // drop_column():
135 // BEGIN TRANSACTION;
136 // CREATE TEMPORARY TABLE t1_backup(a,b);
137 // INSERT INTO t1_backup SELECT a,b FROM t1;
138 // DROP TABLE t1;
139 // CREATE TABLE t1(a,b);
140 // INSERT INTO t1 SELECT a,b FROM t1_backup;
141 // DROP TABLE t1_backup;
142 // COMMIT;
143
144 return FALSE;
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200145 }
146
Andrey Andreeva287a342012-11-05 23:19:59 +0200147 return parent::_alter_table($alter_type, $table, $field);
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200148 }
149
150 // --------------------------------------------------------------------
151
152 /**
Andrey Andreeva287a342012-11-05 23:19:59 +0200153 * Process column
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200154 *
Andrey Andreeva287a342012-11-05 23:19:59 +0200155 * @param array $field
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200156 * @return string
157 */
Andrey Andreeva287a342012-11-05 23:19:59 +0200158 protected function _process_column($field)
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200159 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200160 return $this->db->escape_identifiers($field['name'])
161 .' '.$field['type']
162 .$field['auto_increment']
163 .$field['null']
164 .$field['unique']
165 .$field['default'];
166 }
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200167
Andrey Andreeva287a342012-11-05 23:19:59 +0200168 // --------------------------------------------------------------------
169
170 /**
171 * Field attribute TYPE
172 *
173 * Performs a data type mapping between different databases.
174 *
175 * @param array &$attributes
176 * @return void
177 */
178 protected function _attr_type(&$attributes)
179 {
180 switch (strtoupper($attributes['TYPE']))
181 {
182 case 'ENUM':
183 case 'SET':
184 $attributes['TYPE'] = 'TEXT';
185 return;
186 default: return;
187 }
188 }
189
190 // --------------------------------------------------------------------
191
192 /**
193 * Field attribute AUTO_INCREMENT
194 *
195 * @param array &$attributes
196 * @param array &$field
197 * @return void
198 */
199 protected function _attr_auto_increment(&$attributes, &$field)
200 {
201 if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE && stripos($field['type'], 'int') !== FALSE)
202 {
203 $field['type'] = 'INTEGER PRIMARY KEY';
204 $field['default'] = '';
205 $field['null'] = '';
206 $field['unique'] = '';
207 $field['auto_increment'] = ' AUTOINCREMENT';
208
209 $this->primary_keys = array();
210 }
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200211 }
212
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200213}
214
215/* End of file sqlite3_forge.php */
Andrey Andreevf944d3b2012-03-20 22:12:55 +0200216/* Location: ./system/database/drivers/sqlite3/sqlite3_forge.php */