ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
Common.php
Go to the documentation of this file.
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
62{
63 // {{{ getFieldDeclarationList()
64
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
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
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
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
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
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'])) {
239 }
240 $result .= " TABLE $name ($query_fields)";
241 return $result;
242 }
243
244 // }}}
245 // {{{ _getTemporaryTableQuery()
246
262 {
263 return 'TEMPORARY';
264 }
265
266 // }}}
267 // {{{ createTable()
268
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
317 public function getTableCreationQuery($name, $fields, $options = array()) {
318 return $this->_getCreateTableQuery($name, $fields, $options);
319 }
320
321 // }}}
322 // {{{ dropTable()
323
331 function dropTable($name)
332 {
333 $db =& $this->getDBInstance();
334 if (PEAR::isError($db)) {
335 return $db;
336 }
337
338 $name = $db->quoteIdentifier($name, true);
339 return $db->exec("DROP TABLE $name");
340 }
341
342 // }}}
343 // {{{ alterTable()
344
435 function alterTable($name, $changes, $check)
436 {
437 $db =& $this->getDBInstance();
438 if (PEAR::isError($db)) {
439 return $db;
440 }
441
442 return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
443 'method not implemented', __FUNCTION__);
444 }
445
446 // }}}
447 // {{{ listDatabases()
448
455 function listDatabases()
456 {
457 $db =& $this->getDBInstance();
458 if (PEAR::isError($db)) {
459 return $db;
460 }
461
462 return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
463 'method not implementedd', __FUNCTION__);
464 }
465
466 // }}}
467 // {{{ listUsers()
468
475 function listUsers()
476 {
477 $db =& $this->getDBInstance();
478 if (PEAR::isError($db)) {
479 return $db;
480 }
481
482 return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
483 'method not implemented', __FUNCTION__);
484 }
485
486 // }}}
487 // {{{ listViews()
488
498 function listViews($database = null)
499 {
500 $db =& $this->getDBInstance();
501 if (PEAR::isError($db)) {
502 return $db;
503 }
504
505 return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
506 'method not implemented', __FUNCTION__);
507 }
508
509 // }}}
510 // {{{ listTableViews()
511
519 function listTableViews($table)
520 {
521 $db =& $this->getDBInstance();
522 if (PEAR::isError($db)) {
523 return $db;
524 }
525
526 return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
527 'method not implemented', __FUNCTION__);
528 }
529
530 // }}}
531 // {{{ listTableTriggers()
532
540 function listTableTriggers($table = null)
541 {
542 $db =& $this->getDBInstance();
543 if (PEAR::isError($db)) {
544 return $db;
545 }
546
547 return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
548 'method not implemented', __FUNCTION__);
549 }
550
551 // }}}
552 // {{{ listFunctions()
553
560 function listFunctions()
561 {
562 $db =& $this->getDBInstance();
563 if (PEAR::isError($db)) {
564 return $db;
565 }
566
567 return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
568 'method not implemented', __FUNCTION__);
569 }
570
571 // }}}
572 // {{{ listTables()
573
583 function listTables($database = null)
584 {
585 $db =& $this->getDBInstance();
586 if (PEAR::isError($db)) {
587 return $db;
588 }
589
590 return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
591 'method not implemented', __FUNCTION__);
592 }
593
594 // }}}
595 // {{{ listTableFields()
596
604 function listTableFields($table)
605 {
606 $db =& $this->getDBInstance();
607 if (PEAR::isError($db)) {
608 return $db;
609 }
610
611 return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
612 'method not implemented', __FUNCTION__);
613 }
614
615 // }}}
616 // {{{ createIndex()
617
650 function createIndex($table, $name, $definition)
651 {
652 $db =& $this->getDBInstance();
653 if (PEAR::isError($db)) {
654 return $db;
655 }
656
657 $table = $db->quoteIdentifier($table, true);
658 $name = $db->quoteIdentifier($db->getIndexName($name), true);
659 $query = "CREATE INDEX $name ON $table";
660 $fields = array();
661 foreach (array_keys($definition['fields']) as $field) {
662 $fields[] = $db->quoteIdentifier($field, true);
663 }
664 $query .= ' ('. implode(', ', $fields) . ')';
665 return $db->exec($query);
666 }
667
668 // }}}
669 // {{{ dropIndex()
670
679 function dropIndex($table, $name)
680 {
681 $db =& $this->getDBInstance();
682 if (PEAR::isError($db)) {
683 return $db;
684 }
685
686 $name = $db->quoteIdentifier($db->getIndexName($name), true);
687 return $db->exec("DROP INDEX $name");
688 }
689
690 // }}}
691 // {{{ listTableIndexes()
692
700 function listTableIndexes($table)
701 {
702 $db =& $this->getDBInstance();
703 if (PEAR::isError($db)) {
704 return $db;
705 }
706
707 return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
708 'method not implemented', __FUNCTION__);
709 }
710
711 // }}}
712 // {{{ createConstraint()
713
736 function createConstraint($table, $name, $definition)
737 {
738 $db =& $this->getDBInstance();
739 if (PEAR::isError($db)) {
740 return $db;
741 }
742 $table = $db->quoteIdentifier($table, true);
743 $name = $db->quoteIdentifier($db->getIndexName($name), true);
744 $query = "ALTER TABLE $table ADD CONSTRAINT $name";
745 if (!empty($definition['primary'])) {
746 $query.= ' PRIMARY KEY';
747 } elseif (!empty($definition['unique'])) {
748 $query.= ' UNIQUE';
749 }
750 $fields = array();
751 foreach (array_keys($definition['fields']) as $field) {
752 $fields[] = $db->quoteIdentifier($field, true);
753 }
754 $query .= ' ('. implode(', ', $fields) . ')';
755 return $db->exec($query);
756 }
757
758 // }}}
759 // {{{ dropConstraint()
760
770 function dropConstraint($table, $name, $primary = false)
771 {
772 $db =& $this->getDBInstance();
773 if (PEAR::isError($db)) {
774 return $db;
775 }
776
777 $table = $db->quoteIdentifier($table, true);
778 $name = $db->quoteIdentifier($db->getIndexName($name), true);
779 return $db->exec("ALTER TABLE $table DROP CONSTRAINT $name");
780 }
781
782 // }}}
783 // {{{ listTableConstraints()
784
792 function listTableConstraints($table)
793 {
794 $db =& $this->getDBInstance();
795 if (PEAR::isError($db)) {
796 return $db;
797 }
798
799 return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
800 'method not implemented', __FUNCTION__);
801 }
802
803 // }}}
804 // {{{ createSequence()
805
814 function createSequence($seq_name, $start = 1)
815 {
816 $db =& $this->getDBInstance();
817 if (PEAR::isError($db)) {
818 return $db;
819 }
820
821 return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
822 'method not implemented', __FUNCTION__);
823 }
824
825 // }}}
826 // {{{ dropSequence()
827
835 function dropSequence($name)
836 {
837 $db =& $this->getDBInstance();
838 if (PEAR::isError($db)) {
839 return $db;
840 }
841
842 return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
843 'method not implemented', __FUNCTION__);
844 }
845
846 // }}}
847 // {{{ listSequences()
848
858 function listSequences($database = null)
859 {
860 $db =& $this->getDBInstance();
861 if (PEAR::isError($db)) {
862 return $db;
863 }
864
865 return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
866 'method not implemented', __FUNCTION__);
867 }
868
869 // }}}
870}
871?>
$result
const MDB2_ERROR_UNSUPPORTED
Definition: MDB2.php:78
const MDB2_ERROR_CANNOT_CREATE
Definition: MDB2.php:87
const MDB2_ERROR_NEED_MORE_DATA
Definition: MDB2.php:92
An exception for terminatinating execution or to throw for unit testing.
alterTable($name, $changes, $check)
alter an existing table
Definition: Common.php:435
listTableViews($table)
list the views in the database that reference a given table
Definition: Common.php:519
listTableConstraints($table)
list all constraints in a table
Definition: Common.php:792
listTableFields($table)
list all fields in a table in the current database
Definition: Common.php:604
dropSequence($name)
drop existing sequence
Definition: Common.php:835
_fixIndexName($idx)
Removes any formatting in an index name using the 'idxname_format' option.
Definition: Common.php:144
listTables($database=null)
list all tables in the current database
Definition: Common.php:583
listTableTriggers($table=null)
list all triggers in the database that reference a given table
Definition: Common.php:540
dropConstraint($table, $name, $primary=false)
drop existing constraint
Definition: Common.php:770
createConstraint($table, $name, $definition)
create a constraint on a table
Definition: Common.php:736
listDatabases()
list all databases
Definition: Common.php:455
createDatabase($database)
create a new database
Definition: Common.php:169
createIndex($table, $name, $definition)
Get the stucture of a field into an array.
Definition: Common.php:650
listViews($database=null)
list all views in the current database
Definition: Common.php:498
listUsers()
list all users
Definition: Common.php:475
_getTemporaryTableQuery()
A method to return the required SQL string that fits between CREATE ... TABLE to create the table as ...
Definition: Common.php:261
dropDatabase($database)
drop an existing database
Definition: Common.php:190
_getCreateTableQuery($name, $fields, $options=array())
Create a basic SQL query for a new table creation.
Definition: Common.php:212
listSequences($database=null)
list all sequences in the current database
Definition: Common.php:858
createSequence($seq_name, $start=1)
create sequence
Definition: Common.php:814
dropIndex($table, $name)
drop existing index
Definition: Common.php:679
getTableCreationQuery($name, $fields, $options=array())
PATCH: For Testcases Only.
Definition: Common.php:317
dropTable($name)
drop an existing table
Definition: Common.php:331
getFieldDeclarationList($fields)
Get declaration of a number of field in bulk.
Definition: Common.php:84
listTableIndexes($table)
list all indexes in a table
Definition: Common.php:700
createTable($name, $fields, $options=array())
create a new table
Definition: Common.php:301
_fixSequenceName($sqn, $check=false)
Removes any formatting in an sequence name using the 'seqname_format' option.
Definition: Common.php:116
listFunctions()
list all functions in the current database
Definition: Common.php:560
& getDBInstance()
Get the instance of MDB2 associated with the module instance.
Definition: MDB2.php:4238
isError($data, $code=null)
Tell whether a value is a PEAR error.
Definition: PEAR.php:280
if(!is_array($argv)) $options