- fixed #1485032 and updated MDB2 package+drivers

release-0.6
alecpl 17 years ago
parent bbf15d8115
commit d1403fd726

@ -1,6 +1,13 @@
CHANGELOG RoundCube Webmail
---------------------------
2008/05/02 (alec)
----------
- Updated MDB2 package to version 2.5.0b1
- Updated MDB2 pgsql, mysql, mysqli, sqlite drivers to version 1.5.0b1
- Updated MDB2 mssql driver to version 1.3.0b1
- Fixed identities saving when using MDB2 pgsql driver (#1485032)
2008/05/01 (alec)
----------
- Fix BCC header reset (#1484997)

@ -43,7 +43,7 @@
// | Author: Lukas Smith <smith@pooteeweet.org> |
// +----------------------------------------------------------------------+
//
// $Id: MDB2.php,v 1.307 2007/11/10 13:29:05 quipo Exp $
// $Id: MDB2.php,v 1.318 2008/03/08 14:18:38 quipo Exp $
//
/**
@ -100,6 +100,8 @@ define('MDB2_ERROR_MANAGER', -32);
define('MDB2_ERROR_MANAGER_PARSE', -33);
define('MDB2_ERROR_LOADMODULE', -34);
define('MDB2_ERROR_INSUFFICIENT_DATA', -35);
define('MDB2_ERROR_NO_PERMISSION', -36);
// }}}
// {{{ Verbose constants
/**
@ -564,7 +566,7 @@ class MDB2
*/
function apiVersion()
{
return '2.5.0a2';
return '2.5.0b1';
}
// }}}
@ -764,6 +766,7 @@ class MDB2
MDB2_ERROR_LOADMODULE => 'error while including on demand module',
MDB2_ERROR_TRUNCATED => 'truncated',
MDB2_ERROR_DEADLOCK => 'deadlock detected',
MDB2_ERROR_NO_PERMISSION => 'no permission',
);
}
@ -888,7 +891,7 @@ class MDB2
//"username/password@[//]host[:port][/service_name]"
//e.g. "scott/tiger@//mymachine:1521/oracle"
$proto_opts = $dsn;
$dsn = null;
$dsn = substr($proto_opts, strrpos($proto_opts, '/') + 1);
} elseif (strpos($dsn, '/') !== false) {
list($proto_opts, $dsn) = explode('/', $dsn, 2);
} else {
@ -1095,6 +1098,7 @@ class MDB2_Driver_Common extends PEAR
'LOBs' => false,
'replace' => false,
'sub_selects' => false,
'triggers' => false,
'auto_increment' => false,
'primary_key' => false,
'result_introspection' => false,
@ -1142,6 +1146,7 @@ class MDB2_Driver_Common extends PEAR
* <li>$options['datatype_map'] -> array: map user defined datatypes to other primitive datatypes</li>
* <li>$options['datatype_map_callback'] -> array: callback function/method that should be called</li>
* <li>$options['bindname_format'] -> string: regular expression pattern for named parameters
* <li>$options['max_identifiers_length'] -> integer: max identifier length</li>
* </ul>
*
* @var array
@ -1190,6 +1195,7 @@ class MDB2_Driver_Common extends PEAR
'nativetype_map_callback' => array(),
'lob_allow_url_include' => false,
'bindname_format' => '(?:\d+)|(?:[a-zA-Z][a-zA-Z0-9_]*)',
'max_identifiers_length' => 30,
);
/**
@ -2218,6 +2224,23 @@ class MDB2_Driver_Common extends PEAR
'method not implemented', __FUNCTION__);
}
// }}}
// {{{ 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)
{
return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
'method not implemented', __FUNCTION__);
}
// }}}
// {{{ setCharset($charset, $connection = null)
@ -2277,7 +2300,9 @@ class MDB2_Driver_Common extends PEAR
{
$previous_database_name = (isset($this->database_name)) ? $this->database_name : '';
$this->database_name = $name;
if (!empty($this->connected_database_name) && ($this->connected_database_name != $this->database_name)) {
$this->disconnect(false);
}
return $previous_database_name;
}
@ -2795,7 +2820,7 @@ class MDB2_Driver_Common extends PEAR
return $this->raiseError(MDB2_ERROR_CANNOT_REPLACE, null, null,
'key value '.$name.' may not be NULL', __FUNCTION__);
}
$condition[] = $name . '=' . $value;
$condition[] = $this->quoteIdentifier($name, true) . '=' . $value;
}
}
if (empty($condition)) {
@ -2815,13 +2840,16 @@ class MDB2_Driver_Common extends PEAR
}
$condition = ' WHERE '.implode(' AND ', $condition);
$query = "DELETE FROM $table$condition";
$query = 'DELETE FROM ' . $this->quoteIdentifier($table, true) . $condition;
$result =& $this->_doQuery($query, true, $connection);
if (!PEAR::isError($result)) {
$affected_rows = $this->_affectedRows($connection, $result);
$insert = implode(', ', array_keys($values));
$insert = '';
foreach ($values as $key => $value) {
$insert .= ($insert?', ':'') . $this->quoteIdentifier($key, true);
}
$values = implode(', ', $values);
$query = "INSERT INTO $table ($insert) VALUES ($values)";
$query = 'INSERT INTO '. $this->quoteIdentifier($table, true) . "($insert) VALUES ($values)";
$result =& $this->_doQuery($query, true, $connection);
if (!PEAR::isError($result)) {
$affected_rows += $this->_affectedRows($connection, $result);;
@ -2998,7 +3026,7 @@ class MDB2_Driver_Common extends PEAR
return $err;
}
}
} while ($ignore['escape'] && $query[($end_quote - 1)] == $ignore['escape']);
} while ($ignore['escape'] && $query[($end_quote - 1)] == $ignore['escape'] && $end_quote-1 != $start_quote);
$position = $end_quote + 1;
return $position;
}
@ -3980,9 +4008,11 @@ class MDB2_Statement_Common
$types = is_array($types) ? array_values($types) : array_fill(0, count($values), null);
$parameters = array_keys($values);
foreach ($parameters as $key => $parameter) {
$this->db->pushErrorHandling(PEAR_ERROR_RETURN);
$this->db->expectError(MDB2_ERROR_NOT_FOUND);
$err = $this->bindValue($parameter, $values[$parameter], $types[$key]);
$this->db->popExpect();
$this->db->popErrorHandling();
if (PEAR::isError($err)) {
if ($err->getCode() == MDB2_ERROR_NOT_FOUND) {
//ignore (extra value for missing placeholder)

@ -42,7 +42,7 @@
// | Author: Lukas Smith <smith@pooteeweet.org> |
// +----------------------------------------------------------------------+
//
// $Id: Common.php,v 1.128 2007/11/09 20:54:58 quipo Exp $
// $Id: Common.php,v 1.137 2008/02/17 18:53:40 afz Exp $
require_once 'MDB2/LOB.php';
@ -497,6 +497,7 @@ class MDB2_Driver_Datatype_Common extends MDB2_Module_Common
$charset = empty($field['charset']) ? '' :
' '.$this->_getCharsetFieldDeclaration($field['charset']);
$notnull = empty($field['notnull']) ? '' : ' NOT NULL';
$default = '';
if (array_key_exists('default', $field)) {
if ($field['default'] === '') {
@ -504,27 +505,20 @@ class MDB2_Driver_Datatype_Common extends MDB2_Module_Common
if (PEAR::isError($db)) {
return $db;
}
if (empty($field['notnull'])) {
$field['default'] = null;
} else {
$valid_default_values = $this->getValidTypes();
$field['default'] = $valid_default_values[$field['type']];
}
if ($field['default'] === ''
&& ($db->options['portability'] & MDB2_PORTABILITY_EMPTY_TO_NULL)
) {
if ($field['default'] === ''&& ($db->options['portability'] & MDB2_PORTABILITY_EMPTY_TO_NULL)) {
$field['default'] = ' ';
}
}
if (!is_null($field['default'])) {
$default = ' DEFAULT ' . $this->quote($field['default'], $field['type']);
} elseif (empty($field['notnull'])) {
$default = ' DEFAULT NULL';
}
$notnull = empty($field['notnull']) ? '' : ' NOT NULL';
}
$collation = empty($field['collation']) ? '' :
' '.$this->_getCollationFieldDeclaration($field['collation']);
return $charset.$default.$notnull.$collation;
}
@ -1490,7 +1484,7 @@ class MDB2_Driver_Datatype_Common extends MDB2_Module_Common
{
$value = (string)$value;
$value = preg_replace('/[^\d\.,\-+eE]/', '', $value);
if (preg_match('/[^.0-9]/', $value)) {
if (preg_match('/[^\.\d]/', $value)) {
if (strpos($value, ',')) {
// 1000,00
if (!strpos($value, '.')) {

@ -44,7 +44,7 @@
// | Daniel Convissor <danielc@php.net> |
// +----------------------------------------------------------------------+
//
// $Id: mssql.php,v 1.59 2007/12/03 20:59:50 quipo Exp $
// $Id: mssql.php,v 1.65 2008/02/19 14:54:17 afz Exp $
//
require_once 'MDB2/Driver/Datatype/Common.php';
@ -184,68 +184,6 @@ class MDB2_Driver_Datatype_mssql extends MDB2_Driver_Datatype_Common
return '';
}
// }}}
// {{{ _getDeclaration()
/**
* Obtain DBMS specific SQL code portion needed to declare a generic type
* field to be used in statements like CREATE TABLE.
*
* @param string $name name the field to be declared.
* @param array $field associative array with the name of the properties
* of the field being declared as array indexes. Currently, the types
* of supported field properties are as follows:
*
* length
* Integer value that determines the maximum length of the text
* field. If this argument is missing the field should be
* declared to have the longest length allowed by the DBMS.
*
* default
* Text 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 string DBMS specific SQL code portion that should be used to
* declare the specified field.
* @access protected
*/
function _getDeclarationOptions($field)
{
$charset = empty($field['charset']) ? '' :
' '.$this->_getCharsetFieldDeclaration($field['charset']);
$default = '';
if (array_key_exists('default', $field)) {
if ($field['default'] === '') {
$db =& $this->getDBInstance();
if (PEAR::isError($db)) {
return $db;
}
$field['default'] = empty($field['notnull'])
? null : $this->valid_default_values[$field['type']];
if ($field['default'] === ''
&& ($db->options['portability'] & MDB2_PORTABILITY_EMPTY_TO_NULL)
) {
$field['default'] = ' ';
}
}
$default = ' DEFAULT '.$this->quote($field['default'], $field['type']);
} elseif (empty($field['notnull'])) {
$default = ' DEFAULT NULL';
}
$notnull = empty($field['notnull']) ? ' NULL' : ' NOT NULL';
if ($default == ' DEFAULT NULL' && $notnull == ' NULL') {
$notnull = '';
}
$collation = empty($field['collation']) ? '' :
' '.$this->_getCollationFieldDeclaration($field['collation']);
return $charset.$default.$notnull.$collation;
}
// }}}
// {{{ _getIntegerDeclaration()
@ -282,27 +220,27 @@ class MDB2_Driver_Datatype_mssql extends MDB2_Driver_Datatype_Common
return $db;
}
$default = $autoinc = '';;
$notnull = empty($field['notnull']) ? ' NULL' : ' NOT NULL';
$default = $autoinc = '';
if (!empty($field['autoincrement'])) {
$autoinc = ' IDENTITY PRIMARY KEY';
} elseif (array_key_exists('default', $field)) {
if ($field['default'] === '') {
$field['default'] = empty($field['notnull']) ? null : 0;
$field['default'] = 0;
}
$default = ' DEFAULT '.$this->quote($field['default'], 'integer');
} elseif (empty($field['notnull'])) {
$default = ' DEFAULT NULL';
if (is_null($field['default'])) {
$default = ' DEFAULT (null)';
} else {
$default = ' DEFAULT (' . $this->quote($field['default'], 'integer') . ')';
}
$notnull = empty($field['notnull']) ? ' NULL' : ' NOT NULL';
if ($default == ' DEFAULT NULL' && $notnull == ' NULL') {
$notnull = '';
}
if (!empty($field['unsigned'])) {
$db->warnings[] = "unsigned integer field \"$name\" is being declared as signed integer";
}
$name = $db->quoteIdentifier($name, true);
return $name.' '.$this->getTypeDeclaration($field).$default.$notnull.$autoinc;
return $name.' '.$this->getTypeDeclaration($field).$notnull.$default.$autoinc;
}
// }}}
@ -396,7 +334,7 @@ class MDB2_Driver_Datatype_mssql extends MDB2_Driver_Datatype_Common
if (!$quote) {
return $value;
}
$value = bin2hex("0x".$this->_readFile($value));
$value = '0x'.bin2hex($this->_readFile($value));
return $value;
}

