Subversion-Projekte lars-tiefland.niewerth

Revision

Revision 7 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
// +----------------------------------------------------------------------+
3
// | PHP versions 4 and 5                                                 |
4
// +----------------------------------------------------------------------+
5
// | Copyright (c) 1998-2006 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: Common.php,v 1.58 2006/09/26 13:57:11 quipo Exp $
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) || empty($fields)) {
93
            return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
94
                'missing any fields', __FUNCTION__);
95
        }
96
 
97
        foreach ($fields as $field_name => $field) {
98
            $query = $db->getDeclaration($field['type'], $field_name, $field);
99
            if (PEAR::isError($query)) {
100
                return $query;
101
            }
102
            $query_fields[] = $query;
103
        }
104
        return implode(', ', $query_fields);
105
    }
106
 
107
    // }}}
108
    // {{{ _fixSequenceName()
109
 
110
    /**
111
     * Removes any formatting in an sequence name using the 'seqname_format' option
112
     *
113
     * @param string $sqn string that containts name of a potential sequence
114
     * @param bool $check if only formatted sequences should be returned
115
     * @return string name of the sequence with possible formatting removed
116
     * @access protected
117
     */
118
    function _fixSequenceName($sqn, $check = false)
119
    {
120
        $db =& $this->getDBInstance();
121
        if (PEAR::isError($db)) {
122
            return $db;
123
        }
124
 
125
        $seq_pattern = '/^'.preg_replace('/%s/', '([a-z0-9_]+)', $db->options['seqname_format']).'$/i';
126
        $seq_name = preg_replace($seq_pattern, '\\1', $sqn);
127
        if ($seq_name && !strcasecmp($sqn, $db->getSequenceName($seq_name))) {
128
            return $seq_name;
129
        }
130
        if ($check) {
131
            return false;
132
        }
133
        return $sqn;
134
    }
135
 
136
    // }}}
137
    // {{{ _fixIndexName()
138
 
139
    /**
140
     * Removes any formatting in an index name using the 'idxname_format' option
141
     *
142
     * @param string $idx string that containts name of anl index
143
     * @return string name of the index with possible formatting removed
144
     * @access protected
145
     */
146
    function _fixIndexName($idx)
147
    {
148
        $db =& $this->getDBInstance();
149
        if (PEAR::isError($db)) {
150
            return $db;
151
        }
152
 
153
        $idx_pattern = '/^'.preg_replace('/%s/', '([a-z0-9_]+)', $db->options['idxname_format']).'$/i';
154
        $idx_name = preg_replace($idx_pattern, '\\1', $idx);
155
        if ($idx_name && !strcasecmp($idx, $db->getIndexName($idx_name))) {
156
            return $idx_name;
157
        }
158
        return $idx;
159
    }
160
 
161
    // }}}
162
    // {{{ createDatabase()
163
 
164
    /**
165
     * create a new database
166
     *
167
     * @param string $name name of the database that should be created
168
     * @return mixed MDB2_OK on success, a MDB2 error on failure
169
     * @access public
170
     */
171
    function createDatabase($database)
172
    {
173
        $db =& $this->getDBInstance();
174
        if (PEAR::isError($db)) {
175
            return $db;
176
        }
177
 
178
        return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
179
            'method not implemented', __FUNCTION__);
180
    }
181
 
182
    // }}}
183
    // {{{ dropDatabase()
184
 
185
    /**
186
     * drop an existing database
187
     *
188
     * @param string $name name of the database that should be dropped
189
     * @return mixed MDB2_OK on success, a MDB2 error on failure
190
     * @access public
191
     */
192
    function dropDatabase($database)
193
    {
194
        $db =& $this->getDBInstance();
195
        if (PEAR::isError($db)) {
196
            return $db;
197
        }
198
 
199
        return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
200
            'method not implemented', __FUNCTION__);
201
    }
202
 
203
    // }}}
204
    // {{{
205
 
