thomascube
2006-02-22 745b1466fc76d5ded589e2469328086002430c1c
commit | author | age
d13c36 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 /**
49  * @package  MDB2
50  * @category Database
51  * @author   Lukas Smith <smith@pooteeweet.org>
52  */
53
54 /**
55  * Base class for the management modules that is extended by each MDB2 driver
56  *
57  * @package MDB2
58  * @category Database
59  * @author  Lukas Smith <smith@pooteeweet.org>
60  */
61 class MDB2_Driver_Manager_Common extends MDB2_Module_Common
62 {
63     // }}}
64     // {{{ getFieldDeclarationList()
65
66     /**
67      * get declaration of a number of field in bulk
68      *
69      * @param string $fields  a multidimensional associative array.
70      *      The first dimension determines the field name, while the second
71      *      dimension is keyed with the name of the properties
72      *      of the field being declared as array indexes. Currently, the types
73      *      of supported field properties are as follows:
74      *
75      *      default
76      *          Boolean value to be used as default for this field.
77      *
78      *      notnull
79      *          Boolean flag that indicates whether this field is constrained
80      *          to not be set to null.
81      *
82      * @return mixed string on success, a MDB2 error on failure
83      * @access public
84      */
85     function getFieldDeclarationList($fields)
86     {
87         $db =& $this->getDBInstance();
88         if (PEAR::isError($db)) {
89             return $db;
90         }
91
92         if (is_array($fields)) {
93             foreach ($fields as $field_name => $field) {
94                 $query = $db->getDeclaration($field['type'], $field_name, $field);
95                 if (PEAR::isError($query)) {
96                     return $query;
97                 }
98                 $query_fields[] = $query;
99             }
100             return implode(',', $query_fields);
101         }
102         return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
103             'getFieldDeclarationList: the definition of the table "'.$table_name.'" does not contain any fields');
104     }
105
106     // }}}
107     // {{{ _isSequenceName()
108
109     /**
110      * list all tables in the current database
111      *
112      * @param string $sqn string that containts name of a potential sequence
113      * @return mixed name of the sequence if $sqn is a name of a sequence, else false
114      * @access protected
115      */
116     function _isSequenceName($sqn)
117     {
118         $db =& $this->getDBInstance();
119         if (PEAR::isError($db)) {
120             return $db;
121         }
122
123         $seq_pattern = '/^'.preg_replace('/%s/', '([a-z0-9_]+)', $db->options['seqname_format']).'$/i';
124         $seq_name = preg_replace($seq_pattern, '\\1', $sqn);
125         if ($seq_name && $sqn == $db->getSequenceName($seq_name)) {
126             return $seq_name;
127         }
128         return false;
129     }
130
131     // }}}
132     // {{{ createDatabase()
133
134     /**
135      * create a new database
136      *
137      * @param string $name name of the database that should be created
138      * @return mixed MDB2_OK on success, a MDB2 error on failure
139      * @access public
140      */
141     function createDatabase($database)
142     {
143         $db =& $this->getDBInstance();
144         if (PEAR::isError($db)) {
145             return $db;
146         }
147
148         return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
149             'createDatabase: database creation is not supported');
150     }
151
152     // }}}
153     // {{{ dropDatabase()
154
155     /**
156      * drop an existing database
157      *
158      * @param string $name name of the database that should be dropped
159      * @return mixed MDB2_OK on success, a MDB2 error on failure
160      * @access public
161      */
162     function dropDatabase($database)
163     {
164         $db =& $this->getDBInstance();
165         if (PEAR::isError($db)) {
166             return $db;
167         }
168
169         return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
170             'dropDatabase: database dropping is not supported');
171     }
172
173     // }}}
174     // {{{ createTable()
175
176     /**
177      * create a new table
178      *
179      * @param string $name     Name of the database that should be created
180      * @param array $fields Associative array that contains the definition of each field of the new table
181      *                        The indexes of the array entries are the names of the fields of the table an
182      *                        the array entry values are associative arrays like those that are meant to be
183      *                         passed with the field definitions to get[Type]Declaration() functions.
184      *
185      *                        Example
186      *                        array(
187      *
188      *                            'id' => array(
189      *                                'type' => 'integer',
190      *                                'unsigned' => 1
191      *                                'notnull' => 1
192      *                                'default' => 0
193      *                            ),
194      *                            'name' => array(
195      *                                'type' => 'text',
196      *                                'length' => 12
197      *                            ),
198      *                            'password' => array(
199      *                                'type' => 'text',
200      *                                'length' => 12
201      *                            )
202      *                        );
203      * @return mixed MDB2_OK on success, a MDB2 error on failure
204      * @access public
205      */
206     function createTable($name, $fields)
207     {
208         $db =& $this->getDBInstance();
209         if (PEAR::isError($db)) {
210             return $db;
211         }
212
213         if (!$name) {
214             return $db->raiseError(MDB2_ERROR_CANNOT_CREATE, null, null,
215                 'createTable: no valid table name specified');
216         }
217         if (empty($fields)) {
218             return $db->raiseError(MDB2_ERROR_CANNOT_CREATE, null, null,
219                 'createTable: no fields specified for table "'.$name.'"');
220         }
221         $query_fields = $this->getFieldDeclarationList($fields);
222         if (PEAR::isError($query_fields)) {
223             return $db->raiseError(MDB2_ERROR_CANNOT_CREATE, null, null,
224                 'createTable: unkown error');
225         }
226         $query = "CREATE TABLE $name ($query_fields)";
227         return $db->query($query);
228     }
229
230     // }}}
231     // {{{ dropTable()
232
233     /**
234      * drop an existing table
235      *
236      * @param string $name name of the table that should be dropped
237      * @return mixed MDB2_OK on success, a MDB2 error on failure
238      * @access public
239      */
240     function dropTable($name)
241     {
242         $db =& $this->getDBInstance();
243         if (PEAR::isError($db)) {
244             return $db;
245         }
246
247         return $db->query("DROP TABLE $name");
248     }
249
250     // }}}
251     // {{{ alterTable()
252
253     /**
254      * alter an existing table
255      *
256      * @param string $name         name of the table that is intended to be changed.
257      * @param array $changes     associative array that contains the details of each type
258      *                             of change that is intended to be performed. The types of
259      *                             changes that are currently supported are defined as follows:
260      *
261      *                             name
262      *
263      *                                New name for the table.
264      *
265      *                            add
266      *
267      *                                Associative array with the names of fields to be added as
268      *                                 indexes of the array. The value of each entry of the array
269      *                                 should be set to another associative array with the properties
270      *                                 of the fields to be added. The properties of the fields should
271      *                                 be the same as defined by the Metabase parser.
272      *
273      *
274      *                            remove
275      *
276      *                                Associative array with the names of fields to be removed as indexes
277      *                                 of the array. Currently the values assigned to each entry are ignored.
278      *                                 An empty array should be used for future compatibility.
279      *
280      *                            rename
281      *
282      *                                Associative array with the names of fields to be renamed as indexes
283      *                                 of the array. The value of each entry of the array should be set to
284      *                                 another associative array with the entry named name with the new
285      *                                 field name and the entry named Declaration that is expected to contain
286      *                                 the portion of the field declaration already in DBMS specific SQL code
287      *                                 as it is used in the CREATE TABLE statement.
288      *
289      *                            change
290      *
291      *                                Associative array with the names of the fields to be changed as indexes
292      *                                 of the array. Keep in mind that if it is intended to change either the
293      *                                 name of a field and any other properties, the change array entries
294      *                                 should have the new names of the fields as array indexes.
295      *
296      *                                The value of each entry of the array should be set to another associative
297      *                                 array with the properties of the fields to that are meant to be changed as
298      *                                 array entries. These entries should be assigned to the new values of the
299      *                                 respective properties. The properties of the fields should be the same
300      *                                 as defined by the Metabase parser.
301      *
302      *                            Example
303      *                                array(
304      *                                    'name' => 'userlist',
305      *                                    'add' => array(
306      *                                        'quota' => array(
307      *                                            'type' => 'integer',
308      *                                            'unsigned' => 1
309      *                                        )
310      *                                    ),
311      *                                    'remove' => array(
312      *                                        'file_limit' => array(),
313      *                                        'time_limit' => array()
314      *                                        ),
315      *                                    'change' => array(
316      *                                        'gender' => array(
317      *                                            'default' => 'M',
318      *                                        )
319      *                                    ),
320      *                                    'rename' => array(
321      *                                        'sex' => array(
322      *                                            'name' => 'gender',
323      *                                        )
324      *                                    )
325      *                                )
326      * @param boolean $check     indicates whether the function should just check if the DBMS driver
327      *                             can perform the requested table alterations if the value is true or
328      *                             actually perform them otherwise.
329      * @return mixed MDB2_OK on success, a MDB2 error on failure
330      * @access public
331      */
332     function alterTable($name, $changes, $check)
333     {
334         $db =& $this->getDBInstance();
335         if (PEAR::isError($db)) {
336             return $db;
337         }
338
339         return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
340             'alterTable: database table alterations are not supported');
341     }
342
343     // }}}
344     // {{{ listDatabases()
345
346     /**
347      * list all databases
348      *
349      * @return mixed data array on success, a MDB2 error on failure
350      * @access public
351      */
352     function listDatabases()
353     {
354         $db =& $this->getDBInstance();
355         if (PEAR::isError($db)) {
356             return $db;
357         }
358
359         return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
360             'listDatabases: list databases is not supported');
361     }
362
363     // }}}
364     // {{{ listUsers()
365
366     /**
367      * list all users
368      *
369      * @return mixed data array on success, a MDB2 error on failure
370      * @access public
371      */
372     function listUsers()
373     {
374         $db =& $this->getDBInstance();
375         if (PEAR::isError($db)) {
376             return $db;
377         }
378
379         return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
380             'listUsers: list user is not supported');
381     }
382
383     // }}}
384     // {{{ listViews()
385
386     /**
387      * list all views in the current database
388      *
389      * @return mixed data array on success, a MDB2 error on failure
390      * @access public
391      */
392     function listViews()
393     {
394         $db =& $this->getDBInstance();
395         if (PEAR::isError($db)) {
396             return $db;
397         }
398
399         return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
400             'listViews: list view is not supported');
401     }
402
403     // }}}
404     // {{{ listFunctions()
405
406     /**
407      * list all functions in the current database
408      *
409      * @return mixed data array on success, a MDB2 error on failure
410      * @access public
411      */
412     function listFunctions()
413     {
414         $db =& $this->getDBInstance();
415         if (PEAR::isError($db)) {
416             return $db;
417         }
418
419         return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
420             'listFunctions: list function is not supported');
421     }
422
423     // }}}
424     // {{{ listTables()
425
426     /**
427      * list all tables in the current database
428      *
429      * @return mixed data array on success, a MDB2 error on failure
430      * @access public
431      */
432     function listTables()
433     {
434         $db =& $this->getDBInstance();
435         if (PEAR::isError($db)) {
436             return $db;
437         }
438
439         return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
440             'listTables: list tables is not supported');
441     }
442
443     // }}}
444     // {{{ listTableFields()
445
446     /**
447      * list all fields in a tables in the current database
448      *
449      * @param string $table name of table that should be used in method
450      * @return mixed data array on success, a MDB2 error on failure
451      * @access public
452      */
453     function listTableFields($table)
454     {
455         $db =& $this->getDBInstance();
456         if (PEAR::isError($db)) {
457             return $db;
458         }
459
460         return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
461             'listTableFields: list table fields is not supported');
462     }
463
464     // }}}
465     // {{{ createIndex()
466
467     /**
468      * get the stucture of a field into an array
469      *
470      * @param string    $table         name of the table on which the index is to be created
471      * @param string    $name         name of the index to be created
472      * @param array     $definition        associative array that defines properties of the index to be created.
473      *                                 Currently, only one property named FIELDS is supported. This property
474      *                                 is also an associative with the names of the index fields as array
475      *                                 indexes. Each entry of this array is set to another type of associative
476      *                                 array that specifies properties of the index that are specific to
477      *                                 each field.
478      *
479      *                                Currently, only the sorting property is supported. It should be used
480      *                                 to define the sorting direction of the index. It may be set to either
481      *                                 ascending or descending.
482      *
483      *                                Not all DBMS support index sorting direction configuration. The DBMS
484      *                                 drivers of those that do not support it ignore this property. Use the
485      *                                 function supports() to determine whether the DBMS driver can manage indexes.
486      *
487      *                                 Example
488      *                                    array(
489      *                                        'fields' => array(
490      *                                            'user_name' => array(
491      *                                                'sorting' => 'ascending'
492      *                                            ),
493      *                                            'last_login' => array()
494      *                                        )
495      *                                    )
496      * @return mixed MDB2_OK on success, a MDB2 error on failure
497      * @access public
498      */
499     function createIndex($table, $name, $definition)
500     {
501         $db =& $this->getDBInstance();
502         if (PEAR::isError($db)) {
503             return $db;
504         }
505
506         return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
507             'createIndex: Creating Indexes is not supported');
508     }
509
510     // }}}
511     // {{{ dropIndex()
512
513     /**
514      * drop existing index
515      *
516      * @param string    $table         name of table that should be used in method
517      * @param string    $name         name of the index to be dropped
518      * @return mixed MDB2_OK on success, a MDB2 error on failure
519      * @access public
520      */
521     function dropIndex($table, $name)
522     {
523         $db =& $this->getDBInstance();
524         if (PEAR::isError($db)) {
525             return $db;
526         }
527
528         return $db->query("DROP INDEX $name");
529     }
530
531     // }}}
532     // {{{ listTableIndexes()
533
534     /**
535      * list all indexes in a table
536      *
537      * @param string    $table      name of table that should be used in method
538      * @return mixed data array on success, a MDB2 error on failure
539      * @access public
540      */
541     function listTableIndexes($table)
542     {
543         $db =& $this->getDBInstance();
544         if (PEAR::isError($db)) {
545             return $db;
546         }
547
548         return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
549             'listTableIndexes: List Indexes is not supported');
550     }
551
552     // }}}
553     // {{{ createSequence()
554
555     /**
556      * create sequence
557      *
558      * @param string    $seq_name     name of the sequence to be created
559      * @param string    $start         start value of the sequence; default is 1
560      * @return mixed MDB2_OK on success, a MDB2 error on failure
561      * @access public
562      */
563     function createSequence($seq_name, $start = 1)
564     {
565         $db =& $this->getDBInstance();
566         if (PEAR::isError($db)) {
567             return $db;
568         }
569
570         return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
571             'createSequence: sequence creation not supported');
572     }
573
574     // }}}
575     // {{{ dropSequence()
576
577     /**
578      * drop existing sequence
579      *
580      * @param string    $seq_name     name of the sequence to be dropped
581      * @return mixed MDB2_OK on success, a MDB2 error on failure
582      * @access public
583      */
584     function dropSequence($name)
585     {
586         $db =& $this->getDBInstance();
587         if (PEAR::isError($db)) {
588             return $db;
589         }
590
591         return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
592             'dropSequence: sequence dropping not supported');
593     }
594
595     // }}}
596     // {{{ listSequences()
597
598     /**
599      * list all sequences in the current database
600      *
601      * @return mixed data array on success, a MDB2 error on failure
602      * @access public
603      */
604     function listSequences()
605     {
606         $db =& $this->getDBInstance();
607         if (PEAR::isError($db)) {
608             return $db;
609         }
610
611         return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
612             'listSequences: List sequences is not supported');
613     }
614 }
615
616 ?>