|
indvdum@0
|
1 |
<?php
|
|
indvdum@0
|
2 |
/**
|
|
indvdum@0
|
3 |
*
|
|
indvdum@0
|
4 |
* @package dbal
|
|
indvdum@0
|
5 |
* @version $Id$
|
|
indvdum@0
|
6 |
* @copyright (c) 2007 phpBB Group
|
|
indvdum@0
|
7 |
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
|
|
indvdum@0
|
8 |
*
|
|
indvdum@0
|
9 |
*/
|
|
indvdum@0
|
10 |
|
|
indvdum@0
|
11 |
/**
|
|
indvdum@0
|
12 |
* @ignore
|
|
indvdum@0
|
13 |
*/
|
|
indvdum@0
|
14 |
if (!defined('IN_PHPBB'))
|
|
indvdum@0
|
15 |
{
|
|
indvdum@0
|
16 |
exit;
|
|
indvdum@0
|
17 |
}
|
|
indvdum@0
|
18 |
|
|
indvdum@0
|
19 |
/**
|
|
indvdum@0
|
20 |
* Database Tools for handling cross-db actions such as altering columns, etc.
|
|
indvdum@0
|
21 |
* Currently not supported is returning SQL for creating tables.
|
|
indvdum@0
|
22 |
*
|
|
indvdum@0
|
23 |
* @package dbal
|
|
indvdum@0
|
24 |
* @note currently not used within phpBB3, but may be utilized later.
|
|
indvdum@0
|
25 |
*/
|
|
indvdum@0
|
26 |
class phpbb_db_tools
|
|
indvdum@0
|
27 |
{
|
|
indvdum@0
|
28 |
/**
|
|
indvdum@0
|
29 |
* Current sql layer
|
|
indvdum@0
|
30 |
*/
|
|
indvdum@0
|
31 |
var $sql_layer = '';
|
|
indvdum@0
|
32 |
|
|
indvdum@0
|
33 |
/**
|
|
indvdum@0
|
34 |
* @var object DB object
|
|
indvdum@0
|
35 |
*/
|
|
indvdum@0
|
36 |
var $db = NULL;
|
|
indvdum@0
|
37 |
|
|
indvdum@0
|
38 |
/**
|
|
indvdum@0
|
39 |
* The Column types for every database we support
|
|
indvdum@0
|
40 |
* @var array
|
|
indvdum@0
|
41 |
*/
|
|
indvdum@0
|
42 |
var $dbms_type_map = array(
|
|
indvdum@0
|
43 |
'mysql_41' => array(
|
|
indvdum@0
|
44 |
'INT:' => 'int(%d)',
|
|
indvdum@0
|
45 |
'BINT' => 'bigint(20)',
|
|
indvdum@0
|
46 |
'UINT' => 'mediumint(8) UNSIGNED',
|
|
indvdum@0
|
47 |
'UINT:' => 'int(%d) UNSIGNED',
|
|
indvdum@0
|
48 |
'TINT:' => 'tinyint(%d)',
|
|
indvdum@0
|
49 |
'USINT' => 'smallint(4) UNSIGNED',
|
|
indvdum@0
|
50 |
'BOOL' => 'tinyint(1) UNSIGNED',
|
|
indvdum@0
|
51 |
'VCHAR' => 'varchar(255)',
|
|
indvdum@0
|
52 |
'VCHAR:' => 'varchar(%d)',
|
|
indvdum@0
|
53 |
'CHAR:' => 'char(%d)',
|
|
indvdum@0
|
54 |
'XSTEXT' => 'text',
|
|
indvdum@0
|
55 |
'XSTEXT_UNI'=> 'varchar(100)',
|
|
indvdum@0
|
56 |
'STEXT' => 'text',
|
|
indvdum@0
|
57 |
'STEXT_UNI' => 'varchar(255)',
|
|
indvdum@0
|
58 |
'TEXT' => 'text',
|
|
indvdum@0
|
59 |
'TEXT_UNI' => 'text',
|
|
indvdum@0
|
60 |
'MTEXT' => 'mediumtext',
|
|
indvdum@0
|
61 |
'MTEXT_UNI' => 'mediumtext',
|
|
indvdum@0
|
62 |
'TIMESTAMP' => 'int(11) UNSIGNED',
|
|
indvdum@0
|
63 |
'DECIMAL' => 'decimal(5,2)',
|
|
indvdum@0
|
64 |
'DECIMAL:' => 'decimal(%d,2)',
|
|
indvdum@0
|
65 |
'PDECIMAL' => 'decimal(6,3)',
|
|
indvdum@0
|
66 |
'PDECIMAL:' => 'decimal(%d,3)',
|
|
indvdum@0
|
67 |
'VCHAR_UNI' => 'varchar(255)',
|
|
indvdum@0
|
68 |
'VCHAR_UNI:'=> 'varchar(%d)',
|
|
indvdum@0
|
69 |
'VCHAR_CI' => 'varchar(255)',
|
|
indvdum@0
|
70 |
'VARBINARY' => 'varbinary(255)',
|
|
indvdum@0
|
71 |
),
|
|
indvdum@0
|
72 |
|
|
indvdum@0
|
73 |
'mysql_40' => array(
|
|
indvdum@0
|
74 |
'INT:' => 'int(%d)',
|
|
indvdum@0
|
75 |
'BINT' => 'bigint(20)',
|
|
indvdum@0
|
76 |
'UINT' => 'mediumint(8) UNSIGNED',
|
|
indvdum@0
|
77 |
'UINT:' => 'int(%d) UNSIGNED',
|
|
indvdum@0
|
78 |
'TINT:' => 'tinyint(%d)',
|
|
indvdum@0
|
79 |
'USINT' => 'smallint(4) UNSIGNED',
|
|
indvdum@0
|
80 |
'BOOL' => 'tinyint(1) UNSIGNED',
|
|
indvdum@0
|
81 |
'VCHAR' => 'varbinary(255)',
|
|
indvdum@0
|
82 |
'VCHAR:' => 'varbinary(%d)',
|
|
indvdum@0
|
83 |
'CHAR:' => 'binary(%d)',
|
|
indvdum@0
|
84 |
'XSTEXT' => 'blob',
|
|
indvdum@0
|
85 |
'XSTEXT_UNI'=> 'blob',
|
|
indvdum@0
|
86 |
'STEXT' => 'blob',
|
|
indvdum@0
|
87 |
'STEXT_UNI' => 'blob',
|
|
indvdum@0
|
88 |
'TEXT' => 'blob',
|
|
indvdum@0
|
89 |
'TEXT_UNI' => 'blob',
|
|
indvdum@0
|
90 |
'MTEXT' => 'mediumblob',
|
|
indvdum@0
|
91 |
'MTEXT_UNI' => 'mediumblob',
|
|
indvdum@0
|
92 |
'TIMESTAMP' => 'int(11) UNSIGNED',
|
|
indvdum@0
|
93 |
'DECIMAL' => 'decimal(5,2)',
|
|
indvdum@0
|
94 |
'DECIMAL:' => 'decimal(%d,2)',
|
|
indvdum@0
|
95 |
'PDECIMAL' => 'decimal(6,3)',
|
|
indvdum@0
|
96 |
'PDECIMAL:' => 'decimal(%d,3)',
|
|
indvdum@0
|
97 |
'VCHAR_UNI' => 'blob',
|
|
indvdum@0
|
98 |
'VCHAR_UNI:'=> array('varbinary(%d)', 'limit' => array('mult', 3, 255, 'blob')),
|
|
indvdum@0
|
99 |
'VCHAR_CI' => 'blob',
|
|
indvdum@0
|
100 |
'VARBINARY' => 'varbinary(255)',
|
|
indvdum@0
|
101 |
),
|
|
indvdum@0
|
102 |
|
|
indvdum@0
|
103 |
'firebird' => array(
|
|
indvdum@0
|
104 |
'INT:' => 'INTEGER',
|
|
indvdum@0
|
105 |
'BINT' => 'DOUBLE PRECISION',
|
|
indvdum@0
|
106 |
'UINT' => 'INTEGER',
|
|
indvdum@0
|
107 |
'UINT:' => 'INTEGER',
|
|
indvdum@0
|
108 |
'TINT:' => 'INTEGER',
|
|
indvdum@0
|
109 |
'USINT' => 'INTEGER',
|
|
indvdum@0
|
110 |
'BOOL' => 'INTEGER',
|
|
indvdum@0
|
111 |
'VCHAR' => 'VARCHAR(255) CHARACTER SET NONE',
|
|
indvdum@0
|
112 |
'VCHAR:' => 'VARCHAR(%d) CHARACTER SET NONE',
|
|
indvdum@0
|
113 |
'CHAR:' => 'CHAR(%d) CHARACTER SET NONE',
|
|
indvdum@0
|
114 |
'XSTEXT' => 'BLOB SUB_TYPE TEXT CHARACTER SET NONE',
|
|
indvdum@0
|
115 |
'STEXT' => 'BLOB SUB_TYPE TEXT CHARACTER SET NONE',
|
|
indvdum@0
|
116 |
'TEXT' => 'BLOB SUB_TYPE TEXT CHARACTER SET NONE',
|
|
indvdum@0
|
117 |
'MTEXT' => 'BLOB SUB_TYPE TEXT CHARACTER SET NONE',
|
|
indvdum@0
|
118 |
'XSTEXT_UNI'=> 'VARCHAR(100) CHARACTER SET UTF8',
|
|
indvdum@0
|
119 |
'STEXT_UNI' => 'VARCHAR(255) CHARACTER SET UTF8',
|
|
indvdum@0
|
120 |
'TEXT_UNI' => 'BLOB SUB_TYPE TEXT CHARACTER SET UTF8',
|
|
indvdum@0
|
121 |
'MTEXT_UNI' => 'BLOB SUB_TYPE TEXT CHARACTER SET UTF8',
|
|
indvdum@0
|
122 |
'TIMESTAMP' => 'INTEGER',
|
|
indvdum@0
|
123 |
'DECIMAL' => 'DOUBLE PRECISION',
|
|
indvdum@0
|
124 |
'DECIMAL:' => 'DOUBLE PRECISION',
|
|
indvdum@0
|
125 |
'PDECIMAL' => 'DOUBLE PRECISION',
|
|
indvdum@0
|
126 |
'PDECIMAL:' => 'DOUBLE PRECISION',
|
|
indvdum@0
|
127 |
'VCHAR_UNI' => 'VARCHAR(255) CHARACTER SET UTF8',
|
|
indvdum@0
|
128 |
'VCHAR_UNI:'=> 'VARCHAR(%d) CHARACTER SET UTF8',
|
|
indvdum@0
|
129 |
'VCHAR_CI' => 'VARCHAR(255) CHARACTER SET UTF8',
|
|
indvdum@0
|
130 |
'VARBINARY' => 'CHAR(255) CHARACTER SET NONE',
|
|
indvdum@0
|
131 |
),
|
|
indvdum@0
|
132 |
|
|
indvdum@0
|
133 |
'mssql' => array(
|
|
indvdum@0
|
134 |
'INT:' => '[int]',
|
|
indvdum@0
|
135 |
'BINT' => '[float]',
|
|
indvdum@0
|
136 |
'UINT' => '[int]',
|
|
indvdum@0
|
137 |
'UINT:' => '[int]',
|
|
indvdum@0
|
138 |
'TINT:' => '[int]',
|
|
indvdum@0
|
139 |
'USINT' => '[int]',
|
|
indvdum@0
|
140 |
'BOOL' => '[int]',
|
|
indvdum@0
|
141 |
'VCHAR' => '[varchar] (255)',
|
|
indvdum@0
|
142 |
'VCHAR:' => '[varchar] (%d)',
|
|
indvdum@0
|
143 |
'CHAR:' => '[char] (%d)',
|
|
indvdum@0
|
144 |
'XSTEXT' => '[varchar] (1000)',
|
|
indvdum@0
|
145 |
'STEXT' => '[varchar] (3000)',
|
|
indvdum@0
|
146 |
'TEXT' => '[varchar] (8000)',
|
|
indvdum@0
|
147 |
'MTEXT' => '[text]',
|
|
indvdum@0
|
148 |
'XSTEXT_UNI'=> '[varchar] (100)',
|
|
indvdum@0
|
149 |
'STEXT_UNI' => '[varchar] (255)',
|
|
indvdum@0
|
150 |
'TEXT_UNI' => '[varchar] (4000)',
|
|
indvdum@0
|
151 |
'MTEXT_UNI' => '[text]',
|
|
indvdum@0
|
152 |
'TIMESTAMP' => '[int]',
|
|
indvdum@0
|
153 |
'DECIMAL' => '[float]',
|
|
indvdum@0
|
154 |
'DECIMAL:' => '[float]',
|
|
indvdum@0
|
155 |
'PDECIMAL' => '[float]',
|
|
indvdum@0
|
156 |
'PDECIMAL:' => '[float]',
|
|
indvdum@0
|
157 |
'VCHAR_UNI' => '[varchar] (255)',
|
|
indvdum@0
|
158 |
'VCHAR_UNI:'=> '[varchar] (%d)',
|
|
indvdum@0
|
159 |
'VCHAR_CI' => '[varchar] (255)',
|
|
indvdum@0
|
160 |
'VARBINARY' => '[varchar] (255)',
|
|
indvdum@0
|
161 |
),
|
|
indvdum@0
|
162 |
|
|
indvdum@0
|
163 |
'mssqlnative' => array(
|
|
indvdum@0
|
164 |
'INT:' => '[int]',
|
|
indvdum@0
|
165 |
'BINT' => '[float]',
|
|
indvdum@0
|
166 |
'UINT' => '[int]',
|
|
indvdum@0
|
167 |
'UINT:' => '[int]',
|
|
indvdum@0
|
168 |
'TINT:' => '[int]',
|
|
indvdum@0
|
169 |
'USINT' => '[int]',
|
|
indvdum@0
|
170 |
'BOOL' => '[int]',
|
|
indvdum@0
|
171 |
'VCHAR' => '[varchar] (255)',
|
|
indvdum@0
|
172 |
'VCHAR:' => '[varchar] (%d)',
|
|
indvdum@0
|
173 |
'CHAR:' => '[char] (%d)',
|
|
indvdum@0
|
174 |
'XSTEXT' => '[varchar] (1000)',
|
|
indvdum@0
|
175 |
'STEXT' => '[varchar] (3000)',
|
|
indvdum@0
|
176 |
'TEXT' => '[varchar] (8000)',
|
|
indvdum@0
|
177 |
'MTEXT' => '[text]',
|
|
indvdum@0
|
178 |
'XSTEXT_UNI'=> '[varchar] (100)',
|
|
indvdum@0
|
179 |
'STEXT_UNI' => '[varchar] (255)',
|
|
indvdum@0
|
180 |
'TEXT_UNI' => '[varchar] (4000)',
|
|
indvdum@0
|
181 |
'MTEXT_UNI' => '[text]',
|
|
indvdum@0
|
182 |
'TIMESTAMP' => '[int]',
|
|
indvdum@0
|
183 |
'DECIMAL' => '[float]',
|
|
indvdum@0
|
184 |
'DECIMAL:' => '[float]',
|
|
indvdum@0
|
185 |
'PDECIMAL' => '[float]',
|
|
indvdum@0
|
186 |
'PDECIMAL:' => '[float]',
|
|
indvdum@0
|
187 |
'VCHAR_UNI' => '[varchar] (255)',
|
|
indvdum@0
|
188 |
'VCHAR_UNI:'=> '[varchar] (%d)',
|
|
indvdum@0
|
189 |
'VCHAR_CI' => '[varchar] (255)',
|
|
indvdum@0
|
190 |
'VARBINARY' => '[varchar] (255)',
|
|
indvdum@0
|
191 |
),
|
|
indvdum@0
|
192 |
|
|
indvdum@0
|
193 |
'oracle' => array(
|
|
indvdum@0
|
194 |
'INT:' => 'number(%d)',
|
|
indvdum@0
|
195 |
'BINT' => 'number(20)',
|
|
indvdum@0
|
196 |
'UINT' => 'number(8)',
|
|
indvdum@0
|
197 |
'UINT:' => 'number(%d)',
|
|
indvdum@0
|
198 |
'TINT:' => 'number(%d)',
|
|
indvdum@0
|
199 |
'USINT' => 'number(4)',
|
|
indvdum@0
|
200 |
'BOOL' => 'number(1)',
|
|
indvdum@0
|
201 |
'VCHAR' => 'varchar2(255)',
|
|
indvdum@0
|
202 |
'VCHAR:' => 'varchar2(%d)',
|
|
indvdum@0
|
203 |
'CHAR:' => 'char(%d)',
|
|
indvdum@0
|
204 |
'XSTEXT' => 'varchar2(1000)',
|
|
indvdum@0
|
205 |
'STEXT' => 'varchar2(3000)',
|
|
indvdum@0
|
206 |
'TEXT' => 'clob',
|
|
indvdum@0
|
207 |
'MTEXT' => 'clob',
|
|
indvdum@0
|
208 |
'XSTEXT_UNI'=> 'varchar2(300)',
|
|
indvdum@0
|
209 |
'STEXT_UNI' => 'varchar2(765)',
|
|
indvdum@0
|
210 |
'TEXT_UNI' => 'clob',
|
|
indvdum@0
|
211 |
'MTEXT_UNI' => 'clob',
|
|
indvdum@0
|
212 |
'TIMESTAMP' => 'number(11)',
|
|
indvdum@0
|
213 |
'DECIMAL' => 'number(5, 2)',
|
|
indvdum@0
|
214 |
'DECIMAL:' => 'number(%d, 2)',
|
|
indvdum@0
|
215 |
'PDECIMAL' => 'number(6, 3)',
|
|
indvdum@0
|
216 |
'PDECIMAL:' => 'number(%d, 3)',
|
|
indvdum@0
|
217 |
'VCHAR_UNI' => 'varchar2(765)',
|
|
indvdum@0
|
218 |
'VCHAR_UNI:'=> array('varchar2(%d)', 'limit' => array('mult', 3, 765, 'clob')),
|
|
indvdum@0
|
219 |
'VCHAR_CI' => 'varchar2(255)',
|
|
indvdum@0
|
220 |
'VARBINARY' => 'raw(255)',
|
|
indvdum@0
|
221 |
),
|
|
indvdum@0
|
222 |
|
|
indvdum@0
|
223 |
'sqlite' => array(
|
|
indvdum@0
|
224 |
'INT:' => 'int(%d)',
|
|
indvdum@0
|
225 |
'BINT' => 'bigint(20)',
|
|
indvdum@0
|
226 |
'UINT' => 'INTEGER UNSIGNED', //'mediumint(8) UNSIGNED',
|
|
indvdum@0
|
227 |
'UINT:' => 'INTEGER UNSIGNED', // 'int(%d) UNSIGNED',
|
|
indvdum@0
|
228 |
'TINT:' => 'tinyint(%d)',
|
|
indvdum@0
|
229 |
'USINT' => 'INTEGER UNSIGNED', //'mediumint(4) UNSIGNED',
|
|
indvdum@0
|
230 |
'BOOL' => 'INTEGER UNSIGNED', //'tinyint(1) UNSIGNED',
|
|
indvdum@0
|
231 |
'VCHAR' => 'varchar(255)',
|
|
indvdum@0
|
232 |
'VCHAR:' => 'varchar(%d)',
|
|
indvdum@0
|
233 |
'CHAR:' => 'char(%d)',
|
|
indvdum@0
|
234 |
'XSTEXT' => 'text(65535)',
|
|
indvdum@0
|
235 |
'STEXT' => 'text(65535)',
|
|
indvdum@0
|
236 |
'TEXT' => 'text(65535)',
|
|
indvdum@0
|
237 |
'MTEXT' => 'mediumtext(16777215)',
|
|
indvdum@0
|
238 |
'XSTEXT_UNI'=> 'text(65535)',
|
|
indvdum@0
|
239 |
'STEXT_UNI' => 'text(65535)',
|
|
indvdum@0
|
240 |
'TEXT_UNI' => 'text(65535)',
|
|
indvdum@0
|
241 |
'MTEXT_UNI' => 'mediumtext(16777215)',
|
|
indvdum@0
|
242 |
'TIMESTAMP' => 'INTEGER UNSIGNED', //'int(11) UNSIGNED',
|
|
indvdum@0
|
243 |
'DECIMAL' => 'decimal(5,2)',
|
|
indvdum@0
|
244 |
'DECIMAL:' => 'decimal(%d,2)',
|
|
indvdum@0
|
245 |
'PDECIMAL' => 'decimal(6,3)',
|
|
indvdum@0
|
246 |
'PDECIMAL:' => 'decimal(%d,3)',
|
|
indvdum@0
|
247 |
'VCHAR_UNI' => 'varchar(255)',
|
|
indvdum@0
|
248 |
'VCHAR_UNI:'=> 'varchar(%d)',
|
|
indvdum@0
|
249 |
'VCHAR_CI' => 'varchar(255)',
|
|
indvdum@0
|
250 |
'VARBINARY' => 'blob',
|
|
indvdum@0
|
251 |
),
|
|
indvdum@0
|
252 |
|
|
indvdum@0
|
253 |
'postgres' => array(
|
|
indvdum@0
|
254 |
'INT:' => 'INT4',
|
|
indvdum@0
|
255 |
'BINT' => 'INT8',
|
|
indvdum@0
|
256 |
'UINT' => 'INT4', // unsigned
|
|
indvdum@0
|
257 |
'UINT:' => 'INT4', // unsigned
|
|
indvdum@0
|
258 |
'USINT' => 'INT2', // unsigned
|
|
indvdum@0
|
259 |
'BOOL' => 'INT2', // unsigned
|
|
indvdum@0
|
260 |
'TINT:' => 'INT2',
|
|
indvdum@0
|
261 |
'VCHAR' => 'varchar(255)',
|
|
indvdum@0
|
262 |
'VCHAR:' => 'varchar(%d)',
|
|
indvdum@0
|
263 |
'CHAR:' => 'char(%d)',
|
|
indvdum@0
|
264 |
'XSTEXT' => 'varchar(1000)',
|
|
indvdum@0
|
265 |
'STEXT' => 'varchar(3000)',
|
|
indvdum@0
|
266 |
'TEXT' => 'varchar(8000)',
|
|
indvdum@0
|
267 |
'MTEXT' => 'TEXT',
|
|
indvdum@0
|
268 |
'XSTEXT_UNI'=> 'varchar(100)',
|
|
indvdum@0
|
269 |
'STEXT_UNI' => 'varchar(255)',
|
|
indvdum@0
|
270 |
'TEXT_UNI' => 'varchar(4000)',
|
|
indvdum@0
|
271 |
'MTEXT_UNI' => 'TEXT',
|
|
indvdum@0
|
272 |
'TIMESTAMP' => 'INT4', // unsigned
|
|
indvdum@0
|
273 |
'DECIMAL' => 'decimal(5,2)',
|
|
indvdum@0
|
274 |
'DECIMAL:' => 'decimal(%d,2)',
|
|
indvdum@0
|
275 |
'PDECIMAL' => 'decimal(6,3)',
|
|
indvdum@0
|
276 |
'PDECIMAL:' => 'decimal(%d,3)',
|
|
indvdum@0
|
277 |
'VCHAR_UNI' => 'varchar(255)',
|
|
indvdum@0
|
278 |
'VCHAR_UNI:'=> 'varchar(%d)',
|
|
indvdum@0
|
279 |
'VCHAR_CI' => 'varchar_ci',
|
|
indvdum@0
|
280 |
'VARBINARY' => 'bytea',
|
|
indvdum@0
|
281 |
),
|
|
indvdum@0
|
282 |
);
|
|
indvdum@0
|
283 |
|
|
indvdum@0
|
284 |
/**
|
|
indvdum@0
|
285 |
* A list of types being unsigned for better reference in some db's
|
|
indvdum@0
|
286 |
* @var array
|
|
indvdum@0
|
287 |
*/
|
|
indvdum@0
|
288 |
var $unsigned_types = array('UINT', 'UINT:', 'USINT', 'BOOL', 'TIMESTAMP');
|
|
indvdum@0
|
289 |
|
|
indvdum@0
|
290 |
/**
|
|
indvdum@0
|
291 |
* A list of supported DBMS. We change this class to support more DBMS, the DBMS itself only need to follow some rules.
|
|
indvdum@0
|
292 |
* @var array
|
|
indvdum@0
|
293 |
*/
|
|
indvdum@0
|
294 |
var $supported_dbms = array('firebird', 'mssql', 'mssqlnative', 'mysql_40', 'mysql_41', 'oracle', 'postgres', 'sqlite');
|
|
indvdum@0
|
295 |
|
|
indvdum@0
|
296 |
/**
|
|
indvdum@0
|
297 |
* This is set to true if user only wants to return the 'to-be-executed' SQL statement(s) (as an array).
|
|
indvdum@0
|
298 |
* This mode has no effect on some methods (inserting of data for example). This is expressed within the methods command.
|
|
indvdum@0
|
299 |
*/
|
|
indvdum@0
|
300 |
var $return_statements = false;
|
|
indvdum@0
|
301 |
|
|
indvdum@0
|
302 |
/**
|
|
indvdum@0
|
303 |
* Constructor. Set DB Object and set {@link $return_statements return_statements}.
|
|
indvdum@0
|
304 |
*
|
|
indvdum@0
|
305 |
* @param phpbb_dbal $db DBAL object
|
|
indvdum@0
|
306 |
* @param bool $return_statements True if only statements should be returned and no SQL being executed
|
|
indvdum@0
|
307 |
*/
|
|
indvdum@0
|
308 |
function phpbb_db_tools(&$db, $return_statements = false)
|
|
indvdum@0
|
309 |
{
|
|
indvdum@0
|
310 |
$this->db = $db;
|
|
indvdum@0
|
311 |
$this->return_statements = $return_statements;
|
|
indvdum@0
|
312 |
|
|
indvdum@0
|
313 |
// Determine mapping database type
|
|
indvdum@0
|
314 |
switch ($this->db->sql_layer)
|
|
indvdum@0
|
315 |
{
|
|
indvdum@0
|
316 |
case 'mysql':
|
|
indvdum@0
|
317 |
$this->sql_layer = 'mysql_40';
|
|
indvdum@0
|
318 |
break;
|
|
indvdum@0
|
319 |
|
|
indvdum@0
|
320 |
case 'mysql4':
|
|
indvdum@0
|
321 |
if (version_compare($this->db->sql_server_info(true), '4.1.3', '>='))
|
|
indvdum@0
|
322 |
{
|
|
indvdum@0
|
323 |
$this->sql_layer = 'mysql_41';
|
|
indvdum@0
|
324 |
}
|
|
indvdum@0
|
325 |
else
|
|
indvdum@0
|
326 |
{
|
|
indvdum@0
|
327 |
$this->sql_layer = 'mysql_40';
|
|
indvdum@0
|
328 |
}
|
|
indvdum@0
|
329 |
break;
|
|
indvdum@0
|
330 |
|
|
indvdum@0
|
331 |
case 'mysqli':
|
|
indvdum@0
|
332 |
$this->sql_layer = 'mysql_41';
|
|
indvdum@0
|
333 |
break;
|
|
indvdum@0
|
334 |
|
|
indvdum@0
|
335 |
case 'mssql':
|
|
indvdum@0
|
336 |
case 'mssql_odbc':
|
|
indvdum@0
|
337 |
$this->sql_layer = 'mssql';
|
|
indvdum@0
|
338 |
break;
|
|
indvdum@0
|
339 |
|
|
indvdum@0
|
340 |
case 'mssqlnative':
|
|
indvdum@0
|
341 |
$this->sql_layer = 'mssqlnative';
|
|
indvdum@0
|
342 |
break;
|
|
indvdum@0
|
343 |
|
|
indvdum@0
|
344 |
default:
|
|
indvdum@0
|
345 |
$this->sql_layer = $this->db->sql_layer;
|
|
indvdum@0
|
346 |
break;
|
|
indvdum@0
|
347 |
}
|
|
indvdum@0
|
348 |
}
|
|
indvdum@0
|
349 |
|
|
indvdum@0
|
350 |
/**
|
|
indvdum@0
|
351 |
* Gets a list of tables in the database.
|
|
indvdum@0
|
352 |
*
|
|
indvdum@0
|
353 |
* @return array Array of table names (all lower case)
|
|
indvdum@0
|
354 |
*/
|
|
indvdum@0
|
355 |
function sql_list_tables()
|
|
indvdum@0
|
356 |
{
|
|
indvdum@0
|
357 |
switch ($this->db->sql_layer)
|
|
indvdum@0
|
358 |
{
|
|
indvdum@0
|
359 |
case 'mysql':
|
|
indvdum@0
|
360 |
case 'mysql4':
|
|
indvdum@0
|
361 |
case 'mysqli':
|
|
indvdum@0
|
362 |
$sql = 'SHOW TABLES';
|
|
indvdum@0
|
363 |
break;
|
|
indvdum@0
|
364 |
|
|
indvdum@0
|
365 |
case 'sqlite':
|
|
indvdum@0
|
366 |
$sql = 'SELECT name
|
|
indvdum@0
|
367 |
FROM sqlite_master
|
|
indvdum@0
|
368 |
WHERE type = "table"';
|
|
indvdum@0
|
369 |
break;
|
|
indvdum@0
|
370 |
|
|
indvdum@0
|
371 |
case 'mssql':
|
|
indvdum@0
|
372 |
case 'mssql_odbc':
|
|
indvdum@0
|
373 |
case 'mssqlnative':
|
|
indvdum@0
|
374 |
$sql = "SELECT name
|
|
indvdum@0
|
375 |
FROM sysobjects
|
|
indvdum@0
|
376 |
WHERE type='U'";
|
|
indvdum@0
|
377 |
break;
|
|
indvdum@0
|
378 |
|
|
indvdum@0
|
379 |
case 'postgres':
|
|
indvdum@0
|
380 |
$sql = 'SELECT relname
|
|
indvdum@0
|
381 |
FROM pg_stat_user_tables';
|
|
indvdum@0
|
382 |
break;
|
|
indvdum@0
|
383 |
|
|
indvdum@0
|
384 |
case 'firebird':
|
|
indvdum@0
|
385 |
$sql = 'SELECT rdb$relation_name
|
|
indvdum@0
|
386 |
FROM rdb$relations
|
|
indvdum@0
|
387 |
WHERE rdb$view_source is null
|
|
indvdum@0
|
388 |
AND rdb$system_flag = 0';
|
|
indvdum@0
|
389 |
break;
|
|
indvdum@0
|
390 |
|
|
indvdum@0
|
391 |
case 'oracle':
|
|
indvdum@0
|
392 |
$sql = 'SELECT table_name
|
|
indvdum@0
|
393 |
FROM USER_TABLES';
|
|
indvdum@0
|
394 |
break;
|
|
indvdum@0
|
395 |
}
|
|
indvdum@0
|
396 |
|
|
indvdum@0
|
397 |
$result = $this->db->sql_query($sql);
|
|
indvdum@0
|
398 |
|
|
indvdum@0
|
399 |
$tables = array();
|
|
indvdum@0
|
400 |
while ($row = $this->db->sql_fetchrow($result))
|
|
indvdum@0
|
401 |
{
|
|
indvdum@0
|
402 |
$name = current($row);
|
|
indvdum@0
|
403 |
$tables[$name] = $name;
|
|
indvdum@0
|
404 |
}
|
|
indvdum@0
|
405 |
$this->db->sql_freeresult($result);
|
|
indvdum@0
|
406 |
|
|
indvdum@0
|
407 |
return $tables;
|
|
indvdum@0
|
408 |
}
|
|
indvdum@0
|
409 |
|
|
indvdum@0
|
410 |
/**
|
|
indvdum@0
|
411 |
* Check if table exists
|
|
indvdum@0
|
412 |
*
|
|
indvdum@0
|
413 |
*
|
|
indvdum@0
|
414 |
* @param string $table_name The table name to check for
|
|
indvdum@0
|
415 |
* @return bool true if table exists, else false
|
|
indvdum@0
|
416 |
*/
|
|
indvdum@0
|
417 |
function sql_table_exists($table_name)
|
|
indvdum@0
|
418 |
{
|
|
indvdum@0
|
419 |
$this->db->sql_return_on_error(true);
|
|
indvdum@0
|
420 |
$result = $this->db->sql_query_limit('SELECT * FROM ' . $table_name, 1);
|
|
indvdum@0
|
421 |
$this->db->sql_return_on_error(false);
|
|
indvdum@0
|
422 |
|
|
indvdum@0
|
423 |
if ($result)
|
|
indvdum@0
|
424 |
{
|
|
indvdum@0
|
425 |
$this->db->sql_freeresult($result);
|
|
indvdum@0
|
426 |
return true;
|
|
indvdum@0
|
427 |
}
|
|
indvdum@0
|
428 |
|
|
indvdum@0
|
429 |
return false;
|
|
indvdum@0
|
430 |
}
|
|
indvdum@0
|
431 |
|
|
indvdum@0
|
432 |
/**
|
|
indvdum@0
|
433 |
* Create SQL Table
|
|
indvdum@0
|
434 |
*
|
|
indvdum@0
|
435 |
* @param string $table_name The table name to create
|
|
indvdum@0
|
436 |
* @param array $table_data Array containing table data.
|
|
indvdum@0
|
437 |
* @return array Statements if $return_statements is true.
|
|
indvdum@0
|
438 |
*/
|
|
indvdum@0
|
439 |
function sql_create_table($table_name, $table_data)
|
|
indvdum@0
|
440 |
{
|
|
indvdum@0
|
441 |
// holds the DDL for a column
|
|
indvdum@0
|
442 |
$columns = $statements = array();
|
|
indvdum@0
|
443 |
|
|
indvdum@0
|
444 |
if ($this->sql_table_exists($table_name))
|
|
indvdum@0
|
445 |
{
|
|
indvdum@0
|
446 |
return $this->_sql_run_sql($statements);
|
|
indvdum@0
|
447 |
}
|
|
indvdum@0
|
448 |
|
|
indvdum@0
|
449 |
// Begin transaction
|
|
indvdum@0
|
450 |
$statements[] = 'begin';
|
|
indvdum@0
|
451 |
|
|
indvdum@0
|
452 |
// Determine if we have created a PRIMARY KEY in the earliest
|
|
indvdum@0
|
453 |
$primary_key_gen = false;
|
|
indvdum@0
|
454 |
|
|
indvdum@0
|
455 |
// Determine if the table must be created with TEXTIMAGE
|
|
indvdum@0
|
456 |
$create_textimage = false;
|
|
indvdum@0
|
457 |
|
|
indvdum@0
|
458 |
// Determine if the table requires a sequence
|
|
indvdum@0
|
459 |
$create_sequence = false;
|
|
indvdum@0
|
460 |
|
|
indvdum@0
|
461 |
// Begin table sql statement
|
|
indvdum@0
|
462 |
switch ($this->sql_layer)
|
|
indvdum@0
|
463 |
{
|
|
indvdum@0
|
464 |
case 'mssql':
|
|
indvdum@0
|
465 |
case 'mssqlnative':
|
|
indvdum@0
|
466 |
$table_sql = 'CREATE TABLE [' . $table_name . '] (' . "\n";
|
|
indvdum@0
|
467 |
break;
|
|
indvdum@0
|
468 |
|
|
indvdum@0
|
469 |
default:
|
|
indvdum@0
|
470 |
$table_sql = 'CREATE TABLE ' . $table_name . ' (' . "\n";
|
|
indvdum@0
|
471 |
break;
|
|
indvdum@0
|
472 |
}
|
|
indvdum@0
|
473 |
|
|
indvdum@0
|
474 |
// Iterate through the columns to create a table
|
|
indvdum@0
|
475 |
foreach ($table_data['COLUMNS'] as $column_name => $column_data)
|
|
indvdum@0
|
476 |
{
|
|
indvdum@0
|
477 |
// here lies an array, filled with information compiled on the column's data
|
|
indvdum@0
|
478 |
$prepared_column = $this->sql_prepare_column_data($table_name, $column_name, $column_data);
|
|
indvdum@0
|
479 |
|
|
indvdum@0
|
480 |
if (isset($prepared_column['auto_increment']) && strlen($column_name) > 26) // "${column_name}_gen"
|
|
indvdum@0
|
481 |
{
|
|
indvdum@0
|
482 |
trigger_error("Index name '${column_name}_gen' on table '$table_name' is too long. The maximum auto increment column length is 26 characters.", E_USER_ERROR);
|
|
indvdum@0
|
483 |
}
|
|
indvdum@0
|
484 |
|
|
indvdum@0
|
485 |
// here we add the definition of the new column to the list of columns
|
|
indvdum@0
|
486 |
switch ($this->sql_layer)
|
|
indvdum@0
|
487 |
{
|
|
indvdum@0
|
488 |
case 'mssql':
|
|
indvdum@0
|
489 |
case 'mssqlnative':
|
|
indvdum@0
|
490 |
$columns[] = "\t [{$column_name}] " . $prepared_column['column_type_sql_default'];
|
|
indvdum@0
|
491 |
break;
|
|
indvdum@0
|
492 |
|
|
indvdum@0
|
493 |
default:
|
|
indvdum@0
|
494 |
$columns[] = "\t {$column_name} " . $prepared_column['column_type_sql'];
|
|
indvdum@0
|
495 |
break;
|
|
indvdum@0
|
496 |
}
|
|
indvdum@0
|
497 |
|
|
indvdum@0
|
498 |
// see if we have found a primary key set due to a column definition if we have found it, we can stop looking
|
|
indvdum@0
|
499 |
if (!$primary_key_gen)
|
|
indvdum@0
|
500 |
{
|
|
indvdum@0
|
501 |
$primary_key_gen = isset($prepared_column['primary_key_set']) && $prepared_column['primary_key_set'];
|
|
indvdum@0
|
502 |
}
|
|
indvdum@0
|
503 |
|
|
indvdum@0
|
504 |
// create textimage DDL based off of the existance of certain column types
|
|
indvdum@0
|
505 |
if (!$create_textimage)
|
|
indvdum@0
|
506 |
{
|
|
indvdum@0
|
507 |
$create_textimage = isset($prepared_column['textimage']) && $prepared_column['textimage'];
|
|
indvdum@0
|
508 |
}
|
|
indvdum@0
|
509 |
|
|
indvdum@0
|
510 |
// create sequence DDL based off of the existance of auto incrementing columns
|
|
indvdum@0
|
511 |
if (!$create_sequence && isset($prepared_column['auto_increment']) && $prepared_column['auto_increment'])
|
|
indvdum@0
|
512 |
{
|
|
indvdum@0
|
513 |
$create_sequence = $column_name;
|
|
indvdum@0
|
514 |
}
|
|
indvdum@0
|
515 |
}
|
|
indvdum@0
|
516 |
|
|
indvdum@0
|
517 |
// this makes up all the columns in the create table statement
|
|
indvdum@0
|
518 |
$table_sql .= implode(",\n", $columns);
|
|
indvdum@0
|
519 |
|
|
indvdum@0
|
520 |
// Close the table for two DBMS and add to the statements
|
|
indvdum@0
|
521 |
switch ($this->sql_layer)
|
|
indvdum@0
|
522 |
{
|
|
indvdum@0
|
523 |
case 'firebird':
|
|
indvdum@0
|
524 |
$table_sql .= "\n);";
|
|
indvdum@0
|
525 |
$statements[] = $table_sql;
|
|
indvdum@0
|
526 |
break;
|
|
indvdum@0
|
527 |
|
|
indvdum@0
|
528 |
case 'mssql':
|
|
indvdum@0
|
529 |
case 'mssqlnative':
|
|
indvdum@0
|
530 |
$table_sql .= "\n) ON [PRIMARY]" . (($create_textimage) ? ' TEXTIMAGE_ON [PRIMARY]' : '');
|
|
indvdum@0
|
531 |
$statements[] = $table_sql;
|
|
indvdum@0
|
532 |
break;
|
|
indvdum@0
|
533 |
}
|
|
indvdum@0
|
534 |
|
|
indvdum@0
|
535 |
// we have yet to create a primary key for this table,
|
|
indvdum@0
|
536 |
// this means that we can add the one we really wanted instead
|
|
indvdum@0
|
537 |
if (!$primary_key_gen)
|
|
indvdum@0
|
538 |
{
|
|
indvdum@0
|
539 |
// Write primary key
|
|
indvdum@0
|
540 |
if (isset($table_data['PRIMARY_KEY']))
|
|
indvdum@0
|
541 |
{
|
|
indvdum@0
|
542 |
if (!is_array($table_data['PRIMARY_KEY']))
|
|
indvdum@0
|
543 |
{
|
|
indvdum@0
|
544 |
$table_data['PRIMARY_KEY'] = array($table_data['PRIMARY_KEY']);
|
|
indvdum@0
|
545 |
}
|
|
indvdum@0
|
546 |
|
|
indvdum@0
|
547 |
switch ($this->sql_layer)
|
|
indvdum@0
|
548 |
{
|
|
indvdum@0
|
549 |
case 'mysql_40':
|
|
indvdum@0
|
550 |
case 'mysql_41':
|
|
indvdum@0
|
551 |
case 'postgres':
|
|
indvdum@0
|
552 |
case 'sqlite':
|
|
indvdum@0
|
553 |
$table_sql .= ",\n\t PRIMARY KEY (" . implode(', ', $table_data['PRIMARY_KEY']) . ')';
|
|
indvdum@0
|
554 |
break;
|
|
indvdum@0
|
555 |
|
|
indvdum@0
|
556 |
case 'firebird':
|
|
indvdum@0
|
557 |
case 'mssql':
|
|
indvdum@0
|
558 |
case 'mssqlnative':
|
|
indvdum@0
|
559 |
// We need the data here
|
|
indvdum@0
|
560 |
$old_return_statements = $this->return_statements;
|
|
indvdum@0
|
561 |
$this->return_statements = true;
|
|
indvdum@0
|
562 |
|
|
indvdum@0
|
563 |
$primary_key_stmts = $this->sql_create_primary_key($table_name, $table_data['PRIMARY_KEY']);
|
|
indvdum@0
|
564 |
foreach ($primary_key_stmts as $pk_stmt)
|
|
indvdum@0
|
565 |
{
|
|
indvdum@0
|
566 |
$statements[] = $pk_stmt;
|
|
indvdum@0
|
567 |
}
|
|
indvdum@0
|
568 |
|
|
indvdum@0
|
569 |
$this->return_statements = $old_return_statements;
|
|
indvdum@0
|
570 |
break;
|
|
indvdum@0
|
571 |
|
|
indvdum@0
|
572 |
case 'oracle':
|
|
indvdum@0
|
573 |
$table_sql .= ",\n\t CONSTRAINT pk_{$table_name} PRIMARY KEY (" . implode(', ', $table_data['PRIMARY_KEY']) . ')';
|
|
indvdum@0
|
574 |
break;
|
|
indvdum@0
|
575 |
}
|
|
indvdum@0
|
576 |
}
|
|
indvdum@0
|
577 |
}
|
|
indvdum@0
|
578 |
|
|
indvdum@0
|
579 |
// close the table
|
|
indvdum@0
|
580 |
switch ($this->sql_layer)
|
|
indvdum@0
|
581 |
{
|
|
indvdum@0
|
582 |
case 'mysql_41':
|
|
indvdum@0
|
583 |
// make sure the table is in UTF-8 mode
|
|
indvdum@0
|
584 |
$table_sql .= "\n) CHARACTER SET `utf8` COLLATE `utf8_bin`;";
|
|
indvdum@0
|
585 |
$statements[] = $table_sql;
|
|
indvdum@0
|
586 |
break;
|
|
indvdum@0
|
587 |
|
|
indvdum@0
|
588 |
case 'mysql_40':
|
|
indvdum@0
|
589 |
case 'sqlite':
|
|
indvdum@0
|
590 |
$table_sql .= "\n);";
|
|
indvdum@0
|
591 |
$statements[] = $table_sql;
|
|
indvdum@0
|
592 |
break;
|
|
indvdum@0
|
593 |
|
|
indvdum@0
|
594 |
case 'postgres':
|
|
indvdum@0
|
595 |
// do we need to add a sequence for auto incrementing columns?
|
|
indvdum@0
|
596 |
if ($create_sequence)
|
|
indvdum@0
|
597 |
{
|
|
indvdum@0
|
598 |
$statements[] = "CREATE SEQUENCE {$table_name}_seq;";
|
|
indvdum@0
|
599 |
}
|
|
indvdum@0
|
600 |
|
|
indvdum@0
|
601 |
$table_sql .= "\n);";
|
|
indvdum@0
|
602 |
$statements[] = $table_sql;
|
|
indvdum@0
|
603 |
break;
|
|
indvdum@0
|
604 |
|
|
indvdum@0
|
605 |
case 'oracle':
|
|
indvdum@0
|
606 |
$table_sql .= "\n)";
|
|
indvdum@0
|
607 |
$statements[] = $table_sql;
|
|
indvdum@0
|
608 |
|
|
indvdum@0
|
609 |
// do we need to add a sequence and a tigger for auto incrementing columns?
|
|
indvdum@0
|
610 |
if ($create_sequence)
|
|
indvdum@0
|
611 |
{
|
|
indvdum@0
|
612 |
// create the actual sequence
|
|
indvdum@0
|
613 |
$statements[] = "CREATE SEQUENCE {$table_name}_seq";
|
|
indvdum@0
|
614 |
|
|
indvdum@0
|
615 |
// the trigger is the mechanism by which we increment the counter
|
|
indvdum@0
|
616 |
$trigger = "CREATE OR REPLACE TRIGGER t_{$table_name}\n";
|
|
indvdum@0
|
617 |
$trigger .= "BEFORE INSERT ON {$table_name}\n";
|
|
indvdum@0
|
618 |
$trigger .= "FOR EACH ROW WHEN (\n";
|
|
indvdum@0
|
619 |
$trigger .= "\tnew.{$create_sequence} IS NULL OR new.{$create_sequence} = 0\n";
|
|
indvdum@0
|
620 |
$trigger .= ")\n";
|
|
indvdum@0
|
621 |
$trigger .= "BEGIN\n";
|
|
indvdum@0
|
622 |
$trigger .= "\tSELECT {$table_name}_seq.nextval\n";
|
|
indvdum@0
|
623 |
$trigger .= "\tINTO :new.{$create_sequence}\n";
|
|
indvdum@0
|
624 |
$trigger .= "\tFROM dual;\n";
|
|
indvdum@0
|
625 |
$trigger .= "END;";
|
|
indvdum@0
|
626 |
|
|
indvdum@0
|
627 |
$statements[] = $trigger;
|
|
indvdum@0
|
628 |
}
|
|
indvdum@0
|
629 |
break;
|
|
indvdum@0
|
630 |
|
|
indvdum@0
|
631 |
case 'firebird':
|
|
indvdum@0
|
632 |
if ($create_sequence)
|
|
indvdum@0
|
633 |
{
|
|
indvdum@0
|
634 |
$statements[] = "CREATE GENERATOR {$table_name}_gen;";
|
|
indvdum@0
|
635 |
$statements[] = "SET GENERATOR {$table_name}_gen TO 0;";
|
|
indvdum@0
|
636 |
|
|
indvdum@0
|
637 |
$trigger = "CREATE TRIGGER t_$table_name FOR $table_name\n";
|
|
indvdum@0
|
638 |
$trigger .= "BEFORE INSERT\nAS\nBEGIN\n";
|
|
indvdum@0
|
639 |
$trigger .= "\tNEW.{$create_sequence} = GEN_ID({$table_name}_gen, 1);\nEND;";
|
|
indvdum@0
|
640 |
$statements[] = $trigger;
|
|
indvdum@0
|
641 |
}
|
|
indvdum@0
|
642 |
break;
|
|
indvdum@0
|
643 |
}
|
|
indvdum@0
|
644 |
|
|
indvdum@0
|
645 |
// Write Keys
|
|
indvdum@0
|
646 |
if (isset($table_data['KEYS']))
|
|
indvdum@0
|
647 |
{
|
|
indvdum@0
|
648 |
foreach ($table_data['KEYS'] as $key_name => $key_data)
|
|
indvdum@0
|
649 |
{
|
|
indvdum@0
|
650 |
if (!is_array($key_data[1]))
|
|
indvdum@0
|
651 |
{
|
|
indvdum@0
|
652 |
$key_data[1] = array($key_data[1]);
|
|
indvdum@0
|
653 |
}
|
|
indvdum@0
|
654 |
|
|
indvdum@0
|
655 |
$old_return_statements = $this->return_statements;
|
|
indvdum@0
|
656 |
$this->return_statements = true;
|
|
indvdum@0
|
657 |
|
|
indvdum@0
|
658 |
$key_stmts = ($key_data[0] == 'UNIQUE') ? $this->sql_create_unique_index($table_name, $key_name, $key_data[1]) : $this->sql_create_index($table_name, $key_name, $key_data[1]);
|
|
indvdum@0
|
659 |
|
|
indvdum@0
|
660 |
foreach ($key_stmts as $key_stmt)
|
|
indvdum@0
|
661 |
{
|
|
indvdum@0
|
662 |
$statements[] = $key_stmt;
|
|
indvdum@0
|
663 |
}
|
|
indvdum@0
|
664 |
|
|
indvdum@0
|
665 |
$this->return_statements = $old_return_statements;
|
|
indvdum@0
|
666 |
}
|
|
indvdum@0
|
667 |
}
|
|
indvdum@0
|
668 |
|
|
indvdum@0
|
669 |
// Commit Transaction
|
|
indvdum@0
|
670 |
$statements[] = 'commit';
|
|
indvdum@0
|
671 |
|
|
indvdum@0
|
672 |
return $this->_sql_run_sql($statements);
|
|
indvdum@0
|
673 |
}
|
|
indvdum@0
|
674 |
|
|
indvdum@0
|
675 |
/**
|
|
indvdum@0
|
676 |
* Handle passed database update array.
|
|
indvdum@0
|
677 |
* Expected structure...
|
|
indvdum@0
|
678 |
* Key being one of the following
|
|
indvdum@0
|
679 |
* change_columns: Column changes (only type, not name)
|
|
indvdum@0
|
680 |
* add_columns: Add columns to a table
|
|
indvdum@0
|
681 |
* drop_keys: Dropping keys
|
|
indvdum@0
|
682 |
* drop_columns: Removing/Dropping columns
|
|
indvdum@0
|
683 |
* add_primary_keys: adding primary keys
|
|
indvdum@0
|
684 |
* add_unique_index: adding an unique index
|
|
indvdum@0
|
685 |
* add_index: adding an index (can be column:index_size if you need to provide size)
|
|
indvdum@0
|
686 |
*
|
|
indvdum@0
|
687 |
* The values are in this format:
|
|
indvdum@0
|
688 |
* {TABLE NAME} => array(
|
|
indvdum@0
|
689 |
* {COLUMN NAME} => array({COLUMN TYPE}, {DEFAULT VALUE}, {OPTIONAL VARIABLES}),
|
|
indvdum@0
|
690 |
* {KEY/INDEX NAME} => array({COLUMN NAMES}),
|
|
indvdum@0
|
691 |
* )
|
|
indvdum@0
|
692 |
*
|
|
indvdum@0
|
693 |
* For more information have a look at /develop/create_schema_files.php (only available through SVN)
|
|
indvdum@0
|
694 |
*/
|
|
indvdum@0
|
695 |
function perform_schema_changes($schema_changes)
|
|
indvdum@0
|
696 |
{
|
|
indvdum@0
|
697 |
if (empty($schema_changes))
|
|
indvdum@0
|
698 |
{
|
|
indvdum@0
|
699 |
return;
|
|
indvdum@0
|
700 |
}
|
|
indvdum@0
|
701 |
|
|
indvdum@0
|
702 |
$statements = array();
|
|
indvdum@0
|
703 |
$sqlite = false;
|
|
indvdum@0
|
704 |
|
|
indvdum@0
|
705 |
// For SQLite we need to perform the schema changes in a much more different way
|
|
indvdum@0
|
706 |
if ($this->db->sql_layer == 'sqlite' && $this->return_statements)
|
|
indvdum@0
|
707 |
{
|
|
indvdum@0
|
708 |
$sqlite_data = array();
|
|
indvdum@0
|
709 |
$sqlite = true;
|
|
indvdum@0
|
710 |
}
|
|
indvdum@0
|
711 |
|
|
indvdum@0
|
712 |
// Drop tables?
|
|
indvdum@0
|
713 |
if (!empty($schema_changes['drop_tables']))
|
|
indvdum@0
|
714 |
{
|
|
indvdum@0
|
715 |
foreach ($schema_changes['drop_tables'] as $table)
|
|
indvdum@0
|
716 |
{
|
|
indvdum@0
|
717 |
// only drop table if it exists
|
|
indvdum@0
|
718 |
if ($this->sql_table_exists($table))
|
|
indvdum@0
|
719 |
{
|
|
indvdum@0
|
720 |
$result = $this->sql_table_drop($table);
|
|
indvdum@0
|
721 |
if ($this->return_statements)
|
|
indvdum@0
|
722 |
{
|
|
indvdum@0
|
723 |
$statements = array_merge($statements, $result);
|
|
indvdum@0
|
724 |
}
|
|
indvdum@0
|
725 |
}
|
|
indvdum@0
|
726 |
}
|
|
indvdum@0
|
727 |
}
|
|
indvdum@0
|
728 |
|
|
indvdum@0
|
729 |
// Add tables?
|
|
indvdum@0
|
730 |
if (!empty($schema_changes['add_tables']))
|
|
indvdum@0
|
731 |
{
|
|
indvdum@0
|
732 |
foreach ($schema_changes['add_tables'] as $table => $table_data)
|
|
indvdum@0
|
733 |
{
|
|
indvdum@0
|
734 |
$result = $this->sql_create_table($table, $table_data);
|
|
indvdum@0
|
735 |
if ($this->return_statements)
|
|
indvdum@0
|
736 |
{
|
|
indvdum@0
|
737 |
$statements = array_merge($statements, $result);
|
|
indvdum@0
|
738 |
}
|
|
indvdum@0
|
739 |
}
|
|
indvdum@0
|
740 |
}
|
|
indvdum@0
|
741 |
|
|
indvdum@0
|
742 |
// Change columns?
|
|
indvdum@0
|
743 |
if (!empty($schema_changes['change_columns']))
|
|
indvdum@0
|
744 |
{
|
|
indvdum@0
|
745 |
foreach ($schema_changes['change_columns'] as $table => $columns)
|
|
indvdum@0
|
746 |
{
|
|
indvdum@0
|
747 |
foreach ($columns as $column_name => $column_data)
|
|
indvdum@0
|
748 |
{
|
|
indvdum@0
|
749 |
// If the column exists we change it, else we add it ;)
|
|
indvdum@0
|
750 |
if ($column_exists = $this->sql_column_exists($table, $column_name))
|
|
indvdum@0
|
751 |
{
|
|
indvdum@0
|
752 |
$result = $this->sql_column_change($table, $column_name, $column_data, true);
|
|
indvdum@0
|
753 |
}
|
|
indvdum@0
|
754 |
else
|
|
indvdum@0
|
755 |
{
|
|
indvdum@0
|
756 |
$result = $this->sql_column_add($table, $column_name, $column_data, true);
|
|
indvdum@0
|
757 |
}
|
|
indvdum@0
|
758 |
|
|
indvdum@0
|
759 |
if ($sqlite)
|
|
indvdum@0
|
760 |
{
|
|
indvdum@0
|
761 |
if ($column_exists)
|
|
indvdum@0
|
762 |
{
|
|
indvdum@0
|
763 |
$sqlite_data[$table]['change_columns'][] = $result;
|
|
indvdum@0
|
764 |
}
|
|
indvdum@0
|
765 |
else
|
|
indvdum@0
|
766 |
{
|
|
indvdum@0
|
767 |
$sqlite_data[$table]['add_columns'][] = $result;
|
|
indvdum@0
|
768 |
}
|
|
indvdum@0
|
769 |
}
|
|
indvdum@0
|
770 |
else if ($this->return_statements)
|
|
indvdum@0
|
771 |
{
|
|
indvdum@0
|
772 |
$statements = array_merge($statements, $result);
|
|
indvdum@0
|
773 |
}
|
|
indvdum@0
|
774 |
}
|
|
indvdum@0
|
775 |
}
|
|
indvdum@0
|
776 |
}
|
|
indvdum@0
|
777 |
|
|
indvdum@0
|
778 |
// Add columns?
|
|
indvdum@0
|
779 |
if (!empty($schema_changes['add_columns']))
|
|
indvdum@0
|
780 |
{
|
|
indvdum@0
|
781 |
foreach ($schema_changes['add_columns'] as $table => $columns)
|
|
indvdum@0
|
782 |
{
|
|
indvdum@0
|
783 |
foreach ($columns as $column_name => $column_data)
|
|
indvdum@0
|
784 |
{
|
|
indvdum@0
|
785 |
// Only add the column if it does not exist yet
|
|
indvdum@0
|
786 |
if ($column_exists = $this->sql_column_exists($table, $column_name))
|
|
indvdum@0
|
787 |
{
|
|
indvdum@0
|
788 |
continue;
|
|
indvdum@0
|
789 |
// This is commented out here because it can take tremendous time on updates
|
|
indvdum@0
|
790 |
// $result = $this->sql_column_change($table, $column_name, $column_data, true);
|
|
indvdum@0
|
791 |
}
|
|
indvdum@0
|
792 |
else
|
|
indvdum@0
|
793 |
{
|
|
indvdum@0
|
794 |
$result = $this->sql_column_add($table, $column_name, $column_data, true);
|
|
indvdum@0
|
795 |
}
|
|
indvdum@0
|
796 |
|
|
indvdum@0
|
797 |
if ($sqlite)
|
|
indvdum@0
|
798 |
{
|
|
indvdum@0
|
799 |
if ($column_exists)
|
|
indvdum@0
|
800 |
{
|
|
indvdum@0
|
801 |
continue;
|
|
indvdum@0
|
802 |
// $sqlite_data[$table]['change_columns'][] = $result;
|
|
indvdum@0
|
803 |
}
|
|
indvdum@0
|
804 |
else
|
|
indvdum@0
|
805 |
{
|
|
indvdum@0
|
806 |
$sqlite_data[$table]['add_columns'][] = $result;
|
|
indvdum@0
|
807 |
}
|
|
indvdum@0
|
808 |
}
|
|
indvdum@0
|
809 |
else if ($this->return_statements)
|
|
indvdum@0
|
810 |
{
|
|
indvdum@0
|
811 |
$statements = array_merge($statements, $result);
|
|
indvdum@0
|
812 |
}
|
|
indvdum@0
|
813 |
}
|
|
indvdum@0
|
814 |
}
|
|
indvdum@0
|
815 |
}
|
|
indvdum@0
|
816 |
|
|
indvdum@0
|
817 |
// Remove keys?
|
|
indvdum@0
|
818 |
if (!empty($schema_changes['drop_keys']))
|
|
indvdum@0
|
819 |
{
|
|
indvdum@0
|
820 |
foreach ($schema_changes['drop_keys'] as $table => $indexes)
|
|
indvdum@0
|
821 |
{
|
|
indvdum@0
|
822 |
foreach ($indexes as $index_name)
|
|
indvdum@0
|
823 |
{
|
|
indvdum@0
|
824 |
if (!$this->sql_index_exists($table, $index_name))
|
|
indvdum@0
|
825 |
{
|
|
indvdum@0
|
826 |
continue;
|
|
indvdum@0
|
827 |
}
|
|
indvdum@0
|
828 |
|
|
indvdum@0
|
829 |
$result = $this->sql_index_drop($table, $index_name);
|
|
indvdum@0
|
830 |
|
|
indvdum@0
|
831 |
if ($this->return_statements)
|
|
indvdum@0
|
832 |
{
|
|
indvdum@0
|
833 |
$statements = array_merge($statements, $result);
|
|
indvdum@0
|
834 |
}
|
|
indvdum@0
|
835 |
}
|
|
indvdum@0
|
836 |
}
|
|
indvdum@0
|
837 |
}
|
|
indvdum@0
|
838 |
|
|
indvdum@0
|
839 |
// Drop columns?
|
|
indvdum@0
|
840 |
if (!empty($schema_changes['drop_columns']))
|
|
indvdum@0
|
841 |
{
|
|
indvdum@0
|
842 |
foreach ($schema_changes['drop_columns'] as $table => $columns)
|
|
indvdum@0
|
843 |
{
|
|
indvdum@0
|
844 |
foreach ($columns as $column)
|
|
indvdum@0
|
845 |
{
|
|
indvdum@0
|
846 |
// Only remove the column if it exists...
|
|
indvdum@0
|
847 |
if ($this->sql_column_exists($table, $column))
|
|
indvdum@0
|
848 |
{
|
|
indvdum@0
|
849 |
$result = $this->sql_column_remove($table, $column, true);
|
|
indvdum@0
|
850 |
|
|
indvdum@0
|
851 |
if ($sqlite)
|
|
indvdum@0
|
852 |
{
|
|
indvdum@0
|
853 |
$sqlite_data[$table]['drop_columns'][] = $result;
|
|
indvdum@0
|
854 |
}
|
|
indvdum@0
|
855 |
else if ($this->return_statements)
|
|
indvdum@0
|
856 |
{
|
|
indvdum@0
|
857 |
$statements = array_merge($statements, $result);
|
|
indvdum@0
|
858 |
}
|
|
indvdum@0
|
859 |
}
|
|
indvdum@0
|
860 |
}
|
|
indvdum@0
|
861 |
}
|
|
indvdum@0
|
862 |
}
|
|
indvdum@0
|
863 |
|
|
indvdum@0
|
864 |
// Add primary keys?
|
|
indvdum@0
|
865 |
if (!empty($schema_changes['add_primary_keys']))
|
|
indvdum@0
|
866 |
{
|
|
indvdum@0
|
867 |
foreach ($schema_changes['add_primary_keys'] as $table => $columns)
|
|
indvdum@0
|
868 |
{
|
|
indvdum@0
|
869 |
$result = $this->sql_create_primary_key($table, $columns, true);
|
|
indvdum@0
|
870 |
|
|
indvdum@0
|
871 |
if ($sqlite)
|
|
indvdum@0
|
872 |
{
|
|
indvdum@0
|
873 |
$sqlite_data[$table]['primary_key'] = $result;
|
|
indvdum@0
|
874 |
}
|
|
indvdum@0
|
875 |
else if ($this->return_statements)
|
|
indvdum@0
|
876 |
{
|
|
indvdum@0
|
877 |
$statements = array_merge($statements, $result);
|
|
indvdum@0
|
878 |
}
|
|
indvdum@0
|
879 |
}
|
|
indvdum@0
|
880 |
}
|
|
indvdum@0
|
881 |
|
|
indvdum@0
|
882 |
// Add unqiue indexes?
|
|
indvdum@0
|
883 |
if (!empty($schema_changes['add_unique_index']))
|
|
indvdum@0
|
884 |
{
|
|
indvdum@0
|
885 |
foreach ($schema_changes['add_unique_index'] as $table => $index_array)
|
|
indvdum@0
|
886 |
{
|
|
indvdum@0
|
887 |
foreach ($index_array as $index_name => $column)
|
|
indvdum@0
|
888 |
{
|
|
indvdum@0
|
889 |
if ($this->sql_unique_index_exists($table, $index_name))
|
|
indvdum@0
|
890 |
{
|
|
indvdum@0
|
891 |
continue;
|
|
indvdum@0
|
892 |
}
|
|
indvdum@0
|
893 |
|
|
indvdum@0
|
894 |
$result = $this->sql_create_unique_index($table, $index_name, $column);
|
|
indvdum@0
|
895 |
|
|
indvdum@0
|
896 |
if ($this->return_statements)
|
|
indvdum@0
|
897 |
{
|
|
indvdum@0
|
898 |
$statements = array_merge($statements, $result);
|
|
indvdum@0
|
899 |
}
|
|
indvdum@0
|
900 |
}
|
|
indvdum@0
|
901 |
}
|
|
indvdum@0
|
902 |
}
|
|
indvdum@0
|
903 |
|
|
indvdum@0
|
904 |
// Add indexes?
|
|
indvdum@0
|
905 |
if (!empty($schema_changes['add_index']))
|
|
indvdum@0
|
906 |
{
|
|
indvdum@0
|
907 |
foreach ($schema_changes['add_index'] as $table => $index_array)
|
|
indvdum@0
|
908 |
{
|
|
indvdum@0
|
909 |
foreach ($index_array as $index_name => $column)
|
|
indvdum@0
|
910 |
{
|
|
indvdum@0
|
911 |
if ($this->sql_index_exists($table, $index_name))
|
|
indvdum@0
|
912 |
{
|
|
indvdum@0
|
913 |
continue;
|
|
indvdum@0
|
914 |
}
|
|
indvdum@0
|
915 |
|
|
indvdum@0
|
916 |
$result = $this->sql_create_index($table, $index_name, $column);
|
|
indvdum@0
|
917 |
|
|
indvdum@0
|
918 |
if ($this->return_statements)
|
|
indvdum@0
|
919 |
{
|
|
indvdum@0
|
920 |
$statements = array_merge($statements, $result);
|
|
indvdum@0
|
921 |
}
|
|
indvdum@0
|
922 |
}
|
|
indvdum@0
|
923 |
}
|
|
indvdum@0
|
924 |
}
|
|
indvdum@0
|
925 |
|
|
indvdum@0
|
926 |
if ($sqlite)
|
|
indvdum@0
|
927 |
{
|
|
indvdum@0
|
928 |
foreach ($sqlite_data as $table_name => $sql_schema_changes)
|
|
indvdum@0
|
929 |
{
|
|
indvdum@0
|
930 |
// Create temporary table with original data
|
|
indvdum@0
|
931 |
$statements[] = 'begin';
|
|
indvdum@0
|
932 |
|
|
indvdum@0
|
933 |
$sql = "SELECT sql
|
|
indvdum@0
|
934 |
FROM sqlite_master
|
|
indvdum@0
|
935 |
WHERE type = 'table'
|
|
indvdum@0
|
936 |
AND name = '{$table_name}'
|
|
indvdum@0
|
937 |
ORDER BY type DESC, name;";
|
|
indvdum@0
|
938 |
$result = $this->db->sql_query($sql);
|
|
indvdum@0
|
939 |
|
|
indvdum@0
|
940 |
if (!$result)
|
|
indvdum@0
|
941 |
{
|
|
indvdum@0
|
942 |
continue;
|
|
indvdum@0
|
943 |
}
|
|
indvdum@0
|
944 |
|
|
indvdum@0
|
945 |
$row = $this->db->sql_fetchrow($result);
|
|
indvdum@0
|
946 |
$this->db->sql_freeresult($result);
|
|
indvdum@0
|
947 |
|
|
indvdum@0
|
948 |
// Create a backup table and populate it, destroy the existing one
|
|
indvdum@0
|
949 |
$statements[] = preg_replace('#CREATE\s+TABLE\s+"?' . $table_name . '"?#i', 'CREATE TEMPORARY TABLE ' . $table_name . '_temp', $row['sql']);
|
|
indvdum@0
|
950 |
$statements[] = 'INSERT INTO ' . $table_name . '_temp SELECT * FROM ' . $table_name;
|
|
indvdum@0
|
951 |
$statements[] = 'DROP TABLE ' . $table_name;
|
|
indvdum@0
|
952 |
|
|
indvdum@0
|
953 |
// Get the columns...
|
|
indvdum@0
|
954 |
preg_match('#\((.*)\)#s', $row['sql'], $matches);
|
|
indvdum@0
|
955 |
|
|
indvdum@0
|
956 |
$plain_table_cols = trim($matches[1]);
|
|
indvdum@0
|
957 |
$new_table_cols = preg_split('/,(?![\s\w]+\))/m', $plain_table_cols);
|
|
indvdum@0
|
958 |
$column_list = array();
|
|
indvdum@0
|
959 |
|
|
indvdum@0
|
960 |
foreach ($new_table_cols as $declaration)
|
|
indvdum@0
|
961 |
{
|
|
indvdum@0
|
962 |
$entities = preg_split('#\s+#', trim($declaration));
|
|
indvdum@0
|
963 |
if ($entities[0] == 'PRIMARY')
|
|
indvdum@0
|
964 |
{
|
|
indvdum@0
|
965 |
continue;
|
|
indvdum@0
|
966 |
}
|
|
indvdum@0
|
967 |
$column_list[] = $entities[0];
|
|
indvdum@0
|
968 |
}
|
|
indvdum@0
|
969 |
|
|
indvdum@0
|
970 |
// note down the primary key notation because sqlite only supports adding it to the end for the new table
|
|
indvdum@0
|
971 |
$primary_key = false;
|
|
indvdum@0
|
972 |
$_new_cols = array();
|
|
indvdum@0
|
973 |
|
|
indvdum@0
|
974 |
foreach ($new_table_cols as $key => $declaration)
|
|
indvdum@0
|
975 |
{
|
|
indvdum@0
|
976 |
$entities = preg_split('#\s+#', trim($declaration));
|
|
indvdum@0
|
977 |
if ($entities[0] == 'PRIMARY')
|
|
indvdum@0
|
978 |
{
|
|
indvdum@0
|
979 |
$primary_key = $declaration;
|
|
indvdum@0
|
980 |
continue;
|
|
indvdum@0
|
981 |
}
|
|
indvdum@0
|
982 |
$_new_cols[] = $declaration;
|
|
indvdum@0
|
983 |
}
|
|
indvdum@0
|
984 |
|
|
indvdum@0
|
985 |
$new_table_cols = $_new_cols;
|
|
indvdum@0
|
986 |
|
|
indvdum@0
|
987 |
// First of all... change columns
|
|
indvdum@0
|
988 |
if (!empty($sql_schema_changes['change_columns']))
|
|
indvdum@0
|
989 |
{
|
|
indvdum@0
|
990 |
foreach ($sql_schema_changes['change_columns'] as $column_sql)
|
|
indvdum@0
|
991 |
{
|
|
indvdum@0
|
992 |
foreach ($new_table_cols as $key => $declaration)
|
|
indvdum@0
|
993 |
{
|
|
indvdum@0
|
994 |
$entities = preg_split('#\s+#', trim($declaration));
|
|
indvdum@0
|
995 |
if (strpos($column_sql, $entities[0] . ' ') === 0)
|
|
indvdum@0
|
996 |
{
|
|
indvdum@0
|
997 |
$new_table_cols[$key] = $column_sql;
|
|
indvdum@0
|
998 |
}
|
|
indvdum@0
|
999 |
}
|
|
indvdum@0
|
1000 |
}
|
|
indvdum@0
|
1001 |
}
|
|
indvdum@0
|
1002 |
|
|
indvdum@0
|
1003 |
if (!empty($sql_schema_changes['add_columns']))
|
|
indvdum@0
|
1004 |
{
|
|
indvdum@0
|
1005 |
foreach ($sql_schema_changes['add_columns'] as $column_sql)
|
|
indvdum@0
|
1006 |
{
|
|
indvdum@0
|
1007 |
$new_table_cols[] = $column_sql;
|
|
indvdum@0
|
1008 |
}
|
|
indvdum@0
|
1009 |
}
|
|
indvdum@0
|
1010 |
|
|
indvdum@0
|
1011 |
// Now drop them...
|
|
indvdum@0
|
1012 |
if (!empty($sql_schema_changes['drop_columns']))
|
|
indvdum@0
|
1013 |
{
|
|
indvdum@0
|
1014 |
foreach ($sql_schema_changes['drop_columns'] as $column_name)
|
|
indvdum@0
|
1015 |
{
|
|
indvdum@0
|
1016 |
// Remove from column list...
|
|
indvdum@0
|
1017 |
$new_column_list = array();
|
|
indvdum@0
|
1018 |
foreach ($column_list as $key => $value)
|
|
indvdum@0
|
1019 |
{
|
|
indvdum@0
|
1020 |
if ($value === $column_name)
|
|
indvdum@0
|
1021 |
{
|
|
indvdum@0
|
1022 |
continue;
|
|
indvdum@0
|
1023 |
}
|
|
indvdum@0
|
1024 |
|
|
indvdum@0
|
1025 |
$new_column_list[] = $value;
|
|
indvdum@0
|
1026 |
}
|
|
indvdum@0
|
1027 |
|
|
indvdum@0
|
1028 |
$column_list = $new_column_list;
|
|
indvdum@0
|
1029 |
|
|
indvdum@0
|
1030 |
// Remove from table...
|
|
indvdum@0
|
1031 |
$_new_cols = array();
|
|
indvdum@0
|
1032 |
foreach ($new_table_cols as $key => $declaration)
|
|
indvdum@0
|
1033 |
{
|
|
indvdum@0
|
1034 |
$entities = preg_split('#\s+#', trim($declaration));
|
|
indvdum@0
|
1035 |
if (strpos($column_name . ' ', $entities[0] . ' ') === 0)
|
|
indvdum@0
|
1036 |
{
|
|
indvdum@0
|
1037 |
continue;
|
|
indvdum@0
|
1038 |
}
|
|
indvdum@0
|
1039 |
$_new_cols[] = $declaration;
|
|
indvdum@0
|
1040 |
}
|
|
indvdum@0
|
1041 |
$new_table_cols = $_new_cols;
|
|
indvdum@0
|
1042 |
}
|
|
indvdum@0
|
1043 |
}
|
|
indvdum@0
|
1044 |
|
|
indvdum@0
|
1045 |
// Primary key...
|
|
indvdum@0
|
1046 |
if (!empty($sql_schema_changes['primary_key']))
|
|
indvdum@0
|
1047 |
{
|
|
indvdum@0
|
1048 |
$new_table_cols[] = 'PRIMARY KEY (' . implode(', ', $sql_schema_changes['primary_key']) . ')';
|
|
indvdum@0
|
1049 |
}
|
|
indvdum@0
|
1050 |
// Add a new one or the old primary key
|
|
indvdum@0
|
1051 |
else if ($primary_key !== false)
|
|
indvdum@0
|
1052 |
{
|
|
indvdum@0
|
1053 |
$new_table_cols[] = $primary_key;
|
|
indvdum@0
|
1054 |
}
|
|
indvdum@0
|
1055 |
|
|
indvdum@0
|
1056 |
$columns = implode(',', $column_list);
|
|
indvdum@0
|
1057 |
|
|
indvdum@0
|
1058 |
// create a new table and fill it up. destroy the temp one
|
|
indvdum@0
|
1059 |
$statements[] = 'CREATE TABLE ' . $table_name . ' (' . implode(',', $new_table_cols) . ');';
|
|
indvdum@0
|
1060 |
$statements[] = 'INSERT INTO ' . $table_name . ' (' . $columns . ') SELECT ' . $columns . ' FROM ' . $table_name . '_temp;';
|
|
indvdum@0
|
1061 |
$statements[] = 'DROP TABLE ' . $table_name . '_temp';
|
|
indvdum@0
|
1062 |
|
|
indvdum@0
|
1063 |
$statements[] = 'commit';
|
|
indvdum@0
|
1064 |
}
|
|
indvdum@0
|
1065 |
}
|
|
indvdum@0
|
1066 |
|
|
indvdum@0
|
1067 |
if ($this->return_statements)
|
|
indvdum@0
|
1068 |
{
|
|
indvdum@0
|
1069 |
return $statements;
|
|
indvdum@0
|
1070 |
}
|
|
indvdum@0
|
1071 |
}
|
|
indvdum@0
|
1072 |
|
|
indvdum@0
|
1073 |
/**
|
|
indvdum@0
|
1074 |
* Gets a list of columns of a table.
|
|
indvdum@0
|
1075 |
*
|
|
indvdum@0
|
1076 |
* @param string $table Table name
|
|
indvdum@0
|
1077 |
*
|
|
indvdum@0
|
1078 |
* @return array Array of column names (all lower case)
|
|
indvdum@0
|
1079 |
*/
|
|
indvdum@0
|
1080 |
function sql_list_columns($table)
|
|
indvdum@0
|
1081 |
{
|
|
indvdum@0
|
1082 |
$columns = array();
|
|
indvdum@0
|
1083 |
|
|
indvdum@0
|
1084 |
switch ($this->sql_layer)
|
|
indvdum@0
|
1085 |
{
|
|
indvdum@0
|
1086 |
case 'mysql_40':
|
|
indvdum@0
|
1087 |
case 'mysql_41':
|
|
indvdum@0
|
1088 |
$sql = "SHOW COLUMNS FROM $table";
|
|
indvdum@0
|
1089 |
break;
|
|
indvdum@0
|
1090 |
|
|
indvdum@0
|
1091 |
// PostgreSQL has a way of doing this in a much simpler way but would
|
|
indvdum@0
|
1092 |
// not allow us to support all versions of PostgreSQL
|
|
indvdum@0
|
1093 |
case 'postgres':
|
|
indvdum@0
|
1094 |
$sql = "SELECT a.attname
|
|
indvdum@0
|
1095 |
FROM pg_class c, pg_attribute a
|
|
indvdum@0
|
1096 |
WHERE c.relname = '{$table}'
|
|
indvdum@0
|
1097 |
AND a.attnum > 0
|
|
indvdum@0
|
1098 |
AND a.attrelid = c.oid";
|
|
indvdum@0
|
1099 |
break;
|
|
indvdum@0
|
1100 |
|
|
indvdum@0
|
1101 |
// same deal with PostgreSQL, we must perform more complex operations than
|
|
indvdum@0
|
1102 |
// we technically could
|
|
indvdum@0
|
1103 |
case 'mssql':
|
|
indvdum@0
|
1104 |
case 'mssqlnative':
|
|
indvdum@0
|
1105 |
$sql = "SELECT c.name
|
|
indvdum@0
|
1106 |
FROM syscolumns c
|
|
indvdum@0
|
1107 |
LEFT JOIN sysobjects o ON c.id = o.id
|
|
indvdum@0
|
1108 |
WHERE o.name = '{$table}'";
|
|
indvdum@0
|
1109 |
break;
|
|
indvdum@0
|
1110 |
|
|
indvdum@0
|
1111 |
case 'oracle':
|
|
indvdum@0
|
1112 |
$sql = "SELECT column_name
|
|
indvdum@0
|
1113 |
FROM user_tab_columns
|
|
indvdum@0
|
1114 |
WHERE LOWER(table_name) = '" . strtolower($table) . "'";
|
|
indvdum@0
|
1115 |
break;
|
|
indvdum@0
|
1116 |
|
|
indvdum@0
|
1117 |
case 'firebird':
|
|
indvdum@0
|
1118 |
$sql = "SELECT RDB\$FIELD_NAME as FNAME
|
|
indvdum@0
|
1119 |
FROM RDB\$RELATION_FIELDS
|
|
indvdum@0
|
1120 |
WHERE RDB\$RELATION_NAME = '" . strtoupper($table) . "'";
|
|
indvdum@0
|
1121 |
break;
|
|
indvdum@0
|
1122 |
|
|
indvdum@0
|
1123 |
case 'sqlite':
|
|
indvdum@0
|
1124 |
$sql = "SELECT sql
|
|
indvdum@0
|
1125 |
FROM sqlite_master
|
|
indvdum@0
|
1126 |
WHERE type = 'table'
|
|
indvdum@0
|
1127 |
AND name = '{$table}'";
|
|
indvdum@0
|
1128 |
|
|
indvdum@0
|
1129 |
$result = $this->db->sql_query($sql);
|
|
indvdum@0
|
1130 |
|
|
indvdum@0
|
1131 |
if (!$result)
|
|
indvdum@0
|
1132 |
{
|
|
indvdum@0
|
1133 |
return false;
|
|
indvdum@0
|
1134 |
}
|
|
indvdum@0
|
1135 |
|
|
indvdum@0
|
1136 |
$row = $this->db->sql_fetchrow($result);
|
|
indvdum@0
|
1137 |
$this->db->sql_freeresult($result);
|
|
indvdum@0
|
1138 |
|
|
indvdum@0
|
1139 |
preg_match('#\((.*)\)#s', $row['sql'], $matches);
|
|
indvdum@0
|
1140 |
|
|
indvdum@0
|
1141 |
$cols = trim($matches[1]);
|
|
indvdum@0
|
1142 |
$col_array = preg_split('/,(?![\s\w]+\))/m', $cols);
|
|
indvdum@0
|
1143 |
|
|
indvdum@0
|
1144 |
foreach ($col_array as $declaration)
|
|
indvdum@0
|
1145 |
{
|
|
indvdum@0
|
1146 |
$entities = preg_split('#\s+#', trim($declaration));
|
|
indvdum@0
|
1147 |
if ($entities[0] == 'PRIMARY')
|
|
indvdum@0
|
1148 |
{
|
|
indvdum@0
|
1149 |
continue;
|
|
indvdum@0
|
1150 |
}
|
|
indvdum@0
|
1151 |
|
|
indvdum@0
|
1152 |
$column = strtolower($entities[0]);
|
|
indvdum@0
|
1153 |
$columns[$column] = $column;
|
|
indvdum@0
|
1154 |
}
|
|
indvdum@0
|
1155 |
|
|
indvdum@0
|
1156 |
return $columns;
|
|
indvdum@0
|
1157 |
break;
|
|
indvdum@0
|
1158 |
}
|
|
indvdum@0
|
1159 |
|
|
indvdum@0
|
1160 |
$result = $this->db->sql_query($sql);
|
|
indvdum@0
|
1161 |
|
|
indvdum@0
|
1162 |
while ($row = $this->db->sql_fetchrow($result))
|
|
indvdum@0
|
1163 |
{
|
|
indvdum@0
|
1164 |
$column = strtolower(current($row));
|
|
indvdum@0
|
1165 |
$columns[$column] = $column;
|
|
indvdum@0
|
1166 |
}
|
|
indvdum@0
|
1167 |
$this->db->sql_freeresult($result);
|
|
indvdum@0
|
1168 |
|
|
indvdum@0
|
1169 |
return $columns;
|
|
indvdum@0
|
1170 |
}
|
|
indvdum@0
|
1171 |
|
|
indvdum@0
|
1172 |
/**
|
|
indvdum@0
|
1173 |
* Check whether a specified column exist in a table
|
|
indvdum@0
|
1174 |
*
|
|
indvdum@0
|
1175 |
* @param string $table Table to check
|
|
indvdum@0
|
1176 |
* @param string $column_name Column to check
|
|
indvdum@0
|
1177 |
*
|
|
indvdum@0
|
1178 |
* @return bool True if column exists, false otherwise
|
|
indvdum@0
|
1179 |
*/
|
|
indvdum@0
|
1180 |
function sql_column_exists($table, $column_name)
|
|
indvdum@0
|
1181 |
{
|
|
indvdum@0
|
1182 |
$columns = $this->sql_list_columns($table);
|
|
indvdum@0
|
1183 |
|
|
indvdum@0
|
1184 |
return isset($columns[$column_name]);
|
|
indvdum@0
|
1185 |
}
|
|
indvdum@0
|
1186 |
|
|
indvdum@0
|
1187 |
/**
|
|
indvdum@0
|
1188 |
* Check if a specified index exists in table. Does not return PRIMARY KEY and UNIQUE indexes.
|
|
indvdum@0
|
1189 |
*
|
|
indvdum@0
|
1190 |
* @param string $table_name Table to check the index at
|
|
indvdum@0
|
1191 |
* @param string $index_name The index name to check
|
|
indvdum@0
|
1192 |
*
|
|
indvdum@0
|
1193 |
* @return bool True if index exists, else false
|
|
indvdum@0
|
1194 |
*/
|
|
indvdum@0
|
1195 |
function sql_index_exists($table_name, $index_name)
|
|
indvdum@0
|
1196 |
{
|
|
indvdum@0
|
1197 |
if ($this->sql_layer == 'mssql' || $this->sql_layer == 'mssqlnative')
|
|
indvdum@0
|
1198 |
{
|
|
indvdum@0
|
1199 |
$sql = "EXEC sp_statistics '$table_name'";
|
|
indvdum@0
|
1200 |
$result = $this->db->sql_query($sql);
|
|
indvdum@0
|
1201 |
|
|
indvdum@0
|
1202 |
while ($row = $this->db->sql_fetchrow($result))
|
|
indvdum@0
|
1203 |
{
|
|
indvdum@0
|
1204 |
if ($row['TYPE'] == 3)
|
|
indvdum@0
|
1205 |
{
|
|
indvdum@0
|
1206 |
if (strtolower($row['INDEX_NAME']) == strtolower($index_name))
|
|
indvdum@0
|
1207 |
{
|
|
indvdum@0
|
1208 |
$this->db->sql_freeresult($result);
|
|
indvdum@0
|
1209 |
return true;
|
|
indvdum@0
|
1210 |
}
|
|
indvdum@0
|
1211 |
}
|
|
indvdum@0
|
1212 |
}
|
|
indvdum@0
|
1213 |
$this->db->sql_freeresult($result);
|
|
indvdum@0
|
1214 |
|
|
indvdum@0
|
1215 |
return false;
|
|
indvdum@0
|
1216 |
}
|
|
indvdum@0
|
1217 |
|
|
indvdum@0
|
1218 |
switch ($this->sql_layer)
|
|
indvdum@0
|
1219 |
{
|
|
indvdum@0
|
1220 |
case 'firebird':
|
|
indvdum@0
|
1221 |
$sql = "SELECT LOWER(RDB\$INDEX_NAME) as index_name
|
|
indvdum@0
|
1222 |
FROM RDB\$INDICES
|
|
indvdum@0
|
1223 |
WHERE RDB\$RELATION_NAME = '" . strtoupper($table_name) . "'
|
|
indvdum@0
|
1224 |
AND RDB\$UNIQUE_FLAG IS NULL
|
|
indvdum@0
|
1225 |
AND RDB\$FOREIGN_KEY IS NULL";
|
|
indvdum@0
|
1226 |
$col = 'index_name';
|
|
indvdum@0
|
1227 |
break;
|
|
indvdum@0
|
1228 |
|
|
indvdum@0
|
1229 |
case 'postgres':
|
|
indvdum@0
|
1230 |
$sql = "SELECT ic.relname as index_name
|
|
indvdum@0
|
1231 |
FROM pg_class bc, pg_class ic, pg_index i
|
|
indvdum@0
|
1232 |
WHERE (bc.oid = i.indrelid)
|
|
indvdum@0
|
1233 |
AND (ic.oid = i.indexrelid)
|
|
indvdum@0
|
1234 |
AND (bc.relname = '" . $table_name . "')
|
|
indvdum@0
|
1235 |
AND (i.indisunique != 't')
|
|
indvdum@0
|
1236 |
AND (i.indisprimary != 't')";
|
|
indvdum@0
|
1237 |
$col = 'index_name';
|
|
indvdum@0
|
1238 |
break;
|
|
indvdum@0
|
1239 |
|
|
indvdum@0
|
1240 |
case 'mysql_40':
|
|
indvdum@0
|
1241 |
case 'mysql_41':
|
|
indvdum@0
|
1242 |
$sql = 'SHOW KEYS
|
|
indvdum@0
|
1243 |
FROM ' . $table_name;
|
|
indvdum@0
|
1244 |
$col = 'Key_name';
|
|
indvdum@0
|
1245 |
break;
|
|
indvdum@0
|
1246 |
|
|
indvdum@0
|
1247 |
case 'oracle':
|
|
indvdum@0
|
1248 |
$sql = "SELECT index_name
|
|
indvdum@0
|
1249 |
FROM user_indexes
|
|
indvdum@0
|
1250 |
WHERE table_name = '" . strtoupper($table_name) . "'
|
|
indvdum@0
|
1251 |
AND generated = 'N'
|
|
indvdum@0
|
1252 |
AND uniqueness = 'NONUNIQUE'";
|
|
indvdum@0
|
1253 |
$col = 'index_name';
|
|
indvdum@0
|
1254 |
break;
|
|
indvdum@0
|
1255 |
|
|
indvdum@0
|
1256 |
case 'sqlite':
|
|
indvdum@0
|
1257 |
$sql = "PRAGMA index_list('" . $table_name . "');";
|
|
indvdum@0
|
1258 |
$col = 'name';
|
|
indvdum@0
|
1259 |
break;
|
|
indvdum@0
|
1260 |
}
|
|
indvdum@0
|
1261 |
|
|
indvdum@0
|
1262 |
$result = $this->db->sql_query($sql);
|
|
indvdum@0
|
1263 |
while ($row = $this->db->sql_fetchrow($result))
|
|
indvdum@0
|
1264 |
{
|
|
indvdum@0
|
1265 |
if (($this->sql_layer == 'mysql_40' || $this->sql_layer == 'mysql_41') && !$row['Non_unique'])
|
|
indvdum@0
|
1266 |
{
|
|
indvdum@0
|
1267 |
continue;
|
|
indvdum@0
|
1268 |
}
|
|
indvdum@0
|
1269 |
|
|
indvdum@0
|
1270 |
// These DBMS prefix index name with the table name
|
|
indvdum@0
|
1271 |
switch ($this->sql_layer)
|
|
indvdum@0
|
1272 |
{
|
|
indvdum@0
|
1273 |
case 'firebird':
|
|
indvdum@0
|
1274 |
case 'oracle':
|
|
indvdum@0
|
1275 |
case 'postgres':
|
|
indvdum@0
|
1276 |
case 'sqlite':
|
|
indvdum@0
|
1277 |
$row[$col] = substr($row[$col], strlen($table_name) + 1);
|
|
indvdum@0
|
1278 |
break;
|
|
indvdum@0
|
1279 |
}
|
|
indvdum@0
|
1280 |
|
|
indvdum@0
|
1281 |
if (strtolower($row[$col]) == strtolower($index_name))
|
|
indvdum@0
|
1282 |
{
|
|
indvdum@0
|
1283 |
$this->db->sql_freeresult($result);
|
|
indvdum@0
|
1284 |
return true;
|
|
indvdum@0
|
1285 |
}
|
|
indvdum@0
|
1286 |
}
|
|
indvdum@0
|
1287 |
$this->db->sql_freeresult($result);
|
|
indvdum@0
|
1288 |
|
|
indvdum@0
|
1289 |
return false;
|
|
indvdum@0
|
1290 |
}
|
|
indvdum@0
|
1291 |
|
|
indvdum@0
|
1292 |
/**
|
|
indvdum@0
|
1293 |
* Check if a specified index exists in table. Does not return PRIMARY KEY and UNIQUE indexes.
|
|
indvdum@0
|
1294 |
*
|
|
indvdum@0
|
1295 |
* @param string $table_name Table to check the index at
|
|
indvdum@0
|
1296 |
* @param string $index_name The index name to check
|
|
indvdum@0
|
1297 |
*
|
|
indvdum@0
|
1298 |
* @return bool True if index exists, else false
|
|
indvdum@0
|
1299 |
*/
|
|
indvdum@0
|
1300 |
function sql_unique_index_exists($table_name, $index_name)
|
|
indvdum@0
|
1301 |
{
|
|
indvdum@0
|
1302 |
if ($this->sql_layer == 'mssql' || $this->sql_layer == 'mssqlnative')
|
|
indvdum@0
|
1303 |
{
|
|
indvdum@0
|
1304 |
$sql = "EXEC sp_statistics '$table_name'";
|
|
indvdum@0
|
1305 |
$result = $this->db->sql_query($sql);
|
|
indvdum@0
|
1306 |
|
|
indvdum@0
|
1307 |
while ($row = $this->db->sql_fetchrow($result))
|
|
indvdum@0
|
1308 |
{
|
|
indvdum@0
|
1309 |
// Usually NON_UNIQUE is the column we want to check, but we allow for both
|
|
indvdum@0
|
1310 |
if ($row['TYPE'] == 3)
|
|
indvdum@0
|
1311 |
{
|
|
indvdum@0
|
1312 |
if (strtolower($row['INDEX_NAME']) == strtolower($index_name))
|
|
indvdum@0
|
1313 |
{
|
|
indvdum@0
|
1314 |
$this->db->sql_freeresult($result);
|
|
indvdum@0
|
1315 |
return true;
|
|
indvdum@0
|
1316 |
}
|
|
indvdum@0
|
1317 |
}
|
|
indvdum@0
|
1318 |
}
|
|
indvdum@0
|
1319 |
$this->db->sql_freeresult($result);
|
|
indvdum@0
|
1320 |
return false;
|
|
indvdum@0
|
1321 |
}
|
|
indvdum@0
|
1322 |
|
|
indvdum@0
|
1323 |
switch ($this->sql_layer)
|
|
indvdum@0
|
1324 |
{
|
|
indvdum@0
|
1325 |
case 'firebird':
|
|
indvdum@0
|
1326 |
$sql = "SELECT LOWER(RDB\$INDEX_NAME) as index_name
|
|
indvdum@0
|
1327 |
FROM RDB\$INDICES
|
|
indvdum@0
|
1328 |
WHERE RDB\$RELATION_NAME = '" . strtoupper($table_name) . "'
|
|
indvdum@0
|
1329 |
AND RDB\$UNIQUE_FLAG IS NOT NULL
|
|
indvdum@0
|
1330 |
AND RDB\$FOREIGN_KEY IS NULL";
|
|
indvdum@0
|
1331 |
$col = 'index_name';
|
|
indvdum@0
|
1332 |
break;
|
|
indvdum@0
|
1333 |
|
|
indvdum@0
|
1334 |
case 'postgres':
|
|
indvdum@0
|
1335 |
$sql = "SELECT ic.relname as index_name, i.indisunique
|
|
indvdum@0
|
1336 |
FROM pg_class bc, pg_class ic, pg_index i
|
|
indvdum@0
|
1337 |
WHERE (bc.oid = i.indrelid)
|
|
indvdum@0
|
1338 |
AND (ic.oid = i.indexrelid)
|
|
indvdum@0
|
1339 |
AND (bc.relname = '" . $table_name . "')
|
|
indvdum@0
|
1340 |
AND (i.indisprimary != 't')";
|
|
indvdum@0
|
1341 |
$col = 'index_name';
|
|
indvdum@0
|
1342 |
break;
|
|
indvdum@0
|
1343 |
|
|
indvdum@0
|
1344 |
case 'mysql_40':
|
|
indvdum@0
|
1345 |
case 'mysql_41':
|
|
indvdum@0
|
1346 |
$sql = 'SHOW KEYS
|
|
indvdum@0
|
1347 |
FROM ' . $table_name;
|
|
indvdum@0
|
1348 |
$col = 'Key_name';
|
|
indvdum@0
|
1349 |
break;
|
|
indvdum@0
|
1350 |
|
|
indvdum@0
|
1351 |
case 'oracle':
|
|
indvdum@0
|
1352 |
$sql = "SELECT index_name, table_owner
|
|
indvdum@0
|
1353 |
FROM user_indexes
|
|
indvdum@0
|
1354 |
WHERE table_name = '" . strtoupper($table_name) . "'
|
|
indvdum@0
|
1355 |
AND generated = 'N'
|
|
indvdum@0
|
1356 |
AND uniqueness = 'UNIQUE'";
|
|
indvdum@0
|
1357 |
$col = 'index_name';
|
|
indvdum@0
|
1358 |
break;
|
|
indvdum@0
|
1359 |
|
|
indvdum@0
|
1360 |
case 'sqlite':
|
|
indvdum@0
|
1361 |
$sql = "PRAGMA index_list('" . $table_name . "');";
|
|
indvdum@0
|
1362 |
$col = 'name';
|
|
indvdum@0
|
1363 |
break;
|
|
indvdum@0
|
1364 |
}
|
|
indvdum@0
|
1365 |
|
|
indvdum@0
|
1366 |
$result = $this->db->sql_query($sql);
|
|
indvdum@0
|
1367 |
while ($row = $this->db->sql_fetchrow($result))
|
|
indvdum@0
|
1368 |
{
|
|
indvdum@0
|
1369 |
if (($this->sql_layer == 'mysql_40' || $this->sql_layer == 'mysql_41') && ($row['Non_unique'] || $row[$col] == 'PRIMARY'))
|
|
indvdum@0
|
1370 |
{
|
|
indvdum@0
|
1371 |
continue;
|
|
indvdum@0
|
1372 |
}
|
|
indvdum@0
|
1373 |
|
|
indvdum@0
|
1374 |
if ($this->sql_layer == 'sqlite' && !$row['unique'])
|
|
indvdum@0
|
1375 |
{
|
|
indvdum@0
|
1376 |
continue;
|
|
indvdum@0
|
1377 |
}
|
|
indvdum@0
|
1378 |
|
|
indvdum@0
|
1379 |
if ($this->sql_layer == 'postgres' && $row['indisunique'] != 't')
|
|
indvdum@0
|
1380 |
{
|
|
indvdum@0
|
1381 |
continue;
|
|
indvdum@0
|
1382 |
}
|
|
indvdum@0
|
1383 |
|
|
indvdum@0
|
1384 |
// These DBMS prefix index name with the table name
|
|
indvdum@0
|
1385 |
switch ($this->sql_layer)
|
|
indvdum@0
|
1386 |
{
|
|
indvdum@0
|
1387 |
case 'oracle':
|
|
indvdum@0
|
1388 |
// Two cases here... prefixed with U_[table_owner] and not prefixed with table_name
|
|
indvdum@0
|
1389 |
if (strpos($row[$col], 'U_') === 0)
|
|
indvdum@0
|
1390 |
{
|
|
indvdum@0
|
1391 |
$row[$col] = substr($row[$col], strlen('U_' . $row['table_owner']) + 1);
|
|
indvdum@0
|
1392 |
}
|
|
indvdum@0
|
1393 |
else if (strpos($row[$col], strtoupper($table_name)) === 0)
|
|
indvdum@0
|
1394 |
{
|
|
indvdum@0
|
1395 |
$row[$col] = substr($row[$col], strlen($table_name) + 1);
|
|
indvdum@0
|
1396 |
}
|
|
indvdum@0
|
1397 |
break;
|
|
indvdum@0
|
1398 |
|
|
indvdum@0
|
1399 |
case 'firebird':
|
|
indvdum@0
|
1400 |
case 'postgres':
|
|
indvdum@0
|
1401 |
case 'sqlite':
|
|
indvdum@0
|
1402 |
$row[$col] = substr($row[$col], strlen($table_name) + 1);
|
|
indvdum@0
|
1403 |
break;
|
|
indvdum@0
|
1404 |
}
|
|
indvdum@0
|
1405 |
|
|
indvdum@0
|
1406 |
if (strtolower($row[$col]) == strtolower($index_name))
|
|
indvdum@0
|
1407 |
{
|
|
indvdum@0
|
1408 |
$this->db->sql_freeresult($result);
|
|
indvdum@0
|
1409 |
return true;
|
|
indvdum@0
|
1410 |
}
|
|
indvdum@0
|
1411 |
}
|
|
indvdum@0
|
1412 |
$this->db->sql_freeresult($result);
|
|
indvdum@0
|
1413 |
|
|
indvdum@0
|
1414 |
return false;
|
|
indvdum@0
|
1415 |
}
|
|
indvdum@0
|
1416 |
|
|
indvdum@0
|
1417 |
/**
|
|
indvdum@0
|
1418 |
* Private method for performing sql statements (either execute them or return them)
|
|
indvdum@0
|
1419 |
* @access private
|
|
indvdum@0
|
1420 |
*/
|
|
indvdum@0
|
1421 |
function _sql_run_sql($statements)
|
|
indvdum@0
|
1422 |
{
|
|
indvdum@0
|
1423 |
if ($this->return_statements)
|
|
indvdum@0
|
1424 |
{
|
|
indvdum@0
|
1425 |
return $statements;
|
|
indvdum@0
|
1426 |
}
|
|
indvdum@0
|
1427 |
|
|
indvdum@0
|
1428 |
// We could add error handling here...
|
|
indvdum@0
|
1429 |
foreach ($statements as $sql)
|
|
indvdum@0
|
1430 |
{
|
|
indvdum@0
|
1431 |
if ($sql === 'begin')
|
|
indvdum@0
|
1432 |
{
|
|
indvdum@0
|
1433 |
$this->db->sql_transaction('begin');
|
|
indvdum@0
|
1434 |
}
|
|
indvdum@0
|
1435 |
else if ($sql === 'commit')
|
|
indvdum@0
|
1436 |
{
|
|
indvdum@0
|
1437 |
$this->db->sql_transaction('commit');
|
|
indvdum@0
|
1438 |
}
|
|
indvdum@0
|
1439 |
else
|
|
indvdum@0
|
1440 |
{
|
|
indvdum@0
|
1441 |
$this->db->sql_query($sql);
|
|
indvdum@0
|
1442 |
}
|
|
indvdum@0
|
1443 |
}
|
|
indvdum@0
|
1444 |
|
|
indvdum@0
|
1445 |
return true;
|
|
indvdum@0
|
1446 |
}
|
|
indvdum@0
|
1447 |
|
|
indvdum@0
|
1448 |
/**
|
|
indvdum@0
|
1449 |
* Function to prepare some column information for better usage
|
|
indvdum@0
|
1450 |
* @access private
|
|
indvdum@0
|
1451 |
*/
|
|
indvdum@0
|
1452 |
function sql_prepare_column_data($table_name, $column_name, $column_data)
|
|
indvdum@0
|
1453 |
{
|
|
indvdum@0
|
1454 |
if (strlen($column_name) > 30)
|
|
indvdum@0
|
1455 |
{
|
|
indvdum@0
|
1456 |
trigger_error("Column name '$column_name' on table '$table_name' is too long. The maximum is 30 characters.", E_USER_ERROR);
|
|
indvdum@0
|
1457 |
}
|
|
indvdum@0
|
1458 |
|
|
indvdum@0
|
1459 |
// Get type
|
|
indvdum@0
|
1460 |
if (strpos($column_data[0], ':') !== false)
|
|
indvdum@0
|
1461 |
{
|
|
indvdum@0
|
1462 |
list($orig_column_type, $column_length) = explode(':', $column_data[0]);
|
|
indvdum@0
|
1463 |
if (!is_array($this->dbms_type_map[$this->sql_layer][$orig_column_type . ':']))
|
|
indvdum@0
|
1464 |
{
|
|
indvdum@0
|
1465 |
$column_type = sprintf($this->dbms_type_map[$this->sql_layer][$orig_column_type . ':'], $column_length);
|
|
indvdum@0
|
1466 |
}
|
|
indvdum@0
|
1467 |
else
|
|
indvdum@0
|
1468 |
{
|
|
indvdum@0
|
1469 |
if (isset($this->dbms_type_map[$this->sql_layer][$orig_column_type . ':']['rule']))
|
|
indvdum@0
|
1470 |
{
|
|
indvdum@0
|
1471 |
switch ($this->dbms_type_map[$this->sql_layer][$orig_column_type . ':']['rule'][0])
|
|
indvdum@0
|
1472 |
{
|
|
indvdum@0
|
1473 |
case 'div':
|
|
indvdum@0
|
1474 |
$column_length /= $this->dbms_type_map[$this->sql_layer][$orig_column_type . ':']['rule'][1];
|
|
indvdum@0
|
1475 |
$column_length = ceil($column_length);
|
|
indvdum@0
|
1476 |
$column_type = sprintf($this->dbms_type_map[$this->sql_layer][$orig_column_type . ':'][0], $column_length);
|
|
indvdum@0
|
1477 |
break;
|
|
indvdum@0
|
1478 |
}
|
|
indvdum@0
|
1479 |
}
|
|
indvdum@0
|
1480 |
|
|
indvdum@0
|
1481 |
if (isset($this->dbms_type_map[$this->sql_layer][$orig_column_type . ':']['limit']))
|
|
indvdum@0
|
1482 |
{
|
|
indvdum@0
|
1483 |
switch ($this->dbms_type_map[$this->sql_layer][$orig_column_type . ':']['limit'][0])
|
|
indvdum@0
|
1484 |
{
|
|
indvdum@0
|
1485 |
case 'mult':
|
|
indvdum@0
|
1486 |
$column_length *= $this->dbms_type_map[$this->sql_layer][$orig_column_type . ':']['limit'][1];
|
|
indvdum@0
|
1487 |
if ($column_length > $this->dbms_type_map[$this->sql_layer][$orig_column_type . ':']['limit'][2])
|
|
indvdum@0
|
1488 |
{
|
|
indvdum@0
|
1489 |
$column_type = $this->dbms_type_map[$this->sql_layer][$orig_column_type . ':']['limit'][3];
|
|
indvdum@0
|
1490 |
}
|
|
indvdum@0
|
1491 |
else
|
|
indvdum@0
|
1492 |
{
|
|
indvdum@0
|
1493 |
$column_type = sprintf($this->dbms_type_map[$this->sql_layer][$orig_column_type . ':'][0], $column_length);
|
|
indvdum@0
|
1494 |
}
|
|
indvdum@0
|
1495 |
break;
|
|
indvdum@0
|
1496 |
}
|
|
indvdum@0
|
1497 |
}
|
|
indvdum@0
|
1498 |
}
|
|
indvdum@0
|
1499 |
$orig_column_type .= ':';
|
|
indvdum@0
|
1500 |
}
|
|
indvdum@0
|
1501 |
else
|
|
indvdum@0
|
1502 |
{
|
|
indvdum@0
|
1503 |
$orig_column_type = $column_data[0];
|
|
indvdum@0
|
1504 |
$column_type = $this->dbms_type_map[$this->sql_layer][$column_data[0]];
|
|
indvdum@0
|
1505 |
}
|
|
indvdum@0
|
1506 |
|
|
indvdum@0
|
1507 |
// Adjust default value if db-dependant specified
|
|
indvdum@0
|
1508 |
if (is_array($column_data[1]))
|
|
indvdum@0
|
1509 |
{
|
|
indvdum@0
|
1510 |
$column_data[1] = (isset($column_data[1][$this->sql_layer])) ? $column_data[1][$this->sql_layer] : $column_data[1]['default'];
|
|
indvdum@0
|
1511 |
}
|
|
indvdum@0
|
1512 |
|
|
indvdum@0
|
1513 |
$sql = '';
|
|
indvdum@0
|
1514 |
|
|
indvdum@0
|
1515 |
$return_array = array();
|
|
indvdum@0
|
1516 |
|
|
indvdum@0
|
1517 |
switch ($this->sql_layer)
|
|
indvdum@0
|
1518 |
{
|
|
indvdum@0
|
1519 |
case 'firebird':
|
|
indvdum@0
|
1520 |
$sql .= " {$column_type} ";
|
|
indvdum@0
|
1521 |
$return_array['column_type_sql_type'] = " {$column_type} ";
|
|
indvdum@0
|
1522 |
|
|
indvdum@0
|
1523 |
if (!is_null($column_data[1]))
|
|
indvdum@0
|
1524 |
{
|
|
indvdum@0
|
1525 |
$sql .= 'DEFAULT ' . ((is_numeric($column_data[1])) ? $column_data[1] : "'{$column_data[1]}'") . ' ';
|
|
indvdum@0
|
1526 |
$return_array['column_type_sql_default'] = ((is_numeric($column_data[1])) ? $column_data[1] : "'{$column_data[1]}'") . ' ';
|
|
indvdum@0
|
1527 |
}
|
|
indvdum@0
|
1528 |
|
|
indvdum@0
|
1529 |
$sql .= 'NOT NULL';
|
|
indvdum@0
|
1530 |
|
|
indvdum@0
|
1531 |
// This is a UNICODE column and thus should be given it's fair share
|
|
indvdum@0
|
1532 |
if (preg_match('/^X?STEXT_UNI|VCHAR_(CI|UNI:?)/', $column_data[0]))
|
|
indvdum@0
|
1533 |
{
|
|
indvdum@0
|
1534 |
$sql .= ' COLLATE UNICODE';
|
|
indvdum@0
|
1535 |
}
|
|
indvdum@0
|
1536 |
|
|
indvdum@0
|
1537 |
$return_array['auto_increment'] = false;
|
|
indvdum@0
|
1538 |
if (isset($column_data[2]) && $column_data[2] == 'auto_increment')
|
|
indvdum@0
|
1539 |
{
|
|
indvdum@0
|
1540 |
$return_array['auto_increment'] = true;
|
|
indvdum@0
|
1541 |
}
|
|
indvdum@0
|
1542 |
|
|
indvdum@0
|
1543 |
break;
|
|
indvdum@0
|
1544 |
|
|
indvdum@0
|
1545 |
case 'mssql':
|
|
indvdum@0
|
1546 |
case 'mssqlnative':
|
|
indvdum@0
|
1547 |
$sql .= " {$column_type} ";
|
|
indvdum@0
|
1548 |
$sql_default = " {$column_type} ";
|
|
indvdum@0
|
1549 |
|
|
indvdum@0
|
1550 |
// For adding columns we need the default definition
|
|
indvdum@0
|
1551 |
if (!is_null($column_data[1]))
|
|
indvdum@0
|
1552 |
{
|
|
indvdum@0
|
1553 |
// For hexadecimal values do not use single quotes
|
|
indvdum@0
|
1554 |
if (strpos($column_data[1], '0x') === 0)
|
|
indvdum@0
|
1555 |
{
|
|
indvdum@0
|
1556 |
$return_array['default'] = 'DEFAULT (' . $column_data[1] . ') ';
|
|
indvdum@0
|
1557 |
$sql_default .= $return_array['default'];
|
|
indvdum@0
|
1558 |
}
|
|
indvdum@0
|
1559 |
else
|
|
indvdum@0
|
1560 |
{
|
|
indvdum@0
|
1561 |
$return_array['default'] = 'DEFAULT (' . ((is_numeric($column_data[1])) ? $column_data[1] : "'{$column_data[1]}'") . ') ';
|
|
indvdum@0
|
1562 |
$sql_default .= $return_array['default'];
|
|
indvdum@0
|
1563 |
}
|
|
indvdum@0
|
1564 |
}
|
|
indvdum@0
|
1565 |
|
|
indvdum@0
|
1566 |
if (isset($column_data[2]) && $column_data[2] == 'auto_increment')
|
|
indvdum@0
|
1567 |
{
|
|
indvdum@0
|
1568 |
// $sql .= 'IDENTITY (1, 1) ';
|
|
indvdum@0
|
1569 |
$sql_default .= 'IDENTITY (1, 1) ';
|
|
indvdum@0
|
1570 |
}
|
|
indvdum@0
|
1571 |
|
|
indvdum@0
|
1572 |
$return_array['textimage'] = $column_type === '[text]';
|
|
indvdum@0
|
1573 |
|
|
indvdum@0
|
1574 |
$sql .= 'NOT NULL';
|
|
indvdum@0
|
1575 |
$sql_default .= 'NOT NULL';
|
|
indvdum@0
|
1576 |
|
|
indvdum@0
|
1577 |
$return_array['column_type_sql_default'] = $sql_default;
|
|
indvdum@0
|
1578 |
|
|
indvdum@0
|
1579 |
break;
|
|
indvdum@0
|
1580 |
|
|
indvdum@0
|
1581 |
case 'mysql_40':
|
|
indvdum@0
|
1582 |
case 'mysql_41':
|
|
indvdum@0
|
1583 |
$sql .= " {$column_type} ";
|
|
indvdum@0
|
1584 |
|
|
indvdum@0
|
1585 |
// For hexadecimal values do not use single quotes
|
|
indvdum@0
|
1586 |
if (!is_null($column_data[1]) && substr($column_type, -4) !== 'text' && substr($column_type, -4) !== 'blob')
|
|
indvdum@0
|
1587 |
{
|
|
indvdum@0
|
1588 |
$sql .= (strpos($column_data[1], '0x') === 0) ? "DEFAULT {$column_data[1]} " : "DEFAULT '{$column_data[1]}' ";
|
|
indvdum@0
|
1589 |
}
|
|
indvdum@0
|
1590 |
$sql .= 'NOT NULL';
|
|
indvdum@0
|
1591 |
|
|
indvdum@0
|
1592 |
if (isset($column_data[2]))
|
|
indvdum@0
|
1593 |
{
|
|
indvdum@0
|
1594 |
if ($column_data[2] == 'auto_increment')
|
|
indvdum@0
|
1595 |
{
|
|
indvdum@0
|
1596 |
$sql .= ' auto_increment';
|
|
indvdum@0
|
1597 |
}
|
|
indvdum@0
|
1598 |
else if ($this->sql_layer === 'mysql_41' && $column_data[2] == 'true_sort')
|
|
indvdum@0
|
1599 |
{
|
|
indvdum@0
|
1600 |
$sql .= ' COLLATE utf8_unicode_ci';
|
|
indvdum@0
|
1601 |
}
|
|
indvdum@0
|
1602 |
}
|
|
indvdum@0
|
1603 |
|
|
indvdum@0
|
1604 |
break;
|
|
indvdum@0
|
1605 |
|
|
indvdum@0
|
1606 |
case 'oracle':
|
|
indvdum@0
|
1607 |
$sql .= " {$column_type} ";
|
|
indvdum@0
|
1608 |
$sql .= (!is_null($column_data[1])) ? "DEFAULT '{$column_data[1]}' " : '';
|
|
indvdum@0
|
1609 |
|
|
indvdum@0
|
1610 |
// In Oracle empty strings ('') are treated as NULL.
|
|
indvdum@0
|
1611 |
// Therefore in oracle we allow NULL's for all DEFAULT '' entries
|
|
indvdum@0
|
1612 |
// Oracle does not like setting NOT NULL on a column that is already NOT NULL (this happens only on number fields)
|
|
indvdum@0
|
1613 |
if (!preg_match('/number/i', $column_type))
|
|
indvdum@0
|
1614 |
{
|
|
indvdum@0
|
1615 |
$sql .= ($column_data[1] === '') ? '' : 'NOT NULL';
|
|
indvdum@0
|
1616 |
}
|
|
indvdum@0
|
1617 |
|
|
indvdum@0
|
1618 |
$return_array['auto_increment'] = false;
|
|
indvdum@0
|
1619 |
if (isset($column_data[2]) && $column_data[2] == 'auto_increment')
|
|
indvdum@0
|
1620 |
{
|
|
indvdum@0
|
1621 |
$return_array['auto_increment'] = true;
|
|
indvdum@0
|
1622 |
}
|
|
indvdum@0
|
1623 |
|
|
indvdum@0
|
1624 |
break;
|
|
indvdum@0
|
1625 |
|
|
indvdum@0
|
1626 |
case 'postgres':
|
|
indvdum@0
|
1627 |
$return_array['column_type'] = $column_type;
|
|
indvdum@0
|
1628 |
|
|
indvdum@0
|
1629 |
$sql .= " {$column_type} ";
|
|
indvdum@0
|
1630 |
|
|
indvdum@0
|
1631 |
$return_array['auto_increment'] = false;
|
|
indvdum@0
|
1632 |
if (isset($column_data[2]) && $column_data[2] == 'auto_increment')
|
|
indvdum@0
|
1633 |
{
|
|
indvdum@0
|
1634 |
$default_val = "nextval('{$table_name}_seq')";
|
|
indvdum@0
|
1635 |
$return_array['auto_increment'] = true;
|
|
indvdum@0
|
1636 |
}
|
|
indvdum@0
|
1637 |
else if (!is_null($column_data[1]))
|
|
indvdum@0
|
1638 |
{
|
|
indvdum@0
|
1639 |
$default_val = "'" . $column_data[1] . "'";
|
|
indvdum@0
|
1640 |
$return_array['null'] = 'NOT NULL';
|
|
indvdum@0
|
1641 |
$sql .= 'NOT NULL ';
|
|
indvdum@0
|
1642 |
}
|
|
indvdum@0
|
1643 |
|
|
indvdum@0
|
1644 |
$return_array['default'] = $default_val;
|
|
indvdum@0
|
1645 |
|
|
indvdum@0
|
1646 |
$sql .= "DEFAULT {$default_val}";
|
|
indvdum@0
|
1647 |
|
|
indvdum@0
|
1648 |
// Unsigned? Then add a CHECK contraint
|
|
indvdum@0
|
1649 |
if (in_array($orig_column_type, $this->unsigned_types))
|
|
indvdum@0
|
1650 |
{
|
|
indvdum@0
|
1651 |
$return_array['constraint'] = "CHECK ({$column_name} >= 0)";
|
|
indvdum@0
|
1652 |
$sql .= " CHECK ({$column_name} >= 0)";
|
|
indvdum@0
|
1653 |
}
|
|
indvdum@0
|
1654 |
|
|
indvdum@0
|
1655 |
break;
|
|
indvdum@0
|
1656 |
|
|
indvdum@0
|
1657 |
case 'sqlite':
|
|
indvdum@0
|
1658 |
$return_array['primary_key_set'] = false;
|
|
indvdum@0
|
1659 |
if (isset($column_data[2]) && $column_data[2] == 'auto_increment')
|
|
indvdum@0
|
1660 |
{
|
|
indvdum@0
|
1661 |
$sql .= ' INTEGER PRIMARY KEY';
|
|
indvdum@0
|
1662 |
$return_array['primary_key_set'] = true;
|
|
indvdum@0
|
1663 |
}
|
|
indvdum@0
|
1664 |
else
|
|
indvdum@0
|
1665 |
{
|
|
indvdum@0
|
1666 |
$sql .= ' ' . $column_type;
|
|
indvdum@0
|
1667 |
}
|
|
indvdum@0
|
1668 |
|
|
indvdum@0
|
1669 |
$sql .= ' NOT NULL ';
|
|
indvdum@0
|
1670 |
$sql .= (!is_null($column_data[1])) ? "DEFAULT '{$column_data[1]}'" : '';
|
|
indvdum@0
|
1671 |
|
|
indvdum@0
|
1672 |
break;
|
|
indvdum@0
|
1673 |
}
|
|
indvdum@0
|
1674 |
|
|
indvdum@0
|
1675 |
$return_array['column_type_sql'] = $sql;
|
|
indvdum@0
|
1676 |
|
|
indvdum@0
|
1677 |
return $return_array;
|
|
indvdum@0
|
1678 |
}
|
|
indvdum@0
|
1679 |
|
|
indvdum@0
|
1680 |
/**
|
|
indvdum@0
|
1681 |
* Add new column
|
|
indvdum@0
|
1682 |
*/
|
|
indvdum@0
|
1683 |
function sql_column_add($table_name, $column_name, $column_data, $inline = false)
|
|
indvdum@0
|
1684 |
{
|
|
indvdum@0
|
1685 |
$column_data = $this->sql_prepare_column_data($table_name, $column_name, $column_data);
|
|
indvdum@0
|
1686 |
$statements = array();
|
|
indvdum@0
|
1687 |
|
|
indvdum@0
|
1688 |
switch ($this->sql_layer)
|
|
indvdum@0
|
1689 |
{
|
|
indvdum@0
|
1690 |
case 'firebird':
|
|
indvdum@0
|
1691 |
// Does not support AFTER statement, only POSITION (and there you need the column position)
|
|
indvdum@0
|
1692 |
$statements[] = 'ALTER TABLE ' . $table_name . ' ADD "' . strtoupper($column_name) . '" ' . $column_data['column_type_sql'];
|
|
indvdum@0
|
1693 |
break;
|
|
indvdum@0
|
1694 |
|
|
indvdum@0
|
1695 |
case 'mssql':
|
|
indvdum@0
|
1696 |
case 'mssqlnative':
|
|
indvdum@0
|
1697 |
// Does not support AFTER, only through temporary table
|
|
indvdum@0
|
1698 |
$statements[] = 'ALTER TABLE [' . $table_name . '] ADD [' . $column_name . '] ' . $column_data['column_type_sql_default'];
|
|
indvdum@0
|
1699 |
break;
|
|
indvdum@0
|
1700 |
|
|
indvdum@0
|
1701 |
case 'mysql_40':
|
|
indvdum@0
|
1702 |
case 'mysql_41':
|
|
indvdum@0
|
1703 |
$after = (!empty($column_data['after'])) ? ' AFTER ' . $column_data['after'] : '';
|
|
indvdum@0
|
1704 |
$statements[] = 'ALTER TABLE `' . $table_name . '` ADD COLUMN `' . $column_name . '` ' . $column_data['column_type_sql'] . $after;
|
|
indvdum@0
|
1705 |
break;
|
|
indvdum@0
|
1706 |
|
|
indvdum@0
|
1707 |
case 'oracle':
|
|
indvdum@0
|
1708 |
// Does not support AFTER, only through temporary table
|
|
indvdum@0
|
1709 |
$statements[] = 'ALTER TABLE ' . $table_name . ' ADD ' . $column_name . ' ' . $column_data['column_type_sql'];
|
|
indvdum@0
|
1710 |
break;
|
|
indvdum@0
|
1711 |
|
|
indvdum@0
|
1712 |
case 'postgres':
|
|
indvdum@0
|
1713 |
// Does not support AFTER, only through temporary table
|
|
indvdum@0
|
1714 |
if (version_compare($this->db->sql_server_info(true), '8.0', '>='))
|
|
indvdum@0
|
1715 |
{
|
|
indvdum@0
|
1716 |
$statements[] = 'ALTER TABLE ' . $table_name . ' ADD COLUMN "' . $column_name . '" ' . $column_data['column_type_sql'];
|
|
indvdum@0
|
1717 |
}
|
|
indvdum@0
|
1718 |
else
|
|
indvdum@0
|
1719 |
{
|
|
indvdum@0
|
1720 |
// old versions cannot add columns with default and null information
|
|
indvdum@0
|
1721 |
$statements[] = 'ALTER TABLE ' . $table_name . ' ADD COLUMN "' . $column_name . '" ' . $column_data['column_type'] . ' ' . $column_data['constraint'];
|
|
indvdum@0
|
1722 |
|
|
indvdum@0
|
1723 |
if (isset($column_data['null']))
|
|
indvdum@0
|
1724 |
{
|
|
indvdum@0
|
1725 |
if ($column_data['null'] == 'NOT NULL')
|
|
indvdum@0
|
1726 |
{
|
|
indvdum@0
|
1727 |
$statements[] = 'ALTER TABLE ' . $table_name . ' ALTER COLUMN ' . $column_name . ' SET NOT NULL';
|
|
indvdum@0
|
1728 |
}
|
|
indvdum@0
|
1729 |
}
|
|
indvdum@0
|
1730 |
|
|
indvdum@0
|
1731 |
if (isset($column_data['default']))
|
|
indvdum@0
|
1732 |
{
|
|
indvdum@0
|
1733 |
$statements[] = 'ALTER TABLE ' . $table_name . ' ALTER COLUMN ' . $column_name . ' SET DEFAULT ' . $column_data['default'];
|
|
indvdum@0
|
1734 |
}
|
|
indvdum@0
|
1735 |
}
|
|
indvdum@0
|
1736 |
|
|
indvdum@0
|
1737 |
break;
|
|
indvdum@0
|
1738 |
|
|
indvdum@0
|
1739 |
case 'sqlite':
|
|
indvdum@0
|
1740 |
|
|
indvdum@0
|
1741 |
if ($inline && $this->return_statements)
|
|
indvdum@0
|
1742 |
{
|
|
indvdum@0
|
1743 |
return $column_name . ' ' . $column_data['column_type_sql'];
|
|
indvdum@0
|
1744 |
}
|
|
indvdum@0
|
1745 |
|
|
indvdum@0
|
1746 |
if (version_compare(sqlite_libversion(), '3.0') == -1)
|
|
indvdum@0
|
1747 |
{
|
|
indvdum@0
|
1748 |
$sql = "SELECT sql
|
|
indvdum@0
|
1749 |
FROM sqlite_master
|
|
indvdum@0
|
1750 |
WHERE type = 'table'
|
|
indvdum@0
|
1751 |
AND name = '{$table_name}'
|
|
indvdum@0
|
1752 |
ORDER BY type DESC, name;";
|
|
indvdum@0
|
1753 |
$result = $this->db->sql_query($sql);
|
|
indvdum@0
|
1754 |
|
|
indvdum@0
|
1755 |
if (!$result)
|
|
indvdum@0
|
1756 |
{
|
|
indvdum@0
|
1757 |
break;
|
|
indvdum@0
|
1758 |
}
|
|
indvdum@0
|
1759 |
|
|
indvdum@0
|
1760 |
$row = $this->db->sql_fetchrow($result);
|
|
indvdum@0
|
1761 |
$this->db->sql_freeresult($result);
|
|
indvdum@0
|
1762 |
|
|
indvdum@0
|
1763 |
$statements[] = 'begin';
|
|
indvdum@0
|
1764 |
|
|
indvdum@0
|
1765 |
// Create a backup table and populate it, destroy the existing one
|
|
indvdum@0
|
1766 |
$statements[] = preg_replace('#CREATE\s+TABLE\s+"?' . $table_name . '"?#i', 'CREATE TEMPORARY TABLE ' . $table_name . '_temp', $row['sql']);
|
|
indvdum@0
|
1767 |
$statements[] = 'INSERT INTO ' . $table_name . '_temp SELECT * FROM ' . $table_name;
|
|
indvdum@0
|
1768 |
$statements[] = 'DROP TABLE ' . $table_name;
|
|
indvdum@0
|
1769 |
|
|
indvdum@0
|
1770 |
preg_match('#\((.*)\)#s', $row['sql'], $matches);
|
|
indvdum@0
|
1771 |
|
|
indvdum@0
|
1772 |
$new_table_cols = trim($matches[1]);
|
|
indvdum@0
|
1773 |
$old_table_cols = preg_split('/,(?![\s\w]+\))/m', $new_table_cols);
|
|
indvdum@0
|
1774 |
$column_list = array();
|
|
indvdum@0
|
1775 |
|
|
indvdum@0
|
1776 |
foreach ($old_table_cols as $declaration)
|
|
indvdum@0
|
1777 |
{
|
|
indvdum@0
|
1778 |
$entities = preg_split('#\s+#', trim($declaration));
|
|
indvdum@0
|
1779 |
if ($entities[0] == 'PRIMARY')
|
|
indvdum@0
|
1780 |
{
|
|
indvdum@0
|
1781 |
continue;
|
|
indvdum@0
|
1782 |
}
|
|
indvdum@0
|
1783 |
$column_list[] = $entities[0];
|
|
indvdum@0
|
1784 |
}
|
|
indvdum@0
|
1785 |
|
|
indvdum@0
|
1786 |
$columns = implode(',', $column_list);
|
|
indvdum@0
|
1787 |
|
|
indvdum@0
|
1788 |
$new_table_cols = $column_name . ' ' . $column_data['column_type_sql'] . ',' . $new_table_cols;
|
|
indvdum@0
|
1789 |
|
|
indvdum@0
|
1790 |
// create a new table and fill it up. destroy the temp one
|
|
indvdum@0
|
1791 |
$statements[] = 'CREATE TABLE ' . $table_name . ' (' . $new_table_cols . ');';
|
|
indvdum@0
|
1792 |
$statements[] = 'INSERT INTO ' . $table_name . ' (' . $columns . ') SELECT ' . $columns . ' FROM ' . $table_name . '_temp;';
|
|
indvdum@0
|
1793 |
$statements[] = 'DROP TABLE ' . $table_name . '_temp';
|
|
indvdum@0
|
1794 |
|
|
indvdum@0
|
1795 |
$statements[] = 'commit';
|
|
indvdum@0
|
1796 |
}
|
|
indvdum@0
|
1797 |
else
|
|
indvdum@0
|
1798 |
{
|
|
indvdum@0
|
1799 |
$statements[] = 'ALTER TABLE ' . $table_name . ' ADD ' . $column_name . ' [' . $column_data['column_type_sql'] . ']';
|
|
indvdum@0
|
1800 |
}
|
|
indvdum@0
|
1801 |
break;
|
|
indvdum@0
|
1802 |
}
|
|
indvdum@0
|
1803 |
|
|
indvdum@0
|
1804 |
return $this->_sql_run_sql($statements);
|
|
indvdum@0
|
1805 |
}
|
|
indvdum@0
|
1806 |
|
|
indvdum@0
|
1807 |
/**
|
|
indvdum@0
|
1808 |
* Drop column
|
|
indvdum@0
|
1809 |
*/
|
|
indvdum@0
|
1810 |
function sql_column_remove($table_name, $column_name, $inline = false)
|
|
indvdum@0
|
1811 |
{
|
|
indvdum@0
|
1812 |
$statements = array();
|
|
indvdum@0
|
1813 |
|
|
indvdum@0
|
1814 |
switch ($this->sql_layer)
|
|
indvdum@0
|
1815 |
{
|
|
indvdum@0
|
1816 |
case 'firebird':
|
|
indvdum@0
|
1817 |
$statements[] = 'ALTER TABLE ' . $table_name . ' DROP "' . strtoupper($column_name) . '"';
|
|
indvdum@0
|
1818 |
break;
|
|
indvdum@0
|
1819 |
|
|
indvdum@0
|
1820 |
case 'mssql':
|
|
indvdum@0
|
1821 |
case 'mssqlnative':
|
|
indvdum@0
|
1822 |
$statements[] = 'ALTER TABLE [' . $table_name . '] DROP COLUMN [' . $column_name . ']';
|
|
indvdum@0
|
1823 |
break;
|
|
indvdum@0
|
1824 |
|
|
indvdum@0
|
1825 |
case 'mysql_40':
|
|
indvdum@0
|
1826 |
case 'mysql_41':
|
|
indvdum@0
|
1827 |
$statements[] = 'ALTER TABLE `' . $table_name . '` DROP COLUMN `' . $column_name . '`';
|
|
indvdum@0
|
1828 |
break;
|
|
indvdum@0
|
1829 |
|
|
indvdum@0
|
1830 |
case 'oracle':
|
|
indvdum@0
|
1831 |
$statements[] = 'ALTER TABLE ' . $table_name . ' DROP COLUMN ' . $column_name;
|
|
indvdum@0
|
1832 |
break;
|
|
indvdum@0
|
1833 |
|
|
indvdum@0
|
1834 |
case 'postgres':
|
|
indvdum@0
|
1835 |
$statements[] = 'ALTER TABLE ' . $table_name . ' DROP COLUMN "' . $column_name . '"';
|
|
indvdum@0
|
1836 |
break;
|
|
indvdum@0
|
1837 |
|
|
indvdum@0
|
1838 |
case 'sqlite':
|
|
indvdum@0
|
1839 |
|
|
indvdum@0
|
1840 |
if ($inline && $this->return_statements)
|
|
indvdum@0
|
1841 |
{
|
|
indvdum@0
|
1842 |
return $column_name;
|
|
indvdum@0
|
1843 |
}
|
|
indvdum@0
|
1844 |
|
|
indvdum@0
|
1845 |
if (version_compare(sqlite_libversion(), '3.0') == -1)
|
|
indvdum@0
|
1846 |
{
|
|
indvdum@0
|
1847 |
$sql = "SELECT sql
|
|
indvdum@0
|
1848 |
FROM sqlite_master
|
|
indvdum@0
|
1849 |
WHERE type = 'table'
|
|
indvdum@0
|
1850 |
AND name = '{$table_name}'
|
|
indvdum@0
|
1851 |
ORDER BY type DESC, name;";
|
|
indvdum@0
|
1852 |
$result = $this->db->sql_query($sql);
|
|
indvdum@0
|
1853 |
|
|
indvdum@0
|
1854 |
if (!$result)
|
|
indvdum@0
|
1855 |
{
|
|
indvdum@0
|
1856 |
break;
|
|
indvdum@0
|
1857 |
}
|
|
indvdum@0
|
1858 |
|
|
indvdum@0
|
1859 |
$row = $this->db->sql_fetchrow($result);
|
|
indvdum@0
|
1860 |
$this->db->sql_freeresult($result);
|
|
indvdum@0
|
1861 |
|
|
indvdum@0
|
1862 |
$statements[] = 'begin';
|
|
indvdum@0
|
1863 |
|
|
indvdum@0
|
1864 |
// Create a backup table and populate it, destroy the existing one
|
|
indvdum@0
|
1865 |
$statements[] = preg_replace('#CREATE\s+TABLE\s+"?' . $table_name . '"?#i', 'CREATE TEMPORARY TABLE ' . $table_name . '_temp', $row['sql']);
|
|
indvdum@0
|
1866 |
$statements[] = 'INSERT INTO ' . $table_name . '_temp SELECT * FROM ' . $table_name;
|
|
indvdum@0
|
1867 |
$statements[] = 'DROP TABLE ' . $table_name;
|
|
indvdum@0
|
1868 |
|
|
indvdum@0
|
1869 |
preg_match('#\((.*)\)#s', $row['sql'], $matches);
|
|
indvdum@0
|
1870 |
|
|
indvdum@0
|
1871 |
$new_table_cols = trim($matches[1]);
|
|
indvdum@0
|
1872 |
$old_table_cols = preg_split('/,(?![\s\w]+\))/m', $new_table_cols);
|
|
indvdum@0
|
1873 |
$column_list = array();
|
|
indvdum@0
|
1874 |
|
|
indvdum@0
|
1875 |
foreach ($old_table_cols as $declaration)
|
|
indvdum@0
|
1876 |
{
|
|
indvdum@0
|
1877 |
$entities = preg_split('#\s+#', trim($declaration));
|
|
indvdum@0
|
1878 |
if ($entities[0] == 'PRIMARY' || $entities[0] === $column_name)
|
|
indvdum@0
|
1879 |
{
|
|
indvdum@0
|
1880 |
continue;
|
|
indvdum@0
|
1881 |
}
|
|
indvdum@0
|
1882 |
$column_list[] = $entities[0];
|
|
indvdum@0
|
1883 |
}
|
|
indvdum@0
|
1884 |
|
|
indvdum@0
|
1885 |
$columns = implode(',', $column_list);
|
|
indvdum@0
|
1886 |
|
|
indvdum@0
|
1887 |
$new_table_cols = preg_replace('/' . $column_name . '[^,]+(?:,|$)/m', '', $new_table_cols);
|
|
indvdum@0
|
1888 |
|
|
indvdum@0
|
1889 |
// create a new table and fill it up. destroy the temp one
|
|
indvdum@0
|
1890 |
$statements[] = 'CREATE TABLE ' . $table_name . ' (' . $new_table_cols . ');';
|
|
indvdum@0
|
1891 |
$statements[] = 'INSERT INTO ' . $table_name . ' (' . $columns . ') SELECT ' . $columns . ' FROM ' . $table_name . '_temp;';
|
|
indvdum@0
|
1892 |
$statements[] = 'DROP TABLE ' . $table_name . '_temp';
|
|
indvdum@0
|
1893 |
|
|
indvdum@0
|
1894 |
$statements[] = 'commit';
|
|
indvdum@0
|
1895 |
}
|
|
indvdum@0
|
1896 |
else
|
|
indvdum@0
|
1897 |
{
|
|
indvdum@0
|
1898 |
$statements[] = 'ALTER TABLE ' . $table_name . ' DROP COLUMN ' . $column_name;
|
|
indvdum@0
|
1899 |
}
|
|
indvdum@0
|
1900 |
break;
|
|
indvdum@0
|
1901 |
}
|
|
indvdum@0
|
1902 |
|
|
indvdum@0
|
1903 |
return $this->_sql_run_sql($statements);
|
|
indvdum@0
|
1904 |
}
|
|
indvdum@0
|
1905 |
|
|
indvdum@0
|
1906 |
/**
|
|
indvdum@0
|
1907 |
* Drop Index
|
|
indvdum@0
|
1908 |
*/
|
|
indvdum@0
|
1909 |
function sql_index_drop($table_name, $index_name)
|
|
indvdum@0
|
1910 |
{
|
|
indvdum@0
|
1911 |
$statements = array();
|
|
indvdum@0
|
1912 |
|
|
indvdum@0
|
1913 |
switch ($this->sql_layer)
|
|
indvdum@0
|
1914 |
{
|
|
indvdum@0
|
1915 |
case 'mssql':
|
|
indvdum@0
|
1916 |
case 'mssqlnative':
|
|
indvdum@0
|
1917 |
$statements[] = 'DROP INDEX ' . $table_name . '.' . $index_name;
|
|
indvdum@0
|
1918 |
break;
|
|
indvdum@0
|
1919 |
|
|
indvdum@0
|
1920 |
case 'mysql_40':
|
|
indvdum@0
|
1921 |
case 'mysql_41':
|
|
indvdum@0
|
1922 |
$statements[] = 'DROP INDEX ' . $index_name . ' ON ' . $table_name;
|
|
indvdum@0
|
1923 |
break;
|
|
indvdum@0
|
1924 |
|
|
indvdum@0
|
1925 |
case 'firebird':
|
|
indvdum@0
|
1926 |
case 'oracle':
|
|
indvdum@0
|
1927 |
case 'postgres':
|
|
indvdum@0
|
1928 |
case 'sqlite':
|
|
indvdum@0
|
1929 |
$statements[] = 'DROP INDEX ' . $table_name . '_' . $index_name;
|
|
indvdum@0
|
1930 |
break;
|
|
indvdum@0
|
1931 |
}
|
|
indvdum@0
|
1932 |
|
|
indvdum@0
|
1933 |
return $this->_sql_run_sql($statements);
|
|
indvdum@0
|
1934 |
}
|
|
indvdum@0
|
1935 |
|
|
indvdum@0
|
1936 |
/**
|
|
indvdum@0
|
1937 |
* Drop Table
|
|
indvdum@0
|
1938 |
*/
|
|
indvdum@0
|
1939 |
function sql_table_drop($table_name)
|
|
indvdum@0
|
1940 |
{
|
|
indvdum@0
|
1941 |
$statements = array();
|
|
indvdum@0
|
1942 |
|
|
indvdum@0
|
1943 |
if (!$this->sql_table_exists($table_name))
|
|
indvdum@0
|
1944 |
{
|
|
indvdum@0
|
1945 |
return $this->_sql_run_sql($statements);
|
|
indvdum@0
|
1946 |
}
|
|
indvdum@0
|
1947 |
|
|
indvdum@0
|
1948 |
// the most basic operation, get rid of the table
|
|
indvdum@0
|
1949 |
$statements[] = 'DROP TABLE ' . $table_name;
|
|
indvdum@0
|
1950 |
|
|
indvdum@0
|
1951 |
switch ($this->sql_layer)
|
|
indvdum@0
|
1952 |
{
|
|
indvdum@0
|
1953 |
case 'firebird':
|
|
indvdum@0
|
1954 |
$sql = 'SELECT RDB$GENERATOR_NAME as gen
|
|
indvdum@0
|
1955 |
FROM RDB$GENERATORS
|
|
indvdum@0
|
1956 |
WHERE RDB$SYSTEM_FLAG = 0
|
|
indvdum@0
|
1957 |
AND RDB$GENERATOR_NAME = \'' . strtoupper($table_name) . "_GEN'";
|
|
indvdum@0
|
1958 |
$result = $this->db->sql_query($sql);
|
|
indvdum@0
|
1959 |
|
|
indvdum@0
|
1960 |
// does a generator exist?
|
|
indvdum@0
|
1961 |
if ($row = $this->db->sql_fetchrow($result))
|
|
indvdum@0
|
1962 |
{
|
|
indvdum@0
|
1963 |
$statements[] = "DROP GENERATOR {$row['gen']};";
|
|
indvdum@0
|
1964 |
}
|
|
indvdum@0
|
1965 |
$this->db->sql_freeresult($result);
|
|
indvdum@0
|
1966 |
break;
|
|
indvdum@0
|
1967 |
|
|
indvdum@0
|
1968 |
case 'oracle':
|
|
indvdum@0
|
1969 |
$sql = 'SELECT A.REFERENCED_NAME
|
|
indvdum@0
|
1970 |
FROM USER_DEPENDENCIES A, USER_TRIGGERS B
|
|
indvdum@0
|
1971 |
WHERE A.REFERENCED_TYPE = \'SEQUENCE\'
|
|
indvdum@0
|
1972 |
AND A.NAME = B.TRIGGER_NAME
|
|
indvdum@0
|
1973 |
AND B.TABLE_NAME = \'' . strtoupper($table_name) . "'";
|
|
indvdum@0
|
1974 |
$result = $this->db->sql_query($sql);
|
|
indvdum@0
|
1975 |
|
|
indvdum@0
|
1976 |
// any sequences ref'd to this table's triggers?
|
|
indvdum@0
|
1977 |
while ($row = $this->db->sql_fetchrow($result))
|
|
indvdum@0
|
1978 |
{
|
|
indvdum@0
|
1979 |
$statements[] = "DROP SEQUENCE {$row['referenced_name']}";
|
|
indvdum@0
|
1980 |
}
|
|
indvdum@0
|
1981 |
$this->db->sql_freeresult($result);
|
|
indvdum@0
|
1982 |
break;
|
|
indvdum@0
|
1983 |
|
|
indvdum@0
|
1984 |
case 'postgres':
|
|
indvdum@0
|
1985 |
// PGSQL does not "tightly" bind sequences and tables, we must guess...
|
|
indvdum@0
|
1986 |
$sql = "SELECT relname
|
|
indvdum@0
|
1987 |
FROM pg_class
|
|
indvdum@0
|
1988 |
WHERE relkind = 'S'
|
|
indvdum@0
|
1989 |
AND relname = '{$table_name}_seq'";
|
|
indvdum@0
|
1990 |
$result = $this->db->sql_query($sql);
|
|
indvdum@0
|
1991 |
|
|
indvdum@0
|
1992 |
// We don't even care about storing the results. We already know the answer if we get rows back.
|
|
indvdum@0
|
1993 |
if ($this->db->sql_fetchrow($result))
|
|
indvdum@0
|
1994 |
{
|
|
indvdum@0
|
1995 |
$statements[] = "DROP SEQUENCE {$table_name}_seq;\n";
|
|
indvdum@0
|
1996 |
}
|
|
indvdum@0
|
1997 |
$this->db->sql_freeresult($result);
|
|
indvdum@0
|
1998 |
break;
|
|
indvdum@0
|
1999 |
}
|
|
indvdum@0
|
2000 |
|
|
indvdum@0
|
2001 |
return $this->_sql_run_sql($statements);
|
|
indvdum@0
|
2002 |
}
|
|
indvdum@0
|
2003 |
|
|
indvdum@0
|
2004 |
/**
|
|
indvdum@0
|
2005 |
* Add primary key
|
|
indvdum@0
|
2006 |
*/
|
|
indvdum@0
|
2007 |
function sql_create_primary_key($table_name, $column, $inline = false)
|
|
indvdum@0
|
2008 |
{
|
|
indvdum@0
|
2009 |
$statements = array();
|
|
indvdum@0
|
2010 |
|
|
indvdum@0
|
2011 |
switch ($this->sql_layer)
|
|
indvdum@0
|
2012 |
{
|
|
indvdum@0
|
2013 |
case 'firebird':
|
|
indvdum@0
|
2014 |
case 'postgres':
|
|
indvdum@0
|
2015 |
case 'mysql_40':
|
|
indvdum@0
|
2016 |
case 'mysql_41':
|
|
indvdum@0
|
2017 |
$statements[] = 'ALTER TABLE ' . $table_name . ' ADD PRIMARY KEY (' . implode(', ', $column) . ')';
|
|
indvdum@0
|
2018 |
break;
|
|
indvdum@0
|
2019 |
|
|
indvdum@0
|
2020 |
case 'mssql':
|
|
indvdum@0
|
2021 |
case 'mssqlnative':
|
|
indvdum@0
|
2022 |
$sql = "ALTER TABLE [{$table_name}] WITH NOCHECK ADD ";
|
|
indvdum@0
|
2023 |
$sql .= "CONSTRAINT [PK_{$table_name}] PRIMARY KEY CLUSTERED (";
|
|
indvdum@0
|
2024 |
$sql .= '[' . implode("],\n\t\t[", $column) . ']';
|
|
indvdum@0
|
2025 |
$sql .= ') ON [PRIMARY]';
|
|
indvdum@0
|
2026 |
|
|
indvdum@0
|
2027 |
$statements[] = $sql;
|
|
indvdum@0
|
2028 |
break;
|
|
indvdum@0
|
2029 |
|
|
indvdum@0
|
2030 |
case 'oracle':
|
|
indvdum@0
|
2031 |
$statements[] = 'ALTER TABLE ' . $table_name . 'add CONSTRAINT pk_' . $table_name . ' PRIMARY KEY (' . implode(', ', $column) . ')';
|
|
indvdum@0
|
2032 |
break;
|
|
indvdum@0
|
2033 |
|
|
indvdum@0
|
2034 |
case 'sqlite':
|
|
indvdum@0
|
2035 |
|
|
indvdum@0
|
2036 |
if ($inline && $this->return_statements)
|
|
indvdum@0
|
2037 |
{
|
|
indvdum@0
|
2038 |
return $column;
|
|
indvdum@0
|
2039 |
}
|
|
indvdum@0
|
2040 |
|
|
indvdum@0
|
2041 |
$sql = "SELECT sql
|
|
indvdum@0
|
2042 |
FROM sqlite_master
|
|
indvdum@0
|
2043 |
WHERE type = 'table'
|
|
indvdum@0
|
2044 |
AND name = '{$table_name}'
|
|
indvdum@0
|
2045 |
ORDER BY type DESC, name;";
|
|
indvdum@0
|
2046 |
$result = $this->db->sql_query($sql);
|
|
indvdum@0
|
2047 |
|
|
indvdum@0
|
2048 |
if (!$result)
|
|
indvdum@0
|
2049 |
{
|
|
indvdum@0
|
2050 |
break;
|
|
indvdum@0
|
2051 |
}
|
|
indvdum@0
|
2052 |
|
|
indvdum@0
|
2053 |
$row = $this->db->sql_fetchrow($result);
|
|
indvdum@0
|
2054 |
$this->db->sql_freeresult($result);
|
|
indvdum@0
|
2055 |
|
|
indvdum@0
|
2056 |
$statements[] = 'begin';
|
|
indvdum@0
|
2057 |
|
|
indvdum@0
|
2058 |
// Create a backup table and populate it, destroy the existing one
|
|
indvdum@0
|
2059 |
$statements[] = preg_replace('#CREATE\s+TABLE\s+"?' . $table_name . '"?#i', 'CREATE TEMPORARY TABLE ' . $table_name . '_temp', $row['sql']);
|
|
indvdum@0
|
2060 |
$statements[] = 'INSERT INTO ' . $table_name . '_temp SELECT * FROM ' . $table_name;
|
|
indvdum@0
|
2061 |
$statements[] = 'DROP TABLE ' . $table_name;
|
|
indvdum@0
|
2062 |
|
|
indvdum@0
|
2063 |
preg_match('#\((.*)\)#s', $row['sql'], $matches);
|
|
indvdum@0
|
2064 |
|
|
indvdum@0
|
2065 |
$new_table_cols = trim($matches[1]);
|
|
indvdum@0
|
2066 |
$old_table_cols = preg_split('/,(?![\s\w]+\))/m', $new_table_cols);
|
|
indvdum@0
|
2067 |
$column_list = array();
|
|
indvdum@0
|
2068 |
|
|
indvdum@0
|
2069 |
foreach ($old_table_cols as $declaration)
|
|
indvdum@0
|
2070 |
{
|
|
indvdum@0
|
2071 |
$entities = preg_split('#\s+#', trim($declaration));
|
|
indvdum@0
|
2072 |
if ($entities[0] == 'PRIMARY')
|
|
indvdum@0
|
2073 |
{
|
|
indvdum@0
|
2074 |
continue;
|
|
indvdum@0
|
2075 |
}
|
|
indvdum@0
|
2076 |
$column_list[] = $entities[0];
|
|
indvdum@0
|
2077 |
}
|
|
indvdum@0
|
2078 |
|
|
indvdum@0
|
2079 |
$columns = implode(',', $column_list);
|
|
indvdum@0
|
2080 |
|
|
indvdum@0
|
2081 |
// create a new table and fill it up. destroy the temp one
|
|
indvdum@0
|
2082 |
$statements[] = 'CREATE TABLE ' . $table_name . ' (' . $new_table_cols . ', PRIMARY KEY (' . implode(', ', $column) . '));';
|
|
indvdum@0
|
2083 |
$statements[] = 'INSERT INTO ' . $table_name . ' (' . $columns . ') SELECT ' . $columns . ' FROM ' . $table_name . '_temp;';
|
|
indvdum@0
|
2084 |
$statements[] = 'DROP TABLE ' . $table_name . '_temp';
|
|
indvdum@0
|
2085 |
|
|
indvdum@0
|
2086 |
$statements[] = 'commit';
|
|
indvdum@0
|
2087 |
break;
|
|
indvdum@0
|
2088 |
}
|
|
indvdum@0
|
2089 |
|
|
indvdum@0
|
2090 |
return $this->_sql_run_sql($statements);
|
|
indvdum@0
|
2091 |
}
|
|
indvdum@0
|
2092 |
|
|
indvdum@0
|
2093 |
/**
|
|
indvdum@0
|
2094 |
* Add unique index
|
|
indvdum@0
|
2095 |
*/
|
|
indvdum@0
|
2096 |
function sql_create_unique_index($table_name, $index_name, $column)
|
|
indvdum@0
|
2097 |
{
|
|
indvdum@0
|
2098 |
$statements = array();
|
|
indvdum@0
|
2099 |
|
|
indvdum@0
|
2100 |
$table_prefix = substr(CONFIG_TABLE, 0, -6); // strlen(config)
|
|
indvdum@0
|
2101 |
if (strlen($table_name . $index_name) - strlen($table_prefix) > 24)
|
|
indvdum@0
|
2102 |
{
|
|
indvdum@0
|
2103 |
$max_length = strlen($table_prefix) + 24;
|
|
indvdum@0
|
2104 |
trigger_error("Index name '{$table_name}_$index_name' on table '$table_name' is too long. The maximum is $max_length characters.", E_USER_ERROR);
|
|
indvdum@0
|
2105 |
}
|
|
indvdum@0
|
2106 |
|
|
indvdum@0
|
2107 |
switch ($this->sql_layer)
|
|
indvdum@0
|
2108 |
{
|
|
indvdum@0
|
2109 |
case 'firebird':
|
|
indvdum@0
|
2110 |
case 'postgres':
|
|
indvdum@0
|
2111 |
case 'oracle':
|
|
indvdum@0
|
2112 |
case 'sqlite':
|
|
indvdum@0
|
2113 |
$statements[] = 'CREATE UNIQUE INDEX ' . $table_name . '_' . $index_name . ' ON ' . $table_name . '(' . implode(', ', $column) . ')';
|
|
indvdum@0
|
2114 |
break;
|
|
indvdum@0
|
2115 |
|
|
indvdum@0
|
2116 |
case 'mysql_40':
|
|
indvdum@0
|
2117 |
case 'mysql_41':
|
|
indvdum@0
|
2118 |
$statements[] = 'ALTER TABLE ' . $table_name . ' ADD UNIQUE INDEX (' . implode(', ', $column) . ')';
|
|
indvdum@0
|
2119 |
break;
|
|
indvdum@0
|
2120 |
|
|
indvdum@0
|
2121 |
case 'mssql':
|
|
indvdum@0
|
2122 |
case 'mssqlnative':
|
|
indvdum@0
|
2123 |
$statements[] = 'CREATE UNIQUE INDEX ' . $index_name . ' ON ' . $table_name . '(' . implode(', ', $column) . ') ON [PRIMARY]';
|
|
indvdum@0
|
2124 |
break;
|
|
indvdum@0
|
2125 |
}
|
|
indvdum@0
|
2126 |
|
|
indvdum@0
|
2127 |
return $this->_sql_run_sql($statements);
|
|
indvdum@0
|
2128 |
}
|
|
indvdum@0
|
2129 |
|
|
indvdum@0
|
2130 |
/**
|
|
indvdum@0
|
2131 |
* Add index
|
|
indvdum@0
|
2132 |
*/
|
|
indvdum@0
|
2133 |
function sql_create_index($table_name, $index_name, $column)
|
|
indvdum@0
|
2134 |
{
|
|
indvdum@0
|
2135 |
$statements = array();
|
|
indvdum@0
|
2136 |
|
|
indvdum@0
|
2137 |
$table_prefix = substr(CONFIG_TABLE, 0, -6); // strlen(config)
|
|
indvdum@0
|
2138 |
if (strlen($table_name . $index_name) - strlen($table_prefix) > 24)
|
|
indvdum@0
|
2139 |
{
|
|
indvdum@0
|
2140 |
$max_length = strlen($table_prefix) + 24;
|
|
indvdum@0
|
2141 |
trigger_error("Index name '{$table_name}_$index_name' on table '$table_name' is too long. The maximum is $max_length characters.", E_USER_ERROR);
|
|
indvdum@0
|
2142 |
}
|
|
indvdum@0
|
2143 |
|
|
indvdum@0
|
2144 |
// remove index length unless MySQL4
|
|
indvdum@0
|
2145 |
if ('mysql_40' != $this->sql_layer)
|
|
indvdum@0
|
2146 |
{
|
|
indvdum@0
|
2147 |
$column = preg_replace('#:.*$#', '', $column);
|
|
indvdum@0
|
2148 |
}
|
|
indvdum@0
|
2149 |
|
|
indvdum@0
|
2150 |
switch ($this->sql_layer)
|
|
indvdum@0
|
2151 |
{
|
|
indvdum@0
|
2152 |
case 'firebird':
|
|
indvdum@0
|
2153 |
case 'postgres':
|
|
indvdum@0
|
2154 |
case 'oracle':
|
|
indvdum@0
|
2155 |
case 'sqlite':
|
|
indvdum@0
|
2156 |
$statements[] = 'CREATE INDEX ' . $table_name . '_' . $index_name . ' ON ' . $table_name . '(' . implode(', ', $column) . ')';
|
|
indvdum@0
|
2157 |
break;
|
|
indvdum@0
|
2158 |
|
|
indvdum@0
|
2159 |
case 'mysql_40':
|
|
indvdum@0
|
2160 |
// add index size to definition as required by MySQL4
|
|
indvdum@0
|
2161 |
foreach ($column as $i => $col)
|
|
indvdum@0
|
2162 |
{
|
|
indvdum@0
|
2163 |
if (false !== strpos($col, ':'))
|
|
indvdum@0
|
2164 |
{
|
|
indvdum@0
|
2165 |
list($col, $index_size) = explode(':', $col);
|
|
indvdum@0
|
2166 |
$column[$i] = "$col($index_size)";
|
|
indvdum@0
|
2167 |
}
|
|
indvdum@0
|
2168 |
}
|
|
indvdum@0
|
2169 |
// no break
|
|
indvdum@0
|
2170 |
case 'mysql_41':
|
|
indvdum@0
|
2171 |
$statements[] = 'ALTER TABLE ' . $table_name . ' ADD INDEX ' . $index_name . '(' . implode(', ', $column) . ')';
|
|
indvdum@0
|
2172 |
break;
|
|
indvdum@0
|
2173 |
|
|
indvdum@0
|
2174 |
case 'mssql':
|
|
indvdum@0
|
2175 |
case 'mssqlnative':
|
|
indvdum@0
|
2176 |
$statements[] = 'CREATE INDEX ' . $index_name . ' ON ' . $table_name . '(' . implode(', ', $column) . ') ON [PRIMARY]';
|
|
indvdum@0
|
2177 |
break;
|
|
indvdum@0
|
2178 |
}
|
|
indvdum@0
|
2179 |
|
|
indvdum@0
|
2180 |
return $this->_sql_run_sql($statements);
|
|
indvdum@0
|
2181 |
}
|
|
indvdum@0
|
2182 |
|
|
indvdum@0
|
2183 |
/**
|
|
indvdum@0
|
2184 |
* List all of the indices that belong to a table,
|
|
indvdum@0
|
2185 |
* does not count:
|
|
indvdum@0
|
2186 |
* * UNIQUE indices
|
|
indvdum@0
|
2187 |
* * PRIMARY keys
|
|
indvdum@0
|
2188 |
*/
|
|
indvdum@0
|
2189 |
function sql_list_index($table_name)
|
|
indvdum@0
|
2190 |
{
|
|
indvdum@0
|
2191 |
$index_array = array();
|
|
indvdum@0
|
2192 |
|
|
indvdum@0
|
2193 |
if ($this->sql_layer == 'mssql' || $this->sql_layer == 'mssqlnative')
|
|
indvdum@0
|
2194 |
{
|
|
indvdum@0
|
2195 |
$sql = "EXEC sp_statistics '$table_name'";
|
|
indvdum@0
|
2196 |
$result = $this->db->sql_query($sql);
|
|
indvdum@0
|
2197 |
while ($row = $this->db->sql_fetchrow($result))
|
|
indvdum@0
|
2198 |
{
|
|
indvdum@0
|
2199 |
if ($row['TYPE'] == 3)
|
|
indvdum@0
|
2200 |
{
|
|
indvdum@0
|
2201 |
$index_array[] = $row['INDEX_NAME'];
|
|
indvdum@0
|
2202 |
}
|
|
indvdum@0
|
2203 |
}
|
|
indvdum@0
|
2204 |
$this->db->sql_freeresult($result);
|
|
indvdum@0
|
2205 |
}
|
|
indvdum@0
|
2206 |
else
|
|
indvdum@0
|
2207 |
{
|
|
indvdum@0
|
2208 |
switch ($this->sql_layer)
|
|
indvdum@0
|
2209 |
{
|
|
indvdum@0
|
2210 |
case 'firebird':
|
|
indvdum@0
|
2211 |
$sql = "SELECT LOWER(RDB\$INDEX_NAME) as index_name
|
|
indvdum@0
|
2212 |
FROM RDB\$INDICES
|
|
indvdum@0
|
2213 |
WHERE RDB\$RELATION_NAME = '" . strtoupper($table_name) . "'
|
|
indvdum@0
|
2214 |
AND RDB\$UNIQUE_FLAG IS NULL
|
|
indvdum@0
|
2215 |
AND RDB\$FOREIGN_KEY IS NULL";
|
|
indvdum@0
|
2216 |
$col = 'index_name';
|
|
indvdum@0
|
2217 |
break;
|
|
indvdum@0
|
2218 |
|
|
indvdum@0
|
2219 |
case 'postgres':
|
|
indvdum@0
|
2220 |
$sql = "SELECT ic.relname as index_name
|
|
indvdum@0
|
2221 |
FROM pg_class bc, pg_class ic, pg_index i
|
|
indvdum@0
|
2222 |
WHERE (bc.oid = i.indrelid)
|
|
indvdum@0
|
2223 |
AND (ic.oid = i.indexrelid)
|
|
indvdum@0
|
2224 |
AND (bc.relname = '" . $table_name . "')
|
|
indvdum@0
|
2225 |
AND (i.indisunique != 't')
|
|
indvdum@0
|
2226 |
AND (i.indisprimary != 't')";
|
|
indvdum@0
|
2227 |
$col = 'index_name';
|
|
indvdum@0
|
2228 |
break;
|
|
indvdum@0
|
2229 |
|
|
indvdum@0
|
2230 |
case 'mysql_40':
|
|
indvdum@0
|
2231 |
case 'mysql_41':
|
|
indvdum@0
|
2232 |
$sql = 'SHOW KEYS
|
|
indvdum@0
|
2233 |
FROM ' . $table_name;
|
|
indvdum@0
|
2234 |
$col = 'Key_name';
|
|
indvdum@0
|
2235 |
break;
|
|
indvdum@0
|
2236 |
|
|
indvdum@0
|
2237 |
case 'oracle':
|
|
indvdum@0
|
2238 |
$sql = "SELECT index_name
|
|
indvdum@0
|
2239 |
FROM user_indexes
|
|
indvdum@0
|
2240 |
WHERE table_name = '" . strtoupper($table_name) . "'
|
|
indvdum@0
|
2241 |
AND generated = 'N'
|
|
indvdum@0
|
2242 |
AND uniqueness = 'NONUNIQUE'";
|
|
indvdum@0
|
2243 |
$col = 'index_name';
|
|
indvdum@0
|
2244 |
break;
|
|
indvdum@0
|
2245 |
|
|
indvdum@0
|
2246 |
case 'sqlite':
|
|
indvdum@0
|
2247 |
$sql = "PRAGMA index_info('" . $table_name . "');";
|
|
indvdum@0
|
2248 |
$col = 'name';
|
|
indvdum@0
|
2249 |
break;
|
|
indvdum@0
|
2250 |
}
|
|
indvdum@0
|
2251 |
|
|
indvdum@0
|
2252 |
$result = $this->db->sql_query($sql);
|
|
indvdum@0
|
2253 |
while ($row = $this->db->sql_fetchrow($result))
|
|
indvdum@0
|
2254 |
{
|
|
indvdum@0
|
2255 |
if (($this->sql_layer == 'mysql_40' || $this->sql_layer == 'mysql_41') && !$row['Non_unique'])
|
|
indvdum@0
|
2256 |
{
|
|
indvdum@0
|
2257 |
continue;
|
|
indvdum@0
|
2258 |
}
|
|
indvdum@0
|
2259 |
|
|
indvdum@0
|
2260 |
switch ($this->sql_layer)
|
|
indvdum@0
|
2261 |
{
|
|
indvdum@0
|
2262 |
case 'firebird':
|
|
indvdum@0
|
2263 |
case 'oracle':
|
|
indvdum@0
|
2264 |
case 'postgres':
|
|
indvdum@0
|
2265 |
case 'sqlite':
|
|
indvdum@0
|
2266 |
$row[$col] = substr($row[$col], strlen($table_name) + 1);
|
|
indvdum@0
|
2267 |
break;
|
|
indvdum@0
|
2268 |
}
|
|
indvdum@0
|
2269 |
|
|
indvdum@0
|
2270 |
$index_array[] = $row[$col];
|
|
indvdum@0
|
2271 |
}
|
|
indvdum@0
|
2272 |
$this->db->sql_freeresult($result);
|
|
indvdum@0
|
2273 |
}
|
|
indvdum@0
|
2274 |
|
|
indvdum@0
|
2275 |
return array_map('strtolower', $index_array);
|
|
indvdum@0
|
2276 |
}
|
|
indvdum@0
|
2277 |
|
|
indvdum@0
|
2278 |
/**
|
|
indvdum@0
|
2279 |
* Change column type (not name!)
|
|
indvdum@0
|
2280 |
*/
|
|
indvdum@0
|
2281 |
function sql_column_change($table_name, $column_name, $column_data, $inline = false)
|
|
indvdum@0
|
2282 |
{
|
|
indvdum@0
|
2283 |
$column_data = $this->sql_prepare_column_data($table_name, $column_name, $column_data);
|
|
indvdum@0
|
2284 |
$statements = array();
|
|
indvdum@0
|
2285 |
|
|
indvdum@0
|
2286 |
switch ($this->sql_layer)
|
|
indvdum@0
|
2287 |
{
|
|
indvdum@0
|
2288 |
case 'firebird':
|
|
indvdum@0
|
2289 |
// Change type...
|
|
indvdum@0
|
2290 |
if (!empty($column_data['column_type_sql_default']))
|
|
indvdum@0
|
2291 |
{
|
|
indvdum@0
|
2292 |
$statements[] = 'ALTER TABLE ' . $table_name . ' ALTER COLUMN "' . strtoupper($column_name) . '" TYPE ' . ' ' . $column_data['column_type_sql_type'];
|
|
indvdum@0
|
2293 |
$statements[] = 'ALTER TABLE ' . $table_name . ' ALTER COLUMN "' . strtoupper($column_name) . '" SET DEFAULT ' . ' ' . $column_data['column_type_sql_default'];
|
|
indvdum@0
|
2294 |
}
|
|
indvdum@0
|
2295 |
else
|
|
indvdum@0
|
2296 |
{
|
|
indvdum@0
|
2297 |
// TODO: try to change pkey without removing trigger, generator or constraints. ATM this query may fail.
|
|
indvdum@0
|
2298 |
$statements[] = 'ALTER TABLE ' . $table_name . ' ALTER COLUMN "' . strtoupper($column_name) . '" TYPE ' . ' ' . $column_data['column_type_sql_type'];
|
|
indvdum@0
|
2299 |
}
|
|
indvdum@0
|
2300 |
break;
|
|
indvdum@0
|
2301 |
|
|
indvdum@0
|
2302 |
case 'mssql':
|
|
indvdum@0
|
2303 |
case 'mssqlnative':
|
|
indvdum@0
|
2304 |
$statements[] = 'ALTER TABLE [' . $table_name . '] ALTER COLUMN [' . $column_name . '] ' . $column_data['column_type_sql'];
|
|
indvdum@0
|
2305 |
|
|
indvdum@0
|
2306 |
if (!empty($column_data['default']))
|
|
indvdum@0
|
2307 |
{
|
|
indvdum@0
|
2308 |
// Using TRANSACT-SQL for this statement because we do not want to have colliding data if statements are executed at a later stage
|
|
indvdum@0
|
2309 |
$statements[] = "DECLARE @drop_default_name VARCHAR(100), @cmd VARCHAR(1000)
|
|
indvdum@0
|
2310 |
SET @drop_default_name =
|
|
indvdum@0
|
2311 |
(SELECT so.name FROM sysobjects so
|
|
indvdum@0
|
2312 |
JOIN sysconstraints sc ON so.id = sc.constid
|
|
indvdum@0
|
2313 |
WHERE object_name(so.parent_obj) = '{$table_name}'
|
|
indvdum@0
|
2314 |
AND so.xtype = 'D'
|
|
indvdum@0
|
2315 |
AND sc.colid = (SELECT colid FROM syscolumns
|
|
indvdum@0
|
2316 |
WHERE id = object_id('{$table_name}')
|
|
indvdum@0
|
2317 |
AND name = '{$column_name}'))
|
|
indvdum@0
|
2318 |
IF @drop_default_name <> ''
|
|
indvdum@0
|
2319 |
BEGIN
|
|
indvdum@0
|
2320 |
SET @cmd = 'ALTER TABLE [{$table_name}] DROP CONSTRAINT [' + @drop_default_name + ']'
|
|
indvdum@0
|
2321 |
EXEC(@cmd)
|
|
indvdum@0
|
2322 |
END
|
|
indvdum@0
|
2323 |
SET @cmd = 'ALTER TABLE [{$table_name}] ADD CONSTRAINT [DF_{$table_name}_{$column_name}_1] {$column_data['default']} FOR [{$column_name}]'
|
|
indvdum@0
|
2324 |
EXEC(@cmd)";
|
|
indvdum@0
|
2325 |
}
|
|
indvdum@0
|
2326 |
break;
|
|
indvdum@0
|
2327 |
|
|
indvdum@0
|
2328 |
case 'mysql_40':
|
|
indvdum@0
|
2329 |
case 'mysql_41':
|
|
indvdum@0
|
2330 |
$statements[] = 'ALTER TABLE `' . $table_name . '` CHANGE `' . $column_name . '` `' . $column_name . '` ' . $column_data['column_type_sql'];
|
|
indvdum@0
|
2331 |
break;
|
|
indvdum@0
|
2332 |
|
|
indvdum@0
|
2333 |
case 'oracle':
|
|
indvdum@0
|
2334 |
$statements[] = 'ALTER TABLE ' . $table_name . ' MODIFY ' . $column_name . ' ' . $column_data['column_type_sql'];
|
|
indvdum@0
|
2335 |
break;
|
|
indvdum@0
|
2336 |
|
|
indvdum@0
|
2337 |
case 'postgres':
|
|
indvdum@0
|
2338 |
$sql = 'ALTER TABLE ' . $table_name . ' ';
|
|
indvdum@0
|
2339 |
|
|
indvdum@0
|
2340 |
$sql_array = array();
|
|
indvdum@0
|
2341 |
$sql_array[] = 'ALTER COLUMN ' . $column_name . ' TYPE ' . $column_data['column_type'];
|
|
indvdum@0
|
2342 |
|
|
indvdum@0
|
2343 |
if (isset($column_data['null']))
|
|
indvdum@0
|
2344 |
{
|
|
indvdum@0
|
2345 |
if ($column_data['null'] == 'NOT NULL')
|
|
indvdum@0
|
2346 |
{
|
|
indvdum@0
|
2347 |
$sql_array[] = 'ALTER COLUMN ' . $column_name . ' SET NOT NULL';
|
|
indvdum@0
|
2348 |
}
|
|
indvdum@0
|
2349 |
else if ($column_data['null'] == 'NULL')
|
|
indvdum@0
|
2350 |
{
|
|
indvdum@0
|
2351 |
$sql_array[] = 'ALTER COLUMN ' . $column_name . ' DROP NOT NULL';
|
|
indvdum@0
|
2352 |
}
|
|
indvdum@0
|
2353 |
}
|
|
indvdum@0
|
2354 |
|
|
indvdum@0
|
2355 |
if (isset($column_data['default']))
|
|
indvdum@0
|
2356 |
{
|
|
indvdum@0
|
2357 |
$sql_array[] = 'ALTER COLUMN ' . $column_name . ' SET DEFAULT ' . $column_data['default'];
|
|
indvdum@0
|
2358 |
}
|
|
indvdum@0
|
2359 |
|
|
indvdum@0
|
2360 |
// we don't want to double up on constraints if we change different number data types
|
|
indvdum@0
|
2361 |
if (isset($column_data['constraint']))
|
|
indvdum@0
|
2362 |
{
|
|
indvdum@0
|
2363 |
$constraint_sql = "SELECT consrc as constraint_data
|
|
indvdum@0
|
2364 |
FROM pg_constraint, pg_class bc
|
|
indvdum@0
|
2365 |
WHERE conrelid = bc.oid
|
|
indvdum@0
|
2366 |
AND bc.relname = '{$table_name}'
|
|
indvdum@0
|
2367 |
AND NOT EXISTS (
|
|
indvdum@0
|
2368 |
SELECT *
|
|
indvdum@0
|
2369 |
FROM pg_constraint as c, pg_inherits as i
|
|
indvdum@0
|
2370 |
WHERE i.inhrelid = pg_constraint.conrelid
|
|
indvdum@0
|
2371 |
AND c.conname = pg_constraint.conname
|
|
indvdum@0
|
2372 |
AND c.consrc = pg_constraint.consrc
|
|
indvdum@0
|
2373 |
AND c.conrelid = i.inhparent
|
|
indvdum@0
|
2374 |
)";
|
|
indvdum@0
|
2375 |
|
|
indvdum@0
|
2376 |
$constraint_exists = false;
|
|
indvdum@0
|
2377 |
|
|
indvdum@0
|
2378 |
$result = $this->db->sql_query($constraint_sql);
|
|
indvdum@0
|
2379 |
while ($row = $this->db->sql_fetchrow($result))
|
|
indvdum@0
|
2380 |
{
|
|
indvdum@0
|
2381 |
if (trim($row['constraint_data']) == trim($column_data['constraint']))
|
|
indvdum@0
|
2382 |
{
|
|
indvdum@0
|
2383 |
$constraint_exists = true;
|
|
indvdum@0
|
2384 |
break;
|
|
indvdum@0
|
2385 |
}
|
|
indvdum@0
|
2386 |
}
|
|
indvdum@0
|
2387 |
$this->db->sql_freeresult($result);
|
|
indvdum@0
|
2388 |
|
|
indvdum@0
|
2389 |
if (!$constraint_exists)
|
|
indvdum@0
|
2390 |
{
|
|
indvdum@0
|
2391 |
$sql_array[] = 'ADD ' . $column_data['constraint'];
|
|
indvdum@0
|
2392 |
}
|
|
indvdum@0
|
2393 |
}
|
|
indvdum@0
|
2394 |
|
|
indvdum@0
|
2395 |
$sql .= implode(', ', $sql_array);
|
|
indvdum@0
|
2396 |
|
|
indvdum@0
|
2397 |
$statements[] = $sql;
|
|
indvdum@0
|
2398 |
break;
|
|
indvdum@0
|
2399 |
|
|
indvdum@0
|
2400 |
case 'sqlite':
|
|
indvdum@0
|
2401 |
|
|
indvdum@0
|
2402 |
if ($inline && $this->return_statements)
|
|
indvdum@0
|
2403 |
{
|
|
indvdum@0
|
2404 |
return $column_name . ' ' . $column_data['column_type_sql'];
|
|
indvdum@0
|
2405 |
}
|
|
indvdum@0
|
2406 |
|
|
indvdum@0
|
2407 |
$sql = "SELECT sql
|
|
indvdum@0
|
2408 |
FROM sqlite_master
|
|
indvdum@0
|
2409 |
WHERE type = 'table'
|
|
indvdum@0
|
2410 |
AND name = '{$table_name}'
|
|
indvdum@0
|
2411 |
ORDER BY type DESC, name;";
|
|
indvdum@0
|
2412 |
$result = $this->db->sql_query($sql);
|
|
indvdum@0
|
2413 |
|
|
indvdum@0
|
2414 |
if (!$result)
|
|
indvdum@0
|
2415 |
{
|
|
indvdum@0
|
2416 |
break;
|
|
indvdum@0
|
2417 |
}
|
|
indvdum@0
|
2418 |
|
|
indvdum@0
|
2419 |
$row = $this->db->sql_fetchrow($result);
|
|
indvdum@0
|
2420 |
$this->db->sql_freeresult($result);
|
|
indvdum@0
|
2421 |
|
|
indvdum@0
|
2422 |
$statements[] = 'begin';
|
|
indvdum@0
|
2423 |
|
|
indvdum@0
|
2424 |
// Create a temp table and populate it, destroy the existing one
|
|
indvdum@0
|
2425 |
$statements[] = preg_replace('#CREATE\s+TABLE\s+"?' . $table_name . '"?#i', 'CREATE TEMPORARY TABLE ' . $table_name . '_temp', $row['sql']);
|
|
indvdum@0
|
2426 |
$statements[] = 'INSERT INTO ' . $table_name . '_temp SELECT * FROM ' . $table_name;
|
|
indvdum@0
|
2427 |
$statements[] = 'DROP TABLE ' . $table_name;
|
|
indvdum@0
|
2428 |
|
|
indvdum@0
|
2429 |
preg_match('#\((.*)\)#s', $row['sql'], $matches);
|
|
indvdum@0
|
2430 |
|
|
indvdum@0
|
2431 |
$new_table_cols = trim($matches[1]);
|
|
indvdum@0
|
2432 |
$old_table_cols = preg_split('/,(?![\s\w]+\))/m', $new_table_cols);
|
|
indvdum@0
|
2433 |
$column_list = array();
|
|
indvdum@0
|
2434 |
|
|
indvdum@0
|
2435 |
foreach ($old_table_cols as $key => $declaration)
|
|
indvdum@0
|
2436 |
{
|
|
indvdum@0
|
2437 |
$entities = preg_split('#\s+#', trim($declaration));
|
|
indvdum@0
|
2438 |
$column_list[] = $entities[0];
|
|
indvdum@0
|
2439 |
if ($entities[0] == $column_name)
|
|
indvdum@0
|
2440 |
{
|
|
indvdum@0
|
2441 |
$old_table_cols[$key] = $column_name . ' ' . $column_data['column_type_sql'];
|
|
indvdum@0
|
2442 |
}
|
|
indvdum@0
|
2443 |
}
|
|
indvdum@0
|
2444 |
|
|
indvdum@0
|
2445 |
$columns = implode(',', $column_list);
|
|
indvdum@0
|
2446 |
|
|
indvdum@0
|
2447 |
// create a new table and fill it up. destroy the temp one
|
|
indvdum@0
|
2448 |
$statements[] = 'CREATE TABLE ' . $table_name . ' (' . implode(',', $old_table_cols) . ');';
|
|
indvdum@0
|
2449 |
$statements[] = 'INSERT INTO ' . $table_name . ' (' . $columns . ') SELECT ' . $columns . ' FROM ' . $table_name . '_temp;';
|
|
indvdum@0
|
2450 |
$statements[] = 'DROP TABLE ' . $table_name . '_temp';
|
|
indvdum@0
|
2451 |
|
|
indvdum@0
|
2452 |
$statements[] = 'commit';
|
|
indvdum@0
|
2453 |
|
|
indvdum@0
|
2454 |
break;
|
|
indvdum@0
|
2455 |
}
|
|
indvdum@0
|
2456 |
|
|
indvdum@0
|
2457 |
return $this->_sql_run_sql($statements);
|
|
indvdum@0
|
2458 |
}
|
|
indvdum@0
|
2459 |
}
|
|
indvdum@0
|
2460 |
|
|
indvdum@0
|
2461 |
?> |