206
    /**
207
     * Create a basic SQL query for a new table creation
208
     * @param string $name   Name of the database that should be created
209
     * @param array $fields  Associative array that contains the definition of each field of the new table
210
     * @param array $options  An associative array of table options
211
     * @return mixed string (the SQL query) on success, a MDB2 error on failure
212
     * @see createTable()
213
     */
214
    function _getCreateTableQuery($name, $fields, $options = array())
215
    {
216
        $db =& $this->getDBInstance();
217
        if (PEAR::isError($db)) {
218
            return $db;
219
        }
220
 
221
        if (!$name) {
222
            return $db->raiseError(MDB2_ERROR_CANNOT_CREATE, null, null,
223
                'no valid table name specified', __FUNCTION__);
224
        }
225
        if (empty($fields)) {
226
            return $db->raiseError(MDB2_ERROR_CANNOT_CREATE, null, null,
227
                'no fields specified for table "'.$name.'"', __FUNCTION__);
228
        }
229
        $query_fields = $this->getFieldDeclarationList($fields);
230
        if (PEAR::isError($query_fields)) {
231
            return $query_fields;
232
        }
233
        if (!empty($options['primary'])) {
234
            $query_fields.= ', PRIMARY KEY ('.implode(', ', array_keys($options['primary'])).')';
235
        }
236
 
237
        $name = $db->quoteIdentifier($name, true);
238
        return "CREATE TABLE $name ($query_fields)";
239
    }
240
 
241
    // }}}
242
    // {{{ createTable()
243
 
244
    /**
245
     * create a new table
246
     *
247
     * @param string $name   Name of the database that should be created
248
     * @param array $fields  Associative array that contains the definition of each field of the new table
249
     *                       The indexes of the array entries are the names of the fields of the table an
250
     *                       the array entry values are associative arrays like those that are meant to be
251
     *                       passed with the field definitions to get[Type]Declaration() functions.
252
     *                          array(
253
     *                              'id' => array(
254
     *                                  'type' => 'integer',
255
     *                                  'unsigned' => 1
256
     *                                  'notnull' => 1
257
     *                                  'default' => 0
258
     *                              ),
259
     *                              'name' => array(
260
     *                                  'type' => 'text',
261
     *                                  'length' => 12
262
     *                              ),
263
     *                              'password' => array(
264
     *                                  'type' => 'text',
265
     *                                  'length' => 12
266
     *                              )
267
     *                          );
268
     * @param array $options  An associative array of table options:
269
     *
270
     * @return mixed MDB2_OK on success, a MDB2 error on failure
271
     * @access public
272
     */
273
    function createTable($name, $fields, $options = array())
274
    {
275
        $query = $this->_getCreateTableQuery($name, $fields, $options);
276
        if (PEAR::isError($query)) {
277
            return $query;
278
        }
279
        $db =& $this->getDBInstance();
280
        if (PEAR::isError($db)) {
281
            return $db;
282
        }
283
        return $db->exec($query);
284
    }
285
 
286
    // }}}
287
    // {{{ dropTable()
288
 
289
    /**
290
     * drop an existing table
291
     *
292
     * @param string $name name of the table that should be dropped
293
     * @return mixed MDB2_OK on success, a MDB2 error on failure
294
     * @access public
295
     */
296
    function dropTable($name)
297
    {
298
        $db =& $this->getDBInstance();
299
        if (PEAR::isError($db)) {
300
            return $db;
301
        }
302
 
303
        $name = $db->quoteIdentifier($name, true);
304
        return $db->exec("DROP TABLE $name");
305
    }
306
 
307
    // }}}
308
    // {{{ alterTable()
309
 