@ -3,7 +3,7 @@
// +----------------------------------------------------------------------+
// | PHP versions 4 and 5 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1998-2007 Manuel Lemos, Tomas V.V.Cox, |
// | Copyright (c) 1998-2008 Manuel Lemos, Tomas V.V.Cox, |
// | Stig. S. Bakken, Lukas Smith |
// | All rights reserved. |
// +----------------------------------------------------------------------+
@ -43,7 +43,7 @@
// | Author: Lukas Smith <smith@pooteeweet.org> |
// +----------------------------------------------------------------------+
//
// $Id: mysql.php,v 1.62 2007/11/09 20:54:58 quipo Exp $
// $Id: mysql.php,v 1.65 2008/02/22 19:23:49 quipo Exp $
//
require_once 'MDB2/Driver/Datatype/Common.php';
@ -232,6 +232,93 @@ class MDB2_Driver_Datatype_mysql extends MDB2_Driver_Datatype_Common
$field['default'] = empty($field['notnull']) ? null : 0;
}
$default = ' DEFAULT '.$this->quote($field['default'], 'integer');
}
$notnull = empty($field['notnull']) ? '' : ' NOT NULL';
$unsigned = empty($field['unsigned']) ? '' : ' UNSIGNED';
$name = $db->quoteIdentifier($name, true);
return $name.' '.$this->getTypeDeclaration($field).$unsigned.$default.$notnull.$autoinc;
}
// }}}
// {{{ _getFloatDeclaration()
/**
* Obtain DBMS specific SQL code portion needed to declare an float type
* field to be used in statements like CREATE TABLE.
*
* @param string $name name the field to be declared.
* @param string $field associative array with the name of the properties
* of the field being declared as array indexes.
* Currently, the types of supported field
* properties are as follows:
*
* unsigned
* Boolean flag that indicates whether the field
* should be declared as unsigned float if
* possible.
*
* default
* float 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 string DBMS specific SQL code portion that should be used to
* declare the specified field.
* @access protected
*/
function _getFloatDeclaration($name, $field)
{
// Since AUTO_INCREMENT can be used for integer or floating-point types,
// reuse the INTEGER declaration
// @see http://bugs.mysql.com/bug.php?id=31032
return $this->_getIntegerDeclaration($name, $field);
}
// }}}
// {{{ _getDecimalDeclaration()
/**
* Obtain DBMS specific SQL code portion needed to declare an decimal type
* field to be used in statements like CREATE TABLE.
*
* @param string $name name the field to be declared.
* @param string $field associative array with the name of the properties
* of the field being declared as array indexes.
* Currently, the types of supported field
* properties are as follows:
*
* unsigned
* Boolean flag that indicates whether the field
* should be declared as unsigned integer if
* possible.
*
* default
* Decimal 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 string DBMS specific SQL code portion that should be used to
* declare the specified field.
* @access protected
*/
function _getDecimalDeclaration($name, $field)
{
$db =& $this->getDBInstance();
if (PEAR::isError($db)) {
return $db;
}
$default = '';
if (array_key_exists('default', $field)) {
if ($field['default'] === '') {
$field['default'] = empty($field['notnull']) ? null : 0;
}
$default = ' DEFAULT '.$this->quote($field['default'], 'integer');
} elseif (empty($field['notnull'])) {
$default = ' DEFAULT NULL';
}
@ -239,7 +326,7 @@ class MDB2_Driver_Datatype_mysql extends MDB2_Driver_Datatype_Common
$notnull = empty($field['notnull']) ? '' : ' NOT NULL';
$unsigned = empty($field['unsigned']) ? '' : ' UNSIGNED';
$name = $db->quoteIdentifier($name, true);
return $name.' '.$this->getTypeDeclaration($field).$unsigned.$default.$notnull.$autoinc;
return $name.' '.$this->getTypeDeclaration($field).$unsigned.$default.$notnull;
}
// }}}

@ -43,7 +43,7 @@
// | Author: Lukas Smith <smith@pooteeweet.org> |
// +----------------------------------------------------------------------+
//
// $Id: mysqli.php,v 1.61 2007/11/09 20:54:58 quipo Exp $
// $Id: mysqli.php,v 1.63 2008/02/22 19:23:49 quipo Exp $
//
require_once 'MDB2/Driver/Datatype/Common.php';
@ -232,6 +232,93 @@ class MDB2_Driver_Datatype_mysqli extends MDB2_Driver_Datatype_Common
$field['default'] = empty($field['notnull']) ? null : 0;
}
$default = ' DEFAULT '.$this->quote($field['default'], 'integer');
}
$notnull = empty($field['notnull']) ? '' : ' NOT NULL';
$unsigned = empty($field['unsigned']) ? '' : ' UNSIGNED';
$name = $db->quoteIdentifier($name, true);
return $name.' '.$this->getTypeDeclaration($field).$unsigned.$default.$notnull.$autoinc;
}
// }}}
// {{{ _getFloatDeclaration()
/**
* Obtain DBMS specific SQL code portion needed to declare an float type
* field to be used in statements like CREATE TABLE.
*
* @param string $name name the field to be declared.
* @param string $field associative array with the name of the properties
* of the field being declared as array indexes.
* Currently, the types of supported field
* properties are as follows:
*
* unsigned
* Boolean flag that indicates whether the field
* should be declared as unsigned float if
* possible.
*
* default
* float 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 string DBMS specific SQL code portion that should be used to
* declare the specified field.
* @access protected
*/
function _getFloatDeclaration($name, $field)
{
// Since AUTO_INCREMENT can be used for integer or floating-point types,
// reuse the INTEGER declaration
// @see http://bugs.mysql.com/bug.php?id=31032
return $this->_getIntegerDeclaration($name, $field);
}
// }}}
// {{{ _getDecimalDeclaration()
/**
* Obtain DBMS specific SQL code portion needed to declare an decimal type
* field to be used in statements like CREATE TABLE.
*
* @param string $name name the field to be declared.
* @param string $field associative array with the name of the properties
* of the field being declared as array indexes.
* Currently, the types of supported field
* properties are as follows:
*
* unsigned
* Boolean flag that indicates whether the field
* should be declared as unsigned integer if
* possible.
*
* default
* Decimal 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 string DBMS specific SQL code portion that should be used to
* declare the specified field.
* @access protected
*/
function _getDecimalDeclaration($name, $field)
{
$db =& $this->getDBInstance();
if (PEAR::isError($db)) {
return $db;
}
$default = '';
if (array_key_exists('default', $field)) {
if ($field['default'] === '') {
$field['default'] = empty($field['notnull']) ? null : 0;
}
$default = ' DEFAULT '.$this->quote($field['default'], 'integer');
} elseif (empty($field['notnull'])) {
$default = ' DEFAULT NULL';
}
@ -239,7 +326,7 @@ class MDB2_Driver_Datatype_mysqli extends MDB2_Driver_Datatype_Common
$notnull = empty($field['notnull']) ? '' : ' NOT NULL';
$unsigned = empty($field['unsigned']) ? '' : ' UNSIGNED';
$name = $db->quoteIdentifier($name, true);
return $name.' '.$this->getTypeDeclaration($field).$unsigned.$default.$notnull.$autoinc;
return $name.' '.$this->getTypeDeclaration($field).$unsigned.$default.$notnull;
}
// }}}
@ -356,7 +443,6 @@ class MDB2_Driver_Datatype_mysqli extends MDB2_Driver_Datatype_Common
case 'mediumtext':
case 'longtext':
case 'text':
case 'text':
case 'varchar':
$fixed = false;
case 'string':

@ -42,7 +42,7 @@
// | Author: Paul Cooper <pgc@ucecom.com> |
// +----------------------------------------------------------------------+
//
// $Id: pgsql.php,v 1.88 2007/11/09 20:54:58 quipo Exp $
// $Id: pgsql.php,v 1.91 2008/03/09 12:28:08 quipo Exp $
require_once 'MDB2/Driver/Datatype/Common.php';
@ -217,8 +217,6 @@ class MDB2_Driver_Datatype_pgsql extends MDB2_Driver_Datatype_Common
$field['default'] = empty($field['notnull']) ? null : 0;
}
$default = ' DEFAULT '.$this->quote($field['default'], 'integer');
} elseif (empty($field['notnull'])) {
$default = ' DEFAULT NULL';
}
$notnull = empty($field['notnull']) ? '' : ' NOT NULL';
@ -453,6 +451,7 @@ class MDB2_Driver_Datatype_pgsql extends MDB2_Driver_Datatype_Common
break;
case 'datetime':
case 'timestamp':
case 'timestamptz':
$type[] = 'timestamp';
$length = null;
break;
@ -461,6 +460,7 @@ class MDB2_Driver_Datatype_pgsql extends MDB2_Driver_Datatype_Common
$length = null;
break;
case 'float':
case 'float4':
case 'float8':
case 'double':
case 'real':

@ -43,7 +43,7 @@
// | Author: Lukas Smith <smith@pooteeweet.org> |
// +----------------------------------------------------------------------+
//
// $Id: sqlite.php,v 1.65 2007/12/03 20:59:51 quipo Exp $
// $Id: sqlite.php,v 1.67 2008/02/22 19:58:06 quipo Exp $
//
require_once 'MDB2/Driver/Datatype/Common.php';
@ -212,8 +212,6 @@ class MDB2_Driver_Datatype_sqlite extends MDB2_Driver_Datatype_Common
$field['default'] = empty($field['notnull']) ? null : 0;
}
$default = ' DEFAULT '.$this->quote($field['default'], 'integer');
} elseif (empty($field['notnull'])) {
$default = ' DEFAULT NULL';
}
$notnull = empty($field['notnull']) ? '' : ' NOT NULL';
@ -394,7 +392,6 @@ class MDB2_Driver_Datatype_sqlite extends MDB2_Driver_Datatype_Common
if (PEAR::isError($db)) {
return $db;
}
return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
'unknown database attribute type: '.$db_type, __FUNCTION__);
}

@ -42,7 +42,7 @@
// | Author: Lukas Smith <smith@pooteeweet.org> |
// +----------------------------------------------------------------------+
//
// $Id: Common.php,v 1.19 2007/09/09 13:47:36 quipo Exp $
// $Id: Common.php,v 1.21 2008/02/17 18:51:39 quipo Exp $
//
/**
@ -74,6 +74,7 @@ class MDB2_Driver_Function_Common extends MDB2_Module_Common
* 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
*/
@ -113,6 +114,8 @@ class MDB2_Driver_Function_Common extends MDB2_Module_Common
* - CURRENT_DATE (date, DATE type)
* - CURRENT_TIME (time, TIME type)
*
* @param string $type 'timestamp' | 'time' | 'date'
*
* @return string to call a variable with the current timestamp
* @access public
*/
@ -129,6 +132,29 @@ class MDB2_Driver_Function_Common extends MDB2_Module_Common
}
}
// }}}
// {{{ 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)
{
$db =& $this->getDBInstance();
if (PEAR::isError($db)) {
return $db;
}
$error =& $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
'method not implemented', __FUNCTION__);
return $error;
}
// }}}
// {{{ substring()
@ -146,6 +172,20 @@ class MDB2_Driver_Function_Common extends MDB2_Module_Common
return "SUBSTRING($value FROM $position)";
}
// }}}
// {{{ replace()
/**
* return string to call a function to get replace inside an SQL statement.
*
* @return string to call a function to get a replace
* @access public
*/
function replace($str, $from_str, $to_str)
{
return "REPLACE($str, $from_str , $to_str)";
}
// }}}
// {{{ concat()
@ -155,6 +195,7 @@ class MDB2_Driver_Function_Common extends MDB2_Module_Common
* @param string $value1
* @param string $value2
* @param string $values...
*
* @return string to concatenate two strings
* @access public
*/
@ -185,6 +226,7 @@ class MDB2_Driver_Function_Common extends MDB2_Module_Common
* return string to call a function to lower the case of an expression
*
* @param string $expression
*
* @return return string to lower case of an expression
* @access public
*/
@ -200,6 +242,7 @@ class MDB2_Driver_Function_Common extends MDB2_Module_Common
* return string to call a function to upper the case of an expression
*
* @param string $expression
*
* @return return string to upper case of an expression
* @access public
*/
@ -215,6 +258,7 @@ class MDB2_Driver_Function_Common extends MDB2_Module_Common
* return string to call a function to get the length of a string expression
*
* @param string $expression
*
* @return return string to get the string expression length
* @access public
*/

