| 1 |
lars |
1 |
<?php
|
|
|
2 |
// +----------------------------------------------------------------------+
|
|
|
3 |
// | PHP Version 4 |
|
|
|
4 |
// +----------------------------------------------------------------------+
|
|
|
5 |
// | Copyright (c) 1998-2004 Manuel Lemos, Tomas V.V.Cox, |
|
|
|
6 |
// | Stig. S. Bakken, Lukas Smith, Frank M. Kromann |
|
|
|
7 |
// | All rights reserved. |
|
|
|
8 |
// +----------------------------------------------------------------------+
|
|
|
9 |
// | MDB 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@dybnet.de> |
|
|
|
43 |
// +----------------------------------------------------------------------+
|
|
|
44 |
//
|
|
|
45 |
// $Id: fbsql.php,v 1.7.4.8 2004/04/08 17:19:01 lsmith Exp $
|
|
|
46 |
//
|
|
|
47 |
|
|
|
48 |
if(!defined('MDB_MANAGER_FBSQL_INCLUDED'))
|
|
|
49 |
{
|
|
|
50 |
define('MDB_MANAGER_FBSQL_INCLUDED', 1);
|
|
|
51 |
|
|
|
52 |
require_once('MDB/Modules/Manager/Common.php');
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
* MDB FrontBase driver for the management modules
|
|
|
56 |
*
|
|
|
57 |
* @package MDB
|
|
|
58 |
* @category Database
|
|
|
59 |
* @access private
|
|
|
60 |
* @author Lukas Smith <smith@dybnet.de>
|
|
|
61 |
* @author Frank M. Kromann <frank@kromann.info>
|
|
|
62 |
*/
|
|
|
63 |
class MDB_Manager_fbsql extends MDB_Manager_Common
|
|
|
64 |
{
|
|
|
65 |
// }}}
|
|
|
66 |
// {{{ createDatabase()
|
|
|
67 |
|
|
|
68 |
/**
|
|
|
69 |
* create a new database
|
|
|
70 |
*
|
|
|
71 |
* @param object $dbs database object that is extended by this class
|
|
|
72 |
* @param string $name name of the database that should be created
|
|
|
73 |
* @return mixed MDB_OK on success, a MDB error on failure
|
|
|
74 |
* @access public
|
|
|
75 |
*/
|
|
|
76 |
function createDatabase(&$db, $name)
|
|
|
77 |
{
|
|
|
78 |
if (MDB::isError($result = $db->connect())) {
|
|
|
79 |
return($result);
|
|
|
80 |
}
|
|
|
81 |
if (!@fbsql_create_db($name, $db->connection)) {
|
|
|
82 |
return($db->fbsqlRaiseError());
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
return(MDB_OK);
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
// }}}
|
|
|
89 |
// {{{ dropDatabase()
|
|
|
90 |
|
|
|
91 |
/**
|
|
|
92 |
* drop an existing database
|
|
|
93 |
*
|
|
|
94 |
* @param object $dbs database object that is extended by this class
|
|
|
95 |
* @param string $name name of the database that should be dropped
|
|
|
96 |
* @return mixed MDB_OK on success, a MDB error on failure
|
|
|
97 |
* @access public
|
|
|
98 |
*/
|
|
|
99 |
function dropDatabase(&$db, $name)
|
|
|
100 |
{
|
|
|
101 |
if (MDB::isError($result = $db->connect())) {
|
|
|
102 |
return($result);
|
|
|
103 |
}
|
|
|
104 |
if (!@fbsql_stop_db($name, $db->connection)) {
|
|
|
105 |
return($db->fbsqlRaiseError());
|
|
|
106 |
}
|
|
|
107 |
if (!@fbsql_drop_db($name, $db->connection)) {
|
|
|
108 |
return($db->fbsqlRaiseError());
|
|
|
109 |
}
|
|
|
110 |
return($db->disconnect());
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
// }}}
|
|
|
114 |
// {{{ createTable()
|
|
|
115 |
|
|
|
116 |
/**
|
|
|
117 |
* create a new table
|
|
|
118 |
*
|
|
|
119 |
* @param object $dbs database object that is extended by this class
|
|
|
120 |
* @param string $name Name of the database that should be created
|
|
|
121 |
* @param array $fields Associative array that contains the definition of each field of the new table
|
|
|
122 |
* The indexes of the array entries are the names of the fields of the table an
|
|
|
123 |
* the array entry values are associative arrays like those that are meant to be
|
|
|
124 |
* passed with the field definitions to get[Type]Declaration() functions.
|
|
|
125 |
*
|
|
|
126 |
* Example
|
|
|
127 |
* array(
|
|
|
128 |
*
|
|
|
129 |
* 'id' => array(
|
|
|
130 |
* 'type' => 'integer',
|
|
|
131 |
* 'unsigned' => 1
|
|
|
132 |
* 'notnull' => 1
|
|
|
133 |
* 'default' => 0
|
|
|
134 |
* ),
|
|
|
135 |
* 'name' => array(
|
|
|
136 |
* 'type' => 'text',
|
|
|
137 |
* 'length' => 12
|
|
|
138 |
* ),
|
|
|
139 |
* 'password' => array(
|
|
|
140 |
* 'type' => 'text',
|
|
|
141 |
* 'length' => 12
|
|
|
142 |
* )
|
|
|
143 |
* );
|
|
|
144 |
* @return mixed MDB_OK on success, a MDB error on failure
|
|
|
145 |
* @access public
|
|
|
146 |
*/
|
|
|
147 |
function createTable(&$db, $name, $fields)
|
|
|
148 |
{
|
|
|
149 |
if (!isset($name) || !strcmp($name, '')) {
|
|
|
150 |
return($db->raiseError(MDB_ERROR_CANNOT_CREATE, NULL, NULL, 'no valid table name specified'));
|
|
|
151 |
}
|
|
|
152 |
if (count($fields) == 0) {
|
|
|
153 |
return($db->raiseError(MDB_ERROR_CANNOT_CREATE, NULL, NULL, 'no fields specified for table "'.$name.'"'));
|
|
|
154 |
}
|
|
|
155 |
if (MDB::isError($query_fields = $db->getFieldDeclarationList($fields))) {
|
|
|
156 |
return($db->raiseError(MDB_ERROR_CANNOT_CREATE, NULL, NULL, 'unkown error'));
|
|
|
157 |
}
|
|
|
158 |
$query = "CREATE TABLE $name ($query_fields)";
|
|
|
159 |
|
|
|
160 |
return($db->query($query));
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
// }}}
|
|
|
164 |
// {{{ dropTable()
|
|
|
165 |
|
|
|
166 |
/**
|
|
|
167 |
* drop an existing table
|
|
|
168 |
*
|
|
|
169 |
* @param object $dbs database object that is extended by this class
|
|
|
170 |
* @param string $name name of the table that should be dropped
|
|
|
171 |
* @return mixed MDB_OK on success, a MDB error on failure
|
|
|
172 |
* @access public
|
|
|
173 |
*/
|
|
|
174 |
function dropTable(&$db, $name)
|
|
|
175 |
{
|
|
|
176 |
return($db->query("DROP TABLE $name CASCADE"));
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
// }}}
|
|
|
180 |
// {{{ alterTable()
|
|
|
181 |
|
|
|
182 |
/**
|
|
|
183 |
* alter an existing table
|
|
|
184 |
*
|
|
|
185 |
* @param object $dbs database object that is extended by this class
|
|
|
186 |
* @param string $name name of the table that is intended to be changed.
|
|
|
187 |
* @param array $changes associative array that contains the details of each type
|
|
|
188 |
* of change that is intended to be performed. The types of
|
|
|
189 |
* changes that are currently supported are defined as follows:
|
|
|
190 |
*
|
|
|
191 |
* name
|
|
|
192 |
*
|
|
|
193 |
* New name for the table.
|
|
|
194 |
*
|
|
|
195 |
* AddedFields
|
|
|
196 |
*
|
|
|
197 |
* Associative array with the names of fields to be added as
|
|
|
198 |
* indexes of the array. The value of each entry of the array
|
|
|
199 |
* should be set to another associative array with the properties
|
|
|
200 |
* of the fields to be added. The properties of the fields should
|
|
|
201 |
* be the same as defined by the Metabase parser.
|
|
|
202 |
*
|
|
|
203 |
* Additionally, there should be an entry named Declaration that
|
|
|
204 |
* is expected to contain the portion of the field declaration already
|
|
|
205 |
* in DBMS specific SQL code as it is used in the CREATE TABLE statement.
|
|
|
206 |
*
|
|
|
207 |
* RemovedFields
|
|
|
208 |
*
|
|
|
209 |
* Associative array with the names of fields to be removed as indexes
|
|
|
210 |
* of the array. Currently the values assigned to each entry are ignored.
|
|
|
211 |
* An empty array should be used for future compatibility.
|
|
|
212 |
*
|
|
|
213 |
* RenamedFields
|
|
|
214 |
*
|
|
|
215 |
* Associative array with the names of fields to be renamed as indexes
|
|
|
216 |
* of the array. The value of each entry of the array should be set to
|
|
|
217 |
* another associative array with the entry named name with the new
|
|
|
218 |
* field name and the entry named Declaration that is expected to contain
|
|
|
219 |
* the portion of the field declaration already in DBMS specific SQL code
|
|
|
220 |
* as it is used in the CREATE TABLE statement.
|
|
|
221 |
*
|
|
|
222 |
* ChangedFields
|
|
|
223 |
*
|
|
|
224 |
* Associative array with the names of the fields to be changed as indexes
|
|
|
225 |
* of the array. Keep in mind that if it is intended to change either the
|
|
|
226 |
* name of a field and any other properties, the ChangedFields array entries
|
|
|
227 |
* should have the new names of the fields as array indexes.
|
|
|
228 |
*
|
|
|
229 |
* The value of each entry of the array should be set to another associative
|
|
|
230 |
* array with the properties of the fields to that are meant to be changed as
|
|
|
231 |
* array entries. These entries should be assigned to the new values of the
|
|
|
232 |
* respective properties. The properties of the fields should be the same
|
|
|
233 |
* as defined by the Metabase parser.
|
|
|
234 |
*
|
|
|
235 |
* If the default property is meant to be added, removed or changed, there
|
|
|
236 |
* should also be an entry with index ChangedDefault assigned to 1. Similarly,
|
|
|
237 |
* if the notnull constraint is to be added or removed, there should also be
|
|
|
238 |
* an entry with index ChangedNotNull assigned to 1.
|
|
|
239 |
*
|
|
|
240 |
* Additionally, there should be an entry named Declaration that is expected
|
|
|
241 |
* to contain the portion of the field changed declaration already in DBMS
|
|
|
242 |
* specific SQL code as it is used in the CREATE TABLE statement.
|
|
|
243 |
* Example
|
|
|
244 |
* array(
|
|
|
245 |
* 'name' => 'userlist',
|
|
|
246 |
* 'AddedFields' => array(
|
|
|
247 |
* 'quota' => array(
|
|
|
248 |
* 'type' => 'integer',
|
|
|
249 |
* 'unsigned' => 1
|
|
|
250 |
* 'Declaration' => 'quota INT'
|
|
|
251 |
* )
|
|
|
252 |
* ),
|
|
|
253 |
* 'RemovedFields' => array(
|
|
|
254 |
* 'file_limit' => array(),
|
|
|
255 |
* 'time_limit' => array()
|
|
|
256 |
* ),
|
|
|
257 |
* 'ChangedFields' => array(
|
|
|
258 |
* 'gender' => array(
|
|
|
259 |
* 'default' => 'M',
|
|
|
260 |
* 'ChangeDefault' => 1,
|
|
|
261 |
* 'Declaration' => "gender CHAR(1) DEFAULT 'M'"
|
|
|
262 |
* )
|
|
|
263 |
* ),
|
|
|
264 |
* 'RenamedFields' => array(
|
|
|
265 |
* 'sex' => array(
|
|
|
266 |
* 'name' => 'gender',
|
|
|
267 |
* 'Declaration' => "gender CHAR(1) DEFAULT 'M'"
|
|
|
268 |
* )
|
|
|
269 |
* )
|
|
|
270 |
* )
|
|
|
271 |
*
|
|
|
272 |
* @param boolean $check indicates whether the function should just check if the DBMS driver
|
|
|
273 |
* can perform the requested table alterations if the value is true or
|
|
|
274 |
* actually perform them otherwise.
|
|
|
275 |
* @access public
|
|
|
276 |
*
|
|
|
277 |
* @return mixed MDB_OK on success, a MDB error on failure
|
|
|
278 |
*/
|
|
|
279 |
function alterTable(&$db, $name, $changes, $check)
|
|
|
280 |
{
|
|
|
281 |
if ($check) {
|
|
|
282 |
for($change = 0,reset($changes);
|
|
|
283 |
$change < count($changes);
|
|
|
284 |
next($changes), $change++)
|
|
|
285 |
{
|
|
|
286 |
switch(key($changes)) {
|
|
|
287 |
case 'AddedFields':
|
|
|
288 |
case 'RemovedFields':
|
|
|
289 |
case 'ChangedFields':
|
|
|
290 |
case 'RenamedFields':
|
|
|
291 |
case 'name':
|
|
|
292 |
break;
|
|
|
293 |
default:
|
|
|
294 |
return($db->raiseError(MDB_ERROR_CANNOT_ALTER, NULL, NULL,
|
|
|
295 |
'Alter table: change type "'.Key($changes).'" not yet supported'));
|
|
|
296 |
}
|
|
|
297 |
}
|
|
|
298 |
return(MDB_OK);
|
|
|
299 |
} else {
|
|
|
300 |
$query = (isset($changes['name']) ? 'RENAME AS '.$changes['name'] : '');
|
|
|
301 |
if (isset($changes['AddedFields'])) {
|
|
|
302 |
$fields = $changes['AddedFields'];
|
|
|
303 |
for($field = 0, reset($fields);
|
|
|
304 |
$field<count($fields);
|
|
|
305 |
next($fields), $field++)
|
|
|
306 |
{
|
|
|
307 |
if (strcmp($query, '')) {
|
|
|
308 |
$query .= ',';
|
|
|
309 |
}
|
|
|
310 |
$query .= 'ADD '.$fields[key($fields)]['Declaration'];
|
|
|
311 |
}
|
|
|
312 |
}
|
|
|
313 |
if (isset($changes['RemovedFields'])) {
|
|
|
314 |
$fields = $changes['RemovedFields'];
|
|
|
315 |
for($field = 0,reset($fields);
|
|
|
316 |
$field<count($fields);
|
|
|
317 |
next($fields), $field++)
|
|
|
318 |
{
|
|
|
319 |
if (strcmp($query, '')) {
|
|
|
320 |
$query .= ',';
|
|
|
321 |
}
|
|
|
322 |
$query .= 'DROP '.key($fields);
|
|
|
323 |
}
|
|
|
324 |
}
|
|
|
325 |
$renamed_fields = array();
|
|
|
326 |
if (isset($changes['RenamedFields'])) {
|
|
|
327 |
$fields = $changes['RenamedFields'];
|
|
|
328 |
for($field = 0,reset($fields);
|
|
|
329 |
$field<count($fields);
|
|
|
330 |
next($fields), $field++)
|
|
|
331 |
{
|
|
|
332 |
$renamed_fields[$fields[key($fields)]['name']] = key($fields);
|
|
|
333 |
}
|
|
|
334 |
}
|
|
|
335 |
if (isset($changes['ChangedFields'])) {
|
|
|
336 |
$fields = $changes['ChangedFields'];
|
|
|
337 |
for($field = 0,reset($fields);
|
|
|
338 |
$field<count($fields);
|
|
|
339 |
next($fields), $field++)
|
|
|
340 |
{
|
|
|
341 |
if (strcmp($query, '')) {
|
|
|
342 |
$query .= ',';
|
|
|
343 |
}
|
|
|
344 |
if (isset($renamed_fields[key($fields)])) {
|
|
|
345 |
$field_name = $renamed_fields[key($fields)];
|
|
|
346 |
unset($renamed_fields[key($fields)]);
|
|
|
347 |
} else {
|
|
|
348 |
$field_name = key($fields);
|
|
|
349 |
}
|
|
|
350 |
$query .= "CHANGE $field_name ".$fields[key($fields)]['Declaration'];
|
|
|
351 |
}
|
|
|
352 |
}
|
|
|
353 |
if (count($renamed_fields))
|
|
|
354 |
{
|
|
|
355 |
for($field = 0,reset($renamed_fields);
|
|
|
356 |
$field<count($renamed_fields);
|
|
|
357 |
next($renamed_fields), $field++)
|
|
|
358 |
{
|
|
|
359 |
if (strcmp($query, '')) {
|
|
|
360 |
$query .= ',';
|
|
|
361 |
}
|
|
|
362 |
$old_field_name = $renamed_fields[Key($renamed_fields)];
|
|
|
363 |
$query .= "CHANGE $old_field_name ".$changes['RenamedFields'][$old_field_name]['Declaration'];
|
|
|
364 |
}
|
|
|
365 |
}
|
|
|
366 |
return($db->query("ALTER TABLE $name $query"));
|
|
|
367 |
}
|
|
|
368 |
}
|
|
|
369 |
|
|
|
370 |
// }}}
|
|
|
371 |
// {{{ listDatabases()
|
|
|
372 |
|
|
|
373 |
/**
|
|
|
374 |
* list all databases
|
|
|
375 |
*
|
|
|
376 |
* @param object $dbs database object that is extended by this class
|
|
|
377 |
* @return mixed data array on success, a MDB error on failure
|
|
|
378 |
* @access public
|
|
|
379 |
*/
|
|
|
380 |
function listDatabases(&$db)
|
|
|
381 |
{
|
|
|
382 |
$result = $db->queryCol('SHOW DATABASES');
|
|
|
383 |
if(MDB::isError($result)) {
|
|
|
384 |
return($result);
|
|
|
385 |
}
|
|
|
386 |
return($result);
|
|
|
387 |
}
|
|
|
388 |
|
|
|
389 |
// }}}
|
|
|
390 |
// {{{ listUsers()
|
|
|
391 |
|
|
|
392 |
/**
|
|
|
393 |
* list all users
|
|
|
394 |
*
|
|
|
395 |
* @param object $dbs database object that is extended by this class
|
|
|
396 |
* @return mixed data array on success, a MDB error on failure
|
|
|
397 |
* @access public
|
|
|
398 |
*/
|
|
|
399 |
function listUsers(&$db)
|
|
|
400 |
{
|
|
|
401 |
$result = $db->queryCol('SELECT DISTINCT USER FROM USER');
|
|
|
402 |
if(MDB::isError($result)) {
|
|
|
403 |
return($result);
|
|
|
404 |
}
|
|
|
405 |
return($result);
|
|
|
406 |
}
|
|
|
407 |
|
|
|
408 |
// }}}
|
|
|
409 |
// {{{ listTables()
|
|
|
410 |
|
|
|
411 |
/**
|
|
|
412 |
* list all tables in the current database
|
|
|
413 |
*
|
|
|
414 |
* @param object $dbs database object that is extended by this class
|
|
|
415 |
* @return mixed data array on success, a MDB error on failure
|
|
|
416 |
* @access public
|
|
|
417 |
*/
|
|
|
418 |
function listTables(&$db)
|
|
|
419 |
{
|
|
|
420 |
$table_names = $db->queryCol('SHOW TABLES');
|
|
|
421 |
if(MDB::isError($table_names)) {
|
|
|
422 |
return($table_names);
|
|
|
423 |
}
|
|
|
424 |
for($i = 0, $j = count($table_names), $tables = array(); $i < $j; ++$i)
|
|
|
425 |
{
|
|
|
426 |
if (!$this->_isSequenceName($db, $table_names[$i]))
|
|
|
427 |
$tables[] = $table_names[$i];
|
|
|
428 |
}
|
|
|
429 |
return($tables);
|
|
|
430 |
}
|
|
|
431 |
|
|
|
432 |
// }}}
|
|
|
433 |
// {{{ listTableFields()
|
|
|
434 |
|
|
|
435 |
/**
|
|
|
436 |
* list all fields in a tables in the current database
|
|
|
437 |
*
|
|
|
438 |
* @param object $dbs database object that is extended by this class
|
|
|
439 |
* @param string $table name of table that should be used in method
|
|
|
440 |
* @return mixed data array on success, a MDB error on failure
|
|
|
441 |
* @access public
|
|
|
442 |
*/
|
|
|
443 |
function listTableFields(&$db, $table)
|
|
|
444 |
{
|
|
|
445 |
$result = $db->query("SHOW COLUMNS FROM $table");
|
|
|
446 |
if(MDB::isError($result)) {
|
|
|
447 |
return($result);
|
|
|
448 |
}
|
|
|
449 |
$columns = $db->getColumnNames($result);
|
|
|
450 |
if(MDB::isError($columns)) {
|
|
|
451 |
$db->freeResult($columns);
|
|
|
452 |
return($columns);
|
|
|
453 |
}
|
|
|
454 |
if(!isset($columns['field'])) {
|
|
|
455 |
$db->freeResult($result);
|
|
|
456 |
return($db->raiseError(MDB_ERROR_MANAGER, NULL, NULL,
|
|
|
457 |
'List table fields: show columns does not return the table field names'));
|
|
|
458 |
}
|
|
|
459 |
$field_column = $columns['field'];
|
|
|
460 |
for($fields = array(), $field = 0; !$db->endOfResult($result); ++$field) {
|
|
|
461 |
$field_name = $db->fetch($result, $field, $field_column);
|
|
|
462 |
if ($field_name != $db->dummy_primary_key)
|
|
|
463 |
$fields[] = $field_name;
|
|
|
464 |
}
|
|
|
465 |
$db->freeResult($result);
|
|
|
466 |
return($fields);
|
|
|
467 |
}
|
|
|
468 |
|
|
|
469 |
// }}}
|
|
|
470 |
// {{{ getTableFieldDefinition()
|
|
|
471 |
|
|
|
472 |
/**
|
|
|
473 |
* get the stucture of a field into an array
|
|
|
474 |
*
|
|
|
475 |
* @param object $dbs database object that is extended by this class
|
|
|
476 |
* @param string $table name of table that should be used in method
|
|
|
477 |
* @param string $field_name name of field that should be used in method
|
|
|
478 |
* @return mixed data array on success, a MDB error on failure
|
|
|
479 |
* @access public
|
|
|
480 |
*/
|
|
|
481 |
function getTableFieldDefinition(&$db, $table, $field_name)
|
|
|
482 |
{
|
|
|
483 |
if ($field_name == $db->dummy_primary_key) {
|
|
|
484 |
return($db->raiseError(MDB_ERROR_MANAGER, NULL, NULL,
|
|
|
485 |
'Get table field definiton: '.$db->dummy_primary_key.' is an hidden column'));
|
|
|
486 |
}
|
|
|
487 |
$result = $db->query("SHOW COLUMNS FROM $table");
|
|
|
488 |
if(MDB::isError($result)) {
|
|
|
489 |
return($result);
|
|
|
490 |
}
|
|
|
491 |
$columns = $db->getColumnNames($result);
|
|
|
492 |
if(MDB::isError($columns)) {
|
|
|
493 |
$db->freeResult($columns);
|
|
|
494 |
return($columns);
|
|
|
495 |
}
|
|
|
496 |
if (!isset($columns[$column = 'field'])
|
|
|
497 |
|| !isset($columns[$column = 'type']))
|
|
|
498 |
{
|
|
|
499 |
$db->freeResult($result);
|
|
|
500 |
return($db->raiseError(MDB_ERROR_MANAGER, NULL, NULL,
|
|
|
501 |
'Get table field definition: show columns does not return the column '.$column));
|
|
|
502 |
}
|
|
|
503 |
$field_column = $columns['field'];
|
|
|
504 |
$type_column = $columns['type'];
|
|
|
505 |
while (is_array($row = $db->fetchInto($result))) {
|
|
|
506 |
if ($field_name == $row[$field_column]) {
|
|
|
507 |
$db_type = strtolower($row[$type_column]);
|
|
|
508 |
$db_type = strtok($db_type, '(), ');
|
|
|
509 |
if ($db_type == 'national') {
|
|
|
510 |
$db_type = strtok('(), ');
|
|
|
511 |
}
|
|
|
512 |
$length = strtok('(), ');
|
|
|
513 |
$decimal = strtok('(), ');
|
|
|
514 |
$type = array();
|
|
|
515 |
switch($db_type) {
|
|
|
516 |
case 'tinyint':
|
|
|
517 |
case 'smallint':
|
|
|
518 |
case 'mediumint':
|
|
|
519 |
case 'int':
|
|
|
520 |
case 'integer':
|
|
|
521 |
case 'bigint':
|
|
|
522 |
$type[0] = 'integer';
|
|
|
523 |
if($length == '1') {
|
|
|
524 |
$type[1] = 'boolean';
|
|
|
525 |
if (preg_match('/^[is|has]/', $field_name)) {
|
|
|
526 |
$type = array_reverse($type);
|
|
|
527 |
}
|
|
|
528 |
}
|
|
|
529 |
break;
|
|
|
530 |
case 'tinytext':
|
|
|
531 |
case 'mediumtext':
|
|
|
532 |
case 'longtext':
|
|
|
533 |
case 'text':
|
|
|
534 |
case 'char':
|
|
|
535 |
case 'varchar':
|
|
|
536 |
$type[0] = 'text';
|
|
|
537 |
if($decimal == 'binary') {
|
|
|
538 |
$type[1] = 'blob';
|
|
|
539 |
} elseif($length == '1') {
|
|
|
540 |
$type[1] = 'boolean';
|
|
|
541 |
if (preg_match('/[is|has]/', $field_name)) {
|
|
|
542 |
$type = array_reverse($type);
|
|
|
543 |
}
|
|
|
544 |
} elseif(strstr($db_type, 'text'))
|
|
|
545 |
$type[1] = 'clob';
|
|
|
546 |
break;
|
|
|
547 |
case 'enum':
|
|
|
548 |
preg_match_all('/\'.+\'/U',$row[$type_column], $matches);
|
|
|
549 |
$length = 0;
|
|
|
550 |
if(is_array($matches)) {
|
|
|
551 |
foreach($matches[0] as $value) {
|
|
|
552 |
$length = max($length, strlen($value)-2);
|
|
|
553 |
}
|
|
|
554 |
}
|
|
|
555 |
unset($decimal);
|
|
|
556 |
case 'set':
|
|
|
557 |
$type[0] = 'text';
|
|
|
558 |
$type[1] = 'integer';
|
|
|
559 |
break;
|
|
|
560 |
case 'date':
|
|
|
561 |
$type[0] = 'date';
|
|
|
562 |
break;
|
|
|
563 |
case 'datetime':
|
|
|
564 |
case 'timestamp':
|
|
|
565 |
$type[0] = 'timestamp';
|
|
|
566 |
break;
|
|
|
567 |
case 'time':
|
|
|
568 |
$type[0] = 'time';
|
|
|
569 |
break;
|
|
|
570 |
case 'float':
|
|
|
571 |
case 'double':
|
|
|
572 |
case 'real':
|
|
|
573 |
$type[0] = 'float';
|
|
|
574 |
break;
|
|
|
575 |
case 'decimal':
|
|
|
576 |
case 'numeric':
|
|
|
577 |
$type[0] = 'decimal';
|
|
|
578 |
break;
|
|
|
579 |
case 'tinyblob':
|
|
|
580 |
case 'mediumblob':
|
|
|
581 |
case 'longblob':
|
|
|
582 |
case 'blob':
|
|
|
583 |
$type[0] = 'blob';
|
|
|
584 |
$type[1] = 'text';
|
|
|
585 |
break;
|
|
|
586 |
case 'year':
|
|
|
587 |
$type[0] = 'integer';
|
|
|
588 |
$type[1] = 'date';
|
|
|
589 |
break;
|
|
|
590 |
default:
|
|
|
591 |
return($db->raiseError(MDB_ERROR_MANAGER, NULL, NULL,
|
|
|
592 |
'List table fields: unknown database attribute type'));
|
|
|
593 |
}
|
|
|
594 |
unset($notnull);
|
|
|
595 |
if (isset($columns['null'])
|
|
|
596 |
&& $row[$columns['null']] != 'YES')
|
|
|
597 |
{
|
|
|
598 |
$notnull = 1;
|
|
|
599 |
}
|
|
|
600 |
unset($default);
|
|
|
601 |
if (isset($columns['default'])
|
|
|
602 |
&& isset($row[$columns['default']]))
|
|
|
603 |
{
|
|
|
604 |
$default = $row[$columns['default']];
|
|
|
605 |
}
|
|
|
606 |
$definition = array();
|
|
|
607 |
for($field_choices = array(), $datatype = 0; $datatype < count($type); $datatype++) {
|
|
|
608 |
$field_choices[$datatype] = array('type' => $type[$datatype]);
|
|
|
609 |
if(isset($notnull)) {
|
|
|
610 |
$field_choices[$datatype]['notnull'] = 1;
|
|
|
611 |
}
|
|
|
612 |
if(isset($default)) {
|
|
|
613 |
$field_choices[$datatype]['default'] = $default;
|
|
|
614 |
}
|
|
|
615 |
if($type[$datatype] != 'boolean'
|
|
|
616 |
&& $type[$datatype] != 'time'
|
|
|
617 |
&& $type[$datatype] != 'date'
|
|
|
618 |
&& $type[$datatype] != 'timestamp')
|
|
|
619 |
{
|
|
|
620 |
if(strlen($length)) {
|
|
|
621 |
$field_choices[$datatype]['length'] = $length;
|
|
|
622 |
}
|
|
|
623 |
}
|
|
|
624 |
}
|
|
|
625 |
$definition[0] = $field_choices;
|
|
|
626 |
if (isset($columns['extra'])
|
|
|
627 |
&& isset($row[$columns['extra']])
|
|
|
628 |
&& $row[$columns['extra']] == 'auto_increment')
|
|
|
629 |
{
|
|
|
630 |
$implicit_sequence = array();
|
|
|
631 |
$implicit_sequence['on'] = array();
|
|
|
632 |
$implicit_sequence['on']['table'] = $table;
|
|
|
633 |
$implicit_sequence['on']['field'] = $field_name;
|
|
|
634 |
$definition[1]['name'] = $table.'_'.$field_name;
|
|
|
635 |
$definition[1]['definition'] = $implicit_sequence;
|
|
|
636 |
}
|
|
|
637 |
if (isset($columns['key'])
|
|
|
638 |
&& isset($row[$columns['key']])
|
|
|
639 |
&& $row[$columns['key']] == 'PRI')
|
|
|
640 |
{
|
|
|
641 |
// check that its not just a unique field
|
|
|
642 |
if(MDB::isError($indexes = $db->queryAll("SHOW INDEX FROM $table", NULL, MDB_FETCHMODE_ASSOC))) {
|
|
|
643 |
return($indexes);
|
|
|
644 |
}
|
|
|
645 |
$is_primary = FALSE;
|
|
|
646 |
foreach($indexes as $index) {
|
|
|
647 |
if ($index['Key_name'] == 'PRIMARY' && $index['Column_name'] == $field_name) {
|
|
|
648 |
$is_primary = TRUE;
|
|
|
649 |
break;
|
|
|
650 |
}
|
|
|
651 |
}
|
|
|
652 |
if($is_primary) {
|
|
|
653 |
$implicit_index = array();
|
|
|
654 |
$implicit_index['unique'] = 1;
|
|
|
655 |
$implicit_index['FIELDS'][$field_name] = '';
|
|
|
656 |
$definition[2]['name'] = $field_name;
|
|
|
657 |
$definition[2]['definition'] = $implicit_index;
|
|
|
658 |
}
|
|
|
659 |
}
|
|
|
660 |
$db->freeResult($result);
|
|
|
661 |
return($definition);
|
|
|
662 |
}
|
|
|
663 |
}
|
|
|
664 |
if(!$db->options['autofree']) {
|
|
|
665 |
$db->freeResult($result);
|
|
|
666 |
}
|
|
|
667 |
if(MDB::isError($row)) {
|
|
|
668 |
return($row);
|
|
|
669 |
}
|
|
|
670 |
return($db->raiseError(MDB_ERROR_MANAGER, NULL, NULL,
|
|
|
671 |
'Get table field definition: it was not specified an existing table column'));
|
|
|
672 |
}
|
|
|
673 |
|
|
|
674 |
// }}}
|
|
|
675 |
// {{{ createIndex()
|
|
|
676 |
|
|
|
677 |
/**
|
|
|
678 |
* get the stucture of a field into an array
|
|
|
679 |
*
|
|
|
680 |
* @param object $dbs database object that is extended by this class
|
|
|
681 |
* @param string $table name of the table on which the index is to be created
|
|
|
682 |
* @param string $name name of the index to be created
|
|
|
683 |
* @param array $definition associative array that defines properties of the index to be created.
|
|
|
684 |
* Currently, only one property named FIELDS is supported. This property
|
|
|
685 |
* is also an associative with the names of the index fields as array
|
|
|
686 |
* indexes. Each entry of this array is set to another type of associative
|
|
|
687 |
* array that specifies properties of the index that are specific to
|
|
|
688 |
* each field.
|
|
|
689 |
*
|
|
|
690 |
* Currently, only the sorting property is supported. It should be used
|
|
|
691 |
* to define the sorting direction of the index. It may be set to either
|
|
|
692 |
* ascending or descending.
|
|
|
693 |
*
|
|
|
694 |
* Not all DBMS support index sorting direction configuration. The DBMS
|
|
|
695 |
* drivers of those that do not support it ignore this property. Use the
|
|
|
696 |
* function support() to determine whether the DBMS driver can manage indexes.
|
|
|
697 |
|
|
|
698 |
* Example
|
|
|
699 |
* array(
|
|
|
700 |
* 'FIELDS' => array(
|
|
|
701 |
* 'user_name' => array(
|
|
|
702 |
* 'sorting' => 'ascending'
|
|
|
703 |
* ),
|
|
|
704 |
* 'last_login' => array()
|
|
|
705 |
* )
|
|
|
706 |
* )
|
|
|
707 |
* @return mixed MDB_OK on success, a MDB error on failure
|
|
|
708 |
* @access public
|
|
|
709 |
*/
|
|
|
710 |
function createIndex(&$db, $table, $name, $definition)
|
|
|
711 |
{
|
|
|
712 |
$query = "CREATE ".(isset($definition['unique']) ? 'UNIQUE INDEX' : 'INDEX')." $name on $table (";
|
|
|
713 |
for($field = 0, reset($definition['FIELDS']);
|
|
|
714 |
$field < count($definition['FIELDS']);
|
|
|
715 |
$field++, next($definition['FIELDS']))
|
|
|
716 |
{
|
|
|
717 |
if ($field > 0) {
|
|
|
718 |
$query .= ',';
|
|
|
719 |
}
|
|
|
720 |
$query .= key($definition['FIELDS']);
|
|
|
721 |
}
|
|
|
722 |
$query .= ')';
|
|
|
723 |
return($db->query($query));
|
|
|
724 |
}
|
|
|
725 |
|
|
|
726 |
// }}}
|
|
|
727 |
// {{{ dropIndex()
|
|
|
728 |
|
|
|
729 |
/**
|
|
|
730 |
* drop existing index
|
|
|
731 |
*
|
|
|
732 |
* @param object $dbs database object that is extended by this class
|
|
|
733 |
* @param string $table name of table that should be used in method
|
|
|
734 |
* @param string $name name of the index to be dropped
|
|
|
735 |
* @return mixed MDB_OK on success, a MDB error on failure
|
|
|
736 |
* @access public
|
|
|
737 |
*/
|
|
|
738 |
function dropIndex(&$db, $table, $name)
|
|
|
739 |
{
|
|
|
740 |
return($db->query("ALTER TABLE $table DROP INDEX $name"));
|
|
|
741 |
}
|
|
|
742 |
|
|
|
743 |
// }}}
|
|
|
744 |
// {{{ listTableIndexes()
|
|
|
745 |
|
|
|
746 |
/**
|
|
|
747 |
* list all indexes in a table
|
|
|
748 |
*
|
|
|
749 |
* @param object $dbs database object that is extended by this class
|
|
|
750 |
* @param string $table name of table that should be used in method
|
|
|
751 |
* @return mixed data array on success, a MDB error on failure
|
|
|
752 |
* @access public
|
|
|
753 |
*/
|
|
|
754 |
function listTableIndexes(&$db, $table)
|
|
|
755 |
{
|
|
|
756 |
if(MDB::isError($result = $db->query("SHOW INDEX FROM $table"))) {
|
|
|
757 |
return($result);
|
|
|
758 |
}
|
|
|
759 |
$indexes_all = $db->fetchCol($result, 'Key_name');
|
|
|
760 |
for($found = $indexes = array(), $index = 0, $indexes_all_cnt = count($indexes_all);
|
|
|
761 |
$index < $indexes_all_cnt;
|
|
|
762 |
$index++)
|
|
|
763 |
{
|
|
|
764 |
if ($indexes_all[$index] != 'PRIMARY'
|
|
|
765 |
&& !isset($found[$indexes_all[$index]]))
|
|
|
766 |
{
|
|
|
767 |
$indexes[] = $indexes_all[$index];
|
|
|
768 |
$found[$indexes_all[$index]] = 1;
|
|
|
769 |
}
|
|
|
770 |
}
|
|
|
771 |
return($indexes);
|
|
|
772 |
}
|
|
|
773 |
|
|
|
774 |
// }}}
|
|
|
775 |
// {{{ getTableIndexDefinition()
|
|
|
776 |
|
|
|
777 |
/**
|
|
|
778 |
* get the stucture of an index into an array
|
|
|
779 |
*
|
|
|
780 |
* @param object $dbs database object that is extended by this class
|
|
|
781 |
* @param string $table name of table that should be used in method
|
|
|
782 |
* @param string $index_name name of index that should be used in method
|
|
|
783 |
* @return mixed data array on success, a MDB error on failure
|
|
|
784 |
* @access public
|
|
|
785 |
*/
|
|
|
786 |
function getTableIndexDefinition(&$db, $table, $index_name)
|
|
|
787 |
{
|
|
|
788 |
if($index_name == 'PRIMARY') {
|
|
|
789 |
return($db->raiseError(MDB_ERROR_MANAGER, NULL, NULL, 'Get table index definition: PRIMARY is an hidden index'));
|
|
|
790 |
}
|
|
|
791 |
if(MDB::isError($result = $db->query("SHOW INDEX FROM $table"))) {
|
|
|
792 |
return($result);
|
|
|
793 |
}
|
|
|
794 |
$definition = array();
|
|
|
795 |
while (is_array($row = $db->fetchInto($result, MDB_FETCHMODE_ASSOC))) {
|
|
|
796 |
$key_name = $row['Key_name'];
|
|
|
797 |
if(!strcmp($index_name, $key_name)) {
|
|
|
798 |
if(!$row['Non_unique']) {
|
|
|
799 |
$definition[$index_name]['unique'] = 1;
|
|
|
800 |
}
|
|
|
801 |
$column_name = $row['Column_name'];
|
|
|
802 |
$definition['FIELDS'][$column_name] = array();
|
|
|
803 |
if(isset($row['Collation'])) {
|
|
|
804 |
$definition['FIELDS'][$column_name]['sorting'] = ($row['Collation'] == 'A' ? 'ascending' : 'descending');
|
|
|
805 |
}
|
|
|
806 |
}
|
|
|
807 |
}
|
|
|
808 |
$db->freeResult($result);
|
|
|
809 |
if (!isset($definition['FIELDS'])) {
|
|
|
810 |
return($db->raiseError(MDB_ERROR_MANAGER, NULL, NULL, 'Get table index definition: it was not specified an existing table index'));
|
|
|
811 |
}
|
|
|
812 |
return($definition);
|
|
|
813 |
}
|
|
|
814 |
|
|
|
815 |
// }}}
|
|
|
816 |
// {{{ createSequence()
|
|
|
817 |
|
|
|
818 |
/**
|
|
|
819 |
* create sequence
|
|
|
820 |
*
|
|
|
821 |
* @param object $dbs database object that is extended by this class
|
|
|
822 |
* @param string $seq_name name of the sequence to be created
|
|
|
823 |
* @param string $start start value of the sequence; default is 1
|
|
|
824 |
* @return mixed MDB_OK on success, a MDB error on failure
|
|
|
825 |
* @access public
|
|
|
826 |
*/
|
|
|
827 |
function createSequence(&$db, $seq_name, $start)
|
|
|
828 |
{
|
|
|
829 |
$sequence_name = $db->getSequenceName($seq_name);
|
|
|
830 |
$res = $db->query("CREATE TABLE $sequence_name
|
|
|
831 |
(".$db->options['sequence_col_name']." INTEGER DEFAULT UNIQUE, PRIMARY KEY(".$db->options['sequence_col_name']."))");
|
|
|
832 |
$res = $db->query("set unique = 1 for $sequence_name");
|
|
|
833 |
if (MDB::isError($res)) {
|
|
|
834 |
return($res);
|
|
|
835 |
}
|
|
|
836 |
if ($start == 1) {
|
|
|
837 |
return(MDB_OK);
|
|
|
838 |
}
|
|
|
839 |
$res = $db->query("INSERT INTO $sequence_name VALUES (".($start-1).')');
|
|
|
840 |
if (!MDB::isError($res)) {
|
|
|
841 |
return(MDB_OK);
|
|
|
842 |
}
|
|
|
843 |
// Handle error
|
|
|
844 |
$result = $db->query("DROP TABLE $sequence_name");
|
|
|
845 |
if (MDB::isError($result)) {
|
|
|
846 |
return($db->raiseError(MDB_ERROR, NULL, NULL,
|
|
|
847 |
'Create sequence: could not drop inconsistent sequence table ('.
|
|
|
848 |
$result->getMessage().' ('.$result->getUserinfo().'))'));
|
|
|
849 |
}
|
|
|
850 |
return($db->raiseError(MDB_ERROR, NULL, NULL,
|
|
|
851 |
'Create sequence: could not create sequence table ('.
|
|
|
852 |
$res->getMessage().' ('.$res->getUserinfo().'))'));
|
|
|
853 |
}
|
|
|
854 |
|
|
|
855 |
// }}}
|
|
|
856 |
// {{{ dropSequence()
|
|
|
857 |
|
|
|
858 |
/**
|
|
|
859 |
* drop existing sequence
|
|
|
860 |
*
|
|
|
861 |
* @param object $dbs database object that is extended by this class
|
|
|
862 |
* @param string $seq_name name of the sequence to be dropped
|
|
|
863 |
* @return mixed MDB_OK on success, a MDB error on failure
|
|
|
864 |
* @access public
|
|
|
865 |
*/
|
|
|
866 |
function dropSequence(&$db, $seq_name)
|
|
|
867 |
{
|
|
|
868 |
$sequence_name = $db->getSequenceName($seq_name);
|
|
|
869 |
return($db->query("DROP TABLE $sequence_name CASCADE"));
|
|
|
870 |
}
|
|
|
871 |
|
|
|
872 |
// }}}
|
|
|
873 |
// {{{ listSequences()
|
|
|
874 |
|
|
|
875 |
/**
|
|
|
876 |
* list all sequences in the current database
|
|
|
877 |
*
|
|
|
878 |
* @param object $dbs database object that is extended by this class
|
|
|
879 |
* @return mixed data array on success, a MDB error on failure
|
|
|
880 |
* @access public
|
|
|
881 |
*/
|
|
|
882 |
function listSequences(&$db)
|
|
|
883 |
{
|
|
|
884 |
$table_names = $db->queryCol('SHOW TABLES');
|
|
|
885 |
if(MDB::isError($table_names)) {
|
|
|
886 |
return($table_names);
|
|
|
887 |
}
|
|
|
888 |
for($i = 0, $j = count($table_names), $sequences = array(); $i < $j; ++$i)
|
|
|
889 |
{
|
|
|
890 |
if ($sqn = $this->_isSequenceName($db, $table_names[$i]))
|
|
|
891 |
$sequences[] = $sqn;
|
|
|
892 |
}
|
|
|
893 |
return($sequences);
|
|
|
894 |
}
|
|
|
895 |
|
|
|
896 |
// }}}
|
|
|
897 |
}
|
|
|
898 |
|
|
|
899 |
};
|
|
|
900 |
?>
|