310
    /**
311
     * alter an existing table
312
     *
313
     * @param string $name         name of the table that is intended to be changed.
314
     * @param array $changes     associative array that contains the details of each type
315
     *                             of change that is intended to be performed. The types of
316
     *                             changes that are currently supported are defined as follows:
317
     *
318
     *                             name
319
     *
320
     *                                New name for the table.
321
     *
322
     *                            add
323
     *
324
     *                                Associative array with the names of fields to be added as
325
     *                                 indexes of the array. The value of each entry of the array
326
     *                                 should be set to another associative array with the properties
327
     *                                 of the fields to be added. The properties of the fields should
328
     *                                 be the same as defined by the MDB2 parser.
329
     *
330
     *
331
     *                            remove
332
     *
333
     *                                Associative array with the names of fields to be removed as indexes
334
     *                                 of the array. Currently the values assigned to each entry are ignored.
335
     *                                 An empty array should be used for future compatibility.
336
     *
337
     *                            rename
338
     *
339
     *                                Associative array with the names of fields to be renamed as indexes
340
     *                                 of the array. The value of each entry of the array should be set to
341
     *                                 another associative array with the entry named name with the new
342
     *                                 field name and the entry named Declaration that is expected to contain
343
     *                                 the portion of the field declaration already in DBMS specific SQL code
344
     *                                 as it is used in the CREATE TABLE statement.
345
     *
346
     *                            change
347
     *
348
     *                                Associative array with the names of the fields to be changed as indexes
349
     *                                 of the array. Keep in mind that if it is intended to change either the
350
     *                                 name of a field and any other properties, the change array entries
351
     *                                 should have the new names of the fields as array indexes.
352
     *
353
     *                                The value of each entry of the array should be set to another associative
354
     *                                 array with the properties of the fields to that are meant to be changed as
355
     *                                 array entries. These entries should be assigned to the new values of the
356
     *                                 respective properties. The properties of the fields should be the same
357
     *                                 as defined by the MDB2 parser.
358
     *
359
     *                            Example
360
     *                                array(
361
     *                                    'name' => 'userlist',
362
     *                                    'add' => array(
363
     *                                        'quota' => array(
364
     *                                            'type' => 'integer',
365
     *                                            'unsigned' => 1
366
     *                                        )
367
     *                                    ),
368
     *                                    'remove' => array(
369
     *                                        'file_limit' => array(),
370
     *                                        'time_limit' => array()
371
     *                                    ),
372
     *                                    'change' => array(
373
     *                                        'name' => array(
374
     *                                            'length' => '20',
375
     *                                            'definition' => array(
376
     *                                                'type' => 'text',
377
     *                                                'length' => 20,
378
     *                                            ),
379
     *                                        )
380
     *                                    ),
381
     *                                    'rename' => array(
382
     *                                        'sex' => array(
383
     *                                            'name' => 'gender',
384
     *                                            'definition' => array(
385
     *                                                'type' => 'text',
386
     *                                                'length' => 1,
387
     *                                                'default' => 'M',
388
     *                                            ),
389
     *                                        )
390
     *                                    )
391
     *                                )
392
     *
393
     * @param boolean $check     indicates whether the function should just check if the DBMS driver
394
     *                             can perform the requested table alterations if the value is true or
395
     *                             actually perform them otherwise.
396
     * @access public
397
     *
398
      * @return mixed MDB2_OK on success, a MDB2 error on failure
399
     */
400
    function alterTable($name, $changes, $check)
401
    {
402
        $db =& $this->getDBInstance();
403
        if (PEAR::isError($db)) {
404
            return $db;
405
        }
406
 
407
        return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
408
            'method not implemented', __FUNCTION__);
409
    }
410
 
411
    // }}}
412
    // {{{ listDatabases()
413
 
414
    /**
415
     * list all databases
416
     *
417
     * @return mixed data array on success, a MDB2 error on failure
418
     * @access public
419
     */
420
    function listDatabases()
421
    {
422
        $db =& $this->getDBInstance();
423
        if (PEAR::isError($db)) {
424
            return $db;
425
        }
426
 
427
        return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
428
            'method not implementedd', __FUNCTION__);
429
    }
430
 
431
    // }}}
432
    // {{{ listUsers()
433
 
434
    /**
435
     * list all users
436
     *
437
     * @return mixed data array on success, a MDB2 error on failure
438
     * @access public
439
     */
