commit | author | age
|
2273d4
|
1 |
<?php |
A |
2 |
// +----------------------------------------------------------------------+ |
|
3 |
// | PHP versions 4 and 5 | |
|
4 |
// +----------------------------------------------------------------------+ |
|
5 |
// | Copyright (c) 1998-2007 Manuel Lemos, Tomas V.V.Cox, | |
|
6 |
// | Stig. S. Bakken, Lukas Smith, Lorenzo Alberton | |
|
7 |
// | All rights reserved. | |
|
8 |
// +----------------------------------------------------------------------+ |
|
9 |
// | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB | |
|
10 |
// | API as well as database abstraction for PHP applications. | |
|
11 |
// | This LICENSE is in the BSD license style. | |
|
12 |
// | | |
|
13 |
// | Redistribution and use in source and binary forms, with or without | |
|
14 |
// | modification, are permitted provided that the following conditions | |
|
15 |
// | are met: | |
|
16 |
// | | |
|
17 |
// | Redistributions of source code must retain the above copyright | |
|
18 |
// | notice, this list of conditions and the following disclaimer. | |
|
19 |
// | | |
|
20 |
// | Redistributions in binary form must reproduce the above copyright | |
|
21 |
// | notice, this list of conditions and the following disclaimer in the | |
|
22 |
// | documentation and/or other materials provided with the distribution. | |
|
23 |
// | | |
|
24 |
// | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken, | |
|
25 |
// | Lukas Smith nor the names of his contributors may be used to endorse | |
|
26 |
// | or promote products derived from this software without specific prior| |
|
27 |
// | written permission. | |
|
28 |
// | | |
|
29 |
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
|
30 |
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
|
31 |
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
|
32 |
// | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
|
33 |
// | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
|
34 |
// | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
|
35 |
// | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS| |
|
36 |
// | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | |
|
37 |
// | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
|
38 |
// | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY| |
|
39 |
// | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
|
40 |
// | POSSIBILITY OF SUCH DAMAGE. | |
|
41 |
// +----------------------------------------------------------------------+ |
|
42 |
// | Authors: Lukas Smith <smith@pooteeweet.org> | |
|
43 |
// | Lorenzo Alberton <l.alberton@quipo.it> | |
|
44 |
// +----------------------------------------------------------------------+ |
|
45 |
// |
7244b4
|
46 |
// $Id: sqlite.php 295587 2010-02-28 17:16:38Z quipo $ |
2273d4
|
47 |
// |
A |
48 |
|
|
49 |
require_once 'MDB2/Driver/Reverse/Common.php'; |
|
50 |
|
|
51 |
/** |
|
52 |
* MDB2 SQlite driver for the schema reverse engineering module |
|
53 |
* |
|
54 |
* @package MDB2 |
|
55 |
* @category Database |
|
56 |
* @author Lukas Smith <smith@pooteeweet.org> |
|
57 |
*/ |
|
58 |
class MDB2_Driver_Reverse_sqlite extends MDB2_Driver_Reverse_Common |
|
59 |
{ |
|
60 |
/** |
|
61 |
* Remove SQL comments from the field definition |
|
62 |
* |
|
63 |
* @access private |
|
64 |
*/ |
|
65 |
function _removeComments($sql) { |
|
66 |
$lines = explode("\n", $sql); |
|
67 |
foreach ($lines as $k => $line) { |
|
68 |
$pieces = explode('--', $line); |
|
69 |
if (count($pieces) > 1 && (substr_count($pieces[0], '\'') % 2) == 0) { |
|
70 |
$lines[$k] = substr($line, 0, strpos($line, '--')); |
|
71 |
} |
|
72 |
} |
|
73 |
return implode("\n", $lines); |
|
74 |
} |
|
75 |
|
|
76 |
/** |
|
77 |
* |
|
78 |
*/ |
|
79 |
function _getTableColumns($sql) |
|
80 |
{ |
7244b4
|
81 |
$db = $this->getDBInstance(); |
2273d4
|
82 |
if (PEAR::isError($db)) { |
A |
83 |
return $db; |
|
84 |
} |
|
85 |
$start_pos = strpos($sql, '('); |
|
86 |
$end_pos = strrpos($sql, ')'); |
|
87 |
$column_def = substr($sql, $start_pos+1, $end_pos-$start_pos-1); |
|
88 |
// replace the decimal length-places-separator with a colon |
|
89 |
$column_def = preg_replace('/(\d),(\d)/', '\1:\2', $column_def); |
|
90 |
$column_def = $this->_removeComments($column_def); |
|
91 |
$column_sql = explode(',', $column_def); |
|
92 |
$columns = array(); |
|
93 |
$count = count($column_sql); |
|
94 |
if ($count == 0) { |
|
95 |
return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, |
|
96 |
'unexpected empty table column definition list', __FUNCTION__); |
|
97 |
} |
e6bb83
|
98 |
$regexp = '/^\s*([^\s]+) +(CHAR|VARCHAR|VARCHAR2|TEXT|BOOLEAN|SMALLINT|INT|INTEGER|DECIMAL|TINYINT|BIGINT|DOUBLE|FLOAT|DATETIME|DATE|TIME|LONGTEXT|LONGBLOB)( ?\(([1-9][0-9]*)(:([1-9][0-9]*))?\))?( NULL| NOT NULL)?( UNSIGNED)?( NULL| NOT NULL)?( PRIMARY KEY)?( DEFAULT (\'[^\']*\'|[^ ]+))?( NULL| NOT NULL)?( PRIMARY KEY)?(\s*\-\-.*)?$/i'; |
2273d4
|
99 |
$regexp2 = '/^\s*([^ ]+) +(PRIMARY|UNIQUE|CHECK)$/i'; |
A |
100 |
for ($i=0, $j=0; $i<$count; ++$i) { |
|
101 |
if (!preg_match($regexp, trim($column_sql[$i]), $matches)) { |
|
102 |
if (!preg_match($regexp2, trim($column_sql[$i]))) { |
|
103 |
continue; |
|
104 |
} |
|
105 |
return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, |
|
106 |
'unexpected table column SQL definition: "'.$column_sql[$i].'"', __FUNCTION__); |
|
107 |
} |
|
108 |
$columns[$j]['name'] = trim($matches[1], implode('', $db->identifier_quoting)); |
|
109 |
$columns[$j]['type'] = strtolower($matches[2]); |
|
110 |
if (isset($matches[4]) && strlen($matches[4])) { |
|
111 |
$columns[$j]['length'] = $matches[4]; |
|
112 |
} |
|
113 |
if (isset($matches[6]) && strlen($matches[6])) { |
|
114 |
$columns[$j]['decimal'] = $matches[6]; |
|
115 |
} |
|
116 |
if (isset($matches[8]) && strlen($matches[8])) { |
|
117 |
$columns[$j]['unsigned'] = true; |
|
118 |
} |
|
119 |
if (isset($matches[9]) && strlen($matches[9])) { |
|
120 |
$columns[$j]['autoincrement'] = true; |
|
121 |
} |
|
122 |
if (isset($matches[12]) && strlen($matches[12])) { |
|
123 |
$default = $matches[12]; |
|
124 |
if (strlen($default) && $default[0]=="'") { |
|
125 |
$default = str_replace("''", "'", substr($default, 1, strlen($default)-2)); |
|
126 |
} |
|
127 |
if ($default === 'NULL') { |
|
128 |
$default = null; |
|
129 |
} |
|
130 |
$columns[$j]['default'] = $default; |
|
131 |
} |
|
132 |
if (isset($matches[7]) && strlen($matches[7])) { |
|
133 |
$columns[$j]['notnull'] = ($matches[7] === ' NOT NULL'); |
|
134 |
} else if (isset($matches[9]) && strlen($matches[9])) { |
|
135 |
$columns[$j]['notnull'] = ($matches[9] === ' NOT NULL'); |
|
136 |
} else if (isset($matches[13]) && strlen($matches[13])) { |
|
137 |
$columns[$j]['notnull'] = ($matches[13] === ' NOT NULL'); |
|
138 |
} |
|
139 |
++$j; |
|
140 |
} |
|
141 |
return $columns; |
|
142 |
} |
|
143 |
|
|
144 |
// {{{ getTableFieldDefinition() |
|
145 |
|
|
146 |
/** |
|
147 |
* Get the stucture of a field into an array |
|
148 |
* |
|
149 |
* @param string $table_name name of table that should be used in method |
|
150 |
* @param string $field_name name of field that should be used in method |
|
151 |
* @return mixed data array on success, a MDB2 error on failure. |
|
152 |
* The returned array contains an array for each field definition, |
|
153 |
* with (some of) these indices: |
|
154 |
* [notnull] [nativetype] [length] [fixed] [default] [type] [mdb2type] |
|
155 |
* @access public |
|
156 |
*/ |
|
157 |
function getTableFieldDefinition($table_name, $field_name) |
|
158 |
{ |
7244b4
|
159 |
$db = $this->getDBInstance(); |
2273d4
|
160 |
if (PEAR::isError($db)) { |
A |
161 |
return $db; |
|
162 |
} |
|
163 |
|
|
164 |
list($schema, $table) = $this->splitTableSchema($table_name); |
|
165 |
|
|
166 |
$result = $db->loadModule('Datatype', null, true); |
|
167 |
if (PEAR::isError($result)) { |
|
168 |
return $result; |
|
169 |
} |
|
170 |
$query = "SELECT sql FROM sqlite_master WHERE type='table' AND "; |
|
171 |
if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) { |
|
172 |
$query.= 'LOWER(name)='.$db->quote(strtolower($table), 'text'); |
|
173 |
} else { |
|
174 |
$query.= 'name='.$db->quote($table, 'text'); |
|
175 |
} |
|
176 |
$sql = $db->queryOne($query); |
|
177 |
if (PEAR::isError($sql)) { |
|
178 |
return $sql; |
|
179 |
} |
|
180 |
$columns = $this->_getTableColumns($sql); |
|
181 |
foreach ($columns as $column) { |
|
182 |
if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) { |
|
183 |
if ($db->options['field_case'] == CASE_LOWER) { |
|
184 |
$column['name'] = strtolower($column['name']); |
|
185 |
} else { |
|
186 |
$column['name'] = strtoupper($column['name']); |
|
187 |
} |
|
188 |
} else { |
|
189 |
$column = array_change_key_case($column, $db->options['field_case']); |
|
190 |
} |
|
191 |
if ($field_name == $column['name']) { |
|
192 |
$mapped_datatype = $db->datatype->mapNativeDatatype($column); |
|
193 |
if (PEAR::isError($mapped_datatype)) { |
|
194 |
return $mapped_datatype; |
|
195 |
} |
|
196 |
list($types, $length, $unsigned, $fixed) = $mapped_datatype; |
|
197 |
$notnull = false; |
|
198 |
if (!empty($column['notnull'])) { |
|
199 |
$notnull = $column['notnull']; |
|
200 |
} |
|
201 |
$default = false; |
|
202 |
if (array_key_exists('default', $column)) { |
|
203 |
$default = $column['default']; |
|
204 |
if ((null === $default) && $notnull) { |
|
205 |
$default = ''; |
|
206 |
} |
|
207 |
} |
|
208 |
$autoincrement = false; |
|
209 |
if (!empty($column['autoincrement'])) { |
|
210 |
$autoincrement = true; |
|
211 |
} |
|
212 |
|
|
213 |
$definition[0] = array( |
|
214 |
'notnull' => $notnull, |
|
215 |
'nativetype' => preg_replace('/^([a-z]+)[^a-z].*/i', '\\1', $column['type']) |
|
216 |
); |
|
217 |
if (null !== $length) { |
|
218 |
$definition[0]['length'] = $length; |
|
219 |
} |
|
220 |
if (null !== $unsigned) { |
|
221 |
$definition[0]['unsigned'] = $unsigned; |
|
222 |
} |
|
223 |
if (null !== $fixed) { |
|
224 |
$definition[0]['fixed'] = $fixed; |
|
225 |
} |
|
226 |
if ($default !== false) { |
|
227 |
$definition[0]['default'] = $default; |
|
228 |
} |
|
229 |
if ($autoincrement !== false) { |
|
230 |
$definition[0]['autoincrement'] = $autoincrement; |
|
231 |
} |
|
232 |
foreach ($types as $key => $type) { |
|
233 |
$definition[$key] = $definition[0]; |
|
234 |
if ($type == 'clob' || $type == 'blob') { |
|
235 |
unset($definition[$key]['default']); |
|
236 |
} |
|
237 |
$definition[$key]['type'] = $type; |
|
238 |
$definition[$key]['mdb2type'] = $type; |
|
239 |
} |
|
240 |
return $definition; |
|
241 |
} |
|
242 |
} |
|
243 |
|
|
244 |
return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null, |
|
245 |
'it was not specified an existing table column', __FUNCTION__); |
|
246 |
} |
|
247 |
|
|
248 |
// }}} |
|
249 |
// {{{ getTableIndexDefinition() |
|
250 |
|
|
251 |
/** |
|
252 |
* Get the stucture of an index into an array |
|
253 |
* |
|
254 |
* @param string $table_name name of table that should be used in method |
|
255 |
* @param string $index_name name of index that should be used in method |
|
256 |
* @return mixed data array on success, a MDB2 error on failure |
|
257 |
* @access public |
|
258 |
*/ |
|
259 |
function getTableIndexDefinition($table_name, $index_name) |
|
260 |
{ |
7244b4
|
261 |
$db = $this->getDBInstance(); |
2273d4
|
262 |
if (PEAR::isError($db)) { |
A |
263 |
return $db; |
|
264 |
} |
|
265 |
|
|
266 |
list($schema, $table) = $this->splitTableSchema($table_name); |
|
267 |
|
|
268 |
$query = "SELECT sql FROM sqlite_master WHERE type='index' AND "; |
|
269 |
if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) { |
|
270 |
$query.= 'LOWER(name)=%s AND LOWER(tbl_name)=' . $db->quote(strtolower($table), 'text'); |
|
271 |
} else { |
|
272 |
$query.= 'name=%s AND tbl_name=' . $db->quote($table, 'text'); |
|
273 |
} |
|
274 |
$query.= ' AND sql NOT NULL ORDER BY name'; |
|
275 |
$index_name_mdb2 = $db->getIndexName($index_name); |
|
276 |
if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) { |
|
277 |
$qry = sprintf($query, $db->quote(strtolower($index_name_mdb2), 'text')); |
|
278 |
} else { |
|
279 |
$qry = sprintf($query, $db->quote($index_name_mdb2, 'text')); |
|
280 |
} |
|
281 |
$sql = $db->queryOne($qry, 'text'); |
|
282 |
if (PEAR::isError($sql) || empty($sql)) { |
|
283 |
// fallback to the given $index_name, without transformation |
|
284 |
if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) { |
|
285 |
$qry = sprintf($query, $db->quote(strtolower($index_name), 'text')); |
|
286 |
} else { |
|
287 |
$qry = sprintf($query, $db->quote($index_name, 'text')); |
|
288 |
} |
|
289 |
$sql = $db->queryOne($qry, 'text'); |
|
290 |
} |
|
291 |
if (PEAR::isError($sql)) { |
|
292 |
return $sql; |
|
293 |
} |
|
294 |
if (!$sql) { |
|
295 |
return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null, |
|
296 |
'it was not specified an existing table index', __FUNCTION__); |
|
297 |
} |
|
298 |
|
|
299 |
$sql = strtolower($sql); |
|
300 |
$start_pos = strpos($sql, '('); |
|
301 |
$end_pos = strrpos($sql, ')'); |
|
302 |
$column_names = substr($sql, $start_pos+1, $end_pos-$start_pos-1); |
|
303 |
$column_names = explode(',', $column_names); |
|
304 |
|
|
305 |
if (preg_match("/^create unique/", $sql)) { |
|
306 |
return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null, |
|
307 |
'it was not specified an existing table index', __FUNCTION__); |
|
308 |
} |
|
309 |
|
|
310 |
$definition = array(); |
|
311 |
$count = count($column_names); |
|
312 |
for ($i=0; $i<$count; ++$i) { |
|
313 |
$column_name = strtok($column_names[$i], ' '); |
|
314 |
$collation = strtok(' '); |
|
315 |
$definition['fields'][$column_name] = array( |
|
316 |
'position' => $i+1 |
|
317 |
); |
|
318 |
if (!empty($collation)) { |
|
319 |
$definition['fields'][$column_name]['sorting'] = |
|
320 |
($collation=='ASC' ? 'ascending' : 'descending'); |
|
321 |
} |
|
322 |
} |
|
323 |
|
|
324 |
if (empty($definition['fields'])) { |
|
325 |
return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null, |
|
326 |
'it was not specified an existing table index', __FUNCTION__); |
|
327 |
} |
|
328 |
return $definition; |
|
329 |
} |
|
330 |
|
|
331 |
// }}} |
|
332 |
// {{{ getTableConstraintDefinition() |
|
333 |
|
|
334 |
/** |
|
335 |
* Get the stucture of a constraint into an array |
|
336 |
* |
|
337 |
* @param string $table_name name of table that should be used in method |
|
338 |
* @param string $constraint_name name of constraint that should be used in method |
|
339 |
* @return mixed data array on success, a MDB2 error on failure |
|
340 |
* @access public |
|
341 |
*/ |
|
342 |
function getTableConstraintDefinition($table_name, $constraint_name) |
|
343 |
{ |
7244b4
|
344 |
$db = $this->getDBInstance(); |
2273d4
|
345 |
if (PEAR::isError($db)) { |
A |
346 |
return $db; |
|
347 |
} |
|
348 |
|
|
349 |
list($schema, $table) = $this->splitTableSchema($table_name); |
|
350 |
|
|
351 |
$query = "SELECT sql FROM sqlite_master WHERE type='index' AND "; |
|
352 |
if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) { |
|
353 |
$query.= 'LOWER(name)=%s AND LOWER(tbl_name)=' . $db->quote(strtolower($table), 'text'); |
|
354 |
} else { |
|
355 |
$query.= 'name=%s AND tbl_name=' . $db->quote($table, 'text'); |
|
356 |
} |
|
357 |
$query.= ' AND sql NOT NULL ORDER BY name'; |
|
358 |
$constraint_name_mdb2 = $db->getIndexName($constraint_name); |
|
359 |
if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) { |
|
360 |
$qry = sprintf($query, $db->quote(strtolower($constraint_name_mdb2), 'text')); |
|
361 |
} else { |
|
362 |
$qry = sprintf($query, $db->quote($constraint_name_mdb2, 'text')); |
|
363 |
} |
|
364 |
$sql = $db->queryOne($qry, 'text'); |
|
365 |
if (PEAR::isError($sql) || empty($sql)) { |
|
366 |
// fallback to the given $index_name, without transformation |
|
367 |
if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) { |
|
368 |
$qry = sprintf($query, $db->quote(strtolower($constraint_name), 'text')); |
|
369 |
} else { |
|
370 |
$qry = sprintf($query, $db->quote($constraint_name, 'text')); |
|
371 |
} |
|
372 |
$sql = $db->queryOne($qry, 'text'); |
|
373 |
} |
|
374 |
if (PEAR::isError($sql)) { |
|
375 |
return $sql; |
|
376 |
} |
|
377 |
//default values, eventually overridden |
|
378 |
$definition = array( |
|
379 |
'primary' => false, |
|
380 |
'unique' => false, |
|
381 |
'foreign' => false, |
|
382 |
'check' => false, |
|
383 |
'fields' => array(), |
|
384 |
'references' => array( |
|
385 |
'table' => '', |
|
386 |
'fields' => array(), |
|
387 |
), |
|
388 |
'onupdate' => '', |
|
389 |
'ondelete' => '', |
|
390 |
'match' => '', |
|
391 |
'deferrable' => false, |
|
392 |
'initiallydeferred' => false, |
|
393 |
); |
|
394 |
if (!$sql) { |
|
395 |
$query = "SELECT sql FROM sqlite_master WHERE type='table' AND "; |
|
396 |
if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) { |
|
397 |
$query.= 'LOWER(name)='.$db->quote(strtolower($table), 'text'); |
|
398 |
} else { |
|
399 |
$query.= 'name='.$db->quote($table, 'text'); |
|
400 |
} |
|
401 |
$query.= " AND sql NOT NULL ORDER BY name"; |
|
402 |
$sql = $db->queryOne($query, 'text'); |
|
403 |
if (PEAR::isError($sql)) { |
|
404 |
return $sql; |
|
405 |
} |
|
406 |
if ($constraint_name == 'primary') { |
|
407 |
// search in table definition for PRIMARY KEYs |
|
408 |
if (preg_match("/\bPRIMARY\s+KEY\b\s*\(([^)]+)/i", $sql, $tmp)) { |
|
409 |
$definition['primary'] = true; |
|
410 |
$definition['fields'] = array(); |
|
411 |
$column_names = explode(',', $tmp[1]); |
|
412 |
$colpos = 1; |
|
413 |
foreach ($column_names as $column_name) { |
|
414 |
$definition['fields'][trim($column_name)] = array( |
|
415 |
'position' => $colpos++ |
|
416 |
); |
|
417 |
} |
|
418 |
return $definition; |
|
419 |
} |
|
420 |
if (preg_match("/\"([^\"]+)\"[^\,\"]+\bPRIMARY\s+KEY\b[^\,\)]*/i", $sql, $tmp)) { |
|
421 |
$definition['primary'] = true; |
|
422 |
$definition['fields'] = array(); |
|
423 |
$column_names = explode(',', $tmp[1]); |
|
424 |
$colpos = 1; |
|
425 |
foreach ($column_names as $column_name) { |
|
426 |
$definition['fields'][trim($column_name)] = array( |
|
427 |
'position' => $colpos++ |
|
428 |
); |
|
429 |
} |
|
430 |
return $definition; |
|
431 |
} |
|
432 |
} else { |
|
433 |
// search in table definition for FOREIGN KEYs |
|
434 |
$pattern = "/\bCONSTRAINT\b\s+%s\s+ |
|
435 |
\bFOREIGN\s+KEY\b\s*\(([^\)]+)\)\s* |
|
436 |
\bREFERENCES\b\s+([^\s]+)\s*\(([^\)]+)\)\s* |
|
437 |
(?:\bMATCH\s*([^\s]+))?\s* |
|
438 |
(?:\bON\s+UPDATE\s+([^\s,\)]+))?\s* |
|
439 |
(?:\bON\s+DELETE\s+([^\s,\)]+))?\s* |
|
440 |
/imsx"; |
|
441 |
$found_fk = false; |
|
442 |
if (preg_match(sprintf($pattern, $constraint_name_mdb2), $sql, $tmp)) { |
|
443 |
$found_fk = true; |
|
444 |
} elseif (preg_match(sprintf($pattern, $constraint_name), $sql, $tmp)) { |
|
445 |
$found_fk = true; |
|
446 |
} |
|
447 |
if ($found_fk) { |
|
448 |
$definition['foreign'] = true; |
|
449 |
$definition['match'] = 'SIMPLE'; |
|
450 |
$definition['onupdate'] = 'NO ACTION'; |
|
451 |
$definition['ondelete'] = 'NO ACTION'; |
|
452 |
$definition['references']['table'] = $tmp[2]; |
|
453 |
$column_names = explode(',', $tmp[1]); |
|
454 |
$colpos = 1; |
|
455 |
foreach ($column_names as $column_name) { |
|
456 |
$definition['fields'][trim($column_name)] = array( |
|
457 |
'position' => $colpos++ |
|
458 |
); |
|
459 |
} |
|
460 |
$referenced_cols = explode(',', $tmp[3]); |
|
461 |
$colpos = 1; |
|
462 |
foreach ($referenced_cols as $column_name) { |
|
463 |
$definition['references']['fields'][trim($column_name)] = array( |
|
464 |
'position' => $colpos++ |
|
465 |
); |
|
466 |
} |
|
467 |
if (isset($tmp[4])) { |
|
468 |
$definition['match'] = $tmp[4]; |
|
469 |
} |
|
470 |
if (isset($tmp[5])) { |
|
471 |
$definition['onupdate'] = $tmp[5]; |
|
472 |
} |
|
473 |
if (isset($tmp[6])) { |
|
474 |
$definition['ondelete'] = $tmp[6]; |
|
475 |
} |
|
476 |
return $definition; |
|
477 |
} |
|
478 |
} |
|
479 |
$sql = false; |
|
480 |
} |
|
481 |
if (!$sql) { |
|
482 |
return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null, |
|
483 |
$constraint_name . ' is not an existing table constraint', __FUNCTION__); |
|
484 |
} |
|
485 |
|
|
486 |
$sql = strtolower($sql); |
|
487 |
$start_pos = strpos($sql, '('); |
|
488 |
$end_pos = strrpos($sql, ')'); |
|
489 |
$column_names = substr($sql, $start_pos+1, $end_pos-$start_pos-1); |
|
490 |
$column_names = explode(',', $column_names); |
|
491 |
|
|
492 |
if (!preg_match("/^create unique/", $sql)) { |
|
493 |
return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null, |
|
494 |
$constraint_name . ' is not an existing table constraint', __FUNCTION__); |
|
495 |
} |
|
496 |
|
|
497 |
$definition['unique'] = true; |
|
498 |
$count = count($column_names); |
|
499 |
for ($i=0; $i<$count; ++$i) { |
|
500 |
$column_name = strtok($column_names[$i]," "); |
|
501 |
$collation = strtok(" "); |
|
502 |
$definition['fields'][$column_name] = array( |
|
503 |
'position' => $i+1 |
|
504 |
); |
|
505 |
if (!empty($collation)) { |
|
506 |
$definition['fields'][$column_name]['sorting'] = |
|
507 |
($collation=='ASC' ? 'ascending' : 'descending'); |
|
508 |
} |
|
509 |
} |
|
510 |
|
|
511 |
if (empty($definition['fields'])) { |
|
512 |
return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null, |
|
513 |
$constraint_name . ' is not an existing table constraint', __FUNCTION__); |
|
514 |
} |
|
515 |
return $definition; |
|
516 |
} |
|
517 |
|
|
518 |
// }}} |
|
519 |
// {{{ getTriggerDefinition() |
|
520 |
|
|
521 |
/** |
|
522 |
* Get the structure of a trigger into an array |
|
523 |
* |
|
524 |
* EXPERIMENTAL |
|
525 |
* |
|
526 |
* WARNING: this function is experimental and may change the returned value |
|
527 |
* at any time until labelled as non-experimental |
|
528 |
* |
|
529 |
* @param string $trigger name of trigger that should be used in method |
|
530 |
* @return mixed data array on success, a MDB2 error on failure |
|
531 |
* @access public |
|
532 |
*/ |
|
533 |
function getTriggerDefinition($trigger) |
|
534 |
{ |
7244b4
|
535 |
$db = $this->getDBInstance(); |
2273d4
|
536 |
if (PEAR::isError($db)) { |
A |
537 |
return $db; |
|
538 |
} |
|
539 |
|
|
540 |
$query = "SELECT name as trigger_name, |
|
541 |
tbl_name AS table_name, |
|
542 |
sql AS trigger_body, |
|
543 |
NULL AS trigger_type, |
|
544 |
NULL AS trigger_event, |
|
545 |
NULL AS trigger_comment, |
|
546 |
1 AS trigger_enabled |
|
547 |
FROM sqlite_master |
|
548 |
WHERE type='trigger'"; |
|
549 |
if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) { |
|
550 |
$query.= ' AND LOWER(name)='.$db->quote(strtolower($trigger), 'text'); |
|
551 |
} else { |
|
552 |
$query.= ' AND name='.$db->quote($trigger, 'text'); |
|
553 |
} |
|
554 |
$types = array( |
|
555 |
'trigger_name' => 'text', |
|
556 |
'table_name' => 'text', |
|
557 |
'trigger_body' => 'text', |
|
558 |
'trigger_type' => 'text', |
|
559 |
'trigger_event' => 'text', |
|
560 |
'trigger_comment' => 'text', |
|
561 |
'trigger_enabled' => 'boolean', |
|
562 |
); |
|
563 |
$def = $db->queryRow($query, $types, MDB2_FETCHMODE_ASSOC); |
|
564 |
if (PEAR::isError($def)) { |
|
565 |
return $def; |
|
566 |
} |
|
567 |
if (empty($def)) { |
|
568 |
return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null, |
|
569 |
'it was not specified an existing trigger', __FUNCTION__); |
|
570 |
} |
|
571 |
if (preg_match("/^create\s+(?:temp|temporary)?trigger\s+(?:if\s+not\s+exists\s+)?.*(before|after)?\s+(insert|update|delete)/Uims", $def['trigger_body'], $tmp)) { |
|
572 |
$def['trigger_type'] = strtoupper($tmp[1]); |
|
573 |
$def['trigger_event'] = strtoupper($tmp[2]); |
|
574 |
} |
|
575 |
return $def; |
|
576 |
} |
|
577 |
|
|
578 |
// }}} |
|
579 |
// {{{ tableInfo() |
|
580 |
|
|
581 |
/** |
|
582 |
* Returns information about a table |
|
583 |
* |
|
584 |
* @param string $result a string containing the name of a table |
|
585 |
* @param int $mode a valid tableInfo mode |
|
586 |
* |
|
587 |
* @return array an associative array with the information requested. |
|
588 |
* A MDB2_Error object on failure. |
|
589 |
* |
|
590 |
* @see MDB2_Driver_Common::tableInfo() |
|
591 |
* @since Method available since Release 1.7.0 |
|
592 |
*/ |
|
593 |
function tableInfo($result, $mode = null) |
|
594 |
{ |
|
595 |
if (is_string($result)) { |
|
596 |
return parent::tableInfo($result, $mode); |
|
597 |
} |
|
598 |
|
7244b4
|
599 |
$db = $this->getDBInstance(); |
2273d4
|
600 |
if (PEAR::isError($db)) { |
A |
601 |
return $db; |
|
602 |
} |
|
603 |
|
|
604 |
return $db->raiseError(MDB2_ERROR_NOT_CAPABLE, null, null, |
|
605 |
'This DBMS can not obtain tableInfo from result sets', __FUNCTION__); |
|
606 |
} |
|
607 |
} |
|
608 |
|
95ebbc
|
609 |
?> |