ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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
314 // }}}
315 // {{{ dropTable()
316
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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?>
$result
const MDB2_ERROR_UNSUPPORTED
Definition: MDB2.php:73
const MDB2_ERROR_CANNOT_CREATE
Definition: MDB2.php:82
const MDB2_ERROR_NEED_MORE_DATA
Definition: MDB2.php:87
alterTable($name, $changes, $check)
alter an existing table
Definition: Common.php:428
listTableViews($table)
list the views in the database that reference a given table
Definition: Common.php:512
listTableConstraints($table)
list all constraints in a table
Definition: Common.php:785
listTableFields($table)
list all fields in a table in the current database
Definition: Common.php:597
dropSequence($name)
drop existing sequence
Definition: Common.php:828
_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:576
listTableTriggers($table=null)
list all triggers in the database that reference a given table
Definition: Common.php:533
dropConstraint($table, $name, $primary=false)
drop existing constraint
Definition: Common.php:763
createConstraint($table, $name, $definition)
create a constraint on a table
Definition: Common.php:729
listDatabases()
list all databases
Definition: Common.php:448
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:643
listViews($database=null)
list all views in the current database
Definition: Common.php:491
listUsers()
list all users
Definition: Common.php:468
_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:851
createSequence($seq_name, $start=1)
create sequence
Definition: Common.php:807
dropIndex($table, $name)
drop existing index
Definition: Common.php:672
dropTable($name)
drop an existing table
Definition: Common.php:324
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:693
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:553
& getDBInstance()
Get the instance of MDB2 associated with the module instance.
Definition: MDB2.php:4206
isError($data, $code=null)
Tell whether a value is a PEAR error.
Definition: PEAR.php:279
if(!is_array($argv)) $options