440
    function listUsers()
441
    {
442
        $db =& $this->getDBInstance();
443
        if (PEAR::isError($db)) {
444
            return $db;
445
        }
446
 
447
        return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
448
            'method not implemented', __FUNCTION__);
449
    }
450
 
451
    // }}}
452
    // {{{ listViews()
453
 
454
    /**
455
     * list all views in the current database
456
     *
457
     * @param string database, the current is default
458
     * @return mixed data array on success, a MDB2 error on failure
459
     * @access public
460
     */
461
    function listViews($database = null)
462
    {
463
        $db =& $this->getDBInstance();
464
        if (PEAR::isError($db)) {
465
            return $db;
466
        }
467
 
468
        return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
469
            'method not implemented', __FUNCTION__);
470
    }
471
 
472
    // }}}
473
    // {{{ listTableViews()
474
 
475
    /**
476
     * list the views in the database that reference a given table
477
     *
478
     * @param string table for which all references views should be found
479
     * @return mixed MDB2_OK on success, a MDB2 error on failure
480
     * @access public
481
     **/
482
    function listTableViews($table)
483
    {
484
        $db =& $this->getDBInstance();
485
        if (PEAR::isError($db)) {
486
            return $db;
487
        }
488
 
489
        return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
490
            'method not implemented', __FUNCTION__);
491
    }
492
 
493
    // }}}
494
    // {{{ listTableTriggers()
495
    /**
496
     * This function will be called to get all triggers of the
497
     * current database ($db->getDatabase())
498
     *
499
     * @access public
500
     * @param  string $table      The name of the table from the
501
     *                            previous database to query against.
502
     * @return mixed Array on success or MDB2 error on failure
503
     */
504
    function listTableTriggers($table = null)
505
    {
506
        $db =& $this->getDBInstance();
507
        if (PEAR::isError($db)) {
508
            return $db;
509
        }
510
 
511
        return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
512
            'method not implemented', __FUNCTION__);
513
    }
514
    // }}}
515
    // {{{ listFunctions()
516
 
517
    /**
518
     * list all functions in the current database
519
     *
520
     * @return mixed data array on success, a MDB2 error on failure
521
     * @access public
522
     */
523
    function listFunctions()
524
    {
525
        $db =& $this->getDBInstance();
526
        if (PEAR::isError($db)) {
527
            return $db;
528
        }
529
 
530
        return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
531
            'method not implemented', __FUNCTION__);
532
    }
533
    // }}}
534
    // {{{ listTables()
535
 
536
    /**
537
     * list all tables in the current database
538
     *
539
     * @param string database, the current is default
540
     * @return mixed data array on success, a MDB2 error on failure
541
     * @access public
542
     */
543
    function listTables($database = null)
544
    {
545
        $db =& $this->getDBInstance();
546
        if (PEAR::isError($db)) {
547
            return $db;
548
        }
549
 
550
        return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
551
            'method not implemented', __FUNCTION__);
552
    }
553
 
554
    // }}}
555
    // {{{ listTableFields()
556
 
557
    /**
558
     * list all fields in a tables in the current database
559
     *
560
     * @param string $table name of table that should be used in method
561
     * @return mixed data array on success, a MDB2 error on failure
562
     * @access public
563
     */
564
    function listTableFields($table)
565
    {
566
        $db =& $this->getDBInstance();
567
        if (PEAR::isError($db)) {
568
            return $db;
569
        }
570
 
571
        return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
572
            'method not implemented', __FUNCTION__);
573
    }
574
 
575
    // }}}
576
    // {{{ createIndex()
577
 