@ -2,7 +2,7 @@
// +----------------------------------------------------------------------+
// | PHP versions 4 and 5 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1998-2006 Manuel Lemos, Tomas V.V.Cox, |
// | Copyright (c) 1998-2008 Manuel Lemos, Tomas V.V.Cox, |
// | Stig. S. Bakken, Lukas Smith |
// | All rights reserved. |
// +----------------------------------------------------------------------+
@ -42,7 +42,7 @@
// | Author: Frank M. Kromann <frank@kromann.info> |
// +----------------------------------------------------------------------+
//
// $Id: mssql.php,v 1.15 2007/08/11 16:02:22 quipo Exp $
// $Id: mssql.php,v 1.16 2008/02/17 18:54:08 quipo Exp $
//
require_once 'MDB2/Driver/Function/Common.php';
@ -57,7 +57,6 @@ require_once 'MDB2/Driver/Function/Common.php';
*/
class MDB2_Driver_Function_mssql extends MDB2_Driver_Function_Common
{
// }}}
// {{{ executeStoredProc()
/**
@ -108,6 +107,22 @@ class MDB2_Driver_Function_mssql extends MDB2_Driver_Function_Common
}
}
// }}}
// {{{ 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 'DATEDIFF(second, \'19700101\', '. $expression.') + DATEDIFF(second, GETDATE(), GETUTCDATE())';
}
// }}}
// {{{ substring()

@ -2,7 +2,7 @@
// +----------------------------------------------------------------------+
// | PHP versions 4 and 5 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1998-2006 Manuel Lemos, Tomas V.V.Cox, |
// | Copyright (c) 1998-2008 Manuel Lemos, Tomas V.V.Cox, |
// | Stig. S. Bakken, Lukas Smith |
// | All rights reserved. |
// +----------------------------------------------------------------------+
@ -42,7 +42,7 @@
// | Author: Lukas Smith <smith@pooteeweet.org> |
// +----------------------------------------------------------------------+
//
// $Id: mysql.php,v 1.11 2007/01/12 11:29:12 quipo Exp $
// $Id: mysql.php,v 1.12 2008/02/17 18:54:08 quipo Exp $
//
require_once 'MDB2/Driver/Function/Common.php';
@ -83,6 +83,22 @@ class MDB2_Driver_Function_mysql extends MDB2_Driver_Function_Common
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 'UNIX_TIMESTAMP('. $expression.')';
}
// }}}
// {{{ concat()

@ -2,7 +2,7 @@
// +----------------------------------------------------------------------+
// | PHP versions 4 and 5 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1998-2006 Manuel Lemos, Tomas V.V.Cox, |
// | Copyright (c) 1998-2008 Manuel Lemos, Tomas V.V.Cox, |
// | Stig. S. Bakken, Lukas Smith |
// | All rights reserved. |
// +----------------------------------------------------------------------+
@ -42,7 +42,7 @@
// | Author: Lukas Smith <smith@pooteeweet.org> |
// +----------------------------------------------------------------------+
//
// $Id: mysqli.php,v 1.13 2007/01/12 11:29:12 quipo Exp $
// $Id: mysqli.php,v 1.14 2008/02/17 18:54:08 quipo Exp $
//
require_once 'MDB2/Driver/Function/Common.php';
@ -91,6 +91,22 @@ class MDB2_Driver_Function_mysqli extends MDB2_Driver_Function_Common
return $result;
}
// }}}
// {{{ 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 'UNIX_TIMESTAMP('. $expression.')';
}
// }}}
// {{{ concat()

@ -2,7 +2,7 @@
// +----------------------------------------------------------------------+
// | PHP versions 4 and 5 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1998-2006 Manuel Lemos, Tomas V.V.Cox, |
// | Copyright (c) 1998-2008 Manuel Lemos, Tomas V.V.Cox, |
// | Stig. S. Bakken, Lukas Smith |
// | All rights reserved. |
// +----------------------------------------------------------------------+
@ -42,7 +42,7 @@
// | Author: Paul Cooper <pgc@ucecom.com> |
// +----------------------------------------------------------------------+
//
// $Id: pgsql.php,v 1.9 2006/06/12 21:48:43 lsmith Exp $
// $Id: pgsql.php,v 1.10 2008/02/17 18:54:08 quipo Exp $
require_once 'MDB2/Driver/Function/Common.php';
@ -80,6 +80,22 @@ class MDB2_Driver_Function_pgsql extends MDB2_Driver_Function_Common
$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\', TIMESTAMP '. $expression.'))';
}
// }}}
// {{{ random()

@ -2,7 +2,7 @@
// +----------------------------------------------------------------------+
// | PHP versions 4 and 5 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1998-2006 Manuel Lemos, Tomas V.V.Cox, |
// | Copyright (c) 1998-2008 Manuel Lemos, Tomas V.V.Cox, |
// | Stig. S. Bakken, Lukas Smith |
// | All rights reserved. |
// +----------------------------------------------------------------------+
@ -42,7 +42,7 @@
// | Author: Lukas Smith <smith@pooteeweet.org> |
// +----------------------------------------------------------------------+
//
// $Id: sqlite.php,v 1.8 2006/06/13 22:55:55 lsmith Exp $
// $Id: sqlite.php,v 1.10 2008/02/17 18:54:08 quipo Exp $
//
require_once 'MDB2/Driver/Function/Common.php';
@ -89,6 +89,22 @@ class MDB2_Driver_Function_sqlite extends MDB2_Driver_Function_Common
}
}
// }}}
// {{{ 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 'strftime("%s",'. $expression.', "utc")';
}
// }}}
// {{{ substring()
@ -120,6 +136,27 @@ class MDB2_Driver_Function_sqlite extends MDB2_Driver_Function_Common
return '((RANDOM()+2147483648)/4294967296)';
}
// }}}
// {{{ replace()
/**
* return string to call a function to get a replacement inside an SQL statement.
*
* @return string to call a function to get a replace
* @access public
*/
function replace($str, $from_str, $to_str)
{
$db =& $this->getDBInstance();
if (PEAR::isError($db)) {
return $db;
}
$error =& $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
'method not implemented', __FUNCTION__);
return $error;
}
// }}}
}
?>

@ -2,7 +2,7 @@
// +----------------------------------------------------------------------+
// | PHP versions 4 and 5 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1998-2007 Manuel Lemos, Tomas V.V.Cox, |
// | Copyright (c) 1998-2008 Manuel Lemos, Tomas V.V.Cox, |
// | Stig. S. Bakken, Lukas Smith |
// | All rights reserved. |
// +----------------------------------------------------------------------+
@ -39,16 +39,18 @@
// | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
// | POSSIBILITY OF SUCH DAMAGE. |
// +----------------------------------------------------------------------+
// | Author: Lukas Smith <smith@pooteeweet.org> |
// | Authors: Lukas Smith <smith@pooteeweet.org> |
// | Lorenzo Alberton <l.alberton@quipo.it> |
// +----------------------------------------------------------------------+
//
// $Id: Common.php,v 1.68 2007/12/03 20:59:15 quipo Exp $
// $Id: Common.php,v 1.71 2008/02/12 23:12:27 quipo Exp $
//
/**
* @package MDB2
* @category Database
* @author Lukas Smith <smith@pooteeweet.org>
* @author Lorenzo Alberton <l.alberton@quipo.it>
*/
/**
@ -198,6 +200,29 @@ class MDB2_Driver_Manager_Common extends MDB2_Module_Common
'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()
@ -359,6 +384,56 @@ class MDB2_Driver_Manager_Common extends MDB2_Module_Common
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()

@ -2,7 +2,7 @@
// +----------------------------------------------------------------------+
// | PHP versions 4 and 5 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1998-2007 Manuel Lemos, Tomas V.V.Cox, |
// | Copyright (c) 1998-2008 Manuel Lemos, Tomas V.V.Cox, |
// | Stig. S. Bakken, Lukas Smith |
// | All rights reserved. |
// +----------------------------------------------------------------------+
@ -44,7 +44,7 @@
// | Lorenzo Alberton <l.alberton@quipo.it> |
// +----------------------------------------------------------------------+
//
// $Id: mssql.php,v 1.93 2007/12/03 20:59:15 quipo Exp $
// $Id: mssql.php,v 1.109 2008/03/05 12:55:57 afz Exp $
//
require_once 'MDB2/Driver/Manager/Common.php';
@ -92,6 +92,39 @@ class MDB2_Driver_Manager_mssql extends MDB2_Driver_Manager_Common
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 name, 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 = '';
if (!empty($options['name'])) {
$query .= ' MODIFY NAME = ' .$db->quoteIdentifier($options['name'], true);
}
if (!empty($options['collation'])) {
$query .= ' COLLATE ' . $options['collation'];
}
if (!empty($query)) {
$query = 'ALTER DATABASE '. $db->quoteIdentifier($name, true) . $query;
return $db->standaloneQuery($query, null, true);
}
return MDB2_OK;
}
// }}}
// {{{ dropDatabase()
@ -199,6 +232,64 @@ class MDB2_Driver_Manager_mssql extends MDB2_Driver_Manager_Common
return parent::createTable($name, $fields, $options);
}
// }}}
// {{{ 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]
*
* NB: you have to run the NSControl Create utility to enable VACUUM
*
* @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;
}
$timeout = isset($options['timeout']) ? (int)$options['timeout'] : 300;
$query = 'NSControl Create';
$result = $db->exec($query);
if (PEAR::isError($result)) {
return $result;
}
return $db->exec('EXEC NSVacuum '.$timeout);
}
// }}}
// {{{ alterTable()
@ -298,16 +389,16 @@ class MDB2_Driver_Manager_mssql extends MDB2_Driver_Manager_Common
if (PEAR::isError($db)) {
return $db;
}
$name_quoted = $db->quoteIdentifier($name, true);
foreach ($changes as $change_name => $change) {
switch ($change_name) {
case 'add':
break;
case 'remove':
break;
case 'name':
case 'rename':
case 'add':
case 'change':
case 'name':
break;
default:
return $db->raiseError(MDB2_ERROR_CANNOT_ALTER, null, null,
'change type "'.$change_name.'" not yet supported', __FUNCTION__);
@ -318,34 +409,257 @@ class MDB2_Driver_Manager_mssql extends MDB2_Driver_Manager_Common
return MDB2_OK;
}
$idxname_format = $db->getOption('idxname_format');
$db->setOption('idxname_format', '%s');
if (!empty($changes['remove']) && is_array($changes['remove'])) {
$result = $this->_dropConflictingIndices($name, array_keys($changes['remove']));
if (PEAR::isError($result)) {
$db->setOption('idxname_format', $idxname_format);
return $result;
}
$result = $this->_dropConflictingConstraints($name, array_keys($changes['remove']));
if (PEAR::isError($result)) {
$db->setOption('idxname_format', $idxname_format);
return $result;
}
$query = '';
foreach ($changes['remove'] as $field_name => $field) {
if ($query) {
$query.= ', ';
}
$field_name = $db->quoteIdentifier($field_name, true);
$query.= 'COLUMN ' . $field_name;
}
$result = $db->exec("ALTER TABLE $name_quoted DROP $query");
if (PEAR::isError($result)) {
$db->setOption('idxname_format', $idxname_format);
return $result;
}
}
if (!empty($changes['rename']) && is_array($changes['rename'])) {
foreach ($changes['rename'] as $field_name => $field) {
$field_name = $db->quoteIdentifier($field_name, true);
$result = $db->exec("sp_rename '$name_quoted.$field_name', '".$field['name']."', 'COLUMN'");
if (PEAR::isError($result)) {
$db->setOption('idxname_format', $idxname_format);
return $result;
}
}
}
if (!empty($changes['add']) && is_array($changes['add'])) {
$query = '';
foreach ($changes['add'] as $field_name => $field) {
if ($query) {
$query.= ', ';
} else {
$query.= 'ADD COLUMN ';
$query.= 'ADD ';
}
$query.= $db->getDeclaration($field['type'], $field_name, $field);
}
$result = $db->exec("ALTER TABLE $name_quoted $query");
if (PEAR::isError($result)) {
$db->setOption('idxname_format', $idxname_format);
return $result;
}
}
if (!empty($changes['remove']) && is_array($changes['remove'])) {
foreach ($changes['remove'] as $field_name => $field) {
if ($query) {
$query.= ', ';
$dropped_indices = array();
$dropped_constraints = array();
if (!empty($changes['change']) && is_array($changes['change'])) {
$dropped = $this->_dropConflictingIndices($name, array_keys($changes['change']));
if (PEAR::isError($dropped)) {
$db->setOption('idxname_format', $idxname_format);
return $dropped;
}
$field_name = $db->quoteIdentifier($field_name, true);
$query.= 'DROP COLUMN ' . $field_name;
$dropped_indices = array_merge($dropped_indices, $dropped);
$dropped = $this->_dropConflictingConstraints($name, array_keys($changes['change']));
if (PEAR::isError($dropped)) {
$db->setOption('idxname_format', $idxname_format);
return $dropped;
}
$dropped_constraints = array_merge($dropped_constraints, $dropped);
foreach ($changes['change'] as $field_name => $field) {
//MSSQL doesn't allow multiple ALTER COLUMNs in one query
$query = 'ALTER COLUMN ';
//MSSQL doesn't allow changing the DEFAULT value of a field in altering mode
if (array_key_exists('default', $field['definition'])) {
unset($field['definition']['default']);
}
$query .= $db->getDeclaration($field['definition']['type'], $field_name, $field['definition']);
$result = $db->exec("ALTER TABLE $name_quoted $query");
if (PEAR::isError($result)) {
$db->setOption('idxname_format', $idxname_format);
return $result;
}
}
}
// restore the dropped conflicting indices and constraints
foreach ($dropped_indices as $index_name => $index) {
$result = $this->createIndex($name, $index_name, $index);
if (PEAR::isError($result)) {
$db->setOption('idxname_format', $idxname_format);
return $result;
}
}
foreach ($dropped_constraints as $constraint_name => $constraint) {
$result = $this->createConstraint($name, $constraint_name, $constraint);
if (PEAR::isError($result)) {
$db->setOption('idxname_format', $idxname_format);
return $result;
}
}
$db->setOption('idxname_format', $idxname_format);
if (!empty($changes['name'])) {
$new_name = $db->quoteIdentifier($changes['name'], true);
$result = $db->exec("sp_rename '$name_quoted', '$new_name'");
if (PEAR::isError($result)) {
return $result;
}
}
if (!$query) {
return MDB2_OK;
}
$name = $db->quoteIdentifier($name, true);
return $db->exec("ALTER TABLE $name $query");
// }}}
// {{{ _dropConflictingIndices()
/**
* Drop the indices that prevent a successful ALTER TABLE action
*
* @param string $table table name
* @param array $fields array of names of the fields affected by the change
*
* @return array dropped indices definitions
*/
function _dropConflictingIndices($table, $fields)
{
$db =& $this->getDBInstance();
if (PEAR::isError($db)) {
return $db;
}
$dropped = array();
$index_names = $this->listTableIndexes($table);
if (PEAR::isError($index_names)) {
return $index_names;
}
$db->loadModule('Reverse');
$indexes = array();
foreach ($index_names as $index_name) {
$idx_def = $db->reverse->getTableIndexDefinition($table, $index_name);
if (!PEAR::isError($idx_def)) {
$indexes[$index_name] = $idx_def;
}
}
foreach ($fields as $field_name) {
foreach ($indexes as $index_name => $index) {
if (!isset($dropped[$index_name]) && array_key_exists($field_name, $index['fields'])) {
$dropped[$index_name] = $index;
$result = $this->dropIndex($table, $index_name);
if (PEAR::isError($result)) {
return $result;
}
}
}
}
return $dropped;
}
// }}}
// {{{ _dropConflictingConstraints()
/**
* Drop the constraints that prevent a successful ALTER TABLE action
*
* @param string $table table name
* @param array $fields array of names of the fields affected by the change
*
* @return array dropped constraints definitions
*/
function _dropConflictingConstraints($table, $fields)
{
$db =& $this->getDBInstance();
if (PEAR::isError($db)) {
return $db;
}
$dropped = array();
$constraint_names = $this->listTableConstraints($table);
if (PEAR::isError($constraint_names)) {
return $constraint_names;
}
$db->loadModule('Reverse');
$constraints = array();
foreach ($constraint_names as $constraint_name) {
$cons_def = $db->reverse->getTableConstraintDefinition($table, $constraint_name);
if (!PEAR::isError($cons_def)) {
$constraints[$constraint_name] = $cons_def;
}
}
foreach ($fields as $field_name) {
foreach ($constraints as $constraint_name => $constraint) {
if (!isset($dropped[$constraint_name]) && array_key_exists($field_name, $constraint['fields'])) {
$dropped[$constraint_name] = $constraint;
$result = $this->dropConstraint($table, $constraint_name);
if (PEAR::isError($result)) {
return $result;
}
}
}
// also drop implicit DEFAULT constraints
$default = $this->_getTableFieldDefaultConstraint($table, $field_name);
if (!PEAR::isError($default) && !empty($default)) {
$result = $this->dropConstraint($table, $default);
if (PEAR::isError($result)) {
return $result;
}
}
}
return $dropped;
}
// }}}
// {{{ _getTableFieldDefaultConstraint()
/**
* Get the default constraint for a table field
*
* @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 name of default constraint on success, a MDB2 error on failure
* @access private
*/
function _getTableFieldDefaultConstraint($table, $field)
{
$db =& $this->getDBInstance();
if (PEAR::isError($db)) {
return $db;
}
$table = $db->quoteIdentifier($table, true);
$field = $db->quote($field, 'text');
$query = "SELECT OBJECT_NAME(syscolumns.cdefault)
FROM syscolumns
WHERE syscolumns.id = object_id('$table')
AND syscolumns.name = $field
AND syscolumns.cdefault <> 0";
return $db->queryOne($query);
}
// }}}

