12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472 |
- <?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 |
- // | are met: |
- // | |
- // | 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. |
- // +----------------------------------------------------------------------+
- // | Author: Lukas Smith <smith@pooteeweet.org> |
- // +----------------------------------------------------------------------+
- //
- // $Id$
- //
- require_once 'MDB2/Driver/Manager/Common.php';
- /**
- * MDB2 MySQLi driver for the management modules
- *
- * @package MDB2
- * @category Database
- * @author Lukas Smith <smith@pooteeweet.org>
- */
- class MDB2_Driver_Manager_mysqli extends MDB2_Driver_Manager_Common
- {
- // }}}
- // {{{ createDatabase()
- /**
- * create a new 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
- * @access public
- */
- function createDatabase($name, $options = array())
- {
- $db = $this->getDBInstance();
- if (MDB2::isError($db)) {
- return $db;
- }
- $name = $db->quoteIdentifier($name, true);
- $query = 'CREATE DATABASE ' . $name;
- if (!empty($options['charset'])) {
- $query .= ' DEFAULT CHARACTER SET ' . $db->quote($options['charset'], 'text');
- }
- if (!empty($options['collation'])) {
- $query .= ' COLLATE ' . $db->quote($options['collation'], 'text');
- }
- return $db->standaloneQuery($query, null, true);
- }
- // }}}
- // {{{ alterDatabase()
- /**
- * alter an existing database
- *
- * @param string $name name of the database that is intended to be changed
- * @param array $options array with charset, collation info
- *
- * @return mixed MDB2_OK on success, a MDB2 error on failure
- * @access public
- */
- function alterDatabase($name, $options = array())
- {
- $db = $this->getDBInstance();
- if (MDB2::isError($db)) {
- return $db;
- }
- $query = 'ALTER DATABASE '. $db->quoteIdentifier($name, true);
- if (!empty($options['charset'])) {
- $query .= ' DEFAULT CHARACTER SET ' . $db->quote($options['charset'], 'text');
- }
- if (!empty($options['collation'])) {
- $query .= ' COLLATE ' . $db->quote($options['collation'], 'text');
- }
- return $db->standaloneQuery($query, null, true);
- }
- // }}}
- // {{{ dropDatabase()
- /**
- * 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
- * @access public
- */
- function dropDatabase($name)
- {
- $db = $this->getDBInstance();
- if (MDB2::isError($db)) {
- return $db;
- }
- $name = $db->quoteIdentifier($name, true);
- $query = "DROP DATABASE $name";
- return $db->standaloneQuery($query, null, true);
- }
- // }}}
- // {{{ _getAdvancedFKOptions()
- /**
- * Return the FOREIGN KEY query section dealing with non-standard options
- * as MATCH, INITIALLY DEFERRED, ON UPDATE, ...
- *
- * @param array $definition
- * @return string
- * @access protected
- */
- function _getAdvancedFKOptions($definition)
- {
- $query = '';
- if (!empty($definition['match'])) {
- $query .= ' MATCH '.$definition['match'];
- }
- if (!empty($definition['onupdate'])) {
- $query .= ' ON UPDATE '.$definition['onupdate'];
- }
- if (!empty($definition['ondelete'])) {
- $query .= ' ON DELETE '.$definition['ondelete'];
- }
- return $query;
- }
- // }}}
- // {{{ createTable()
- /**
- * create a new table
- *
- * @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.
- * array(
- * 'id' => array(
- * 'type' => 'integer',
- * 'unsigned' => 1
- * 'notnull' => 1
- * 'default' => 0
- * ),
- * 'name' => array(
- * 'type' => 'text',
- * 'length' => 12
- * ),
- * 'password' => array(
- * 'type' => 'text',
- * 'length' => 12
- * )
- * );
- * @param array $options An associative array of table options:
- * array(
- * 'comment' => 'Foo',
- * 'charset' => 'utf8',
- * 'collate' => 'utf8_unicode_ci',
- * 'type' => 'innodb',
- * );
- *
- * @return mixed MDB2_OK on success, a MDB2 error on failure
- * @access public
- */
- function createTable($name, $fields, $options = array())
- {
- $db = $this->getDBInstance();
- if (MDB2::isError($db)) {
- return $db;
- }
- // if we have an AUTO_INCREMENT column and a PK on more than one field,
- // we have to handle it differently...
- $autoincrement = null;
- if (empty($options['primary'])) {
- $pk_fields = array();
- foreach ($fields as $fieldname => $def) {
- if (!empty($def['primary'])) {
- $pk_fields[$fieldname] = true;
- }
- if (!empty($def['autoincrement'])) {
- $autoincrement = $fieldname;
- }
- }
- if ((null !== $autoincrement) && count($pk_fields) > 1) {
- $options['primary'] = $pk_fields;
- } else {
- // the PK constraint is on max one field => OK
- $autoincrement = null;
- }
- }
- $query = $this->_getCreateTableQuery($name, $fields, $options);
- if (MDB2::isError($query)) {
- return $query;
- }
- if (null !== $autoincrement) {
- // we have to remove the PK clause added by _getIntegerDeclaration()
- $query = str_replace('AUTO_INCREMENT PRIMARY KEY', 'AUTO_INCREMENT', $query);
- }
- $options_strings = array();
- if (!empty($options['comment'])) {
- $options_strings['comment'] = 'COMMENT = '.$db->quote($options['comment'], 'text');
- }
- if (!empty($options['charset'])) {
- $options_strings['charset'] = 'DEFAULT CHARACTER SET '.$options['charset'];
- if (!empty($options['collate'])) {
- $options_strings['charset'].= ' COLLATE '.$options['collate'];
- }
- }
- $type = false;
- if (!empty($options['type'])) {
- $type = $options['type'];
- } elseif ($db->options['default_table_type']) {
- $type = $db->options['default_table_type'];
- }
- if ($type) {
- $options_strings[] = "ENGINE = $type";
- }
- if (!empty($options_strings)) {
- $query .= ' '.implode(' ', $options_strings);
- }
- $result = $db->exec($query);
- if (MDB2::isError($result)) {
- return $result;
- }
- return MDB2_OK;
- }
- // }}}
- // {{{ dropTable()
- /**
- * drop an existing table
- *
- * @param string $name name of the table that should be dropped
- * @return mixed MDB2_OK on success, a MDB2 error on failure
- * @access public
- */
- function dropTable($name)
- {
- $db = $this->getDBInstance();
- if (MDB2::isError($db)) {
- return $db;
- }
- //delete the triggers associated to existing FK constraints
- $constraints = $this->listTableConstraints($name);
- if (!MDB2::isError($constraints) && !empty($constraints)) {
- $db->loadModule('Reverse', null, true);
- foreach ($constraints as $constraint) {
- $definition = $db->reverse->getTableConstraintDefinition($name, $constraint);
- if (!MDB2::isError($definition) && !empty($definition['foreign'])) {
- $result = $this->_dropFKTriggers($name, $constraint, $definition['references']['table']);
- if (MDB2::isError($result)) {
- return $result;
- }
- }
- }
- }
- return parent::dropTable($name);
- }
- // }}}
- // {{{ truncateTable()
- /**
- * 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
- * @access public
- */
- function truncateTable($name)
- {
- $db = $this->getDBInstance();
- if (MDB2::isError($db)) {
- return $db;
- }
- $name = $db->quoteIdentifier($name, true);
- $result = $db->exec("TRUNCATE TABLE $name");
- if (MDB2::isError($result)) {
- return $result;
- }
- return MDB2_OK;
- }
- // }}}
- // {{{ vacuum()
- /**
- * 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
- * @access public
- */
- function vacuum($table = null, $options = array())
- {
- $db = $this->getDBInstance();
- if (MDB2::isError($db)) {
- return $db;
- }
- if (empty($table)) {
- $table = $this->listTables();
- if (MDB2::isError($table)) {
- return $table;
- }
- }
- if (is_array($table)) {
- foreach (array_keys($table) as $k) {
- $table[$k] = $db->quoteIdentifier($table[$k], true);
- }
- $table = implode(', ', $table);
- } else {
- $table = $db->quoteIdentifier($table, true);
- }
- $result = $db->exec('OPTIMIZE TABLE '.$table);
- if (MDB2::isError($result)) {
- return $result;
- }
- if (!empty($options['analyze'])) {
- $result = $db->exec('ANALYZE TABLE '.$table);
- if (MDB2::isError($result)) {
- return $result;
- }
- }
- return MDB2_OK;
- }
- // }}}
- // {{{ alterTable()
- /**
- * 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:
- *
- * name
- *
- * New name for the table.
- *
- * add
- *
- * 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.
- *
- *
- * remove
- *
- * 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.
- *
- * rename
- *
- * 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.
- *
- * change
- *
- * 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.
- *
- * Example
- * array(
- * 'name' => 'userlist',
- * 'add' => array(
- * 'quota' => array(
- * 'type' => 'integer',
- * 'unsigned' => 1
- * )
- * ),
- * 'remove' => array(
- * 'file_limit' => array(),
- * 'time_limit' => array()
- * ),
- * 'change' => array(
- * 'name' => array(
- * 'length' => '20',
- * 'definition' => array(
- * 'type' => 'text',
- * 'length' => 20,
- * ),
- * )
- * ),
- * 'rename' => array(
- * 'sex' => array(
- * 'name' => 'gender',
- * 'definition' => array(
- * 'type' => 'text',
- * 'length' => 1,
- * 'default' => 'M',
- * ),
- * )
- * )
- * )
- *
- * @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.
- * @access public
- *
- * @return mixed MDB2_OK on success, a MDB2 error on failure
- */
- function alterTable($name, $changes, $check)
- {
- $db = $this->getDBInstance();
- if (MDB2::isError($db)) {
- return $db;
- }
- foreach ($changes as $change_name => $change) {
- switch ($change_name) {
- case 'add':
- case 'remove':
- case 'change':
- case 'rename':
- case 'name':
- break;
- default:
- return $db->raiseError(MDB2_ERROR_CANNOT_ALTER, null, null,
- 'change type "'.$change_name.'" not yet supported', __FUNCTION__);
- }
- }
- if ($check) {
- return MDB2_OK;
- }
- $query = '';
- if (!empty($changes['name'])) {
- $change_name = $db->quoteIdentifier($changes['name'], true);
- $query .= 'RENAME TO ' . $change_name;
- }
- if (!empty($changes['add']) && is_array($changes['add'])) {
- foreach ($changes['add'] as $field_name => $field) {
- if ($query) {
- $query.= ', ';
- }
- $query.= 'ADD ' . $db->getDeclaration($field['type'], $field_name, $field);
- }
- }
- if (!empty($changes['remove']) && is_array($changes['remove'])) {
- foreach ($changes['remove'] as $field_name => $field) {
- if ($query) {
- $query.= ', ';
- }
- $field_name = $db->quoteIdentifier($field_name, true);
- $query.= 'DROP ' . $field_name;
- }
- }
- $rename = array();
- if (!empty($changes['rename']) && is_array($changes['rename'])) {
- foreach ($changes['rename'] as $field_name => $field) {
- $rename[$field['name']] = $field_name;
- }
- }
- if (!empty($changes['change']) && is_array($changes['change'])) {
- foreach ($changes['change'] as $field_name => $field) {
- if ($query) {
- $query.= ', ';
- }
- if (isset($rename[$field_name])) {
- $old_field_name = $rename[$field_name];
- unset($rename[$field_name]);
- } else {
- $old_field_name = $field_name;
- }
- $old_field_name = $db->quoteIdentifier($old_field_name, true);
- $query.= "CHANGE $old_field_name " . $db->getDeclaration($field['definition']['type'], $field_name, $field['definition']);
- }
- }
- if (!empty($rename) && is_array($rename)) {
- foreach ($rename as $rename_name => $renamed_field) {
- if ($query) {
- $query.= ', ';
- }
- $field = $changes['rename'][$renamed_field];
- $renamed_field = $db->quoteIdentifier($renamed_field, true);
- $query.= 'CHANGE ' . $renamed_field . ' ' . $db->getDeclaration($field['definition']['type'], $field['name'], $field['definition']);
- }
- }
- if (!$query) {
- return MDB2_OK;
- }
- $name = $db->quoteIdentifier($name, true);
- $result = $db->exec("ALTER TABLE $name $query");
- if (MDB2::isError($result)) {
- return $result;
- }
- return MDB2_OK;
- }
- // }}}
- // {{{ listDatabases()
- /**
- * list all databases
- *
- * @return mixed array of database names on success, a MDB2 error on failure
- * @access public
- */
- function listDatabases()
- {
- $db = $this->getDBInstance();
- if (MDB2::isError($db)) {
- return $db;
- }
- $result = $db->queryCol('SHOW DATABASES');
- if (MDB2::isError($result)) {
- return $result;
- }
- if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
- $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
- }
- return $result;
- }
- // }}}
- // {{{ listUsers()
- /**
- * list all users
- *
- * @return mixed array of user names on success, a MDB2 error on failure
- * @access public
- */
- function listUsers()
- {
- $db = $this->getDBInstance();
- if (MDB2::isError($db)) {
- return $db;
- }
- return $db->queryCol('SELECT DISTINCT USER FROM mysql.USER');
- }
- // }}}
- // {{{ listFunctions()
- /**
- * list all functions in the current database
- *
- * @return mixed array of function names on success, a MDB2 error on failure
- * @access public
- */
- function listFunctions()
- {
- $db = $this->getDBInstance();
- if (MDB2::isError($db)) {
- return $db;
- }
- $query = "SELECT name FROM mysql.proc";
- /*
- SELECT ROUTINE_NAME
- FROM INFORMATION_SCHEMA.ROUTINES
- WHERE ROUTINE_TYPE = 'FUNCTION'
- */
- $result = $db->queryCol($query);
- if (MDB2::isError($result)) {
- return $result;
- }
- if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
- $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
- }
- return $result;
- }
- // }}}
- // {{{ 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
- * @access public
- */
- function listTableTriggers($table = null)
- {
- $db = $this->getDBInstance();
- if (MDB2::isError($db)) {
- return $db;
- }
- $query = 'SHOW TRIGGERS';
- if (null !== $table) {
- $table = $db->quote($table, 'text');
- $query .= " LIKE $table";
- }
- $result = $db->queryCol($query);
- if (MDB2::isError($result)) {
- return $result;
- }
- if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
- $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
- }
- return $result;
- }
- // }}}
- // {{{ listTables()
- /**
- * list all tables in the current database
- *
- * @param string database, the current is default
- * @return mixed array of table names on success, a MDB2 error on failure
- * @access public
- */
- function listTables($database = null)
- {
- $db = $this->getDBInstance();
- if (MDB2::isError($db)) {
- return $db;
- }
- $query = "SHOW /*!50002 FULL*/ TABLES";
- if (null !== $database) {
- $query .= " FROM $database";
- }
- $query.= "/*!50002 WHERE Table_type = 'BASE TABLE'*/";
- $table_names = $db->queryAll($query, null, MDB2_FETCHMODE_ORDERED);
- if (MDB2::isError($table_names)) {
- return $table_names;
- }
- $result = array();
- foreach ($table_names as $table) {
- if (!$this->_fixSequenceName($table[0], true)) {
- $result[] = $table[0];
- }
- }
- if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
- $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
- }
- return $result;
- }
- // }}}
- // {{{ listViews()
- /**
- * list all views in the current database
- *
- * @param string database, the current is default
- * @return mixed array of view names on success, a MDB2 error on failure
- * @access public
- */
- function listViews($database = null)
- {
- $db = $this->getDBInstance();
- if (MDB2::isError($db)) {
- return $db;
- }
- $query = 'SHOW FULL TABLES';
- if (null !== $database) {
- $query.= " FROM $database";
- }
- $query.= " WHERE Table_type = 'VIEW'";
- $result = $db->queryCol($query);
- if (MDB2::isError($result)) {
- return $result;
- }
- if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
- $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
- }
- return $result;
- }
- // }}}
- // {{{ listTableFields()
- /**
- * 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
- * @access public
- */
- function listTableFields($table)
- {
- $db = $this->getDBInstance();
- if (MDB2::isError($db)) {
- return $db;
- }
- $table = $db->quoteIdentifier($table, true);
- $result = $db->queryCol("SHOW COLUMNS FROM $table");
- if (MDB2::isError($result)) {
- return $result;
- }
- if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
- $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
- }
- return $result;
- }
- // }}}
- // {{{ createIndex()
- /**
- * Get the stucture of a field into an array
- *
- * @author Leoncx
- * @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
- * each field.
- *
- * 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.
- *
- * Example
- * array(
- * 'fields' => array(
- * 'user_name' => array(
- * 'sorting' => 'ascending'
- * 'length' => 10
- * ),
- * 'last_login' => array()
- * )
- * )
- *
- * @return mixed MDB2_OK on success, a MDB2 error on failure
- * @access public
- */
- function createIndex($table, $name, $definition)
- {
- $db = $this->getDBInstance();
- if (MDB2::isError($db)) {
- return $db;
- }
- $table = $db->quoteIdentifier($table, true);
- $name = $db->quoteIdentifier($db->getIndexName($name), true);
- $query = "CREATE INDEX $name ON $table";
- $fields = array();
- foreach ($definition['fields'] as $field => $fieldinfo) {
- if (!empty($fieldinfo['length'])) {
- $fields[] = $db->quoteIdentifier($field, true) . '(' . $fieldinfo['length'] . ')';
- } else {
- $fields[] = $db->quoteIdentifier($field, true);
- }
- }
- $query .= ' ('. implode(', ', $fields) . ')';
- $result = $db->exec($query);
- if (MDB2::isError($result)) {
- return $result;
- }
- return MDB2_OK;
- }
- // }}}
- // {{{ dropIndex()
- /**
- * drop existing index
- *
- * @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
- * @access public
- */
- function dropIndex($table, $name)
- {
- $db = $this->getDBInstance();
- if (MDB2::isError($db)) {
- return $db;
- }
- $table = $db->quoteIdentifier($table, true);
- $name = $db->quoteIdentifier($db->getIndexName($name), true);
- $result = $db->exec("DROP INDEX $name ON $table");
- if (MDB2::isError($result)) {
- return $result;
- }
- return MDB2_OK;
- }
- // }}}
- // {{{ 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
- * @access public
- */
- function listTableIndexes($table)
- {
- $db = $this->getDBInstance();
- if (MDB2::isError($db)) {
- return $db;
- }
- $key_name = 'Key_name';
- $non_unique = 'Non_unique';
- if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
- if ($db->options['field_case'] == CASE_LOWER) {
- $key_name = strtolower($key_name);
- $non_unique = strtolower($non_unique);
- } else {
- $key_name = strtoupper($key_name);
- $non_unique = strtoupper($non_unique);
- }
- }
- $table = $db->quoteIdentifier($table, true);
- $query = "SHOW INDEX FROM $table";
- $indexes = $db->queryAll($query, null, MDB2_FETCHMODE_ASSOC);
- if (MDB2::isError($indexes)) {
- return $indexes;
- }
- $result = array();
- foreach ($indexes as $index_data) {
- if ($index_data[$non_unique] && ($index = $this->_fixIndexName($index_data[$key_name]))) {
- $result[$index] = true;
- }
- }
- if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
- $result = array_change_key_case($result, $db->options['field_case']);
- }
- return array_keys($result);
- }
- // }}}
- // {{{ 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.
- * Currently, only one property named FIELDS is supported. This property
- * is also an associative with the names of the constraint fields as array
- * constraints. Each entry of this array is set to another type of associative
- * array that specifies properties of the constraint that are specific to
- * each field.
- *
- * Example
- * array(
- * 'fields' => array(
- * 'user_name' => array(),
- * 'last_login' => array()
- * )
- * )
- * @return mixed MDB2_OK on success, a MDB2 error on failure
- * @access public
- */
- function createConstraint($table, $name, $definition)
- {
- $db = $this->getDBInstance();
- if (MDB2::isError($db)) {
- return $db;
- }
- $type = '';
- $idx_name = $db->quoteIdentifier($db->getIndexName($name), true);
- if (!empty($definition['primary'])) {
- $type = 'PRIMARY';
- $idx_name = 'KEY';
- } elseif (!empty($definition['unique'])) {
- $type = 'UNIQUE';
- } elseif (!empty($definition['foreign'])) {
- $type = 'CONSTRAINT';
- }
- if (empty($type)) {
- return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
- 'invalid definition, could not create constraint', __FUNCTION__);
- }
- $table_quoted = $db->quoteIdentifier($table, true);
- $query = "ALTER TABLE $table_quoted ADD $type $idx_name";
- if (!empty($definition['foreign'])) {
- $query .= ' FOREIGN KEY';
- }
- $fields = array();
- foreach ($definition['fields'] as $field => $fieldinfo) {
- $quoted = $db->quoteIdentifier($field, true);
- if (!empty($fieldinfo['length'])) {
- $quoted .= '(' . $fieldinfo['length'] . ')';
- }
- $fields[] = $quoted;
- }
- $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) . ')';
- $query .= $this->_getAdvancedFKOptions($definition);
- // add index on FK column(s) or we can't add a FK constraint
- // @see http://forums.mysql.com/read.php?22,19755,226009
- $result = $this->createIndex($table, $name.'_fkidx', $definition);
- if (MDB2::isError($result)) {
- return $result;
- }
- }
- $res = $db->exec($query);
- if (MDB2::isError($res)) {
- return $res;
- }
- if (!empty($definition['foreign'])) {
- return $this->_createFKTriggers($table, array($name => $definition));
- }
- return MDB2_OK;
- }
- // }}}
- // {{{ dropConstraint()
- /**
- * 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
- * @access public
- */
- function dropConstraint($table, $name, $primary = false)
- {
- $db = $this->getDBInstance();
- if (MDB2::isError($db)) {
- return $db;
- }
-
- if ($primary || strtolower($name) == 'primary') {
- $query = 'ALTER TABLE '. $db->quoteIdentifier($table, true) .' DROP PRIMARY KEY';
- $result = $db->exec($query);
- if (MDB2::isError($result)) {
- return $result;
- }
- return MDB2_OK;
- }
-
- //is it a FK constraint? If so, also delete the associated triggers
- $db->loadModule('Reverse', null, true);
- $definition = $db->reverse->getTableConstraintDefinition($table, $name);
- if (!MDB2::isError($definition) && !empty($definition['foreign'])) {
- //first drop the FK enforcing triggers
- $result = $this->_dropFKTriggers($table, $name, $definition['references']['table']);
- if (MDB2::isError($result)) {
- return $result;
- }
- //then drop the constraint itself
- $table = $db->quoteIdentifier($table, true);
- $name = $db->quoteIdentifier($db->getIndexName($name), true);
- $query = "ALTER TABLE $table DROP FOREIGN KEY $name";
- $result = $db->exec($query);
- if (MDB2::isError($result)) {
- return $result;
- }
- return MDB2_OK;
- }
- $table = $db->quoteIdentifier($table, true);
- $name = $db->quoteIdentifier($db->getIndexName($name), true);
- $query = "ALTER TABLE $table DROP INDEX $name";
- $result = $db->exec($query);
- if (MDB2::isError($result)) {
- return $result;
- }
- return MDB2_OK;
- }
- // }}}
- // {{{ _createFKTriggers()
- /**
- * Create triggers to enforce the FOREIGN KEY constraint on the table
- *
- * NB: since there's no RAISE_APPLICATION_ERROR facility in mysql,
- * we call a non-existent procedure to raise the FK violation message.
- * @see http://forums.mysql.com/read.php?99,55108,71877#msg-71877
- *
- * @param string $table table name
- * @param array $foreign_keys FOREIGN KEY definitions
- *
- * @return mixed MDB2_OK on success, a MDB2 error on failure
- * @access private
- */
- function _createFKTriggers($table, $foreign_keys)
- {
- $db = $this->getDBInstance();
- if (MDB2::isError($db)) {
- return $db;
- }
- // create triggers to enforce FOREIGN KEY constraints
- if ($db->supports('triggers') && !empty($foreign_keys)) {
- $table_quoted = $db->quoteIdentifier($table, true);
- foreach ($foreign_keys as $fkname => $fkdef) {
- if (empty($fkdef)) {
- continue;
- }
- //set actions to default if not set
- $fkdef['onupdate'] = empty($fkdef['onupdate']) ? $db->options['default_fk_action_onupdate'] : strtoupper($fkdef['onupdate']);
- $fkdef['ondelete'] = empty($fkdef['ondelete']) ? $db->options['default_fk_action_ondelete'] : strtoupper($fkdef['ondelete']);
- $trigger_names = array(
- 'insert' => $fkname.'_insert_trg',
- 'update' => $fkname.'_update_trg',
- 'pk_update' => $fkname.'_pk_update_trg',
- 'pk_delete' => $fkname.'_pk_delete_trg',
- );
- $table_fields = array_keys($fkdef['fields']);
- $referenced_fields = array_keys($fkdef['references']['fields']);
- //create the ON [UPDATE|DELETE] triggers on the primary table
- $restrict_action = ' IF (SELECT ';
- $aliased_fields = array();
- foreach ($table_fields as $field) {
- $aliased_fields[] = $table_quoted .'.'.$field .' AS '.$field;
- }
- $restrict_action .= implode(',', $aliased_fields)
- .' FROM '.$table_quoted
- .' WHERE ';
- $conditions = array();
- $new_values = array();
- $null_values = array();
- for ($i=0; $i<count($table_fields); $i++) {
- $conditions[] = $table_fields[$i] .' = OLD.'.$referenced_fields[$i];
- $new_values[] = $table_fields[$i] .' = NEW.'.$referenced_fields[$i];
- $null_values[] = $table_fields[$i] .' = NULL';
- }
- $conditions2 = array();
- for ($i=0; $i<count($referenced_fields); $i++) {
- $conditions2[] = 'NEW.'.$referenced_fields[$i] .' <> OLD.'.$referenced_fields[$i];
- }
- $restrict_action .= implode(' AND ', $conditions).') IS NOT NULL';
- $restrict_action2 = empty($conditions2) ? '' : ' AND (' .implode(' OR ', $conditions2) .')';
- $restrict_action3 = ' THEN CALL %s_ON_TABLE_'.$table.'_VIOLATES_FOREIGN_KEY_CONSTRAINT();'
- .' END IF;';
- $restrict_action_update = $restrict_action . $restrict_action2 . $restrict_action3;
- $restrict_action_delete = $restrict_action . $restrict_action3; // There is no NEW row in on DELETE trigger
- $cascade_action_update = 'UPDATE '.$table_quoted.' SET '.implode(', ', $new_values) .' WHERE '.implode(' AND ', $conditions). ';';
- $cascade_action_delete = 'DELETE FROM '.$table_quoted.' WHERE '.implode(' AND ', $conditions). ';';
- $setnull_action = 'UPDATE '.$table_quoted.' SET '.implode(', ', $null_values).' WHERE '.implode(' AND ', $conditions). ';';
- if ('SET DEFAULT' == $fkdef['onupdate'] || 'SET DEFAULT' == $fkdef['ondelete']) {
- $db->loadModule('Reverse', null, true);
- $default_values = array();
- foreach ($table_fields as $table_field) {
- $field_definition = $db->reverse->getTableFieldDefinition($table, $field);
- if (MDB2::isError($field_definition)) {
- return $field_definition;
- }
- $default_values[] = $table_field .' = '. $field_definition[0]['default'];
- }
- $setdefault_action = 'UPDATE '.$table_quoted.' SET '.implode(', ', $default_values).' WHERE '.implode(' AND ', $conditions). ';';
- }
- $query = 'CREATE TRIGGER %s'
- .' %s ON '.$fkdef['references']['table']
- .' FOR EACH ROW BEGIN '
- .' SET FOREIGN_KEY_CHECKS = 0; '; //only really needed for ON UPDATE CASCADE
- if ('CASCADE' == $fkdef['onupdate']) {
- $sql_update = sprintf($query, $trigger_names['pk_update'], 'BEFORE UPDATE', 'update') . $cascade_action_update;
- } elseif ('SET NULL' == $fkdef['onupdate']) {
- $sql_update = sprintf($query, $trigger_names['pk_update'], 'BEFORE UPDATE', 'update') . $setnull_action;
- } elseif ('SET DEFAULT' == $fkdef['onupdate']) {
- $sql_update = sprintf($query, $trigger_names['pk_update'], 'BEFORE UPDATE', 'update') . $setdefault_action;
- } elseif ('NO ACTION' == $fkdef['onupdate']) {
- $sql_update = sprintf($query.$restrict_action_update, $trigger_names['pk_update'], 'AFTER UPDATE', 'update');
- } elseif ('RESTRICT' == $fkdef['onupdate']) {
- $sql_update = sprintf($query.$restrict_action_update, $trigger_names['pk_update'], 'BEFORE UPDATE', 'update');
- }
- if ('CASCADE' == $fkdef['ondelete']) {
- $sql_delete = sprintf($query, $trigger_names['pk_delete'], 'BEFORE DELETE', 'delete') . $cascade_action_delete;
- } elseif ('SET NULL' == $fkdef['ondelete']) {
- $sql_delete = sprintf($query, $trigger_names['pk_delete'], 'BEFORE DELETE', 'delete') . $setnull_action;
- } elseif ('SET DEFAULT' == $fkdef['ondelete']) {
- $sql_delete = sprintf($query, $trigger_names['pk_delete'], 'BEFORE DELETE', 'delete') . $setdefault_action;
- } elseif ('NO ACTION' == $fkdef['ondelete']) {
- $sql_delete = sprintf($query.$restrict_action_delete, $trigger_names['pk_delete'], 'AFTER DELETE', 'delete');
- } elseif ('RESTRICT' == $fkdef['ondelete']) {
- $sql_delete = sprintf($query.$restrict_action_delete, $trigger_names['pk_delete'], 'BEFORE DELETE', 'delete');
- }
- $sql_update .= ' SET FOREIGN_KEY_CHECKS = 1; END;';
- $sql_delete .= ' SET FOREIGN_KEY_CHECKS = 1; END;';
- $db->pushErrorHandling(PEAR_ERROR_RETURN);
- $db->expectError(MDB2_ERROR_CANNOT_CREATE);
- $result = $db->exec($sql_delete);
- $expected_errmsg = 'This MySQL version doesn\'t support multiple triggers with the same action time and event for one table';
- $db->popExpect();
- $db->popErrorHandling();
- if (MDB2::isError($result)) {
- if ($result->getCode() != MDB2_ERROR_CANNOT_CREATE) {
- return $result;
- }
- $db->warnings[] = $expected_errmsg;
- }
- $db->pushErrorHandling(PEAR_ERROR_RETURN);
- $db->expectError(MDB2_ERROR_CANNOT_CREATE);
- $result = $db->exec($sql_update);
- $db->popExpect();
- $db->popErrorHandling();
- if (MDB2::isError($result) && $result->getCode() != MDB2_ERROR_CANNOT_CREATE) {
- if ($result->getCode() != MDB2_ERROR_CANNOT_CREATE) {
- return $result;
- }
- $db->warnings[] = $expected_errmsg;
- }
- }
- }
- return MDB2_OK;
- }
- // }}}
- // {{{ _dropFKTriggers()
- /**
- * Drop the triggers created to enforce the FOREIGN KEY constraint on the table
- *
- * @param string $table table name
- * @param string $fkname FOREIGN KEY constraint name
- * @param string $referenced_table referenced table name
- *
- * @return mixed MDB2_OK on success, a MDB2 error on failure
- * @access private
- */
- function _dropFKTriggers($table, $fkname, $referenced_table)
- {
- $db = $this->getDBInstance();
- if (MDB2::isError($db)) {
- return $db;
- }
- $triggers = $this->listTableTriggers($table);
- $triggers2 = $this->listTableTriggers($referenced_table);
- if (!MDB2::isError($triggers2) && !MDB2::isError($triggers)) {
- $triggers = array_merge($triggers, $triggers2);
- $pattern = '/^'.$fkname.'(_pk)?_(insert|update|delete)_trg$/i';
- foreach ($triggers as $trigger) {
- if (preg_match($pattern, $trigger)) {
- $result = $db->exec('DROP TRIGGER '.$trigger);
- if (MDB2::isError($result)) {
- return $result;
- }
- }
- }
- }
- return MDB2_OK;
- }
- // }}}
- // {{{ 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
- * @access public
- */
- function listTableConstraints($table)
- {
- $db = $this->getDBInstance();
- if (MDB2::isError($db)) {
- return $db;
- }
- $key_name = 'Key_name';
- $non_unique = 'Non_unique';
- if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
- if ($db->options['field_case'] == CASE_LOWER) {
- $key_name = strtolower($key_name);
- $non_unique = strtolower($non_unique);
- } else {
- $key_name = strtoupper($key_name);
- $non_unique = strtoupper($non_unique);
- }
- }
- $query = 'SHOW INDEX FROM ' . $db->quoteIdentifier($table, true);
- $indexes = $db->queryAll($query, null, MDB2_FETCHMODE_ASSOC);
- if (MDB2::isError($indexes)) {
- return $indexes;
- }
- $result = array();
- foreach ($indexes as $index_data) {
- if (!$index_data[$non_unique]) {
- if ($index_data[$key_name] !== 'PRIMARY') {
- $index = $this->_fixIndexName($index_data[$key_name]);
- } else {
- $index = 'PRIMARY';
- }
- if (!empty($index)) {
- $result[$index] = true;
- }
- }
- }
- //list FOREIGN KEY constraints...
- $query = 'SHOW CREATE TABLE '. $db->escape($table);
- $definition = $db->queryOne($query, 'text', 1);
- if (!MDB2::isError($definition) && !empty($definition)) {
- $pattern = '/\bCONSTRAINT\b\s+([^\s]+)\s+\bFOREIGN KEY\b/Uims';
- if (preg_match_all($pattern, str_replace('`', '', $definition), $matches) > 0) {
- foreach ($matches[1] as $constraint) {
- $result[$constraint] = true;
- }
- }
- }
- if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
- $result = array_change_key_case($result, $db->options['field_case']);
- }
- return array_keys($result);
- }
- // }}}
- // {{{ createSequence()
- /**
- * create sequence
- *
- * @param string $seq_name name of the sequence to be created
- * @param string $start start value of the sequence; default is 1
- * @param array $options An associative array of table options:
- * array(
- * 'comment' => 'Foo',
- * 'charset' => 'utf8',
- * 'collate' => 'utf8_unicode_ci',
- * 'type' => 'innodb',
- * );
- * @return mixed MDB2_OK on success, a MDB2 error on failure
- * @access public
- */
- function createSequence($seq_name, $start = 1, $options = array())
- {
- $db = $this->getDBInstance();
- if (MDB2::isError($db)) {
- return $db;
- }
- $sequence_name = $db->quoteIdentifier($db->getSequenceName($seq_name), true);
- $seqcol_name = $db->quoteIdentifier($db->options['seqcol_name'], true);
-
- $options_strings = array();
- if (!empty($options['comment'])) {
- $options_strings['comment'] = 'COMMENT = '.$db->quote($options['comment'], 'text');
- }
- if (!empty($options['charset'])) {
- $options_strings['charset'] = 'DEFAULT CHARACTER SET '.$options['charset'];
- if (!empty($options['collate'])) {
- $options_strings['charset'].= ' COLLATE '.$options['collate'];
- }
- }
- $type = false;
- if (!empty($options['type'])) {
- $type = $options['type'];
- } elseif ($db->options['default_table_type']) {
- $type = $db->options['default_table_type'];
- }
- if ($type) {
- $options_strings[] = "ENGINE = $type";
- }
- $query = "CREATE TABLE $sequence_name ($seqcol_name INT NOT NULL AUTO_INCREMENT, PRIMARY KEY ($seqcol_name))";
- if (!empty($options_strings)) {
- $query .= ' '.implode(' ', $options_strings);
- }
- $res = $db->exec($query);
- if (MDB2::isError($res)) {
- return $res;
- }
- if ($start == 1) {
- return MDB2_OK;
- }
- $query = "INSERT INTO $sequence_name ($seqcol_name) VALUES (".($start-1).')';
- $res = $db->exec($query);
- if (!MDB2::isError($res)) {
- return MDB2_OK;
- }
- // Handle error
- $result = $db->exec("DROP TABLE $sequence_name");
- if (MDB2::isError($result)) {
- return $db->raiseError($result, null, null,
- 'could not drop inconsistent sequence table', __FUNCTION__);
- }
- return $db->raiseError($res, null, null,
- 'could not create sequence table', __FUNCTION__);
- }
- // }}}
- // {{{ dropSequence()
- /**
- * drop existing sequence
- *
- * @param string $seq_name name of the sequence to be dropped
- * @return mixed MDB2_OK on success, a MDB2 error on failure
- * @access public
- */
- function dropSequence($seq_name)
- {
- $db = $this->getDBInstance();
- if (MDB2::isError($db)) {
- return $db;
- }
- $sequence_name = $db->quoteIdentifier($db->getSequenceName($seq_name), true);
- $result = $db->exec("DROP TABLE $sequence_name");
- if (MDB2::isError($result)) {
- return $result;
- }
- return MDB2_OK;
- }
- // }}}
- // {{{ listSequences()
- /**
- * list all sequences in the current database
- *
- * @param string database, the current is default
- * @return mixed array of sequence names on success, a MDB2 error on failure
- * @access public
- */
- function listSequences($database = null)
- {
- $db = $this->getDBInstance();
- if (MDB2::isError($db)) {
- return $db;
- }
- $query = "SHOW TABLES";
- if (null !== $database) {
- $query .= " FROM $database";
- }
- $table_names = $db->queryCol($query);
- if (MDB2::isError($table_names)) {
- return $table_names;
- }
- $result = array();
- foreach ($table_names as $table_name) {
- if ($sqn = $this->_fixSequenceName($table_name, true)) {
- $result[] = $sqn;
- }
- }
- if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
- $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
- }
- return $result;
- }
- // }}}
- }
- ?>
|