commit | author | age
|
c9462d
|
1 |
<?php |
S |
2 |
// +----------------------------------------------------------------------+ |
|
3 |
// | PHP versions 4 and 5 | |
|
4 |
// +----------------------------------------------------------------------+ |
|
5 |
// | Copyright (c) 1998-2004 Manuel Lemos, Tomas V.V.Cox, | |
|
6 |
// | Stig. S. Bakken, Lukas Smith | |
|
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 |
// | Author: Lukas Smith <smith@pooteeweet.org> | |
|
43 |
// +----------------------------------------------------------------------+ |
|
44 |
// |
|
45 |
// $Id$ |
|
46 |
// |
|
47 |
|
|
48 |
require_once 'MDB2/Driver/Manager/Common.php'; |
|
49 |
|
|
50 |
/** |
|
51 |
* MDB2 MySQLi driver for the management modules |
|
52 |
* |
|
53 |
* @package MDB2 |
|
54 |
* @category Database |
|
55 |
* @author Lukas Smith <smith@pooteeweet.org> |
|
56 |
*/ |
|
57 |
class MDB2_Driver_Manager_mysqli extends MDB2_Driver_Manager_Common |
|
58 |
{ |
|
59 |
// {{{ properties |
|
60 |
var $verified_table_types = array();# |
|
61 |
// }}} |
|
62 |
|
|
63 |
// }}} |
|
64 |
// {{{ _verifyTableType() |
|
65 |
|
|
66 |
/** |
|
67 |
* verify that chosen transactional table hanlder is available in the database |
|
68 |
* |
|
69 |
* @param string $table_type name of the table handler |
|
70 |
* @return mixed MDB2_OK on success, a MDB2 error on failure |
|
71 |
* @access protected |
|
72 |
*/ |
|
73 |
function _verifyTableType($table_type) |
|
74 |
{ |
|
75 |
$db =& $this->getDBInstance(); |
|
76 |
if (PEAR::isError($db)) { |
|
77 |
return $db; |
|
78 |
} |
|
79 |
|
|
80 |
switch (strtoupper($table_type)) { |
|
81 |
case 'BERKELEYDB': |
|
82 |
case 'BDB': |
|
83 |
$check = array('have_bdb'); |
|
84 |
break; |
|
85 |
case 'INNODB': |
|
86 |
$check = array('have_innobase', 'have_innodb'); |
|
87 |
break; |
|
88 |
case 'GEMINI': |
|
89 |
$check = array('have_gemini'); |
|
90 |
break; |
|
91 |
case 'HEAP': |
|
92 |
case 'ISAM': |
|
93 |
case 'MERGE': |
|
94 |
case 'MRG_MYISAM': |
|
95 |
case 'MYISAM': |
|
96 |
case '': |
|
97 |
return MDB2_OK; |
|
98 |
default: |
|
99 |
return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, |
|
100 |
$table_type.' is not a supported table type'); |
|
101 |
} |
|
102 |
if (isset($this->verified_table_types[$table_type]) |
|
103 |
&& $this->verified_table_types[$table_type] == $db->connection |
|
104 |
) { |
|
105 |
return MDB2_OK; |
|
106 |
} |
|
107 |
$not_supported = false; |
|
108 |
for ($i = 0, $j = count($check); $i < $j; ++$i) { |
|
109 |
$query = 'SHOW VARIABLES LIKE '.$db->quote($check[$i], 'text'); |
|
110 |
$has = $db->queryRow($query, null, MDB2_FETCHMODE_ORDERED); |
|
111 |
if (PEAR::isError($has)) { |
|
112 |
return $has; |
|
113 |
} |
|
114 |
if (is_array($has)) { |
|
115 |
$not_supported = true; |
|
116 |
if ($has[1] == 'YES') { |
|
117 |
$this->verified_table_types[$table_type] = $db->connection; |
|
118 |
return MDB2_OK; |
|
119 |
} |
|
120 |
} |
|
121 |
} |
|
122 |
if ($not_supported) { |
|
123 |
return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, |
|
124 |
$table_type.' is not a supported table type by this MySQL database server'); |
|
125 |
} |
|
126 |
return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, |
|
127 |
'could not tell if '.$table_type.' is a supported table type'); |
|
128 |
} |
|
129 |
|
|
130 |
// }}} |
|
131 |
// {{{ createDatabase() |
|
132 |
|
|
133 |
/** |
|
134 |
* create a new database |
|
135 |
* |
|
136 |
* @param string $name name of the database that should be created |
|
137 |
* @return mixed MDB2_OK on success, a MDB2 error on failure |
|
138 |
* @access public |
|
139 |
*/ |
|
140 |
function createDatabase($name) |
|
141 |
{ |
|
142 |
$db =& $this->getDBInstance(); |
|
143 |
if (PEAR::isError($db)) { |
|
144 |
return $db; |
|
145 |
} |
|
146 |
|
|
147 |
$query = 'CREATE DATABASE '.$name; |
|
148 |
$result = $db->query($query); |
|
149 |
if (PEAR::isError($result)) { |
|
150 |
return $result; |
|
151 |
} |
|
152 |
return MDB2_OK; |
|
153 |
} |
|
154 |
|
|
155 |
// }}} |
|
156 |
// {{{ dropDatabase() |
|
157 |
|
|
158 |
/** |
|
159 |
* drop an existing database |
|
160 |
* |
|
161 |
* @param string $name name of the database that should be dropped |
|
162 |
* @return mixed MDB2_OK on success, a MDB2 error on failure |
|
163 |
* @access public |
|
164 |
*/ |
|
165 |
function dropDatabase($name) |
|
166 |
{ |
|
167 |
$db =& $this->getDBInstance(); |
|
168 |
if (PEAR::isError($db)) { |
|
169 |
return $db; |
|
170 |
} |
|
171 |
|
|
172 |
$query = 'DROP DATABASE '.$name; |
|
173 |
$result = $db->query($query); |
|
174 |
if (PEAR::isError($result)) { |
|
175 |
return $result; |
|
176 |
} |
|
177 |
return MDB2_OK; |
|
178 |
} |
|
179 |
|
|
180 |
// }}} |
|
181 |
// {{{ createTable() |
|
182 |
|
|
183 |
/** |
|
184 |
* create a new table |
|
185 |
* |
|
186 |
* @param string $name Name of the database that should be created |
|
187 |
* @param array $fields Associative array that contains the definition of each field of the new table |
|
188 |
* The indexes of the array entries are the names of the fields of the table an |
|
189 |
* the array entry values are associative arrays like those that are meant to be |
|
190 |
* passed with the field definitions to get[Type]Declaration() functions. |
|
191 |
* |
|
192 |
* Example |
|
193 |
* array( |
|
194 |
* |
|
195 |
* 'id' => array( |
|
196 |
* 'type' => 'integer', |
|
197 |
* 'unsigned' => 1 |
|
198 |
* 'notnull' => 1 |
|
199 |
* 'default' => 0 |
|
200 |
* ), |
|
201 |
* 'name' => array( |
|
202 |
* 'type' => 'text', |
|
203 |
* 'length' => 12 |
|
204 |
* ), |
|
205 |
* 'password' => array( |
|
206 |
* 'type' => 'text', |
|
207 |
* 'length' => 12 |
|
208 |
* ) |
|
209 |
* ); |
|
210 |
* @return mixed MDB2_OK on success, a MDB2 error on failure |
|
211 |
* @access public |
|
212 |
*/ |
|
213 |
function createTable($name, $fields) |
|
214 |
{ |
|
215 |
$db =& $this->getDBInstance(); |
|
216 |
if (PEAR::isError($db)) { |
|
217 |
return $db; |
|
218 |
} |
|
219 |
|
|
220 |
if (!$name) { |
|
221 |
return $db->raiseError(MDB2_ERROR_CANNOT_CREATE, null, null, |
|
222 |
'createTable: no valid table name specified'); |
|
223 |
} |
|
224 |
if (empty($fields)) { |
|
225 |
return $db->raiseError(MDB2_ERROR_CANNOT_CREATE, null, null, |
|
226 |
'createTable: no fields specified for table "'.$name.'"'); |
|
227 |
} |
|
228 |
$verify = $this->_verifyTableType($db->options['default_table_type']); |
|
229 |
if (PEAR::isError($verify)) { |
|
230 |
return $verify; |
|
231 |
} |
|
232 |
$query_fields = $this->getFieldDeclarationList($fields); |
|
233 |
if (PEAR::isError($query_fields)) { |
|
234 |
return $db->raiseError(MDB2_ERROR_CANNOT_CREATE, null, null, |
|
235 |
'createTable: '.$this->getUserinfo()); |
|
236 |
} |
|
237 |
$query = "CREATE TABLE $name ($query_fields)".(strlen($db->options['default_table_type']) |
|
238 |
? ' TYPE='.$db->options['default_table_type'] : ''); |
|
239 |
|
|
240 |
return $db->query($query); |
|
241 |
} |
|
242 |
|
|
243 |
// }}} |
|
244 |
// {{{ alterTable() |
|
245 |
|
|
246 |
/** |
|
247 |
* alter an existing table |
|
248 |
* |
|
249 |
* @param string $name name of the table that is intended to be changed. |
|
250 |
* @param array $changes associative array that contains the details of each type |
|
251 |
* of change that is intended to be performed. The types of |
|
252 |
* changes that are currently supported are defined as follows: |
|
253 |
* |
|
254 |
* name |
|
255 |
* |
|
256 |
* New name for the table. |
|
257 |
* |
|
258 |
* add |
|
259 |
* |
|
260 |
* Associative array with the names of fields to be added as |
|
261 |
* indexes of the array. The value of each entry of the array |
|
262 |
* should be set to another associative array with the properties |
|
263 |
* of the fields to be added. The properties of the fields should |
|
264 |
* be the same as defined by the Metabase parser. |
|
265 |
* |
|
266 |
* |
|
267 |
* remove |
|
268 |
* |
|
269 |
* Associative array with the names of fields to be removed as indexes |
|
270 |
* of the array. Currently the values assigned to each entry are ignored. |
|
271 |
* An empty array should be used for future compatibility. |
|
272 |
* |
|
273 |
* rename |
|
274 |
* |
|
275 |
* Associative array with the names of fields to be renamed as indexes |
|
276 |
* of the array. The value of each entry of the array should be set to |
|
277 |
* another associative array with the entry named name with the new |
|
278 |
* field name and the entry named Declaration that is expected to contain |
|
279 |
* the portion of the field declaration already in DBMS specific SQL code |
|
280 |
* as it is used in the CREATE TABLE statement. |
|
281 |
* |
|
282 |
* change |
|
283 |
* |
|
284 |
* Associative array with the names of the fields to be changed as indexes |
|
285 |
* of the array. Keep in mind that if it is intended to change either the |
|
286 |
* name of a field and any other properties, the change array entries |
|
287 |
* should have the new names of the fields as array indexes. |
|
288 |
* |
|
289 |
* The value of each entry of the array should be set to another associative |
|
290 |
* array with the properties of the fields to that are meant to be changed as |
|
291 |
* array entries. These entries should be assigned to the new values of the |
|
292 |
* respective properties. The properties of the fields should be the same |
|
293 |
* as defined by the Metabase parser. |
|
294 |
* |
|
295 |
* Example |
|
296 |
* array( |
|
297 |
* 'name' => 'userlist', |
|
298 |
* 'add' => array( |
|
299 |
* 'quota' => array( |
|
300 |
* 'type' => 'integer', |
|
301 |
* 'unsigned' => 1 |
|
302 |
* ) |
|
303 |
* ), |
|
304 |
* 'remove' => array( |
|
305 |
* 'file_limit' => array(), |
|
306 |
* 'time_limit' => array() |
|
307 |
* ), |
|
308 |
* 'change' => array( |
|
309 |
* 'gender' => array( |
|
310 |
* 'default' => 'M', |
|
311 |
* ) |
|
312 |
* ), |
|
313 |
* 'rename' => array( |
|
314 |
* 'sex' => array( |
|
315 |
* 'name' => 'gender', |
|
316 |
* ) |
|
317 |
* ) |
|
318 |
* ) |
|
319 |
* |
|
320 |
* @param boolean $check indicates whether the function should just check if the DBMS driver |
|
321 |
* can perform the requested table alterations if the value is true or |
|
322 |
* actually perform them otherwise. |
|
323 |
* @access public |
|
324 |
* |
|
325 |
* @return mixed MDB2_OK on success, a MDB2 error on failure |
|
326 |
*/ |
|
327 |
function alterTable($name, $changes, $check) |
|
328 |
{ |
|
329 |
$db =& $this->getDBInstance(); |
|
330 |
if (PEAR::isError($db)) { |
|
331 |
return $db; |
|
332 |
} |
|
333 |
|
|
334 |
foreach ($changes as $change_name => $change) { |
|
335 |
switch ($change_name) { |
|
336 |
case 'add': |
|
337 |
case 'remove': |
|
338 |
case 'change': |
|
339 |
case 'rename': |
|
340 |
case 'name': |
|
341 |
break; |
|
342 |
default: |
|
343 |
return $db->raiseError(MDB2_ERROR_CANNOT_ALTER, null, null, |
|
344 |
'alterTable: change type "'.$change_name.'" not yet supported'); |
|
345 |
} |
|
346 |
} |
|
347 |
|
|
348 |
if ($check) { |
|
349 |
return MDB2_OK; |
|
350 |
} |
|
351 |
|
|
352 |
$query = (array_key_exists('name', $changes) ? 'RENAME AS '.$changes['name'] : ''); |
|
353 |
|
|
354 |
if (array_key_exists('add', $changes)) { |
|
355 |
foreach ($changes['add'] as $field_name => $field) { |
|
356 |
$type_declaration = $db->getDeclaration($field['type'], $field_name, $field); |
|
357 |
if (PEAR::isError($type_declaration)) { |
|
358 |
return $err; |
|
359 |
} |
|
360 |
if ($query) { |
|
361 |
$query.= ', '; |
|
362 |
} |
|
363 |
$query.= 'ADD ' . $type_declaration; |
|
364 |
} |
|
365 |
} |
|
366 |
|
|
367 |
if (array_key_exists('remove', $changes)) { |
|
368 |
foreach ($changes['remove'] as $field_name => $field) { |
|
369 |
if ($query) { |
|
370 |
$query.= ', '; |
|
371 |
} |
|
372 |
$query.= 'DROP ' . $field_name; |
|
373 |
} |
|
374 |
} |
|
375 |
|
|
376 |
$rename = array(); |
|
377 |
if (array_key_exists('rename', $changes)) { |
|
378 |
foreach ($changes['rename'] as $field_name => $field) { |
|
379 |
$rename[$field['name']] = $field_name; |
|
380 |
} |
|
381 |
} |
|
382 |
|
|
383 |
if (array_key_exists('change', $changes)) { |
|
384 |
foreach ($changes['change'] as $field_name => $field) { |
|
385 |
if ($query) { |
|
386 |
$query.= ', '; |
|
387 |
} |
|
388 |
if (isset($rename[$field_name])) { |
|
389 |
$old_field_name = $rename[$field_name]; |
|
390 |
unset($rename[$field_name]); |
|
391 |
} else { |
|
392 |
$old_field_name = $field_name; |
|
393 |
} |
|
394 |
$query.= "CHANGE $field_name " . $db->getDeclaration($field['type'], $old_field_name, $field); |
|
395 |
} |
|
396 |
} |
|
397 |
|
|
398 |
if (!empty($rename)) { |
|
399 |
foreach ($rename as $rename_name => $renamed_field) { |
|
400 |
if ($query) { |
|
401 |
$query.= ', '; |
|
402 |
} |
|
403 |
$old_field_name = $renamed_field; |
|
404 |
$field = $changes['rename'][$old_field_name]; |
|
405 |
$query.= 'CHANGE ' . $db->getDeclaration($field['type'], $old_field_name, $field); |
|
406 |
} |
|
407 |
} |
|
408 |
|
|
409 |
if (!$query) { |
|
410 |
return MDB2_OK; |
|
411 |
} |
|
412 |
|
|
413 |
return $db->query("ALTER TABLE $name $query"); |
|
414 |
} |
|
415 |
|
|
416 |
// }}} |
|
417 |
// {{{ listDatabases() |
|
418 |
|
|
419 |
/** |
|
420 |
* list all databases |
|
421 |
* |
|
422 |
* @return mixed data array on success, a MDB2 error on failure |
|
423 |
* @access public |
|
424 |
*/ |
|
425 |
function listDatabases() |
|
426 |
{ |
|
427 |
$db =& $this->getDBInstance(); |
|
428 |
if (PEAR::isError($db)) { |
|
429 |
return $db; |
|
430 |
} |
|
431 |
|
|
432 |
$databases = $db->queryCol('SHOW DATABASES'); |
|
433 |
return $databases; |
|
434 |
} |
|
435 |
|
|
436 |
// }}} |
|
437 |
// {{{ listUsers() |
|
438 |
|
|
439 |
/** |
|
440 |
* list all users |
|
441 |
* |
|
442 |
* @return mixed data array on success, a MDB2 error on failure |
|
443 |
* @access public |
|
444 |
*/ |
|
445 |
function listUsers() |
|
446 |
{ |
|
447 |
$db =& $this->getDBInstance(); |
|
448 |
if (PEAR::isError($db)) { |
|
449 |
return $db; |
|
450 |
} |
|
451 |
|
|
452 |
$users = $db->queryCol('SELECT DISTINCT USER FROM USER'); |
|
453 |
return $users; |
|
454 |
} |
|
455 |
|
|
456 |
// }}} |
|
457 |
// {{{ listTables() |
|
458 |
|
|
459 |
/** |
|
460 |
* list all tables in the current database |
|
461 |
* |
|
462 |
* @return mixed data array on success, a MDB2 error on failure |
|
463 |
* @access public |
|
464 |
*/ |
|
465 |
function listTables() |
|
466 |
{ |
|
467 |
$db =& $this->getDBInstance(); |
|
468 |
if (PEAR::isError($db)) { |
|
469 |
return $db; |
|
470 |
} |
|
471 |
|
|
472 |
$table_names = $db->queryCol('SHOW TABLES'); |
|
473 |
if (PEAR::isError($table_names)) { |
|
474 |
return $table_names; |
|
475 |
} |
|
476 |
|
|
477 |
$tables = array(); |
|
478 |
for ($i = 0, $j = count($table_names); $i < $j; ++$i) { |
|
479 |
if (!$this->_isSequenceName($table_names[$i])) |
|
480 |
$tables[] = $table_names[$i]; |
|
481 |
} |
|
482 |
|
|
483 |
if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) { |
|
484 |
$tables = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $tables); |
|
485 |
} |
|
486 |
|
|
487 |
return $tables; |
|
488 |
} |
|
489 |
|
|
490 |
// }}} |
|
491 |
// {{{ listTableFields() |
|
492 |
|
|
493 |
/** |
|
494 |
* list all fields in a tables in the current database |
|
495 |
* |
|
496 |
* @param string $table name of table that should be used in method |
|
497 |
* @return mixed data array on success, a MDB2 error on failure |
|
498 |
* @access public |
|
499 |
*/ |
|
500 |
function listTableFields($table) |
|
501 |
{ |
|
502 |
$db =& $this->getDBInstance(); |
|
503 |
if (PEAR::isError($db)) { |
|
504 |
return $db; |
|
505 |
} |
|
506 |
|
|
507 |
$fields = $db->queryCol("SHOW COLUMNS FROM $table"); |
|
508 |
if (PEAR::isError($fields)) { |
|
509 |
return $fields; |
|
510 |
} |
|
511 |
|
|
512 |
if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) { |
|
513 |
$fields = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $fields); |
|
514 |
} |
|
515 |
|
|
516 |
return $fields; |
|
517 |
} |
|
518 |
|
|
519 |
// }}} |
|
520 |
// {{{ createIndex() |
|
521 |
|
|
522 |
/** |
|
523 |
* get the stucture of a field into an array |
|
524 |
* |
|
525 |
* @param string $table name of the table on which the index is to be created |
|
526 |
* @param string $name name of the index to be created |
|
527 |
* @param array $definition associative array that defines properties of the index to be created. |
|
528 |
* Currently, only one property named FIELDS is supported. This property |
|
529 |
* is also an associative with the names of the index fields as array |
|
530 |
* indexes. Each entry of this array is set to another type of associative |
|
531 |
* array that specifies properties of the index that are specific to |
|
532 |
* each field. |
|
533 |
* |
|
534 |
* Currently, only the sorting property is supported. It should be used |
|
535 |
* to define the sorting direction of the index. It may be set to either |
|
536 |
* ascending or descending. |
|
537 |
* |
|
538 |
* Not all DBMS support index sorting direction configuration. The DBMS |
|
539 |
* drivers of those that do not support it ignore this property. Use the |
|
540 |
* function supports() to determine whether the DBMS driver can manage indexes. |
|
541 |
|
|
542 |
* Example |
|
543 |
* array( |
|
544 |
* 'fields' => array( |
|
545 |
* 'user_name' => array( |
|
546 |
* 'sorting' => 'ascending' |
|
547 |
* ), |
|
548 |
* 'last_login' => array() |
|
549 |
* ) |
|
550 |
* ) |
|
551 |
* @return mixed MDB2_OK on success, a MDB2 error on failure |
|
552 |
* @access public |
|
553 |
*/ |
|
554 |
function createIndex($table, $name, $definition) |
|
555 |
{ |
|
556 |
$db =& $this->getDBInstance(); |
|
557 |
if (PEAR::isError($db)) { |
|
558 |
return $db; |
|
559 |
} |
|
560 |
|
|
561 |
if (array_key_exists('primary', $definition) && $definition['primary']) { |
|
562 |
$type = 'PRIMARY'; |
|
563 |
$name = 'KEY'; |
|
564 |
} elseif (array_key_exists('unique', $definition) && $definition['unique']) { |
|
565 |
$type = 'UNIQUE'; |
|
566 |
} else { |
|
567 |
$type = 'INDEX'; |
|
568 |
} |
|
569 |
|
|
570 |
$query = "ALTER TABLE $table ADD $type $name ("; |
|
571 |
$query.= implode(', ', array_keys($definition['fields'])); |
|
572 |
$query.= ')'; |
|
573 |
|
|
574 |
return $db->query($query); |
|
575 |
} |
|
576 |
|
|
577 |
// }}} |
|
578 |
// {{{ dropIndex() |
|
579 |
|
|
580 |
/** |
|
581 |
* drop existing index |
|
582 |
* |
|
583 |
* @param string $table name of table that should be used in method |
|
584 |
* @param string $name name of the index to be dropped |
|
585 |
* @return mixed MDB2_OK on success, a MDB2 error on failure |
|
586 |
* @access public |
|
587 |
*/ |
|
588 |
function dropIndex($table, $name) |
|
589 |
{ |
|
590 |
$db =& $this->getDBInstance(); |
|
591 |
if (PEAR::isError($db)) { |
|
592 |
return $db; |
|
593 |
} |
|
594 |
|
|
595 |
return $db->query("ALTER TABLE $table DROP INDEX $name"); |
|
596 |
} |
|
597 |
|
|
598 |
// }}} |
|
599 |
// {{{ listTableIndexes() |
|
600 |
|
|
601 |
/** |
|
602 |
* list all indexes in a table |
|
603 |
* |
|
604 |
* @param string $table name of table that should be used in method |
|
605 |
* @return mixed data array on success, a MDB2 error on failure |
|
606 |
* @access public |
|
607 |
*/ |
|
608 |
function listTableIndexes($table) |
|
609 |
{ |
|
610 |
$db =& $this->getDBInstance(); |
|
611 |
if (PEAR::isError($db)) { |
|
612 |
return $db; |
|
613 |
} |
|
614 |
|
|
615 |
$key_name = 'Key_name'; |
|
616 |
if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) { |
|
617 |
if ($db->options['field_case'] == CASE_LOWER) { |
|
618 |
$key_name = strtolower($key_name); |
|
619 |
} else { |
|
620 |
$key_name = strtoupper($key_name); |
|
621 |
} |
|
622 |
} |
|
623 |
|
|
624 |
$query = "SHOW INDEX FROM $table"; |
|
625 |
$indexes_all = $db->queryCol($query, 'text', $key_name); |
|
626 |
if (PEAR::isError($indexes_all)) { |
|
627 |
return $indexes_all; |
|
628 |
} |
|
629 |
|
|
630 |
$indexes = array_unique($indexes_all); |
|
631 |
|
|
632 |
if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) { |
|
633 |
$indexes = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $indexes); |
|
634 |
} |
|
635 |
|
|
636 |
return $indexes; |
|
637 |
} |
|
638 |
|
|
639 |
// }}} |
|
640 |
// {{{ createSequence() |
|
641 |
|
|
642 |
/** |
|
643 |
* create sequence |
|
644 |
* |
|
645 |
* @param string $seq_name name of the sequence to be created |
|
646 |
* @param string $start start value of the sequence; default is 1 |
|
647 |
* @return mixed MDB2_OK on success, a MDB2 error on failure |
|
648 |
* @access public |
|
649 |
*/ |
|
650 |
function createSequence($seq_name, $start = 1) |
|
651 |
{ |
|
652 |
$db =& $this->getDBInstance(); |
|
653 |
if (PEAR::isError($db)) { |
|
654 |
return $db; |
|
655 |
} |
|
656 |
|
|
657 |
$sequence_name = $db->getSequenceName($seq_name); |
|
658 |
$seqcol_name = $db->options['seqcol_name']; |
|
659 |
$result = $this->_verifyTableType($db->options['default_table_type']); |
|
660 |
if (PEAR::isError($result)) { |
|
661 |
return $result; |
|
662 |
} |
|
663 |
|
|
664 |
$res = $db->query("CREATE TABLE $sequence_name". |
|
665 |
"($seqcol_name INT NOT NULL AUTO_INCREMENT, PRIMARY KEY ($seqcol_name))". |
|
666 |
(strlen($db->options['default_table_type']) ? ' TYPE='.$db->options['default_table_type'] : '') |
|
667 |
); |
|
668 |
|
|
669 |
if (PEAR::isError($res)) { |
|
670 |
return $res; |
|
671 |
} |
|
672 |
|
|
673 |
if ($start == 1) { |
|
674 |
return MDB2_OK; |
|
675 |
} |
|
676 |
|
|
677 |
$res = $db->query("INSERT INTO $sequence_name ($seqcol_name) VALUES (".($start-1).')'); |
|
678 |
if (!PEAR::isError($res)) { |
|
679 |
return MDB2_OK; |
|
680 |
} |
|
681 |
|
|
682 |
// Handle error |
|
683 |
$result = $db->query("DROP TABLE $sequence_name"); |
|
684 |
if (PEAR::isError($result)) { |
|
685 |
return $db->raiseError(MDB2_ERROR, null, null, |
|
686 |
'createSequence: could not drop inconsistent sequence table ('. |
|
687 |
$result->getMessage().' ('.$result->getUserinfo().'))'); |
|
688 |
} |
|
689 |
|
|
690 |
return $db->raiseError(MDB2_ERROR, null, null, |
|
691 |
'createSequence: could not create sequence table ('. |
|
692 |
$res->getMessage().' ('.$res->getUserinfo().'))'); |
|
693 |
} |
|
694 |
|
|
695 |
// }}} |
|
696 |
// {{{ dropSequence() |
|
697 |
|
|
698 |
/** |
|
699 |
* drop existing sequence |
|
700 |
* |
|
701 |
* @param string $seq_name name of the sequence to be dropped |
|
702 |
* @return mixed MDB2_OK on success, a MDB2 error on failure |
|
703 |
* @access public |
|
704 |
*/ |
|
705 |
function dropSequence($seq_name) |
|
706 |
{ |
|
707 |
$db =& $this->getDBInstance(); |
|
708 |
if (PEAR::isError($db)) { |
|
709 |
return $db; |
|
710 |
} |
|
711 |
|
|
712 |
$sequence_name = $db->getSequenceName($seq_name); |
|
713 |
return $db->query("DROP TABLE $sequence_name"); |
|
714 |
} |
|
715 |
|
|
716 |
// }}} |
|
717 |
// {{{ listSequences() |
|
718 |
|
|
719 |
/** |
|
720 |
* list all sequences in the current database |
|
721 |
* |
|
722 |
* @return mixed data array on success, a MDB2 error on failure |
|
723 |
* @access public |
|
724 |
*/ |
|
725 |
function listSequences() |
|
726 |
{ |
|
727 |
$db =& $this->getDBInstance(); |
|
728 |
if (PEAR::isError($db)) { |
|
729 |
return $db; |
|
730 |
} |
|
731 |
|
|
732 |
$table_names = $db->queryCol('SHOW TABLES'); |
|
733 |
if (PEAR::isError($table_names)) { |
|
734 |
return $table_names; |
|
735 |
} |
|
736 |
|
|
737 |
$sequences = array(); |
|
738 |
for ($i = 0, $j = count($table_names); $i < $j; ++$i) { |
|
739 |
if ($sqn = $this->_isSequenceName($table_names[$i])) { |
|
740 |
$sequences[] = $sqn; |
|
741 |
} |
|
742 |
} |
|
743 |
|
|
744 |
return $sequences; |
|
745 |
} |
|
746 |
|
|
747 |
// }}} |
|
748 |
} |
|
749 |
?> |