-<?php\r
-// +----------------------------------------------------------------------+\r
-// | PHP versions 4 and 5 |\r
-// +----------------------------------------------------------------------+\r
-// | Copyright (c) 1998-2008 Manuel Lemos, Tomas V.V.Cox, |\r
-// | Stig. S. Bakken, Lukas Smith |\r
-// | All rights reserved. |\r
-// +----------------------------------------------------------------------+\r
-// | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB |\r
-// | API as well as database abstraction for PHP applications. |\r
-// | This LICENSE is in the BSD license style. |\r
-// | |\r
-// | Redistribution and use in source and binary forms, with or without |\r
-// | modification, are permitted provided that the following conditions |\r
-// | are met: |\r
-// | |\r
-// | Redistributions of source code must retain the above copyright |\r
-// | notice, this list of conditions and the following disclaimer. |\r
-// | |\r
-// | Redistributions in binary form must reproduce the above copyright |\r
-// | notice, this list of conditions and the following disclaimer in the |\r
-// | documentation and/or other materials provided with the distribution. |\r
-// | |\r
-// | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken, |\r
-// | Lukas Smith nor the names of his contributors may be used to endorse |\r
-// | or promote products derived from this software without specific prior|\r
-// | written permission. |\r
-// | |\r
-// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |\r
-// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |\r
-// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |\r
-// | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |\r
-// | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |\r
-// | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |\r
-// | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|\r
-// | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |\r
-// | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |\r
-// | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|\r
-// | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |\r
-// | POSSIBILITY OF SUCH DAMAGE. |\r
-// +----------------------------------------------------------------------+\r
-// | Author: Paul Cooper <pgc@ucecom.com> |\r
-// +----------------------------------------------------------------------+\r
-//\r
-// $Id: pgsql.php,v 1.11 2008/11/09 19:46:50 quipo Exp $\r
-\r
-require_once 'MDB2/Driver/Function/Common.php';\r
-\r
-/**\r
- * MDB2 MySQL driver for the function modules\r
- *\r
- * @package MDB2\r
- * @category Database\r
- * @author Lukas Smith <smith@pooteeweet.org>\r
- */\r
-class MDB2_Driver_Function_pgsql extends MDB2_Driver_Function_Common\r
-{\r
- // {{{ executeStoredProc()\r
-\r
- /**\r
- * Execute a stored procedure and return any results\r
- *\r
- * @param string $name string that identifies the function to execute\r
- * @param mixed $params array that contains the paramaters to pass the stored proc\r
- * @param mixed $types array that contains the types of the columns in\r
- * the result set\r
- * @param mixed $result_class string which specifies which result class to use\r
- * @param mixed $result_wrap_class string which specifies which class to wrap results in\r
- * @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function &executeStoredProc($name, $params = null, $types = null, $result_class = true, $result_wrap_class = false)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- $query = 'SELECT * FROM '.$name;\r
- $query .= $params ? '('.implode(', ', $params).')' : '()';\r
- return $db->query($query, $types, $result_class, $result_wrap_class);\r
- }\r
- // }}}\r
- // {{{ unixtimestamp()\r
-\r
- /**\r
- * return string to call a function to get the unix timestamp from a iso timestamp\r
- *\r
- * @param string $expression\r
- *\r
- * @return string to call a variable with the timestamp\r
- * @access public\r
- */\r
- function unixtimestamp($expression)\r
- {\r
- return 'EXTRACT(EPOCH FROM DATE_TRUNC(\'seconds\', CAST ((' . $expression . ') AS TIMESTAMP)))';\r
- }\r
-\r
- // }}}\r
- // {{{ random()\r
-\r
- /**\r
- * return string to call a function to get random value inside an SQL statement\r
- *\r
- * @return return string to generate float between 0 and 1\r
- * @access public\r
- */\r
- function random()\r
- {\r
- return 'RANDOM()';\r
- }\r
-\r
- // }}}\r
-}\r
+<?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: Paul Cooper <pgc@ucecom.com> |
+// +----------------------------------------------------------------------+
+//
+// $Id: pgsql.php,v 1.11 2008/11/09 19:46:50 quipo Exp $
+
+require_once 'MDB2/Driver/Function/Common.php';
+
+/**
+ * MDB2 MySQL driver for the function modules
+ *
+ * @package MDB2
+ * @category Database
+ * @author Lukas Smith <smith@pooteeweet.org>
+ */
+class MDB2_Driver_Function_pgsql extends MDB2_Driver_Function_Common
+{
+ // {{{ executeStoredProc()
+
+ /**
+ * Execute a stored procedure and return any results
+ *
+ * @param string $name string that identifies the function to execute
+ * @param mixed $params array that contains the paramaters to pass the stored proc
+ * @param mixed $types array that contains the types of the columns in
+ * the result set
+ * @param mixed $result_class string which specifies which result class to use
+ * @param mixed $result_wrap_class string which specifies which class to wrap results in
+ * @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure
+ * @access public
+ */
+ function &executeStoredProc($name, $params = null, $types = null, $result_class = true, $result_wrap_class = false)
+ {
+ $db =& $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ $query = 'SELECT * FROM '.$name;
+ $query .= $params ? '('.implode(', ', $params).')' : '()';
+ return $db->query($query, $types, $result_class, $result_wrap_class);
+ }
+ // }}}
+ // {{{ unixtimestamp()
+
+ /**
+ * return string to call a function to get the unix timestamp from a iso timestamp
+ *
+ * @param string $expression
+ *
+ * @return string to call a variable with the timestamp
+ * @access public
+ */
+ function unixtimestamp($expression)
+ {
+ return 'EXTRACT(EPOCH FROM DATE_TRUNC(\'seconds\', CAST ((' . $expression . ') AS TIMESTAMP)))';
+ }
+
+ // }}}
+ // {{{ random()
+
+ /**
+ * return string to call a function to get random value inside an SQL statement
+ *
+ * @return return string to generate float between 0 and 1
+ * @access public
+ */
+ function random()
+ {
+ return 'RANDOM()';
+ }
+
+ // }}}
+}
?>
\ No newline at end of file
-<?php\r
-// +----------------------------------------------------------------------+\r
-// | PHP versions 4 and 5 |\r
-// +----------------------------------------------------------------------+\r
-// | Copyright (c) 1998-2008 Manuel Lemos, Tomas V.V.Cox, |\r
-// | Stig. S. Bakken, Lukas Smith |\r
-// | All rights reserved. |\r
-// +----------------------------------------------------------------------+\r
-// | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB |\r
-// | API as well as database abstraction for PHP applications. |\r
-// | This LICENSE is in the BSD license style. |\r
-// | |\r
-// | Redistribution and use in source and binary forms, with or without |\r
-// | modification, are permitted provided that the following conditions |\r
-// | are met: |\r
-// | |\r
-// | Redistributions of source code must retain the above copyright |\r
-// | notice, this list of conditions and the following disclaimer. |\r
-// | |\r
-// | Redistributions in binary form must reproduce the above copyright |\r
-// | notice, this list of conditions and the following disclaimer in the |\r
-// | documentation and/or other materials provided with the distribution. |\r
-// | |\r
-// | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken, |\r
-// | Lukas Smith nor the names of his contributors may be used to endorse |\r
-// | or promote products derived from this software without specific prior|\r
-// | written permission. |\r
-// | |\r
-// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |\r
-// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |\r
-// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |\r
-// | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |\r
-// | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |\r
-// | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |\r
-// | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|\r
-// | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |\r
-// | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |\r
-// | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|\r
-// | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |\r
-// | POSSIBILITY OF SUCH DAMAGE. |\r
-// +----------------------------------------------------------------------+\r
-// | Authors: Lukas Smith <smith@pooteeweet.org> |\r
-// | Lorenzo Alberton <l.alberton@quipo.it> |\r
-// +----------------------------------------------------------------------+\r
-//\r
-// $Id: Common.php,v 1.72 2009/01/14 15:00:40 quipo Exp $\r
-//\r
-\r
-/**\r
- * @package MDB2\r
- * @category Database\r
- * @author Lukas Smith <smith@pooteeweet.org>\r
- * @author Lorenzo Alberton <l.alberton@quipo.it>\r
- */\r
-\r
-/**\r
- * Base class for the management modules that is extended by each MDB2 driver\r
- *\r
- * To load this module in the MDB2 object:\r
- * $mdb->loadModule('Manager');\r
- *\r
- * @package MDB2\r
- * @category Database\r
- * @author Lukas Smith <smith@pooteeweet.org>\r
- */\r
-class MDB2_Driver_Manager_Common extends MDB2_Module_Common\r
-{\r
- // {{{ splitTableSchema()\r
-\r
- /**\r
- * Split the "[owner|schema].table" notation into an array\r
- *\r
- * @param string $table [schema and] table name\r
- *\r
- * @return array array(schema, table)\r
- * @access private\r
- */\r
- function splitTableSchema($table)\r
- {\r
- $ret = array();\r
- if (strpos($table, '.') !== false) {\r
- return explode('.', $table);\r
- }\r
- return array(null, $table);\r
- }\r
-\r
- // }}}\r
- // {{{ getFieldDeclarationList()\r
-\r
- /**\r
- * Get declaration of a number of field in bulk\r
- *\r
- * @param array $fields a multidimensional associative array.\r
- * The first dimension determines the field name, while the second\r
- * dimension is keyed with the name of the properties\r
- * of the field being declared as array indexes. Currently, the types\r
- * of supported field properties are as follows:\r
- *\r
- * default\r
- * Boolean value to be used as default for this field.\r
- *\r
- * notnull\r
- * Boolean flag that indicates whether this field is constrained\r
- * to not be set to null.\r
- *\r
- * @return mixed string on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function getFieldDeclarationList($fields)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- if (!is_array($fields) || empty($fields)) {\r
- return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,\r
- 'missing any fields', __FUNCTION__);\r
- }\r
- foreach ($fields as $field_name => $field) {\r
- $query = $db->getDeclaration($field['type'], $field_name, $field);\r
- if (PEAR::isError($query)) {\r
- return $query;\r
- }\r
- $query_fields[] = $query;\r
- }\r
- return implode(', ', $query_fields);\r
- }\r
-\r
- // }}}\r
- // {{{ _fixSequenceName()\r
-\r
- /**\r
- * Removes any formatting in an sequence name using the 'seqname_format' option\r
- *\r
- * @param string $sqn string that containts name of a potential sequence\r
- * @param bool $check if only formatted sequences should be returned\r
- * @return string name of the sequence with possible formatting removed\r
- * @access protected\r
- */\r
- function _fixSequenceName($sqn, $check = false)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- $seq_pattern = '/^'.preg_replace('/%s/', '([a-z0-9_]+)', $db->options['seqname_format']).'$/i';\r
- $seq_name = preg_replace($seq_pattern, '\\1', $sqn);\r
- if ($seq_name && !strcasecmp($sqn, $db->getSequenceName($seq_name))) {\r
- return $seq_name;\r
- }\r
- if ($check) {\r
- return false;\r
- }\r
- return $sqn;\r
- }\r
-\r
- // }}}\r
- // {{{ _fixIndexName()\r
-\r
- /**\r
- * Removes any formatting in an index name using the 'idxname_format' option\r
- *\r
- * @param string $idx string that containts name of anl index\r
- * @return string name of the index with eventual formatting removed\r
- * @access protected\r
- */\r
- function _fixIndexName($idx)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- $idx_pattern = '/^'.preg_replace('/%s/', '([a-z0-9_]+)', $db->options['idxname_format']).'$/i';\r
- $idx_name = preg_replace($idx_pattern, '\\1', $idx);\r
- if ($idx_name && !strcasecmp($idx, $db->getIndexName($idx_name))) {\r
- return $idx_name;\r
- }\r
- return $idx;\r
- }\r
-\r
- // }}}\r
- // {{{ createDatabase()\r
-\r
- /**\r
- * create a new database\r
- *\r
- * @param string $name name of the database that should be created\r
- * @param array $options array with charset, collation info\r
- *\r
- * @return mixed MDB2_OK on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function createDatabase($database, $options = array())\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,\r
- 'method not implemented', __FUNCTION__);\r
- }\r
-\r
- // }}}\r
- // {{{ alterDatabase()\r
-\r
- /**\r
- * alter an existing database\r
- *\r
- * @param string $name name of the database that should be created\r
- * @param array $options array with charset, collation info\r
- *\r
- * @return mixed MDB2_OK on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function alterDatabase($database, $options = array())\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,\r
- 'method not implemented', __FUNCTION__);\r
- }\r
-\r
- // }}}\r
- // {{{ dropDatabase()\r
-\r
- /**\r
- * drop an existing database\r
- *\r
- * @param string $name name of the database that should be dropped\r
- * @return mixed MDB2_OK on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function dropDatabase($database)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,\r
- 'method not implemented', __FUNCTION__);\r
- }\r
-\r
- // }}}\r
- // {{{ _getCreateTableQuery()\r
-\r
- /**\r
- * Create a basic SQL query for a new table creation\r
- *\r
- * @param string $name Name of the database that should be created\r
- * @param array $fields Associative array that contains the definition of each field of the new table\r
- * @param array $options An associative array of table options\r
- *\r
- * @return mixed string (the SQL query) on success, a MDB2 error on failure\r
- * @see createTable()\r
- */\r
- function _getCreateTableQuery($name, $fields, $options = array())\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- if (!$name) {\r
- return $db->raiseError(MDB2_ERROR_CANNOT_CREATE, null, null,\r
- 'no valid table name specified', __FUNCTION__);\r
- }\r
- if (empty($fields)) {\r
- return $db->raiseError(MDB2_ERROR_CANNOT_CREATE, null, null,\r
- 'no fields specified for table "'.$name.'"', __FUNCTION__);\r
- }\r
- $query_fields = $this->getFieldDeclarationList($fields);\r
- if (PEAR::isError($query_fields)) {\r
- return $query_fields;\r
- }\r
- if (!empty($options['primary'])) {\r
- $query_fields.= ', PRIMARY KEY ('.implode(', ', array_keys($options['primary'])).')';\r
- }\r
-\r
- $name = $db->quoteIdentifier($name, true);\r
- $result = 'CREATE ';\r
- if (!empty($options['temporary'])) {\r
- $result .= $this->_getTemporaryTableQuery();\r
- }\r
- $result .= " TABLE $name ($query_fields)";\r
- return $result;\r
- }\r
-\r
- // }}}\r
- // {{{ _getTemporaryTableQuery()\r
-\r
- /**\r
- * A method to return the required SQL string that fits between CREATE ... TABLE\r
- * to create the table as a temporary table.\r
- *\r
- * Should be overridden in driver classes to return the correct string for the\r
- * specific database type.\r
- *\r
- * The default is to return the string "TEMPORARY" - this will result in a\r
- * SQL error for any database that does not support temporary tables, or that\r
- * requires a different SQL command from "CREATE TEMPORARY TABLE".\r
- *\r
- * @return string The string required to be placed between "CREATE" and "TABLE"\r
- * to generate a temporary table, if possible.\r
- */\r
- function _getTemporaryTableQuery()\r
- {\r
- return 'TEMPORARY';\r
- }\r
-\r
- // }}}\r
- // {{{ createTable()\r
-\r
- /**\r
- * create a new table\r
- *\r
- * @param string $name Name of the database that should be created\r
- * @param array $fields Associative array that contains the definition of each field of the new table\r
- * The indexes of the array entries are the names of the fields of the table an\r
- * the array entry values are associative arrays like those that are meant to be\r
- * passed with the field definitions to get[Type]Declaration() functions.\r
- * array(\r
- * 'id' => array(\r
- * 'type' => 'integer',\r
- * 'unsigned' => 1\r
- * 'notnull' => 1\r
- * 'default' => 0\r
- * ),\r
- * 'name' => array(\r
- * 'type' => 'text',\r
- * 'length' => 12\r
- * ),\r
- * 'password' => array(\r
- * 'type' => 'text',\r
- * 'length' => 12\r
- * )\r
- * );\r
- * @param array $options An associative array of table options:\r
- * array(\r
- * 'comment' => 'Foo',\r
- * 'temporary' => true|false,\r
- * );\r
- * @return mixed MDB2_OK on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function createTable($name, $fields, $options = array())\r
- {\r
- $query = $this->_getCreateTableQuery($name, $fields, $options);\r
- if (PEAR::isError($query)) {\r
- return $query;\r
- }\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
- $result = $db->exec($query);\r
- if (PEAR::isError($result)) {\r
- return $result;\r
- }\r
- return MDB2_OK;\r
- }\r
-\r
- // }}}\r
- // {{{ dropTable()\r
-\r
- /**\r
- * drop an existing table\r
- *\r
- * @param string $name name of the table that should be dropped\r
- * @return mixed MDB2_OK on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function dropTable($name)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- $name = $db->quoteIdentifier($name, true);\r
- return $db->exec("DROP TABLE $name");\r
- }\r
-\r
- // }}}\r
- // {{{ truncateTable()\r
-\r
- /**\r
- * Truncate an existing table (if the TRUNCATE TABLE syntax is not supported,\r
- * it falls back to a DELETE FROM TABLE query)\r
- *\r
- * @param string $name name of the table that should be truncated\r
- * @return mixed MDB2_OK on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function truncateTable($name)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- $name = $db->quoteIdentifier($name, true);\r
- return $db->exec("DELETE FROM $name");\r
- }\r
-\r
- // }}}\r
- // {{{ vacuum()\r
-\r
- /**\r
- * Optimize (vacuum) all the tables in the db (or only the specified table)\r
- * and optionally run ANALYZE.\r
- *\r
- * @param string $table table name (all the tables if empty)\r
- * @param array $options an array with driver-specific options:\r
- * - timeout [int] (in seconds) [mssql-only]\r
- * - analyze [boolean] [pgsql and mysql]\r
- * - full [boolean] [pgsql-only]\r
- * - freeze [boolean] [pgsql-only]\r
- *\r
- * @return mixed MDB2_OK success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function vacuum($table = null, $options = array())\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,\r
- 'method not implemented', __FUNCTION__);\r
- }\r
-\r
- // }}}\r
- // {{{ alterTable()\r
-\r
- /**\r
- * alter an existing table\r
- *\r
- * @param string $name name of the table that is intended to be changed.\r
- * @param array $changes associative array that contains the details of each type\r
- * of change that is intended to be performed. The types of\r
- * changes that are currently supported are defined as follows:\r
- *\r
- * name\r
- *\r
- * New name for the table.\r
- *\r
- * add\r
- *\r
- * Associative array with the names of fields to be added as\r
- * indexes of the array. The value of each entry of the array\r
- * should be set to another associative array with the properties\r
- * of the fields to be added. The properties of the fields should\r
- * be the same as defined by the MDB2 parser.\r
- *\r
- *\r
- * remove\r
- *\r
- * Associative array with the names of fields to be removed as indexes\r
- * of the array. Currently the values assigned to each entry are ignored.\r
- * An empty array should be used for future compatibility.\r
- *\r
- * rename\r
- *\r
- * Associative array with the names of fields to be renamed as indexes\r
- * of the array. The value of each entry of the array should be set to\r
- * another associative array with the entry named name with the new\r
- * field name and the entry named Declaration that is expected to contain\r
- * the portion of the field declaration already in DBMS specific SQL code\r
- * as it is used in the CREATE TABLE statement.\r
- *\r
- * change\r
- *\r
- * Associative array with the names of the fields to be changed as indexes\r
- * of the array. Keep in mind that if it is intended to change either the\r
- * name of a field and any other properties, the change array entries\r
- * should have the new names of the fields as array indexes.\r
- *\r
- * The value of each entry of the array should be set to another associative\r
- * array with the properties of the fields to that are meant to be changed as\r
- * array entries. These entries should be assigned to the new values of the\r
- * respective properties. The properties of the fields should be the same\r
- * as defined by the MDB2 parser.\r
- *\r
- * Example\r
- * array(\r
- * 'name' => 'userlist',\r
- * 'add' => array(\r
- * 'quota' => array(\r
- * 'type' => 'integer',\r
- * 'unsigned' => 1\r
- * )\r
- * ),\r
- * 'remove' => array(\r
- * 'file_limit' => array(),\r
- * 'time_limit' => array()\r
- * ),\r
- * 'change' => array(\r
- * 'name' => array(\r
- * 'length' => '20',\r
- * 'definition' => array(\r
- * 'type' => 'text',\r
- * 'length' => 20,\r
- * ),\r
- * )\r
- * ),\r
- * 'rename' => array(\r
- * 'sex' => array(\r
- * 'name' => 'gender',\r
- * 'definition' => array(\r
- * 'type' => 'text',\r
- * 'length' => 1,\r
- * 'default' => 'M',\r
- * ),\r
- * )\r
- * )\r
- * )\r
- *\r
- * @param boolean $check indicates whether the function should just check if the DBMS driver\r
- * can perform the requested table alterations if the value is true or\r
- * actually perform them otherwise.\r
- * @access public\r
- *\r
- * @return mixed MDB2_OK on success, a MDB2 error on failure\r
- */\r
- function alterTable($name, $changes, $check)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,\r
- 'method not implemented', __FUNCTION__);\r
- }\r
-\r
- // }}}\r
- // {{{ listDatabases()\r
-\r
- /**\r
- * list all databases\r
- *\r
- * @return mixed array of database names on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function listDatabases()\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,\r
- 'method not implementedd', __FUNCTION__);\r
- }\r
-\r
- // }}}\r
- // {{{ listUsers()\r
-\r
- /**\r
- * list all users\r
- *\r
- * @return mixed array of user names on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function listUsers()\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,\r
- 'method not implemented', __FUNCTION__);\r
- }\r
-\r
- // }}}\r
- // {{{ listViews()\r
-\r
- /**\r
- * list all views in the current database\r
- *\r
- * @param string database, the current is default\r
- * NB: not all the drivers can get the view names from\r
- * a database other than the current one\r
- * @return mixed array of view names on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function listViews($database = null)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,\r
- 'method not implemented', __FUNCTION__);\r
- }\r
-\r
- // }}}\r
- // {{{ listTableViews()\r
-\r
- /**\r
- * list the views in the database that reference a given table\r
- *\r
- * @param string table for which all referenced views should be found\r
- * @return mixed array of view names on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function listTableViews($table)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,\r
- 'method not implemented', __FUNCTION__);\r
- }\r
-\r
- // }}}\r
- // {{{ listTableTriggers()\r
-\r
- /**\r
- * list all triggers in the database that reference a given table\r
- *\r
- * @param string table for which all referenced triggers should be found\r
- * @return mixed array of trigger names on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function listTableTriggers($table = null)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,\r
- 'method not implemented', __FUNCTION__);\r
- }\r
-\r
- // }}}\r
- // {{{ listFunctions()\r
-\r
- /**\r
- * list all functions in the current database\r
- *\r
- * @return mixed array of function names on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function listFunctions()\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,\r
- 'method not implemented', __FUNCTION__);\r
- }\r
-\r
- // }}}\r
- // {{{ listTables()\r
-\r
- /**\r
- * list all tables in the current database\r
- *\r
- * @param string database, the current is default.\r
- * NB: not all the drivers can get the table names from\r
- * a database other than the current one\r
- * @return mixed array of table names on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function listTables($database = null)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,\r
- 'method not implemented', __FUNCTION__);\r
- }\r
-\r
- // }}}\r
- // {{{ listTableFields()\r
-\r
- /**\r
- * list all fields in a table in the current database\r
- *\r
- * @param string $table name of table that should be used in method\r
- * @return mixed array of field names on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function listTableFields($table)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,\r
- 'method not implemented', __FUNCTION__);\r
- }\r
-\r
- // }}}\r
- // {{{ createIndex()\r
-\r
- /**\r
- * Get the stucture of a field into an array\r
- *\r
- * @param string $table name of the table on which the index is to be created\r
- * @param string $name name of the index to be created\r
- * @param array $definition associative array that defines properties of the index to be created.\r
- * Currently, only one property named FIELDS is supported. This property\r
- * is also an associative with the names of the index fields as array\r
- * indexes. Each entry of this array is set to another type of associative\r
- * array that specifies properties of the index that are specific to\r
- * each field.\r
- *\r
- * Currently, only the sorting property is supported. It should be used\r
- * to define the sorting direction of the index. It may be set to either\r
- * ascending or descending.\r
- *\r
- * Not all DBMS support index sorting direction configuration. The DBMS\r
- * drivers of those that do not support it ignore this property. Use the\r
- * function supports() to determine whether the DBMS driver can manage indexes.\r
- *\r
- * Example\r
- * array(\r
- * 'fields' => array(\r
- * 'user_name' => array(\r
- * 'sorting' => 'ascending'\r
- * ),\r
- * 'last_login' => array()\r
- * )\r
- * )\r
- * @return mixed MDB2_OK on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function createIndex($table, $name, $definition)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- $table = $db->quoteIdentifier($table, true);\r
- $name = $db->quoteIdentifier($db->getIndexName($name), true);\r
- $query = "CREATE INDEX $name ON $table";\r
- $fields = array();\r
- foreach (array_keys($definition['fields']) as $field) {\r
- $fields[] = $db->quoteIdentifier($field, true);\r
- }\r
- $query .= ' ('. implode(', ', $fields) . ')';\r
- return $db->exec($query);\r
- }\r
-\r
- // }}}\r
- // {{{ dropIndex()\r
-\r
- /**\r
- * drop existing index\r
- *\r
- * @param string $table name of table that should be used in method\r
- * @param string $name name of the index to be dropped\r
- * @return mixed MDB2_OK on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function dropIndex($table, $name)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- $name = $db->quoteIdentifier($db->getIndexName($name), true);\r
- return $db->exec("DROP INDEX $name");\r
- }\r
-\r
- // }}}\r
- // {{{ listTableIndexes()\r
-\r
- /**\r
- * list all indexes in a table\r
- *\r
- * @param string $table name of table that should be used in method\r
- * @return mixed array of index names on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function listTableIndexes($table)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,\r
- 'method not implemented', __FUNCTION__);\r
- }\r
-\r
- // }}}\r
- // {{{ _getAdvancedFKOptions()\r
-\r
- /**\r
- * Return the FOREIGN KEY query section dealing with non-standard options\r
- * as MATCH, INITIALLY DEFERRED, ON UPDATE, ...\r
- *\r
- * @param array $definition\r
- * @return string\r
- * @access protected\r
- */\r
- function _getAdvancedFKOptions($definition)\r
- {\r
- return '';\r
- }\r
-\r
- // }}}\r
- // {{{ createConstraint()\r
-\r
- /**\r
- * create a constraint on a table\r
- *\r
- * @param string $table name of the table on which the constraint is to be created\r
- * @param string $name name of the constraint to be created\r
- * @param array $definition associative array that defines properties of the constraint to be created.\r
- * The full structure of the array looks like this:\r
- * <pre>\r
- * array (\r
- * [primary] => 0\r
- * [unique] => 0\r
- * [foreign] => 1\r
- * [check] => 0\r
- * [fields] => array (\r
- * [field1name] => array() // one entry per each field covered\r
- * [field2name] => array() // by the index\r
- * [field3name] => array(\r
- * [sorting] => ascending\r
- * [position] => 3\r
- * )\r
- * )\r
- * [references] => array(\r
- * [table] => name\r
- * [fields] => array(\r
- * [field1name] => array( //one entry per each referenced field\r
- * [position] => 1\r
- * )\r
- * )\r
- * )\r
- * [deferrable] => 0\r
- * [initiallydeferred] => 0\r
- * [onupdate] => CASCADE|RESTRICT|SET NULL|SET DEFAULT|NO ACTION\r
- * [ondelete] => CASCADE|RESTRICT|SET NULL|SET DEFAULT|NO ACTION\r
- * [match] => SIMPLE|PARTIAL|FULL\r
- * );\r
- * </pre>\r
- * @return mixed MDB2_OK on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function createConstraint($table, $name, $definition)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
- $table = $db->quoteIdentifier($table, true);\r
- $name = $db->quoteIdentifier($db->getIndexName($name), true);\r
- $query = "ALTER TABLE $table ADD CONSTRAINT $name";\r
- if (!empty($definition['primary'])) {\r
- $query.= ' PRIMARY KEY';\r
- } elseif (!empty($definition['unique'])) {\r
- $query.= ' UNIQUE';\r
- } elseif (!empty($definition['foreign'])) {\r
- $query.= ' FOREIGN KEY';\r
- }\r
- $fields = array();\r
- foreach (array_keys($definition['fields']) as $field) {\r
- $fields[] = $db->quoteIdentifier($field, true);\r
- }\r
- $query .= ' ('. implode(', ', $fields) . ')';\r
- if (!empty($definition['foreign'])) {\r
- $query.= ' REFERENCES ' . $db->quoteIdentifier($definition['references']['table'], true);\r
- $referenced_fields = array();\r
- foreach (array_keys($definition['references']['fields']) as $field) {\r
- $referenced_fields[] = $db->quoteIdentifier($field, true);\r
- }\r
- $query .= ' ('. implode(', ', $referenced_fields) . ')';\r
- $query .= $this->_getAdvancedFKOptions($definition);\r
- }\r
- return $db->exec($query);\r
- }\r
-\r
- // }}}\r
- // {{{ dropConstraint()\r
-\r
- /**\r
- * drop existing constraint\r
- *\r
- * @param string $table name of table that should be used in method\r
- * @param string $name name of the constraint to be dropped\r
- * @param string $primary hint if the constraint is primary\r
- * @return mixed MDB2_OK on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function dropConstraint($table, $name, $primary = false)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- $table = $db->quoteIdentifier($table, true);\r
- $name = $db->quoteIdentifier($db->getIndexName($name), true);\r
- return $db->exec("ALTER TABLE $table DROP CONSTRAINT $name");\r
- }\r
-\r
- // }}}\r
- // {{{ listTableConstraints()\r
-\r
- /**\r
- * list all constraints in a table\r
- *\r
- * @param string $table name of table that should be used in method\r
- * @return mixed array of constraint names on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function listTableConstraints($table)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,\r
- 'method not implemented', __FUNCTION__);\r
- }\r
-\r
- // }}}\r
- // {{{ createSequence()\r
-\r
- /**\r
- * create sequence\r
- *\r
- * @param string $seq_name name of the sequence to be created\r
- * @param string $start start value of the sequence; default is 1\r
- * @return mixed MDB2_OK on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function createSequence($seq_name, $start = 1)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,\r
- 'method not implemented', __FUNCTION__);\r
- }\r
-\r
- // }}}\r
- // {{{ dropSequence()\r
-\r
- /**\r
- * drop existing sequence\r
- *\r
- * @param string $seq_name name of the sequence to be dropped\r
- * @return mixed MDB2_OK on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function dropSequence($name)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,\r
- 'method not implemented', __FUNCTION__);\r
- }\r
-\r
- // }}}\r
- // {{{ listSequences()\r
-\r
- /**\r
- * list all sequences in the current database\r
- *\r
- * @param string database, the current is default\r
- * NB: not all the drivers can get the sequence names from\r
- * a database other than the current one\r
- * @return mixed array of sequence names on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function listSequences($database = null)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,\r
- 'method not implemented', __FUNCTION__);\r
- }\r
-\r
- // }}}\r
-}\r
+<?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. |
+// +----------------------------------------------------------------------+
+// | Authors: Lukas Smith <smith@pooteeweet.org> |
+// | Lorenzo Alberton <l.alberton@quipo.it> |
+// +----------------------------------------------------------------------+
+//
+// $Id: Common.php,v 1.72 2009/01/14 15:00:40 quipo Exp $
+//
+
+/**
+ * @package MDB2
+ * @category Database
+ * @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');
+ *
+ * @package MDB2
+ * @category Database
+ * @author Lukas Smith <smith@pooteeweet.org>
+ */
+class MDB2_Driver_Manager_Common extends MDB2_Module_Common
+{
+ // {{{ splitTableSchema()
+
+ /**
+ * Split the "[owner|schema].table" notation into an array
+ *
+ * @param string $table [schema and] table name
+ *
+ * @return array array(schema, table)
+ * @access private
+ */
+ function splitTableSchema($table)
+ {
+ $ret = array();
+ if (strpos($table, '.') !== false) {
+ return explode('.', $table);
+ }
+ 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:
+ *
+ * default
+ * Boolean value to be used as default for this field.
+ *
+ * notnull
+ * Boolean flag that indicates whether this field is constrained
+ * to not be set to null.
+ *
+ * @return mixed string on success, a MDB2 error on failure
+ * @access public
+ */
+ function getFieldDeclarationList($fields)
+ {
+ $db =& $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ if (!is_array($fields) || empty($fields)) {
+ return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
+ 'missing any fields', __FUNCTION__);
+ }
+ foreach ($fields as $field_name => $field) {
+ $query = $db->getDeclaration($field['type'], $field_name, $field);
+ if (PEAR::isError($query)) {
+ return $query;
+ }
+ $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
+ * @access protected
+ */
+ function _fixSequenceName($sqn, $check = false)
+ {
+ $db =& $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ $seq_pattern = '/^'.preg_replace('/%s/', '([a-z0-9_]+)', $db->options['seqname_format']).'$/i';
+ $seq_name = preg_replace($seq_pattern, '\\1', $sqn);
+ if ($seq_name && !strcasecmp($sqn, $db->getSequenceName($seq_name))) {
+ return $seq_name;
+ }
+ if ($check) {
+ return false;
+ }
+ return $sqn;
+ }
+
+ // }}}
+ // {{{ _fixIndexName()
+
+ /**
+ * 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
+ * @access protected
+ */
+ function _fixIndexName($idx)
+ {
+ $db =& $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ $idx_pattern = '/^'.preg_replace('/%s/', '([a-z0-9_]+)', $db->options['idxname_format']).'$/i';
+ $idx_name = preg_replace($idx_pattern, '\\1', $idx);
+ if ($idx_name && !strcasecmp($idx, $db->getIndexName($idx_name))) {
+ return $idx_name;
+ }
+ return $idx;
+ }
+
+ // }}}
+ // {{{ 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($database, $options = array())
+ {
+ $db =& $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ alterDatabase()
+
+ /**
+ * 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
+ * @access public
+ */
+ function alterDatabase($database, $options = array())
+ {
+ $db =& $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ 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($database)
+ {
+ $db =& $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ '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
+ * @see createTable()
+ */
+ function _getCreateTableQuery($name, $fields, $options = array())
+ {
+ $db =& $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ if (!$name) {
+ return $db->raiseError(MDB2_ERROR_CANNOT_CREATE, null, null,
+ 'no valid table name specified', __FUNCTION__);
+ }
+ if (empty($fields)) {
+ return $db->raiseError(MDB2_ERROR_CANNOT_CREATE, null, null,
+ 'no fields specified for table "'.$name.'"', __FUNCTION__);
+ }
+ $query_fields = $this->getFieldDeclarationList($fields);
+ if (PEAR::isError($query_fields)) {
+ return $query_fields;
+ }
+ if (!empty($options['primary'])) {
+ $query_fields.= ', PRIMARY KEY ('.implode(', ', array_keys($options['primary'])).')';
+ }
+
+ $name = $db->quoteIdentifier($name, true);
+ $result = 'CREATE ';
+ if (!empty($options['temporary'])) {
+ $result .= $this->_getTemporaryTableQuery();
+ }
+ $result .= " TABLE $name ($query_fields)";
+ return $result;
+ }
+
+ // }}}
+ // {{{ _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()
+ {
+ return 'TEMPORARY';
+ }
+
+ // }}}
+ // {{{ 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',
+ * 'temporary' => true|false,
+ * );
+ * @return mixed MDB2_OK on success, a MDB2 error on failure
+ * @access public
+ */
+ function createTable($name, $fields, $options = array())
+ {
+ $query = $this->_getCreateTableQuery($name, $fields, $options);
+ if (PEAR::isError($query)) {
+ return $query;
+ }
+ $db =& $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+ $result = $db->exec($query);
+ if (PEAR::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 (PEAR::isError($db)) {
+ return $db;
+ }
+
+ $name = $db->quoteIdentifier($name, true);
+ return $db->exec("DROP TABLE $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 (PEAR::isError($db)) {
+ return $db;
+ }
+
+ $name = $db->quoteIdentifier($name, true);
+ return $db->exec("DELETE FROM $name");
+ }
+
+ // }}}
+ // {{{ 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 (PEAR::isError($db)) {
+ return $db;
+ }
+
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ 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 (PEAR::isError($db)) {
+ return $db;
+ }
+
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ 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 (PEAR::isError($db)) {
+ return $db;
+ }
+
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implementedd', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ 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 (PEAR::isError($db)) {
+ return $db;
+ }
+
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ listViews()
+
+ /**
+ * 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
+ * @access public
+ */
+ function listViews($database = null)
+ {
+ $db =& $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ listTableViews()
+
+ /**
+ * 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
+ * @access public
+ */
+ function listTableViews($table)
+ {
+ $db =& $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ '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
+ * @access public
+ */
+ function listTableTriggers($table = null)
+ {
+ $db =& $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ 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 (PEAR::isError($db)) {
+ return $db;
+ }
+
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ listTables()
+
+ /**
+ * 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
+ * @access public
+ */
+ function listTables($database = null)
+ {
+ $db =& $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ 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 (PEAR::isError($db)) {
+ return $db;
+ }
+
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ createIndex()
+
+ /**
+ * 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
+ * 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'
+ * ),
+ * '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 (PEAR::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 (array_keys($definition['fields']) as $field) {
+ $fields[] = $db->quoteIdentifier($field, true);
+ }
+ $query .= ' ('. implode(', ', $fields) . ')';
+ return $db->exec($query);
+ }
+
+ // }}}
+ // {{{ 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 (PEAR::isError($db)) {
+ return $db;
+ }
+
+ $name = $db->quoteIdentifier($db->getIndexName($name), true);
+ return $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
+ * @access public
+ */
+ function listTableIndexes($table)
+ {
+ $db =& $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ '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
+ * @return string
+ * @access protected
+ */
+ function _getAdvancedFKOptions($definition)
+ {
+ return '';
+ }
+
+ // }}}
+ // {{{ 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:
+ * <pre>
+ * array (
+ * [primary] => 0
+ * [unique] => 0
+ * [foreign] => 1
+ * [check] => 0
+ * [fields] => array (
+ * [field1name] => array() // one entry per each field covered
+ * [field2name] => array() // by the index
+ * [field3name] => array(
+ * [sorting] => ascending
+ * [position] => 3
+ * )
+ * )
+ * [references] => array(
+ * [table] => name
+ * [fields] => array(
+ * [field1name] => array( //one entry per each referenced field
+ * [position] => 1
+ * )
+ * )
+ * )
+ * [deferrable] => 0
+ * [initiallydeferred] => 0
+ * [onupdate] => CASCADE|RESTRICT|SET NULL|SET DEFAULT|NO ACTION
+ * [ondelete] => CASCADE|RESTRICT|SET NULL|SET DEFAULT|NO ACTION
+ * [match] => SIMPLE|PARTIAL|FULL
+ * );
+ * </pre>
+ * @return mixed MDB2_OK on success, a MDB2 error on failure
+ * @access public
+ */
+ function createConstraint($table, $name, $definition)
+ {
+ $db =& $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+ $table = $db->quoteIdentifier($table, true);
+ $name = $db->quoteIdentifier($db->getIndexName($name), true);
+ $query = "ALTER TABLE $table ADD CONSTRAINT $name";
+ if (!empty($definition['primary'])) {
+ $query.= ' PRIMARY KEY';
+ } elseif (!empty($definition['unique'])) {
+ $query.= ' UNIQUE';
+ } elseif (!empty($definition['foreign'])) {
+ $query.= ' FOREIGN KEY';
+ }
+ $fields = array();
+ 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) . ')';
+ $query .= $this->_getAdvancedFKOptions($definition);
+ }
+ return $db->exec($query);
+ }
+
+ // }}}
+ // {{{ 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 (PEAR::isError($db)) {
+ return $db;
+ }
+
+ $table = $db->quoteIdentifier($table, true);
+ $name = $db->quoteIdentifier($db->getIndexName($name), true);
+ return $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
+ * @access public
+ */
+ function listTableConstraints($table)
+ {
+ $db =& $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ 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
+ * @return mixed MDB2_OK on success, a MDB2 error on failure
+ * @access public
+ */
+ function createSequence($seq_name, $start = 1)
+ {
+ $db =& $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __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($name)
+ {
+ $db =& $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ listSequences()
+
+ /**
+ * 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
+ * @access public
+ */
+ function listSequences($database = null)
+ {
+ $db =& $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+}
?>
\ No newline at end of file
-<?php\r
-// +----------------------------------------------------------------------+\r
-// | PHP versions 4 and 5 |\r
-// +----------------------------------------------------------------------+\r
-// | Copyright (c) 1998-2008 Manuel Lemos, Tomas V.V.Cox, |\r
-// | Stig. S. Bakken, Lukas Smith |\r
-// | All rights reserved. |\r
-// +----------------------------------------------------------------------+\r
-// | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB |\r
-// | API as well as database abstraction for PHP applications. |\r
-// | This LICENSE is in the BSD license style. |\r
-// | |\r
-// | Redistribution and use in source and binary forms, with or without |\r
-// | modification, are permitted provided that the following conditions |\r
-// | are met: |\r
-// | |\r
-// | Redistributions of source code must retain the above copyright |\r
-// | notice, this list of conditions and the following disclaimer. |\r
-// | |\r
-// | Redistributions in binary form must reproduce the above copyright |\r
-// | notice, this list of conditions and the following disclaimer in the |\r
-// | documentation and/or other materials provided with the distribution. |\r
-// | |\r
-// | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken, |\r
-// | Lukas Smith nor the names of his contributors may be used to endorse |\r
-// | or promote products derived from this software without specific prior|\r
-// | written permission. |\r
-// | |\r
-// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |\r
-// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |\r
-// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |\r
-// | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |\r
-// | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |\r
-// | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |\r
-// | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|\r
-// | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |\r
-// | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |\r
-// | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|\r
-// | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |\r
-// | POSSIBILITY OF SUCH DAMAGE. |\r
-// +----------------------------------------------------------------------+\r
-// | Author: Lukas Smith <smith@pooteeweet.org> |\r
-// +----------------------------------------------------------------------+\r
-//\r
-// $Id: mysql.php,v 1.113 2008/11/23 20:30:29 quipo Exp $\r
-//\r
-\r
-require_once 'MDB2/Driver/Manager/Common.php';\r
-\r
-/**\r
- * MDB2 MySQL driver for the management modules\r
- *\r
- * @package MDB2\r
- * @category Database\r
- * @author Lukas Smith <smith@pooteeweet.org>\r
- */\r
-class MDB2_Driver_Manager_mysql extends MDB2_Driver_Manager_Common\r
-{\r
-\r
- // }}}\r
- // {{{ createDatabase()\r
-\r
- /**\r
- * create a new database\r
- *\r
- * @param string $name name of the database that should be created\r
- * @param array $options array with charset, collation info\r
- *\r
- * @return mixed MDB2_OK on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function createDatabase($name, $options = array())\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- $name = $db->quoteIdentifier($name, true);\r
- $query = 'CREATE DATABASE ' . $name;\r
- if (!empty($options['charset'])) {\r
- $query .= ' DEFAULT CHARACTER SET ' . $db->quote($options['charset'], 'text');\r
- }\r
- if (!empty($options['collation'])) {\r
- $query .= ' COLLATE ' . $db->quote($options['collation'], 'text');\r
- }\r
- return $db->standaloneQuery($query, null, true);\r
- }\r
-\r
- // }}}\r
- // {{{ alterDatabase()\r
-\r
- /**\r
- * alter an existing database\r
- *\r
- * @param string $name name of the database that is intended to be changed\r
- * @param array $options array with charset, collation info\r
- *\r
- * @return mixed MDB2_OK on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function alterDatabase($name, $options = array())\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- $query = 'ALTER DATABASE '. $db->quoteIdentifier($name, true);\r
- if (!empty($options['charset'])) {\r
- $query .= ' DEFAULT CHARACTER SET ' . $db->quote($options['charset'], 'text');\r
- }\r
- if (!empty($options['collation'])) {\r
- $query .= ' COLLATE ' . $db->quote($options['collation'], 'text');\r
- }\r
- return $db->standaloneQuery($query, null, true);\r
- }\r
-\r
- // }}}\r
- // {{{ dropDatabase()\r
-\r
- /**\r
- * drop an existing database\r
- *\r
- * @param string $name name of the database that should be dropped\r
- * @return mixed MDB2_OK on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function dropDatabase($name)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- $name = $db->quoteIdentifier($name, true);\r
- $query = "DROP DATABASE $name";\r
- return $db->standaloneQuery($query, null, true);\r
- }\r
-\r
- // }}}\r
- // {{{ _getAdvancedFKOptions()\r
-\r
- /**\r
- * Return the FOREIGN KEY query section dealing with non-standard options\r
- * as MATCH, INITIALLY DEFERRED, ON UPDATE, ...\r
- *\r
- * @param array $definition\r
- * @return string\r
- * @access protected\r
- */\r
- function _getAdvancedFKOptions($definition)\r
- {\r
- $query = '';\r
- if (!empty($definition['match'])) {\r
- $query .= ' MATCH '.$definition['match'];\r
- }\r
- if (!empty($definition['onupdate'])) {\r
- $query .= ' ON UPDATE '.$definition['onupdate'];\r
- }\r
- if (!empty($definition['ondelete'])) {\r
- $query .= ' ON DELETE '.$definition['ondelete'];\r
- }\r
- return $query;\r
- }\r
-\r
- // }}}\r
- // {{{ createTable()\r
-\r
- /**\r
- * create a new table\r
- *\r
- * @param string $name Name of the database that should be created\r
- * @param array $fields Associative array that contains the definition of each field of the new table\r
- * The indexes of the array entries are the names of the fields of the table an\r
- * the array entry values are associative arrays like those that are meant to be\r
- * passed with the field definitions to get[Type]Declaration() functions.\r
- * array(\r
- * 'id' => array(\r
- * 'type' => 'integer',\r
- * 'unsigned' => 1\r
- * 'notnull' => 1\r
- * 'default' => 0\r
- * ),\r
- * 'name' => array(\r
- * 'type' => 'text',\r
- * 'length' => 12\r
- * ),\r
- * 'password' => array(\r
- * 'type' => 'text',\r
- * 'length' => 12\r
- * )\r
- * );\r
- * @param array $options An associative array of table options:\r
- * array(\r
- * 'comment' => 'Foo',\r
- * 'charset' => 'utf8',\r
- * 'collate' => 'utf8_unicode_ci',\r
- * 'type' => 'innodb',\r
- * );\r
- *\r
- * @return mixed MDB2_OK on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function createTable($name, $fields, $options = array())\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- // if we have an AUTO_INCREMENT column and a PK on more than one field,\r
- // we have to handle it differently...\r
- $autoincrement = null;\r
- if (empty($options['primary'])) {\r
- $pk_fields = array();\r
- foreach ($fields as $fieldname => $def) {\r
- if (!empty($def['primary'])) {\r
- $pk_fields[$fieldname] = true;\r
- }\r
- if (!empty($def['autoincrement'])) {\r
- $autoincrement = $fieldname;\r
- }\r
- }\r
- if (!is_null($autoincrement) && count($pk_fields) > 1) {\r
- $options['primary'] = $pk_fields;\r
- } else {\r
- // the PK constraint is on max one field => OK\r
- $autoincrement = null;\r
- }\r
- }\r
-\r
- $query = $this->_getCreateTableQuery($name, $fields, $options);\r
- if (PEAR::isError($query)) {\r
- return $query;\r
- }\r
-\r
- if (!is_null($autoincrement)) {\r
- // we have to remove the PK clause added by _getIntegerDeclaration()\r
- $query = str_replace('AUTO_INCREMENT PRIMARY KEY', 'AUTO_INCREMENT', $query);\r
- }\r
-\r
- $options_strings = array();\r
-\r
- if (!empty($options['comment'])) {\r
- $options_strings['comment'] = 'COMMENT = '.$db->quote($options['comment'], 'text');\r
- }\r
-\r
- if (!empty($options['charset'])) {\r
- $options_strings['charset'] = 'DEFAULT CHARACTER SET '.$options['charset'];\r
- if (!empty($options['collate'])) {\r
- $options_strings['charset'].= ' COLLATE '.$options['collate'];\r
- }\r
- }\r
-\r
- $type = false;\r
- if (!empty($options['type'])) {\r
- $type = $options['type'];\r
- } elseif ($db->options['default_table_type']) {\r
- $type = $db->options['default_table_type'];\r
- }\r
- if ($type) {\r
- $options_strings[] = "ENGINE = $type";\r
- }\r
-\r
- if (!empty($options_strings)) {\r
- $query .= ' '.implode(' ', $options_strings);\r
- }\r
- $result = $db->exec($query);\r
- if (PEAR::isError($result)) {\r
- return $result;\r
- }\r
- return MDB2_OK;\r
- }\r
-\r
- // }}}\r
- // {{{ dropTable()\r
-\r
- /**\r
- * drop an existing table\r
- *\r
- * @param string $name name of the table that should be dropped\r
- * @return mixed MDB2_OK on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function dropTable($name)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- //delete the triggers associated to existing FK constraints\r
- $constraints = $this->listTableConstraints($name);\r
- if (!PEAR::isError($constraints) && !empty($constraints)) {\r
- $db->loadModule('Reverse', null, true);\r
- foreach ($constraints as $constraint) {\r
- $definition = $db->reverse->getTableConstraintDefinition($name, $constraint);\r
- if (!PEAR::isError($definition) && !empty($definition['foreign'])) {\r
- $result = $this->_dropFKTriggers($name, $constraint, $definition['references']['table']);\r
- if (PEAR::isError($result)) {\r
- return $result;\r
- }\r
- }\r
- }\r
- }\r
-\r
- return parent::dropTable($name);\r
- }\r
-\r
- // }}}\r
- // {{{ truncateTable()\r
-\r
- /**\r
- * Truncate an existing table (if the TRUNCATE TABLE syntax is not supported,\r
- * it falls back to a DELETE FROM TABLE query)\r
- *\r
- * @param string $name name of the table that should be truncated\r
- * @return mixed MDB2_OK on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function truncateTable($name)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- $name = $db->quoteIdentifier($name, true);\r
- return $db->exec("TRUNCATE TABLE $name");\r
- }\r
-\r
- // }}}\r
- // {{{ vacuum()\r
-\r
- /**\r
- * Optimize (vacuum) all the tables in the db (or only the specified table)\r
- * and optionally run ANALYZE.\r
- *\r
- * @param string $table table name (all the tables if empty)\r
- * @param array $options an array with driver-specific options:\r
- * - timeout [int] (in seconds) [mssql-only]\r
- * - analyze [boolean] [pgsql and mysql]\r
- * - full [boolean] [pgsql-only]\r
- * - freeze [boolean] [pgsql-only]\r
- *\r
- * @return mixed MDB2_OK success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function vacuum($table = null, $options = array())\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- if (empty($table)) {\r
- $table = $this->listTables();\r
- if (PEAR::isError($table)) {\r
- return $table;\r
- }\r
- }\r
- if (is_array($table)) {\r
- foreach (array_keys($table) as $k) {\r
- $table[$k] = $db->quoteIdentifier($table[$k], true);\r
- }\r
- $table = implode(', ', $table);\r
- } else {\r
- $table = $db->quoteIdentifier($table, true);\r
- }\r
- \r
- $result = $db->exec('OPTIMIZE TABLE '.$table);\r
- if (PEAR::isError($result)) {\r
- return $result;\r
- }\r
- if (!empty($options['analyze'])) {\r
- return $db->exec('ANALYZE TABLE '.$table);\r
- }\r
- return MDB2_OK;\r
- }\r
-\r
- // }}}\r
- // {{{ alterTable()\r
-\r
- /**\r
- * alter an existing table\r
- *\r
- * @param string $name name of the table that is intended to be changed.\r
- * @param array $changes associative array that contains the details of each type\r
- * of change that is intended to be performed. The types of\r
- * changes that are currently supported are defined as follows:\r
- *\r
- * name\r
- *\r
- * New name for the table.\r
- *\r
- * add\r
- *\r
- * Associative array with the names of fields to be added as\r
- * indexes of the array. The value of each entry of the array\r
- * should be set to another associative array with the properties\r
- * of the fields to be added. The properties of the fields should\r
- * be the same as defined by the MDB2 parser.\r
- *\r
- *\r
- * remove\r
- *\r
- * Associative array with the names of fields to be removed as indexes\r
- * of the array. Currently the values assigned to each entry are ignored.\r
- * An empty array should be used for future compatibility.\r
- *\r
- * rename\r
- *\r
- * Associative array with the names of fields to be renamed as indexes\r
- * of the array. The value of each entry of the array should be set to\r
- * another associative array with the entry named name with the new\r
- * field name and the entry named Declaration that is expected to contain\r
- * the portion of the field declaration already in DBMS specific SQL code\r
- * as it is used in the CREATE TABLE statement.\r
- *\r
- * change\r
- *\r
- * Associative array with the names of the fields to be changed as indexes\r
- * of the array. Keep in mind that if it is intended to change either the\r
- * name of a field and any other properties, the change array entries\r
- * should have the new names of the fields as array indexes.\r
- *\r
- * The value of each entry of the array should be set to another associative\r
- * array with the properties of the fields to that are meant to be changed as\r
- * array entries. These entries should be assigned to the new values of the\r
- * respective properties. The properties of the fields should be the same\r
- * as defined by the MDB2 parser.\r
- *\r
- * Example\r
- * array(\r
- * 'name' => 'userlist',\r
- * 'add' => array(\r
- * 'quota' => array(\r
- * 'type' => 'integer',\r
- * 'unsigned' => 1\r
- * )\r
- * ),\r
- * 'remove' => array(\r
- * 'file_limit' => array(),\r
- * 'time_limit' => array()\r
- * ),\r
- * 'change' => array(\r
- * 'name' => array(\r
- * 'length' => '20',\r
- * 'definition' => array(\r
- * 'type' => 'text',\r
- * 'length' => 20,\r
- * ),\r
- * )\r
- * ),\r
- * 'rename' => array(\r
- * 'sex' => array(\r
- * 'name' => 'gender',\r
- * 'definition' => array(\r
- * 'type' => 'text',\r
- * 'length' => 1,\r
- * 'default' => 'M',\r
- * ),\r
- * )\r
- * )\r
- * )\r
- *\r
- * @param boolean $check indicates whether the function should just check if the DBMS driver\r
- * can perform the requested table alterations if the value is true or\r
- * actually perform them otherwise.\r
- * @access public\r
- *\r
- * @return mixed MDB2_OK on success, a MDB2 error on failure\r
- */\r
- function alterTable($name, $changes, $check)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- foreach ($changes as $change_name => $change) {\r
- switch ($change_name) {\r
- case 'add':\r
- case 'remove':\r
- case 'change':\r
- case 'rename':\r
- case 'name':\r
- break;\r
- default:\r
- return $db->raiseError(MDB2_ERROR_CANNOT_ALTER, null, null,\r
- 'change type "'.$change_name.'" not yet supported', __FUNCTION__);\r
- }\r
- }\r
-\r
- if ($check) {\r
- return MDB2_OK;\r
- }\r
-\r
- $query = '';\r
- if (!empty($changes['name'])) {\r
- $change_name = $db->quoteIdentifier($changes['name'], true);\r
- $query .= 'RENAME TO ' . $change_name;\r
- }\r
-\r
- if (!empty($changes['add']) && is_array($changes['add'])) {\r
- foreach ($changes['add'] as $field_name => $field) {\r
- if ($query) {\r
- $query.= ', ';\r
- }\r
- $query.= 'ADD ' . $db->getDeclaration($field['type'], $field_name, $field);\r
- }\r
- }\r
-\r
- if (!empty($changes['remove']) && is_array($changes['remove'])) {\r
- foreach ($changes['remove'] as $field_name => $field) {\r
- if ($query) {\r
- $query.= ', ';\r
- }\r
- $field_name = $db->quoteIdentifier($field_name, true);\r
- $query.= 'DROP ' . $field_name;\r
- }\r
- }\r
-\r
- $rename = array();\r
- if (!empty($changes['rename']) && is_array($changes['rename'])) {\r
- foreach ($changes['rename'] as $field_name => $field) {\r
- $rename[$field['name']] = $field_name;\r
- }\r
- }\r
-\r
- if (!empty($changes['change']) && is_array($changes['change'])) {\r
- foreach ($changes['change'] as $field_name => $field) {\r
- if ($query) {\r
- $query.= ', ';\r
- }\r
- if (isset($rename[$field_name])) {\r
- $old_field_name = $rename[$field_name];\r
- unset($rename[$field_name]);\r
- } else {\r
- $old_field_name = $field_name;\r
- }\r
- $old_field_name = $db->quoteIdentifier($old_field_name, true);\r
- $query.= "CHANGE $old_field_name " . $db->getDeclaration($field['definition']['type'], $field_name, $field['definition']);\r
- }\r
- }\r
-\r
- if (!empty($rename) && is_array($rename)) {\r
- foreach ($rename as $rename_name => $renamed_field) {\r
- if ($query) {\r
- $query.= ', ';\r
- }\r
- $field = $changes['rename'][$renamed_field];\r
- $renamed_field = $db->quoteIdentifier($renamed_field, true);\r
- $query.= 'CHANGE ' . $renamed_field . ' ' . $db->getDeclaration($field['definition']['type'], $field['name'], $field['definition']);\r
- }\r
- }\r
-\r
- if (!$query) {\r
- return MDB2_OK;\r
- }\r
-\r
- $name = $db->quoteIdentifier($name, true);\r
- return $db->exec("ALTER TABLE $name $query");\r
- }\r
-\r
- // }}}\r
- // {{{ listDatabases()\r
-\r
- /**\r
- * list all databases\r
- *\r
- * @return mixed array of database names on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function listDatabases()\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- $result = $db->queryCol('SHOW DATABASES');\r
- if (PEAR::isError($result)) {\r
- return $result;\r
- }\r
- if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {\r
- $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);\r
- }\r
- return $result;\r
- }\r
-\r
- // }}}\r
- // {{{ listUsers()\r
-\r
- /**\r
- * list all users\r
- *\r
- * @return mixed array of user names on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function listUsers()\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- return $db->queryCol('SELECT DISTINCT USER FROM mysql.USER');\r
- }\r
-\r
- // }}}\r
- // {{{ listFunctions()\r
-\r
- /**\r
- * list all functions in the current database\r
- *\r
- * @return mixed array of function names on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function listFunctions()\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- $query = "SELECT name FROM mysql.proc";\r
- /*\r
- SELECT ROUTINE_NAME\r
- FROM INFORMATION_SCHEMA.ROUTINES\r
- WHERE ROUTINE_TYPE = 'FUNCTION'\r
- */\r
- $result = $db->queryCol($query);\r
- if (PEAR::isError($result)) {\r
- return $result;\r
- }\r
- if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {\r
- $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);\r
- }\r
- return $result;\r
- }\r
-\r
- // }}}\r
- // {{{ listTableTriggers()\r
-\r
- /**\r
- * list all triggers in the database that reference a given table\r
- *\r
- * @param string table for which all referenced triggers should be found\r
- * @return mixed array of trigger names on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function listTableTriggers($table = null)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- $query = 'SHOW TRIGGERS';\r
- if (!is_null($table)) {\r
- $table = $db->quote($table, 'text');\r
- $query .= " LIKE $table";\r
- }\r
- $result = $db->queryCol($query);\r
- if (PEAR::isError($result)) {\r
- return $result;\r
- }\r
- if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {\r
- $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);\r
- }\r
- return $result;\r
- }\r
-\r
- // }}}\r
- // {{{ listTables()\r
-\r
- /**\r
- * list all tables in the current database\r
- *\r
- * @param string database, the current is default\r
- * @return mixed array of table names on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function listTables($database = null)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- $query = "SHOW /*!50002 FULL*/ TABLES";\r
- if (!is_null($database)) {\r
- $query .= " FROM $database";\r
- }\r
- $query.= "/*!50002 WHERE Table_type = 'BASE TABLE'*/";\r
-\r
- $table_names = $db->queryAll($query, null, MDB2_FETCHMODE_ORDERED);\r
- if (PEAR::isError($table_names)) {\r
- return $table_names;\r
- }\r
-\r
- $result = array();\r
- foreach ($table_names as $table) {\r
- if (!$this->_fixSequenceName($table[0], true)) {\r
- $result[] = $table[0];\r
- }\r
- }\r
- if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {\r
- $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);\r
- }\r
- return $result;\r
- }\r
-\r
- // }}}\r
- // {{{ listViews()\r
-\r
- /**\r
- * list all views in the current database\r
- *\r
- * @param string database, the current is default\r
- * @return mixed array of view names on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function listViews($database = null)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- $query = 'SHOW FULL TABLES';\r
- if (!is_null($database)) {\r
- $query.= " FROM $database";\r
- }\r
- $query.= " WHERE Table_type = 'VIEW'";\r
-\r
- $result = $db->queryCol($query);\r
- if (PEAR::isError($result)) {\r
- return $result;\r
- }\r
-\r
- if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {\r
- $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);\r
- }\r
- return $result;\r
- }\r
-\r
- // }}}\r
- // {{{ listTableFields()\r
-\r
- /**\r
- * list all fields in a table in the current database\r
- *\r
- * @param string $table name of table that should be used in method\r
- * @return mixed array of field names on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function listTableFields($table)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- $table = $db->quoteIdentifier($table, true);\r
- $result = $db->queryCol("SHOW COLUMNS FROM $table");\r
- if (PEAR::isError($result)) {\r
- return $result;\r
- }\r
- if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {\r
- $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);\r
- }\r
- return $result;\r
- }\r
-\r
- // }}}\r
- // {{{ createIndex()\r
-\r
- /**\r
- * Get the stucture of a field into an array\r
- *\r
- * @author Leoncx\r
- * @param string $table name of the table on which the index is to be created\r
- * @param string $name name of the index to be created\r
- * @param array $definition associative array that defines properties of the index to be created.\r
- * Currently, only one property named FIELDS is supported. This property\r
- * is also an associative with the names of the index fields as array\r
- * indexes. Each entry of this array is set to another type of associative\r
- * array that specifies properties of the index that are specific to\r
- * each field.\r
- *\r
- * Currently, only the sorting property is supported. It should be used\r
- * to define the sorting direction of the index. It may be set to either\r
- * ascending or descending.\r
- *\r
- * Not all DBMS support index sorting direction configuration. The DBMS\r
- * drivers of those that do not support it ignore this property. Use the\r
- * function supports() to determine whether the DBMS driver can manage indexes.\r
- *\r
- * Example\r
- * array(\r
- * 'fields' => array(\r
- * 'user_name' => array(\r
- * 'sorting' => 'ascending'\r
- * 'length' => 10\r
- * ),\r
- * 'last_login' => array()\r
- * )\r
- * )\r
- *\r
- * @return mixed MDB2_OK on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function createIndex($table, $name, $definition)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- $table = $db->quoteIdentifier($table, true);\r
- $name = $db->quoteIdentifier($db->getIndexName($name), true);\r
- $query = "CREATE INDEX $name ON $table";\r
- $fields = array();\r
- foreach ($definition['fields'] as $field => $fieldinfo) {\r
- if (!empty($fieldinfo['length'])) {\r
- $fields[] = $db->quoteIdentifier($field, true) . '(' . $fieldinfo['length'] . ')';\r
- } else {\r
- $fields[] = $db->quoteIdentifier($field, true);\r
- }\r
- }\r
- $query .= ' ('. implode(', ', $fields) . ')';\r
- return $db->exec($query);\r
- }\r
-\r
- // }}}\r
- // {{{ dropIndex()\r
-\r
- /**\r
- * drop existing index\r
- *\r
- * @param string $table name of table that should be used in method\r
- * @param string $name name of the index to be dropped\r
- * @return mixed MDB2_OK on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function dropIndex($table, $name)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- $table = $db->quoteIdentifier($table, true);\r
- $name = $db->quoteIdentifier($db->getIndexName($name), true);\r
- return $db->exec("DROP INDEX $name ON $table");\r
- }\r
-\r
- // }}}\r
- // {{{ listTableIndexes()\r
-\r
- /**\r
- * list all indexes in a table\r
- *\r
- * @param string $table name of table that should be used in method\r
- * @return mixed array of index names on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function listTableIndexes($table)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- $key_name = 'Key_name';\r
- $non_unique = 'Non_unique';\r
- if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {\r
- if ($db->options['field_case'] == CASE_LOWER) {\r
- $key_name = strtolower($key_name);\r
- $non_unique = strtolower($non_unique);\r
- } else {\r
- $key_name = strtoupper($key_name);\r
- $non_unique = strtoupper($non_unique);\r
- }\r
- }\r
-\r
- $table = $db->quoteIdentifier($table, true);\r
- $query = "SHOW INDEX FROM $table";\r
- $indexes = $db->queryAll($query, null, MDB2_FETCHMODE_ASSOC);\r
- if (PEAR::isError($indexes)) {\r
- return $indexes;\r
- }\r
-\r
- $result = array();\r
- foreach ($indexes as $index_data) {\r
- if ($index_data[$non_unique] && ($index = $this->_fixIndexName($index_data[$key_name]))) {\r
- $result[$index] = true;\r
- }\r
- }\r
-\r
- if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {\r
- $result = array_change_key_case($result, $db->options['field_case']);\r
- }\r
- return array_keys($result);\r
- }\r
-\r
- // }}}\r
- // {{{ createConstraint()\r
-\r
- /**\r
- * create a constraint on a table\r
- *\r
- * @param string $table name of the table on which the constraint is to be created\r
- * @param string $name name of the constraint to be created\r
- * @param array $definition associative array that defines properties of the constraint to be created.\r
- * Currently, only one property named FIELDS is supported. This property\r
- * is also an associative with the names of the constraint fields as array\r
- * constraints. Each entry of this array is set to another type of associative\r
- * array that specifies properties of the constraint that are specific to\r
- * each field.\r
- *\r
- * Example\r
- * array(\r
- * 'fields' => array(\r
- * 'user_name' => array(),\r
- * 'last_login' => array()\r
- * )\r
- * )\r
- * @return mixed MDB2_OK on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function createConstraint($table, $name, $definition)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- $type = '';\r
- $idx_name = $db->quoteIdentifier($db->getIndexName($name), true);\r
- if (!empty($definition['primary'])) {\r
- $type = 'PRIMARY';\r
- $idx_name = 'KEY';\r
- } elseif (!empty($definition['unique'])) {\r
- $type = 'UNIQUE';\r
- } elseif (!empty($definition['foreign'])) {\r
- $type = 'CONSTRAINT';\r
- }\r
- if (empty($type)) {\r
- return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,\r
- 'invalid definition, could not create constraint', __FUNCTION__);\r
- }\r
-\r
- $table_quoted = $db->quoteIdentifier($table, true);\r
- $query = "ALTER TABLE $table_quoted ADD $type $idx_name";\r
- if (!empty($definition['foreign'])) {\r
- $query .= ' FOREIGN KEY';\r
- }\r
- $fields = array();\r
- foreach ($definition['fields'] as $field => $fieldinfo) {\r
- $quoted = $db->quoteIdentifier($field, true);\r
- if (!empty($fieldinfo['length'])) {\r
- $quoted .= '(' . $fieldinfo['length'] . ')';\r
- }\r
- $fields[] = $quoted;\r
- }\r
- $query .= ' ('. implode(', ', $fields) . ')';\r
- if (!empty($definition['foreign'])) {\r
- $query.= ' REFERENCES ' . $db->quoteIdentifier($definition['references']['table'], true);\r
- $referenced_fields = array();\r
- foreach (array_keys($definition['references']['fields']) as $field) {\r
- $referenced_fields[] = $db->quoteIdentifier($field, true);\r
- }\r
- $query .= ' ('. implode(', ', $referenced_fields) . ')';\r
- $query .= $this->_getAdvancedFKOptions($definition);\r
-\r
- // add index on FK column(s) or we can't add a FK constraint\r
- // @see http://forums.mysql.com/read.php?22,19755,226009\r
- $result = $this->createIndex($table, $name.'_fkidx', $definition);\r
- if (PEAR::isError($result)) {\r
- return $result;\r
- }\r
- }\r
- $res = $db->exec($query);\r
- if (PEAR::isError($res)) {\r
- return $res;\r
- }\r
- if (!empty($definition['foreign'])) {\r
- return $this->_createFKTriggers($table, array($name => $definition));\r
- }\r
- return MDB2_OK;\r
- }\r
-\r
- // }}}\r
- // {{{ dropConstraint()\r
-\r
- /**\r
- * drop existing constraint\r
- *\r
- * @param string $table name of table that should be used in method\r
- * @param string $name name of the constraint to be dropped\r
- * @param string $primary hint if the constraint is primary\r
- * @return mixed MDB2_OK on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function dropConstraint($table, $name, $primary = false)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- if ($primary || strtolower($name) == 'primary') {\r
- $query = 'ALTER TABLE '. $db->quoteIdentifier($table, true) .' DROP PRIMARY KEY';\r
- return $db->exec($query);\r
- }\r
-\r
- //is it a FK constraint? If so, also delete the associated triggers\r
- $db->loadModule('Reverse', null, true);\r
- $definition = $db->reverse->getTableConstraintDefinition($table, $name);\r
- if (!PEAR::isError($definition) && !empty($definition['foreign'])) {\r
- //first drop the FK enforcing triggers\r
- $result = $this->_dropFKTriggers($table, $name, $definition['references']['table']);\r
- if (PEAR::isError($result)) {\r
- return $result;\r
- }\r
- //then drop the constraint itself\r
- $table = $db->quoteIdentifier($table, true);\r
- $name = $db->quoteIdentifier($db->getIndexName($name), true);\r
- $query = "ALTER TABLE $table DROP FOREIGN KEY $name";\r
- return $db->exec($query);\r
- }\r
-\r
- $table = $db->quoteIdentifier($table, true);\r
- $name = $db->quoteIdentifier($db->getIndexName($name), true);\r
- $query = "ALTER TABLE $table DROP INDEX $name";\r
- return $db->exec($query);\r
- }\r
-\r
- // }}}\r
- // {{{ _createFKTriggers()\r
-\r
- /**\r
- * Create triggers to enforce the FOREIGN KEY constraint on the table\r
- *\r
- * NB: since there's no RAISE_APPLICATION_ERROR facility in mysql,\r
- * we call a non-existent procedure to raise the FK violation message.\r
- * @see http://forums.mysql.com/read.php?99,55108,71877#msg-71877\r
- *\r
- * @param string $table table name\r
- * @param array $foreign_keys FOREIGN KEY definitions\r
- *\r
- * @return mixed MDB2_OK on success, a MDB2 error on failure\r
- * @access private\r
- */\r
- function _createFKTriggers($table, $foreign_keys)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
- // create triggers to enforce FOREIGN KEY constraints\r
- if ($db->supports('triggers') && !empty($foreign_keys)) {\r
- $table_quoted = $db->quoteIdentifier($table, true);\r
- foreach ($foreign_keys as $fkname => $fkdef) {\r
- if (empty($fkdef)) {\r
- continue;\r
- }\r
- //set actions to default if not set\r
- $fkdef['onupdate'] = empty($fkdef['onupdate']) ? $db->options['default_fk_action_onupdate'] : strtoupper($fkdef['onupdate']);\r
- $fkdef['ondelete'] = empty($fkdef['ondelete']) ? $db->options['default_fk_action_ondelete'] : strtoupper($fkdef['ondelete']);\r
-\r
- $trigger_names = array(\r
- 'insert' => $fkname.'_insert_trg',\r
- 'update' => $fkname.'_update_trg',\r
- 'pk_update' => $fkname.'_pk_update_trg',\r
- 'pk_delete' => $fkname.'_pk_delete_trg',\r
- );\r
- $table_fields = array_keys($fkdef['fields']);\r
- $referenced_fields = array_keys($fkdef['references']['fields']);\r
-\r
- //create the ON [UPDATE|DELETE] triggers on the primary table\r
- $restrict_action = ' IF (SELECT ';\r
- $aliased_fields = array();\r
- foreach ($table_fields as $field) {\r
- $aliased_fields[] = $table_quoted .'.'.$field .' AS '.$field;\r
- }\r
- $restrict_action .= implode(',', $aliased_fields)\r
- .' FROM '.$table_quoted\r
- .' WHERE ';\r
- $conditions = array();\r
- $new_values = array();\r
- $null_values = array();\r
- for ($i=0; $i<count($table_fields); $i++) {\r
- $conditions[] = $table_fields[$i] .' = OLD.'.$referenced_fields[$i];\r
- $new_values[] = $table_fields[$i] .' = NEW.'.$referenced_fields[$i];\r
- $null_values[] = $table_fields[$i] .' = NULL';\r
- }\r
- $conditions2 = array();\r
- for ($i=0; $i<count($referenced_fields); $i++) {\r
- $conditions2[] = 'NEW.'.$referenced_fields[$i] .' <> OLD.'.$referenced_fields[$i];\r
- }\r
- $restrict_action .= implode(' AND ', $conditions).') IS NOT NULL'\r
- .' AND (' .implode(' OR ', $conditions2) .')'\r
- .' THEN CALL %s_ON_TABLE_'.$table.'_VIOLATES_FOREIGN_KEY_CONSTRAINT();'\r
- .' END IF;';\r
-\r
- $cascade_action_update = 'UPDATE '.$table_quoted.' SET '.implode(', ', $new_values) .' WHERE '.implode(' AND ', $conditions). ';';\r
- $cascade_action_delete = 'DELETE FROM '.$table_quoted.' WHERE '.implode(' AND ', $conditions). ';';\r
- $setnull_action = 'UPDATE '.$table_quoted.' SET '.implode(', ', $null_values).' WHERE '.implode(' AND ', $conditions). ';';\r
-\r
- if ('SET DEFAULT' == $fkdef['onupdate'] || 'SET DEFAULT' == $fkdef['ondelete']) {\r
- $db->loadModule('Reverse', null, true);\r
- $default_values = array();\r
- foreach ($table_fields as $table_field) {\r
- $field_definition = $db->reverse->getTableFieldDefinition($table, $field);\r
- if (PEAR::isError($field_definition)) {\r
- return $field_definition;\r
- }\r
- $default_values[] = $table_field .' = '. $field_definition[0]['default'];\r
- }\r
- $setdefault_action = 'UPDATE '.$table_quoted.' SET '.implode(', ', $default_values).' WHERE '.implode(' AND ', $conditions). ';';\r
- }\r
-\r
- $query = 'CREATE TRIGGER %s'\r
- .' %s ON '.$fkdef['references']['table']\r
- .' FOR EACH ROW BEGIN '\r
- .' SET FOREIGN_KEY_CHECKS = 0; '; //only really needed for ON UPDATE CASCADE\r
-\r
- if ('CASCADE' == $fkdef['onupdate']) {\r
- $sql_update = sprintf($query, $trigger_names['pk_update'], 'BEFORE UPDATE', 'update') . $cascade_action_update;\r
- } elseif ('SET NULL' == $fkdef['onupdate']) {\r
- $sql_update = sprintf($query, $trigger_names['pk_update'], 'BEFORE UPDATE', 'update') . $setnull_action;\r
- } elseif ('SET DEFAULT' == $fkdef['onupdate']) {\r
- $sql_update = sprintf($query, $trigger_names['pk_update'], 'BEFORE UPDATE', 'update') . $setdefault_action;\r
- } elseif ('NO ACTION' == $fkdef['onupdate']) {\r
- $sql_update = sprintf($query.$restrict_action, $trigger_names['pk_update'], 'AFTER UPDATE', 'update');\r
- } elseif ('RESTRICT' == $fkdef['onupdate']) {\r
- $sql_update = sprintf($query.$restrict_action, $trigger_names['pk_update'], 'BEFORE UPDATE', 'update');\r
- }\r
- if ('CASCADE' == $fkdef['ondelete']) {\r
- $sql_delete = sprintf($query, $trigger_names['pk_delete'], 'BEFORE DELETE', 'delete') . $cascade_action_delete;\r
- } elseif ('SET NULL' == $fkdef['ondelete']) {\r
- $sql_delete = sprintf($query, $trigger_names['pk_delete'], 'BEFORE DELETE', 'delete') . $setnull_action;\r
- } elseif ('SET DEFAULT' == $fkdef['ondelete']) {\r
- $sql_delete = sprintf($query, $trigger_names['pk_delete'], 'BEFORE DELETE', 'delete') . $setdefault_action;\r
- } elseif ('NO ACTION' == $fkdef['ondelete']) {\r
- $sql_delete = sprintf($query.$restrict_action, $trigger_names['pk_delete'], 'AFTER DELETE', 'delete');\r
- } elseif ('RESTRICT' == $fkdef['ondelete']) {\r
- $sql_delete = sprintf($query.$restrict_action, $trigger_names['pk_delete'], 'BEFORE DELETE', 'delete');\r
- }\r
- $sql_update .= ' SET FOREIGN_KEY_CHECKS = 1; END;';\r
- $sql_delete .= ' SET FOREIGN_KEY_CHECKS = 1; END;';\r
-\r
- $db->pushErrorHandling(PEAR_ERROR_RETURN);\r
- $db->expectError(MDB2_ERROR_CANNOT_CREATE);\r
- $result = $db->exec($sql_delete);\r
- $expected_errmsg = 'This MySQL version doesn\'t support multiple triggers with the same action time and event for one table';\r
- $db->popExpect();\r
- $db->popErrorHandling();\r
- if (PEAR::isError($result)) {\r
- if ($result->getCode() != MDB2_ERROR_CANNOT_CREATE) {\r
- return $result;\r
- }\r
- $db->warnings[] = $expected_errmsg;\r
- }\r
- $db->pushErrorHandling(PEAR_ERROR_RETURN);\r
- $db->expectError(MDB2_ERROR_CANNOT_CREATE);\r
- $result = $db->exec($sql_update);\r
- $db->popExpect();\r
- $db->popErrorHandling();\r
- if (PEAR::isError($result) && $result->getCode() != MDB2_ERROR_CANNOT_CREATE) {\r
- if ($result->getCode() != MDB2_ERROR_CANNOT_CREATE) {\r
- return $result;\r
- }\r
- $db->warnings[] = $expected_errmsg;\r
- }\r
- }\r
- }\r
- return MDB2_OK;\r
- }\r
-\r
- // }}}\r
- // {{{ _dropFKTriggers()\r
-\r
- /**\r
- * Drop the triggers created to enforce the FOREIGN KEY constraint on the table\r
- *\r
- * @param string $table table name\r
- * @param string $fkname FOREIGN KEY constraint name\r
- * @param string $referenced_table referenced table name\r
- *\r
- * @return mixed MDB2_OK on success, a MDB2 error on failure\r
- * @access private\r
- */\r
- function _dropFKTriggers($table, $fkname, $referenced_table)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- $triggers = $this->listTableTriggers($table);\r
- $triggers2 = $this->listTableTriggers($referenced_table);\r
- if (!PEAR::isError($triggers2) && !PEAR::isError($triggers)) {\r
- $triggers = array_merge($triggers, $triggers2);\r
- $pattern = '/^'.$fkname.'(_pk)?_(insert|update|delete)_trg$/i';\r
- foreach ($triggers as $trigger) {\r
- if (preg_match($pattern, $trigger)) {\r
- $result = $db->exec('DROP TRIGGER '.$trigger);\r
- if (PEAR::isError($result)) {\r
- return $result;\r
- }\r
- }\r
- }\r
- }\r
- return MDB2_OK;\r
- }\r
-\r
- // }}}\r
- // {{{ listTableConstraints()\r
-\r
- /**\r
- * list all constraints in a table\r
- *\r
- * @param string $table name of table that should be used in method\r
- * @return mixed array of constraint names on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function listTableConstraints($table)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- $key_name = 'Key_name';\r
- $non_unique = 'Non_unique';\r
- if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {\r
- if ($db->options['field_case'] == CASE_LOWER) {\r
- $key_name = strtolower($key_name);\r
- $non_unique = strtolower($non_unique);\r
- } else {\r
- $key_name = strtoupper($key_name);\r
- $non_unique = strtoupper($non_unique);\r
- }\r
- }\r
-\r
- $query = 'SHOW INDEX FROM ' . $db->quoteIdentifier($table, true);\r
- $indexes = $db->queryAll($query, null, MDB2_FETCHMODE_ASSOC);\r
- if (PEAR::isError($indexes)) {\r
- return $indexes;\r
- }\r
-\r
- $result = array();\r
- foreach ($indexes as $index_data) {\r
- if (!$index_data[$non_unique]) {\r
- if ($index_data[$key_name] !== 'PRIMARY') {\r
- $index = $this->_fixIndexName($index_data[$key_name]);\r
- } else {\r
- $index = 'PRIMARY';\r
- }\r
- if (!empty($index)) {\r
- $result[$index] = true;\r
- }\r
- }\r
- }\r
- \r
- //list FOREIGN KEY constraints...\r
- $query = 'SHOW CREATE TABLE '. $db->escape($table);\r
- $definition = $db->queryOne($query, 'text', 1);\r
- if (!PEAR::isError($definition) && !empty($definition)) {\r
- $pattern = '/\bCONSTRAINT\b\s+([^\s]+)\s+\bFOREIGN KEY\b/Uims';\r
- if (preg_match_all($pattern, str_replace('`', '', $definition), $matches) > 0) {\r
- foreach ($matches[1] as $constraint) {\r
- $result[$constraint] = true;\r
- }\r
- }\r
- }\r
-\r
- if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {\r
- $result = array_change_key_case($result, $db->options['field_case']);\r
- }\r
- return array_keys($result);\r
- }\r
-\r
- // }}}\r
- // {{{ createSequence()\r
-\r
- /**\r
- * create sequence\r
- *\r
- * @param string $seq_name name of the sequence to be created\r
- * @param string $start start value of the sequence; default is 1\r
- * @param array $options An associative array of table options:\r
- * array(\r
- * 'comment' => 'Foo',\r
- * 'charset' => 'utf8',\r
- * 'collate' => 'utf8_unicode_ci',\r
- * 'type' => 'innodb',\r
- * );\r
- * @return mixed MDB2_OK on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function createSequence($seq_name, $start = 1, $options = array())\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- $sequence_name = $db->quoteIdentifier($db->getSequenceName($seq_name), true);\r
- $seqcol_name = $db->quoteIdentifier($db->options['seqcol_name'], true);\r
- \r
- $options_strings = array();\r
-\r
- if (!empty($options['comment'])) {\r
- $options_strings['comment'] = 'COMMENT = '.$db->quote($options['comment'], 'text');\r
- }\r
-\r
- if (!empty($options['charset'])) {\r
- $options_strings['charset'] = 'DEFAULT CHARACTER SET '.$options['charset'];\r
- if (!empty($options['collate'])) {\r
- $options_strings['charset'].= ' COLLATE '.$options['collate'];\r
- }\r
- }\r
-\r
- $type = false;\r
- if (!empty($options['type'])) {\r
- $type = $options['type'];\r
- } elseif ($db->options['default_table_type']) {\r
- $type = $db->options['default_table_type'];\r
- }\r
- if ($type) {\r
- $options_strings[] = "ENGINE = $type";\r
- }\r
-\r
- $query = "CREATE TABLE $sequence_name ($seqcol_name INT NOT NULL AUTO_INCREMENT, PRIMARY KEY ($seqcol_name))";\r
- if (!empty($options_strings)) {\r
- $query .= ' '.implode(' ', $options_strings);\r
- }\r
- $res = $db->exec($query);\r
- if (PEAR::isError($res)) {\r
- return $res;\r
- }\r
-\r
- if ($start == 1) {\r
- return MDB2_OK;\r
- }\r
-\r
- $query = "INSERT INTO $sequence_name ($seqcol_name) VALUES (".($start-1).')';\r
- $res = $db->exec($query);\r
- if (!PEAR::isError($res)) {\r
- return MDB2_OK;\r
- }\r
-\r
- // Handle error\r
- $result = $db->exec("DROP TABLE $sequence_name");\r
- if (PEAR::isError($result)) {\r
- return $db->raiseError($result, null, null,\r
- 'could not drop inconsistent sequence table', __FUNCTION__);\r
- }\r
-\r
- return $db->raiseError($res, null, null,\r
- 'could not create sequence table', __FUNCTION__);\r
- }\r
-\r
- // }}}\r
- // {{{ dropSequence()\r
-\r
- /**\r
- * drop existing sequence\r
- *\r
- * @param string $seq_name name of the sequence to be dropped\r
- * @return mixed MDB2_OK on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function dropSequence($seq_name)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- $sequence_name = $db->quoteIdentifier($db->getSequenceName($seq_name), true);\r
- return $db->exec("DROP TABLE $sequence_name");\r
- }\r
-\r
- // }}}\r
- // {{{ listSequences()\r
-\r
- /**\r
- * list all sequences in the current database\r
- *\r
- * @param string database, the current is default\r
- * @return mixed array of sequence names on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function listSequences($database = null)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- $query = "SHOW TABLES";\r
- if (!is_null($database)) {\r
- $query .= " FROM $database";\r
- }\r
- $table_names = $db->queryCol($query);\r
- if (PEAR::isError($table_names)) {\r
- return $table_names;\r
- }\r
-\r
- $result = array();\r
- foreach ($table_names as $table_name) {\r
- if ($sqn = $this->_fixSequenceName($table_name, true)) {\r
- $result[] = $sqn;\r
- }\r
- }\r
- if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {\r
- $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);\r
- }\r
- return $result;\r
- }\r
-\r
- // }}}\r
-}\r
+<?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: mysql.php,v 1.113 2008/11/23 20:30:29 quipo Exp $
+//
+
+require_once 'MDB2/Driver/Manager/Common.php';
+
+/**
+ * MDB2 MySQL driver for the management modules
+ *
+ * @package MDB2
+ * @category Database
+ * @author Lukas Smith <smith@pooteeweet.org>
+ */
+class MDB2_Driver_Manager_mysql 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 (PEAR::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 (PEAR::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 (PEAR::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 (PEAR::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 (!is_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 (PEAR::isError($query)) {
+ return $query;
+ }
+
+ if (!is_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 (PEAR::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 (PEAR::isError($db)) {
+ return $db;
+ }
+
+ //delete the triggers associated to existing FK constraints
+ $constraints = $this->listTableConstraints($name);
+ if (!PEAR::isError($constraints) && !empty($constraints)) {
+ $db->loadModule('Reverse', null, true);
+ foreach ($constraints as $constraint) {
+ $definition = $db->reverse->getTableConstraintDefinition($name, $constraint);
+ if (!PEAR::isError($definition) && !empty($definition['foreign'])) {
+ $result = $this->_dropFKTriggers($name, $constraint, $definition['references']['table']);
+ if (PEAR::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 (PEAR::isError($db)) {
+ return $db;
+ }
+
+ $name = $db->quoteIdentifier($name, true);
+ return $db->exec("TRUNCATE TABLE $name");
+ }
+
+ // }}}
+ // {{{ 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 (PEAR::isError($db)) {
+ return $db;
+ }
+
+ if (empty($table)) {
+ $table = $this->listTables();
+ if (PEAR::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 (PEAR::isError($result)) {
+ return $result;
+ }
+ if (!empty($options['analyze'])) {
+ return $db->exec('ANALYZE TABLE '.$table);
+ }
+ 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 (PEAR::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);
+ return $db->exec("ALTER TABLE $name $query");
+ }
+
+ // }}}
+ // {{{ 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 (PEAR::isError($db)) {
+ return $db;
+ }
+
+ $result = $db->queryCol('SHOW DATABASES');
+ if (PEAR::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 (PEAR::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 (PEAR::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 (PEAR::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 (PEAR::isError($db)) {
+ return $db;
+ }
+
+ $query = 'SHOW TRIGGERS';
+ if (!is_null($table)) {
+ $table = $db->quote($table, 'text');
+ $query .= " LIKE $table";
+ }
+ $result = $db->queryCol($query);
+ if (PEAR::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 (PEAR::isError($db)) {
+ return $db;
+ }
+
+ $query = "SHOW /*!50002 FULL*/ TABLES";
+ if (!is_null($database)) {
+ $query .= " FROM $database";
+ }
+ $query.= "/*!50002 WHERE Table_type = 'BASE TABLE'*/";
+
+ $table_names = $db->queryAll($query, null, MDB2_FETCHMODE_ORDERED);
+ if (PEAR::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 (PEAR::isError($db)) {
+ return $db;
+ }
+
+ $query = 'SHOW FULL TABLES';
+ if (!is_null($database)) {
+ $query.= " FROM $database";
+ }
+ $query.= " WHERE Table_type = 'VIEW'";
+
+ $result = $db->queryCol($query);
+ if (PEAR::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 (PEAR::isError($db)) {
+ return $db;
+ }
+
+ $table = $db->quoteIdentifier($table, true);
+ $result = $db->queryCol("SHOW COLUMNS FROM $table");
+ if (PEAR::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 (PEAR::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) . ')';
+ return $db->exec($query);
+ }
+
+ // }}}
+ // {{{ 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 (PEAR::isError($db)) {
+ return $db;
+ }
+
+ $table = $db->quoteIdentifier($table, true);
+ $name = $db->quoteIdentifier($db->getIndexName($name), true);
+ return $db->exec("DROP INDEX $name ON $table");
+ }
+
+ // }}}
+ // {{{ 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 (PEAR::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 (PEAR::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 (PEAR::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 (PEAR::isError($result)) {
+ return $result;
+ }
+ }
+ $res = $db->exec($query);
+ if (PEAR::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 (PEAR::isError($db)) {
+ return $db;
+ }
+
+ if ($primary || strtolower($name) == 'primary') {
+ $query = 'ALTER TABLE '. $db->quoteIdentifier($table, true) .' DROP PRIMARY KEY';
+ return $db->exec($query);
+ }
+
+ //is it a FK constraint? If so, also delete the associated triggers
+ $db->loadModule('Reverse', null, true);
+ $definition = $db->reverse->getTableConstraintDefinition($table, $name);
+ if (!PEAR::isError($definition) && !empty($definition['foreign'])) {
+ //first drop the FK enforcing triggers
+ $result = $this->_dropFKTriggers($table, $name, $definition['references']['table']);
+ if (PEAR::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";
+ return $db->exec($query);
+ }
+
+ $table = $db->quoteIdentifier($table, true);
+ $name = $db->quoteIdentifier($db->getIndexName($name), true);
+ $query = "ALTER TABLE $table DROP INDEX $name";
+ return $db->exec($query);
+ }
+
+ // }}}
+ // {{{ _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 (PEAR::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'
+ .' AND (' .implode(' OR ', $conditions2) .')'
+ .' THEN CALL %s_ON_TABLE_'.$table.'_VIOLATES_FOREIGN_KEY_CONSTRAINT();'
+ .' END IF;';
+
+ $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 (PEAR::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, $trigger_names['pk_update'], 'AFTER UPDATE', 'update');
+ } elseif ('RESTRICT' == $fkdef['onupdate']) {
+ $sql_update = sprintf($query.$restrict_action, $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, $trigger_names['pk_delete'], 'AFTER DELETE', 'delete');
+ } elseif ('RESTRICT' == $fkdef['ondelete']) {
+ $sql_delete = sprintf($query.$restrict_action, $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 (PEAR::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 (PEAR::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 (PEAR::isError($db)) {
+ return $db;
+ }
+
+ $triggers = $this->listTableTriggers($table);
+ $triggers2 = $this->listTableTriggers($referenced_table);
+ if (!PEAR::isError($triggers2) && !PEAR::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 (PEAR::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 (PEAR::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 (PEAR::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 (!PEAR::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 (PEAR::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 (PEAR::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 (!PEAR::isError($res)) {
+ return MDB2_OK;
+ }
+
+ // Handle error
+ $result = $db->exec("DROP TABLE $sequence_name");
+ if (PEAR::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 (PEAR::isError($db)) {
+ return $db;
+ }
+
+ $sequence_name = $db->quoteIdentifier($db->getSequenceName($seq_name), true);
+ return $db->exec("DROP TABLE $sequence_name");
+ }
+
+ // }}}
+ // {{{ 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 (PEAR::isError($db)) {
+ return $db;
+ }
+
+ $query = "SHOW TABLES";
+ if (!is_null($database)) {
+ $query .= " FROM $database";
+ }
+ $table_names = $db->queryCol($query);
+ if (PEAR::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;
+ }
+
+ // }}}
+}
?>
\ No newline at end of file
-<?php\r
-// +----------------------------------------------------------------------+\r
-// | PHP versions 4 and 5 |\r
-// +----------------------------------------------------------------------+\r
-// | Copyright (c) 1998-2007 Manuel Lemos, Tomas V.V.Cox, |\r
-// | Stig. S. Bakken, Lukas Smith |\r
-// | All rights reserved. |\r
-// +----------------------------------------------------------------------+\r
-// | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB |\r
-// | API as well as database abstraction for PHP applications. |\r
-// | This LICENSE is in the BSD license style. |\r
-// | |\r
-// | Redistribution and use in source and binary forms, with or without |\r
-// | modification, are permitted provided that the following conditions |\r
-// | are met: |\r
-// | |\r
-// | Redistributions of source code must retain the above copyright |\r
-// | notice, this list of conditions and the following disclaimer. |\r
-// | |\r
-// | Redistributions in binary form must reproduce the above copyright |\r
-// | notice, this list of conditions and the following disclaimer in the |\r
-// | documentation and/or other materials provided with the distribution. |\r
-// | |\r
-// | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken, |\r
-// | Lukas Smith nor the names of his contributors may be used to endorse |\r
-// | or promote products derived from this software without specific prior|\r
-// | written permission. |\r
-// | |\r
-// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |\r
-// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |\r
-// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |\r
-// | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |\r
-// | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |\r
-// | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |\r
-// | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|\r
-// | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |\r
-// | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |\r
-// | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|\r
-// | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |\r
-// | POSSIBILITY OF SUCH DAMAGE. |\r
-// +----------------------------------------------------------------------+\r
-// | Author: Lukas Smith <smith@pooteeweet.org> |\r
-// +----------------------------------------------------------------------+\r
-//\r
-// $Id: Common.php,v 1.43 2009/01/14 15:01:21 quipo Exp $\r
-//\r
-\r
-/**\r
- * @package MDB2\r
- * @category Database\r
- */\r
-\r
-/**\r
- * These are constants for the tableInfo-function\r
- * they are bitwised or'ed. so if there are more constants to be defined\r
- * in the future, adjust MDB2_TABLEINFO_FULL accordingly\r
- */\r
-\r
-define('MDB2_TABLEINFO_ORDER', 1);\r
-define('MDB2_TABLEINFO_ORDERTABLE', 2);\r
-define('MDB2_TABLEINFO_FULL', 3);\r
-\r
-/**\r
- * Base class for the schema reverse engineering module that is extended by each MDB2 driver\r
- *\r
- * To load this module in the MDB2 object:\r
- * $mdb->loadModule('Reverse');\r
- *\r
- * @package MDB2\r
- * @category Database\r
- * @author Lukas Smith <smith@pooteeweet.org>\r
- */\r
-class MDB2_Driver_Reverse_Common extends MDB2_Module_Common\r
-{\r
- // {{{ splitTableSchema()\r
-\r
- /**\r
- * Split the "[owner|schema].table" notation into an array\r
- *\r
- * @param string $table [schema and] table name\r
- *\r
- * @return array array(schema, table)\r
- * @access private\r
- */\r
- function splitTableSchema($table)\r
- {\r
- $ret = array();\r
- if (strpos($table, '.') !== false) {\r
- return explode('.', $table);\r
- }\r
- return array(null, $table);\r
- }\r
-\r
- // }}}\r
- // {{{ getTableFieldDefinition()\r
-\r
- /**\r
- * Get the structure of a field into an array\r
- *\r
- * @param string $table name of table that should be used in method\r
- * @param string $field name of field that should be used in method\r
- * @return mixed data array on success, a MDB2 error on failure.\r
- * The returned array contains an array for each field definition,\r
- * with all or some of these indices, depending on the field data type:\r
- * [notnull] [nativetype] [length] [fixed] [default] [type] [mdb2type]\r
- * @access public\r
- */\r
- function getTableFieldDefinition($table, $field)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,\r
- 'method not implemented', __FUNCTION__);\r
- }\r
-\r
- // }}}\r
- // {{{ getTableIndexDefinition()\r
-\r
- /**\r
- * Get the structure of an index into an array\r
- *\r
- * @param string $table name of table that should be used in method\r
- * @param string $index name of index that should be used in method\r
- * @return mixed data array on success, a MDB2 error on failure\r
- * The returned array has this structure:\r
- * </pre>\r
- * array (\r
- * [fields] => array (\r
- * [field1name] => array() // one entry per each field covered\r
- * [field2name] => array() // by the index\r
- * [field3name] => array(\r
- * [sorting] => ascending\r
- * )\r
- * )\r
- * );\r
- * </pre>\r
- * @access public\r
- */\r
- function getTableIndexDefinition($table, $index)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,\r
- 'method not implemented', __FUNCTION__);\r
- }\r
-\r
- // }}}\r
- // {{{ getTableConstraintDefinition()\r
-\r
- /**\r
- * Get the structure of an constraints into an array\r
- *\r
- * @param string $table name of table that should be used in method\r
- * @param string $index name of index that should be used in method\r
- * @return mixed data array on success, a MDB2 error on failure\r
- * The returned array has this structure:\r
- * <pre>\r
- * array (\r
- * [primary] => 0\r
- * [unique] => 0\r
- * [foreign] => 1\r
- * [check] => 0\r
- * [fields] => array (\r
- * [field1name] => array() // one entry per each field covered\r
- * [field2name] => array() // by the index\r
- * [field3name] => array(\r
- * [sorting] => ascending\r
- * [position] => 3\r
- * )\r
- * )\r
- * [references] => array(\r
- * [table] => name\r
- * [fields] => array(\r
- * [field1name] => array( //one entry per each referenced field\r
- * [position] => 1\r
- * )\r
- * )\r
- * )\r
- * [deferrable] => 0\r
- * [initiallydeferred] => 0\r
- * [onupdate] => CASCADE|RESTRICT|SET NULL|SET DEFAULT|NO ACTION\r
- * [ondelete] => CASCADE|RESTRICT|SET NULL|SET DEFAULT|NO ACTION\r
- * [match] => SIMPLE|PARTIAL|FULL\r
- * );\r
- * </pre>\r
- * @access public\r
- */\r
- function getTableConstraintDefinition($table, $index)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,\r
- 'method not implemented', __FUNCTION__);\r
- }\r
-\r
- // }}}\r
- // {{{ getSequenceDefinition()\r
-\r
- /**\r
- * Get the structure of a sequence into an array\r
- *\r
- * @param string $sequence name of sequence that should be used in method\r
- * @return mixed data array on success, a MDB2 error on failure\r
- * The returned array has this structure:\r
- * <pre>\r
- * array (\r
- * [start] => n\r
- * );\r
- * </pre>\r
- * @access public\r
- */\r
- function getSequenceDefinition($sequence)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- $start = $db->currId($sequence);\r
- if (PEAR::isError($start)) {\r
- return $start;\r
- }\r
- if ($db->supports('current_id')) {\r
- $start++;\r
- } else {\r
- $db->warnings[] = 'database does not support getting current\r
- sequence value, the sequence value was incremented';\r
- }\r
- $definition = array();\r
- if ($start != 1) {\r
- $definition = array('start' => $start);\r
- }\r
- return $definition;\r
- }\r
-\r
- // }}}\r
- // {{{ getTriggerDefinition()\r
-\r
- /**\r
- * Get the structure of a trigger into an array\r
- *\r
- * EXPERIMENTAL\r
- *\r
- * WARNING: this function is experimental and may change the returned value \r
- * at any time until labelled as non-experimental\r
- *\r
- * @param string $trigger name of trigger that should be used in method\r
- * @return mixed data array on success, a MDB2 error on failure\r
- * The returned array has this structure:\r
- * <pre>\r
- * array (\r
- * [trigger_name] => 'trigger name',\r
- * [table_name] => 'table name',\r
- * [trigger_body] => 'trigger body definition',\r
- * [trigger_type] => 'BEFORE' | 'AFTER',\r
- * [trigger_event] => 'INSERT' | 'UPDATE' | 'DELETE'\r
- * //or comma separated list of multiple events, when supported\r
- * [trigger_enabled] => true|false\r
- * [trigger_comment] => 'trigger comment',\r
- * );\r
- * </pre>\r
- * The oci8 driver also returns a [when_clause] index.\r
- * @access public\r
- */\r
- function getTriggerDefinition($trigger)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,\r
- 'method not implemented', __FUNCTION__);\r
- }\r
-\r
- // }}}\r
- // {{{ tableInfo()\r
-\r
- /**\r
- * Returns information about a table or a result set\r
- *\r
- * The format of the resulting array depends on which <var>$mode</var>\r
- * you select. The sample output below is based on this query:\r
- * <pre>\r
- * SELECT tblFoo.fldID, tblFoo.fldPhone, tblBar.fldId\r
- * FROM tblFoo\r
- * JOIN tblBar ON tblFoo.fldId = tblBar.fldId\r
- * </pre>\r
- *\r
- * <ul>\r
- * <li>\r
- *\r
- * <kbd>null</kbd> (default)\r
- * <pre>\r
- * [0] => Array (\r
- * [table] => tblFoo\r
- * [name] => fldId\r
- * [type] => int\r
- * [len] => 11\r
- * [flags] => primary_key not_null\r
- * )\r
- * [1] => Array (\r
- * [table] => tblFoo\r
- * [name] => fldPhone\r
- * [type] => string\r
- * [len] => 20\r
- * [flags] =>\r
- * )\r
- * [2] => Array (\r
- * [table] => tblBar\r
- * [name] => fldId\r
- * [type] => int\r
- * [len] => 11\r
- * [flags] => primary_key not_null\r
- * )\r
- * </pre>\r
- *\r
- * </li><li>\r
- *\r
- * <kbd>MDB2_TABLEINFO_ORDER</kbd>\r
- *\r
- * <p>In addition to the information found in the default output,\r
- * a notation of the number of columns is provided by the\r
- * <samp>num_fields</samp> element while the <samp>order</samp>\r
- * element provides an array with the column names as the keys and\r
- * their location index number (corresponding to the keys in the\r
- * the default output) as the values.</p>\r
- *\r
- * <p>If a result set has identical field names, the last one is\r
- * used.</p>\r
- *\r
- * <pre>\r
- * [num_fields] => 3\r
- * [order] => Array (\r
- * [fldId] => 2\r
- * [fldTrans] => 1\r
- * )\r
- * </pre>\r
- *\r
- * </li><li>\r
- *\r
- * <kbd>MDB2_TABLEINFO_ORDERTABLE</kbd>\r
- *\r
- * <p>Similar to <kbd>MDB2_TABLEINFO_ORDER</kbd> but adds more\r
- * dimensions to the array in which the table names are keys and\r
- * the field names are sub-keys. This is helpful for queries that\r
- * join tables which have identical field names.</p>\r
- *\r
- * <pre>\r
- * [num_fields] => 3\r
- * [ordertable] => Array (\r
- * [tblFoo] => Array (\r
- * [fldId] => 0\r
- * [fldPhone] => 1\r
- * )\r
- * [tblBar] => Array (\r
- * [fldId] => 2\r
- * )\r
- * )\r
- * </pre>\r
- *\r
- * </li>\r
- * </ul>\r
- *\r
- * The <samp>flags</samp> element contains a space separated list\r
- * of extra information about the field. This data is inconsistent\r
- * between DBMS's due to the way each DBMS works.\r
- * + <samp>primary_key</samp>\r
- * + <samp>unique_key</samp>\r
- * + <samp>multiple_key</samp>\r
- * + <samp>not_null</samp>\r
- *\r
- * Most DBMS's only provide the <samp>table</samp> and <samp>flags</samp>\r
- * elements if <var>$result</var> is a table name. The following DBMS's\r
- * provide full information from queries:\r
- * + fbsql\r
- * + mysql\r
- *\r
- * If the 'portability' option has <samp>MDB2_PORTABILITY_FIX_CASE</samp>\r
- * turned on, the names of tables and fields will be lower or upper cased.\r
- *\r
- * @param object|string $result MDB2_result object from a query or a\r
- * string containing the name of a table.\r
- * While this also accepts a query result\r
- * resource identifier, this behavior is\r
- * deprecated.\r
- * @param int $mode either unused or one of the tableInfo modes:\r
- * <kbd>MDB2_TABLEINFO_ORDERTABLE</kbd>,\r
- * <kbd>MDB2_TABLEINFO_ORDER</kbd> or\r
- * <kbd>MDB2_TABLEINFO_FULL</kbd> (which does both).\r
- * These are bitwise, so the first two can be\r
- * combined using <kbd>|</kbd>.\r
- *\r
- * @return array an associative array with the information requested.\r
- * A MDB2_Error object on failure.\r
- *\r
- * @see MDB2_Driver_Common::setOption()\r
- */\r
- function tableInfo($result, $mode = null)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- if (!is_string($result)) {\r
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,\r
- 'method not implemented', __FUNCTION__);\r
- }\r
-\r
- $db->loadModule('Manager', null, true);\r
- $fields = $db->manager->listTableFields($result);\r
- if (PEAR::isError($fields)) {\r
- return $fields;\r
- }\r
-\r
- $flags = array();\r
-\r
- $idxname_format = $db->getOption('idxname_format');\r
- $db->setOption('idxname_format', '%s');\r
-\r
- $indexes = $db->manager->listTableIndexes($result);\r
- if (PEAR::isError($indexes)) {\r
- $db->setOption('idxname_format', $idxname_format);\r
- return $indexes;\r
- }\r
-\r
- foreach ($indexes as $index) {\r
- $definition = $this->getTableIndexDefinition($result, $index);\r
- if (PEAR::isError($definition)) {\r
- $db->setOption('idxname_format', $idxname_format);\r
- return $definition;\r
- }\r
- if (count($definition['fields']) > 1) {\r
- foreach ($definition['fields'] as $field => $sort) {\r
- $flags[$field] = 'multiple_key';\r
- }\r
- }\r
- }\r
-\r
- $constraints = $db->manager->listTableConstraints($result);\r
- if (PEAR::isError($constraints)) {\r
- return $constraints;\r
- }\r
-\r
- foreach ($constraints as $constraint) {\r
- $definition = $this->getTableConstraintDefinition($result, $constraint);\r
- if (PEAR::isError($definition)) {\r
- $db->setOption('idxname_format', $idxname_format);\r
- return $definition;\r
- }\r
- $flag = !empty($definition['primary'])\r
- ? 'primary_key' : (!empty($definition['unique'])\r
- ? 'unique_key' : false);\r
- if ($flag) {\r
- foreach ($definition['fields'] as $field => $sort) {\r
- if (empty($flags[$field]) || $flags[$field] != 'primary_key') {\r
- $flags[$field] = $flag;\r
- }\r
- }\r
- }\r
- }\r
-\r
- $res = array();\r
-\r
- if ($mode) {\r
- $res['num_fields'] = count($fields);\r
- }\r
-\r
- foreach ($fields as $i => $field) {\r
- $definition = $this->getTableFieldDefinition($result, $field);\r
- if (PEAR::isError($definition)) {\r
- $db->setOption('idxname_format', $idxname_format);\r
- return $definition;\r
- }\r
- $res[$i] = $definition[0];\r
- $res[$i]['name'] = $field;\r
- $res[$i]['table'] = $result;\r
- $res[$i]['type'] = preg_replace('/^([a-z]+).*$/i', '\\1', trim($definition[0]['nativetype']));\r
- // 'primary_key', 'unique_key', 'multiple_key'\r
- $res[$i]['flags'] = empty($flags[$field]) ? '' : $flags[$field];\r
- // not_null', 'unsigned', 'auto_increment', 'default_[rawencodedvalue]'\r
- if (!empty($res[$i]['notnull'])) {\r
- $res[$i]['flags'].= ' not_null';\r
- }\r
- if (!empty($res[$i]['unsigned'])) {\r
- $res[$i]['flags'].= ' unsigned';\r
- }\r
- if (!empty($res[$i]['auto_increment'])) {\r
- $res[$i]['flags'].= ' autoincrement';\r
- }\r
- if (!empty($res[$i]['default'])) {\r
- $res[$i]['flags'].= ' default_'.rawurlencode($res[$i]['default']);\r
- }\r
-\r
- if ($mode & MDB2_TABLEINFO_ORDER) {\r
- $res['order'][$res[$i]['name']] = $i;\r
- }\r
- if ($mode & MDB2_TABLEINFO_ORDERTABLE) {\r
- $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i;\r
- }\r
- }\r
-\r
- $db->setOption('idxname_format', $idxname_format);\r
- return $res;\r
- }\r
-}\r
+<?php
+// +----------------------------------------------------------------------+
+// | PHP versions 4 and 5 |
+// +----------------------------------------------------------------------+
+// | Copyright (c) 1998-2007 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: Common.php,v 1.43 2009/01/14 15:01:21 quipo Exp $
+//
+
+/**
+ * @package MDB2
+ * @category Database
+ */
+
+/**
+ * These are constants for the tableInfo-function
+ * they are bitwised or'ed. so if there are more constants to be defined
+ * in the future, adjust MDB2_TABLEINFO_FULL accordingly
+ */
+
+define('MDB2_TABLEINFO_ORDER', 1);
+define('MDB2_TABLEINFO_ORDERTABLE', 2);
+define('MDB2_TABLEINFO_FULL', 3);
+
+/**
+ * Base class for the schema reverse engineering module that is extended by each MDB2 driver
+ *
+ * To load this module in the MDB2 object:
+ * $mdb->loadModule('Reverse');
+ *
+ * @package MDB2
+ * @category Database
+ * @author Lukas Smith <smith@pooteeweet.org>
+ */
+class MDB2_Driver_Reverse_Common extends MDB2_Module_Common
+{
+ // {{{ splitTableSchema()
+
+ /**
+ * Split the "[owner|schema].table" notation into an array
+ *
+ * @param string $table [schema and] table name
+ *
+ * @return array array(schema, table)
+ * @access private
+ */
+ function splitTableSchema($table)
+ {
+ $ret = array();
+ if (strpos($table, '.') !== false) {
+ return explode('.', $table);
+ }
+ return array(null, $table);
+ }
+
+ // }}}
+ // {{{ getTableFieldDefinition()
+
+ /**
+ * Get the structure of a field into an array
+ *
+ * @param string $table name of table that should be used in method
+ * @param string $field name of field that should be used in method
+ * @return mixed data array on success, a MDB2 error on failure.
+ * The returned array contains an array for each field definition,
+ * with all or some of these indices, depending on the field data type:
+ * [notnull] [nativetype] [length] [fixed] [default] [type] [mdb2type]
+ * @access public
+ */
+ function getTableFieldDefinition($table, $field)
+ {
+ $db =& $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ getTableIndexDefinition()
+
+ /**
+ * Get the structure of an index into an array
+ *
+ * @param string $table name of table that should be used in method
+ * @param string $index name of index that should be used in method
+ * @return mixed data array on success, a MDB2 error on failure
+ * The returned array has this structure:
+ * </pre>
+ * array (
+ * [fields] => array (
+ * [field1name] => array() // one entry per each field covered
+ * [field2name] => array() // by the index
+ * [field3name] => array(
+ * [sorting] => ascending
+ * )
+ * )
+ * );
+ * </pre>
+ * @access public
+ */
+ function getTableIndexDefinition($table, $index)
+ {
+ $db =& $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ getTableConstraintDefinition()
+
+ /**
+ * Get the structure of an constraints into an array
+ *
+ * @param string $table name of table that should be used in method
+ * @param string $index name of index that should be used in method
+ * @return mixed data array on success, a MDB2 error on failure
+ * The returned array has this structure:
+ * <pre>
+ * array (
+ * [primary] => 0
+ * [unique] => 0
+ * [foreign] => 1
+ * [check] => 0
+ * [fields] => array (
+ * [field1name] => array() // one entry per each field covered
+ * [field2name] => array() // by the index
+ * [field3name] => array(
+ * [sorting] => ascending
+ * [position] => 3
+ * )
+ * )
+ * [references] => array(
+ * [table] => name
+ * [fields] => array(
+ * [field1name] => array( //one entry per each referenced field
+ * [position] => 1
+ * )
+ * )
+ * )
+ * [deferrable] => 0
+ * [initiallydeferred] => 0
+ * [onupdate] => CASCADE|RESTRICT|SET NULL|SET DEFAULT|NO ACTION
+ * [ondelete] => CASCADE|RESTRICT|SET NULL|SET DEFAULT|NO ACTION
+ * [match] => SIMPLE|PARTIAL|FULL
+ * );
+ * </pre>
+ * @access public
+ */
+ function getTableConstraintDefinition($table, $index)
+ {
+ $db =& $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ getSequenceDefinition()
+
+ /**
+ * Get the structure of a sequence into an array
+ *
+ * @param string $sequence name of sequence that should be used in method
+ * @return mixed data array on success, a MDB2 error on failure
+ * The returned array has this structure:
+ * <pre>
+ * array (
+ * [start] => n
+ * );
+ * </pre>
+ * @access public
+ */
+ function getSequenceDefinition($sequence)
+ {
+ $db =& $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ $start = $db->currId($sequence);
+ if (PEAR::isError($start)) {
+ return $start;
+ }
+ if ($db->supports('current_id')) {
+ $start++;
+ } else {
+ $db->warnings[] = 'database does not support getting current
+ sequence value, the sequence value was incremented';
+ }
+ $definition = array();
+ if ($start != 1) {
+ $definition = array('start' => $start);
+ }
+ return $definition;
+ }
+
+ // }}}
+ // {{{ getTriggerDefinition()
+
+ /**
+ * Get the structure of a trigger into an array
+ *
+ * EXPERIMENTAL
+ *
+ * WARNING: this function is experimental and may change the returned value
+ * at any time until labelled as non-experimental
+ *
+ * @param string $trigger name of trigger that should be used in method
+ * @return mixed data array on success, a MDB2 error on failure
+ * The returned array has this structure:
+ * <pre>
+ * array (
+ * [trigger_name] => 'trigger name',
+ * [table_name] => 'table name',
+ * [trigger_body] => 'trigger body definition',
+ * [trigger_type] => 'BEFORE' | 'AFTER',
+ * [trigger_event] => 'INSERT' | 'UPDATE' | 'DELETE'
+ * //or comma separated list of multiple events, when supported
+ * [trigger_enabled] => true|false
+ * [trigger_comment] => 'trigger comment',
+ * );
+ * </pre>
+ * The oci8 driver also returns a [when_clause] index.
+ * @access public
+ */
+ function getTriggerDefinition($trigger)
+ {
+ $db =& $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ tableInfo()
+
+ /**
+ * Returns information about a table or a result set
+ *
+ * The format of the resulting array depends on which <var>$mode</var>
+ * you select. The sample output below is based on this query:
+ * <pre>
+ * SELECT tblFoo.fldID, tblFoo.fldPhone, tblBar.fldId
+ * FROM tblFoo
+ * JOIN tblBar ON tblFoo.fldId = tblBar.fldId
+ * </pre>
+ *
+ * <ul>
+ * <li>
+ *
+ * <kbd>null</kbd> (default)
+ * <pre>
+ * [0] => Array (
+ * [table] => tblFoo
+ * [name] => fldId
+ * [type] => int
+ * [len] => 11
+ * [flags] => primary_key not_null
+ * )
+ * [1] => Array (
+ * [table] => tblFoo
+ * [name] => fldPhone
+ * [type] => string
+ * [len] => 20
+ * [flags] =>
+ * )
+ * [2] => Array (
+ * [table] => tblBar
+ * [name] => fldId
+ * [type] => int
+ * [len] => 11
+ * [flags] => primary_key not_null
+ * )
+ * </pre>
+ *
+ * </li><li>
+ *
+ * <kbd>MDB2_TABLEINFO_ORDER</kbd>
+ *
+ * <p>In addition to the information found in the default output,
+ * a notation of the number of columns is provided by the
+ * <samp>num_fields</samp> element while the <samp>order</samp>
+ * element provides an array with the column names as the keys and
+ * their location index number (corresponding to the keys in the
+ * the default output) as the values.</p>
+ *
+ * <p>If a result set has identical field names, the last one is
+ * used.</p>
+ *
+ * <pre>
+ * [num_fields] => 3
+ * [order] => Array (
+ * [fldId] => 2
+ * [fldTrans] => 1
+ * )
+ * </pre>
+ *
+ * </li><li>
+ *
+ * <kbd>MDB2_TABLEINFO_ORDERTABLE</kbd>
+ *
+ * <p>Similar to <kbd>MDB2_TABLEINFO_ORDER</kbd> but adds more
+ * dimensions to the array in which the table names are keys and
+ * the field names are sub-keys. This is helpful for queries that
+ * join tables which have identical field names.</p>
+ *
+ * <pre>
+ * [num_fields] => 3
+ * [ordertable] => Array (
+ * [tblFoo] => Array (
+ * [fldId] => 0
+ * [fldPhone] => 1
+ * )
+ * [tblBar] => Array (
+ * [fldId] => 2
+ * )
+ * )
+ * </pre>
+ *
+ * </li>
+ * </ul>
+ *
+ * The <samp>flags</samp> element contains a space separated list
+ * of extra information about the field. This data is inconsistent
+ * between DBMS's due to the way each DBMS works.
+ * + <samp>primary_key</samp>
+ * + <samp>unique_key</samp>
+ * + <samp>multiple_key</samp>
+ * + <samp>not_null</samp>
+ *
+ * Most DBMS's only provide the <samp>table</samp> and <samp>flags</samp>
+ * elements if <var>$result</var> is a table name. The following DBMS's
+ * provide full information from queries:
+ * + fbsql
+ * + mysql
+ *
+ * If the 'portability' option has <samp>MDB2_PORTABILITY_FIX_CASE</samp>
+ * turned on, the names of tables and fields will be lower or upper cased.
+ *
+ * @param object|string $result MDB2_result object from a query or a
+ * string containing the name of a table.
+ * While this also accepts a query result
+ * resource identifier, this behavior is
+ * deprecated.
+ * @param int $mode either unused or one of the tableInfo modes:
+ * <kbd>MDB2_TABLEINFO_ORDERTABLE</kbd>,
+ * <kbd>MDB2_TABLEINFO_ORDER</kbd> or
+ * <kbd>MDB2_TABLEINFO_FULL</kbd> (which does both).
+ * These are bitwise, so the first two can be
+ * combined using <kbd>|</kbd>.
+ *
+ * @return array an associative array with the information requested.
+ * A MDB2_Error object on failure.
+ *
+ * @see MDB2_Driver_Common::setOption()
+ */
+ function tableInfo($result, $mode = null)
+ {
+ $db =& $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ if (!is_string($result)) {
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ $db->loadModule('Manager', null, true);
+ $fields = $db->manager->listTableFields($result);
+ if (PEAR::isError($fields)) {
+ return $fields;
+ }
+
+ $flags = array();
+
+ $idxname_format = $db->getOption('idxname_format');
+ $db->setOption('idxname_format', '%s');
+
+ $indexes = $db->manager->listTableIndexes($result);
+ if (PEAR::isError($indexes)) {
+ $db->setOption('idxname_format', $idxname_format);
+ return $indexes;
+ }
+
+ foreach ($indexes as $index) {
+ $definition = $this->getTableIndexDefinition($result, $index);
+ if (PEAR::isError($definition)) {
+ $db->setOption('idxname_format', $idxname_format);
+ return $definition;
+ }
+ if (count($definition['fields']) > 1) {
+ foreach ($definition['fields'] as $field => $sort) {
+ $flags[$field] = 'multiple_key';
+ }
+ }
+ }
+
+ $constraints = $db->manager->listTableConstraints($result);
+ if (PEAR::isError($constraints)) {
+ return $constraints;
+ }
+
+ foreach ($constraints as $constraint) {
+ $definition = $this->getTableConstraintDefinition($result, $constraint);
+ if (PEAR::isError($definition)) {
+ $db->setOption('idxname_format', $idxname_format);
+ return $definition;
+ }
+ $flag = !empty($definition['primary'])
+ ? 'primary_key' : (!empty($definition['unique'])
+ ? 'unique_key' : false);
+ if ($flag) {
+ foreach ($definition['fields'] as $field => $sort) {
+ if (empty($flags[$field]) || $flags[$field] != 'primary_key') {
+ $flags[$field] = $flag;
+ }
+ }
+ }
+ }
+
+ $res = array();
+
+ if ($mode) {
+ $res['num_fields'] = count($fields);
+ }
+
+ foreach ($fields as $i => $field) {
+ $definition = $this->getTableFieldDefinition($result, $field);
+ if (PEAR::isError($definition)) {
+ $db->setOption('idxname_format', $idxname_format);
+ return $definition;
+ }
+ $res[$i] = $definition[0];
+ $res[$i]['name'] = $field;
+ $res[$i]['table'] = $result;
+ $res[$i]['type'] = preg_replace('/^([a-z]+).*$/i', '\\1', trim($definition[0]['nativetype']));
+ // 'primary_key', 'unique_key', 'multiple_key'
+ $res[$i]['flags'] = empty($flags[$field]) ? '' : $flags[$field];
+ // not_null', 'unsigned', 'auto_increment', 'default_[rawencodedvalue]'
+ if (!empty($res[$i]['notnull'])) {
+ $res[$i]['flags'].= ' not_null';
+ }
+ if (!empty($res[$i]['unsigned'])) {
+ $res[$i]['flags'].= ' unsigned';
+ }
+ if (!empty($res[$i]['auto_increment'])) {
+ $res[$i]['flags'].= ' autoincrement';
+ }
+ if (!empty($res[$i]['default'])) {
+ $res[$i]['flags'].= ' default_'.rawurlencode($res[$i]['default']);
+ }
+
+ if ($mode & MDB2_TABLEINFO_ORDER) {
+ $res['order'][$res[$i]['name']] = $i;
+ }
+ if ($mode & MDB2_TABLEINFO_ORDERTABLE) {
+ $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i;
+ }
+ }
+
+ $db->setOption('idxname_format', $idxname_format);
+ return $res;
+ }
+}
?>
\ No newline at end of file
-<?php\r
-// +----------------------------------------------------------------------+\r
-// | PHP versions 4 and 5 |\r
-// +----------------------------------------------------------------------+\r
-// | Copyright (c) 1998-2008 Manuel Lemos, Tomas V.V.Cox, |\r
-// | Stig. S. Bakken, Lukas Smith |\r
-// | All rights reserved. |\r
-// +----------------------------------------------------------------------+\r
-// | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB |\r
-// | API as well as database abstraction for PHP applications. |\r
-// | This LICENSE is in the BSD license style. |\r
-// | |\r
-// | Redistribution and use in source and binary forms, with or without |\r
-// | modification, are permitted provided that the following conditions |\r
-// | are met: |\r
-// | |\r
-// | Redistributions of source code must retain the above copyright |\r
-// | notice, this list of conditions and the following disclaimer. |\r
-// | |\r
-// | Redistributions in binary form must reproduce the above copyright |\r
-// | notice, this list of conditions and the following disclaimer in the |\r
-// | documentation and/or other materials provided with the distribution. |\r
-// | |\r
-// | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken, |\r
-// | Lukas Smith nor the names of his contributors may be used to endorse |\r
-// | or promote products derived from this software without specific prior|\r
-// | written permission. |\r
-// | |\r
-// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |\r
-// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |\r
-// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |\r
-// | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |\r
-// | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |\r
-// | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |\r
-// | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|\r
-// | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |\r
-// | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |\r
-// | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|\r
-// | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |\r
-// | POSSIBILITY OF SUCH DAMAGE. |\r
-// +----------------------------------------------------------------------+\r
-// | Authors: Paul Cooper <pgc@ucecom.com> |\r
-// | Lorenzo Alberton <l.alberton@quipo.it> |\r
-// +----------------------------------------------------------------------+\r
-//\r
-// $Id: pgsql.php,v 1.75 2008/08/22 16:36:20 quipo Exp $\r
-\r
-require_once 'MDB2/Driver/Reverse/Common.php';\r
-\r
-/**\r
- * MDB2 PostGreSQL driver for the schema reverse engineering module\r
- *\r
- * @package MDB2\r
- * @category Database\r
- * @author Paul Cooper <pgc@ucecom.com>\r
- * @author Lorenzo Alberton <l.alberton@quipo.it>\r
- */\r
-class MDB2_Driver_Reverse_pgsql extends MDB2_Driver_Reverse_Common\r
-{\r
- // {{{ getTableFieldDefinition()\r
-\r
- /**\r
- * Get the structure of a field into an array\r
- *\r
- * @param string $table_name name of table that should be used in method\r
- * @param string $field_name name of field that should be used in method\r
- * @return mixed data array on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function getTableFieldDefinition($table_name, $field_name)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- $result = $db->loadModule('Datatype', null, true);\r
- if (PEAR::isError($result)) {\r
- return $result;\r
- }\r
-\r
- list($schema, $table) = $this->splitTableSchema($table_name);\r
-\r
- $query = "SELECT a.attname AS name,\r
- t.typname AS type,\r
- CASE a.attlen\r
- WHEN -1 THEN\r
- CASE t.typname\r
- WHEN 'numeric' THEN (a.atttypmod / 65536)\r
- WHEN 'decimal' THEN (a.atttypmod / 65536)\r
- WHEN 'money' THEN (a.atttypmod / 65536)\r
- ELSE CASE a.atttypmod\r
- WHEN -1 THEN NULL\r
- ELSE a.atttypmod - 4\r
- END\r
- END\r
- ELSE a.attlen\r
- END AS length,\r
- CASE t.typname\r
- WHEN 'numeric' THEN (a.atttypmod % 65536) - 4\r
- WHEN 'decimal' THEN (a.atttypmod % 65536) - 4\r
- WHEN 'money' THEN (a.atttypmod % 65536) - 4\r
- ELSE 0\r
- END AS scale,\r
- a.attnotnull,\r
- a.atttypmod,\r
- a.atthasdef,\r
- (SELECT substring(pg_get_expr(d.adbin, d.adrelid) for 128)\r
- FROM pg_attrdef d\r
- WHERE d.adrelid = a.attrelid\r
- AND d.adnum = a.attnum\r
- AND a.atthasdef\r
- ) as default\r
- FROM pg_attribute a,\r
- pg_class c,\r
- pg_type t\r
- WHERE c.relname = ".$db->quote($table, 'text')."\r
- AND a.atttypid = t.oid\r
- AND c.oid = a.attrelid\r
- AND NOT a.attisdropped\r
- AND a.attnum > 0\r
- AND a.attname = ".$db->quote($field_name, 'text')."\r
- ORDER BY a.attnum";\r
- $column = $db->queryRow($query, null, MDB2_FETCHMODE_ASSOC);\r
- if (PEAR::isError($column)) {\r
- return $column;\r
- }\r
-\r
- if (empty($column)) {\r
- return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,\r
- 'it was not specified an existing table column', __FUNCTION__);\r
- }\r
-\r
- $column = array_change_key_case($column, CASE_LOWER);\r
- $mapped_datatype = $db->datatype->mapNativeDatatype($column);\r
- if (PEAR::isError($mapped_datatype)) {\r
- return $mapped_datatype;\r
- }\r
- list($types, $length, $unsigned, $fixed) = $mapped_datatype;\r
- $notnull = false;\r
- if (!empty($column['attnotnull']) && $column['attnotnull'] == 't') {\r
- $notnull = true;\r
- }\r
- $default = null;\r
- if ($column['atthasdef'] === 't'\r
- && !preg_match("/nextval\('([^']+)'/", $column['default'])\r
- ) {\r
- $pattern = '/^\'(.*)\'::[\w ]+$/i';\r
- $default = $column['default'];#substr($column['adsrc'], 1, -1);\r
- if (is_null($default) && $notnull) {\r
- $default = '';\r
- } elseif (!empty($default) && preg_match($pattern, $default)) {\r
- //remove data type cast\r
- $default = preg_replace ($pattern, '\\1', $default);\r
- }\r
- }\r
- $autoincrement = false;\r
- if (preg_match("/nextval\('([^']+)'/", $column['default'], $nextvals)) {\r
- $autoincrement = true;\r
- }\r
- $definition[0] = array('notnull' => $notnull, 'nativetype' => $column['type']);\r
- if (!is_null($length)) {\r
- $definition[0]['length'] = $length;\r
- }\r
- if (!is_null($unsigned)) {\r
- $definition[0]['unsigned'] = $unsigned;\r
- }\r
- if (!is_null($fixed)) {\r
- $definition[0]['fixed'] = $fixed;\r
- }\r
- if ($default !== false) {\r
- $definition[0]['default'] = $default;\r
- }\r
- if ($autoincrement !== false) {\r
- $definition[0]['autoincrement'] = $autoincrement;\r
- }\r
- foreach ($types as $key => $type) {\r
- $definition[$key] = $definition[0];\r
- if ($type == 'clob' || $type == 'blob') {\r
- unset($definition[$key]['default']);\r
- }\r
- $definition[$key]['type'] = $type;\r
- $definition[$key]['mdb2type'] = $type;\r
- }\r
- return $definition;\r
- }\r
-\r
- // }}}\r
- // {{{ getTableIndexDefinition()\r
-\r
- /**\r
- * Get the structure of an index into an array\r
- *\r
- * @param string $table_name name of table that should be used in method\r
- * @param string $index_name name of index that should be used in method\r
- * @return mixed data array on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function getTableIndexDefinition($table_name, $index_name)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
- \r
- list($schema, $table) = $this->splitTableSchema($table_name);\r
-\r
- $query = 'SELECT relname, indkey FROM pg_index, pg_class';\r
- $query.= ' WHERE pg_class.oid = pg_index.indexrelid';\r
- $query.= " AND indisunique != 't' AND indisprimary != 't'";\r
- $query.= ' AND pg_class.relname = %s';\r
- $index_name_mdb2 = $db->getIndexName($index_name);\r
- $row = $db->queryRow(sprintf($query, $db->quote($index_name_mdb2, 'text')), null, MDB2_FETCHMODE_ASSOC);\r
- if (PEAR::isError($row) || empty($row)) {\r
- // fallback to the given $index_name, without transformation\r
- $row = $db->queryRow(sprintf($query, $db->quote($index_name, 'text')), null, MDB2_FETCHMODE_ASSOC);\r
- }\r
- if (PEAR::isError($row)) {\r
- return $row;\r
- }\r
-\r
- if (empty($row)) {\r
- return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,\r
- 'it was not specified an existing table index', __FUNCTION__);\r
- }\r
-\r
- $row = array_change_key_case($row, CASE_LOWER);\r
-\r
- $db->loadModule('Manager', null, true);\r
- $columns = $db->manager->listTableFields($table_name);\r
-\r
- $definition = array();\r
-\r
- $index_column_numbers = explode(' ', $row['indkey']);\r
-\r
- $colpos = 1;\r
- foreach ($index_column_numbers as $number) {\r
- $definition['fields'][$columns[($number - 1)]] = array(\r
- 'position' => $colpos++,\r
- 'sorting' => 'ascending',\r
- );\r
- }\r
- return $definition;\r
- }\r
-\r
- // }}}\r
- // {{{ getTableConstraintDefinition()\r
-\r
- /**\r
- * Get the structure of a constraint into an array\r
- *\r
- * @param string $table_name name of table that should be used in method\r
- * @param string $constraint_name name of constraint that should be used in method\r
- * @return mixed data array on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function getTableConstraintDefinition($table_name, $constraint_name)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
- \r
- list($schema, $table) = $this->splitTableSchema($table_name);\r
-\r
- $query = "SELECT c.oid,\r
- c.conname AS constraint_name,\r
- CASE WHEN c.contype = 'c' THEN 1 ELSE 0 END AS \"check\",\r
- CASE WHEN c.contype = 'f' THEN 1 ELSE 0 END AS \"foreign\",\r
- CASE WHEN c.contype = 'p' THEN 1 ELSE 0 END AS \"primary\",\r
- CASE WHEN c.contype = 'u' THEN 1 ELSE 0 END AS \"unique\",\r
- CASE WHEN c.condeferrable = 'f' THEN 0 ELSE 1 END AS deferrable,\r
- CASE WHEN c.condeferred = 'f' THEN 0 ELSE 1 END AS initiallydeferred,\r
- --array_to_string(c.conkey, ' ') AS constraint_key,\r
- t.relname AS table_name,\r
- t2.relname AS references_table,\r
- CASE confupdtype\r
- WHEN 'a' THEN 'NO ACTION'\r
- WHEN 'r' THEN 'RESTRICT'\r
- WHEN 'c' THEN 'CASCADE'\r
- WHEN 'n' THEN 'SET NULL'\r
- WHEN 'd' THEN 'SET DEFAULT'\r
- END AS onupdate,\r
- CASE confdeltype\r
- WHEN 'a' THEN 'NO ACTION'\r
- WHEN 'r' THEN 'RESTRICT'\r
- WHEN 'c' THEN 'CASCADE'\r
- WHEN 'n' THEN 'SET NULL'\r
- WHEN 'd' THEN 'SET DEFAULT'\r
- END AS ondelete,\r
- CASE confmatchtype\r
- WHEN 'u' THEN 'UNSPECIFIED'\r
- WHEN 'f' THEN 'FULL'\r
- WHEN 'p' THEN 'PARTIAL'\r
- END AS match,\r
- --array_to_string(c.confkey, ' ') AS fk_constraint_key,\r
- consrc\r
- FROM pg_constraint c\r
- LEFT JOIN pg_class t ON c.conrelid = t.oid\r
- LEFT JOIN pg_class t2 ON c.confrelid = t2.oid\r
- WHERE c.conname = %s\r
- AND t.relname = " . $db->quote($table, 'text');\r
- $constraint_name_mdb2 = $db->getIndexName($constraint_name);\r
- $row = $db->queryRow(sprintf($query, $db->quote($constraint_name_mdb2, 'text')), null, MDB2_FETCHMODE_ASSOC);\r
- if (PEAR::isError($row) || empty($row)) {\r
- // fallback to the given $index_name, without transformation\r
- $constraint_name_mdb2 = $constraint_name;\r
- $row = $db->queryRow(sprintf($query, $db->quote($constraint_name_mdb2, 'text')), null, MDB2_FETCHMODE_ASSOC);\r
- }\r
- if (PEAR::isError($row)) {\r
- return $row;\r
- }\r
- $uniqueIndex = false;\r
- if (empty($row)) {\r
- // We might be looking for a UNIQUE index that was not created\r
- // as a constraint but should be treated as such.\r
- $query = 'SELECT relname AS constraint_name,\r
- indkey,\r
- 0 AS "check",\r
- 0 AS "foreign",\r
- 0 AS "primary",\r
- 1 AS "unique",\r
- 0 AS deferrable,\r
- 0 AS initiallydeferred,\r
- NULL AS references_table,\r
- NULL AS onupdate,\r
- NULL AS ondelete,\r
- NULL AS match\r
- FROM pg_index, pg_class\r
- WHERE pg_class.oid = pg_index.indexrelid\r
- AND indisunique = \'t\'\r
- AND pg_class.relname = %s';\r
- $constraint_name_mdb2 = $db->getIndexName($constraint_name);\r
- $row = $db->queryRow(sprintf($query, $db->quote($constraint_name_mdb2, 'text')), null, MDB2_FETCHMODE_ASSOC);\r
- if (PEAR::isError($row) || empty($row)) {\r
- // fallback to the given $index_name, without transformation\r
- $constraint_name_mdb2 = $constraint_name;\r
- $row = $db->queryRow(sprintf($query, $db->quote($constraint_name_mdb2, 'text')), null, MDB2_FETCHMODE_ASSOC);\r
- }\r
- if (PEAR::isError($row)) {\r
- return $row;\r
- }\r
- if (empty($row)) {\r
- return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,\r
- $constraint_name . ' is not an existing table constraint', __FUNCTION__);\r
- }\r
- $uniqueIndex = true;\r
- }\r
-\r
- $row = array_change_key_case($row, CASE_LOWER);\r
-\r
- $definition = array(\r
- 'primary' => (boolean)$row['primary'],\r
- 'unique' => (boolean)$row['unique'],\r
- 'foreign' => (boolean)$row['foreign'],\r
- 'check' => (boolean)$row['check'],\r
- 'fields' => array(),\r
- 'references' => array(\r
- 'table' => $row['references_table'],\r
- 'fields' => array(),\r
- ),\r
- 'deferrable' => (boolean)$row['deferrable'],\r
- 'initiallydeferred' => (boolean)$row['initiallydeferred'],\r
- 'onupdate' => $row['onupdate'],\r
- 'ondelete' => $row['ondelete'],\r
- 'match' => $row['match'],\r
- );\r
-\r
- if ($uniqueIndex) {\r
- $db->loadModule('Manager', null, true);\r
- $columns = $db->manager->listTableFields($table_name);\r
- $index_column_numbers = explode(' ', $row['indkey']);\r
- $colpos = 1;\r
- foreach ($index_column_numbers as $number) {\r
- $definition['fields'][$columns[($number - 1)]] = array(\r
- 'position' => $colpos++,\r
- 'sorting' => 'ascending',\r
- );\r
- }\r
- return $definition;\r
- }\r
-\r
- $query = 'SELECT a.attname\r
- FROM pg_constraint c\r
- LEFT JOIN pg_class t ON c.conrelid = t.oid\r
- LEFT JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = ANY(c.conkey)\r
- WHERE c.conname = %s\r
- AND t.relname = ' . $db->quote($table, 'text');\r
- $fields = $db->queryCol(sprintf($query, $db->quote($constraint_name_mdb2, 'text')), null);\r
- if (PEAR::isError($fields)) {\r
- return $fields;\r
- }\r
- $colpos = 1;\r
- foreach ($fields as $field) {\r
- $definition['fields'][$field] = array(\r
- 'position' => $colpos++,\r
- 'sorting' => 'ascending',\r
- );\r
- }\r
- \r
- if ($definition['foreign']) {\r
- $query = 'SELECT a.attname\r
- FROM pg_constraint c\r
- LEFT JOIN pg_class t ON c.confrelid = t.oid\r
- LEFT JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = ANY(c.confkey)\r
- WHERE c.conname = %s\r
- AND t.relname = ' . $db->quote($definition['references']['table'], 'text');\r
- $foreign_fields = $db->queryCol(sprintf($query, $db->quote($constraint_name_mdb2, 'text')), null);\r
- if (PEAR::isError($foreign_fields)) {\r
- return $foreign_fields;\r
- }\r
- $colpos = 1;\r
- foreach ($foreign_fields as $foreign_field) {\r
- $definition['references']['fields'][$foreign_field] = array(\r
- 'position' => $colpos++,\r
- );\r
- }\r
- }\r
- \r
- if ($definition['check']) {\r
- $check_def = $db->queryOne("SELECT pg_get_constraintdef(" . $row['oid'] . ", 't')");\r
- // ...\r
- }\r
- return $definition;\r
- }\r
-\r
- // }}}\r
- // {{{ getTriggerDefinition()\r
-\r
- /**\r
- * Get the structure of a trigger into an array\r
- *\r
- * EXPERIMENTAL\r
- *\r
- * WARNING: this function is experimental and may change the returned value\r
- * at any time until labelled as non-experimental\r
- *\r
- * @param string $trigger name of trigger that should be used in method\r
- * @return mixed data array on success, a MDB2 error on failure\r
- * @access public\r
- *\r
- * @TODO: add support for plsql functions and functions with args\r
- */\r
- function getTriggerDefinition($trigger)\r
- {\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- $query = "SELECT trg.tgname AS trigger_name,\r
- tbl.relname AS table_name,\r
- CASE\r
- WHEN p.proname IS NOT NULL THEN 'EXECUTE PROCEDURE ' || p.proname || '();'\r
- ELSE ''\r
- END AS trigger_body,\r
- CASE trg.tgtype & cast(2 as int2)\r
- WHEN 0 THEN 'AFTER'\r
- ELSE 'BEFORE'\r
- END AS trigger_type,\r
- CASE trg.tgtype & cast(28 as int2)\r
- WHEN 16 THEN 'UPDATE'\r
- WHEN 8 THEN 'DELETE'\r
- WHEN 4 THEN 'INSERT'\r
- WHEN 20 THEN 'INSERT, UPDATE'\r
- WHEN 28 THEN 'INSERT, UPDATE, DELETE'\r
- WHEN 24 THEN 'UPDATE, DELETE'\r
- WHEN 12 THEN 'INSERT, DELETE'\r
- END AS trigger_event,\r
- CASE trg.tgenabled\r
- WHEN 'O' THEN 't'\r
- ELSE trg.tgenabled\r
- END AS trigger_enabled,\r
- obj_description(trg.oid, 'pg_trigger') AS trigger_comment\r
- FROM pg_trigger trg,\r
- pg_class tbl,\r
- pg_proc p\r
- WHERE trg.tgrelid = tbl.oid\r
- AND trg.tgfoid = p.oid\r
- AND trg.tgname = ". $db->quote($trigger, 'text');\r
- $types = array(\r
- 'trigger_name' => 'text',\r
- 'table_name' => 'text',\r
- 'trigger_body' => 'text',\r
- 'trigger_type' => 'text',\r
- 'trigger_event' => 'text',\r
- 'trigger_comment' => 'text',\r
- 'trigger_enabled' => 'boolean',\r
- );\r
- return $db->queryRow($query, $types, MDB2_FETCHMODE_ASSOC);\r
- }\r
- \r
- // }}}\r
- // {{{ tableInfo()\r
-\r
- /**\r
- * Returns information about a table or a result set\r
- *\r
- * NOTE: only supports 'table' and 'flags' if <var>$result</var>\r
- * is a table name.\r
- *\r
- * @param object|string $result MDB2_result object from a query or a\r
- * string containing the name of a table.\r
- * While this also accepts a query result\r
- * resource identifier, this behavior is\r
- * deprecated.\r
- * @param int $mode a valid tableInfo mode\r
- *\r
- * @return array an associative array with the information requested.\r
- * A MDB2_Error object on failure.\r
- *\r
- * @see MDB2_Driver_Common::tableInfo()\r
- */\r
- function tableInfo($result, $mode = null)\r
- {\r
- if (is_string($result)) {\r
- return parent::tableInfo($result, $mode);\r
- }\r
-\r
- $db =& $this->getDBInstance();\r
- if (PEAR::isError($db)) {\r
- return $db;\r
- }\r
-\r
- $resource = MDB2::isResultCommon($result) ? $result->getResource() : $result;\r
- if (!is_resource($resource)) {\r
- return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,\r
- 'Could not generate result resource', __FUNCTION__);\r
- }\r
-\r
- if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {\r
- if ($db->options['field_case'] == CASE_LOWER) {\r
- $case_func = 'strtolower';\r
- } else {\r
- $case_func = 'strtoupper';\r
- }\r
- } else {\r
- $case_func = 'strval';\r
- }\r
-\r
- $count = @pg_num_fields($resource);\r
- $res = array();\r
-\r
- if ($mode) {\r
- $res['num_fields'] = $count;\r
- }\r
-\r
- $db->loadModule('Datatype', null, true);\r
- for ($i = 0; $i < $count; $i++) {\r
- $res[$i] = array(\r
- 'table' => function_exists('pg_field_table') ? @pg_field_table($resource, $i) : '',\r
- 'name' => $case_func(@pg_field_name($resource, $i)),\r
- 'type' => @pg_field_type($resource, $i),\r
- 'length' => @pg_field_size($resource, $i),\r
- 'flags' => '',\r
- );\r
- $mdb2type_info = $db->datatype->mapNativeDatatype($res[$i]);\r
- if (PEAR::isError($mdb2type_info)) {\r
- return $mdb2type_info;\r
- }\r
- $res[$i]['mdb2type'] = $mdb2type_info[0][0];\r
- if ($mode & MDB2_TABLEINFO_ORDER) {\r
- $res['order'][$res[$i]['name']] = $i;\r
- }\r
- if ($mode & MDB2_TABLEINFO_ORDERTABLE) {\r
- $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i;\r
- }\r
- }\r
-\r
- return $res;\r
- }\r
-}\r
+<?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. |
+// +----------------------------------------------------------------------+
+// | Authors: Paul Cooper <pgc@ucecom.com> |
+// | Lorenzo Alberton <l.alberton@quipo.it> |
+// +----------------------------------------------------------------------+
+//
+// $Id: pgsql.php,v 1.75 2008/08/22 16:36:20 quipo Exp $
+
+require_once 'MDB2/Driver/Reverse/Common.php';
+
+/**
+ * MDB2 PostGreSQL driver for the schema reverse engineering module
+ *
+ * @package MDB2
+ * @category Database
+ * @author Paul Cooper <pgc@ucecom.com>
+ * @author Lorenzo Alberton <l.alberton@quipo.it>
+ */
+class MDB2_Driver_Reverse_pgsql extends MDB2_Driver_Reverse_Common
+{
+ // {{{ getTableFieldDefinition()
+
+ /**
+ * Get the structure of a field into an array
+ *
+ * @param string $table_name name of table that should be used in method
+ * @param string $field_name name of field that should be used in method
+ * @return mixed data array on success, a MDB2 error on failure
+ * @access public
+ */
+ function getTableFieldDefinition($table_name, $field_name)
+ {
+ $db =& $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ $result = $db->loadModule('Datatype', null, true);
+ if (PEAR::isError($result)) {
+ return $result;
+ }
+
+ list($schema, $table) = $this->splitTableSchema($table_name);
+
+ $query = "SELECT a.attname AS name,
+ t.typname AS type,
+ CASE a.attlen
+ WHEN -1 THEN
+ CASE t.typname
+ WHEN 'numeric' THEN (a.atttypmod / 65536)
+ WHEN 'decimal' THEN (a.atttypmod / 65536)
+ WHEN 'money' THEN (a.atttypmod / 65536)
+ ELSE CASE a.atttypmod
+ WHEN -1 THEN NULL
+ ELSE a.atttypmod - 4
+ END
+ END
+ ELSE a.attlen
+ END AS length,
+ CASE t.typname
+ WHEN 'numeric' THEN (a.atttypmod % 65536) - 4
+ WHEN 'decimal' THEN (a.atttypmod % 65536) - 4
+ WHEN 'money' THEN (a.atttypmod % 65536) - 4
+ ELSE 0
+ END AS scale,
+ a.attnotnull,
+ a.atttypmod,
+ a.atthasdef,
+ (SELECT substring(pg_get_expr(d.adbin, d.adrelid) for 128)
+ FROM pg_attrdef d
+ WHERE d.adrelid = a.attrelid
+ AND d.adnum = a.attnum
+ AND a.atthasdef
+ ) as default
+ FROM pg_attribute a,
+ pg_class c,
+ pg_type t
+ WHERE c.relname = ".$db->quote($table, 'text')."
+ AND a.atttypid = t.oid
+ AND c.oid = a.attrelid
+ AND NOT a.attisdropped
+ AND a.attnum > 0
+ AND a.attname = ".$db->quote($field_name, 'text')."
+ ORDER BY a.attnum";
+ $column = $db->queryRow($query, null, MDB2_FETCHMODE_ASSOC);
+ if (PEAR::isError($column)) {
+ return $column;
+ }
+
+ if (empty($column)) {
+ return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
+ 'it was not specified an existing table column', __FUNCTION__);
+ }
+
+ $column = array_change_key_case($column, CASE_LOWER);
+ $mapped_datatype = $db->datatype->mapNativeDatatype($column);
+ if (PEAR::isError($mapped_datatype)) {
+ return $mapped_datatype;
+ }
+ list($types, $length, $unsigned, $fixed) = $mapped_datatype;
+ $notnull = false;
+ if (!empty($column['attnotnull']) && $column['attnotnull'] == 't') {
+ $notnull = true;
+ }
+ $default = null;
+ if ($column['atthasdef'] === 't'
+ && !preg_match("/nextval\('([^']+)'/", $column['default'])
+ ) {
+ $pattern = '/^\'(.*)\'::[\w ]+$/i';
+ $default = $column['default'];#substr($column['adsrc'], 1, -1);
+ if (is_null($default) && $notnull) {
+ $default = '';
+ } elseif (!empty($default) && preg_match($pattern, $default)) {
+ //remove data type cast
+ $default = preg_replace ($pattern, '\\1', $default);
+ }
+ }
+ $autoincrement = false;
+ if (preg_match("/nextval\('([^']+)'/", $column['default'], $nextvals)) {
+ $autoincrement = true;
+ }
+ $definition[0] = array('notnull' => $notnull, 'nativetype' => $column['type']);
+ if (!is_null($length)) {
+ $definition[0]['length'] = $length;
+ }
+ if (!is_null($unsigned)) {
+ $definition[0]['unsigned'] = $unsigned;
+ }
+ if (!is_null($fixed)) {
+ $definition[0]['fixed'] = $fixed;
+ }
+ if ($default !== false) {
+ $definition[0]['default'] = $default;
+ }
+ if ($autoincrement !== false) {
+ $definition[0]['autoincrement'] = $autoincrement;
+ }
+ foreach ($types as $key => $type) {
+ $definition[$key] = $definition[0];
+ if ($type == 'clob' || $type == 'blob') {
+ unset($definition[$key]['default']);
+ }
+ $definition[$key]['type'] = $type;
+ $definition[$key]['mdb2type'] = $type;
+ }
+ return $definition;
+ }
+
+ // }}}
+ // {{{ getTableIndexDefinition()
+
+ /**
+ * Get the structure of an index into an array
+ *
+ * @param string $table_name name of table that should be used in method
+ * @param string $index_name name of index that should be used in method
+ * @return mixed data array on success, a MDB2 error on failure
+ * @access public
+ */
+ function getTableIndexDefinition($table_name, $index_name)
+ {
+ $db =& $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ list($schema, $table) = $this->splitTableSchema($table_name);
+
+ $query = 'SELECT relname, indkey FROM pg_index, pg_class';
+ $query.= ' WHERE pg_class.oid = pg_index.indexrelid';
+ $query.= " AND indisunique != 't' AND indisprimary != 't'";
+ $query.= ' AND pg_class.relname = %s';
+ $index_name_mdb2 = $db->getIndexName($index_name);
+ $row = $db->queryRow(sprintf($query, $db->quote($index_name_mdb2, 'text')), null, MDB2_FETCHMODE_ASSOC);
+ if (PEAR::isError($row) || empty($row)) {
+ // fallback to the given $index_name, without transformation
+ $row = $db->queryRow(sprintf($query, $db->quote($index_name, 'text')), null, MDB2_FETCHMODE_ASSOC);
+ }
+ if (PEAR::isError($row)) {
+ return $row;
+ }
+
+ if (empty($row)) {
+ return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
+ 'it was not specified an existing table index', __FUNCTION__);
+ }
+
+ $row = array_change_key_case($row, CASE_LOWER);
+
+ $db->loadModule('Manager', null, true);
+ $columns = $db->manager->listTableFields($table_name);
+
+ $definition = array();
+
+ $index_column_numbers = explode(' ', $row['indkey']);
+
+ $colpos = 1;
+ foreach ($index_column_numbers as $number) {
+ $definition['fields'][$columns[($number - 1)]] = array(
+ 'position' => $colpos++,
+ 'sorting' => 'ascending',
+ );
+ }
+ return $definition;
+ }
+
+ // }}}
+ // {{{ getTableConstraintDefinition()
+
+ /**
+ * Get the structure of a constraint into an array
+ *
+ * @param string $table_name name of table that should be used in method
+ * @param string $constraint_name name of constraint that should be used in method
+ * @return mixed data array on success, a MDB2 error on failure
+ * @access public
+ */
+ function getTableConstraintDefinition($table_name, $constraint_name)
+ {
+ $db =& $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ list($schema, $table) = $this->splitTableSchema($table_name);
+
+ $query = "SELECT c.oid,
+ c.conname AS constraint_name,
+ CASE WHEN c.contype = 'c' THEN 1 ELSE 0 END AS \"check\",
+ CASE WHEN c.contype = 'f' THEN 1 ELSE 0 END AS \"foreign\",
+ CASE WHEN c.contype = 'p' THEN 1 ELSE 0 END AS \"primary\",
+ CASE WHEN c.contype = 'u' THEN 1 ELSE 0 END AS \"unique\",
+ CASE WHEN c.condeferrable = 'f' THEN 0 ELSE 1 END AS deferrable,
+ CASE WHEN c.condeferred = 'f' THEN 0 ELSE 1 END AS initiallydeferred,
+ --array_to_string(c.conkey, ' ') AS constraint_key,
+ t.relname AS table_name,
+ t2.relname AS references_table,
+ CASE confupdtype
+ WHEN 'a' THEN 'NO ACTION'
+ WHEN 'r' THEN 'RESTRICT'
+ WHEN 'c' THEN 'CASCADE'
+ WHEN 'n' THEN 'SET NULL'
+ WHEN 'd' THEN 'SET DEFAULT'
+ END AS onupdate,
+ CASE confdeltype
+ WHEN 'a' THEN 'NO ACTION'
+ WHEN 'r' THEN 'RESTRICT'
+ WHEN 'c' THEN 'CASCADE'
+ WHEN 'n' THEN 'SET NULL'
+ WHEN 'd' THEN 'SET DEFAULT'
+ END AS ondelete,
+ CASE confmatchtype
+ WHEN 'u' THEN 'UNSPECIFIED'
+ WHEN 'f' THEN 'FULL'
+ WHEN 'p' THEN 'PARTIAL'
+ END AS match,
+ --array_to_string(c.confkey, ' ') AS fk_constraint_key,
+ consrc
+ FROM pg_constraint c
+ LEFT JOIN pg_class t ON c.conrelid = t.oid
+ LEFT JOIN pg_class t2 ON c.confrelid = t2.oid
+ WHERE c.conname = %s
+ AND t.relname = " . $db->quote($table, 'text');
+ $constraint_name_mdb2 = $db->getIndexName($constraint_name);
+ $row = $db->queryRow(sprintf($query, $db->quote($constraint_name_mdb2, 'text')), null, MDB2_FETCHMODE_ASSOC);
+ if (PEAR::isError($row) || empty($row)) {
+ // fallback to the given $index_name, without transformation
+ $constraint_name_mdb2 = $constraint_name;
+ $row = $db->queryRow(sprintf($query, $db->quote($constraint_name_mdb2, 'text')), null, MDB2_FETCHMODE_ASSOC);
+ }
+ if (PEAR::isError($row)) {
+ return $row;
+ }
+ $uniqueIndex = false;
+ if (empty($row)) {
+ // We might be looking for a UNIQUE index that was not created
+ // as a constraint but should be treated as such.
+ $query = 'SELECT relname AS constraint_name,
+ indkey,
+ 0 AS "check",
+ 0 AS "foreign",
+ 0 AS "primary",
+ 1 AS "unique",
+ 0 AS deferrable,
+ 0 AS initiallydeferred,
+ NULL AS references_table,
+ NULL AS onupdate,
+ NULL AS ondelete,
+ NULL AS match
+ FROM pg_index, pg_class
+ WHERE pg_class.oid = pg_index.indexrelid
+ AND indisunique = \'t\'
+ AND pg_class.relname = %s';
+ $constraint_name_mdb2 = $db->getIndexName($constraint_name);
+ $row = $db->queryRow(sprintf($query, $db->quote($constraint_name_mdb2, 'text')), null, MDB2_FETCHMODE_ASSOC);
+ if (PEAR::isError($row) || empty($row)) {
+ // fallback to the given $index_name, without transformation
+ $constraint_name_mdb2 = $constraint_name;
+ $row = $db->queryRow(sprintf($query, $db->quote($constraint_name_mdb2, 'text')), null, MDB2_FETCHMODE_ASSOC);
+ }
+ if (PEAR::isError($row)) {
+ return $row;
+ }
+ if (empty($row)) {
+ return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
+ $constraint_name . ' is not an existing table constraint', __FUNCTION__);
+ }
+ $uniqueIndex = true;
+ }
+
+ $row = array_change_key_case($row, CASE_LOWER);
+
+ $definition = array(
+ 'primary' => (boolean)$row['primary'],
+ 'unique' => (boolean)$row['unique'],
+ 'foreign' => (boolean)$row['foreign'],
+ 'check' => (boolean)$row['check'],
+ 'fields' => array(),
+ 'references' => array(
+ 'table' => $row['references_table'],
+ 'fields' => array(),
+ ),
+ 'deferrable' => (boolean)$row['deferrable'],
+ 'initiallydeferred' => (boolean)$row['initiallydeferred'],
+ 'onupdate' => $row['onupdate'],
+ 'ondelete' => $row['ondelete'],
+ 'match' => $row['match'],
+ );
+
+ if ($uniqueIndex) {
+ $db->loadModule('Manager', null, true);
+ $columns = $db->manager->listTableFields($table_name);
+ $index_column_numbers = explode(' ', $row['indkey']);
+ $colpos = 1;
+ foreach ($index_column_numbers as $number) {
+ $definition['fields'][$columns[($number - 1)]] = array(
+ 'position' => $colpos++,
+ 'sorting' => 'ascending',
+ );
+ }
+ return $definition;
+ }
+
+ $query = 'SELECT a.attname
+ FROM pg_constraint c
+ LEFT JOIN pg_class t ON c.conrelid = t.oid
+ LEFT JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = ANY(c.conkey)
+ WHERE c.conname = %s
+ AND t.relname = ' . $db->quote($table, 'text');
+ $fields = $db->queryCol(sprintf($query, $db->quote($constraint_name_mdb2, 'text')), null);
+ if (PEAR::isError($fields)) {
+ return $fields;
+ }
+ $colpos = 1;
+ foreach ($fields as $field) {
+ $definition['fields'][$field] = array(
+ 'position' => $colpos++,
+ 'sorting' => 'ascending',
+ );
+ }
+
+ if ($definition['foreign']) {
+ $query = 'SELECT a.attname
+ FROM pg_constraint c
+ LEFT JOIN pg_class t ON c.confrelid = t.oid
+ LEFT JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = ANY(c.confkey)
+ WHERE c.conname = %s
+ AND t.relname = ' . $db->quote($definition['references']['table'], 'text');
+ $foreign_fields = $db->queryCol(sprintf($query, $db->quote($constraint_name_mdb2, 'text')), null);
+ if (PEAR::isError($foreign_fields)) {
+ return $foreign_fields;
+ }
+ $colpos = 1;
+ foreach ($foreign_fields as $foreign_field) {
+ $definition['references']['fields'][$foreign_field] = array(
+ 'position' => $colpos++,
+ );
+ }
+ }
+
+ if ($definition['check']) {
+ $check_def = $db->queryOne("SELECT pg_get_constraintdef(" . $row['oid'] . ", 't')");
+ // ...
+ }
+ return $definition;
+ }
+
+ // }}}
+ // {{{ getTriggerDefinition()
+
+ /**
+ * Get the structure of a trigger into an array
+ *
+ * EXPERIMENTAL
+ *
+ * WARNING: this function is experimental and may change the returned value
+ * at any time until labelled as non-experimental
+ *
+ * @param string $trigger name of trigger that should be used in method
+ * @return mixed data array on success, a MDB2 error on failure
+ * @access public
+ *
+ * @TODO: add support for plsql functions and functions with args
+ */
+ function getTriggerDefinition($trigger)
+ {
+ $db =& $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ $query = "SELECT trg.tgname AS trigger_name,
+ tbl.relname AS table_name,
+ CASE
+ WHEN p.proname IS NOT NULL THEN 'EXECUTE PROCEDURE ' || p.proname || '();'
+ ELSE ''
+ END AS trigger_body,
+ CASE trg.tgtype & cast(2 as int2)
+ WHEN 0 THEN 'AFTER'
+ ELSE 'BEFORE'
+ END AS trigger_type,
+ CASE trg.tgtype & cast(28 as int2)
+ WHEN 16 THEN 'UPDATE'
+ WHEN 8 THEN 'DELETE'
+ WHEN 4 THEN 'INSERT'
+ WHEN 20 THEN 'INSERT, UPDATE'
+ WHEN 28 THEN 'INSERT, UPDATE, DELETE'
+ WHEN 24 THEN 'UPDATE, DELETE'
+ WHEN 12 THEN 'INSERT, DELETE'
+ END AS trigger_event,
+ CASE trg.tgenabled
+ WHEN 'O' THEN 't'
+ ELSE trg.tgenabled
+ END AS trigger_enabled,
+ obj_description(trg.oid, 'pg_trigger') AS trigger_comment
+ FROM pg_trigger trg,
+ pg_class tbl,
+ pg_proc p
+ WHERE trg.tgrelid = tbl.oid
+ AND trg.tgfoid = p.oid
+ AND trg.tgname = ". $db->quote($trigger, 'text');
+ $types = array(
+ 'trigger_name' => 'text',
+ 'table_name' => 'text',
+ 'trigger_body' => 'text',
+ 'trigger_type' => 'text',
+ 'trigger_event' => 'text',
+ 'trigger_comment' => 'text',
+ 'trigger_enabled' => 'boolean',
+ );
+ return $db->queryRow($query, $types, MDB2_FETCHMODE_ASSOC);
+ }
+
+ // }}}
+ // {{{ tableInfo()
+
+ /**
+ * Returns information about a table or a result set
+ *
+ * NOTE: only supports 'table' and 'flags' if <var>$result</var>
+ * is a table name.
+ *
+ * @param object|string $result MDB2_result object from a query or a
+ * string containing the name of a table.
+ * While this also accepts a query result
+ * resource identifier, this behavior is
+ * deprecated.
+ * @param int $mode a valid tableInfo mode
+ *
+ * @return array an associative array with the information requested.
+ * A MDB2_Error object on failure.
+ *
+ * @see MDB2_Driver_Common::tableInfo()
+ */
+ function tableInfo($result, $mode = null)
+ {
+ if (is_string($result)) {
+ return parent::tableInfo($result, $mode);
+ }
+
+ $db =& $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ $resource = MDB2::isResultCommon($result) ? $result->getResource() : $result;
+ if (!is_resource($resource)) {
+ return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
+ 'Could not generate result resource', __FUNCTION__);
+ }
+
+ if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
+ if ($db->options['field_case'] == CASE_LOWER) {
+ $case_func = 'strtolower';
+ } else {
+ $case_func = 'strtoupper';
+ }
+ } else {
+ $case_func = 'strval';
+ }
+
+ $count = @pg_num_fields($resource);
+ $res = array();
+
+ if ($mode) {
+ $res['num_fields'] = $count;
+ }
+
+ $db->loadModule('Datatype', null, true);
+ for ($i = 0; $i < $count; $i++) {
+ $res[$i] = array(
+ 'table' => function_exists('pg_field_table') ? @pg_field_table($resource, $i) : '',
+ 'name' => $case_func(@pg_field_name($resource, $i)),
+ 'type' => @pg_field_type($resource, $i),
+ 'length' => @pg_field_size($resource, $i),
+ 'flags' => '',
+ );
+ $mdb2type_info = $db->datatype->mapNativeDatatype($res[$i]);
+ if (PEAR::isError($mdb2type_info)) {
+ return $mdb2type_info;
+ }
+ $res[$i]['mdb2type'] = $mdb2type_info[0][0];
+ if ($mode & MDB2_TABLEINFO_ORDER) {
+ $res['order'][$res[$i]['name']] = $i;
+ }
+ if ($mode & MDB2_TABLEINFO_ORDERTABLE) {
+ $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i;
+ }
+ }
+
+ return $res;
+ }
+}
?>
\ No newline at end of file
-<?php\r
-// vim: set et ts=4 sw=4 fdm=marker:\r
-// +----------------------------------------------------------------------+\r
-// | PHP versions 4 and 5 |\r
-// +----------------------------------------------------------------------+\r
-// | Copyright (c) 1998-2008 Manuel Lemos, Tomas V.V.Cox, |\r
-// | Stig. S. Bakken, Lukas Smith |\r
-// | All rights reserved. |\r
-// +----------------------------------------------------------------------+\r
-// | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB |\r
-// | API as well as database abstraction for PHP applications. |\r
-// | This LICENSE is in the BSD license style. |\r
-// | |\r
-// | Redistribution and use in source and binary forms, with or without |\r
-// | modification, are permitted provided that the following conditions |\r
-// | are met: |\r
-// | |\r
-// | Redistributions of source code must retain the above copyright |\r
-// | notice, this list of conditions and the following disclaimer. |\r
-// | |\r
-// | Redistributions in binary form must reproduce the above copyright |\r
-// | notice, this list of conditions and the following disclaimer in the |\r
-// | documentation and/or other materials provided with the distribution. |\r
-// | |\r
-// | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken, |\r
-// | Lukas Smith nor the names of his contributors may be used to endorse |\r
-// | or promote products derived from this software without specific prior|\r
-// | written permission. |\r
-// | |\r
-// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |\r
-// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |\r
-// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |\r
-// | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |\r
-// | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |\r
-// | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |\r
-// | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|\r
-// | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |\r
-// | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |\r
-// | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|\r
-// | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |\r
-// | POSSIBILITY OF SUCH DAMAGE. |\r
-// +----------------------------------------------------------------------+\r
-// | Author: Lukas Smith <smith@pooteeweet.org> |\r
-// +----------------------------------------------------------------------+\r
-//\r
-// $Id: mysql.php,v 1.214 2008/11/16 21:45:08 quipo Exp $\r
-//\r
-\r
-/**\r
- * MDB2 MySQL driver\r
- *\r
- * @package MDB2\r
- * @category Database\r
- * @author Lukas Smith <smith@pooteeweet.org>\r
- */\r
-class MDB2_Driver_mysql extends MDB2_Driver_Common\r
-{\r
- // {{{ properties\r
-\r
- var $string_quoting = array('start' => "'", 'end' => "'", 'escape' => '\\', 'escape_pattern' => '\\');\r
-\r
- var $identifier_quoting = array('start' => '`', 'end' => '`', 'escape' => '`');\r
-\r
- var $sql_comments = array(\r
- array('start' => '-- ', 'end' => "\n", 'escape' => false),\r
- array('start' => '#', 'end' => "\n", 'escape' => false),\r
- array('start' => '/*', 'end' => '*/', 'escape' => false),\r
- );\r
-\r
- var $server_capabilities_checked = false;\r
-\r
- var $start_transaction = false;\r
-\r
- var $varchar_max_length = 255;\r
-\r
- // }}}\r
- // {{{ constructor\r
-\r
- /**\r
- * Constructor\r
- */\r
- function __construct()\r
- {\r
- parent::__construct();\r
-\r
- $this->phptype = 'mysql';\r
- $this->dbsyntax = 'mysql';\r
-\r
- $this->supported['sequences'] = 'emulated';\r
- $this->supported['indexes'] = true;\r
- $this->supported['affected_rows'] = true;\r
- $this->supported['transactions'] = false;\r
- $this->supported['savepoints'] = false;\r
- $this->supported['summary_functions'] = true;\r
- $this->supported['order_by_text'] = true;\r
- $this->supported['current_id'] = 'emulated';\r
- $this->supported['limit_queries'] = true;\r
- $this->supported['LOBs'] = true;\r
- $this->supported['replace'] = true;\r
- $this->supported['sub_selects'] = 'emulated';\r
- $this->supported['triggers'] = false;\r
- $this->supported['auto_increment'] = true;\r
- $this->supported['primary_key'] = true;\r
- $this->supported['result_introspection'] = true;\r
- $this->supported['prepared_statements'] = 'emulated';\r
- $this->supported['identifier_quoting'] = true;\r
- $this->supported['pattern_escaping'] = true;\r
- $this->supported['new_link'] = true;\r
-\r
- $this->options['DBA_username'] = false;\r
- $this->options['DBA_password'] = false;\r
- $this->options['default_table_type'] = '';\r
- $this->options['max_identifiers_length'] = 64;\r
-\r
- $this->_reCheckSupportedOptions();\r
- }\r
-\r
- // }}}\r
- // {{{ _reCheckSupportedOptions()\r
- \r
- /**\r
- * If the user changes certain options, other capabilities may depend\r
- * on the new settings, so we need to check them (again).\r
- *\r
- * @access private\r
- */\r
- function _reCheckSupportedOptions()\r
- {\r
- $this->supported['transactions'] = $this->options['use_transactions'];\r
- $this->supported['savepoints'] = $this->options['use_transactions'];\r
- if ($this->options['default_table_type']) {\r
- switch (strtoupper($this->options['default_table_type'])) {\r
- case 'BLACKHOLE':\r
- case 'MEMORY':\r
- case 'ARCHIVE':\r
- case 'CSV':\r
- case 'HEAP':\r
- case 'ISAM':\r
- case 'MERGE':\r
- case 'MRG_ISAM':\r
- case 'ISAM':\r
- case 'MRG_MYISAM':\r
- case 'MYISAM':\r
- $this->supported['savepoints'] = false;\r
- $this->supported['transactions'] = false;\r
- $this->warnings[] = $this->options['default_table_type'] .\r
- ' is not a supported default table type';\r
- break;\r
- }\r
- }\r
- }\r
-\r
- // }}}\r
- // {{{ function setOption($option, $value)\r
-\r
- /**\r
- * set the option for the db class\r
- *\r
- * @param string option name\r
- * @param mixed value for the option\r
- *\r
- * @return mixed MDB2_OK or MDB2 Error Object\r
- *\r
- * @access public\r
- */\r
- function setOption($option, $value)\r
- {\r
- $res = parent::setOption($option, $value);\r
- $this->_reCheckSupportedOptions();\r
- }\r
-\r
- // }}}\r
- // {{{ errorInfo()\r
-\r
- /**\r
- * This method is used to collect information about an error\r
- *\r
- * @param integer $error\r
- * @return array\r
- * @access public\r
- */\r
- function errorInfo($error = null)\r
- {\r
- if ($this->connection) {\r
- $native_code = @mysql_errno($this->connection);\r
- $native_msg = @mysql_error($this->connection);\r
- } else {\r
- $native_code = @mysql_errno();\r
- $native_msg = @mysql_error();\r
- }\r
- if (is_null($error)) {\r
- static $ecode_map;\r
- if (empty($ecode_map)) {\r
- $ecode_map = array(\r
- 1000 => MDB2_ERROR_INVALID, //hashchk\r
- 1001 => MDB2_ERROR_INVALID, //isamchk\r
- 1004 => MDB2_ERROR_CANNOT_CREATE,\r
- 1005 => MDB2_ERROR_CANNOT_CREATE,\r
- 1006 => MDB2_ERROR_CANNOT_CREATE,\r
- 1007 => MDB2_ERROR_ALREADY_EXISTS,\r
- 1008 => MDB2_ERROR_CANNOT_DROP,\r
- 1009 => MDB2_ERROR_CANNOT_DROP,\r
- 1010 => MDB2_ERROR_CANNOT_DROP,\r
- 1011 => MDB2_ERROR_CANNOT_DELETE,\r
- 1022 => MDB2_ERROR_ALREADY_EXISTS,\r
- 1029 => MDB2_ERROR_NOT_FOUND,\r
- 1032 => MDB2_ERROR_NOT_FOUND,\r
- 1044 => MDB2_ERROR_ACCESS_VIOLATION,\r
- 1045 => MDB2_ERROR_ACCESS_VIOLATION,\r
- 1046 => MDB2_ERROR_NODBSELECTED,\r
- 1048 => MDB2_ERROR_CONSTRAINT,\r
- 1049 => MDB2_ERROR_NOSUCHDB,\r
- 1050 => MDB2_ERROR_ALREADY_EXISTS,\r
- 1051 => MDB2_ERROR_NOSUCHTABLE,\r
- 1054 => MDB2_ERROR_NOSUCHFIELD,\r
- 1060 => MDB2_ERROR_ALREADY_EXISTS,\r
- 1061 => MDB2_ERROR_ALREADY_EXISTS,\r
- 1062 => MDB2_ERROR_ALREADY_EXISTS,\r
- 1064 => MDB2_ERROR_SYNTAX,\r
- 1067 => MDB2_ERROR_INVALID,\r
- 1072 => MDB2_ERROR_NOT_FOUND,\r
- 1086 => MDB2_ERROR_ALREADY_EXISTS,\r
- 1091 => MDB2_ERROR_NOT_FOUND,\r
- 1100 => MDB2_ERROR_NOT_LOCKED,\r
- 1109 => MDB2_ERROR_NOT_FOUND,\r
- 1125 => MDB2_ERROR_ALREADY_EXISTS,\r
- 1136 => MDB2_ERROR_VALUE_COUNT_ON_ROW,\r
- 1138 => MDB2_ERROR_INVALID,\r
- 1142 => MDB2_ERROR_ACCESS_VIOLATION,\r
- 1143 => MDB2_ERROR_ACCESS_VIOLATION,\r
- 1146 => MDB2_ERROR_NOSUCHTABLE,\r
- 1149 => MDB2_ERROR_SYNTAX,\r
- 1169 => MDB2_ERROR_CONSTRAINT,\r
- 1176 => MDB2_ERROR_NOT_FOUND,\r
- 1177 => MDB2_ERROR_NOSUCHTABLE,\r
- 1213 => MDB2_ERROR_DEADLOCK,\r
- 1216 => MDB2_ERROR_CONSTRAINT,\r
- 1217 => MDB2_ERROR_CONSTRAINT,\r
- 1227 => MDB2_ERROR_ACCESS_VIOLATION,\r
- 1235 => MDB2_ERROR_CANNOT_CREATE,\r
- 1299 => MDB2_ERROR_INVALID_DATE,\r
- 1300 => MDB2_ERROR_INVALID,\r
- 1304 => MDB2_ERROR_ALREADY_EXISTS,\r
- 1305 => MDB2_ERROR_NOT_FOUND,\r
- 1306 => MDB2_ERROR_CANNOT_DROP,\r
- 1307 => MDB2_ERROR_CANNOT_CREATE,\r
- 1334 => MDB2_ERROR_CANNOT_ALTER,\r
- 1339 => MDB2_ERROR_NOT_FOUND,\r
- 1356 => MDB2_ERROR_INVALID,\r
- 1359 => MDB2_ERROR_ALREADY_EXISTS,\r
- 1360 => MDB2_ERROR_NOT_FOUND,\r
- 1363 => MDB2_ERROR_NOT_FOUND,\r
- 1365 => MDB2_ERROR_DIVZERO,\r
- 1451 => MDB2_ERROR_CONSTRAINT,\r
- 1452 => MDB2_ERROR_CONSTRAINT,\r
- 1542 => MDB2_ERROR_CANNOT_DROP,\r
- 1546 => MDB2_ERROR_CONSTRAINT,\r
- 1582 => MDB2_ERROR_CONSTRAINT,\r
- 2003 => MDB2_ERROR_CONNECT_FAILED,\r
- 2019 => MDB2_ERROR_INVALID,\r
- );\r
- }\r
- if ($this->options['portability'] & MDB2_PORTABILITY_ERRORS) {\r
- $ecode_map[1022] = MDB2_ERROR_CONSTRAINT;\r
- $ecode_map[1048] = MDB2_ERROR_CONSTRAINT_NOT_NULL;\r
- $ecode_map[1062] = MDB2_ERROR_CONSTRAINT;\r
- } else {\r
- // Doing this in case mode changes during runtime.\r
- $ecode_map[1022] = MDB2_ERROR_ALREADY_EXISTS;\r
- $ecode_map[1048] = MDB2_ERROR_CONSTRAINT;\r
- $ecode_map[1062] = MDB2_ERROR_ALREADY_EXISTS;\r
- }\r
- if (isset($ecode_map[$native_code])) {\r
- $error = $ecode_map[$native_code];\r
- }\r
- }\r
- return array($error, $native_code, $native_msg);\r
- }\r
-\r
- // }}}\r
- // {{{ escape()\r
-\r
- /**\r
- * Quotes a string so it can be safely used in a query. It will quote\r
- * the text so it can safely be used within a query.\r
- *\r
- * @param string the input string to quote\r
- * @param bool escape wildcards\r
- *\r
- * @return string quoted string\r
- *\r
- * @access public\r
- */\r
- function escape($text, $escape_wildcards = false)\r
- {\r
- if ($escape_wildcards) {\r
- $text = $this->escapePattern($text);\r
- }\r
- $connection = $this->getConnection();\r
- if (PEAR::isError($connection)) {\r
- return $connection;\r
- }\r
- $text = @mysql_real_escape_string($text, $connection);\r
- return $text;\r
- }\r
-\r
- // }}}\r
- // {{{ beginTransaction()\r
-\r
- /**\r
- * Start a transaction or set a savepoint.\r
- *\r
- * @param string name of a savepoint to set\r
- * @return mixed MDB2_OK on success, a MDB2 error on failure\r
- *\r
- * @access public\r
- */\r
- function beginTransaction($savepoint = null)\r
- {\r
- $this->debug('Starting transaction/savepoint', __FUNCTION__, array('is_manip' => true, 'savepoint' => $savepoint));\r
- $this->_getServerCapabilities();\r
- if (!is_null($savepoint)) {\r
- if (!$this->supports('savepoints')) {\r
- return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,\r
- 'savepoints are not supported', __FUNCTION__);\r
- }\r
- if (!$this->in_transaction) {\r
- return $this->raiseError(MDB2_ERROR_INVALID, null, null,\r
- 'savepoint cannot be released when changes are auto committed', __FUNCTION__);\r
- }\r
- $query = 'SAVEPOINT '.$savepoint;\r
- return $this->_doQuery($query, true);\r
- } elseif ($this->in_transaction) {\r
- return MDB2_OK; //nothing to do\r
- }\r
- if (!$this->destructor_registered && $this->opened_persistent) {\r
- $this->destructor_registered = true;\r
- register_shutdown_function('MDB2_closeOpenTransactions');\r
- }\r
- $query = $this->start_transaction ? 'START TRANSACTION' : 'SET AUTOCOMMIT = 0';\r
- $result =& $this->_doQuery($query, true);\r
- if (PEAR::isError($result)) {\r
- return $result;\r
- }\r
- $this->in_transaction = true;\r
- return MDB2_OK;\r
- }\r
-\r
- // }}}\r
- // {{{ commit()\r
-\r
- /**\r
- * Commit the database changes done during a transaction that is in\r
- * progress or release a savepoint. This function may only be called when\r
- * auto-committing is disabled, otherwise it will fail. Therefore, a new\r
- * transaction is implicitly started after committing the pending changes.\r
- *\r
- * @param string name of a savepoint to release\r
- * @return mixed MDB2_OK on success, a MDB2 error on failure\r
- *\r
- * @access public\r
- */\r
- function commit($savepoint = null)\r
- {\r
- $this->debug('Committing transaction/savepoint', __FUNCTION__, array('is_manip' => true, 'savepoint' => $savepoint));\r
- if (!$this->in_transaction) {\r
- return $this->raiseError(MDB2_ERROR_INVALID, null, null,\r
- 'commit/release savepoint cannot be done changes are auto committed', __FUNCTION__);\r
- }\r
- if (!is_null($savepoint)) {\r
- if (!$this->supports('savepoints')) {\r
- return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,\r
- 'savepoints are not supported', __FUNCTION__);\r
- }\r
- $server_info = $this->getServerVersion();\r
- if (version_compare($server_info['major'].'.'.$server_info['minor'].'.'.$server_info['patch'], '5.0.3', '<')) {\r
- return MDB2_OK;\r
- }\r
- $query = 'RELEASE SAVEPOINT '.$savepoint;\r
- return $this->_doQuery($query, true);\r
- }\r
-\r
- if (!$this->supports('transactions')) {\r
- return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,\r
- 'transactions are not supported', __FUNCTION__);\r
- }\r
-\r
- $result =& $this->_doQuery('COMMIT', true);\r
- if (PEAR::isError($result)) {\r
- return $result;\r
- }\r
- if (!$this->start_transaction) {\r
- $query = 'SET AUTOCOMMIT = 1';\r
- $result =& $this->_doQuery($query, true);\r
- if (PEAR::isError($result)) {\r
- return $result;\r
- }\r
- }\r
- $this->in_transaction = false;\r
- return MDB2_OK;\r
- }\r
-\r
- // }}}\r
- // {{{ rollback()\r
-\r
- /**\r
- * Cancel any database changes done during a transaction or since a specific\r
- * savepoint that is in progress. This function may only be called when\r
- * auto-committing is disabled, otherwise it will fail. Therefore, a new\r
- * transaction is implicitly started after canceling the pending changes.\r
- *\r
- * @param string name of a savepoint to rollback to\r
- * @return mixed MDB2_OK on success, a MDB2 error on failure\r
- *\r
- * @access public\r
- */\r
- function rollback($savepoint = null)\r
- {\r
- $this->debug('Rolling back transaction/savepoint', __FUNCTION__, array('is_manip' => true, 'savepoint' => $savepoint));\r
- if (!$this->in_transaction) {\r
- return $this->raiseError(MDB2_ERROR_INVALID, null, null,\r
- 'rollback cannot be done changes are auto committed', __FUNCTION__);\r
- }\r
- if (!is_null($savepoint)) {\r
- if (!$this->supports('savepoints')) {\r
- return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,\r
- 'savepoints are not supported', __FUNCTION__);\r
- }\r
- $query = 'ROLLBACK TO SAVEPOINT '.$savepoint;\r
- return $this->_doQuery($query, true);\r
- }\r
-\r
- $query = 'ROLLBACK';\r
- $result =& $this->_doQuery($query, true);\r
- if (PEAR::isError($result)) {\r
- return $result;\r
- }\r
- if (!$this->start_transaction) {\r
- $query = 'SET AUTOCOMMIT = 1';\r
- $result =& $this->_doQuery($query, true);\r
- if (PEAR::isError($result)) {\r
- return $result;\r
- }\r
- }\r
- $this->in_transaction = false;\r
- return MDB2_OK;\r
- }\r
-\r
- // }}}\r
- // {{{ function setTransactionIsolation()\r
-\r
- /**\r
- * Set the transacton isolation level.\r
- *\r
- * @param string standard isolation level\r
- * READ UNCOMMITTED (allows dirty reads)\r
- * READ COMMITTED (prevents dirty reads)\r
- * REPEATABLE READ (prevents nonrepeatable reads)\r
- * SERIALIZABLE (prevents phantom reads)\r
- * @return mixed MDB2_OK on success, a MDB2 error on failure\r
- *\r
- * @access public\r
- * @since 2.1.1\r
- */\r
- function setTransactionIsolation($isolation)\r
- {\r
- $this->debug('Setting transaction isolation level', __FUNCTION__, array('is_manip' => true));\r
- if (!$this->supports('transactions')) {\r
- return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,\r
- 'transactions are not supported', __FUNCTION__);\r
- }\r
- switch ($isolation) {\r
- case 'READ UNCOMMITTED':\r
- case 'READ COMMITTED':\r
- case 'REPEATABLE READ':\r
- case 'SERIALIZABLE':\r
- break;\r
- default:\r
- return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,\r
- 'isolation level is not supported: '.$isolation, __FUNCTION__);\r
- }\r
-\r
- $query = "SET SESSION TRANSACTION ISOLATION LEVEL $isolation";\r
- return $this->_doQuery($query, true);\r
- }\r
-\r
- // }}}\r
- // {{{ _doConnect()\r
-\r
- /**\r
- * do the grunt work of the connect\r
- *\r
- * @return connection on success or MDB2 Error Object on failure\r
- * @access protected\r
- */\r
- function _doConnect($username, $password, $persistent = false)\r
- {\r
- if (!PEAR::loadExtension($this->phptype)) {\r
- return $this->raiseError(MDB2_ERROR_NOT_FOUND, null, null,\r
- 'extension '.$this->phptype.' is not compiled into PHP', __FUNCTION__);\r
- }\r
-\r
- $params = array();\r
- if ($this->dsn['protocol'] && $this->dsn['protocol'] == 'unix') {\r
- $params[0] = ':' . $this->dsn['socket'];\r
- } else {\r
- $params[0] = $this->dsn['hostspec'] ? $this->dsn['hostspec']\r
- : 'localhost';\r
- if ($this->dsn['port']) {\r
- $params[0].= ':' . $this->dsn['port'];\r
- }\r
- }\r
- $params[] = $username ? $username : null;\r
- $params[] = $password ? $password : null;\r
- if (!$persistent) {\r
- if ($this->_isNewLinkSet()) {\r
- $params[] = true;\r
- } else {\r
- $params[] = false;\r
- }\r
- }\r
- if (version_compare(phpversion(), '4.3.0', '>=')) {\r
- $params[] = isset($this->dsn['client_flags'])\r
- ? $this->dsn['client_flags'] : null;\r
- }\r
- $connect_function = $persistent ? 'mysql_pconnect' : 'mysql_connect';\r
-\r
- $connection = @call_user_func_array($connect_function, $params);\r
- if (!$connection) {\r
- if (($err = @mysql_error()) != '') {\r
- return $this->raiseError(MDB2_ERROR_CONNECT_FAILED, null, null,\r
- $err, __FUNCTION__);\r
- } else {\r
- return $this->raiseError(MDB2_ERROR_CONNECT_FAILED, null, null,\r
- 'unable to establish a connection', __FUNCTION__);\r
- }\r
- }\r
-\r
- if (!empty($this->dsn['charset'])) {\r
- $result = $this->setCharset($this->dsn['charset'], $connection);\r
- if (PEAR::isError($result)) {\r
- $this->disconnect(false);\r
- return $result;\r
- }\r
- }\r
-\r
- return $connection;\r
- }\r
-\r
- // }}}\r
- // {{{ connect()\r
-\r
- /**\r
- * Connect to the database\r
- *\r
- * @return MDB2_OK on success, MDB2 Error Object on failure\r
- * @access public\r
- */\r
- function connect()\r
- {\r
- if (is_resource($this->connection)) {\r
- //if (count(array_diff($this->connected_dsn, $this->dsn)) == 0\r
- if (MDB2::areEquals($this->connected_dsn, $this->dsn)\r
- && $this->opened_persistent == $this->options['persistent']\r
- ) {\r
- return MDB2_OK;\r
- }\r
- $this->disconnect(false);\r
- }\r
-\r
- $connection = $this->_doConnect(\r
- $this->dsn['username'],\r
- $this->dsn['password'],\r
- $this->options['persistent']\r
- );\r
- if (PEAR::isError($connection)) {\r
- return $connection;\r
- }\r
-\r
- $this->connection = $connection;\r
- $this->connected_dsn = $this->dsn;\r
- $this->connected_database_name = '';\r
- $this->opened_persistent = $this->options['persistent'];\r
- $this->dbsyntax = $this->dsn['dbsyntax'] ? $this->dsn['dbsyntax'] : $this->phptype;\r
-\r
- if ($this->database_name) {\r
- if ($this->database_name != $this->connected_database_name) {\r
- if (!@mysql_select_db($this->database_name, $connection)) {\r
- $err = $this->raiseError(null, null, null,\r
- 'Could not select the database: '.$this->database_name, __FUNCTION__);\r
- return $err;\r
- }\r
- $this->connected_database_name = $this->database_name;\r
- }\r
- }\r
-\r
- $this->_getServerCapabilities();\r
-\r
- return MDB2_OK;\r
- }\r
-\r
- // }}}\r
- // {{{ setCharset()\r
-\r
- /**\r
- * Set the charset on the current connection\r
- *\r
- * @param string charset (or array(charset, collation))\r
- * @param resource connection handle\r
- *\r
- * @return true on success, MDB2 Error Object on failure\r
- */\r
- function setCharset($charset, $connection = null)\r
- {\r
- if (is_null($connection)) {\r
- $connection = $this->getConnection();\r
- if (PEAR::isError($connection)) {\r
- return $connection;\r
- }\r
- }\r
- $collation = null;\r
- if (is_array($charset) && 2 == count($charset)) {\r
- $collation = array_pop($charset);\r
- $charset = array_pop($charset);\r
- }\r
- $client_info = mysql_get_client_info();\r
- if (function_exists('mysql_set_charset') && version_compare($client_info, '5.0.6')) {\r
- if (!$result = mysql_set_charset($charset, $connection)) {\r
- $err =& $this->raiseError(null, null, null,\r
- 'Could not set client character set', __FUNCTION__);\r
- return $err;\r
- }\r
- return $result;\r
- }\r
- $query = "SET NAMES '".mysql_real_escape_string($charset, $connection)."'";\r
- if (!is_null($collation)) {\r
- $query .= " COLLATE '".mysqli_real_escape_string($connection, $collation)."'";\r
- }\r
- return $this->_doQuery($query, true, $connection);\r
- }\r
-\r
- // }}}\r
- // {{{ databaseExists()\r
-\r
- /**\r
- * check if given database name is exists?\r
- *\r
- * @param string $name name of the database that should be checked\r
- *\r
- * @return mixed true/false on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function databaseExists($name)\r
- {\r
- $connection = $this->_doConnect($this->dsn['username'],\r
- $this->dsn['password'],\r
- $this->options['persistent']);\r
- if (PEAR::isError($connection)) {\r
- return $connection;\r
- }\r
-\r
- $result = @mysql_select_db($name, $connection);\r
- @mysql_close($connection);\r
-\r
- return $result;\r
- }\r
-\r
- // }}}\r
- // {{{ disconnect()\r
-\r
- /**\r
- * Log out and disconnect from the database.\r
- *\r
- * @param boolean $force if the disconnect should be forced even if the\r
- * connection is opened persistently\r
- * @return mixed true on success, false if not connected and error\r
- * object on error\r
- * @access public\r
- */\r
- function disconnect($force = true)\r
- {\r
- if (is_resource($this->connection)) {\r
- if ($this->in_transaction) {\r
- $dsn = $this->dsn;\r
- $database_name = $this->database_name;\r
- $persistent = $this->options['persistent'];\r
- $this->dsn = $this->connected_dsn;\r
- $this->database_name = $this->connected_database_name;\r
- $this->options['persistent'] = $this->opened_persistent;\r
- $this->rollback();\r
- $this->dsn = $dsn;\r
- $this->database_name = $database_name;\r
- $this->options['persistent'] = $persistent;\r
- }\r
-\r
- if (!$this->opened_persistent || $force) {\r
- $ok = @mysql_close($this->connection);\r
- if (!$ok) {\r
- return $this->raiseError(MDB2_ERROR_DISCONNECT_FAILED,\r
- null, null, null, __FUNCTION__);\r
- }\r
- }\r
- } else {\r
- return false;\r
- }\r
- return parent::disconnect($force);\r
- }\r
-\r
- // }}}\r
- // {{{ standaloneQuery()\r
-\r
- /**\r
- * execute a query as DBA\r
- *\r
- * @param string $query the SQL query\r
- * @param mixed $types array that contains the types of the columns in\r
- * the result set\r
- * @param boolean $is_manip if the query is a manipulation query\r
- * @return mixed MDB2_OK on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function &standaloneQuery($query, $types = null, $is_manip = false)\r
- {\r
- $user = $this->options['DBA_username']? $this->options['DBA_username'] : $this->dsn['username'];\r
- $pass = $this->options['DBA_password']? $this->options['DBA_password'] : $this->dsn['password'];\r
- $connection = $this->_doConnect($user, $pass, $this->options['persistent']);\r
- if (PEAR::isError($connection)) {\r
- return $connection;\r
- }\r
-\r
- $offset = $this->offset;\r
- $limit = $this->limit;\r
- $this->offset = $this->limit = 0;\r
- $query = $this->_modifyQuery($query, $is_manip, $limit, $offset);\r
- \r
- $result =& $this->_doQuery($query, $is_manip, $connection, $this->database_name);\r
- if (!PEAR::isError($result)) {\r
- $result = $this->_affectedRows($connection, $result);\r
- }\r
-\r
- @mysql_close($connection);\r
- return $result;\r
- }\r
-\r
- // }}}\r
- // {{{ _doQuery()\r
-\r
- /**\r
- * Execute a query\r
- * @param string $query query\r
- * @param boolean $is_manip if the query is a manipulation query\r
- * @param resource $connection\r
- * @param string $database_name\r
- * @return result or error object\r
- * @access protected\r
- */\r
- function &_doQuery($query, $is_manip = false, $connection = null, $database_name = null)\r
- {\r
- $this->last_query = $query;\r
- $result = $this->debug($query, 'query', array('is_manip' => $is_manip, 'when' => 'pre'));\r
- if ($result) {\r
- if (PEAR::isError($result)) {\r
- return $result;\r
- }\r
- $query = $result;\r
- }\r
- if ($this->options['disable_query']) {\r
- $result = $is_manip ? 0 : null;\r
- return $result;\r
- }\r
-\r
- if (is_null($connection)) {\r
- $connection = $this->getConnection();\r
- if (PEAR::isError($connection)) {\r
- return $connection;\r
- }\r
- }\r
- if (is_null($database_name)) {\r
- $database_name = $this->database_name;\r
- }\r
-\r
- if ($database_name) {\r
- if ($database_name != $this->connected_database_name) {\r
- if (!@mysql_select_db($database_name, $connection)) {\r
- $err = $this->raiseError(null, null, null,\r
- 'Could not select the database: '.$database_name, __FUNCTION__);\r
- return $err;\r
- }\r
- $this->connected_database_name = $database_name;\r
- }\r
- }\r
-\r
- $function = $this->options['result_buffering']\r
- ? 'mysql_query' : 'mysql_unbuffered_query';\r
- $result = @$function($query, $connection);\r
- if (!$result) {\r
- $err =& $this->raiseError(null, null, null,\r
- 'Could not execute statement', __FUNCTION__);\r
- return $err;\r
- }\r
-\r
- $this->debug($query, 'query', array('is_manip' => $is_manip, 'when' => 'post', 'result' => $result));\r
- return $result;\r
- }\r
-\r
- // }}}\r
- // {{{ _affectedRows()\r
-\r
- /**\r
- * Returns the number of rows affected\r
- *\r
- * @param resource $result\r
- * @param resource $connection\r
- * @return mixed MDB2 Error Object or the number of rows affected\r
- * @access private\r
- */\r
- function _affectedRows($connection, $result = null)\r
- {\r
- if (is_null($connection)) {\r
- $connection = $this->getConnection();\r
- if (PEAR::isError($connection)) {\r
- return $connection;\r
- }\r
- }\r
- return @mysql_affected_rows($connection);\r
- }\r
-\r
- // }}}\r
- // {{{ _modifyQuery()\r
-\r
- /**\r
- * Changes a query string for various DBMS specific reasons\r
- *\r
- * @param string $query query to modify\r
- * @param boolean $is_manip if it is a DML query\r
- * @param integer $limit limit the number of rows\r
- * @param integer $offset start reading from given offset\r
- * @return string modified query\r
- * @access protected\r
- */\r
- function _modifyQuery($query, $is_manip, $limit, $offset)\r
- {\r
- if ($this->options['portability'] & MDB2_PORTABILITY_DELETE_COUNT) {\r
- // "DELETE FROM table" gives 0 affected rows in MySQL.\r
- // This little hack lets you know how many rows were deleted.\r
- if (preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $query)) {\r
- $query = preg_replace('/^\s*DELETE\s+FROM\s+(\S+)\s*$/',\r
- 'DELETE FROM \1 WHERE 1=1', $query);\r
- }\r
- }\r
- if ($limit > 0\r
- && !preg_match('/LIMIT\s*\d(?:\s*(?:,|OFFSET)\s*\d+)?(?:[^\)]*)?$/i', $query)\r
- ) {\r
- $query = rtrim($query);\r
- if (substr($query, -1) == ';') {\r
- $query = substr($query, 0, -1);\r
- }\r
-\r
- // LIMIT doesn't always come last in the query\r
- // @see http://dev.mysql.com/doc/refman/5.0/en/select.html\r
- $after = '';\r
- if (preg_match('/(\s+INTO\s+(?:OUT|DUMP)FILE\s.*)$/ims', $query, $matches)) {\r
- $after = $matches[0];\r
- $query = preg_replace('/(\s+INTO\s+(?:OUT|DUMP)FILE\s.*)$/ims', '', $query);\r
- } elseif (preg_match('/(\s+FOR\s+UPDATE\s*)$/i', $query, $matches)) {\r
- $after = $matches[0];\r
- $query = preg_replace('/(\s+FOR\s+UPDATE\s*)$/im', '', $query);\r
- } elseif (preg_match('/(\s+LOCK\s+IN\s+SHARE\s+MODE\s*)$/im', $query, $matches)) {\r
- $after = $matches[0];\r
- $query = preg_replace('/(\s+LOCK\s+IN\s+SHARE\s+MODE\s*)$/im', '', $query);\r
- }\r
-\r
- if ($is_manip) {\r
- return $query . " LIMIT $limit" . $after;\r
- } else {\r
- return $query . " LIMIT $offset, $limit" . $after;\r
- }\r
- }\r
- return $query;\r
- }\r
-\r
- // }}}\r
- // {{{ getServerVersion()\r
-\r
- /**\r
- * return version information about the server\r
- *\r
- * @param bool $native determines if the raw version string should be returned\r
- * @return mixed array/string with version information or MDB2 error object\r
- * @access public\r
- */\r
- function getServerVersion($native = false)\r
- {\r
- $connection = $this->getConnection();\r
- if (PEAR::isError($connection)) {\r
- return $connection;\r
- }\r
- if ($this->connected_server_info) {\r
- $server_info = $this->connected_server_info;\r
- } else {\r
- $server_info = @mysql_get_server_info($connection);\r
- }\r
- if (!$server_info) {\r
- return $this->raiseError(null, null, null,\r
- 'Could not get server information', __FUNCTION__);\r
- }\r
- // cache server_info\r
- $this->connected_server_info = $server_info;\r
- if (!$native) {\r
- $tmp = explode('.', $server_info, 3);\r
- if (isset($tmp[2]) && strpos($tmp[2], '-')) {\r
- $tmp2 = explode('-', @$tmp[2], 2);\r
- } else {\r
- $tmp2[0] = isset($tmp[2]) ? $tmp[2] : null;\r
- $tmp2[1] = null;\r
- }\r
- $server_info = array(\r
- 'major' => isset($tmp[0]) ? $tmp[0] : null,\r
- 'minor' => isset($tmp[1]) ? $tmp[1] : null,\r
- 'patch' => $tmp2[0],\r
- 'extra' => $tmp2[1],\r
- 'native' => $server_info,\r
- );\r
- }\r
- return $server_info;\r
- }\r
-\r
- // }}}\r
- // {{{ _getServerCapabilities()\r
-\r
- /**\r
- * Fetch some information about the server capabilities\r
- * (transactions, subselects, prepared statements, etc).\r
- *\r
- * @access private\r
- */\r
- function _getServerCapabilities()\r
- {\r
- if (!$this->server_capabilities_checked) {\r
- $this->server_capabilities_checked = true;\r
-\r
- //set defaults\r
- $this->supported['sub_selects'] = 'emulated';\r
- $this->supported['prepared_statements'] = 'emulated';\r
- $this->supported['triggers'] = false;\r
- $this->start_transaction = false;\r
- $this->varchar_max_length = 255;\r
- \r
- $server_info = $this->getServerVersion();\r
- if (is_array($server_info)) {\r
- $server_version = $server_info['major'].'.'.$server_info['minor'].'.'.$server_info['patch'];\r
-\r
- if (!version_compare($server_version, '4.1.0', '<')) {\r
- $this->supported['sub_selects'] = true;\r
- $this->supported['prepared_statements'] = true;\r
- }\r
-\r
- // SAVEPOINTs were introduced in MySQL 4.0.14 and 4.1.1 (InnoDB)\r
- if (version_compare($server_version, '4.1.0', '>=')) {\r
- if (version_compare($server_version, '4.1.1', '<')) {\r
- $this->supported['savepoints'] = false;\r
- }\r
- } elseif (version_compare($server_version, '4.0.14', '<')) {\r
- $this->supported['savepoints'] = false;\r
- }\r
-\r
- if (!version_compare($server_version, '4.0.11', '<')) {\r
- $this->start_transaction = true;\r
- }\r
-\r
- if (!version_compare($server_version, '5.0.3', '<')) {\r
- $this->varchar_max_length = 65532;\r
- }\r
-\r
- if (!version_compare($server_version, '5.0.2', '<')) {\r
- $this->supported['triggers'] = true;\r
- }\r
- }\r
- }\r
- }\r
-\r
- // }}}\r
- // {{{ function _skipUserDefinedVariable($query, $position)\r
-\r
- /**\r
- * Utility method, used by prepare() to avoid misinterpreting MySQL user \r
- * defined variables (SELECT @x:=5) for placeholders.\r
- * Check if the placeholder is a false positive, i.e. if it is an user defined\r
- * variable instead. If so, skip it and advance the position, otherwise\r
- * return the current position, which is valid\r
- *\r
- * @param string $query\r
- * @param integer $position current string cursor position\r
- * @return integer $new_position\r
- * @access protected\r
- */\r
- function _skipUserDefinedVariable($query, $position)\r
- {\r
- $found = strpos(strrev(substr($query, 0, $position)), '@');\r
- if ($found === false) {\r
- return $position;\r
- }\r
- $pos = strlen($query) - strlen(substr($query, $position)) - $found - 1;\r
- $substring = substr($query, $pos, $position - $pos + 2);\r
- if (preg_match('/^@\w+\s*:=$/', $substring)) {\r
- return $position + 1; //found an user defined variable: skip it\r
- }\r
- return $position;\r
- }\r
-\r
- // }}}\r
- // {{{ prepare()\r
-\r
- /**\r
- * Prepares a query for multiple execution with execute().\r
- * With some database backends, this is emulated.\r
- * prepare() requires a generic query as string like\r
- * 'INSERT INTO numbers VALUES(?,?)' or\r
- * 'INSERT INTO numbers VALUES(:foo,:bar)'.\r
- * The ? and :name and are placeholders which can be set using\r
- * bindParam() and the query can be sent off using the execute() method.\r
- * The allowed format for :name can be set with the 'bindname_format' option.\r
- *\r
- * @param string $query the query to prepare\r
- * @param mixed $types array that contains the types of the placeholders\r
- * @param mixed $result_types array that contains the types of the columns in\r
- * the result set or MDB2_PREPARE_RESULT, if set to\r
- * MDB2_PREPARE_MANIP the query is handled as a manipulation query\r
- * @param mixed $lobs key (field) value (parameter) pair for all lob placeholders\r
- * @return mixed resource handle for the prepared query on success, a MDB2\r
- * error on failure\r
- * @access public\r
- * @see bindParam, execute\r
- */\r
- function &prepare($query, $types = null, $result_types = null, $lobs = array())\r
- {\r
- if ($this->options['emulate_prepared']\r
- || $this->supported['prepared_statements'] !== true\r
- ) {\r
- $obj =& parent::prepare($query, $types, $result_types, $lobs);\r
- return $obj;\r
- }\r
- $is_manip = ($result_types === MDB2_PREPARE_MANIP);\r
- $offset = $this->offset;\r
- $limit = $this->limit;\r
- $this->offset = $this->limit = 0;\r
- $query = $this->_modifyQuery($query, $is_manip, $limit, $offset);\r
- $result = $this->debug($query, __FUNCTION__, array('is_manip' => $is_manip, 'when' => 'pre'));\r
- if ($result) {\r
- if (PEAR::isError($result)) {\r
- return $result;\r
- }\r
- $query = $result;\r
- }\r
- $placeholder_type_guess = $placeholder_type = null;\r
- $question = '?';\r
- $colon = ':';\r
- $positions = array();\r
- $position = 0;\r
- while ($position < strlen($query)) {\r
- $q_position = strpos($query, $question, $position);\r
- $c_position = strpos($query, $colon, $position);\r
- if ($q_position && $c_position) {\r
- $p_position = min($q_position, $c_position);\r
- } elseif ($q_position) {\r
- $p_position = $q_position;\r
- } elseif ($c_position) {\r
- $p_position = $c_position;\r
- } else {\r
- break;\r
- }\r
- if (is_null($placeholder_type)) {\r
- $placeholder_type_guess = $query[$p_position];\r
- }\r
- \r
- $new_pos = $this->_skipDelimitedStrings($query, $position, $p_position);\r
- if (PEAR::isError($new_pos)) {\r
- return $new_pos;\r
- }\r
- if ($new_pos != $position) {\r
- $position = $new_pos;\r
- continue; //evaluate again starting from the new position\r
- }\r
- \r
- //make sure this is not part of an user defined variable\r
- $new_pos = $this->_skipUserDefinedVariable($query, $position);\r
- if ($new_pos != $position) {\r
- $position = $new_pos;\r
- continue; //evaluate again starting from the new position\r
- }\r
-\r
- if ($query[$position] == $placeholder_type_guess) {\r
- if (is_null($placeholder_type)) {\r
- $placeholder_type = $query[$p_position];\r
- $question = $colon = $placeholder_type;\r
- }\r
- if ($placeholder_type == ':') {\r
- $regexp = '/^.{'.($position+1).'}('.$this->options['bindname_format'].').*$/s';\r
- $parameter = preg_replace($regexp, '\\1', $query);\r
- if ($parameter === '') {\r
- $err =& $this->raiseError(MDB2_ERROR_SYNTAX, null, null,\r
- 'named parameter name must match "bindname_format" option', __FUNCTION__);\r
- return $err;\r
- }\r
- $positions[$p_position] = $parameter;\r
- $query = substr_replace($query, '?', $position, strlen($parameter)+1);\r
- } else {\r
- $positions[$p_position] = count($positions);\r
- }\r
- $position = $p_position + 1;\r
- } else {\r
- $position = $p_position;\r
- }\r
- }\r
- $connection = $this->getConnection();\r
- if (PEAR::isError($connection)) {\r
- return $connection;\r
- }\r
- static $prep_statement_counter = 1;\r
- $statement_name = sprintf($this->options['statement_format'], $this->phptype, $prep_statement_counter++ . sha1(microtime() + mt_rand()));\r
- $statement_name = substr(strtolower($statement_name), 0, $this->options['max_identifiers_length']);\r
- $query = "PREPARE $statement_name FROM ".$this->quote($query, 'text');\r
- $statement =& $this->_doQuery($query, true, $connection);\r
- if (PEAR::isError($statement)) {\r
- return $statement;\r
- }\r
-\r
- $class_name = 'MDB2_Statement_'.$this->phptype;\r
- $obj = new $class_name($this, $statement_name, $positions, $query, $types, $result_types, $is_manip, $limit, $offset);\r
- $this->debug($query, __FUNCTION__, array('is_manip' => $is_manip, 'when' => 'post', 'result' => $obj));\r
- return $obj;\r
- }\r
-\r
- // }}}\r
- // {{{ replace()\r
-\r
- /**\r
- * Execute a SQL REPLACE query. A REPLACE query is identical to a INSERT\r
- * query, except that if there is already a row in the table with the same\r
- * key field values, the old row is deleted before the new row is inserted.\r
- *\r
- * The REPLACE type of query does not make part of the SQL standards. Since\r
- * practically only MySQL implements it natively, this type of query is\r
- * emulated through this method for other DBMS using standard types of\r
- * queries inside a transaction to assure the atomicity of the operation.\r
- *\r
- * @access public\r
- *\r
- * @param string $table name of the table on which the REPLACE query will\r
- * be executed.\r
- * @param array $fields associative array that describes the fields and the\r
- * values that will be inserted or updated in the specified table. The\r
- * indexes of the array are the names of all the fields of the table. The\r
- * values of the array are also associative arrays that describe the\r
- * values and other properties of the table fields.\r
- *\r
- * Here follows a list of field properties that need to be specified:\r
- *\r
- * value:\r
- * Value to be assigned to the specified field. This value may be\r
- * of specified in database independent type format as this\r
- * function can perform the necessary datatype conversions.\r
- *\r
- * Default:\r
- * this property is required unless the Null property\r
- * is set to 1.\r
- *\r
- * type\r
- * Name of the type of the field. Currently, all types Metabase\r
- * are supported except for clob and blob.\r
- *\r
- * Default: no type conversion\r
- *\r
- * null\r
- * Boolean property that indicates that the value for this field\r
- * should be set to null.\r
- *\r
- * The default value for fields missing in INSERT queries may be\r
- * specified the definition of a table. Often, the default value\r
- * is already null, but since the REPLACE may be emulated using\r
- * an UPDATE query, make sure that all fields of the table are\r
- * listed in this function argument array.\r
- *\r
- * Default: 0\r
- *\r
- * key\r
- * Boolean property that indicates that this field should be\r
- * handled as a primary key or at least as part of the compound\r
- * unique index of the table that will determine the row that will\r
- * updated if it exists or inserted a new row otherwise.\r
- *\r
- * This function will fail if no key field is specified or if the\r
- * value of a key field is set to null because fields that are\r
- * part of unique index they may not be null.\r
- *\r
- * Default: 0\r
- *\r
- * @see http://dev.mysql.com/doc/refman/5.0/en/replace.html\r
- * @return mixed MDB2_OK on success, a MDB2 error on failure\r
- */\r
- function replace($table, $fields)\r
- {\r
- $count = count($fields);\r
- $query = $values = '';\r
- $keys = $colnum = 0;\r
- for (reset($fields); $colnum < $count; next($fields), $colnum++) {\r
- $name = key($fields);\r
- if ($colnum > 0) {\r
- $query .= ',';\r
- $values.= ',';\r
- }\r
- $query.= $this->quoteIdentifier($name, true);\r
- if (isset($fields[$name]['null']) && $fields[$name]['null']) {\r
- $value = 'NULL';\r
- } else {\r
- $type = isset($fields[$name]['type']) ? $fields[$name]['type'] : null;\r
- $value = $this->quote($fields[$name]['value'], $type);\r
- if (PEAR::isError($value)) {\r
- return $value;\r
- }\r
- }\r
- $values.= $value;\r
- if (isset($fields[$name]['key']) && $fields[$name]['key']) {\r
- if ($value === 'NULL') {\r
- return $this->raiseError(MDB2_ERROR_CANNOT_REPLACE, null, null,\r
- 'key value '.$name.' may not be NULL', __FUNCTION__);\r
- }\r
- $keys++;\r
- }\r
- }\r
- if ($keys == 0) {\r
- return $this->raiseError(MDB2_ERROR_CANNOT_REPLACE, null, null,\r
- 'not specified which fields are keys', __FUNCTION__);\r
- }\r
-\r
- $connection = $this->getConnection();\r
- if (PEAR::isError($connection)) {\r
- return $connection;\r
- }\r
-\r
- $table = $this->quoteIdentifier($table, true);\r
- $query = "REPLACE INTO $table ($query) VALUES ($values)";\r
- $result =& $this->_doQuery($query, true, $connection);\r
- if (PEAR::isError($result)) {\r
- return $result;\r
- }\r
- return $this->_affectedRows($connection, $result);\r
- }\r
-\r
- // }}}\r
- // {{{ nextID()\r
-\r
- /**\r
- * Returns the next free id of a sequence\r
- *\r
- * @param string $seq_name name of the sequence\r
- * @param boolean $ondemand when true the sequence is\r
- * automatic created, if it\r
- * not exists\r
- *\r
- * @return mixed MDB2 Error Object or id\r
- * @access public\r
- */\r
- function nextID($seq_name, $ondemand = true)\r
- {\r
- $sequence_name = $this->quoteIdentifier($this->getSequenceName($seq_name), true);\r
- $seqcol_name = $this->quoteIdentifier($this->options['seqcol_name'], true);\r
- $query = "INSERT INTO $sequence_name ($seqcol_name) VALUES (NULL)";\r
- $this->pushErrorHandling(PEAR_ERROR_RETURN);\r
- $this->expectError(MDB2_ERROR_NOSUCHTABLE);\r
- $result =& $this->_doQuery($query, true);\r
- $this->popExpect();\r
- $this->popErrorHandling();\r
- if (PEAR::isError($result)) {\r
- if ($ondemand && $result->getCode() == MDB2_ERROR_NOSUCHTABLE) {\r
- $this->loadModule('Manager', null, true);\r
- $result = $this->manager->createSequence($seq_name);\r
- if (PEAR::isError($result)) {\r
- return $this->raiseError($result, null, null,\r
- 'on demand sequence '.$seq_name.' could not be created', __FUNCTION__);\r
- } else {\r
- return $this->nextID($seq_name, false);\r
- }\r
- }\r
- return $result;\r
- }\r
- $value = $this->lastInsertID();\r
- if (is_numeric($value)) {\r
- $query = "DELETE FROM $sequence_name WHERE $seqcol_name < $value";\r
- $result =& $this->_doQuery($query, true);\r
- if (PEAR::isError($result)) {\r
- $this->warnings[] = 'nextID: could not delete previous sequence table values from '.$seq_name;\r
- }\r
- }\r
- return $value;\r
- }\r
-\r
- // }}}\r
- // {{{ lastInsertID()\r
-\r
- /**\r
- * Returns the autoincrement ID if supported or $id or fetches the current\r
- * ID in a sequence called: $table.(empty($field) ? '' : '_'.$field)\r
- *\r
- * @param string $table name of the table into which a new row was inserted\r
- * @param string $field name of the field into which a new row was inserted\r
- * @return mixed MDB2 Error Object or id\r
- * @access public\r
- */\r
- function lastInsertID($table = null, $field = null)\r
- {\r
- // not using mysql_insert_id() due to http://pear.php.net/bugs/bug.php?id=8051\r
- return $this->queryOne('SELECT LAST_INSERT_ID()', 'integer');\r
- }\r
-\r
- // }}}\r
- // {{{ currID()\r
-\r
- /**\r
- * Returns the current id of a sequence\r
- *\r
- * @param string $seq_name name of the sequence\r
- * @return mixed MDB2 Error Object or id\r
- * @access public\r
- */\r
- function currID($seq_name)\r
- {\r
- $sequence_name = $this->quoteIdentifier($this->getSequenceName($seq_name), true);\r
- $seqcol_name = $this->quoteIdentifier($this->options['seqcol_name'], true);\r
- $query = "SELECT MAX($seqcol_name) FROM $sequence_name";\r
- return $this->queryOne($query, 'integer');\r
- }\r
-}\r
-\r
-/**\r
- * MDB2 MySQL result driver\r
- *\r
- * @package MDB2\r
- * @category Database\r
- * @author Lukas Smith <smith@pooteeweet.org>\r
- */\r
-class MDB2_Result_mysql extends MDB2_Result_Common\r
-{\r
- // }}}\r
- // {{{ fetchRow()\r
-\r
- /**\r
- * Fetch a row and insert the data into an existing array.\r
- *\r
- * @param int $fetchmode how the array data should be indexed\r
- * @param int $rownum number of the row where the data can be found\r
- * @return int data array on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT, $rownum = null)\r
- {\r
- if (!is_null($rownum)) {\r
- $seek = $this->seek($rownum);\r
- if (PEAR::isError($seek)) {\r
- return $seek;\r
- }\r
- }\r
- if ($fetchmode == MDB2_FETCHMODE_DEFAULT) {\r
- $fetchmode = $this->db->fetchmode;\r
- }\r
- if ($fetchmode & MDB2_FETCHMODE_ASSOC) {\r
- $row = @mysql_fetch_assoc($this->result);\r
- if (is_array($row)\r
- && $this->db->options['portability'] & MDB2_PORTABILITY_FIX_CASE\r
- ) {\r
- $row = array_change_key_case($row, $this->db->options['field_case']);\r
- }\r
- } else {\r
- $row = @mysql_fetch_row($this->result);\r
- }\r
-\r
- if (!$row) {\r
- if ($this->result === false) {\r
- $err =& $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,\r
- 'resultset has already been freed', __FUNCTION__);\r
- return $err;\r
- }\r
- $null = null;\r
- return $null;\r
- }\r
- $mode = $this->db->options['portability'] & MDB2_PORTABILITY_EMPTY_TO_NULL;\r
- $rtrim = false;\r
- if ($this->db->options['portability'] & MDB2_PORTABILITY_RTRIM) {\r
- if (empty($this->types)) {\r
- $mode += MDB2_PORTABILITY_RTRIM;\r
- } else {\r
- $rtrim = true;\r
- }\r
- }\r
- if ($mode) {\r
- $this->db->_fixResultArrayValues($row, $mode);\r
- }\r
- if (!empty($this->types)) {\r
- $row = $this->db->datatype->convertResultRow($this->types, $row, $rtrim);\r
- }\r
- if (!empty($this->values)) {\r
- $this->_assignBindColumns($row);\r
- }\r
- if ($fetchmode === MDB2_FETCHMODE_OBJECT) {\r
- $object_class = $this->db->options['fetch_class'];\r
- if ($object_class == 'stdClass') {\r
- $row = (object) $row;\r
- } else {\r
- $row = &new $object_class($row);\r
- }\r
- }\r
- ++$this->rownum;\r
- return $row;\r
- }\r
-\r
- // }}}\r
- // {{{ _getColumnNames()\r
-\r
- /**\r
- * Retrieve the names of columns returned by the DBMS in a query result.\r
- *\r
- * @return mixed Array variable that holds the names of columns as keys\r
- * or an MDB2 error on failure.\r
- * Some DBMS may not return any columns when the result set\r
- * does not contain any rows.\r
- * @access private\r
- */\r
- function _getColumnNames()\r
- {\r
- $columns = array();\r
- $numcols = $this->numCols();\r
- if (PEAR::isError($numcols)) {\r
- return $numcols;\r
- }\r
- for ($column = 0; $column < $numcols; $column++) {\r
- $column_name = @mysql_field_name($this->result, $column);\r
- $columns[$column_name] = $column;\r
- }\r
- if ($this->db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {\r
- $columns = array_change_key_case($columns, $this->db->options['field_case']);\r
- }\r
- return $columns;\r
- }\r
-\r
- // }}}\r
- // {{{ numCols()\r
-\r
- /**\r
- * Count the number of columns returned by the DBMS in a query result.\r
- *\r
- * @return mixed integer value with the number of columns, a MDB2 error\r
- * on failure\r
- * @access public\r
- */\r
- function numCols()\r
- {\r
- $cols = @mysql_num_fields($this->result);\r
- if (is_null($cols)) {\r
- if ($this->result === false) {\r
- return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,\r
- 'resultset has already been freed', __FUNCTION__);\r
- } elseif (is_null($this->result)) {\r
- return count($this->types);\r
- }\r
- return $this->db->raiseError(null, null, null,\r
- 'Could not get column count', __FUNCTION__);\r
- }\r
- return $cols;\r
- }\r
-\r
- // }}}\r
- // {{{ free()\r
-\r
- /**\r
- * Free the internal resources associated with result.\r
- *\r
- * @return boolean true on success, false if result is invalid\r
- * @access public\r
- */\r
- function free()\r
- {\r
- if (is_resource($this->result) && $this->db->connection) {\r
- $free = @mysql_free_result($this->result);\r
- if ($free === false) {\r
- return $this->db->raiseError(null, null, null,\r
- 'Could not free result', __FUNCTION__);\r
- }\r
- }\r
- $this->result = false;\r
- return MDB2_OK;\r
- }\r
-}\r
-\r
-/**\r
- * MDB2 MySQL buffered result driver\r
- *\r
- * @package MDB2\r
- * @category Database\r
- * @author Lukas Smith <smith@pooteeweet.org>\r
- */\r
-class MDB2_BufferedResult_mysql extends MDB2_Result_mysql\r
-{\r
- // }}}\r
- // {{{ seek()\r
-\r
- /**\r
- * Seek to a specific row in a result set\r
- *\r
- * @param int $rownum number of the row where the data can be found\r
- * @return mixed MDB2_OK on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function seek($rownum = 0)\r
- {\r
- if ($this->rownum != ($rownum - 1) && !@mysql_data_seek($this->result, $rownum)) {\r
- if ($this->result === false) {\r
- return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,\r
- 'resultset has already been freed', __FUNCTION__);\r
- } elseif (is_null($this->result)) {\r
- return MDB2_OK;\r
- }\r
- return $this->db->raiseError(MDB2_ERROR_INVALID, null, null,\r
- 'tried to seek to an invalid row number ('.$rownum.')', __FUNCTION__);\r
- }\r
- $this->rownum = $rownum - 1;\r
- return MDB2_OK;\r
- }\r
-\r
- // }}}\r
- // {{{ valid()\r
-\r
- /**\r
- * Check if the end of the result set has been reached\r
- *\r
- * @return mixed true or false on sucess, a MDB2 error on failure\r
- * @access public\r
- */\r
- function valid()\r
- {\r
- $numrows = $this->numRows();\r
- if (PEAR::isError($numrows)) {\r
- return $numrows;\r
- }\r
- return $this->rownum < ($numrows - 1);\r
- }\r
-\r
- // }}}\r
- // {{{ numRows()\r
-\r
- /**\r
- * Returns the number of rows in a result object\r
- *\r
- * @return mixed MDB2 Error Object or the number of rows\r
- * @access public\r
- */\r
- function numRows()\r
- {\r
- $rows = @mysql_num_rows($this->result);\r
- if (false === $rows) {\r
- if (false === $this->result) {\r
- return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,\r
- 'resultset has already been freed', __FUNCTION__);\r
- } elseif (is_null($this->result)) {\r
- return 0;\r
- }\r
- return $this->db->raiseError(null, null, null,\r
- 'Could not get row count', __FUNCTION__);\r
- }\r
- return $rows;\r
- }\r
-}\r
-\r
-/**\r
- * MDB2 MySQL statement driver\r
- *\r
- * @package MDB2\r
- * @category Database\r
- * @author Lukas Smith <smith@pooteeweet.org>\r
- */\r
-class MDB2_Statement_mysql extends MDB2_Statement_Common\r
-{\r
- // {{{ _execute()\r
-\r
- /**\r
- * Execute a prepared query statement helper method.\r
- *\r
- * @param mixed $result_class string which specifies which result class to use\r
- * @param mixed $result_wrap_class string which specifies which class to wrap results in\r
- *\r
- * @return mixed MDB2_Result or integer (affected rows) on success,\r
- * a MDB2 error on failure\r
- * @access private\r
- */\r
- function &_execute($result_class = true, $result_wrap_class = false)\r
- {\r
- if (is_null($this->statement)) {\r
- $result =& parent::_execute($result_class, $result_wrap_class);\r
- return $result;\r
- }\r
- $this->db->last_query = $this->query;\r
- $this->db->debug($this->query, 'execute', array('is_manip' => $this->is_manip, 'when' => 'pre', 'parameters' => $this->values));\r
- if ($this->db->getOption('disable_query')) {\r
- $result = $this->is_manip ? 0 : null;\r
- return $result;\r
- }\r
-\r
- $connection = $this->db->getConnection();\r
- if (PEAR::isError($connection)) {\r
- return $connection;\r
- }\r
-\r
- $query = 'EXECUTE '.$this->statement;\r
- if (!empty($this->positions)) {\r
- $parameters = array();\r
- foreach ($this->positions as $parameter) {\r
- if (!array_key_exists($parameter, $this->values)) {\r
- return $this->db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,\r
- 'Unable to bind to missing placeholder: '.$parameter, __FUNCTION__);\r
- }\r
- $value = $this->values[$parameter];\r
- $type = array_key_exists($parameter, $this->types) ? $this->types[$parameter] : null;\r
- if (is_resource($value) || $type == 'clob' || $type == 'blob' && $this->db->options['lob_allow_url_include']) {\r
- if (!is_resource($value) && preg_match('/^(\w+:\/\/)(.*)$/', $value, $match)) {\r
- if ($match[1] == 'file://') {\r
- $value = $match[2];\r
- }\r
- $value = @fopen($value, 'r');\r
- $close = true;\r
- }\r
- if (is_resource($value)) {\r
- $data = '';\r
- while (!@feof($value)) {\r
- $data.= @fread($value, $this->db->options['lob_buffer_length']);\r
- }\r
- if ($close) {\r
- @fclose($value);\r
- }\r
- $value = $data;\r
- }\r
- }\r
- $quoted = $this->db->quote($value, $type);\r
- if (PEAR::isError($quoted)) {\r
- return $quoted;\r
- }\r
- $param_query = 'SET @'.$parameter.' = '.$quoted;\r
- $result = $this->db->_doQuery($param_query, true, $connection);\r
- if (PEAR::isError($result)) {\r
- return $result;\r
- }\r
- }\r
- $query.= ' USING @'.implode(', @', array_values($this->positions));\r
- }\r
-\r
- $result = $this->db->_doQuery($query, $this->is_manip, $connection);\r
- if (PEAR::isError($result)) {\r
- return $result;\r
- }\r
-\r
- if ($this->is_manip) {\r
- $affected_rows = $this->db->_affectedRows($connection, $result);\r
- return $affected_rows;\r
- }\r
-\r
- $result =& $this->db->_wrapResult($result, $this->result_types,\r
- $result_class, $result_wrap_class, $this->limit, $this->offset);\r
- $this->db->debug($this->query, 'execute', array('is_manip' => $this->is_manip, 'when' => 'post', 'result' => $result));\r
- return $result;\r
- }\r
-\r
- // }}}\r
- // {{{ free()\r
-\r
- /**\r
- * Release resources allocated for the specified prepared query.\r
- *\r
- * @return mixed MDB2_OK on success, a MDB2 error on failure\r
- * @access public\r
- */\r
- function free()\r
- {\r
- if (is_null($this->positions)) {\r
- return $this->db->raiseError(MDB2_ERROR, null, null,\r
- 'Prepared statement has already been freed', __FUNCTION__);\r
- }\r
- $result = MDB2_OK;\r
-\r
- if (!is_null($this->statement)) {\r
- $connection = $this->db->getConnection();\r
- if (PEAR::isError($connection)) {\r
- return $connection;\r
- }\r
- $query = 'DEALLOCATE PREPARE '.$this->statement;\r
- $result = $this->db->_doQuery($query, true, $connection);\r
- }\r
-\r
- parent::free();\r
- return $result;\r
- }\r
-}\r
+<?php
+// vim: set et ts=4 sw=4 fdm=marker:
+// +----------------------------------------------------------------------+
+// | 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: mysql.php,v 1.214 2008/11/16 21:45:08 quipo Exp $
+//
+
+/**
+ * MDB2 MySQL driver
+ *
+ * @package MDB2
+ * @category Database
+ * @author Lukas Smith <smith@pooteeweet.org>
+ */
+class MDB2_Driver_mysql extends MDB2_Driver_Common
+{
+ // {{{ properties
+
+ var $string_quoting = array('start' => "'", 'end' => "'", 'escape' => '\\', 'escape_pattern' => '\\');
+
+ var $identifier_quoting = array('start' => '`', 'end' => '`', 'escape' => '`');
+
+ var $sql_comments = array(
+ array('start' => '-- ', 'end' => "\n", 'escape' => false),
+ array('start' => '#', 'end' => "\n", 'escape' => false),
+ array('start' => '/*', 'end' => '*/', 'escape' => false),
+ );
+
+ var $server_capabilities_checked = false;
+
+ var $start_transaction = false;
+
+ var $varchar_max_length = 255;
+
+ // }}}
+ // {{{ constructor
+
+ /**
+ * Constructor
+ */
+ function __construct()
+ {
+ parent::__construct();
+
+ $this->phptype = 'mysql';
+ $this->dbsyntax = 'mysql';
+
+ $this->supported['sequences'] = 'emulated';
+ $this->supported['indexes'] = true;
+ $this->supported['affected_rows'] = true;
+ $this->supported['transactions'] = false;
+ $this->supported['savepoints'] = false;
+ $this->supported['summary_functions'] = true;
+ $this->supported['order_by_text'] = true;
+ $this->supported['current_id'] = 'emulated';
+ $this->supported['limit_queries'] = true;
+ $this->supported['LOBs'] = true;
+ $this->supported['replace'] = true;
+ $this->supported['sub_selects'] = 'emulated';
+ $this->supported['triggers'] = false;
+ $this->supported['auto_increment'] = true;
+ $this->supported['primary_key'] = true;
+ $this->supported['result_introspection'] = true;
+ $this->supported['prepared_statements'] = 'emulated';
+ $this->supported['identifier_quoting'] = true;
+ $this->supported['pattern_escaping'] = true;
+ $this->supported['new_link'] = true;
+
+ $this->options['DBA_username'] = false;
+ $this->options['DBA_password'] = false;
+ $this->options['default_table_type'] = '';
+ $this->options['max_identifiers_length'] = 64;
+
+ $this->_reCheckSupportedOptions();
+ }
+
+ // }}}
+ // {{{ _reCheckSupportedOptions()
+
+ /**
+ * If the user changes certain options, other capabilities may depend
+ * on the new settings, so we need to check them (again).
+ *
+ * @access private
+ */
+ function _reCheckSupportedOptions()
+ {
+ $this->supported['transactions'] = $this->options['use_transactions'];
+ $this->supported['savepoints'] = $this->options['use_transactions'];
+ if ($this->options['default_table_type']) {
+ switch (strtoupper($this->options['default_table_type'])) {
+ case 'BLACKHOLE':
+ case 'MEMORY':
+ case 'ARCHIVE':
+ case 'CSV':
+ case 'HEAP':
+ case 'ISAM':
+ case 'MERGE':
+ case 'MRG_ISAM':
+ case 'ISAM':
+ case 'MRG_MYISAM':
+ case 'MYISAM':
+ $this->supported['savepoints'] = false;
+ $this->supported['transactions'] = false;
+ $this->warnings[] = $this->options['default_table_type'] .
+ ' is not a supported default table type';
+ break;
+ }
+ }
+ }
+
+ // }}}
+ // {{{ function setOption($option, $value)
+
+ /**
+ * set the option for the db class
+ *
+ * @param string option name
+ * @param mixed value for the option
+ *
+ * @return mixed MDB2_OK or MDB2 Error Object
+ *
+ * @access public
+ */
+ function setOption($option, $value)
+ {
+ $res = parent::setOption($option, $value);
+ $this->_reCheckSupportedOptions();
+ }
+
+ // }}}
+ // {{{ errorInfo()
+
+ /**
+ * This method is used to collect information about an error
+ *
+ * @param integer $error
+ * @return array
+ * @access public
+ */
+ function errorInfo($error = null)
+ {
+ if ($this->connection) {
+ $native_code = @mysql_errno($this->connection);
+ $native_msg = @mysql_error($this->connection);
+ } else {
+ $native_code = @mysql_errno();
+ $native_msg = @mysql_error();
+ }
+ if (is_null($error)) {
+ static $ecode_map;
+ if (empty($ecode_map)) {
+ $ecode_map = array(
+ 1000 => MDB2_ERROR_INVALID, //hashchk
+ 1001 => MDB2_ERROR_INVALID, //isamchk
+ 1004 => MDB2_ERROR_CANNOT_CREATE,
+ 1005 => MDB2_ERROR_CANNOT_CREATE,
+ 1006 => MDB2_ERROR_CANNOT_CREATE,
+ 1007 => MDB2_ERROR_ALREADY_EXISTS,
+ 1008 => MDB2_ERROR_CANNOT_DROP,
+ 1009 => MDB2_ERROR_CANNOT_DROP,
+ 1010 => MDB2_ERROR_CANNOT_DROP,
+ 1011 => MDB2_ERROR_CANNOT_DELETE,
+ 1022 => MDB2_ERROR_ALREADY_EXISTS,
+ 1029 => MDB2_ERROR_NOT_FOUND,
+ 1032 => MDB2_ERROR_NOT_FOUND,
+ 1044 => MDB2_ERROR_ACCESS_VIOLATION,
+ 1045 => MDB2_ERROR_ACCESS_VIOLATION,
+ 1046 => MDB2_ERROR_NODBSELECTED,
+ 1048 => MDB2_ERROR_CONSTRAINT,
+ 1049 => MDB2_ERROR_NOSUCHDB,
+ 1050 => MDB2_ERROR_ALREADY_EXISTS,
+ 1051 => MDB2_ERROR_NOSUCHTABLE,
+ 1054 => MDB2_ERROR_NOSUCHFIELD,
+ 1060 => MDB2_ERROR_ALREADY_EXISTS,
+ 1061 => MDB2_ERROR_ALREADY_EXISTS,
+ 1062 => MDB2_ERROR_ALREADY_EXISTS,
+ 1064 => MDB2_ERROR_SYNTAX,
+ 1067 => MDB2_ERROR_INVALID,
+ 1072 => MDB2_ERROR_NOT_FOUND,
+ 1086 => MDB2_ERROR_ALREADY_EXISTS,
+ 1091 => MDB2_ERROR_NOT_FOUND,
+ 1100 => MDB2_ERROR_NOT_LOCKED,
+ 1109 => MDB2_ERROR_NOT_FOUND,
+ 1125 => MDB2_ERROR_ALREADY_EXISTS,
+ 1136 => MDB2_ERROR_VALUE_COUNT_ON_ROW,
+ 1138 => MDB2_ERROR_INVALID,
+ 1142 => MDB2_ERROR_ACCESS_VIOLATION,
+ 1143 => MDB2_ERROR_ACCESS_VIOLATION,
+ 1146 => MDB2_ERROR_NOSUCHTABLE,
+ 1149 => MDB2_ERROR_SYNTAX,
+ 1169 => MDB2_ERROR_CONSTRAINT,
+ 1176 => MDB2_ERROR_NOT_FOUND,
+ 1177 => MDB2_ERROR_NOSUCHTABLE,
+ 1213 => MDB2_ERROR_DEADLOCK,
+ 1216 => MDB2_ERROR_CONSTRAINT,
+ 1217 => MDB2_ERROR_CONSTRAINT,
+ 1227 => MDB2_ERROR_ACCESS_VIOLATION,
+ 1235 => MDB2_ERROR_CANNOT_CREATE,
+ 1299 => MDB2_ERROR_INVALID_DATE,
+ 1300 => MDB2_ERROR_INVALID,
+ 1304 => MDB2_ERROR_ALREADY_EXISTS,
+ 1305 => MDB2_ERROR_NOT_FOUND,
+ 1306 => MDB2_ERROR_CANNOT_DROP,
+ 1307 => MDB2_ERROR_CANNOT_CREATE,
+ 1334 => MDB2_ERROR_CANNOT_ALTER,
+ 1339 => MDB2_ERROR_NOT_FOUND,
+ 1356 => MDB2_ERROR_INVALID,
+ 1359 => MDB2_ERROR_ALREADY_EXISTS,
+ 1360 => MDB2_ERROR_NOT_FOUND,
+ 1363 => MDB2_ERROR_NOT_FOUND,
+ 1365 => MDB2_ERROR_DIVZERO,
+ 1451 => MDB2_ERROR_CONSTRAINT,
+ 1452 => MDB2_ERROR_CONSTRAINT,
+ 1542 => MDB2_ERROR_CANNOT_DROP,
+ 1546 => MDB2_ERROR_CONSTRAINT,
+ 1582 => MDB2_ERROR_CONSTRAINT,
+ 2003 => MDB2_ERROR_CONNECT_FAILED,
+ 2019 => MDB2_ERROR_INVALID,
+ );
+ }
+ if ($this->options['portability'] & MDB2_PORTABILITY_ERRORS) {
+ $ecode_map[1022] = MDB2_ERROR_CONSTRAINT;
+ $ecode_map[1048] = MDB2_ERROR_CONSTRAINT_NOT_NULL;
+ $ecode_map[1062] = MDB2_ERROR_CONSTRAINT;
+ } else {
+ // Doing this in case mode changes during runtime.
+ $ecode_map[1022] = MDB2_ERROR_ALREADY_EXISTS;
+ $ecode_map[1048] = MDB2_ERROR_CONSTRAINT;
+ $ecode_map[1062] = MDB2_ERROR_ALREADY_EXISTS;
+ }
+ if (isset($ecode_map[$native_code])) {
+ $error = $ecode_map[$native_code];
+ }
+ }
+ return array($error, $native_code, $native_msg);
+ }
+
+ // }}}
+ // {{{ escape()
+
+ /**
+ * Quotes a string so it can be safely used in a query. It will quote
+ * the text so it can safely be used within a query.
+ *
+ * @param string the input string to quote
+ * @param bool escape wildcards
+ *
+ * @return string quoted string
+ *
+ * @access public
+ */
+ function escape($text, $escape_wildcards = false)
+ {
+ if ($escape_wildcards) {
+ $text = $this->escapePattern($text);
+ }
+ $connection = $this->getConnection();
+ if (PEAR::isError($connection)) {
+ return $connection;
+ }
+ $text = @mysql_real_escape_string($text, $connection);
+ return $text;
+ }
+
+ // }}}
+ // {{{ beginTransaction()
+
+ /**
+ * Start a transaction or set a savepoint.
+ *
+ * @param string name of a savepoint to set
+ * @return mixed MDB2_OK on success, a MDB2 error on failure
+ *
+ * @access public
+ */
+ function beginTransaction($savepoint = null)
+ {
+ $this->debug('Starting transaction/savepoint', __FUNCTION__, array('is_manip' => true, 'savepoint' => $savepoint));
+ $this->_getServerCapabilities();
+ if (!is_null($savepoint)) {
+ if (!$this->supports('savepoints')) {
+ return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'savepoints are not supported', __FUNCTION__);
+ }
+ if (!$this->in_transaction) {
+ return $this->raiseError(MDB2_ERROR_INVALID, null, null,
+ 'savepoint cannot be released when changes are auto committed', __FUNCTION__);
+ }
+ $query = 'SAVEPOINT '.$savepoint;
+ return $this->_doQuery($query, true);
+ } elseif ($this->in_transaction) {
+ return MDB2_OK; //nothing to do
+ }
+ if (!$this->destructor_registered && $this->opened_persistent) {
+ $this->destructor_registered = true;
+ register_shutdown_function('MDB2_closeOpenTransactions');
+ }
+ $query = $this->start_transaction ? 'START TRANSACTION' : 'SET AUTOCOMMIT = 0';
+ $result =& $this->_doQuery($query, true);
+ if (PEAR::isError($result)) {
+ return $result;
+ }
+ $this->in_transaction = true;
+ return MDB2_OK;
+ }
+
+ // }}}
+ // {{{ commit()
+
+ /**
+ * Commit the database changes done during a transaction that is in
+ * progress or release a savepoint. This function may only be called when
+ * auto-committing is disabled, otherwise it will fail. Therefore, a new
+ * transaction is implicitly started after committing the pending changes.
+ *
+ * @param string name of a savepoint to release
+ * @return mixed MDB2_OK on success, a MDB2 error on failure
+ *
+ * @access public
+ */
+ function commit($savepoint = null)
+ {
+ $this->debug('Committing transaction/savepoint', __FUNCTION__, array('is_manip' => true, 'savepoint' => $savepoint));
+ if (!$this->in_transaction) {
+ return $this->raiseError(MDB2_ERROR_INVALID, null, null,
+ 'commit/release savepoint cannot be done changes are auto committed', __FUNCTION__);
+ }
+ if (!is_null($savepoint)) {
+ if (!$this->supports('savepoints')) {
+ return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'savepoints are not supported', __FUNCTION__);
+ }
+ $server_info = $this->getServerVersion();
+ if (version_compare($server_info['major'].'.'.$server_info['minor'].'.'.$server_info['patch'], '5.0.3', '<')) {
+ return MDB2_OK;
+ }
+ $query = 'RELEASE SAVEPOINT '.$savepoint;
+ return $this->_doQuery($query, true);
+ }
+
+ if (!$this->supports('transactions')) {
+ return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'transactions are not supported', __FUNCTION__);
+ }
+
+ $result =& $this->_doQuery('COMMIT', true);
+ if (PEAR::isError($result)) {
+ return $result;
+ }
+ if (!$this->start_transaction) {
+ $query = 'SET AUTOCOMMIT = 1';
+ $result =& $this->_doQuery($query, true);
+ if (PEAR::isError($result)) {
+ return $result;
+ }
+ }
+ $this->in_transaction = false;
+ return MDB2_OK;
+ }
+
+ // }}}
+ // {{{ rollback()
+
+ /**
+ * Cancel any database changes done during a transaction or since a specific
+ * savepoint that is in progress. This function may only be called when
+ * auto-committing is disabled, otherwise it will fail. Therefore, a new
+ * transaction is implicitly started after canceling the pending changes.
+ *
+ * @param string name of a savepoint to rollback to
+ * @return mixed MDB2_OK on success, a MDB2 error on failure
+ *
+ * @access public
+ */
+ function rollback($savepoint = null)
+ {
+ $this->debug('Rolling back transaction/savepoint', __FUNCTION__, array('is_manip' => true, 'savepoint' => $savepoint));
+ if (!$this->in_transaction) {
+ return $this->raiseError(MDB2_ERROR_INVALID, null, null,
+ 'rollback cannot be done changes are auto committed', __FUNCTION__);
+ }
+ if (!is_null($savepoint)) {
+ if (!$this->supports('savepoints')) {
+ return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'savepoints are not supported', __FUNCTION__);
+ }
+ $query = 'ROLLBACK TO SAVEPOINT '.$savepoint;
+ return $this->_doQuery($query, true);
+ }
+
+ $query = 'ROLLBACK';
+ $result =& $this->_doQuery($query, true);
+ if (PEAR::isError($result)) {
+ return $result;
+ }
+ if (!$this->start_transaction) {
+ $query = 'SET AUTOCOMMIT = 1';
+ $result =& $this->_doQuery($query, true);
+ if (PEAR::isError($result)) {
+ return $result;
+ }
+ }
+ $this->in_transaction = false;
+ return MDB2_OK;
+ }
+
+ // }}}
+ // {{{ function setTransactionIsolation()
+
+ /**
+ * Set the transacton isolation level.
+ *
+ * @param string standard isolation level
+ * READ UNCOMMITTED (allows dirty reads)
+ * READ COMMITTED (prevents dirty reads)
+ * REPEATABLE READ (prevents nonrepeatable reads)
+ * SERIALIZABLE (prevents phantom reads)
+ * @return mixed MDB2_OK on success, a MDB2 error on failure
+ *
+ * @access public
+ * @since 2.1.1
+ */
+ function setTransactionIsolation($isolation)
+ {
+ $this->debug('Setting transaction isolation level', __FUNCTION__, array('is_manip' => true));
+ if (!$this->supports('transactions')) {
+ return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'transactions are not supported', __FUNCTION__);
+ }
+ switch ($isolation) {
+ case 'READ UNCOMMITTED':
+ case 'READ COMMITTED':
+ case 'REPEATABLE READ':
+ case 'SERIALIZABLE':
+ break;
+ default:
+ return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'isolation level is not supported: '.$isolation, __FUNCTION__);
+ }
+
+ $query = "SET SESSION TRANSACTION ISOLATION LEVEL $isolation";
+ return $this->_doQuery($query, true);
+ }
+
+ // }}}
+ // {{{ _doConnect()
+
+ /**
+ * do the grunt work of the connect
+ *
+ * @return connection on success or MDB2 Error Object on failure
+ * @access protected
+ */
+ function _doConnect($username, $password, $persistent = false)
+ {
+ if (!PEAR::loadExtension($this->phptype)) {
+ return $this->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
+ 'extension '.$this->phptype.' is not compiled into PHP', __FUNCTION__);
+ }
+
+ $params = array();
+ if ($this->dsn['protocol'] && $this->dsn['protocol'] == 'unix') {
+ $params[0] = ':' . $this->dsn['socket'];
+ } else {
+ $params[0] = $this->dsn['hostspec'] ? $this->dsn['hostspec']
+ : 'localhost';
+ if ($this->dsn['port']) {
+ $params[0].= ':' . $this->dsn['port'];
+ }
+ }
+ $params[] = $username ? $username : null;
+ $params[] = $password ? $password : null;
+ if (!$persistent) {
+ if ($this->_isNewLinkSet()) {
+ $params[] = true;
+ } else {
+ $params[] = false;
+ }
+ }
+ if (version_compare(phpversion(), '4.3.0', '>=')) {
+ $params[] = isset($this->dsn['client_flags'])
+ ? $this->dsn['client_flags'] : null;
+ }
+ $connect_function = $persistent ? 'mysql_pconnect' : 'mysql_connect';
+
+ $connection = @call_user_func_array($connect_function, $params);
+ if (!$connection) {
+ if (($err = @mysql_error()) != '') {
+ return $this->raiseError(MDB2_ERROR_CONNECT_FAILED, null, null,
+ $err, __FUNCTION__);
+ } else {
+ return $this->raiseError(MDB2_ERROR_CONNECT_FAILED, null, null,
+ 'unable to establish a connection', __FUNCTION__);
+ }
+ }
+
+ if (!empty($this->dsn['charset'])) {
+ $result = $this->setCharset($this->dsn['charset'], $connection);
+ if (PEAR::isError($result)) {
+ $this->disconnect(false);
+ return $result;
+ }
+ }
+
+ return $connection;
+ }
+
+ // }}}
+ // {{{ connect()
+
+ /**
+ * Connect to the database
+ *
+ * @return MDB2_OK on success, MDB2 Error Object on failure
+ * @access public
+ */
+ function connect()
+ {
+ if (is_resource($this->connection)) {
+ //if (count(array_diff($this->connected_dsn, $this->dsn)) == 0
+ if (MDB2::areEquals($this->connected_dsn, $this->dsn)
+ && $this->opened_persistent == $this->options['persistent']
+ ) {
+ return MDB2_OK;
+ }
+ $this->disconnect(false);
+ }
+
+ $connection = $this->_doConnect(
+ $this->dsn['username'],
+ $this->dsn['password'],
+ $this->options['persistent']
+ );
+ if (PEAR::isError($connection)) {
+ return $connection;
+ }
+
+ $this->connection = $connection;
+ $this->connected_dsn = $this->dsn;
+ $this->connected_database_name = '';
+ $this->opened_persistent = $this->options['persistent'];
+ $this->dbsyntax = $this->dsn['dbsyntax'] ? $this->dsn['dbsyntax'] : $this->phptype;
+
+ if ($this->database_name) {
+ if ($this->database_name != $this->connected_database_name) {
+ if (!@mysql_select_db($this->database_name, $connection)) {
+ $err = $this->raiseError(null, null, null,
+ 'Could not select the database: '.$this->database_name, __FUNCTION__);
+ return $err;
+ }
+ $this->connected_database_name = $this->database_name;
+ }
+ }
+
+ $this->_getServerCapabilities();
+
+ return MDB2_OK;
+ }
+
+ // }}}
+ // {{{ setCharset()
+
+ /**
+ * Set the charset on the current connection
+ *
+ * @param string charset (or array(charset, collation))
+ * @param resource connection handle
+ *
+ * @return true on success, MDB2 Error Object on failure
+ */
+ function setCharset($charset, $connection = null)
+ {
+ if (is_null($connection)) {
+ $connection = $this->getConnection();
+ if (PEAR::isError($connection)) {
+ return $connection;
+ }
+ }
+ $collation = null;
+ if (is_array($charset) && 2 == count($charset)) {
+ $collation = array_pop($charset);
+ $charset = array_pop($charset);
+ }
+ $client_info = mysql_get_client_info();
+ if (function_exists('mysql_set_charset') && version_compare($client_info, '5.0.6')) {
+ if (!$result = mysql_set_charset($charset, $connection)) {
+ $err =& $this->raiseError(null, null, null,
+ 'Could not set client character set', __FUNCTION__);
+ return $err;
+ }
+ return $result;
+ }
+ $query = "SET NAMES '".mysql_real_escape_string($charset, $connection)."'";
+ if (!is_null($collation)) {
+ $query .= " COLLATE '".mysqli_real_escape_string($connection, $collation)."'";
+ }
+ return $this->_doQuery($query, true, $connection);
+ }
+
+ // }}}
+ // {{{ databaseExists()
+
+ /**
+ * check if given database name is exists?
+ *
+ * @param string $name name of the database that should be checked
+ *
+ * @return mixed true/false on success, a MDB2 error on failure
+ * @access public
+ */
+ function databaseExists($name)
+ {
+ $connection = $this->_doConnect($this->dsn['username'],
+ $this->dsn['password'],
+ $this->options['persistent']);
+ if (PEAR::isError($connection)) {
+ return $connection;
+ }
+
+ $result = @mysql_select_db($name, $connection);
+ @mysql_close($connection);
+
+ return $result;
+ }
+
+ // }}}
+ // {{{ disconnect()
+
+ /**
+ * Log out and disconnect from the database.
+ *
+ * @param boolean $force if the disconnect should be forced even if the
+ * connection is opened persistently
+ * @return mixed true on success, false if not connected and error
+ * object on error
+ * @access public
+ */
+ function disconnect($force = true)
+ {
+ if (is_resource($this->connection)) {
+ if ($this->in_transaction) {
+ $dsn = $this->dsn;
+ $database_name = $this->database_name;
+ $persistent = $this->options['persistent'];
+ $this->dsn = $this->connected_dsn;
+ $this->database_name = $this->connected_database_name;
+ $this->options['persistent'] = $this->opened_persistent;
+ $this->rollback();
+ $this->dsn = $dsn;
+ $this->database_name = $database_name;
+ $this->options['persistent'] = $persistent;
+ }
+
+ if (!$this->opened_persistent || $force) {
+ $ok = @mysql_close($this->connection);
+ if (!$ok) {
+ return $this->raiseError(MDB2_ERROR_DISCONNECT_FAILED,
+ null, null, null, __FUNCTION__);
+ }
+ }
+ } else {
+ return false;
+ }
+ return parent::disconnect($force);
+ }
+
+ // }}}
+ // {{{ standaloneQuery()
+
+ /**
+ * execute a query as DBA
+ *
+ * @param string $query the SQL query
+ * @param mixed $types array that contains the types of the columns in
+ * the result set
+ * @param boolean $is_manip if the query is a manipulation query
+ * @return mixed MDB2_OK on success, a MDB2 error on failure
+ * @access public
+ */
+ function &standaloneQuery($query, $types = null, $is_manip = false)
+ {
+ $user = $this->options['DBA_username']? $this->options['DBA_username'] : $this->dsn['username'];
+ $pass = $this->options['DBA_password']? $this->options['DBA_password'] : $this->dsn['password'];
+ $connection = $this->_doConnect($user, $pass, $this->options['persistent']);
+ if (PEAR::isError($connection)) {
+ return $connection;
+ }
+
+ $offset = $this->offset;
+ $limit = $this->limit;
+ $this->offset = $this->limit = 0;
+ $query = $this->_modifyQuery($query, $is_manip, $limit, $offset);
+
+ $result =& $this->_doQuery($query, $is_manip, $connection, $this->database_name);
+ if (!PEAR::isError($result)) {
+ $result = $this->_affectedRows($connection, $result);
+ }
+
+ @mysql_close($connection);
+ return $result;
+ }
+
+ // }}}
+ // {{{ _doQuery()
+
+ /**
+ * Execute a query
+ * @param string $query query
+ * @param boolean $is_manip if the query is a manipulation query
+ * @param resource $connection
+ * @param string $database_name
+ * @return result or error object
+ * @access protected
+ */
+ function &_doQuery($query, $is_manip = false, $connection = null, $database_name = null)
+ {
+ $this->last_query = $query;
+ $result = $this->debug($query, 'query', array('is_manip' => $is_manip, 'when' => 'pre'));
+ if ($result) {
+ if (PEAR::isError($result)) {
+ return $result;
+ }
+ $query = $result;
+ }
+ if ($this->options['disable_query']) {
+ $result = $is_manip ? 0 : null;
+ return $result;
+ }
+
+ if (is_null($connection)) {
+ $connection = $this->getConnection();
+ if (PEAR::isError($connection)) {
+ return $connection;
+ }
+ }
+ if (is_null($database_name)) {
+ $database_name = $this->database_name;
+ }
+
+ if ($database_name) {
+ if ($database_name != $this->connected_database_name) {
+ if (!@mysql_select_db($database_name, $connection)) {
+ $err = $this->raiseError(null, null, null,
+ 'Could not select the database: '.$database_name, __FUNCTION__);
+ return $err;
+ }
+ $this->connected_database_name = $database_name;
+ }
+ }
+
+ $function = $this->options['result_buffering']
+ ? 'mysql_query' : 'mysql_unbuffered_query';
+ $result = @$function($query, $connection);
+ if (!$result) {
+ $err =& $this->raiseError(null, null, null,
+ 'Could not execute statement', __FUNCTION__);
+ return $err;
+ }
+
+ $this->debug($query, 'query', array('is_manip' => $is_manip, 'when' => 'post', 'result' => $result));
+ return $result;
+ }
+
+ // }}}
+ // {{{ _affectedRows()
+
+ /**
+ * Returns the number of rows affected
+ *
+ * @param resource $result
+ * @param resource $connection
+ * @return mixed MDB2 Error Object or the number of rows affected
+ * @access private
+ */
+ function _affectedRows($connection, $result = null)
+ {
+ if (is_null($connection)) {
+ $connection = $this->getConnection();
+ if (PEAR::isError($connection)) {
+ return $connection;
+ }
+ }
+ return @mysql_affected_rows($connection);
+ }
+
+ // }}}
+ // {{{ _modifyQuery()
+
+ /**
+ * Changes a query string for various DBMS specific reasons
+ *
+ * @param string $query query to modify
+ * @param boolean $is_manip if it is a DML query
+ * @param integer $limit limit the number of rows
+ * @param integer $offset start reading from given offset
+ * @return string modified query
+ * @access protected
+ */
+ function _modifyQuery($query, $is_manip, $limit, $offset)
+ {
+ if ($this->options['portability'] & MDB2_PORTABILITY_DELETE_COUNT) {
+ // "DELETE FROM table" gives 0 affected rows in MySQL.
+ // This little hack lets you know how many rows were deleted.
+ if (preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $query)) {
+ $query = preg_replace('/^\s*DELETE\s+FROM\s+(\S+)\s*$/',
+ 'DELETE FROM \1 WHERE 1=1', $query);
+ }
+ }
+ if ($limit > 0
+ && !preg_match('/LIMIT\s*\d(?:\s*(?:,|OFFSET)\s*\d+)?(?:[^\)]*)?$/i', $query)
+ ) {
+ $query = rtrim($query);
+ if (substr($query, -1) == ';') {
+ $query = substr($query, 0, -1);
+ }
+
+ // LIMIT doesn't always come last in the query
+ // @see http://dev.mysql.com/doc/refman/5.0/en/select.html
+ $after = '';
+ if (preg_match('/(\s+INTO\s+(?:OUT|DUMP)FILE\s.*)$/ims', $query, $matches)) {
+ $after = $matches[0];
+ $query = preg_replace('/(\s+INTO\s+(?:OUT|DUMP)FILE\s.*)$/ims', '', $query);
+ } elseif (preg_match('/(\s+FOR\s+UPDATE\s*)$/i', $query, $matches)) {
+ $after = $matches[0];
+ $query = preg_replace('/(\s+FOR\s+UPDATE\s*)$/im', '', $query);
+ } elseif (preg_match('/(\s+LOCK\s+IN\s+SHARE\s+MODE\s*)$/im', $query, $matches)) {
+ $after = $matches[0];
+ $query = preg_replace('/(\s+LOCK\s+IN\s+SHARE\s+MODE\s*)$/im', '', $query);
+ }
+
+ if ($is_manip) {
+ return $query . " LIMIT $limit" . $after;
+ } else {
+ return $query . " LIMIT $offset, $limit" . $after;
+ }
+ }
+ return $query;
+ }
+
+ // }}}
+ // {{{ getServerVersion()
+
+ /**
+ * return version information about the server
+ *
+ * @param bool $native determines if the raw version string should be returned
+ * @return mixed array/string with version information or MDB2 error object
+ * @access public
+ */
+ function getServerVersion($native = false)
+ {
+ $connection = $this->getConnection();
+ if (PEAR::isError($connection)) {
+ return $connection;
+ }
+ if ($this->connected_server_info) {
+ $server_info = $this->connected_server_info;
+ } else {
+ $server_info = @mysql_get_server_info($connection);
+ }
+ if (!$server_info) {
+ return $this->raiseError(null, null, null,
+ 'Could not get server information', __FUNCTION__);
+ }
+ // cache server_info
+ $this->connected_server_info = $server_info;
+ if (!$native) {
+ $tmp = explode('.', $server_info, 3);
+ if (isset($tmp[2]) && strpos($tmp[2], '-')) {
+ $tmp2 = explode('-', @$tmp[2], 2);
+ } else {
+ $tmp2[0] = isset($tmp[2]) ? $tmp[2] : null;
+ $tmp2[1] = null;
+ }
+ $server_info = array(
+ 'major' => isset($tmp[0]) ? $tmp[0] : null,
+ 'minor' => isset($tmp[1]) ? $tmp[1] : null,
+ 'patch' => $tmp2[0],
+ 'extra' => $tmp2[1],
+ 'native' => $server_info,
+ );
+ }
+ return $server_info;
+ }
+
+ // }}}
+ // {{{ _getServerCapabilities()
+
+ /**
+ * Fetch some information about the server capabilities
+ * (transactions, subselects, prepared statements, etc).
+ *
+ * @access private
+ */
+ function _getServerCapabilities()
+ {
+ if (!$this->server_capabilities_checked) {
+ $this->server_capabilities_checked = true;
+
+ //set defaults
+ $this->supported['sub_selects'] = 'emulated';
+ $this->supported['prepared_statements'] = 'emulated';
+ $this->supported['triggers'] = false;
+ $this->start_transaction = false;
+ $this->varchar_max_length = 255;
+
+ $server_info = $this->getServerVersion();
+ if (is_array($server_info)) {
+ $server_version = $server_info['major'].'.'.$server_info['minor'].'.'.$server_info['patch'];
+
+ if (!version_compare($server_version, '4.1.0', '<')) {
+ $this->supported['sub_selects'] = true;
+ $this->supported['prepared_statements'] = true;
+ }
+
+ // SAVEPOINTs were introduced in MySQL 4.0.14 and 4.1.1 (InnoDB)
+ if (version_compare($server_version, '4.1.0', '>=')) {
+ if (version_compare($server_version, '4.1.1', '<')) {
+ $this->supported['savepoints'] = false;
+ }
+ } elseif (version_compare($server_version, '4.0.14', '<')) {
+ $this->supported['savepoints'] = false;
+ }
+
+ if (!version_compare($server_version, '4.0.11', '<')) {
+ $this->start_transaction = true;
+ }
+
+ if (!version_compare($server_version, '5.0.3', '<')) {
+ $this->varchar_max_length = 65532;
+ }
+
+ if (!version_compare($server_version, '5.0.2', '<')) {
+ $this->supported['triggers'] = true;
+ }
+ }
+ }
+ }
+
+ // }}}
+ // {{{ function _skipUserDefinedVariable($query, $position)
+
+ /**
+ * Utility method, used by prepare() to avoid misinterpreting MySQL user
+ * defined variables (SELECT @x:=5) for placeholders.
+ * Check if the placeholder is a false positive, i.e. if it is an user defined
+ * variable instead. If so, skip it and advance the position, otherwise
+ * return the current position, which is valid
+ *
+ * @param string $query
+ * @param integer $position current string cursor position
+ * @return integer $new_position
+ * @access protected
+ */
+ function _skipUserDefinedVariable($query, $position)
+ {
+ $found = strpos(strrev(substr($query, 0, $position)), '@');
+ if ($found === false) {
+ return $position;
+ }
+ $pos = strlen($query) - strlen(substr($query, $position)) - $found - 1;
+ $substring = substr($query, $pos, $position - $pos + 2);
+ if (preg_match('/^@\w+\s*:=$/', $substring)) {
+ return $position + 1; //found an user defined variable: skip it
+ }
+ return $position;
+ }
+
+ // }}}
+ // {{{ prepare()
+
+ /**
+ * Prepares a query for multiple execution with execute().
+ * With some database backends, this is emulated.
+ * prepare() requires a generic query as string like
+ * 'INSERT INTO numbers VALUES(?,?)' or
+ * 'INSERT INTO numbers VALUES(:foo,:bar)'.
+ * The ? and :name and are placeholders which can be set using
+ * bindParam() and the query can be sent off using the execute() method.
+ * The allowed format for :name can be set with the 'bindname_format' option.
+ *
+ * @param string $query the query to prepare
+ * @param mixed $types array that contains the types of the placeholders
+ * @param mixed $result_types array that contains the types of the columns in
+ * the result set or MDB2_PREPARE_RESULT, if set to
+ * MDB2_PREPARE_MANIP the query is handled as a manipulation query
+ * @param mixed $lobs key (field) value (parameter) pair for all lob placeholders
+ * @return mixed resource handle for the prepared query on success, a MDB2
+ * error on failure
+ * @access public
+ * @see bindParam, execute
+ */
+ function &prepare($query, $types = null, $result_types = null, $lobs = array())
+ {
+ if ($this->options['emulate_prepared']
+ || $this->supported['prepared_statements'] !== true
+ ) {
+ $obj =& parent::prepare($query, $types, $result_types, $lobs);
+ return $obj;
+ }
+ $is_manip = ($result_types === MDB2_PREPARE_MANIP);
+ $offset = $this->offset;
+ $limit = $this->limit;
+ $this->offset = $this->limit = 0;
+ $query = $this->_modifyQuery($query, $is_manip, $limit, $offset);
+ $result = $this->debug($query, __FUNCTION__, array('is_manip' => $is_manip, 'when' => 'pre'));
+ if ($result) {
+ if (PEAR::isError($result)) {
+ return $result;
+ }
+ $query = $result;
+ }
+ $placeholder_type_guess = $placeholder_type = null;
+ $question = '?';
+ $colon = ':';
+ $positions = array();
+ $position = 0;
+ while ($position < strlen($query)) {
+ $q_position = strpos($query, $question, $position);
+ $c_position = strpos($query, $colon, $position);
+ if ($q_position && $c_position) {
+ $p_position = min($q_position, $c_position);
+ } elseif ($q_position) {
+ $p_position = $q_position;
+ } elseif ($c_position) {
+ $p_position = $c_position;
+ } else {
+ break;
+ }
+ if (is_null($placeholder_type)) {
+ $placeholder_type_guess = $query[$p_position];
+ }
+
+ $new_pos = $this->_skipDelimitedStrings($query, $position, $p_position);
+ if (PEAR::isError($new_pos)) {
+ return $new_pos;
+ }
+ if ($new_pos != $position) {
+ $position = $new_pos;
+ continue; //evaluate again starting from the new position
+ }
+
+ //make sure this is not part of an user defined variable
+ $new_pos = $this->_skipUserDefinedVariable($query, $position);
+ if ($new_pos != $position) {
+ $position = $new_pos;
+ continue; //evaluate again starting from the new position
+ }
+
+ if ($query[$position] == $placeholder_type_guess) {
+ if (is_null($placeholder_type)) {
+ $placeholder_type = $query[$p_position];
+ $question = $colon = $placeholder_type;
+ }
+ if ($placeholder_type == ':') {
+ $regexp = '/^.{'.($position+1).'}('.$this->options['bindname_format'].').*$/s';
+ $parameter = preg_replace($regexp, '\\1', $query);
+ if ($parameter === '') {
+ $err =& $this->raiseError(MDB2_ERROR_SYNTAX, null, null,
+ 'named parameter name must match "bindname_format" option', __FUNCTION__);
+ return $err;
+ }
+ $positions[$p_position] = $parameter;
+ $query = substr_replace($query, '?', $position, strlen($parameter)+1);
+ } else {
+ $positions[$p_position] = count($positions);
+ }
+ $position = $p_position + 1;
+ } else {
+ $position = $p_position;
+ }
+ }
+ $connection = $this->getConnection();
+ if (PEAR::isError($connection)) {
+ return $connection;
+ }
+ static $prep_statement_counter = 1;
+ $statement_name = sprintf($this->options['statement_format'], $this->phptype, $prep_statement_counter++ . sha1(microtime() + mt_rand()));
+ $statement_name = substr(strtolower($statement_name), 0, $this->options['max_identifiers_length']);
+ $query = "PREPARE $statement_name FROM ".$this->quote($query, 'text');
+ $statement =& $this->_doQuery($query, true, $connection);
+ if (PEAR::isError($statement)) {
+ return $statement;
+ }
+
+ $class_name = 'MDB2_Statement_'.$this->phptype;
+ $obj = new $class_name($this, $statement_name, $positions, $query, $types, $result_types, $is_manip, $limit, $offset);
+ $this->debug($query, __FUNCTION__, array('is_manip' => $is_manip, 'when' => 'post', 'result' => $obj));
+ return $obj;
+ }
+
+ // }}}
+ // {{{ replace()
+
+ /**
+ * Execute a SQL REPLACE query. A REPLACE query is identical to a INSERT
+ * query, except that if there is already a row in the table with the same
+ * key field values, the old row is deleted before the new row is inserted.
+ *
+ * The REPLACE type of query does not make part of the SQL standards. Since
+ * practically only MySQL implements it natively, this type of query is
+ * emulated through this method for other DBMS using standard types of
+ * queries inside a transaction to assure the atomicity of the operation.
+ *
+ * @access public
+ *
+ * @param string $table name of the table on which the REPLACE query will
+ * be executed.
+ * @param array $fields associative array that describes the fields and the
+ * values that will be inserted or updated in the specified table. The
+ * indexes of the array are the names of all the fields of the table. The
+ * values of the array are also associative arrays that describe the
+ * values and other properties of the table fields.
+ *
+ * Here follows a list of field properties that need to be specified:
+ *
+ * value:
+ * Value to be assigned to the specified field. This value may be
+ * of specified in database independent type format as this
+ * function can perform the necessary datatype conversions.
+ *
+ * Default:
+ * this property is required unless the Null property
+ * is set to 1.
+ *
+ * type
+ * Name of the type of the field. Currently, all types Metabase
+ * are supported except for clob and blob.
+ *
+ * Default: no type conversion
+ *
+ * null
+ * Boolean property that indicates that the value for this field
+ * should be set to null.
+ *
+ * The default value for fields missing in INSERT queries may be
+ * specified the definition of a table. Often, the default value
+ * is already null, but since the REPLACE may be emulated using
+ * an UPDATE query, make sure that all fields of the table are
+ * listed in this function argument array.
+ *
+ * Default: 0
+ *
+ * key
+ * Boolean property that indicates that this field should be
+ * handled as a primary key or at least as part of the compound
+ * unique index of the table that will determine the row that will
+ * updated if it exists or inserted a new row otherwise.
+ *
+ * This function will fail if no key field is specified or if the
+ * value of a key field is set to null because fields that are
+ * part of unique index they may not be null.
+ *
+ * Default: 0
+ *
+ * @see http://dev.mysql.com/doc/refman/5.0/en/replace.html
+ * @return mixed MDB2_OK on success, a MDB2 error on failure
+ */
+ function replace($table, $fields)
+ {
+ $count = count($fields);
+ $query = $values = '';
+ $keys = $colnum = 0;
+ for (reset($fields); $colnum < $count; next($fields), $colnum++) {
+ $name = key($fields);
+ if ($colnum > 0) {
+ $query .= ',';
+ $values.= ',';
+ }
+ $query.= $this->quoteIdentifier($name, true);
+ if (isset($fields[$name]['null']) && $fields[$name]['null']) {
+ $value = 'NULL';
+ } else {
+ $type = isset($fields[$name]['type']) ? $fields[$name]['type'] : null;
+ $value = $this->quote($fields[$name]['value'], $type);
+ if (PEAR::isError($value)) {
+ return $value;
+ }
+ }
+ $values.= $value;
+ if (isset($fields[$name]['key']) && $fields[$name]['key']) {
+ if ($value === 'NULL') {
+ return $this->raiseError(MDB2_ERROR_CANNOT_REPLACE, null, null,
+ 'key value '.$name.' may not be NULL', __FUNCTION__);
+ }
+ $keys++;
+ }
+ }
+ if ($keys == 0) {
+ return $this->raiseError(MDB2_ERROR_CANNOT_REPLACE, null, null,
+ 'not specified which fields are keys', __FUNCTION__);
+ }
+
+ $connection = $this->getConnection();
+ if (PEAR::isError($connection)) {
+ return $connection;
+ }
+
+ $table = $this->quoteIdentifier($table, true);
+ $query = "REPLACE INTO $table ($query) VALUES ($values)";
+ $result =& $this->_doQuery($query, true, $connection);
+ if (PEAR::isError($result)) {
+ return $result;
+ }
+ return $this->_affectedRows($connection, $result);
+ }
+
+ // }}}
+ // {{{ nextID()
+
+ /**
+ * Returns the next free id of a sequence
+ *
+ * @param string $seq_name name of the sequence
+ * @param boolean $ondemand when true the sequence is
+ * automatic created, if it
+ * not exists
+ *
+ * @return mixed MDB2 Error Object or id
+ * @access public
+ */
+ function nextID($seq_name, $ondemand = true)
+ {
+ $sequence_name = $this->quoteIdentifier($this->getSequenceName($seq_name), true);
+ $seqcol_name = $this->quoteIdentifier($this->options['seqcol_name'], true);
+ $query = "INSERT INTO $sequence_name ($seqcol_name) VALUES (NULL)";
+ $this->pushErrorHandling(PEAR_ERROR_RETURN);
+ $this->expectError(MDB2_ERROR_NOSUCHTABLE);
+ $result =& $this->_doQuery($query, true);
+ $this->popExpect();
+ $this->popErrorHandling();
+ if (PEAR::isError($result)) {
+ if ($ondemand && $result->getCode() == MDB2_ERROR_NOSUCHTABLE) {
+ $this->loadModule('Manager', null, true);
+ $result = $this->manager->createSequence($seq_name);
+ if (PEAR::isError($result)) {
+ return $this->raiseError($result, null, null,
+ 'on demand sequence '.$seq_name.' could not be created', __FUNCTION__);
+ } else {
+ return $this->nextID($seq_name, false);
+ }
+ }
+ return $result;
+ }
+ $value = $this->lastInsertID();
+ if (is_numeric($value)) {
+ $query = "DELETE FROM $sequence_name WHERE $seqcol_name < $value";
+ $result =& $this->_doQuery($query, true);
+ if (PEAR::isError($result)) {
+ $this->warnings[] = 'nextID: could not delete previous sequence table values from '.$seq_name;
+ }
+ }
+ return $value;
+ }
+
+ // }}}
+ // {{{ lastInsertID()
+
+ /**
+ * Returns the autoincrement ID if supported or $id or fetches the current
+ * ID in a sequence called: $table.(empty($field) ? '' : '_'.$field)
+ *
+ * @param string $table name of the table into which a new row was inserted
+ * @param string $field name of the field into which a new row was inserted
+ * @return mixed MDB2 Error Object or id
+ * @access public
+ */
+ function lastInsertID($table = null, $field = null)
+ {
+ // not using mysql_insert_id() due to http://pear.php.net/bugs/bug.php?id=8051
+ return $this->queryOne('SELECT LAST_INSERT_ID()', 'integer');
+ }
+
+ // }}}
+ // {{{ currID()
+
+ /**
+ * Returns the current id of a sequence
+ *
+ * @param string $seq_name name of the sequence
+ * @return mixed MDB2 Error Object or id
+ * @access public
+ */
+ function currID($seq_name)
+ {
+ $sequence_name = $this->quoteIdentifier($this->getSequenceName($seq_name), true);
+ $seqcol_name = $this->quoteIdentifier($this->options['seqcol_name'], true);
+ $query = "SELECT MAX($seqcol_name) FROM $sequence_name";
+ return $this->queryOne($query, 'integer');
+ }
+}
+
+/**
+ * MDB2 MySQL result driver
+ *
+ * @package MDB2
+ * @category Database
+ * @author Lukas Smith <smith@pooteeweet.org>
+ */
+class MDB2_Result_mysql extends MDB2_Result_Common
+{
+ // }}}
+ // {{{ fetchRow()
+
+ /**
+ * Fetch a row and insert the data into an existing array.
+ *
+ * @param int $fetchmode how the array data should be indexed
+ * @param int $rownum number of the row where the data can be found
+ * @return int data array on success, a MDB2 error on failure
+ * @access public
+ */
+ function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT, $rownum = null)
+ {
+ if (!is_null($rownum)) {
+ $seek = $this->seek($rownum);
+ if (PEAR::isError($seek)) {
+ return $seek;
+ }
+ }
+ if ($fetchmode == MDB2_FETCHMODE_DEFAULT) {
+ $fetchmode = $this->db->fetchmode;
+ }
+ if ($fetchmode & MDB2_FETCHMODE_ASSOC) {
+ $row = @mysql_fetch_assoc($this->result);
+ if (is_array($row)
+ && $this->db->options['portability'] & MDB2_PORTABILITY_FIX_CASE
+ ) {
+ $row = array_change_key_case($row, $this->db->options['field_case']);
+ }
+ } else {
+ $row = @mysql_fetch_row($this->result);
+ }
+
+ if (!$row) {
+ if ($this->result === false) {
+ $err =& $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
+ 'resultset has already been freed', __FUNCTION__);
+ return $err;
+ }
+ $null = null;
+ return $null;
+ }
+ $mode = $this->db->options['portability'] & MDB2_PORTABILITY_EMPTY_TO_NULL;
+ $rtrim = false;
+ if ($this->db->options['portability'] & MDB2_PORTABILITY_RTRIM) {
+ if (empty($this->types)) {
+ $mode += MDB2_PORTABILITY_RTRIM;
+ } else {
+ $rtrim = true;
+ }
+ }
+ if ($mode) {
+ $this->db->_fixResultArrayValues($row, $mode);
+ }
+ if (!empty($this->types)) {
+ $row = $this->db->datatype->convertResultRow($this->types, $row, $rtrim);
+ }
+ if (!empty($this->values)) {
+ $this->_assignBindColumns($row);
+ }
+ if ($fetchmode === MDB2_FETCHMODE_OBJECT) {
+ $object_class = $this->db->options['fetch_class'];
+ if ($object_class == 'stdClass') {
+ $row = (object) $row;
+ } else {
+ $row = &new $object_class($row);
+ }
+ }
+ ++$this->rownum;
+ return $row;
+ }
+
+ // }}}
+ // {{{ _getColumnNames()
+
+ /**
+ * Retrieve the names of columns returned by the DBMS in a query result.
+ *
+ * @return mixed Array variable that holds the names of columns as keys
+ * or an MDB2 error on failure.
+ * Some DBMS may not return any columns when the result set
+ * does not contain any rows.
+ * @access private
+ */
+ function _getColumnNames()
+ {
+ $columns = array();
+ $numcols = $this->numCols();
+ if (PEAR::isError($numcols)) {
+ return $numcols;
+ }
+ for ($column = 0; $column < $numcols; $column++) {
+ $column_name = @mysql_field_name($this->result, $column);
+ $columns[$column_name] = $column;
+ }
+ if ($this->db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
+ $columns = array_change_key_case($columns, $this->db->options['field_case']);
+ }
+ return $columns;
+ }
+
+ // }}}
+ // {{{ numCols()
+
+ /**
+ * Count the number of columns returned by the DBMS in a query result.
+ *
+ * @return mixed integer value with the number of columns, a MDB2 error
+ * on failure
+ * @access public
+ */
+ function numCols()
+ {
+ $cols = @mysql_num_fields($this->result);
+ if (is_null($cols)) {
+ if ($this->result === false) {
+ return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
+ 'resultset has already been freed', __FUNCTION__);
+ } elseif (is_null($this->result)) {
+ return count($this->types);
+ }
+ return $this->db->raiseError(null, null, null,
+ 'Could not get column count', __FUNCTION__);
+ }
+ return $cols;
+ }
+
+ // }}}
+ // {{{ free()
+
+ /**
+ * Free the internal resources associated with result.
+ *
+ * @return boolean true on success, false if result is invalid
+ * @access public
+ */
+ function free()
+ {
+ if (is_resource($this->result) && $this->db->connection) {
+ $free = @mysql_free_result($this->result);
+ if ($free === false) {
+ return $this->db->raiseError(null, null, null,
+ 'Could not free result', __FUNCTION__);
+ }
+ }
+ $this->result = false;
+ return MDB2_OK;
+ }
+}
+
+/**
+ * MDB2 MySQL buffered result driver
+ *
+ * @package MDB2
+ * @category Database
+ * @author Lukas Smith <smith@pooteeweet.org>
+ */
+class MDB2_BufferedResult_mysql extends MDB2_Result_mysql
+{
+ // }}}
+ // {{{ seek()
+
+ /**
+ * Seek to a specific row in a result set
+ *
+ * @param int $rownum number of the row where the data can be found
+ * @return mixed MDB2_OK on success, a MDB2 error on failure
+ * @access public
+ */
+ function seek($rownum = 0)
+ {
+ if ($this->rownum != ($rownum - 1) && !@mysql_data_seek($this->result, $rownum)) {
+ if ($this->result === false) {
+ return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
+ 'resultset has already been freed', __FUNCTION__);
+ } elseif (is_null($this->result)) {
+ return MDB2_OK;
+ }
+ return $this->db->raiseError(MDB2_ERROR_INVALID, null, null,
+ 'tried to seek to an invalid row number ('.$rownum.')', __FUNCTION__);
+ }
+ $this->rownum = $rownum - 1;
+ return MDB2_OK;
+ }
+
+ // }}}
+ // {{{ valid()
+
+ /**
+ * Check if the end of the result set has been reached
+ *
+ * @return mixed true or false on sucess, a MDB2 error on failure
+ * @access public
+ */
+ function valid()
+ {
+ $numrows = $this->numRows();
+ if (PEAR::isError($numrows)) {
+ return $numrows;
+ }
+ return $this->rownum < ($numrows - 1);
+ }
+
+ // }}}
+ // {{{ numRows()
+
+ /**
+ * Returns the number of rows in a result object
+ *
+ * @return mixed MDB2 Error Object or the number of rows
+ * @access public
+ */
+ function numRows()
+ {
+ $rows = @mysql_num_rows($this->result);
+ if (false === $rows) {
+ if (false === $this->result) {
+ return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
+ 'resultset has already been freed', __FUNCTION__);
+ } elseif (is_null($this->result)) {
+ return 0;
+ }
+ return $this->db->raiseError(null, null, null,
+ 'Could not get row count', __FUNCTION__);
+ }
+ return $rows;
+ }
+}
+
+/**
+ * MDB2 MySQL statement driver
+ *
+ * @package MDB2
+ * @category Database
+ * @author Lukas Smith <smith@pooteeweet.org>
+ */
+class MDB2_Statement_mysql extends MDB2_Statement_Common
+{
+ // {{{ _execute()
+
+ /**
+ * Execute a prepared query statement helper method.
+ *
+ * @param mixed $result_class string which specifies which result class to use
+ * @param mixed $result_wrap_class string which specifies which class to wrap results in
+ *
+ * @return mixed MDB2_Result or integer (affected rows) on success,
+ * a MDB2 error on failure
+ * @access private
+ */
+ function &_execute($result_class = true, $result_wrap_class = false)
+ {
+ if (is_null($this->statement)) {
+ $result =& parent::_execute($result_class, $result_wrap_class);
+ return $result;
+ }
+ $this->db->last_query = $this->query;
+ $this->db->debug($this->query, 'execute', array('is_manip' => $this->is_manip, 'when' => 'pre', 'parameters' => $this->values));
+ if ($this->db->getOption('disable_query')) {
+ $result = $this->is_manip ? 0 : null;
+ return $result;
+ }
+
+ $connection = $this->db->getConnection();
+ if (PEAR::isError($connection)) {
+ return $connection;
+ }
+
+ $query = 'EXECUTE '.$this->statement;
+ if (!empty($this->positions)) {
+ $parameters = array();
+ foreach ($this->positions as $parameter) {
+ if (!array_key_exists($parameter, $this->values)) {
+ return $this->db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
+ 'Unable to bind to missing placeholder: '.$parameter, __FUNCTION__);
+ }
+ $value = $this->values[$parameter];
+ $type = array_key_exists($parameter, $this->types) ? $this->types[$parameter] : null;
+ if (is_resource($value) || $type == 'clob' || $type == 'blob' && $this->db->options['lob_allow_url_include']) {
+ if (!is_resource($value) && preg_match('/^(\w+:\/\/)(.*)$/', $value, $match)) {
+ if ($match[1] == 'file://') {
+ $value = $match[2];
+ }
+ $value = @fopen($value, 'r');
+ $close = true;
+ }
+ if (is_resource($value)) {
+ $data = '';
+ while (!@feof($value)) {
+ $data.= @fread($value, $this->db->options['lob_buffer_length']);
+ }
+ if ($close) {
+ @fclose($value);
+ }
+ $value = $data;
+ }
+ }
+ $quoted = $this->db->quote($value, $type);
+ if (PEAR::isError($quoted)) {
+ return $quoted;
+ }
+ $param_query = 'SET @'.$parameter.' = '.$quoted;
+ $result = $this->db->_doQuery($param_query, true, $connection);
+ if (PEAR::isError($result)) {
+ return $result;
+ }
+ }
+ $query.= ' USING @'.implode(', @', array_values($this->positions));
+ }
+
+ $result = $this->db->_doQuery($query, $this->is_manip, $connection);
+ if (PEAR::isError($result)) {
+ return $result;
+ }
+
+ if ($this->is_manip) {
+ $affected_rows = $this->db->_affectedRows($connection, $result);
+ return $affected_rows;
+ }
+
+ $result =& $this->db->_wrapResult($result, $this->result_types,
+ $result_class, $result_wrap_class, $this->limit, $this->offset);
+ $this->db->debug($this->query, 'execute', array('is_manip' => $this->is_manip, 'when' => 'post', 'result' => $result));
+ return $result;
+ }
+
+ // }}}
+ // {{{ free()
+
+ /**
+ * Release resources allocated for the specified prepared query.
+ *
+ * @return mixed MDB2_OK on success, a MDB2 error on failure
+ * @access public
+ */
+ function free()
+ {
+ if (is_null($this->positions)) {
+ return $this->db->raiseError(MDB2_ERROR, null, null,
+ 'Prepared statement has already been freed', __FUNCTION__);
+ }
+ $result = MDB2_OK;
+
+ if (!is_null($this->statement)) {
+ $connection = $this->db->getConnection();
+ if (PEAR::isError($connection)) {
+ return $connection;
+ }
+ $query = 'DEALLOCATE PREPARE '.$this->statement;
+ $result = $this->db->_doQuery($query, true, $connection);
+ }
+
+ parent::free();
+ return $result;
+ }
+}
?>
\ No newline at end of file