Source for file Common.php
Documentation is available at Common.php
// +----------------------------------------------------------------------+
// | PHP versions 4 and 5 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1998-2008 Manuel Lemos, Tomas V.V.Cox, |
// | Stig. S. Bakken, Lukas Smith |
// | All rights reserved. |
// +----------------------------------------------------------------------+
// | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB |
// | API as well as database abstraction for PHP applications. |
// | This LICENSE is in the BSD license style. |
// | Redistribution and use in source and binary forms, with or without |
// | modification, are permitted provided that the following conditions |
// | Redistributions of source code must retain the above copyright |
// | notice, this list of conditions and the following disclaimer. |
// | Redistributions in binary form must reproduce the above copyright |
// | notice, this list of conditions and the following disclaimer in the |
// | documentation and/or other materials provided with the distribution. |
// | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken, |
// | Lukas Smith nor the names of his contributors may be used to endorse |
// | or promote products derived from this software without specific prior|
// | written permission. |
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
// | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
// | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
// | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
// | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
// | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
// | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
// | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
// | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
// | POSSIBILITY OF SUCH DAMAGE. |
// +----------------------------------------------------------------------+
// | Authors: Lukas Smith <smith@pooteeweet.org> |
// | Lorenzo Alberton <l.alberton@quipo.it> |
// +----------------------------------------------------------------------+
// $Id: Common.php 327310 2012-08-27 15:16:18Z danielc $
* @author Lukas Smith <smith@pooteeweet.org>
* @author Lorenzo Alberton <l.alberton@quipo.it>
* Base class for the management modules that is extended by each MDB2 driver
* To load this module in the MDB2 object:
* $mdb->loadModule('Manager');
* @author Lukas Smith <smith@pooteeweet.org>
// {{{ splitTableSchema()
* Split the "[owner|schema].table" notation into an array
* @param string $table [schema and] table name
* @return array array(schema, table)
function splitTableSchema ($table)
if (strpos($table, '.') !== false ) {
return array (null , $table);
// {{{ getFieldDeclarationList()
* Get declaration of a number of field in bulk
* @param array $fields a multidimensional associative array.
* The first dimension determines the field name, while the second
* dimension is keyed with the name of the properties
* of the field being declared as array indexes. Currently, the types
* of supported field properties are as follows:
* Boolean value to be used as default for this field.
* Boolean flag that indicates whether this field is constrained
* @return mixed string on success, a MDB2 error on failure
if (!is_array($fields) || empty ($fields)) {
'missing any fields', __FUNCTION__ );
foreach ($fields as $field_name => $field) {
$query = $db->getDeclaration ($field['type'], $field_name, $field);
$query_fields[] = $query;
return implode(', ', $query_fields);
// {{{ _fixSequenceName()
* Removes any formatting in an sequence name using the 'seqname_format' option
* @param string $sqn string that containts name of a potential sequence
* @param bool $check if only formatted sequences should be returned
* @return string name of the sequence with possible formatting removed
$seq_pattern = '/^'. preg_replace('/%s/', '([a-z0-9_]+)', $db->options ['seqname_format']). '$/i';
if ($seq_name && !strcasecmp($sqn, $db->getSequenceName ($seq_name))) {
* Removes any formatting in an index name using the 'idxname_format' option
* @param string $idx string that containts name of anl index
* @return string name of the index with eventual formatting removed
$idx_pattern = '/^'. preg_replace('/%s/', '([a-z0-9_]+)', $db->options ['idxname_format']). '$/i';
if ($idx_name && !strcasecmp($idx, $db->getIndexName ($idx_name))) {
* @param string $name name of the database that should be created
* @param array $options array with charset, collation info
* @return mixed MDB2_OK on success, a MDB2 error on failure
'method not implemented', __FUNCTION__ );
* alter an existing database
* @param string $name name of the database that should be created
* @param array $options array with charset, collation info
* @return mixed MDB2_OK on success, a MDB2 error on failure
'method not implemented', __FUNCTION__ );
* drop an existing database
* @param string $name name of the database that should be dropped
* @return mixed MDB2_OK on success, a MDB2 error on failure
'method not implemented', __FUNCTION__ );
// {{{ _getCreateTableQuery()
* Create a basic SQL query for a new table creation
* @param string $name Name of the database that should be created
* @param array $fields Associative array that contains the definition of each field of the new table
* @param array $options An associative array of table options
* @return mixed string (the SQL query) on success, a MDB2 error on failure
function _getCreateTableQuery ($name, $fields, $options = array ())
'no valid table name specified', __FUNCTION__ );
'no fields specified for table "'. $name. '"', __FUNCTION__ );
if (!empty ($options['primary'])) {
$query_fields.= ', PRIMARY KEY ('. implode(', ', array_keys($options['primary'])). ')';
$name = $db->quoteIdentifier ($name, true );
if (!empty ($options['temporary'])) {
$result .= $this->_getTemporaryTableQuery ();
$result .= " TABLE $name ($query_fields)";
// {{{ _getTemporaryTableQuery()
* A method to return the required SQL string that fits between CREATE ... TABLE
* to create the table as a temporary table.
* Should be overridden in driver classes to return the correct string for the
* specific database type.
* The default is to return the string "TEMPORARY" - this will result in a
* SQL error for any database that does not support temporary tables, or that
* requires a different SQL command from "CREATE TEMPORARY TABLE".
* @return string The string required to be placed between "CREATE" and "TABLE"
* to generate a temporary table, if possible.
function _getTemporaryTableQuery ()
* @param string $name Name of the database that should be created
* @param array $fields Associative array that contains the definition of each field of the new table
* The indexes of the array entries are the names of the fields of the table an
* the array entry values are associative arrays like those that are meant to be
* passed with the field definitions to get[Type]Declaration() functions.
* @param array $options An associative array of table options:
* 'temporary' => true|false,
* @return mixed MDB2_OK on success, a MDB2 error on failure
function createTable($name, $fields, $options = array ())
$query = $this->_getCreateTableQuery ($name, $fields, $options);
$result = $db->exec ($query);
* @param string $name name of the table that should be dropped
* @return mixed MDB2_OK on success, a MDB2 error on failure
$name = $db->quoteIdentifier ($name, true );
$result = $db->exec (" DROP TABLE $name" );
* Truncate an existing table (if the TRUNCATE TABLE syntax is not supported,
* it falls back to a DELETE FROM TABLE query)
* @param string $name name of the table that should be truncated
* @return mixed MDB2_OK on success, a MDB2 error on failure
$name = $db->quoteIdentifier ($name, true );
$result = $db->exec (" DELETE FROM $name" );
* Optimize (vacuum) all the tables in the db (or only the specified table)
* and optionally run ANALYZE.
* @param string $table table name (all the tables if empty)
* @param array $options an array with driver-specific options:
* - timeout [int] (in seconds) [mssql-only]
* - analyze [boolean] [pgsql and mysql]
* - full [boolean] [pgsql-only]
* - freeze [boolean] [pgsql-only]
* @return mixed MDB2_OK success, a MDB2 error on failure
function vacuum($table = null , $options = array ())
'method not implemented', __FUNCTION__ );
* alter an existing table
* @param string $name name of the table that is intended to be changed.
* @param array $changes associative array that contains the details of each type
* of change that is intended to be performed. The types of
* changes that are currently supported are defined as follows:
* New name for the table.
* Associative array with the names of fields to be added as
* indexes of the array. The value of each entry of the array
* should be set to another associative array with the properties
* of the fields to be added. The properties of the fields should
* be the same as defined by the MDB2 parser.
* Associative array with the names of fields to be removed as indexes
* of the array. Currently the values assigned to each entry are ignored.
* An empty array should be used for future compatibility.
* Associative array with the names of fields to be renamed as indexes
* of the array. The value of each entry of the array should be set to
* another associative array with the entry named name with the new
* field name and the entry named Declaration that is expected to contain
* the portion of the field declaration already in DBMS specific SQL code
* as it is used in the CREATE TABLE statement.
* Associative array with the names of the fields to be changed as indexes
* of the array. Keep in mind that if it is intended to change either the
* name of a field and any other properties, the change array entries
* should have the new names of the fields as array indexes.
* The value of each entry of the array should be set to another associative
* array with the properties of the fields to that are meant to be changed as
* array entries. These entries should be assigned to the new values of the
* respective properties. The properties of the fields should be the same
* as defined by the MDB2 parser.
* 'file_limit' => array(),
* 'time_limit' => array()
* @param boolean $check indicates whether the function should just check if the DBMS driver
* can perform the requested table alterations if the value is true or
* actually perform them otherwise.
* @return mixed MDB2_OK on success, a MDB2 error on failure
'method not implemented', __FUNCTION__ );
* @return mixed array of database names on success, a MDB2 error on failure
'method not implementedd', __FUNCTION__ );
* @return mixed array of user names on success, a MDB2 error on failure
'method not implemented', __FUNCTION__ );
* list all views in the current database
* @param string database, the current is default
* NB: not all the drivers can get the view names from
* a database other than the current one
* @return mixed array of view names on success, a MDB2 error on failure
'method not implemented', __FUNCTION__ );
* list the views in the database that reference a given table
* @param string table for which all referenced views should be found
* @return mixed array of view names on success, a MDB2 error on failure
'method not implemented', __FUNCTION__ );
// {{{ listTableTriggers()
* list all triggers in the database that reference a given table
* @param string table for which all referenced triggers should be found
* @return mixed array of trigger names on success, a MDB2 error on failure
'method not implemented', __FUNCTION__ );
* list all functions in the current database
* @return mixed array of function names on success, a MDB2 error on failure
'method not implemented', __FUNCTION__ );
* list all tables in the current database
* @param string database, the current is default.
* NB: not all the drivers can get the table names from
* a database other than the current one
* @return mixed array of table names on success, a MDB2 error on failure
'method not implemented', __FUNCTION__ );
* list all fields in a table in the current database
* @param string $table name of table that should be used in method
* @return mixed array of field names on success, a MDB2 error on failure
'method not implemented', __FUNCTION__ );
* Get the stucture of a field into an array
* @param string $table name of the table on which the index is to be created
* @param string $name name of the index to be created
* @param array $definition associative array that defines properties of the index to be created.
* Currently, only one property named FIELDS is supported. This property
* is also an associative with the names of the index fields as array
* indexes. Each entry of this array is set to another type of associative
* array that specifies properties of the index that are specific to
* Currently, only the sorting property is supported. It should be used
* to define the sorting direction of the index. It may be set to either
* ascending or descending.
* Not all DBMS support index sorting direction configuration. The DBMS
* drivers of those that do not support it ignore this property. Use the
* function supports() to determine whether the DBMS driver can manage indexes.
* 'sorting' => 'ascending'
* 'last_login' => array()
* @return mixed MDB2_OK on success, a MDB2 error on failure
$table = $db->quoteIdentifier ($table, true );
$name = $db->quoteIdentifier ($db->getIndexName ($name), true );
$query = " CREATE INDEX $name ON $table";
foreach (array_keys($definition['fields']) as $field) {
$fields[] = $db->quoteIdentifier ($field, true );
$query .= ' ('. implode(', ', $fields) . ')';
$result = $db->exec ($query);
* @param string $table name of table that should be used in method
* @param string $name name of the index to be dropped
* @return mixed MDB2_OK on success, a MDB2 error on failure
$name = $db->quoteIdentifier ($db->getIndexName ($name), true );
$result = $db->exec (" DROP INDEX $name" );
// {{{ listTableIndexes()
* list all indexes in a table
* @param string $table name of table that should be used in method
* @return mixed array of index names on success, a MDB2 error on failure
'method not implemented', __FUNCTION__ );
// {{{ _getAdvancedFKOptions()
* Return the FOREIGN KEY query section dealing with non-standard options
* as MATCH, INITIALLY DEFERRED, ON UPDATE, ...
* @param array $definition
// {{{ createConstraint()
* create a constraint on a table
* @param string $table name of the table on which the constraint is to be created
* @param string $name name of the constraint to be created
* @param array $definition associative array that defines properties of the constraint to be created.
* The full structure of the array looks like this:
* [field1name] => array() // one entry per each field covered
* [field2name] => array() // by the index
* [field1name] => array( //one entry per each referenced field
* [initiallydeferred] => 0
* [onupdate] => CASCADE|RESTRICT|SET NULL|SET DEFAULT|NO ACTION
* [ondelete] => CASCADE|RESTRICT|SET NULL|SET DEFAULT|NO ACTION
* [match] => SIMPLE|PARTIAL|FULL
* @return mixed MDB2_OK on success, a MDB2 error on failure
$table = $db->quoteIdentifier ($table, true );
$name = $db->quoteIdentifier ($db->getIndexName ($name), true );
$query = " ALTER TABLE $table ADD CONSTRAINT $name";
if (!empty ($definition['primary'])) {
} elseif (!empty ($definition['unique'])) {
} elseif (!empty ($definition['foreign'])) {
foreach (array_keys($definition['fields']) as $field) {
$fields[] = $db->quoteIdentifier ($field, true );
$query .= ' ('. implode(', ', $fields) . ')';
if (!empty ($definition['foreign'])) {
$query.= ' REFERENCES ' . $db->quoteIdentifier ($definition['references']['table'], true );
$referenced_fields = array ();
foreach (array_keys($definition['references']['fields']) as $field) {
$referenced_fields[] = $db->quoteIdentifier ($field, true );
$query .= ' ('. implode(', ', $referenced_fields) . ')';
$result = $db->exec ($query);
* drop existing constraint
* @param string $table name of table that should be used in method
* @param string $name name of the constraint to be dropped
* @param string $primary hint if the constraint is primary
* @return mixed MDB2_OK on success, a MDB2 error on failure
$table = $db->quoteIdentifier ($table, true );
$name = $db->quoteIdentifier ($db->getIndexName ($name), true );
$result = $db->exec (" ALTER TABLE $table DROP CONSTRAINT $name" );
// {{{ listTableConstraints()
* list all constraints in a table
* @param string $table name of table that should be used in method
* @return mixed array of constraint names on success, a MDB2 error on failure
'method not implemented', __FUNCTION__ );
* @param string $seq_name name of the sequence to be created
* @param string $start start value of the sequence; default is 1
* @return mixed MDB2_OK on success, a MDB2 error on failure
'method not implemented', __FUNCTION__ );
* @param string $seq_name name of the sequence to be dropped
* @return mixed MDB2_OK on success, a MDB2 error on failure
'method not implemented', __FUNCTION__ );
* list all sequences in the current database
* @param string database, the current is default
* NB: not all the drivers can get the sequence names from
* a database other than the current one
* @return mixed array of sequence names on success, a MDB2 error on failure
'method not implemented', __FUNCTION__ );
Documentation generated on Mon, 29 Oct 2012 16:00:07 +0000 by phpDocumentor 1.4.3. PEAR Logo Copyright © PHP Group 2004.
|