578
    /**
579
     * Get the stucture of a field into an array
580
     *
581
     * @param string    $table         name of the table on which the index is to be created
582
     * @param string    $name         name of the index to be created
583
     * @param array     $definition        associative array that defines properties of the index to be created.
584
     *                                 Currently, only one property named FIELDS is supported. This property
585
     *                                 is also an associative with the names of the index fields as array
586
     *                                 indexes. Each entry of this array is set to another type of associative
587
     *                                 array that specifies properties of the index that are specific to
588
     *                                 each field.
589
     *
590
     *                                Currently, only the sorting property is supported. It should be used
591
     *                                 to define the sorting direction of the index. It may be set to either
592
     *                                 ascending or descending.
593
     *
594
     *                                Not all DBMS support index sorting direction configuration. The DBMS
595
     *                                 drivers of those that do not support it ignore this property. Use the
596
     *                                 function supports() to determine whether the DBMS driver can manage indexes.
597
     *
598
     *                                 Example
599
     *                                    array(
600
     *                                        'fields' => array(
601
     *                                            'user_name' => array(
602
     *                                                'sorting' => 'ascending'
603
     *                                            ),
604
     *                                            'last_login' => array()
605
     *                                        )
606
     *                                    )
607
     * @return mixed MDB2_OK on success, a MDB2 error on failure
608
     * @access public
609
     */
610
    function createIndex($table, $name, $definition)
611
    {
612
        $db =& $this->getDBInstance();
613
        if (PEAR::isError($db)) {
614
            return $db;
615
        }
616
 
617
        $table = $db->quoteIdentifier($table, true);
618
        $name = $db->quoteIdentifier($db->getIndexName($name), true);
619
        $query = "CREATE INDEX $name ON $table";
620
        $fields = array();
621
        foreach (array_keys($definition['fields']) as $field) {
622
            $fields[] = $db->quoteIdentifier($field, true);
623
        }
624
        $query .= ' ('. implode(', ', $fields) . ')';
625
        return $db->exec($query);
626
    }
627
 
628
    // }}}
629
    // {{{ dropIndex()
630
 
631
    /**
632
     * drop existing index
633
     *
634
     * @param string    $table         name of table that should be used in method
635
     * @param string    $name         name of the index to be dropped
636
     * @return mixed MDB2_OK on success, a MDB2 error on failure
637
     * @access public
638
     */
639
    function dropIndex($table, $name)
640
    {
641
        $db =& $this->getDBInstance();
642
        if (PEAR::isError($db)) {
643
            return $db;
644
        }
645
 
646
        $name = $db->quoteIdentifier($db->getIndexName($name), true);
647
        return $db->exec("DROP INDEX $name");
648
    }
649
 
650
    // }}}
651
    // {{{ listTableIndexes()
652
 
653
    /**
654
     * list all indexes in a table
655
     *
656
     * @param string    $table      name of table that should be used in method
657
     * @return mixed data array on success, a MDB2 error on failure
658
     * @access public
659
     */
660
    function listTableIndexes($table)
661
    {
662
        $db =& $this->getDBInstance();
663
        if (PEAR::isError($db)) {
664
            return $db;
665
        }
666
 
667
        return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
668
            'method not implemented', __FUNCTION__);
669
    }
670
 
671
    // }}}
672
    // {{{ createConstraint()
673
 
674
    /**
675
     * create a constraint on a table
676
     *
677
     * @param string    $table         name of the table on which the constraint is to be created
678
     * @param string    $name         name of the constraint to be created
679
     * @param array     $definition        associative array that defines properties of the constraint to be created.
680
     *                                 Currently, only one property named FIELDS is supported. This property
681
     *                                 is also an associative with the names of the constraint fields as array
682
     *                                 constraints. Each entry of this array is set to another type of associative
683
     *                                 array that specifies properties of the constraint that are specific to
684
     *                                 each field.
685
     *
686
     *                                 Example
687
     *                                    array(
688
     *                                        'fields' => array(
689
     *                                            'user_name' => array(),
690
     *                                            'last_login' => array()
691
     *                                        )
692
     *                                    )
693
     * @return mixed MDB2_OK on success, a MDB2 error on failure
694
     * @access public
695
     */
696
    function createConstraint($table, $name, $definition)