@ -2,7 +2,7 @@
// +----------------------------------------------------------------------+
// | PHP versions 4 and 5 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1998-2007 Manuel Lemos, Tomas V.V.Cox, |
// | Copyright (c) 1998-2008 Manuel Lemos, Tomas V.V.Cox, |
// | Stig. S. Bakken, Lukas Smith |
// | All rights reserved. |
// +----------------------------------------------------------------------+
@ -42,7 +42,7 @@
// | Author: Lukas Smith <smith@pooteeweet.org> |
// +----------------------------------------------------------------------+
//
// $Id: mysql.php,v 1.100 2007/12/03 20:59:15 quipo Exp $
// $Id: mysql.php,v 1.108 2008/03/11 19:58:12 quipo Exp $
//
require_once 'MDB2/Driver/Manager/Common.php';
@ -79,16 +79,41 @@ class MDB2_Driver_Manager_mysql extends MDB2_Driver_Manager_Common
$name = $db->quoteIdentifier($name, true);
$query = 'CREATE DATABASE ' . $name;
if (!empty($options['charset'])) {
$query .= ' DEFAULT CHARACTER SET ' . $options['charset'];
$query .= ' DEFAULT CHARACTER SET ' . $db->quote($options['charset'], 'text');
}
if (!empty($options['collation'])) {
$query .= ' COLLATE ' . $options['collation'];
$query .= ' COLLATE ' . $db->quote($options['collation'], 'text');
}
$result = $db->exec($query);
if (PEAR::isError($result)) {
return $result;
return $db->standaloneQuery($query, null, true);
}
return MDB2_OK;
// }}}
// {{{ 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);
}
// }}}
@ -110,11 +135,7 @@ class MDB2_Driver_Manager_mysql extends MDB2_Driver_Manager_Common
$name = $db->quoteIdentifier($name, true);
$query = "DROP DATABASE $name";
$result = $db->exec($query);
if (PEAR::isError($result)) {
return $result;
}
return MDB2_OK;
return $db->standaloneQuery($query, null, true);
}
// }}}
@ -188,11 +209,37 @@ class MDB2_Driver_Manager_mysql extends MDB2_Driver_Manager_Common
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'])) {
@ -226,6 +273,112 @@ class MDB2_Driver_Manager_mysql extends MDB2_Driver_Manager_Common
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()
@ -656,6 +809,7 @@ class MDB2_Driver_Manager_mysql extends MDB2_Driver_Manager_Common
* 'last_login' => array()
* )
* )
*
* @return mixed MDB2_OK on success, a MDB2 error on failure
* @access public
*/
@ -800,8 +954,8 @@ class MDB2_Driver_Manager_mysql extends MDB2_Driver_Manager_Common
'invalid definition, could not create constraint', __FUNCTION__);
}
$table = $db->quoteIdentifier($table, true);
$query = "ALTER TABLE $table ADD $type $name";
$table_quoted = $db->quoteIdentifier($table, true);
$query = "ALTER TABLE $table_quoted ADD $type $name";
if (!empty($definition['foreign'])) {
$query .= ' FOREIGN KEY';
}
@ -819,7 +973,14 @@ class MDB2_Driver_Manager_mysql extends MDB2_Driver_Manager_Common
$query .= ' ('. implode(', ', $referenced_fields) . ')';
$query .= $this->_getAdvancedFKOptions($definition);
}
return $db->exec($query);
$res = $db->exec($query);
if (PEAR::isError($res)) {
return $res;
}
if (!empty($definition['foreign'])) {
return $this->_createFKTriggers($table, array($name => $definition));
}
return MDB2_OK;
}
// }}}
@ -841,16 +1002,210 @@ class MDB2_Driver_Manager_mysql extends MDB2_Driver_Manager_Common
return $db;
}
$table = $db->quoteIdentifier($table, true);
if ($primary || strtolower($name) == 'primary') {
$query = "ALTER TABLE $table DROP PRIMARY KEY";
} else {
$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 INDEX $name";
$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 = $db->quoteIdentifier($table, true);
foreach ($foreign_keys as $fkname => $fkdef) {
if (empty($fkdef)) {
continue;
}
//set actions to 'RESTRICT' if not set
$fkdef['onupdate'] = empty($fkdef['onupdate']) ? 'RESTRICT' : strtoupper($fkdef['onupdate']);
$fkdef['ondelete'] = empty($fkdef['ondelete']) ? 'RESTRICT' : 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 .'.'.$field .' AS '.$field;
}
$restrict_action .= implode(',', $aliased_fields)
.' FROM '.$table
.' 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';
}
$restrict_action .= implode(' AND ', $conditions).') IS NOT NULL'
.' THEN CALL %s_ON_TABLE_'.$table.'_VIOLATES_FOREIGN_KEY_CONSTRAINT();'
.' END IF;';
$cascade_action_update = 'UPDATE '.$table.' SET '.implode(', ', $new_values) .' WHERE '.implode(' AND ', $conditions). ';';
$cascade_action_delete = 'DELETE FROM '.$table.' WHERE '.implode(' AND ', $conditions). ';';
$setnull_action = 'UPDATE '.$table.' 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.' 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');
} else {
//'RESTRICT'
$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');
} else {
//'RESTRICT'
$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()
@ -904,8 +1259,8 @@ class MDB2_Driver_Manager_mysql extends MDB2_Driver_Manager_Common
$query = 'SHOW CREATE TABLE '. $db->escape($table);
$definition = $db->queryOne($query, 'text', 1);
if (!PEAR::isError($definition) && !empty($definition)) {
$pattern = '/\bCONSTRAINT\s+([^\s]+)\s+FOREIGN KEY\b/i';
if (preg_match_all($pattern, str_replace('`', '', $definition), $matches) > 1) {
$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;
}
@ -974,7 +1329,6 @@ class MDB2_Driver_Manager_mysql extends MDB2_Driver_Manager_Common
$query .= ' '.implode(' ', $options_strings);
}
$res = $db->exec($query);
if (PEAR::isError($res)) {
return $res;
}

@ -2,7 +2,7 @@
// +----------------------------------------------------------------------+
// | PHP versions 4 and 5 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1998-2007 Manuel Lemos, Tomas V.V.Cox, |
// | Copyright (c) 1998-2008 Manuel Lemos, Tomas V.V.Cox, |
// | Stig. S. Bakken, Lukas Smith |
// | All rights reserved. |
// +----------------------------------------------------------------------+
@ -42,7 +42,7 @@
// | Author: Lukas Smith <smith@pooteeweet.org> |
// +----------------------------------------------------------------------+
//
// $Id: mysqli.php,v 1.87 2007/12/03 20:59:15 quipo Exp $
// $Id: mysqli.php,v 1.95 2008/03/11 19:58:12 quipo Exp $
//
require_once 'MDB2/Driver/Manager/Common.php';
@ -79,16 +79,41 @@ class MDB2_Driver_Manager_mysqli extends MDB2_Driver_Manager_Common
$name = $db->quoteIdentifier($name, true);
$query = 'CREATE DATABASE ' . $name;
if (!empty($options['charset'])) {
$query .= ' DEFAULT CHARACTER SET ' . $options['charset'];
$query .= ' DEFAULT CHARACTER SET ' . $db->quote($options['charset'], 'text');
}
if (!empty($options['collation'])) {
$query .= ' COLLATE ' . $options['collation'];
$query .= ' COLLATE ' . $db->quote($options['collation'], 'text');
}
$result = $db->exec($query);
if (PEAR::isError($result)) {
return $result;
return $db->standaloneQuery($query, null, true);
}
return MDB2_OK;
// }}}
// {{{ 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);
}
// }}}
@ -110,11 +135,7 @@ class MDB2_Driver_Manager_mysqli extends MDB2_Driver_Manager_Common
$name = $db->quoteIdentifier($name, true);
$query = "DROP DATABASE $name";
$result = $db->exec($query);
if (PEAR::isError($result)) {
return $result;
}
return MDB2_OK;
return $db->standaloneQuery($query, null, true);
}
// }}}
@ -188,11 +209,37 @@ class MDB2_Driver_Manager_mysqli extends MDB2_Driver_Manager_Common
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'])) {
@ -226,6 +273,112 @@ class MDB2_Driver_Manager_mysqli extends MDB2_Driver_Manager_Common
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()
@ -656,6 +809,7 @@ class MDB2_Driver_Manager_mysqli extends MDB2_Driver_Manager_Common
* 'last_login' => array()
* )
* )
*
* @return mixed MDB2_OK on success, a MDB2 error on failure
* @access public
*/
@ -800,8 +954,8 @@ class MDB2_Driver_Manager_mysqli extends MDB2_Driver_Manager_Common
'invalid definition, could not create constraint', __FUNCTION__);
}
$table = $db->quoteIdentifier($table, true);
$query = "ALTER TABLE $table ADD $type $name";
$table_quoted = $db->quoteIdentifier($table, true);
$query = "ALTER TABLE $table_quoted ADD $type $name";
if (!empty($definition['foreign'])) {
$query .= ' FOREIGN KEY';
}
@ -819,7 +973,14 @@ class MDB2_Driver_Manager_mysqli extends MDB2_Driver_Manager_Common
$query .= ' ('. implode(', ', $referenced_fields) . ')';
$query .= $this->_getAdvancedFKOptions($definition);
}
return $db->exec($query);
$res = $db->exec($query);
if (PEAR::isError($res)) {
return $res;
}
if (!empty($definition['foreign'])) {
return $this->_createFKTriggers($table, array($name => $definition));
}
return MDB2_OK;
}
// }}}
@ -841,16 +1002,210 @@ class MDB2_Driver_Manager_mysqli extends MDB2_Driver_Manager_Common
return $db;
}
$table = $db->quoteIdentifier($table, true);
if ($primary || strtolower($name) == 'primary') {
$query = "ALTER TABLE $table DROP PRIMARY KEY";
} else {
$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 INDEX $name";
$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 = $db->quoteIdentifier($table, true);
foreach ($foreign_keys as $fkname => $fkdef) {
if (empty($fkdef)) {
continue;
}
//set actions to 'RESTRICT' if not set
$fkdef['onupdate'] = empty($fkdef['onupdate']) ? 'RESTRICT' : strtoupper($fkdef['onupdate']);
$fkdef['ondelete'] = empty($fkdef['ondelete']) ? 'RESTRICT' : 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 .'.'.$field .' AS '.$field;
}
$restrict_action .= implode(',', $aliased_fields)
.' FROM '.$table
.' 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';
}
$restrict_action .= implode(' AND ', $conditions).') IS NOT NULL'
.' THEN CALL %s_ON_TABLE_'.$table.'_VIOLATES_FOREIGN_KEY_CONSTRAINT();'
.' END IF;';
$cascade_action_update = 'UPDATE '.$table.' SET '.implode(', ', $new_values) .' WHERE '.implode(' AND ', $conditions). ';';
$cascade_action_delete = 'DELETE FROM '.$table.' WHERE '.implode(' AND ', $conditions). ';';
$setnull_action = 'UPDATE '.$table.' 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.' 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');
} else {
//'RESTRICT'
$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');
} else {
//'RESTRICT'
$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()
@ -904,8 +1259,8 @@ class MDB2_Driver_Manager_mysqli extends MDB2_Driver_Manager_Common
$query = 'SHOW CREATE TABLE '. $db->escape($table);
$definition = $db->queryOne($query, 'text', 1);
if (!PEAR::isError($definition) && !empty($definition)) {
$pattern = '/\bCONSTRAINT\s+([^\s]+)\s+FOREIGN KEY\b/i';
if (preg_match_all($pattern, str_replace('`', '', $definition), $matches) > 1) {
$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;
}
@ -969,10 +1324,6 @@ class MDB2_Driver_Manager_mysqli extends MDB2_Driver_Manager_Common
$options_strings[] = "ENGINE = $type";
}
if (!empty($options_strings)) {
$query.= ' '.implode(' ', $options_strings);
}
$query = "CREATE TABLE $sequence_name ($seqcol_name INT NOT NULL AUTO_INCREMENT, PRIMARY KEY ($seqcol_name))";
if (!empty($options_strings)) {
$query .= ' '.implode(' ', $options_strings);

@ -2,7 +2,7 @@
// +----------------------------------------------------------------------+
// | PHP versions 4 and 5 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1998-2007 Manuel Lemos, Tomas V.V.Cox, |
// | Copyright (c) 1998-2008 Manuel Lemos, Tomas V.V.Cox, |
// | Stig. S. Bakken, Lukas Smith |
// | All rights reserved. |
// +----------------------------------------------------------------------+
@ -42,7 +42,7 @@
// | Author: Paul Cooper <pgc@ucecom.com> |
// +----------------------------------------------------------------------+
//
// $Id: pgsql.php,v 1.74 2007/12/03 20:59:15 quipo Exp $
// $Id: pgsql.php,v 1.82 2008/03/05 12:55:57 afz Exp $
require_once 'MDB2/Driver/Manager/Common.php';
@ -81,6 +81,35 @@ class MDB2_Driver_Manager_pgsql extends MDB2_Driver_Manager_Common
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 name, owner 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['name'])) {
$query .= ' RENAME TO ' . $options['name'];
}
if (!empty($options['owner'])) {
$query .= ' OWNER TO ' . $options['owner'];
}
return $db->standaloneQuery($query, null, true);
}
// }}}
// {{{ dropDatabase()
@ -99,7 +128,8 @@ class MDB2_Driver_Manager_pgsql extends MDB2_Driver_Manager_Common
}
$name = $db->quoteIdentifier($name, true);
return $db->standaloneQuery("DROP DATABASE $name", null, true);
$query = "DROP DATABASE $name";
return $db->standaloneQuery($query, null, true);
}
// }}}
@ -138,6 +168,69 @@ class MDB2_Driver_Manager_pgsql extends MDB2_Driver_Manager_Common
return $query;
}
// }}}
// {{{ 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;
}
$query = 'VACUUM';
if (!empty($options['full'])) {
$query .= ' FULL';
}
if (!empty($options['freeze'])) {
$query .= ' FREEZE';
}
if (!empty($options['analyze'])) {
$query .= ' ANALYZE';
}
if (!empty($table)) {
$query .= ' '.$db->quoteIdentifier($table, true);
}
return $db->exec($query);
}
// }}}
// {{{ alterTable()
@ -256,9 +349,10 @@ class MDB2_Driver_Manager_pgsql extends MDB2_Driver_Manager_Common
return MDB2_OK;
}
if (!empty($changes['add']) && is_array($changes['add'])) {
foreach ($changes['add'] as $field_name => $field) {
$query = 'ADD ' . $db->getDeclaration($field['type'], $field_name, $field);
if (!empty($changes['remove']) && is_array($changes['remove'])) {
foreach ($changes['remove'] as $field_name => $field) {
$field_name = $db->quoteIdentifier($field_name, true);
$query = 'DROP ' . $field_name;
$result = $db->exec("ALTER TABLE $name $query");
if (PEAR::isError($result)) {
return $result;
@ -266,10 +360,19 @@ class MDB2_Driver_Manager_pgsql extends MDB2_Driver_Manager_Common
}
}
if (!empty($changes['remove']) && is_array($changes['remove'])) {
foreach ($changes['remove'] as $field_name => $field) {
if (!empty($changes['rename']) && is_array($changes['rename'])) {
foreach ($changes['rename'] as $field_name => $field) {
$field_name = $db->quoteIdentifier($field_name, true);
$query = 'DROP ' . $field_name;
$result = $db->exec("ALTER TABLE $name RENAME COLUMN $field_name TO ".$db->quoteIdentifier($field['name'], true));
if (PEAR::isError($result)) {
return $result;
}
}
}
if (!empty($changes['add']) && is_array($changes['add'])) {
foreach ($changes['add'] as $field_name => $field) {
$query = 'ADD ' . $db->getDeclaration($field['type'], $field_name, $field);
$result = $db->exec("ALTER TABLE $name $query");
if (PEAR::isError($result)) {
return $result;
@ -313,16 +416,6 @@ class MDB2_Driver_Manager_pgsql extends MDB2_Driver_Manager_Common
}
}
if (!empty($changes['rename']) && is_array($changes['rename'])) {
foreach ($changes['rename'] as $field_name => $field) {
$field_name = $db->quoteIdentifier($field_name, true);
$result = $db->exec("ALTER TABLE $name RENAME COLUMN $field_name TO ".$db->quoteIdentifier($field['name'], true));
if (PEAR::isError($result)) {
return $result;
}
}
}
$name = $db->quoteIdentifier($name, true);
if (!empty($changes['name'])) {
$change_name = $db->quoteIdentifier($changes['name'], true);
@ -662,9 +755,10 @@ class MDB2_Driver_Manager_pgsql extends MDB2_Driver_Manager_Common
}
$table = $db->quote($table, 'text');
$subquery = "SELECT indexrelid FROM pg_index, pg_class";
$subquery.= " WHERE pg_class.relname=$table AND pg_class.oid=pg_index.indrelid AND (indisunique = 't' OR indisprimary = 't')";
$query = "SELECT relname FROM pg_class WHERE oid IN ($subquery)";
$query = 'SELECT conname
FROM pg_constraint, pg_class
WHERE pg_constraint.conrelid = pg_class.oid
AND relname = ' .$table;
$constraints = $db->queryCol($query);
if (PEAR::isError($constraints)) {
return $constraints;

@ -2,7 +2,7 @@
// +----------------------------------------------------------------------+
// | PHP versions 4 and 5 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1998-2007 Manuel Lemos, Tomas V.V.Cox, |
// | Copyright (c) 1998-2008 Manuel Lemos, Tomas V.V.Cox, |
// | Stig. S. Bakken, Lukas Smith, Lorenzo Alberton |
// | All rights reserved. |
// +----------------------------------------------------------------------+
@ -43,7 +43,7 @@
// | Lorenzo Alberton <l.alberton@quipo.it> |
// +----------------------------------------------------------------------+
//
// $Id: sqlite.php,v 1.72 2007/12/03 20:59:15 quipo Exp $
// $Id: sqlite.php,v 1.74 2008/03/05 11:08:53 quipo Exp $
//
require_once 'MDB2/Driver/Manager/Common.php';
@ -403,6 +403,37 @@ class MDB2_Driver_Manager_sqlite extends MDB2_Driver_Manager_Common
return $db->exec("DROP 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;
}
$query = 'VACUUM';
if (!empty($table)) {
$query .= ' '.$db->quoteIdentifier($table, true);
}
return $db->exec($query);
}
// }}}
// {{{ alterTable()
@ -1207,7 +1238,7 @@ class MDB2_Driver_Manager_sqlite extends MDB2_Driver_Manager_Common
if (PEAR::isError($table_def)) {
return $table_def;
}
if (preg_match("/\bPRIMARY\s+KEY\b\s*\(([^)]+)/i", $table_def, $tmp)) {
if (preg_match("/\bPRIMARY\s+KEY\b/i", $table_def, $tmp)) {
$result['primary'] = true;
}

@ -42,7 +42,7 @@
// | Author: Lukas Smith <smith@pooteeweet.org> |
// +----------------------------------------------------------------------+
//
// $Id: Common.php,v 1.41 2007/09/09 13:47:36 quipo Exp $
// $Id: Common.php,v 1.42 2008/01/12 12:50:58 quipo Exp $
//
/**
@ -466,6 +466,8 @@ class MDB2_Driver_Reverse_Common extends MDB2_Module_Common
}
}
$res = array();
if ($mode) {
$res['num_fields'] = count($fields);
}

@ -43,7 +43,7 @@
// | Lorenzo Alberton <l.alberton@quipo.it> |
// +----------------------------------------------------------------------+
//
// $Id: mssql.php,v 1.48 2007/11/25 13:38:29 quipo Exp $
// $Id: mssql.php,v 1.49 2008/02/17 15:30:57 quipo Exp $
//
require_once 'MDB2/Driver/Reverse/Common.php';

@ -2,7 +2,7 @@
// +----------------------------------------------------------------------+
// | PHP versions 4 and 5 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1998-2007 Manuel Lemos, Tomas V.V.Cox, |
// | Copyright (c) 1998-2008 Manuel Lemos, Tomas V.V.Cox, |
// | Stig. S. Bakken, Lukas Smith |
// | All rights reserved. |
// +----------------------------------------------------------------------+
@ -43,7 +43,7 @@
// | Lorenzo Alberton <l.alberton@quipo.it> |
// +----------------------------------------------------------------------+
//
// $Id: pgsql.php,v 1.68 2007/11/25 13:38:29 quipo Exp $
// $Id: pgsql.php,v 1.70 2008/03/13 20:38:09 quipo Exp $
require_once 'MDB2/Driver/Reverse/Common.php';
@ -358,7 +358,7 @@ class MDB2_Driver_Reverse_pgsql extends MDB2_Driver_Reverse_Common
$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.conkey)
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');
$constraint_name_mdb2 = $db->getIndexName($constraint_name);
@ -424,7 +424,10 @@ class MDB2_Driver_Reverse_pgsql extends MDB2_Driver_Reverse_Common
WHEN 24 THEN 'UPDATE, DELETE'
WHEN 12 THEN 'INSERT, DELETE'
END AS trigger_event,
trg.tgenabled AS trigger_enabled,
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,

@ -43,7 +43,7 @@
// | Lorenzo Alberton <l.alberton@quipo.it> |
// +----------------------------------------------------------------------+
//
// $Id: sqlite.php,v 1.78 2007/12/01 10:46:13 quipo Exp $
// $Id: sqlite.php,v 1.79 2008/03/05 11:08:53 quipo Exp $
//
require_once 'MDB2/Driver/Reverse/Common.php';
@ -393,6 +393,18 @@ class MDB2_Driver_Reverse_sqlite extends MDB2_Driver_Reverse_Common
}
return $definition;
}
if (preg_match("/\"([^\"]+)\"[^\,\"]+\bPRIMARY\s+KEY\b[^\,\)]*/i", $sql, $tmp)) {
$definition['primary'] = true;
$definition['fields'] = array();
$column_names = split(',', $tmp[1]);
$colpos = 1;
foreach ($column_names as $column_name) {
$definition['fields'][trim($column_name)] = array(
'position' => $colpos++
);
}
return $definition;
}
} else {
// search in table definition for FOREIGN KEYs
$pattern = "/\bCONSTRAINT\b\s+%s\s+

@ -3,7 +3,7 @@
// +----------------------------------------------------------------------+
// | PHP versions 4 and 5 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1998-2006 Manuel Lemos, Tomas V.V.Cox, |
// | Copyright (c) 1998-2008 Manuel Lemos, Tomas V.V.Cox, |
// | Stig. S. Bakken, Lukas Smith, Frank M. Kromann |
// | All rights reserved. |
// +----------------------------------------------------------------------+
@ -43,7 +43,7 @@
// | Author: Frank M. Kromann <frank@kromann.info> |
// +----------------------------------------------------------------------+
//
// $Id: mssql.php,v 1.161 2007/11/18 17:52:00 quipo Exp $
// $Id: mssql.php,v 1.174 2008/03/08 14:18:39 quipo Exp $
//
// {{{ Class MDB2_Driver_mssql
/**
@ -86,6 +86,7 @@ class MDB2_Driver_mssql extends MDB2_Driver_Common
$this->supported['LOBs'] = true;
$this->supported['replace'] = 'emulated';
$this->supported['sub_selects'] = true;
$this->supported['triggers'] = true;
$this->supported['auto_increment'] = true;
$this->supported['primary_key'] = true;
$this->supported['result_introspection'] = true;
@ -93,8 +94,11 @@ class MDB2_Driver_mssql extends MDB2_Driver_Common
$this->supported['pattern_escaping'] = true;
$this->supported['new_link'] = true;
$this->options['DBA_username'] = false;
$this->options['DBA_password'] = false;
$this->options['database_device'] = false;
$this->options['database_size'] = false;
$this->options['max_identifiers_length'] = 128; // MS Access: 64
}
// }}}
@ -107,11 +111,15 @@ class MDB2_Driver_mssql extends MDB2_Driver_Common
* @return array
* @access public
*/
function errorInfo($error = null)
function errorInfo($error = null, $connection = null)
{
if (is_null($connection)) {
$connection = $this->connection;
}
$native_code = null;
if ($this->connection) {
$result = @mssql_query('select @@ERROR as ErrorCode', $this->connection);
if ($connection) {
$result = @mssql_query('select @@ERROR as ErrorCode', $connection);
if ($result) {
$native_code = @mssql_result($result, 0, 0);
@mssql_free_result($result);
@ -136,8 +144,10 @@ class MDB2_Driver_mssql extends MDB2_Driver_Common
336 => MDB2_ERROR_SYNTAX,
515 => MDB2_ERROR_CONSTRAINT_NOT_NULL,
547 => MDB2_ERROR_CONSTRAINT,
911 => MDB2_ERROR_NOT_FOUND,
1018 => MDB2_ERROR_SYNTAX,
1035 => MDB2_ERROR_SYNTAX,
1801 => MDB2_ERROR_ALREADY_EXISTS,
1913 => MDB2_ERROR_ALREADY_EXISTS,
2209 => MDB2_ERROR_SYNTAX,
2223 => MDB2_ERROR_SYNTAX,
@ -296,39 +306,30 @@ class MDB2_Driver_mssql extends MDB2_Driver_Common
}
// }}}
// {{{ connect()
// {{{ _doConnect()
/**
* Connect to the database
* do the grunt work of the connect
*
* @return true on success, MDB2 Error Object on failure
* @return connection on success or MDB2 Error Object on failure
* @access protected
*/
function connect()
function _doConnect($username, $password, $persistent = false)
{
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);
}
if (!PEAR::loadExtension($this->phptype)) {
if (!PEAR::loadExtension($this->phptype) && !PEAR::loadExtension('sybase_ct')) {
return $this->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
'extension '.$this->phptype.' is not compiled into PHP', __FUNCTION__);
}
$params = array(
$this->dsn['hostspec'] ? $this->dsn['hostspec'] : 'localhost',
$this->dsn['username'] ? $this->dsn['username'] : null,
$this->dsn['password'] ? $this->dsn['password'] : null,
$username ? $username : null,
$password ? $password : null,
);
if ($this->dsn['port']) {
$params[0].= ((substr(PHP_OS, 0, 3) == 'WIN') ? ',' : ':').$this->dsn['port'];
}
if (!$this->options['persistent']) {
if (!$persistent) {
if (isset($this->dsn['new_link'])
&& ($this->dsn['new_link'] == 'true' || $this->dsn['new_link'] === true)
) {
@ -338,7 +339,7 @@ class MDB2_Driver_mssql extends MDB2_Driver_Common
}
}
$connect_function = $this->options['persistent'] ? 'mssql_pconnect' : 'mssql_connect';
$connect_function = $persistent ? 'mssql_pconnect' : 'mssql_connect';
$connection = @call_user_func_array($connect_function, $params);
if ($connection <= 0) {
@ -346,6 +347,8 @@ class MDB2_Driver_mssql extends MDB2_Driver_Common
'unable to establish a connection', __FUNCTION__, __FUNCTION__);
}
@mssql_query('SET ANSI_NULL_DFLT_ON ON', $connection);
if (!empty($this->dsn['charset'])) {
$result = $this->setCharset($this->dsn['charset'], $connection);
if (PEAR::isError($result)) {
@ -361,6 +364,38 @@ class MDB2_Driver_mssql extends MDB2_Driver_Common
@mssql_query('SET DATEFORMAT ymd', $connection);
}
return $connection;
}
// }}}
// {{{ connect()
/**
* Connect to the database
*
* @return true on success, MDB2 Error Object on failure
*/
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 = '';
@ -381,6 +416,41 @@ class MDB2_Driver_mssql extends MDB2_Driver_Common
return MDB2_OK;
}
// }}}
// {{{ 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 = @mssql_select_db($name, $connection);
$errorInfo = $this->errorInfo(null, $connection);
@mssql_close($connection);
if (!$result) {
if ($errorInfo[0] != MDB2_ERROR_NOT_FOUND) {
exit;
$result = $this->raiseError($errorInfo[0], null, null, $errorInfo[2], __FUNCTION__);
return $result;
}
$result = false;
}
return $result;
}
// }}}
// {{{ disconnect()
@ -416,6 +486,42 @@ class MDB2_Driver_mssql extends MDB2_Driver_Common
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);
}
@mssql_close($connection);
return $result;
}
// }}}
// {{{ _doQuery()
@ -546,7 +652,7 @@ class MDB2_Driver_mssql extends MDB2_Driver_Common
// cache server_info
$this->connected_server_info = $server_info;
if (!$native && !PEAR::isError($server_info)) {
if (preg_match('/([0-9]+)\.([0-9]+)\.([0-9]+)/', $server_info, $tmp)) {
if (preg_match('/(\d+)\.(\d+)\.(\d+)/', $server_info, $tmp)) {
$server_info = array(
'major' => $tmp[1],
'minor' => $tmp[2],
@ -609,6 +715,7 @@ class MDB2_Driver_mssql extends MDB2_Driver_Common
{
$sequence_name = $this->quoteIdentifier($this->getSequenceName($seq_name), true);
$seqcol_name = $this->quoteIdentifier($this->options['seqcol_name'], true);
$this->pushErrorHandling(PEAR_ERROR_RETURN);
$this->expectError(MDB2_ERROR_NOSUCHTABLE);
$seq_val = $this->_checkSequence($sequence_name);
@ -621,6 +728,7 @@ class MDB2_Driver_mssql extends MDB2_Driver_Common
}
$result =& $this->_doQuery($query, true);
$this->popExpect();
$this->popErrorHandling();
if (PEAR::isError($result)) {
if ($ondemand && !$this->_checkSequence($sequence_name)) {
$this->loadModule('Manager', null, true);
@ -669,6 +777,7 @@ class MDB2_Driver_mssql extends MDB2_Driver_Common
*
* @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
*/
@ -678,9 +787,12 @@ class MDB2_Driver_mssql extends MDB2_Driver_Common
if (is_array($server_info) && !is_null($server_info['major'])
&& $server_info['major'] >= 8
) {
$query = "SELECT SCOPE_IDENTITY()";
$query = "SELECT IDENT_CURRENT('$table')";
} else {
$query = "SELECT @@IDENTITY";
if (!is_null($table)) {
$query .= ' FROM '.$this->quoteIdentifier($table, true);
}
}
return $this->queryOne($query, 'integer');

@ -3,7 +3,7 @@
// +----------------------------------------------------------------------+
// | PHP versions 4 and 5 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1998-2006 Manuel Lemos, Tomas V.V.Cox, |
// | Copyright (c) 1998-2008 Manuel Lemos, Tomas V.V.Cox, |
// | Stig. S. Bakken, Lukas Smith |
// | All rights reserved. |
// +----------------------------------------------------------------------+
@ -43,7 +43,7 @@
// | Author: Lukas Smith <smith@pooteeweet.org> |
// +----------------------------------------------------------------------+
//
// $Id: mysql.php,v 1.195 2007/11/10 13:27:03 quipo Exp $
// $Id: mysql.php,v 1.208 2008/03/13 03:31:55 afz Exp $
//
/**
@ -98,6 +98,7 @@ class MDB2_Driver_mysql extends MDB2_Driver_Common
$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;
@ -106,7 +107,66 @@ class MDB2_Driver_mysql extends MDB2_Driver_Common
$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();
}
// }}}
@ -177,6 +237,7 @@ class MDB2_Driver_mysql extends MDB2_Driver_Common
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,
@ -195,6 +256,8 @@ class MDB2_Driver_mysql extends MDB2_Driver_Common
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) {
@ -422,26 +485,16 @@ class MDB2_Driver_mysql extends MDB2_Driver_Common
}
// }}}
// {{{ connect()
// {{{ _doConnect()
/**
* Connect to the database
* do the grunt work of the connect
*
* @return true on success, MDB2 Error Object on failure
* @return connection on success or MDB2 Error Object on failure
* @access protected
*/
function connect()
function _doConnect($username, $password, $persistent = false)
{
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']
&& $this->connected_database_name == $this->database_name
) {
return MDB2_OK;
}
$this->disconnect(false);
}
if (!PEAR::loadExtension($this->phptype)) {
return $this->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
'extension '.$this->phptype.' is not compiled into PHP', __FUNCTION__);
@ -457,9 +510,9 @@ class MDB2_Driver_mysql extends MDB2_Driver_Common
$params[0].= ':' . $this->dsn['port'];
}
}
$params[] = $this->dsn['username'] ? $this->dsn['username'] : null;
$params[] = $this->dsn['password'] ? $this->dsn['password'] : null;
if (!$this->options['persistent']) {
$params[] = $username ? $username : null;
$params[] = $password ? $password : null;
if (!$persistent) {
if (isset($this->dsn['new_link'])
&& ($this->dsn['new_link'] == 'true' || $this->dsn['new_link'] === true)
) {
@ -472,7 +525,7 @@ class MDB2_Driver_mysql extends MDB2_Driver_Common
$params[] = isset($this->dsn['client_flags'])
? $this->dsn['client_flags'] : null;
}
$connect_function = $this->options['persistent'] ? 'mysql_pconnect' : 'mysql_connect';
$connect_function = $persistent ? 'mysql_pconnect' : 'mysql_connect';
$connection = @call_user_func_array($connect_function, $params);
if (!$connection) {
@ -488,10 +541,44 @@ class MDB2_Driver_mysql extends MDB2_Driver_Common
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 = '';
@ -509,27 +596,6 @@ class MDB2_Driver_mysql extends MDB2_Driver_Common
}
}
$this->supported['transactions'] = $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['transactions'] = false;
$this->warnings[] = $this->options['default_table_type'] .
' is not a supported default table type';
break;
}
}
$this->_getServerCapabilities();
return MDB2_OK;
@ -541,7 +607,7 @@ class MDB2_Driver_mysql extends MDB2_Driver_Common
/**
* Set the charset on the current connection
*
* @param string charset
* @param string charset (or array(charset, collation))
* @param resource connection handle
*
* @return true on success, MDB2 Error Object on failure
@ -554,10 +620,44 @@ class MDB2_Driver_mysql extends MDB2_Driver_Common
return $connection;
}
}
$collation = null;
if (is_array($charset) && 2 == count($charset)) {
$collation = array_pop($charset);
$charset = array_pop($charset);
}
$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()
@ -593,6 +693,42 @@ class MDB2_Driver_mysql extends MDB2_Driver_Common
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()
@ -793,29 +929,39 @@ class MDB2_Driver_mysql extends MDB2_Driver_Common
//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)) {
if (!version_compare($server_info['major'].'.'.$server_info['minor'].'.'.$server_info['patch'], '4.1.0', '<')) {
$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;
}
if (!version_compare($server_info['major'].'.'.$server_info['minor'].'.'.$server_info['patch'], '4.0.14', '<')
|| !version_compare($server_info['major'].'.'.$server_info['minor'].'.'.$server_info['patch'], '4.1.1', '<')
) {
$this->supported['savepoints'] = 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_info['major'].'.'.$server_info['minor'].'.'.$server_info['patch'], '4.0.11', '<')) {
if (!version_compare($server_version, '4.0.11', '<')) {
$this->start_transaction = true;
}
if (!version_compare($server_info['major'].'.'.$server_info['minor'].'.'.$server_info['patch'], '5.0.3', '<')) {
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;
}
}
}
}
@ -958,7 +1104,8 @@ class MDB2_Driver_mysql extends MDB2_Driver_Common
return $connection;
}
static $prep_statement_counter = 1;
$statement_name = sprintf($this->options['statement_format'], $this->phptype, sha1(microtime() + mt_rand())) . $prep_statement_counter++;
$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)) {
@ -1049,7 +1196,7 @@ class MDB2_Driver_mysql extends MDB2_Driver_Common
$query .= ',';
$values.= ',';
}
$query.= $name;
$query.= $this->quoteIdentifier($name, true);
if (isset($fields[$name]['null']) && $fields[$name]['null']) {
$value = 'NULL';
} else {
@ -1078,6 +1225,7 @@ class MDB2_Driver_mysql extends MDB2_Driver_Common
return $connection;
}
$table = $this->quoteIdentifier($table, true);
$query = "REPLACE INTO $table ($query) VALUES ($values)";
$result =& $this->_doQuery($query, true, $connection);
if (PEAR::isError($result)) {
@ -1105,9 +1253,11 @@ class MDB2_Driver_mysql extends MDB2_Driver_Common
$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);

@ -43,7 +43,7 @@
// | Author: Lukas Smith <smith@pooteeweet.org> |
// +----------------------------------------------------------------------+
//
// $Id: mysqli.php,v 1.176 2007/11/10 13:27:03 quipo Exp $
// $Id: mysqli.php,v 1.188 2008/03/13 03:31:55 afz Exp $
//
/**
@ -98,6 +98,7 @@ class MDB2_Driver_mysqli extends MDB2_Driver_Common
$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;
@ -106,8 +107,67 @@ class MDB2_Driver_mysqli extends MDB2_Driver_Common
$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['multi_query'] = false;
$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();
}
// }}}
@ -178,6 +238,7 @@ class MDB2_Driver_mysqli extends MDB2_Driver_Common
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,
@ -196,6 +257,7 @@ class MDB2_Driver_mysqli extends MDB2_Driver_Common
1542 => MDB2_ERROR_CANNOT_DROP,
1546 => MDB2_ERROR_CONSTRAINT,
1582 => MDB2_ERROR_CONSTRAINT,
2003 => MDB2_ERROR_CONNECT_FAILED,
2019 => MDB2_ERROR_INVALID,
);
}
@ -420,23 +482,16 @@ class MDB2_Driver_mysqli extends MDB2_Driver_Common
}
// }}}
// {{{ connect()
// {{{ _doConnect()
/**
* Connect to the database
* do the grunt work of the connect
*
* @return true on success, MDB2 Error Object on failure
* @return connection on success or MDB2 Error Object on failure
* @access protected
*/
function connect()
function _doConnect($username, $password, $persistent = false)
{
if (is_object($this->connection)) {
//if (count(array_diff($this->connected_dsn, $this->dsn)) == 0) {
if (MDB2::areEquals($this->connected_dsn, $this->dsn)) {
return MDB2_OK;
}
$this->connection = 0;
}
if (!PEAR::loadExtension($this->phptype)) {
return $this->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
'extension '.$this->phptype.' is not compiled into PHP', __FUNCTION__);
@ -447,7 +502,6 @@ class MDB2_Driver_mysqli extends MDB2_Driver_Common
@mysqli_options($connection, MYSQLI_SET_CHARSET_NAME, $this->dsn['charset']);
}
if ($this->options['ssl']) {
@mysqli_ssl_set(
$connection,
@ -462,13 +516,12 @@ class MDB2_Driver_mysqli extends MDB2_Driver_Common
if (!@mysqli_real_connect(
$connection,
$this->dsn['hostspec'],
$this->dsn['username'],
$this->dsn['password'],
$username,
$password,
$this->database_name,
$this->dsn['port'],
$this->dsn['socket']
)) {
if (($err = @mysqli_connect_error()) != '') {
return $this->raiseError(null,
null, null, $err, __FUNCTION__);
@ -485,32 +538,40 @@ class MDB2_Driver_mysqli extends MDB2_Driver_Common
}
}
return $connection;
}
// }}}
// {{{ connect()
/**
* Connect to the database
*
* @return true on success, MDB2 Error Object on failure
*/
function connect()
{
if (is_object($this->connection)) {
//if (count(array_diff($this->connected_dsn, $this->dsn)) == 0) {
if (MDB2::areEquals($this->connected_dsn, $this->dsn)) {
return MDB2_OK;
}
$this->connection = 0;
}
$connection = $this->_doConnect(
$this->dsn['username'],
$this->dsn['password']
);
if (PEAR::isError($connection)) {
return $connection;
}
$this->connection = $connection;
$this->connected_dsn = $this->dsn;
$this->connected_database_name = $this->database_name;
$this->dbsyntax = $this->dsn['dbsyntax'] ? $this->dsn['dbsyntax'] : $this->phptype;
$this->supported['transactions'] = $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['transactions'] = false;
$this->warnings[] = $this->options['default_table_type'] .
' is not a supported default table type';
break;
}
}
$this->_getServerCapabilities();
return MDB2_OK;
@ -522,7 +583,7 @@ class MDB2_Driver_mysqli extends MDB2_Driver_Common
/**
* Set the charset on the current connection
*
* @param string charset
* @param string charset (or array(charset, collation))
* @param resource connection handle
*
* @return true on success, MDB2 Error Object on failure
@ -535,11 +596,19 @@ class MDB2_Driver_mysqli extends MDB2_Driver_Common
return $connection;
}
}
$collation = null;
if (is_array($charset) && 2 == count($charset)) {
$collation = array_pop($charset);
$charset = array_pop($charset);
}
$client_info = mysqli_get_client_version();
if (OS_WINDOWS && ((40111 > $client_info) ||
((50000 <= $client_info) && (50006 > $client_info)))
) {
$query = "SET NAMES '".mysqli_real_escape_string($connection, $charset)."'";
if (!is_null($collation)) {
$query .= " COLLATE '".mysqli_real_escape_string($connection, $collation)."'";
}
return $this->_doQuery($query, true, $connection);
}
if (!$result = mysqli_set_charset($connection, $charset)) {
@ -550,6 +619,31 @@ class MDB2_Driver_mysqli extends MDB2_Driver_Common
return $result;
}
// }}}
// {{{ 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']);
if (PEAR::isError($connection)) {
return $connection;
}
$result = @mysqli_select_db($connection, $name);
@mysqli_close($connection);
return $result;
}
// }}}
// {{{ disconnect()
@ -585,6 +679,42 @@ class MDB2_Driver_mysqli extends MDB2_Driver_Common
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);
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);
}
@mysqli_close($connection);
return $result;
}
// }}}
// {{{ _doQuery()
@ -803,29 +933,39 @@ class MDB2_Driver_mysqli extends MDB2_Driver_Common
//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)) {
if (!version_compare($server_info['major'].'.'.$server_info['minor'].'.'.$server_info['patch'], '4.1.0', '<')) {
$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;
}
if (!version_compare($server_info['major'].'.'.$server_info['minor'].'.'.$server_info['patch'], '4.0.14', '<')
|| !version_compare($server_info['major'].'.'.$server_info['minor'].'.'.$server_info['patch'], '4.1.1', '<')
) {
$this->supported['savepoints'] = 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_info['major'].'.'.$server_info['minor'].'.'.$server_info['patch'], '4.0.11', '<')) {
if (!version_compare($server_version, '4.0.11', '<')) {
$this->start_transaction = true;
}
if (!version_compare($server_info['major'].'.'.$server_info['minor'].'.'.$server_info['patch'], '5.0.3', '<')) {
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;
}
}
}
}
@ -970,7 +1110,8 @@ class MDB2_Driver_mysqli extends MDB2_Driver_Common
if (!$is_manip) {
static $prep_statement_counter = 1;
$statement_name = sprintf($this->options['statement_format'], $this->phptype, sha1(microtime() + mt_rand())) . $prep_statement_counter++;
$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);
@ -1071,7 +1212,7 @@ class MDB2_Driver_mysqli extends MDB2_Driver_Common
$query .= ',';
$values.= ',';
}
$query.= $name;
$query.= $this->quoteIdentifier($name, true);
if (isset($fields[$name]['null']) && $fields[$name]['null']) {
$value = 'NULL';
} else {
@ -1100,6 +1241,7 @@ class MDB2_Driver_mysqli extends MDB2_Driver_Common
return $connection;
}
$table = $this->quoteIdentifier($table, true);
$query = "REPLACE INTO $table ($query) VALUES ($values)";
$result =& $this->_doQuery($query, true, $connection);
if (PEAR::isError($result)) {
@ -1127,9 +1269,11 @@ class MDB2_Driver_mysqli extends MDB2_Driver_Common
$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);

@ -3,7 +3,7 @@
// +----------------------------------------------------------------------+
// | PHP versions 4 and 5 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1998-2006 Manuel Lemos, Tomas V.V.Cox, |
// | Copyright (c) 1998-2008 Manuel Lemos, Tomas V.V.Cox, |
// | Stig. S. Bakken, Lukas Smith |
// | All rights reserved. |
// +----------------------------------------------------------------------+
@ -43,7 +43,7 @@
// | Author: Paul Cooper <pgc@ucecom.com> |
// +----------------------------------------------------------------------+
//
// $Id: pgsql.php,v 1.186 2007/11/10 13:27:03 quipo Exp $
// $Id: pgsql.php,v 1.197 2008/03/08 14:18:39 quipo Exp $
/**
* MDB2 PostGreSQL driver
@ -83,6 +83,7 @@ class MDB2_Driver_pgsql extends MDB2_Driver_Common
$this->supported['LOBs'] = true;
$this->supported['replace'] = 'emulated';
$this->supported['sub_selects'] = true;
$this->supported['triggers'] = true;
$this->supported['auto_increment'] = 'emulated';
$this->supported['primary_key'] = true;
$this->supported['result_introspection'] = true;
@ -91,8 +92,11 @@ class MDB2_Driver_pgsql extends MDB2_Driver_Common
$this->supported['pattern_escaping'] = true;
$this->supported['new_link'] = true;
$this->options['DBA_username'] = false;
$this->options['DBA_password'] = false;
$this->options['multi_query'] = false;
$this->options['disable_smart_seqname'] = false;
$this->options['max_identifiers_length'] = 63;
}
// }}}
@ -119,6 +123,8 @@ class MDB2_Driver_pgsql extends MDB2_Driver_Common
$native_msg = 'Database connection has been lost.';
$error_code = MDB2_ERROR_CONNECT_FAILED;
}
} else {
$native_msg = @pg_last_error();
}
static $error_regexps;
@ -128,8 +134,12 @@ class MDB2_Driver_pgsql extends MDB2_Driver_Common
=> MDB2_ERROR_NOSUCHFIELD,
'/(relation|sequence|table).*does not exist|class .* not found/i'
=> MDB2_ERROR_NOSUCHTABLE,
'/database .* does not exist/'
=> MDB2_ERROR_NOT_FOUND,
'/index .* does not exist/'
=> MDB2_ERROR_NOT_FOUND,
'/database .* already exists/i'
=> MDB2_ERROR_ALREADY_EXISTS,
'/relation .* already exists/i'
=> MDB2_ERROR_ALREADY_EXISTS,
'/(divide|division) by zero$/i'
@ -202,7 +212,7 @@ class MDB2_Driver_pgsql extends MDB2_Driver_Common
if (PEAR::isError($connection)) {
return $connection;
}
if (version_compare(PHP_VERSION, '5.2.0RC5', '>=')) {
if (is_resource($connection) && version_compare(PHP_VERSION, '5.2.0RC5', '>=')) {
$text = @pg_escape_string($connection, $text);
} else {
$text = @pg_escape_string($text);
@ -353,13 +363,18 @@ class MDB2_Driver_pgsql extends MDB2_Driver_Common
// {{{ _doConnect()
/**
* Does the grunt work of connecting to the database
* Do the grunt work of connecting to the database
*
* @return mixed connection resource on success, MDB2 Error Object on failure
* @access protected
**/
function _doConnect($database_name, $persistent = false)
*/
function _doConnect($username, $password, $database_name, $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__);
}
if ($database_name == '') {
$database_name = 'template1';
}
@ -386,11 +401,11 @@ class MDB2_Driver_pgsql extends MDB2_Driver_Common
if ($database_name) {
$params[0].= ' dbname=\'' . addslashes($database_name) . '\'';
}
if ($this->dsn['username']) {
$params[0].= ' user=\'' . addslashes($this->dsn['username']) . '\'';
if ($username) {
$params[0].= ' user=\'' . addslashes($username) . '\'';
}
if ($this->dsn['password']) {
$params[0].= ' password=\'' . addslashes($this->dsn['password']) . '\'';
if ($password) {
$params[0].= ' password=\'' . addslashes($password) . '\'';
}
if (!empty($this->dsn['options'])) {
$params[0].= ' options=' . $this->dsn['options'];
@ -417,7 +432,6 @@ class MDB2_Driver_pgsql extends MDB2_Driver_Common
}
$connect_function = $persistent ? 'pg_pconnect' : 'pg_connect';
$connection = @call_user_func_array($connect_function, $params);
if (!$connection) {
return $this->raiseError(MDB2_ERROR_CONNECT_FAILED, null, null,
@ -449,7 +463,7 @@ class MDB2_Driver_pgsql extends MDB2_Driver_Common
*
* @return true on success, MDB2 Error Object on failure
* @access public
**/
*/
function connect()
{
if (is_resource($this->connection)) {
@ -463,22 +477,22 @@ class MDB2_Driver_pgsql extends MDB2_Driver_Common
$this->disconnect(false);
}
if (!PEAR::loadExtension($this->phptype)) {
return $this->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
'extension '.$this->phptype.' is not compiled into PHP', __FUNCTION__);
}
if ($this->database_name) {
$connection = $this->_doConnect($this->database_name, $this->options['persistent']);
$connection = $this->_doConnect($this->dsn['username'],
$this->dsn['password'],
$this->database_name,
$this->options['persistent']);
if (PEAR::isError($connection)) {
return $connection;
}
$this->connection = $connection;
$this->connected_dsn = $this->dsn;
$this->connected_database_name = $this->database_name;
$this->opened_persistent = $this->options['persistent'];
$this->dbsyntax = $this->dsn['dbsyntax'] ? $this->dsn['dbsyntax'] : $this->phptype;
}
return MDB2_OK;
}
@ -501,7 +515,10 @@ class MDB2_Driver_pgsql extends MDB2_Driver_Common
return $connection;
}
}
if (is_array($charset)) {
$charset = array_shift($charset);
$this->warnings[] = 'postgresql does not support setting client collation';
}
$result = @pg_set_client_encoding($connection, $charset);
if ($result == -1) {
return $this->raiseError(null, null, null,
@ -510,6 +527,30 @@ class MDB2_Driver_pgsql extends MDB2_Driver_Common
return MDB2_OK;
}
// }}}
// {{{ 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)
{
$res = $this->_doConnect($this->dsn['username'],
$this->dsn['password'],
$this->escape($name),
$this->options['persistent']);
if (!PEAR::isError($res)) {
return true;
}
return false;
}
// }}}
// {{{ disconnect()
@ -560,11 +601,11 @@ class MDB2_Driver_pgsql extends MDB2_Driver_Common
*/
function &standaloneQuery($query, $types = null, $is_manip = false)
{
$connection = $this->_doConnect('template1', 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->database_name, $this->options['persistent']);
if (PEAR::isError($connection)) {
$err =& $this->raiseError(MDB2_ERROR_CONNECT_FAILED, null, null,
'Cannot connect to template1', __FUNCTION__);
return $err;
return $connection;
}
$offset = $this->offset;
@ -572,17 +613,16 @@ class MDB2_Driver_pgsql extends MDB2_Driver_Common
$this->offset = $this->limit = 0;
$query = $this->_modifyQuery($query, $is_manip, $limit, $offset);
$result =& $this->_doQuery($query, $is_manip, $connection, false);
@pg_close($connection);
if (PEAR::isError($result)) {
return $result;
}
$result =& $this->_doQuery($query, $is_manip, $connection, $this->database_name);
if (!PEAR::isError($result)) {
if ($is_manip) {
$affected_rows = $this->_affectedRows($connection, $result);
return $affected_rows;
}
$result = $this->_affectedRows($connection, $result);
} else {
$result =& $this->_wrapResult($result, $types, true, false, $limit, $offset);
}
}
@pg_close($connection);
return $result;
}
@ -910,8 +950,8 @@ class MDB2_Driver_pgsql extends MDB2_Driver_Common
return $connection;
}
static $prep_statement_counter = 1;
$statement_name = sprintf($this->options['statement_format'], $this->phptype, sha1(microtime() + mt_rand())) . $prep_statement_counter++;
$statement_name = strtolower($statement_name);
$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']);
if ($pgtypes === false) {
$result = @pg_prepare($connection, $statement_name, $query);
if (!$result) {
@ -975,7 +1015,7 @@ class MDB2_Driver_pgsql extends MDB2_Driver_Common
WHERE d.adrelid = a.attrelid
AND d.adnum = a.attnum
AND a.atthasdef
) FROM 'nextval[^\']*\'([^\']*)')
) FROM 'nextval[^'']*''([^'']*)')
FROM pg_attribute a
LEFT JOIN pg_class c ON c.oid = a.attrelid
LEFT JOIN pg_attrdef d ON d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef
@ -1017,9 +1057,11 @@ class MDB2_Driver_pgsql extends MDB2_Driver_Common
{
$sequence_name = $this->quoteIdentifier($this->getSequenceName($seq_name), true);
$query = "SELECT NEXTVAL('$sequence_name')";
$this->pushErrorHandling(PEAR_ERROR_RETURN);
$this->expectError(MDB2_ERROR_NOSUCHTABLE);
$result = $this->queryOne($query, 'integer');
$this->popExpect();
$this->popErrorHandling();
if (PEAR::isError($result)) {
if ($ondemand && $result->getCode() == MDB2_ERROR_NOSUCHTABLE) {
$this->loadModule('Manager', null, true);
@ -1052,7 +1094,7 @@ class MDB2_Driver_pgsql extends MDB2_Driver_Common
return $this->queryOne('SELECT lastval()', 'integer');
}
$seq = $table.(empty($field) ? '' : '_'.$field);
$sequence_name = $this->getSequenceName($seq);
$sequence_name = $this->quoteIdentifier($this->getSequenceName($seq), true);
return $this->queryOne("SELECT currval('$sequence_name')", 'integer');
}

@ -3,7 +3,7 @@
// +----------------------------------------------------------------------+
// | PHP versions 4 and 5 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1998-2007 Manuel Lemos, Tomas V.V.Cox, |
// | Copyright (c) 1998-2008 Manuel Lemos, Tomas V.V.Cox, |
// | Stig. S. Bakken, Lukas Smith |
// | All rights reserved. |
// +----------------------------------------------------------------------+
@ -43,7 +43,7 @@
// | Author: Lukas Smith <smith@pooteeweet.org> |
// +----------------------------------------------------------------------+
//
// $Id: sqlite.php,v 1.152 2007/11/27 07:44:41 quipo Exp $
// $Id: sqlite.php,v 1.158 2008/03/08 14:18:39 quipo Exp $
//
/**
@ -89,6 +89,7 @@ class MDB2_Driver_sqlite extends MDB2_Driver_Common
$this->supported['transactions'] = true;
$this->supported['savepoints'] = false;
$this->supported['sub_selects'] = true;
$this->supported['triggers'] = true;
$this->supported['auto_increment'] = true;
$this->supported['primary_key'] = false; // requires alter table implementation
$this->supported['result_introspection'] = false; // not implemented
@ -97,11 +98,14 @@ class MDB2_Driver_sqlite extends MDB2_Driver_Common
$this->supported['pattern_escaping'] = false;
$this->supported['new_link'] = false;
$this->options['DBA_username'] = false;
$this->options['DBA_password'] = false;
$this->options['base_transaction_name'] = '___php_MDB2_sqlite_auto_commit_off';
$this->options['fixed_float'] = 0;
$this->options['database_path'] = '';
$this->options['database_extension'] = '';
$this->options['server_version'] = '';
$this->options['max_identifiers_length'] = 128; //no real limit
}
// }}}
@ -125,7 +129,7 @@ class MDB2_Driver_sqlite extends MDB2_Driver_Common
// PHP 5.2+ prepends the function name to $php_errormsg, so we need
// this hack to work around it, per bug #9599.
$native_msg = preg_replace('/^sqlite[a-z_]+\(\): /', '', $native_msg);
$native_msg = preg_replace('/^sqlite[a-z_]+\(\)[^:]*: /', '', $native_msg);
if (is_null($error)) {
static $error_regexps;
@ -424,6 +428,24 @@ class MDB2_Driver_sqlite extends MDB2_Driver_Common
return MDB2_OK;
}
// }}}
// {{{ 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)
{
$database_file = $this->_getDatabaseFile($name);
$result = file_exists($database_file);
return $result;
}
// }}}
// {{{ disconnect()
@ -721,7 +743,7 @@ class MDB2_Driver_sqlite extends MDB2_Driver_Common
$query .= ',';
$values.= ',';
}
$query.= $name;
$query.= $this->quoteIdentifier($name, true);
if (isset($fields[$name]['null']) && $fields[$name]['null']) {
$value = 'NULL';
} else {
@ -750,6 +772,7 @@ class MDB2_Driver_sqlite extends MDB2_Driver_Common
return $connection;
}
$table = $this->quoteIdentifier($table, true);
$query = "REPLACE INTO $table ($query) VALUES ($values)";
$result =& $this->_doQuery($query, true, $connection);
if (PEAR::isError($result)) {
@ -777,9 +800,11 @@ class MDB2_Driver_sqlite extends MDB2_Driver_Common
$sequence_name = $this->quoteIdentifier($this->getSequenceName($seq_name), true);
$seqcol_name = $this->options['seqcol_name'];
$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);

Loading…
Cancel
Save