| 68 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* CodeIgniter
|
|
|
4 |
*
|
|
|
5 |
* An open source application development framework for PHP
|
|
|
6 |
*
|
|
|
7 |
* This content is released under the MIT License (MIT)
|
|
|
8 |
*
|
| 2254 |
lars |
9 |
* Copyright (c) 2014 - 2017, British Columbia Institute of Technology
|
| 68 |
lars |
10 |
*
|
|
|
11 |
* 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:
|
|
|
17 |
*
|
|
|
18 |
* 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
|
|
|
31 |
* @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
|
| 2254 |
lars |
32 |
* @copyright Copyright (c) 2014 - 2017, British Columbia Institute of Technology (http://bcit.ca/)
|
| 68 |
lars |
33 |
* @license http://opensource.org/licenses/MIT MIT License
|
|
|
34 |
* @link https://codeigniter.com
|
|
|
35 |
* @since Version 3.0.0
|
|
|
36 |
* @filesource
|
|
|
37 |
*/
|
|
|
38 |
defined('BASEPATH') OR exit('No direct script access allowed');
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* PDO SQLite Forge Class
|
|
|
42 |
*
|
|
|
43 |
* @category Database
|
|
|
44 |
* @author EllisLab Dev Team
|
|
|
45 |
* @link https://codeigniter.com/user_guide/database/
|
|
|
46 |
*/
|
|
|
47 |
class CI_DB_pdo_sqlite_forge extends CI_DB_pdo_forge {
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* CREATE TABLE IF statement
|
|
|
51 |
*
|
|
|
52 |
* @var string
|
|
|
53 |
*/
|
|
|
54 |
protected $_create_table_if = 'CREATE TABLE IF NOT EXISTS';
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* DROP TABLE IF statement
|
|
|
58 |
*
|
|
|
59 |
* @var string
|
|
|
60 |
*/
|
|
|
61 |
protected $_drop_table_if = 'DROP TABLE IF EXISTS';
|
|
|
62 |
|
|
|
63 |
/**
|
|
|
64 |
* UNSIGNED support
|
|
|
65 |
*
|
|
|
66 |
* @var bool|array
|
|
|
67 |
*/
|
|
|
68 |
protected $_unsigned = FALSE;
|
|
|
69 |
|
|
|
70 |
/**
|
|
|
71 |
* NULL value representation in CREATE/ALTER TABLE statements
|
|
|
72 |
*
|
|
|
73 |
* @var string
|
|
|
74 |
*/
|
|
|
75 |
protected $_null = 'NULL';
|
|
|
76 |
|
|
|
77 |
// --------------------------------------------------------------------
|
|
|
78 |
|
|
|
79 |
/**
|
|
|
80 |
* Class constructor
|
|
|
81 |
*
|
|
|
82 |
* @param object &$db Database object
|
|
|
83 |
* @return void
|
|
|
84 |
*/
|
|
|
85 |
public function __construct(&$db)
|
|
|
86 |
{
|
|
|
87 |
parent::__construct($db);
|
|
|
88 |
|
|
|
89 |
if (version_compare($this->db->version(), '3.3', '<'))
|
|
|
90 |
{
|
|
|
91 |
$this->_create_table_if = FALSE;
|
|
|
92 |
$this->_drop_table_if = FALSE;
|
|
|
93 |
}
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
// --------------------------------------------------------------------
|
|
|
97 |
|
|
|
98 |
/**
|
|
|
99 |
* Create database
|
|
|
100 |
*
|
|
|
101 |
* @param string $db_name (ignored)
|
|
|
102 |
* @return bool
|
|
|
103 |
*/
|
| 1257 |
lars |
104 |
public function create_database($db_name)
|
| 68 |
lars |
105 |
{
|
|
|
106 |
// In SQLite, a database is created when you connect to the database.
|
|
|
107 |
// We'll return TRUE so that an error isn't generated
|
|
|
108 |
return TRUE;
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
// --------------------------------------------------------------------
|
|
|
112 |
|
|
|
113 |
/**
|
|
|
114 |
* Drop database
|
|
|
115 |
*
|
|
|
116 |
* @param string $db_name (ignored)
|
|
|
117 |
* @return bool
|
|
|
118 |
*/
|
| 1257 |
lars |
119 |
public function drop_database($db_name)
|
| 68 |
lars |
120 |
{
|
|
|
121 |
// In SQLite, a database is dropped when we delete a file
|
|
|
122 |
if (file_exists($this->db->database))
|
|
|
123 |
{
|
|
|
124 |
// We need to close the pseudo-connection first
|
|
|
125 |
$this->db->close();
|
|
|
126 |
if ( ! @unlink($this->db->database))
|
|
|
127 |
{
|
|
|
128 |
return $this->db->db_debug ? $this->db->display_error('db_unable_to_drop') : FALSE;
|
|
|
129 |
}
|
|
|
130 |
elseif ( ! empty($this->db->data_cache['db_names']))
|
|
|
131 |
{
|
|
|
132 |
$key = array_search(strtolower($this->db->database), array_map('strtolower', $this->db->data_cache['db_names']), TRUE);
|
|
|
133 |
if ($key !== FALSE)
|
|
|
134 |
{
|
|
|
135 |
unset($this->db->data_cache['db_names'][$key]);
|
|
|
136 |
}
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
return TRUE;
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
return $this->db->db_debug ? $this->db->display_error('db_unable_to_drop') : FALSE;
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
// --------------------------------------------------------------------
|
|
|
146 |
|
|
|
147 |
/**
|
|
|
148 |
* ALTER TABLE
|
|
|
149 |
*
|
|
|
150 |
* @param string $alter_type ALTER type
|
|
|
151 |
* @param string $table Table name
|
|
|
152 |
* @param mixed $field Column definition
|
|
|
153 |
* @return string|string[]
|
|
|
154 |
*/
|
|
|
155 |
protected function _alter_table($alter_type, $table, $field)
|
|
|
156 |
{
|
|
|
157 |
if ($alter_type === 'DROP' OR $alter_type === 'CHANGE')
|
|
|
158 |
{
|
|
|
159 |
// drop_column():
|
|
|
160 |
// BEGIN TRANSACTION;
|
|
|
161 |
// CREATE TEMPORARY TABLE t1_backup(a,b);
|
|
|
162 |
// INSERT INTO t1_backup SELECT a,b FROM t1;
|
|
|
163 |
// DROP TABLE t1;
|
|
|
164 |
// CREATE TABLE t1(a,b);
|
|
|
165 |
// INSERT INTO t1 SELECT a,b FROM t1_backup;
|
|
|
166 |
// DROP TABLE t1_backup;
|
|
|
167 |
// COMMIT;
|
|
|
168 |
|
|
|
169 |
return FALSE;
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
return parent::_alter_table($alter_type, $table, $field);
|
|
|
173 |
}
|
|
|
174 |
|
|
|
175 |
// --------------------------------------------------------------------
|
|
|
176 |
|
|
|
177 |
/**
|
|
|
178 |
* Process column
|
|
|
179 |
*
|
|
|
180 |
* @param array $field
|
|
|
181 |
* @return string
|
|
|
182 |
*/
|
|
|
183 |
protected function _process_column($field)
|
|
|
184 |
{
|
|
|
185 |
return $this->db->escape_identifiers($field['name'])
|
|
|
186 |
.' '.$field['type']
|
|
|
187 |
.$field['auto_increment']
|
|
|
188 |
.$field['null']
|
|
|
189 |
.$field['unique']
|
|
|
190 |
.$field['default'];
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
// --------------------------------------------------------------------
|
|
|
194 |
|
|
|
195 |
/**
|
|
|
196 |
* Field attribute TYPE
|
|
|
197 |
*
|
|
|
198 |
* Performs a data type mapping between different databases.
|
|
|
199 |
*
|
|
|
200 |
* @param array &$attributes
|
|
|
201 |
* @return void
|
|
|
202 |
*/
|
|
|
203 |
protected function _attr_type(&$attributes)
|
|
|
204 |
{
|
|
|
205 |
switch (strtoupper($attributes['TYPE']))
|
|
|
206 |
{
|
|
|
207 |
case 'ENUM':
|
|
|
208 |
case 'SET':
|
|
|
209 |
$attributes['TYPE'] = 'TEXT';
|
|
|
210 |
return;
|
|
|
211 |
default: return;
|
|
|
212 |
}
|
|
|
213 |
}
|
|
|
214 |
|
|
|
215 |
// --------------------------------------------------------------------
|
|
|
216 |
|
|
|
217 |
/**
|
|
|
218 |
* Field attribute AUTO_INCREMENT
|
|
|
219 |
*
|
|
|
220 |
* @param array &$attributes
|
|
|
221 |
* @param array &$field
|
|
|
222 |
* @return void
|
|
|
223 |
*/
|
|
|
224 |
protected function _attr_auto_increment(&$attributes, &$field)
|
|
|
225 |
{
|
|
|
226 |
if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE && stripos($field['type'], 'int') !== FALSE)
|
|
|
227 |
{
|
|
|
228 |
$field['type'] = 'INTEGER PRIMARY KEY';
|
|
|
229 |
$field['default'] = '';
|
|
|
230 |
$field['null'] = '';
|
|
|
231 |
$field['unique'] = '';
|
|
|
232 |
$field['auto_increment'] = ' AUTOINCREMENT';
|
|
|
233 |
|
|
|
234 |
$this->primary_keys = array();
|
|
|
235 |
}
|
|
|
236 |
}
|
|
|
237 |
|
|
|
238 |
}
|