Subversion-Projekte lars-tiefland.php_share

Revision

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