697
    {
698
        $db =& $this->getDBInstance();
699
        if (PEAR::isError($db)) {
700
            return $db;
701
        }
702
        $table = $db->quoteIdentifier($table, true);
703
        $name = $db->quoteIdentifier($db->getIndexName($name), true);
704
        $query = "ALTER TABLE $table ADD CONSTRAINT $name";
705
        if (!empty($definition['primary'])) {
706
            $query.= ' PRIMARY KEY';
707
        } elseif (!empty($definition['unique'])) {
708
            $query.= ' UNIQUE';
709
        }
710
        $fields = array();
711
        foreach (array_keys($definition['fields']) as $field) {
712
            $fields[] = $db->quoteIdentifier($field, true);
713
        }
714
        $query .= ' ('. implode(', ', $fields) . ')';
715
        return $db->exec($query);
716
    }
717
 
718
    // }}}
719
    // {{{ dropConstraint()
720
 
721
    /**
722
     * drop existing constraint
723
     *
724
     * @param string    $table        name of table that should be used in method
725
     * @param string    $name         name of the constraint to be dropped
726
     * @param string    $primary      hint if the constraint is primary
727
     * @return mixed MDB2_OK on success, a MDB2 error on failure
728
     * @access public
729
     */
730
    function dropConstraint($table, $name, $primary = false)
731
    {
732
        $db =& $this->getDBInstance();
733
        if (PEAR::isError($db)) {
734
            return $db;
735
        }
736
 
737
        $table = $db->quoteIdentifier($table, true);
738
        $name = $db->quoteIdentifier($db->getIndexName($name), true);
739
        return $db->exec("ALTER TABLE $table DROP CONSTRAINT $name");
740
    }
741
 
742
    // }}}
743
    // {{{ listTableConstraints()
744
 
745
    /**
746
     * list all constraints in a table
747
     *
748
     * @param string    $table      name of table that should be used in method
749
     * @return mixed data array on success, a MDB2 error on failure
750
     * @access public
751
     */
752
    function listTableConstraints($table)
753
    {
754
        $db =& $this->getDBInstance();
755
        if (PEAR::isError($db)) {
756
            return $db;
757
        }
758
 
759
        return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
760
            'method not implemented', __FUNCTION__);
761
    }
762
 
763
    // }}}
764
    // {{{ createSequence()
765
 
766
    /**
767
     * create sequence
768
     *
769
     * @param string    $seq_name     name of the sequence to be created
770
     * @param string    $start         start value of the sequence; default is 1
771
     * @return mixed MDB2_OK on success, a MDB2 error on failure
772
     * @access public
773
     */
774
    function createSequence($seq_name, $start = 1)
775
    {
776
        $db =& $this->getDBInstance();
777
        if (PEAR::isError($db)) {
778
            return $db;
779
        }
780
 
781
        return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
782
            'method not implemented', __FUNCTION__);
783
    }
784
 
785
    // }}}
786
    // {{{ dropSequence()
787
 
788
    /**
789
     * drop existing sequence
790
     *
791
     * @param string    $seq_name     name of the sequence to be dropped
792
     * @return mixed MDB2_OK on success, a MDB2 error on failure
793
     * @access public
794
     */
795
    function dropSequence($name)
796
    {
797
        $db =& $this->getDBInstance();
798
        if (PEAR::isError($db)) {
799
            return $db;
800
        }
801
 
802
        return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
803
            'method not implemented', __FUNCTION__);
804
    }
805
 
806
    // }}}
807
    // {{{ listSequences()
808
 
809
    /**
810
     * list all sequences in the current database
811
     *
812
     * @param string database, the current is default
813
     * @return mixed data array on success, a MDB2 error on failure
814
     * @access public
815
     */
816
    function listSequences($database = null)
817
    {
818
        $db =& $this->getDBInstance();
819
        if (PEAR::isError($db)) {
820
            return $db;
821
        }
822
 
823
        return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
824
            'method not implemented', __FUNCTION__);
825
    }
826
}
827
 
828
?>