source: branches/comu-ver2/data/module/MDB2/Driver/Manager/Common.php @ 18754

Revision 18754, 36.8 KB checked in by nanasess, 14 years ago (diff)

PEAR::MDB2 のライブラリ追加(#564)

Line 
1<?php
2// +----------------------------------------------------------------------+
3// | PHP versions 4 and 5                                                 |
4// +----------------------------------------------------------------------+
5// | Copyright (c) 1998-2008 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// | Authors: Lukas Smith <smith@pooteeweet.org>                          |
43// |          Lorenzo Alberton <l.alberton@quipo.it>                      |
44// +----------------------------------------------------------------------+
45//
46// $Id: Common.php,v 1.72 2009/01/14 15:00:40 quipo Exp $
47//
48
49/**
50 * @package  MDB2
51 * @category Database
52 * @author   Lukas Smith <smith@pooteeweet.org>
53 * @author   Lorenzo Alberton <l.alberton@quipo.it>
54 */
55
56/**
57 * Base class for the management modules that is extended by each MDB2 driver
58 *
59 * To load this module in the MDB2 object:
60 * $mdb->loadModule('Manager');
61 *
62 * @package MDB2
63 * @category Database
64 * @author  Lukas Smith <smith@pooteeweet.org>
65 */
66class MDB2_Driver_Manager_Common extends MDB2_Module_Common
67{
68    // {{{ splitTableSchema()
69
70    /**
71     * Split the "[owner|schema].table" notation into an array
72     *
73     * @param string $table [schema and] table name
74     *
75     * @return array array(schema, table)
76     * @access private
77     */
78    function splitTableSchema($table)
79    {
80        $ret = array();
81        if (strpos($table, '.') !== false) {
82            return explode('.', $table);
83        }
84        return array(null, $table);
85    }
86
87    // }}}
88    // {{{ getFieldDeclarationList()
89
90    /**
91     * Get declaration of a number of field in bulk
92     *
93     * @param array $fields  a multidimensional associative array.
94     *      The first dimension determines the field name, while the second
95     *      dimension is keyed with the name of the properties
96     *      of the field being declared as array indexes. Currently, the types
97     *      of supported field properties are as follows:
98     *
99     *      default
100     *          Boolean value to be used as default for this field.
101     *
102     *      notnull
103     *          Boolean flag that indicates whether this field is constrained
104     *          to not be set to null.
105     *
106     * @return mixed string on success, a MDB2 error on failure
107     * @access public
108     */
109    function getFieldDeclarationList($fields)
110    {
111        $db =& $this->getDBInstance();
112        if (PEAR::isError($db)) {
113            return $db;
114        }
115
116        if (!is_array($fields) || empty($fields)) {
117            return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
118                'missing any fields', __FUNCTION__);
119        }
120        foreach ($fields as $field_name => $field) {
121            $query = $db->getDeclaration($field['type'], $field_name, $field);
122            if (PEAR::isError($query)) {
123                return $query;
124            }
125            $query_fields[] = $query;
126        }
127        return implode(', ', $query_fields);
128    }
129
130    // }}}
131    // {{{ _fixSequenceName()
132
133    /**
134     * Removes any formatting in an sequence name using the 'seqname_format' option
135     *
136     * @param string $sqn string that containts name of a potential sequence
137     * @param bool $check if only formatted sequences should be returned
138     * @return string name of the sequence with possible formatting removed
139     * @access protected
140     */
141    function _fixSequenceName($sqn, $check = false)
142    {
143        $db =& $this->getDBInstance();
144        if (PEAR::isError($db)) {
145            return $db;
146        }
147
148        $seq_pattern = '/^'.preg_replace('/%s/', '([a-z0-9_]+)', $db->options['seqname_format']).'$/i';
149        $seq_name = preg_replace($seq_pattern, '\\1', $sqn);
150        if ($seq_name && !strcasecmp($sqn, $db->getSequenceName($seq_name))) {
151            return $seq_name;
152        }
153        if ($check) {
154            return false;
155        }
156        return $sqn;
157    }
158
159    // }}}
160    // {{{ _fixIndexName()
161
162    /**
163     * Removes any formatting in an index name using the 'idxname_format' option
164     *
165     * @param string $idx string that containts name of anl index
166     * @return string name of the index with eventual formatting removed
167     * @access protected
168     */
169    function _fixIndexName($idx)
170    {
171        $db =& $this->getDBInstance();
172        if (PEAR::isError($db)) {
173            return $db;
174        }
175
176        $idx_pattern = '/^'.preg_replace('/%s/', '([a-z0-9_]+)', $db->options['idxname_format']).'$/i';
177        $idx_name = preg_replace($idx_pattern, '\\1', $idx);
178        if ($idx_name && !strcasecmp($idx, $db->getIndexName($idx_name))) {
179            return $idx_name;
180        }
181        return $idx;
182    }
183
184    // }}}
185    // {{{ createDatabase()
186
187    /**
188     * create a new database
189     *
190     * @param string $name    name of the database that should be created
191     * @param array  $options array with charset, collation info
192     *
193     * @return mixed MDB2_OK on success, a MDB2 error on failure
194     * @access public
195     */
196    function createDatabase($database, $options = array())
197    {
198        $db =& $this->getDBInstance();
199        if (PEAR::isError($db)) {
200            return $db;
201        }
202
203        return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
204            'method not implemented', __FUNCTION__);
205    }
206
207    // }}}
208    // {{{ alterDatabase()
209
210    /**
211     * alter an existing database
212     *
213     * @param string $name    name of the database that should be created
214     * @param array  $options array with charset, collation info
215     *
216     * @return mixed MDB2_OK on success, a MDB2 error on failure
217     * @access public
218     */
219    function alterDatabase($database, $options = array())
220    {
221        $db =& $this->getDBInstance();
222        if (PEAR::isError($db)) {
223            return $db;
224        }
225
226        return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
227            'method not implemented', __FUNCTION__);
228    }
229
230    // }}}
231    // {{{ dropDatabase()
232
233    /**
234     * drop an existing database
235     *
236     * @param string $name name of the database that should be dropped
237     * @return mixed MDB2_OK on success, a MDB2 error on failure
238     * @access public
239     */
240    function dropDatabase($database)
241    {
242        $db =& $this->getDBInstance();
243        if (PEAR::isError($db)) {
244            return $db;
245        }
246
247        return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
248            'method not implemented', __FUNCTION__);
249    }
250
251    // }}}
252    // {{{ _getCreateTableQuery()
253
254    /**
255     * Create a basic SQL query for a new table creation
256     *
257     * @param string $name    Name of the database that should be created
258     * @param array  $fields  Associative array that contains the definition of each field of the new table
259     * @param array  $options An associative array of table options
260     *
261     * @return mixed string (the SQL query) on success, a MDB2 error on failure
262     * @see createTable()
263     */
264    function _getCreateTableQuery($name, $fields, $options = array())
265    {
266        $db =& $this->getDBInstance();
267        if (PEAR::isError($db)) {
268            return $db;
269        }
270
271        if (!$name) {
272            return $db->raiseError(MDB2_ERROR_CANNOT_CREATE, null, null,
273                'no valid table name specified', __FUNCTION__);
274        }
275        if (empty($fields)) {
276            return $db->raiseError(MDB2_ERROR_CANNOT_CREATE, null, null,
277                'no fields specified for table "'.$name.'"', __FUNCTION__);
278        }
279        $query_fields = $this->getFieldDeclarationList($fields);
280        if (PEAR::isError($query_fields)) {
281            return $query_fields;
282        }
283        if (!empty($options['primary'])) {
284            $query_fields.= ', PRIMARY KEY ('.implode(', ', array_keys($options['primary'])).')';
285        }
286
287        $name = $db->quoteIdentifier($name, true);
288        $result = 'CREATE ';
289        if (!empty($options['temporary'])) {
290            $result .= $this->_getTemporaryTableQuery();
291        }
292        $result .= " TABLE $name ($query_fields)";
293        return $result;
294    }
295
296    // }}}
297    // {{{ _getTemporaryTableQuery()
298
299    /**
300     * A method to return the required SQL string that fits between CREATE ... TABLE
301     * to create the table as a temporary table.
302     *
303     * Should be overridden in driver classes to return the correct string for the
304     * specific database type.
305     *
306     * The default is to return the string "TEMPORARY" - this will result in a
307     * SQL error for any database that does not support temporary tables, or that
308     * requires a different SQL command from "CREATE TEMPORARY TABLE".
309     *
310     * @return string The string required to be placed between "CREATE" and "TABLE"
311     *                to generate a temporary table, if possible.
312     */
313    function _getTemporaryTableQuery()
314    {
315        return 'TEMPORARY';
316    }
317
318    // }}}
319    // {{{ createTable()
320
321    /**
322     * create a new table
323     *
324     * @param string $name   Name of the database that should be created
325     * @param array $fields  Associative array that contains the definition of each field of the new table
326     *                       The indexes of the array entries are the names of the fields of the table an
327     *                       the array entry values are associative arrays like those that are meant to be
328     *                       passed with the field definitions to get[Type]Declaration() functions.
329     *                          array(
330     *                              'id' => array(
331     *                                  'type' => 'integer',
332     *                                  'unsigned' => 1
333     *                                  'notnull' => 1
334     *                                  'default' => 0
335     *                              ),
336     *                              'name' => array(
337     *                                  'type' => 'text',
338     *                                  'length' => 12
339     *                              ),
340     *                              'password' => array(
341     *                                  'type' => 'text',
342     *                                  'length' => 12
343     *                              )
344     *                          );
345     * @param array $options  An associative array of table options:
346     *                          array(
347     *                              'comment' => 'Foo',
348     *                              'temporary' => true|false,
349     *                          );
350     * @return mixed MDB2_OK on success, a MDB2 error on failure
351     * @access public
352     */
353    function createTable($name, $fields, $options = array())
354    {
355        $query = $this->_getCreateTableQuery($name, $fields, $options);
356        if (PEAR::isError($query)) {
357            return $query;
358        }
359        $db =& $this->getDBInstance();
360        if (PEAR::isError($db)) {
361            return $db;
362        }
363        $result = $db->exec($query);
364        if (PEAR::isError($result)) {
365            return $result;
366        }
367        return MDB2_OK;
368    }
369
370    // }}}
371    // {{{ dropTable()
372
373    /**
374     * drop an existing table
375     *
376     * @param string $name name of the table that should be dropped
377     * @return mixed MDB2_OK on success, a MDB2 error on failure
378     * @access public
379     */
380    function dropTable($name)
381    {
382        $db =& $this->getDBInstance();
383        if (PEAR::isError($db)) {
384            return $db;
385        }
386
387        $name = $db->quoteIdentifier($name, true);
388        return $db->exec("DROP TABLE $name");
389    }
390
391    // }}}
392    // {{{ truncateTable()
393
394    /**
395     * Truncate an existing table (if the TRUNCATE TABLE syntax is not supported,
396     * it falls back to a DELETE FROM TABLE query)
397     *
398     * @param string $name name of the table that should be truncated
399     * @return mixed MDB2_OK on success, a MDB2 error on failure
400     * @access public
401     */
402    function truncateTable($name)
403    {
404        $db =& $this->getDBInstance();
405        if (PEAR::isError($db)) {
406            return $db;
407        }
408
409        $name = $db->quoteIdentifier($name, true);
410        return $db->exec("DELETE FROM $name");
411    }
412
413    // }}}
414    // {{{ vacuum()
415
416    /**
417     * Optimize (vacuum) all the tables in the db (or only the specified table)
418     * and optionally run ANALYZE.
419     *
420     * @param string $table table name (all the tables if empty)
421     * @param array  $options an array with driver-specific options:
422     *               - timeout [int] (in seconds) [mssql-only]
423     *               - analyze [boolean] [pgsql and mysql]
424     *               - full [boolean] [pgsql-only]
425     *               - freeze [boolean] [pgsql-only]
426     *
427     * @return mixed MDB2_OK success, a MDB2 error on failure
428     * @access public
429     */
430    function vacuum($table = null, $options = array())
431    {
432        $db =& $this->getDBInstance();
433        if (PEAR::isError($db)) {
434            return $db;
435        }
436
437        return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
438            'method not implemented', __FUNCTION__);
439    }
440
441    // }}}
442    // {{{ alterTable()
443
444    /**
445     * alter an existing table
446     *
447     * @param string $name         name of the table that is intended to be changed.
448     * @param array $changes     associative array that contains the details of each type
449     *                             of change that is intended to be performed. The types of
450     *                             changes that are currently supported are defined as follows:
451     *
452     *                             name
453     *
454     *                                New name for the table.
455     *
456     *                            add
457     *
458     *                                Associative array with the names of fields to be added as
459     *                                 indexes of the array. The value of each entry of the array
460     *                                 should be set to another associative array with the properties
461     *                                 of the fields to be added. The properties of the fields should
462     *                                 be the same as defined by the MDB2 parser.
463     *
464     *
465     *                            remove
466     *
467     *                                Associative array with the names of fields to be removed as indexes
468     *                                 of the array. Currently the values assigned to each entry are ignored.
469     *                                 An empty array should be used for future compatibility.
470     *
471     *                            rename
472     *
473     *                                Associative array with the names of fields to be renamed as indexes
474     *                                 of the array. The value of each entry of the array should be set to
475     *                                 another associative array with the entry named name with the new
476     *                                 field name and the entry named Declaration that is expected to contain
477     *                                 the portion of the field declaration already in DBMS specific SQL code
478     *                                 as it is used in the CREATE TABLE statement.
479     *
480     *                            change
481     *
482     *                                Associative array with the names of the fields to be changed as indexes
483     *                                 of the array. Keep in mind that if it is intended to change either the
484     *                                 name of a field and any other properties, the change array entries
485     *                                 should have the new names of the fields as array indexes.
486     *
487     *                                The value of each entry of the array should be set to another associative
488     *                                 array with the properties of the fields to that are meant to be changed as
489     *                                 array entries. These entries should be assigned to the new values of the
490     *                                 respective properties. The properties of the fields should be the same
491     *                                 as defined by the MDB2 parser.
492     *
493     *                            Example
494     *                                array(
495     *                                    'name' => 'userlist',
496     *                                    'add' => array(
497     *                                        'quota' => array(
498     *                                            'type' => 'integer',
499     *                                            'unsigned' => 1
500     *                                        )
501     *                                    ),
502     *                                    'remove' => array(
503     *                                        'file_limit' => array(),
504     *                                        'time_limit' => array()
505     *                                    ),
506     *                                    'change' => array(
507     *                                        'name' => array(
508     *                                            'length' => '20',
509     *                                            'definition' => array(
510     *                                                'type' => 'text',
511     *                                                'length' => 20,
512     *                                            ),
513     *                                        )
514     *                                    ),
515     *                                    'rename' => array(
516     *                                        'sex' => array(
517     *                                            'name' => 'gender',
518     *                                            'definition' => array(
519     *                                                'type' => 'text',
520     *                                                'length' => 1,
521     *                                                'default' => 'M',
522     *                                            ),
523     *                                        )
524     *                                    )
525     *                                )
526     *
527     * @param boolean $check     indicates whether the function should just check if the DBMS driver
528     *                             can perform the requested table alterations if the value is true or
529     *                             actually perform them otherwise.
530     * @access public
531     *
532      * @return mixed MDB2_OK on success, a MDB2 error on failure
533     */
534    function alterTable($name, $changes, $check)
535    {
536        $db =& $this->getDBInstance();
537        if (PEAR::isError($db)) {
538            return $db;
539        }
540
541        return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
542            'method not implemented', __FUNCTION__);
543    }
544
545    // }}}
546    // {{{ listDatabases()
547
548    /**
549     * list all databases
550     *
551     * @return mixed array of database names on success, a MDB2 error on failure
552     * @access public
553     */
554    function listDatabases()
555    {
556        $db =& $this->getDBInstance();
557        if (PEAR::isError($db)) {
558            return $db;
559        }
560
561        return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
562            'method not implementedd', __FUNCTION__);
563    }
564
565    // }}}
566    // {{{ listUsers()
567
568    /**
569     * list all users
570     *
571     * @return mixed array of user names on success, a MDB2 error on failure
572     * @access public
573     */
574    function listUsers()
575    {
576        $db =& $this->getDBInstance();
577        if (PEAR::isError($db)) {
578            return $db;
579        }
580
581        return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
582            'method not implemented', __FUNCTION__);
583    }
584
585    // }}}
586    // {{{ listViews()
587
588    /**
589     * list all views in the current database
590     *
591     * @param string database, the current is default
592     *               NB: not all the drivers can get the view names from
593     *               a database other than the current one
594     * @return mixed array of view names on success, a MDB2 error on failure
595     * @access public
596     */
597    function listViews($database = null)
598    {
599        $db =& $this->getDBInstance();
600        if (PEAR::isError($db)) {
601            return $db;
602        }
603
604        return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
605            'method not implemented', __FUNCTION__);
606    }
607
608    // }}}
609    // {{{ listTableViews()
610
611    /**
612     * list the views in the database that reference a given table
613     *
614     * @param string table for which all referenced views should be found
615     * @return mixed array of view names on success, a MDB2 error on failure
616     * @access public
617     */
618    function listTableViews($table)
619    {
620        $db =& $this->getDBInstance();
621        if (PEAR::isError($db)) {
622            return $db;
623        }
624
625        return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
626            'method not implemented', __FUNCTION__);
627    }
628
629    // }}}
630    // {{{ listTableTriggers()
631
632    /**
633     * list all triggers in the database that reference a given table
634     *
635     * @param string table for which all referenced triggers should be found
636     * @return mixed array of trigger names on success, a MDB2 error on failure
637     * @access public
638     */
639    function listTableTriggers($table = null)
640    {
641        $db =& $this->getDBInstance();
642        if (PEAR::isError($db)) {
643            return $db;
644        }
645
646        return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
647            'method not implemented', __FUNCTION__);
648    }
649
650    // }}}
651    // {{{ listFunctions()
652
653    /**
654     * list all functions in the current database
655     *
656     * @return mixed array of function names on success, a MDB2 error on failure
657     * @access public
658     */
659    function listFunctions()
660    {
661        $db =& $this->getDBInstance();
662        if (PEAR::isError($db)) {
663            return $db;
664        }
665
666        return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
667            'method not implemented', __FUNCTION__);
668    }
669
670    // }}}
671    // {{{ listTables()
672
673    /**
674     * list all tables in the current database
675     *
676     * @param string database, the current is default.
677     *               NB: not all the drivers can get the table names from
678     *               a database other than the current one
679     * @return mixed array of table names on success, a MDB2 error on failure
680     * @access public
681     */
682    function listTables($database = null)
683    {
684        $db =& $this->getDBInstance();
685        if (PEAR::isError($db)) {
686            return $db;
687        }
688
689        return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
690            'method not implemented', __FUNCTION__);
691    }
692
693    // }}}
694    // {{{ listTableFields()
695
696    /**
697     * list all fields in a table in the current database
698     *
699     * @param string $table name of table that should be used in method
700     * @return mixed array of field names on success, a MDB2 error on failure
701     * @access public
702     */
703    function listTableFields($table)
704    {
705        $db =& $this->getDBInstance();
706        if (PEAR::isError($db)) {
707            return $db;
708        }
709
710        return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
711            'method not implemented', __FUNCTION__);
712    }
713
714    // }}}
715    // {{{ createIndex()
716
717    /**
718     * Get the stucture of a field into an array
719     *
720     * @param string    $table         name of the table on which the index is to be created
721     * @param string    $name         name of the index to be created
722     * @param array     $definition        associative array that defines properties of the index to be created.
723     *                                 Currently, only one property named FIELDS is supported. This property
724     *                                 is also an associative with the names of the index fields as array
725     *                                 indexes. Each entry of this array is set to another type of associative
726     *                                 array that specifies properties of the index that are specific to
727     *                                 each field.
728     *
729     *                                Currently, only the sorting property is supported. It should be used
730     *                                 to define the sorting direction of the index. It may be set to either
731     *                                 ascending or descending.
732     *
733     *                                Not all DBMS support index sorting direction configuration. The DBMS
734     *                                 drivers of those that do not support it ignore this property. Use the
735     *                                 function supports() to determine whether the DBMS driver can manage indexes.
736     *
737     *                                 Example
738     *                                    array(
739     *                                        'fields' => array(
740     *                                            'user_name' => array(
741     *                                                'sorting' => 'ascending'
742     *                                            ),
743     *                                            'last_login' => array()
744     *                                        )
745     *                                    )
746     * @return mixed MDB2_OK on success, a MDB2 error on failure
747     * @access public
748     */
749    function createIndex($table, $name, $definition)
750    {
751        $db =& $this->getDBInstance();
752        if (PEAR::isError($db)) {
753            return $db;
754        }
755
756        $table = $db->quoteIdentifier($table, true);
757        $name = $db->quoteIdentifier($db->getIndexName($name), true);
758        $query = "CREATE INDEX $name ON $table";
759        $fields = array();
760        foreach (array_keys($definition['fields']) as $field) {
761            $fields[] = $db->quoteIdentifier($field, true);
762        }
763        $query .= ' ('. implode(', ', $fields) . ')';
764        return $db->exec($query);
765    }
766
767    // }}}
768    // {{{ dropIndex()
769
770    /**
771     * drop existing index
772     *
773     * @param string    $table         name of table that should be used in method
774     * @param string    $name         name of the index to be dropped
775     * @return mixed MDB2_OK on success, a MDB2 error on failure
776     * @access public
777     */
778    function dropIndex($table, $name)
779    {
780        $db =& $this->getDBInstance();
781        if (PEAR::isError($db)) {
782            return $db;
783        }
784
785        $name = $db->quoteIdentifier($db->getIndexName($name), true);
786        return $db->exec("DROP INDEX $name");
787    }
788
789    // }}}
790    // {{{ listTableIndexes()
791
792    /**
793     * list all indexes in a table
794     *
795     * @param string $table name of table that should be used in method
796     * @return mixed array of index names on success, a MDB2 error on failure
797     * @access public
798     */
799    function listTableIndexes($table)
800    {
801        $db =& $this->getDBInstance();
802        if (PEAR::isError($db)) {
803            return $db;
804        }
805
806        return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
807            'method not implemented', __FUNCTION__);
808    }
809
810    // }}}
811    // {{{ _getAdvancedFKOptions()
812
813    /**
814     * Return the FOREIGN KEY query section dealing with non-standard options
815     * as MATCH, INITIALLY DEFERRED, ON UPDATE, ...
816     *
817     * @param array $definition
818     * @return string
819     * @access protected
820     */
821    function _getAdvancedFKOptions($definition)
822    {
823        return '';
824    }
825
826    // }}}
827    // {{{ createConstraint()
828
829    /**
830     * create a constraint on a table
831     *
832     * @param string    $table       name of the table on which the constraint is to be created
833     * @param string    $name        name of the constraint to be created
834     * @param array     $definition  associative array that defines properties of the constraint to be created.
835     *                               The full structure of the array looks like this:
836     *          <pre>
837     *          array (
838     *              [primary] => 0
839     *              [unique]  => 0
840     *              [foreign] => 1
841     *              [check]   => 0
842     *              [fields] => array (
843     *                  [field1name] => array() // one entry per each field covered
844     *                  [field2name] => array() // by the index
845     *                  [field3name] => array(
846     *                      [sorting]  => ascending
847     *                      [position] => 3
848     *                  )
849     *              )
850     *              [references] => array(
851     *                  [table] => name
852     *                  [fields] => array(
853     *                      [field1name] => array(  //one entry per each referenced field
854     *                           [position] => 1
855     *                      )
856     *                  )
857     *              )
858     *              [deferrable] => 0
859     *              [initiallydeferred] => 0
860     *              [onupdate] => CASCADE|RESTRICT|SET NULL|SET DEFAULT|NO ACTION
861     *              [ondelete] => CASCADE|RESTRICT|SET NULL|SET DEFAULT|NO ACTION
862     *              [match] => SIMPLE|PARTIAL|FULL
863     *          );
864     *          </pre>
865     * @return mixed MDB2_OK on success, a MDB2 error on failure
866     * @access public
867     */
868    function createConstraint($table, $name, $definition)
869    {
870        $db =& $this->getDBInstance();
871        if (PEAR::isError($db)) {
872            return $db;
873        }
874        $table = $db->quoteIdentifier($table, true);
875        $name = $db->quoteIdentifier($db->getIndexName($name), true);
876        $query = "ALTER TABLE $table ADD CONSTRAINT $name";
877        if (!empty($definition['primary'])) {
878            $query.= ' PRIMARY KEY';
879        } elseif (!empty($definition['unique'])) {
880            $query.= ' UNIQUE';
881        } elseif (!empty($definition['foreign'])) {
882            $query.= ' FOREIGN KEY';
883        }
884        $fields = array();
885        foreach (array_keys($definition['fields']) as $field) {
886            $fields[] = $db->quoteIdentifier($field, true);
887        }
888        $query .= ' ('. implode(', ', $fields) . ')';
889        if (!empty($definition['foreign'])) {
890            $query.= ' REFERENCES ' . $db->quoteIdentifier($definition['references']['table'], true);
891            $referenced_fields = array();
892            foreach (array_keys($definition['references']['fields']) as $field) {
893                $referenced_fields[] = $db->quoteIdentifier($field, true);
894            }
895            $query .= ' ('. implode(', ', $referenced_fields) . ')';
896            $query .= $this->_getAdvancedFKOptions($definition);
897        }
898        return $db->exec($query);
899    }
900
901    // }}}
902    // {{{ dropConstraint()
903
904    /**
905     * drop existing constraint
906     *
907     * @param string    $table        name of table that should be used in method
908     * @param string    $name         name of the constraint to be dropped
909     * @param string    $primary      hint if the constraint is primary
910     * @return mixed MDB2_OK on success, a MDB2 error on failure
911     * @access public
912     */
913    function dropConstraint($table, $name, $primary = false)
914    {
915        $db =& $this->getDBInstance();
916        if (PEAR::isError($db)) {
917            return $db;
918        }
919
920        $table = $db->quoteIdentifier($table, true);
921        $name = $db->quoteIdentifier($db->getIndexName($name), true);
922        return $db->exec("ALTER TABLE $table DROP CONSTRAINT $name");
923    }
924
925    // }}}
926    // {{{ listTableConstraints()
927
928    /**
929     * list all constraints in a table
930     *
931     * @param string $table name of table that should be used in method
932     * @return mixed array of constraint names on success, a MDB2 error on failure
933     * @access public
934     */
935    function listTableConstraints($table)
936    {
937        $db =& $this->getDBInstance();
938        if (PEAR::isError($db)) {
939            return $db;
940        }
941
942        return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
943            'method not implemented', __FUNCTION__);
944    }
945
946    // }}}
947    // {{{ createSequence()
948
949    /**
950     * create sequence
951     *
952     * @param string    $seq_name     name of the sequence to be created
953     * @param string    $start         start value of the sequence; default is 1
954     * @return mixed MDB2_OK on success, a MDB2 error on failure
955     * @access public
956     */
957    function createSequence($seq_name, $start = 1)
958    {
959        $db =& $this->getDBInstance();
960        if (PEAR::isError($db)) {
961            return $db;
962        }
963
964        return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
965            'method not implemented', __FUNCTION__);
966    }
967
968    // }}}
969    // {{{ dropSequence()
970
971    /**
972     * drop existing sequence
973     *
974     * @param string    $seq_name     name of the sequence to be dropped
975     * @return mixed MDB2_OK on success, a MDB2 error on failure
976     * @access public
977     */
978    function dropSequence($name)
979    {
980        $db =& $this->getDBInstance();
981        if (PEAR::isError($db)) {
982            return $db;
983        }
984
985        return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
986            'method not implemented', __FUNCTION__);
987    }
988
989    // }}}
990    // {{{ listSequences()
991
992    /**
993     * list all sequences in the current database
994     *
995     * @param string database, the current is default
996     *               NB: not all the drivers can get the sequence names from
997     *               a database other than the current one
998     * @return mixed array of sequence names on success, a MDB2 error on failure
999     * @access public
1000     */
1001    function listSequences($database = null)
1002    {
1003        $db =& $this->getDBInstance();
1004        if (PEAR::isError($db)) {
1005            return $db;
1006        }
1007
1008        return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
1009            'method not implemented', __FUNCTION__);
1010    }
1011
1012    // }}}
1013}
1014?>
Note: See TracBrowser for help on using the repository browser.