2
0
forked from Wavyzz/dolibarr

Mise jour de Pear MDB2

This commit is contained in:
Regis Houssin
2007-08-17 08:24:13 +00:00
parent 275121d719
commit 1e99ea3005
30 changed files with 2417 additions and 632 deletions

View File

@@ -2,7 +2,7 @@
// +----------------------------------------------------------------------+ // +----------------------------------------------------------------------+
// | PHP versions 4 and 5 | // | PHP versions 4 and 5 |
// +----------------------------------------------------------------------+ // +----------------------------------------------------------------------+
// | Copyright (c) 1998-2006 Manuel Lemos, Tomas V.V.Cox, | // | Copyright (c) 1998-2007 Manuel Lemos, Tomas V.V.Cox, |
// | Stig. S. Bakken, Lukas Smith | // | Stig. S. Bakken, Lukas Smith |
// | All rights reserved. | // | All rights reserved. |
// +----------------------------------------------------------------------+ // +----------------------------------------------------------------------+
@@ -44,8 +44,7 @@
// //
// $Id$ // $Id$
//require_once 'MDB2/LOB.php'; require_once 'MDB2/LOB.php';
require_once PEAR_PATH."/MDB2/LOB.php";
/** /**
* @package MDB2 * @package MDB2
@@ -129,7 +128,7 @@ class MDB2_Driver_Datatype_Common extends MDB2_Module_Common
* function is not called, the type of all result set columns is assumed * function is not called, the type of all result set columns is assumed
* to be text, thus leading to not perform any conversions. * to be text, thus leading to not perform any conversions.
* *
* @param string $types array variable that lists the * @param array $types array variable that lists the
* data types to be expected in the result set columns. If this array * data types to be expected in the result set columns. If this array
* contains less types than the number of columns that are returned * contains less types than the number of columns that are returned
* in the result set, the remaining columns are assumed to be of the * in the result set, the remaining columns are assumed to be of the
@@ -160,11 +159,12 @@ class MDB2_Driver_Datatype_Common extends MDB2_Module_Common
// {{{ _baseConvertResult() // {{{ _baseConvertResult()
/** /**
* general type conversion method * General type conversion method
* *
* @param mixed $value refernce to a value to be converted * @param mixed $value reference to a value to be converted
* @param string $type specifies which type to convert to * @param string $type specifies which type to convert to
* @return object a MDB2 error on failure * @param boolean $rtrim [optional] when TRUE [default], apply rtrim() to text
* @return object an MDB2 error on failure
* @access protected * @access protected
*/ */
function _baseConvertResult($value, $type, $rtrim = true) function _baseConvertResult($value, $type, $rtrim = true)
@@ -219,11 +219,11 @@ class MDB2_Driver_Datatype_Common extends MDB2_Module_Common
// {{{ convertResult() // {{{ convertResult()
/** /**
* convert a value to a RDBMS indepdenant MDB2 type * Convert a value to a RDBMS indipendent MDB2 type
* *
* @param mixed $value value to be converted * @param mixed $value value to be converted
* @param string $type specifies which type to convert to * @param string $type specifies which type to convert to
* @param bool $rtrim if to rtrim text values or not * @param boolean $rtrim [optional] when TRUE [default], apply rtrim() to text
* @return mixed converted value * @return mixed converted value
* @access public * @access public
*/ */
@@ -249,6 +249,34 @@ class MDB2_Driver_Datatype_Common extends MDB2_Module_Common
// }}} // }}}
// {{{ convertResultRow() // {{{ convertResultRow()
/**
* Convert a result row
*
* @param array $types
* @param array $row specifies the types to convert to
* @param boolean $rtrim [optional] when TRUE [default], apply rtrim() to text
* @return mixed MDB2_OK on success, an MDB2 error on failure
* @access public
*/
function convertResultRow($types, $row, $rtrim = true)
{
$types = $this->_sortResultFieldTypes(array_keys($row), $types);
foreach ($row as $key => $value) {
if (empty($types[$key])) {
continue;
}
$value = $this->convertResult($row[$key], $types[$key], $rtrim);
if (PEAR::isError($value)) {
return $value;
}
$row[$key] = $value;
}
return $row;
}
// }}}
// {{{ _sortResultFieldTypes()
/** /**
* convert a result row * convert a result row
* *
@@ -258,32 +286,37 @@ class MDB2_Driver_Datatype_Common extends MDB2_Module_Common
* @return mixed MDB2_OK on success, a MDB2 error on failure * @return mixed MDB2_OK on success, a MDB2 error on failure
* @access public * @access public
*/ */
function convertResultRow($types, $row, $rtrim = true) function _sortResultFieldTypes($columns, $types)
{ {
$n_cols = count($columns);
$n_types = count($types);
if ($n_cols > $n_types) {
for ($i= $n_cols - $n_types; $i >= 0; $i--) {
$types[] = null;
}
}
$sorted_types = array();
foreach ($columns as $col) {
$sorted_types[$col] = null;
}
foreach ($types as $name => $type) {
if (array_key_exists($name, $sorted_types)) {
$sorted_types[$name] = $type;
unset($types[$name]);
}
}
// if there are left types in the array, fill the null values of the
// sorted array with them, in order.
if (count($types)) {
reset($types); reset($types);
$current_column = -1; foreach (array_keys($sorted_types) as $k) {
foreach ($row as $key => $value) { if (is_null($sorted_types[$k])) {
++$current_column; $sorted_types[$k] = current($types);
if (!isset($value)) {
continue;
}
if (isset($types[$current_column])) {
$type = $types[$current_column];
} elseif (isset($types[$key])) {
$type = $types[$key];
} elseif (current($types)) {
$type = current($types);
next($types); next($types);
} else {
continue;
} }
$value = $this->convertResult($row[$key], $type, $rtrim);
if (PEAR::isError($value)) {
return $value;
} }
$row[$key] = $value;
} }
return $row; return $sorted_types;
} }
// }}} // }}}
@@ -313,6 +346,7 @@ class MDB2_Driver_Datatype_Common extends MDB2_Module_Common
$parameter = array('type' => $type, 'name' => $name, 'field' => $field); $parameter = array('type' => $type, 'name' => $name, 'field' => $field);
return call_user_func_array($db->options['datatype_map_callback'][$type], array(&$db, __FUNCTION__, $parameter)); return call_user_func_array($db->options['datatype_map_callback'][$type], array(&$db, __FUNCTION__, $parameter));
} }
$field['type'] = $type;
} }
if (!method_exists($this, "_get{$type}Declaration")) { if (!method_exists($this, "_get{$type}Declaration")) {
@@ -411,7 +445,7 @@ class MDB2_Driver_Datatype_Common extends MDB2_Module_Common
* collation * collation
* Text value with the default COLLATION for this field. * Text value with the default COLLATION for this field.
* @return string DBMS specific SQL code portion that should be used to * @return string DBMS specific SQL code portion that should be used to
* declare the specified field. * declare the specified field, or a MDB2_Error on failure
* @access protected * @access protected
*/ */
function _getDeclaration($name, $field) function _getDeclaration($name, $field)
@@ -421,11 +455,58 @@ class MDB2_Driver_Datatype_Common extends MDB2_Module_Common
return $db; return $db;
} }
$name = $db->quoteIdentifier($name, true);
$declaration_options = $db->datatype->_getDeclarationOptions($field);
if (PEAR::isError($declaration_options)) {
return $declaration_options;
}
return $name.' '.$this->getTypeDeclaration($field).$declaration_options;
}
// }}}
// {{{ _getDeclarationOptions()
/**
* Obtain DBMS specific SQL code portion needed to declare a generic type
* field to be used in statement like CREATE TABLE, without the field name
* and type values (ie. just the character set, default value, if the
* field is permitted to be NULL or not, and the collation options).
*
* @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:
*
* 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.
* charset
* Text value with the default CHARACTER SET for this field.
* collation
* Text value with the default COLLATION for this field.
* @return string DBMS specific SQL code portion that should be used to
* declare the specified field's options.
* @access protected
*/
function _getDeclarationOptions($field)
{
$charset = empty($field['charset']) ? '' :
' '.$this->_getCharsetFieldDeclaration($field['charset']);
$default = ''; $default = '';
if (array_key_exists('default', $field)) { if (array_key_exists('default', $field)) {
if ($field['default'] === '') { if ($field['default'] === '') {
$field['default'] = empty($field['notnull']) $db =& $this->getDBInstance();
? null : $this->valid_default_values[$field['type']]; 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'] === '' if ($field['default'] === ''
&& ($db->options['portability'] & MDB2_PORTABILITY_EMPTY_TO_NULL) && ($db->options['portability'] & MDB2_PORTABILITY_EMPTY_TO_NULL)
) { ) {
@@ -437,15 +518,11 @@ class MDB2_Driver_Datatype_Common extends MDB2_Module_Common
$default = ' DEFAULT NULL'; $default = ' DEFAULT NULL';
} }
$charset = empty($field['charset']) ? '' : $notnull = empty($field['notnull']) ? '' : ' NOT NULL';
' '.$this->_getCharsetFieldDeclaration($field['charset']);
$collation = empty($field['collation']) ? '' : $collation = empty($field['collation']) ? '' :
' '.$this->_getCollationFieldDeclaration($field['collation']); ' '.$this->_getCollationFieldDeclaration($field['collation']);
return $charset.$default.$notnull.$collation;
$notnull = empty($field['notnull']) ? '' : ' NOT NULL';
$name = $db->quoteIdentifier($name, true);
return $name.' '.$this->getTypeDeclaration($field).$charset.$default.$notnull.$collation;
} }
// }}} // }}}
@@ -805,7 +882,11 @@ class MDB2_Driver_Datatype_Common extends MDB2_Module_Common
if (PEAR::isError($db)) { if (PEAR::isError($db)) {
return $db; return $db;
} }
if (!empty($db->options['datatype_map_callback'][$type])) {
$parameter = array('current' => $current, 'previous' => $previous);
$change = call_user_func_array($db->options['datatype_map_callback'][$type], array(&$db, __FUNCTION__, $parameter));
return $change;
}
return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
'type "'.$current['type'].'" is not yet supported', __FUNCTION__); 'type "'.$current['type'].'" is not yet supported', __FUNCTION__);
} }
@@ -1146,6 +1227,9 @@ class MDB2_Driver_Datatype_Common extends MDB2_Module_Common
} }
$value = $db->escape($value, $escape_wildcards); $value = $db->escape($value, $escape_wildcards);
if (PEAR::isError($value)) {
return $value;
}
return "'".$value."'"; return "'".$value."'";
} }
@@ -1208,6 +1292,9 @@ class MDB2_Driver_Datatype_Common extends MDB2_Module_Common
function _quoteLOB($value, $quote, $escape_wildcards) function _quoteLOB($value, $quote, $escape_wildcards)
{ {
$value = $this->_readFile($value); $value = $this->_readFile($value);
if (PEAR::isError($value)) {
return $value;
}
return $this->_quoteText($value, $quote, $escape_wildcards); return $this->_quoteText($value, $quote, $escape_wildcards);
} }
@@ -1399,6 +1486,7 @@ class MDB2_Driver_Datatype_Common extends MDB2_Module_Common
function _quoteDecimal($value, $quote, $escape_wildcards) function _quoteDecimal($value, $quote, $escape_wildcards)
{ {
$value = (string)$value; $value = (string)$value;
$value = preg_replace('/[^\d\.,\-+eE]/', '', $value);
if (preg_match('/[^.0-9]/', $value)) { if (preg_match('/[^.0-9]/', $value)) {
if (strpos($value, ',')) { if (strpos($value, ',')) {
// 1000,00 // 1000,00
@@ -1617,7 +1705,7 @@ class MDB2_Driver_Datatype_Common extends MDB2_Module_Common
'case insensitive LIKE matching requires passing the field name', __FUNCTION__); 'case insensitive LIKE matching requires passing the field name', __FUNCTION__);
} }
$db->loadModule('Function', null, true); $db->loadModule('Function', null, true);
$match = $db->function->lower($field).' '.'LIKE '; $match = $db->function->lower($field).' LIKE ';
break; break;
// case sensitive // case sensitive
case 'LIKE': case 'LIKE':
@@ -1636,7 +1724,11 @@ class MDB2_Driver_Datatype_Common extends MDB2_Module_Common
if ($operator === 'ILIKE') { if ($operator === 'ILIKE') {
$value = strtolower($value); $value = strtolower($value);
} }
$match.= $db->escapePattern($db->escape($value)); $escaped = $db->escape($value);
if (PEAR::isError($escaped)) {
return $escaped;
}
$match.= $db->escapePattern($escaped);
} }
} }
$match.= "'"; $match.= "'";
@@ -1681,6 +1773,35 @@ class MDB2_Driver_Datatype_Common extends MDB2_Module_Common
return $db; return $db;
} }
// If the user has specified an option to map the native field
// type to a custom MDB2 datatype...
$db_type = strtok($field['type'], '(), ');
if (!empty($db->options['nativetype_map_callback'][$db_type])) {
return call_user_func_array($db->options['nativetype_map_callback'][$db_type], array($db, $field));
}
// Otherwise perform the built-in (i.e. normal) MDB2 native type to
// MDB2 datatype conversion
return $this->_mapNativeDatatype($field);
}
// }}}
// {{{ _mapNativeDatatype()
/**
* Maps a native array description of a field to a MDB2 datatype and length
*
* @param array $field native field description
* @return array containing the various possible types, length, sign, fixed
* @access public
*/
function _mapNativeDatatype($field)
{
$db =& $this->getDBInstance();
if (PEAR::isError($db)) {
return $db;
}
return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
'method not implemented', __FUNCTION__); 'method not implemented', __FUNCTION__);
} }
@@ -1713,5 +1834,4 @@ class MDB2_Driver_Datatype_Common extends MDB2_Module_Common
return $type; return $type;
} }
} }
?> ?>

View File

@@ -3,7 +3,7 @@
// +----------------------------------------------------------------------+ // +----------------------------------------------------------------------+
// | PHP versions 4 and 5 | // | PHP versions 4 and 5 |
// +----------------------------------------------------------------------+ // +----------------------------------------------------------------------+
// | Copyright (c) 1998-2006 Manuel Lemos, Tomas V.V.Cox, | // | Copyright (c) 1998-2007 Manuel Lemos, Tomas V.V.Cox, |
// | Stig. S. Bakken, Lukas Smith | // | Stig. S. Bakken, Lukas Smith |
// | All rights reserved. | // | All rights reserved. |
// +----------------------------------------------------------------------+ // +----------------------------------------------------------------------+
@@ -47,8 +47,7 @@
// $Id$ // $Id$
// //
//require_once 'MDB2/Driver/Datatype/Common.php'; require_once 'MDB2/Driver/Datatype/Common.php';
require_once PEAR_PATH."/MDB2/Driver/Datatype/Common.php";
/** /**
* MDB2 MS SQL driver * MDB2 MS SQL driver
@@ -59,17 +58,18 @@ require_once PEAR_PATH."/MDB2/Driver/Datatype/Common.php";
*/ */
class MDB2_Driver_Datatype_mssql extends MDB2_Driver_Datatype_Common class MDB2_Driver_Datatype_mssql extends MDB2_Driver_Datatype_Common
{ {
// {{{ convertResult() // {{{ _baseConvertResult()
/** /**
* convert a value to a RDBMS indepdenant MDB2 type * general type conversion method
* *
* @param mixed $value value to be converted * @param mixed $value refernce to a value to be converted
* @param int $type constant that specifies which type to convert to * @param string $type specifies which type to convert to
* @return mixed converted value * @param boolean $rtrim [optional] when TRUE [default], apply rtrim() to text
* @access public * @return object a MDB2 error on failure
* @access protected
*/ */
function convertResult($value, $type) function _baseConvertResult($value, $type, $rtrim = true)
{ {
if (is_null($value)) { if (is_null($value)) {
return null; return null;
@@ -87,9 +87,8 @@ class MDB2_Driver_Datatype_mssql extends MDB2_Driver_Datatype_Common
$value = substr($value,11,8); $value = substr($value,11,8);
} }
return $value; return $value;
default:
return $this->_baseConvertResult($value,$type);
} }
return parent::_baseConvertResult($value, $type, $rtrim);
} }
// }}} // }}}
// {{{ getTypeDeclaration() // {{{ getTypeDeclaration()
@@ -161,11 +160,205 @@ class MDB2_Driver_Datatype_mssql extends MDB2_Driver_Datatype_Common
return 'FLOAT'; return 'FLOAT';
case 'decimal': case 'decimal':
$length = !empty($field['length']) ? $field['length'] : 18; $length = !empty($field['length']) ? $field['length'] : 18;
return 'DECIMAL('.$length.','.$db->options['decimal_places'].')'; $scale = !empty($field['scale']) ? $field['scale'] : $db->options['decimal_places'];
return 'DECIMAL('.$length.','.$scale.')';
} }
return ''; 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()
/**
* Obtain DBMS specific SQL code portion needed to declare an integer 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
* Integer 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 _getIntegerDeclaration($name, $field)
{
$db =& $this->getDBInstance();
if (PEAR::isError($db)) {
return $db;
}
$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;
}
$default = ' DEFAULT '.$this->quote($field['default'], 'integer');
} elseif (empty($field['notnull'])) {
$default = ' DEFAULT NULL';
}
$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;
}
// }}}
// {{{ _getCLOBDeclaration()
/**
* Obtain DBMS specific SQL code portion needed to declare an character
* large object 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 large
* object field. If this argument is missing the field should be
* declared to have the longest length allowed by the DBMS.
*
* 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 public
*/
function _getCLOBDeclaration($name, $field)
{
$db =& $this->getDBInstance();
if (PEAR::isError($db)) {
return $db;
}
$notnull = empty($field['notnull']) ? ' NULL' : ' NOT NULL';
$name = $db->quoteIdentifier($name, true);
return $name.' '.$this->getTypeDeclaration($field).$notnull;
}
// }}}
// {{{ _getBLOBDeclaration()
/**
* Obtain DBMS specific SQL code portion needed to declare an binary large
* object 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 large
* object field. If this argument is missing the field should be
* declared to have the longest length allowed by the DBMS.
*
* 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 _getBLOBDeclaration($name, $field)
{
$db =& $this->getDBInstance();
if (PEAR::isError($db)) {
return $db;
}
$notnull = empty($field['notnull']) ? ' NULL' : ' NOT NULL';
$name = $db->quoteIdentifier($name, true);
return $name.' '.$this->getTypeDeclaration($field).$notnull;
}
// }}} // }}}
// {{{ _quoteBLOB() // {{{ _quoteBLOB()
@@ -175,11 +368,12 @@ class MDB2_Driver_Datatype_mssql extends MDB2_Driver_Datatype_Common
* *
* @param string $value text string value that is intended to be converted. * @param string $value text string value that is intended to be converted.
* @param bool $quote determines if the value should be quoted and escaped * @param bool $quote determines if the value should be quoted and escaped
* @param bool $escape_wildcards if to escape escape wildcards
* @return string text string that represents the given argument value in * @return string text string that represents the given argument value in
* a DBMS specific format. * a DBMS specific format.
* @access protected * @access protected
*/ */
function _quoteBLOB($value, $quote) function _quoteBLOB($value, $quote, $escape_wildcards)
{ {
if (!$quote) { if (!$quote) {
return $value; return $value;
@@ -189,7 +383,7 @@ class MDB2_Driver_Datatype_mssql extends MDB2_Driver_Datatype_Common
} }
// }}} // }}}
// {{{ mapNativeDatatype() // {{{ _mapNativeDatatype()
/** /**
* Maps a native array description of a field to a MDB2 datatype and length * Maps a native array description of a field to a MDB2 datatype and length
@@ -198,13 +392,11 @@ class MDB2_Driver_Datatype_mssql extends MDB2_Driver_Datatype_Common
* @return array containing the various possible types, length, sign, fixed * @return array containing the various possible types, length, sign, fixed
* @access public * @access public
*/ */
function mapNativeDatatype($field) function _mapNativeDatatype($field)
{ {
// todo: handle length of various int variations
$db_type = preg_replace('/\d/', '', strtolower($field['type'])); $db_type = preg_replace('/\d/', '', strtolower($field['type']));
$length = $field['length']; $length = $field['length'];
if ((int)$length <= 0) {
$length = null;
}
$type = array(); $type = array();
// todo: unsigned handling seems to be missing // todo: unsigned handling seems to be missing
$unsigned = $fixed = null; $unsigned = $fixed = null;
@@ -212,8 +404,21 @@ class MDB2_Driver_Datatype_mssql extends MDB2_Driver_Datatype_Common
case 'bit': case 'bit':
$type[0] = 'boolean'; $type[0] = 'boolean';
break; break;
case 'tinyint':
$type[0] = 'integer';
$length = 1;
break;
case 'smallint':
$type[0] = 'integer';
$length = 2;
break;
case 'int': case 'int':
$type[0] = 'integer'; $type[0] = 'integer';
$length = 4;
break;
case 'bigint':
$type[0] = 'integer';
$length = 8;
break; break;
case 'datetime': case 'datetime':
$type[0] = 'timestamp'; $type[0] = 'timestamp';
@@ -226,6 +431,7 @@ class MDB2_Driver_Datatype_mssql extends MDB2_Driver_Datatype_Common
case 'decimal': case 'decimal':
case 'money': case 'money':
$type[0] = 'decimal'; $type[0] = 'decimal';
$length = $field['numeric_precision'].','.$field['numeric_scale'];
break; break;
case 'text': case 'text':
case 'varchar': case 'varchar':
@@ -234,7 +440,7 @@ class MDB2_Driver_Datatype_mssql extends MDB2_Driver_Datatype_Common
$type[0] = 'text'; $type[0] = 'text';
if ($length == '1') { if ($length == '1') {
$type[] = 'boolean'; $type[] = 'boolean';
if (preg_match('/^[is|has]/', $field['name'])) { if (preg_match('/^(is|has)/', $field['name'])) {
$type = array_reverse($type); $type = array_reverse($type);
} }
} elseif (strstr($db_type, 'text')) { } elseif (strstr($db_type, 'text')) {
@@ -254,9 +460,12 @@ class MDB2_Driver_Datatype_mssql extends MDB2_Driver_Datatype_Common
if (PEAR::isError($db)) { if (PEAR::isError($db)) {
return $db; return $db;
} }
return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
'mapNativeDatatype: unknown database attribute type: '.$db_type); 'unknown database attribute type: '.$db_type, __FUNCTION__);
}
if ((int)$length <= 0) {
$length = null;
} }
return array($type, $length, $unsigned, $fixed); return array($type, $length, $unsigned, $fixed);

View File

@@ -3,7 +3,7 @@
// +----------------------------------------------------------------------+ // +----------------------------------------------------------------------+
// | PHP versions 4 and 5 | // | PHP versions 4 and 5 |
// +----------------------------------------------------------------------+ // +----------------------------------------------------------------------+
// | Copyright (c) 1998-2006 Manuel Lemos, Tomas V.V.Cox, | // | Copyright (c) 1998-2007 Manuel Lemos, Tomas V.V.Cox, |
// | Stig. S. Bakken, Lukas Smith | // | Stig. S. Bakken, Lukas Smith |
// | All rights reserved. | // | All rights reserved. |
// +----------------------------------------------------------------------+ // +----------------------------------------------------------------------+
@@ -46,8 +46,7 @@
// $Id$ // $Id$
// //
//require_once 'MDB2/Driver/Datatype/Common.php'; require_once 'MDB2/Driver/Datatype/Common.php';
require_once PEAR_PATH."/MDB2/Driver/Datatype/Common.php";
/** /**
* MDB2 MySQL driver * MDB2 MySQL driver
@@ -225,7 +224,7 @@ class MDB2_Driver_Datatype_mysql extends MDB2_Driver_Datatype_Common
return $db; return $db;
} }
$default = $autoinc = '';; $default = $autoinc = '';
if (!empty($field['autoincrement'])) { if (!empty($field['autoincrement'])) {
$autoinc = ' AUTO_INCREMENT PRIMARY KEY'; $autoinc = ' AUTO_INCREMENT PRIMARY KEY';
} elseif (array_key_exists('default', $field)) { } elseif (array_key_exists('default', $field)) {
@@ -302,7 +301,7 @@ class MDB2_Driver_Datatype_mysql extends MDB2_Driver_Datatype_Common
} }
// }}} // }}}
// {{{ mapNativeDatatype() // {{{ _mapNativeDatatype()
/** /**
* Maps a native array description of a field to a MDB2 datatype and length * Maps a native array description of a field to a MDB2 datatype and length
@@ -311,7 +310,7 @@ class MDB2_Driver_Datatype_mysql extends MDB2_Driver_Datatype_Common
* @return array containing the various possible types, length, sign, fixed * @return array containing the various possible types, length, sign, fixed
* @access public * @access public
*/ */
function mapNativeDatatype($field) function _mapNativeDatatype($field)
{ {
$db_type = strtolower($field['type']); $db_type = strtolower($field['type']);
$db_type = strtok($db_type, '(), '); $db_type = strtok($db_type, '(), ');
@@ -319,8 +318,8 @@ class MDB2_Driver_Datatype_mysql extends MDB2_Driver_Datatype_Common
$db_type = strtok('(), '); $db_type = strtok('(), ');
} }
if (!empty($field['length'])) { if (!empty($field['length'])) {
$length = $field['length']; $length = strtok($field['length'], ', ');
$decimal = ''; $decimal = strtok(', ');
} else { } else {
$length = strtok('(), '); $length = strtok('(), ');
$decimal = strtok('(), '); $decimal = strtok('(), ');
@@ -429,6 +428,9 @@ class MDB2_Driver_Datatype_mysql extends MDB2_Driver_Datatype_Common
case 'numeric': case 'numeric':
$type[] = 'decimal'; $type[] = 'decimal';
$unsigned = preg_match('/ unsigned/i', $field['type']); $unsigned = preg_match('/ unsigned/i', $field['type']);
if ($decimal !== false) {
$length = $length.','.$decimal;
}
break; break;
case 'tinyblob': case 'tinyblob':
case 'mediumblob': case 'mediumblob':
@@ -437,6 +439,10 @@ class MDB2_Driver_Datatype_mysql extends MDB2_Driver_Datatype_Common
$type[] = 'blob'; $type[] = 'blob';
$length = null; $length = null;
break; break;
case 'binary':
case 'varbinary':
$type[] = 'blob';
break;
case 'year': case 'year':
$type[] = 'integer'; $type[] = 'integer';
$type[] = 'date'; $type[] = 'date';
@@ -452,6 +458,10 @@ class MDB2_Driver_Datatype_mysql extends MDB2_Driver_Datatype_Common
'unknown database attribute type: '.$db_type, __FUNCTION__); 'unknown database attribute type: '.$db_type, __FUNCTION__);
} }
if ((int)$length <= 0) {
$length = null;
}
return array($type, $length, $unsigned, $fixed); return array($type, $length, $unsigned, $fixed);
} }

View File

@@ -3,7 +3,7 @@
// +----------------------------------------------------------------------+ // +----------------------------------------------------------------------+
// | PHP versions 4 and 5 | // | PHP versions 4 and 5 |
// +----------------------------------------------------------------------+ // +----------------------------------------------------------------------+
// | Copyright (c) 1998-2006 Manuel Lemos, Tomas V.V.Cox, | // | Copyright (c) 1998-2007 Manuel Lemos, Tomas V.V.Cox, |
// | Stig. S. Bakken, Lukas Smith | // | Stig. S. Bakken, Lukas Smith |
// | All rights reserved. | // | All rights reserved. |
// +----------------------------------------------------------------------+ // +----------------------------------------------------------------------+
@@ -46,8 +46,7 @@
// $Id$ // $Id$
// //
//require_once 'MDB2/Driver/Datatype/Common.php'; require_once 'MDB2/Driver/Datatype/Common.php';
require_once PEAR_PATH."/MDB2/Driver/Datatype/Common.php";
/** /**
* MDB2 MySQLi driver * MDB2 MySQLi driver
@@ -225,7 +224,7 @@ class MDB2_Driver_Datatype_mysqli extends MDB2_Driver_Datatype_Common
return $db; return $db;
} }
$default = $autoinc = '';; $default = $autoinc = '';
if (!empty($field['autoincrement'])) { if (!empty($field['autoincrement'])) {
$autoinc = ' AUTO_INCREMENT PRIMARY KEY'; $autoinc = ' AUTO_INCREMENT PRIMARY KEY';
} elseif (array_key_exists('default', $field)) { } elseif (array_key_exists('default', $field)) {
@@ -302,7 +301,7 @@ class MDB2_Driver_Datatype_mysqli extends MDB2_Driver_Datatype_Common
} }
// }}} // }}}
// {{{ mapNativeDatatype() // {{{ _mapNativeDatatype()
/** /**
* Maps a native array description of a field to a MDB2 datatype and length * Maps a native array description of a field to a MDB2 datatype and length
@@ -311,7 +310,7 @@ class MDB2_Driver_Datatype_mysqli extends MDB2_Driver_Datatype_Common
* @return array containing the various possible types, length, sign, fixed * @return array containing the various possible types, length, sign, fixed
* @access public * @access public
*/ */
function mapNativeDatatype($field) function _mapNativeDatatype($field)
{ {
$db_type = strtolower($field['type']); $db_type = strtolower($field['type']);
$db_type = strtok($db_type, '(), '); $db_type = strtok($db_type, '(), ');
@@ -319,8 +318,8 @@ class MDB2_Driver_Datatype_mysqli extends MDB2_Driver_Datatype_Common
$db_type = strtok('(), '); $db_type = strtok('(), ');
} }
if (!empty($field['length'])) { if (!empty($field['length'])) {
$length = $field['length']; $length = strtok($field['length'], ', ');
$decimal = ''; $decimal = strtok(', ');
} else { } else {
$length = strtok('(), '); $length = strtok('(), ');
$decimal = strtok('(), '); $decimal = strtok('(), ');
@@ -429,6 +428,9 @@ class MDB2_Driver_Datatype_mysqli extends MDB2_Driver_Datatype_Common
case 'numeric': case 'numeric':
$type[] = 'decimal'; $type[] = 'decimal';
$unsigned = preg_match('/ unsigned/i', $field['type']); $unsigned = preg_match('/ unsigned/i', $field['type']);
if ($decimal !== false) {
$length = $length.','.$decimal;
}
break; break;
case 'tinyblob': case 'tinyblob':
case 'mediumblob': case 'mediumblob':
@@ -437,6 +439,10 @@ class MDB2_Driver_Datatype_mysqli extends MDB2_Driver_Datatype_Common
$type[] = 'blob'; $type[] = 'blob';
$length = null; $length = null;
break; break;
case 'binary':
case 'varbinary':
$type[] = 'blob';
break;
case 'year': case 'year':
$type[] = 'integer'; $type[] = 'integer';
$type[] = 'date'; $type[] = 'date';
@@ -452,6 +458,10 @@ class MDB2_Driver_Datatype_mysqli extends MDB2_Driver_Datatype_Common
'unknown database attribute type: '.$db_type, __FUNCTION__); 'unknown database attribute type: '.$db_type, __FUNCTION__);
} }
if ((int)$length <= 0) {
$length = null;
}
return array($type, $length, $unsigned, $fixed); return array($type, $length, $unsigned, $fixed);
} }
@@ -459,7 +469,7 @@ class MDB2_Driver_Datatype_mysqli extends MDB2_Driver_Datatype_Common
// {{{ mapPrepareDatatype() // {{{ mapPrepareDatatype()
/** /**
* Maps an mdb2 datatype to native prepare type * Maps an MDB2 datatype to native prepare type
* *
* @param string $type * @param string $type
* @return string * @return string
@@ -495,5 +505,4 @@ class MDB2_Driver_Datatype_mysqli extends MDB2_Driver_Datatype_Common
// }}} // }}}
} }
?> ?>

View File

@@ -2,7 +2,7 @@
// +----------------------------------------------------------------------+ // +----------------------------------------------------------------------+
// | PHP versions 4 and 5 | // | PHP versions 4 and 5 |
// +----------------------------------------------------------------------+ // +----------------------------------------------------------------------+
// | Copyright (c) 1998-2006 Manuel Lemos, Tomas V.V.Cox, | // | Copyright (c) 1998-2007 Manuel Lemos, Tomas V.V.Cox, |
// | Stig. S. Bakken, Lukas Smith | // | Stig. S. Bakken, Lukas Smith |
// | All rights reserved. | // | All rights reserved. |
// +----------------------------------------------------------------------+ // +----------------------------------------------------------------------+
@@ -44,8 +44,7 @@
// //
// $Id$ // $Id$
//require_once 'MDB2/Driver/Datatype/Common.php'; require_once 'MDB2/Driver/Datatype/Common.php';
require_once PEAR_PATH."/MDB2/Driver/Datatype/Common.php";
/** /**
* MDB2 PostGreSQL driver * MDB2 PostGreSQL driver
@@ -59,11 +58,11 @@ class MDB2_Driver_Datatype_pgsql extends MDB2_Driver_Datatype_Common
// {{{ _baseConvertResult() // {{{ _baseConvertResult()
/** /**
* general type conversion method * General type conversion method
* *
* @param mixed $value refernce to a value to be converted * @param mixed $value refernce to a value to be converted
* @param string $type specifies which type to convert to * @param string $type specifies which type to convert to
* @param string $rtrim if text should be rtrimmed * @param boolean $rtrim [optional] when TRUE [default], apply rtrim() to text
* @return object a MDB2 error on failure * @return object a MDB2 error on failure
* @access protected * @access protected
*/ */
@@ -383,7 +382,7 @@ class MDB2_Driver_Datatype_pgsql extends MDB2_Driver_Datatype_Common
} }
// }}} // }}}
// {{{ mapNativeDatatype() // {{{ _mapNativeDatatype()
/** /**
* Maps a native array description of a field to a MDB2 datatype and length * Maps a native array description of a field to a MDB2 datatype and length
@@ -392,16 +391,10 @@ class MDB2_Driver_Datatype_pgsql extends MDB2_Driver_Datatype_Common
* @return array containing the various possible types, length, sign, fixed * @return array containing the various possible types, length, sign, fixed
* @access public * @access public
*/ */
function mapNativeDatatype($field) function _mapNativeDatatype($field)
{ {
$db_type = strtolower($field['type']); $db_type = strtolower($field['type']);
$length = $field['length']; $length = $field['length'];
if ($length == '-1' && !empty($field['atttypmod'])) {
$length = $field['atttypmod'] - 4;
}
if ((int)$length <= 0) {
$length = null;
}
$type = array(); $type = array();
$unsigned = $fixed = null; $unsigned = $fixed = null;
switch ($db_type) { switch ($db_type) {
@@ -472,6 +465,7 @@ class MDB2_Driver_Datatype_pgsql extends MDB2_Driver_Datatype_Common
$length = null; $length = null;
break; break;
case 'float': case 'float':
case 'float8':
case 'double': case 'double':
case 'real': case 'real':
$type[] = 'float'; $type[] = 'float';
@@ -480,6 +474,9 @@ class MDB2_Driver_Datatype_pgsql extends MDB2_Driver_Datatype_Common
case 'money': case 'money':
case 'numeric': case 'numeric':
$type[] = 'decimal'; $type[] = 'decimal';
if ($field['scale']) {
$length = $length.','.$field['scale'];
}
break; break;
case 'tinyblob': case 'tinyblob':
case 'mediumblob': case 'mediumblob':
@@ -504,11 +501,14 @@ class MDB2_Driver_Datatype_pgsql extends MDB2_Driver_Datatype_Common
if (PEAR::isError($db)) { if (PEAR::isError($db)) {
return $db; return $db;
} }
return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
'unknown database attribute type: '.$db_type, __FUNCTION__); 'unknown database attribute type: '.$db_type, __FUNCTION__);
} }
if ((int)$length <= 0) {
$length = null;
}
return array($type, $length, $unsigned, $fixed); return array($type, $length, $unsigned, $fixed);
} }

View File

@@ -205,6 +205,27 @@ class MDB2_Driver_Function_Common extends MDB2_Module_Common
return "UPPER($expression)"; return "UPPER($expression)";
} }
// }}}
// {{{ guid()
/**
* Returns global unique identifier
*
* @return string to get global unique identifier
* @access public
*/
function guid()
{
$db =& $this->getDBInstance();
if (PEAR::isError($db)) {
return $db;
}
$error =& $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
'method not implemented', __FUNCTION__);
return $error;
}
// }}} // }}}
} }
?> ?>

View File

@@ -45,8 +45,7 @@
// $Id$ // $Id$
// //
//require_once 'MDB2/Driver/Function/Common.php'; require_once 'MDB2/Driver/Function/Common.php';
require_once PEAR_PATH."/MDB2/Driver/Function/Common.php";
// {{{ class MDB2_Driver_Function_mssql // {{{ class MDB2_Driver_Function_mssql
/** /**
@@ -144,6 +143,20 @@ class MDB2_Driver_Function_mssql extends MDB2_Driver_Function_Common
return "(".implode(' + ', $args).")"; return "(".implode(' + ', $args).")";
} }
// }}}
// {{{ guid()
/**
* Returns global unique identifier
*
* @return string to get global unique identifier
* @access public
*/
function guid()
{
return 'NEWID()';
}
// }}} // }}}
} }
// }}} // }}}

View File

@@ -45,8 +45,7 @@
// $Id$ // $Id$
// //
//require_once 'MDB2/Driver/Function/Common.php'; require_once 'MDB2/Driver/Function/Common.php';
require_once PEAR_PATH."/MDB2/Driver/Function/Common.php";
/** /**
* MDB2 MySQL driver for the function modules * MDB2 MySQL driver for the function modules
@@ -101,5 +100,21 @@ class MDB2_Driver_Function_mysql extends MDB2_Driver_Function_Common
$args = func_get_args(); $args = func_get_args();
return "CONCAT(".implode(', ', $args).")"; return "CONCAT(".implode(', ', $args).")";
} }
// }}}
// {{{ guid()
/**
* Returns global unique identifier
*
* @return string to get global unique identifier
* @access public
*/
function guid()
{
return 'UUID()';
}
// }}}
} }
?> ?>

View File

@@ -45,8 +45,7 @@
// $Id$ // $Id$
// //
//require_once 'MDB2/Driver/Function/Common.php'; require_once 'MDB2/Driver/Function/Common.php';
require_once PEAR_PATH."/MDB2/Driver/Function/Common.php";
/** /**
* MDB2 MySQLi driver for the function modules * MDB2 MySQLi driver for the function modules
@@ -109,5 +108,21 @@ class MDB2_Driver_Function_mysqli extends MDB2_Driver_Function_Common
$args = func_get_args(); $args = func_get_args();
return "CONCAT(".implode(', ', $args).")"; return "CONCAT(".implode(', ', $args).")";
} }
// }}}
// {{{ guid()
/**
* Returns global unique identifier
*
* @return string to get global unique identifier
* @access public
*/
function guid()
{
return 'UUID()';
}
// }}}
} }
?> ?>

View File

@@ -44,8 +44,7 @@
// //
// $Id$ // $Id$
//require_once 'MDB2/Driver/Function/Common.php'; require_once 'MDB2/Driver/Function/Common.php';
require_once PEAR_PATH."/MDB2/Driver/Function/Common.php";
/** /**
* MDB2 MySQL driver for the function modules * MDB2 MySQL driver for the function modules

View File

@@ -60,13 +60,12 @@
*/ */
class MDB2_Driver_Manager_Common extends MDB2_Module_Common class MDB2_Driver_Manager_Common extends MDB2_Module_Common
{ {
// }}}
// {{{ getFieldDeclarationList() // {{{ getFieldDeclarationList()
/** /**
* Get declaration of a number of field in bulk * Get declaration of a number of field in bulk
* *
* @param string $fields a multidimensional associative array. * @param array $fields a multidimensional associative array.
* The first dimension determines the field name, while the second * The first dimension determines the field name, while the second
* dimension is keyed with the name of the properties * dimension is keyed with the name of the properties
* of the field being declared as array indexes. Currently, the types * of the field being declared as array indexes. Currently, the types
@@ -93,7 +92,6 @@ class MDB2_Driver_Manager_Common extends MDB2_Module_Common
return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null, return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
'missing any fields', __FUNCTION__); 'missing any fields', __FUNCTION__);
} }
foreach ($fields as $field_name => $field) { foreach ($fields as $field_name => $field) {
$query = $db->getDeclaration($field['type'], $field_name, $field); $query = $db->getDeclaration($field['type'], $field_name, $field);
if (PEAR::isError($query)) { if (PEAR::isError($query)) {
@@ -201,7 +199,7 @@ class MDB2_Driver_Manager_Common extends MDB2_Module_Common
} }
// }}} // }}}
// {{{ // {{{ _getCreateTableQuery()
/** /**
* Create a basic SQL query for a new table creation * Create a basic SQL query for a new table creation
@@ -235,7 +233,34 @@ class MDB2_Driver_Manager_Common extends MDB2_Module_Common
} }
$name = $db->quoteIdentifier($name, true); $name = $db->quoteIdentifier($name, true);
return "CREATE TABLE $name ($query_fields)"; $result = 'CREATE ';
if (!empty($options['temporary'])) {
$result .= $this->_getTemporaryTableQuery();
}
$result .= " TABLE $name ($query_fields)";
return $result;
}
// }}}
// {{{ _getTemporaryTableQuery()
/**
* A method to return the required SQL string that fits between CREATE ... TABLE
* to create the table as a temporary table.
*
* Should be overridden in driver classes to return the correct string for the
* specific database type.
*
* The default is to return the string "TEMPORARY" - this will result in a
* SQL error for any database that does not support temporary tables, or that
* requires a different SQL command from "CREATE TEMPORARY TABLE".
*
* @return string The string required to be placed between "CREATE" and "TABLE"
* to generate a temporary table, if possible.
*/
function _getTemporaryTableQuery()
{
return 'TEMPORARY';
} }
// }}} // }}}
@@ -266,7 +291,10 @@ class MDB2_Driver_Manager_Common extends MDB2_Module_Common
* ) * )
* ); * );
* @param array $options An associative array of table options: * @param array $options An associative array of table options:
* * array(
* 'comment' => 'Foo',
* 'temporary' => true|false,
* );
* @return mixed MDB2_OK on success, a MDB2 error on failure * @return mixed MDB2_OK on success, a MDB2 error on failure
* @access public * @access public
*/ */
@@ -414,7 +442,7 @@ class MDB2_Driver_Manager_Common extends MDB2_Module_Common
/** /**
* list all databases * list all databases
* *
* @return mixed data array on success, a MDB2 error on failure * @return mixed array of database names on success, a MDB2 error on failure
* @access public * @access public
*/ */
function listDatabases() function listDatabases()
@@ -434,7 +462,7 @@ class MDB2_Driver_Manager_Common extends MDB2_Module_Common
/** /**
* list all users * list all users
* *
* @return mixed data array on success, a MDB2 error on failure * @return mixed array of user names on success, a MDB2 error on failure
* @access public * @access public
*/ */
function listUsers() function listUsers()
@@ -455,7 +483,9 @@ class MDB2_Driver_Manager_Common extends MDB2_Module_Common
* list all views in the current database * list all views in the current database
* *
* @param string database, the current is default * @param string database, the current is default
* @return mixed data array on success, a MDB2 error on failure * NB: not all the drivers can get the view names from
* a database other than the current one
* @return mixed array of view names on success, a MDB2 error on failure
* @access public * @access public
*/ */
function listViews($database = null) function listViews($database = null)
@@ -475,10 +505,10 @@ class MDB2_Driver_Manager_Common extends MDB2_Module_Common
/** /**
* list the views in the database that reference a given table * list the views in the database that reference a given table
* *
* @param string table for which all references views should be found * @param string table for which all referenced views should be found
* @return mixed MDB2_OK on success, a MDB2 error on failure * @return mixed array of view names on success, a MDB2 error on failure
* @access public * @access public
**/ */
function listTableViews($table) function listTableViews($table)
{ {
$db =& $this->getDBInstance(); $db =& $this->getDBInstance();
@@ -492,14 +522,13 @@ class MDB2_Driver_Manager_Common extends MDB2_Module_Common
// }}} // }}}
// {{{ listTableTriggers() // {{{ listTableTriggers()
/** /**
* This function will be called to get all triggers of the * list all triggers in the database that reference a given table
* current database ($db->getDatabase())
* *
* @param string table for which all referenced triggers should be found
* @return mixed array of trigger names on success, a MDB2 error on failure
* @access public * @access public
* @param string $table The name of the table from the
* previous database to query against.
* @return mixed Array on success or MDB2 error on failure
*/ */
function listTableTriggers($table = null) function listTableTriggers($table = null)
{ {
@@ -511,13 +540,14 @@ class MDB2_Driver_Manager_Common extends MDB2_Module_Common
return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
'method not implemented', __FUNCTION__); 'method not implemented', __FUNCTION__);
} }
// }}} // }}}
// {{{ listFunctions() // {{{ listFunctions()
/** /**
* list all functions in the current database * list all functions in the current database
* *
* @return mixed data array on success, a MDB2 error on failure * @return mixed array of function names on success, a MDB2 error on failure
* @access public * @access public
*/ */
function listFunctions() function listFunctions()
@@ -530,14 +560,17 @@ class MDB2_Driver_Manager_Common extends MDB2_Module_Common
return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
'method not implemented', __FUNCTION__); 'method not implemented', __FUNCTION__);
} }
// }}} // }}}
// {{{ listTables() // {{{ listTables()
/** /**
* list all tables in the current database * list all tables in the current database
* *
* @param string database, the current is default * @param string database, the current is default.
* @return mixed data array on success, a MDB2 error on failure * NB: not all the drivers can get the table names from
* a database other than the current one
* @return mixed array of table names on success, a MDB2 error on failure
* @access public * @access public
*/ */
function listTables($database = null) function listTables($database = null)
@@ -555,10 +588,10 @@ class MDB2_Driver_Manager_Common extends MDB2_Module_Common
// {{{ listTableFields() // {{{ listTableFields()
/** /**
* list all fields in a tables in the current database * list all fields in a table in the current database
* *
* @param string $table name of table that should be used in method * @param string $table name of table that should be used in method
* @return mixed data array on success, a MDB2 error on failure * @return mixed array of field names on success, a MDB2 error on failure
* @access public * @access public
*/ */
function listTableFields($table) function listTableFields($table)
@@ -654,7 +687,7 @@ class MDB2_Driver_Manager_Common extends MDB2_Module_Common
* list all indexes in a table * list all indexes in a table
* *
* @param string $table name of table that should be used in method * @param string $table name of table that should be used in method
* @return mixed data array on success, a MDB2 error on failure * @return mixed array of index names on success, a MDB2 error on failure
* @access public * @access public
*/ */
function listTableIndexes($table) function listTableIndexes($table)
@@ -746,7 +779,7 @@ class MDB2_Driver_Manager_Common extends MDB2_Module_Common
* list all constraints in a table * list all constraints in a table
* *
* @param string $table name of table that should be used in method * @param string $table name of table that should be used in method
* @return mixed data array on success, a MDB2 error on failure * @return mixed array of constraint names on success, a MDB2 error on failure
* @access public * @access public
*/ */
function listTableConstraints($table) function listTableConstraints($table)
@@ -810,7 +843,9 @@ class MDB2_Driver_Manager_Common extends MDB2_Module_Common
* list all sequences in the current database * list all sequences in the current database
* *
* @param string database, the current is default * @param string database, the current is default
* @return mixed data array on success, a MDB2 error on failure * NB: not all the drivers can get the sequence names from
* a database other than the current one
* @return mixed array of sequence names on success, a MDB2 error on failure
* @access public * @access public
*/ */
function listSequences($database = null) function listSequences($database = null)
@@ -823,6 +858,7 @@ class MDB2_Driver_Manager_Common extends MDB2_Module_Common
return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
'method not implemented', __FUNCTION__); 'method not implemented', __FUNCTION__);
} }
}
// }}}
}
?> ?>

View File

@@ -2,7 +2,7 @@
// +----------------------------------------------------------------------+ // +----------------------------------------------------------------------+
// | PHP versions 4 and 5 | // | PHP versions 4 and 5 |
// +----------------------------------------------------------------------+ // +----------------------------------------------------------------------+
// | Copyright (c) 1998-2006 Manuel Lemos, Tomas V.V.Cox, | // | Copyright (c) 1998-2007 Manuel Lemos, Tomas V.V.Cox, |
// | Stig. S. Bakken, Lukas Smith | // | Stig. S. Bakken, Lukas Smith |
// | All rights reserved. | // | All rights reserved. |
// +----------------------------------------------------------------------+ // +----------------------------------------------------------------------+
@@ -39,7 +39,9 @@
// | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | // | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
// | POSSIBILITY OF SUCH DAMAGE. | // | POSSIBILITY OF SUCH DAMAGE. |
// +----------------------------------------------------------------------+ // +----------------------------------------------------------------------+
// | Author: Frank M. Kromann <frank@kromann.info> | // | Authors: Frank M. Kromann <frank@kromann.info> |
// | David Coallier <davidc@php.net> |
// | Lorenzo Alberton <l.alberton@quipo.it> |
// +----------------------------------------------------------------------+ // +----------------------------------------------------------------------+
// //
// $Id$ // $Id$
@@ -48,6 +50,7 @@
require_once 'MDB2/Driver/Manager/Common.php'; require_once 'MDB2/Driver/Manager/Common.php';
// {{{ class MDB2_Driver_Manager_mssql // {{{ class MDB2_Driver_Manager_mssql
/** /**
* MDB2 MSSQL driver for the management modules * MDB2 MSSQL driver for the management modules
* *
@@ -55,6 +58,7 @@ require_once 'MDB2/Driver/Manager/Common.php';
* @category Database * @category Database
* @author Frank M. Kromann <frank@kromann.info> * @author Frank M. Kromann <frank@kromann.info>
* @author David Coallier <davidc@php.net> * @author David Coallier <davidc@php.net>
* @author Lorenzo Alberton <l.alberton@quipo.it>
*/ */
class MDB2_Driver_Manager_mssql extends MDB2_Driver_Manager_Common class MDB2_Driver_Manager_mssql extends MDB2_Driver_Manager_Common
{ {
@@ -82,6 +86,7 @@ class MDB2_Driver_Manager_mssql extends MDB2_Driver_Manager_Common
} }
return $db->standaloneQuery($query, null, true); return $db->standaloneQuery($query, null, true);
} }
// }}} // }}}
// {{{ dropDatabase() // {{{ dropDatabase()
@@ -103,6 +108,66 @@ class MDB2_Driver_Manager_mssql extends MDB2_Driver_Manager_Common
return $db->standaloneQuery("DROP DATABASE $name", null, true); return $db->standaloneQuery("DROP DATABASE $name", null, true);
} }
// }}}
// {{{ _getTemporaryTableQuery()
/**
* Override the parent method.
*
* @return string The string required to be placed between "CREATE" and "TABLE"
* to generate a temporary table, if possible.
*/
function _getTemporaryTableQuery()
{
return '';
}
// }}}
// {{{ createTable()
/**
* create a new table
*
* @param string $name Name of the database that should be created
* @param array $fields Associative array that contains the definition of each field of the new table
* The indexes of the array entries are the names of the fields of the table an
* the array entry values are associative arrays like those that are meant to be
* passed with the field definitions to get[Type]Declaration() functions.
*
* Example
* array(
*
* 'id' => array(
* 'type' => 'integer',
* 'unsigned' => 1,
* 'notnull' => 1,
* 'default' => 0,
* ),
* 'name' => array(
* 'type' => 'text',
* 'length' => 12,
* ),
* 'description' => array(
* 'type' => 'text',
* 'length' => 12,
* )
* );
* @param array $options An associative array of table options:
* array(
* 'comment' => 'Foo',
* 'temporary' => true|false,
* );
* @return mixed MDB2_OK on success, a MDB2 error on failure
* @access public
*/
function createTable($name, $fields, $options = array())
{
if (!empty($options['temporary'])) {
$name = '#'.$name;
}
return parent::createTable($name, $fields, $options);
}
// }}} // }}}
// {{{ alterTable() // {{{ alterTable()
@@ -124,7 +189,7 @@ class MDB2_Driver_Manager_mssql extends MDB2_Driver_Manager_Common
* indexes of the array. The value of each entry of the array * indexes of the array. The value of each entry of the array
* should be set to another associative array with the properties * should be set to another associative array with the properties
* of the fields to be added. The properties of the fields should * of the fields to be added. The properties of the fields should
* be the same as defined by the Metabase parser. * be the same as defined by the MDB2 parser.
* *
* *
* remove * remove
@@ -153,7 +218,7 @@ class MDB2_Driver_Manager_mssql extends MDB2_Driver_Manager_Common
* array with the properties of the fields to that are meant to be changed as * array with the properties of the fields to that are meant to be changed as
* array entries. These entries should be assigned to the new values of the * array entries. These entries should be assigned to the new values of the
* respective properties. The properties of the fields should be the same * respective properties. The properties of the fields should be the same
* as defined by the Metabase parser. * as defined by the MDB2 parser.
* *
* Example * Example
* array( * array(
@@ -214,7 +279,7 @@ class MDB2_Driver_Manager_mssql extends MDB2_Driver_Manager_Common
case 'change': case 'change':
default: default:
return $db->raiseError(MDB2_ERROR_CANNOT_ALTER, null, null, return $db->raiseError(MDB2_ERROR_CANNOT_ALTER, null, null,
'alterTable: change type "'.$change_name.'" not yet supported'); 'change type "'.$change_name.'" not yet supported', __FUNCTION__);
} }
} }
@@ -227,8 +292,10 @@ class MDB2_Driver_Manager_mssql extends MDB2_Driver_Manager_Common
foreach ($changes['add'] as $field_name => $field) { foreach ($changes['add'] as $field_name => $field) {
if ($query) { if ($query) {
$query.= ', '; $query.= ', ';
} else {
$query.= 'ADD COLUMN ';
} }
$query.= 'ADD ' . $db->getDeclaration($field['type'], $field_name, $field); $query.= $db->getDeclaration($field['type'], $field_name, $field);
} }
} }
@@ -249,12 +316,14 @@ class MDB2_Driver_Manager_mssql extends MDB2_Driver_Manager_Common
$name = $db->quoteIdentifier($name, true); $name = $db->quoteIdentifier($name, true);
return $db->exec("ALTER TABLE $name $query"); return $db->exec("ALTER TABLE $name $query");
} }
// }}} // }}}
// {{{ listTables() // {{{ listTables()
/** /**
* list all tables in the current database * list all tables in the current database
* *
* @return mixed data array on success, a MDB2 error on failure * @return mixed array of table names on success, a MDB2 error on failure
* @access public * @access public
*/ */
function listTables() function listTables()
@@ -282,13 +351,15 @@ class MDB2_Driver_Manager_mssql extends MDB2_Driver_Manager_Common
} }
return $result; return $result;
} }
// }}} // }}}
// {{{ listTableFields() // {{{ listTableFields()
/** /**
* list all fields in a tables in the current database * list all fields in a table in the current database
* *
* @param string $table name of table that should be used in method * @param string $table name of table that should be used in method
* @return mixed data array on success, a MDB2 error on failure * @return mixed array of field names on success, a MDB2 error on failure
* @access public * @access public
*/ */
function listTableFields($table) function listTableFields($table)
@@ -311,6 +382,7 @@ class MDB2_Driver_Manager_mssql extends MDB2_Driver_Manager_Common
} }
return array_flip($result); return array_flip($result);
} }
// }}} // }}}
// {{{ listTableIndexes() // {{{ listTableIndexes()
@@ -318,7 +390,7 @@ class MDB2_Driver_Manager_mssql extends MDB2_Driver_Manager_Common
* list all indexes in a table * list all indexes in a table
* *
* @param string $table name of table that should be used in method * @param string $table name of table that should be used in method
* @return mixed data array on success, a MDB2 error on failure * @return mixed array of index names on success, a MDB2 error on failure
* @access public * @access public
*/ */
function listTableIndexes($table) function listTableIndexes($table)
@@ -349,8 +421,8 @@ class MDB2_Driver_Manager_mssql extends MDB2_Driver_Manager_Common
$pk_all = $db->queryCol($query, 'text', $pk_name); $pk_all = $db->queryCol($query, 'text', $pk_name);
$result = array(); $result = array();
foreach ($indexes as $index) { foreach ($indexes as $index) {
if (!in_array($index, $pk_all) && $index != null) { if (!in_array($index, $pk_all) && ($index = $this->_fixIndexName($index))) {
$result[$this->_fixIndexName($index)] = true; $result[$index] = true;
} }
} }
@@ -361,21 +433,105 @@ class MDB2_Driver_Manager_mssql extends MDB2_Driver_Manager_Common
} }
// }}} // }}}
// {{{ listTableTriggers() // {{{ listDatabases()
/** /**
* This function will be called to * list all databases
* display all the triggers from the current
* database ($db->getDatabase()).
* *
* @return mixed array of database names on success, a MDB2 error on failure
* @access public
*/
function listDatabases()
{
$db =& $this->getDBInstance();
if (PEAR::isError($db)) {
return $db;
}
$result = $db->queryCol('SELECT name FROM sys.databases');
if (PEAR::isError($result)) {
return $result;
}
if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
$result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
}
return $result;
}
// }}}
// {{{ listUsers()
/**
* list all users
*
* @return mixed array of user names on success, a MDB2 error on failure
* @access public
*/
function listUsers()
{
$db =& $this->getDBInstance();
if (PEAR::isError($db)) {
return $db;
}
$result = $db->queryCol('SELECT DISTINCT loginame FROM master..sysprocesses');
if (PEAR::isError($result) || empty($result)) {
return $result;
}
foreach (array_keys($result) as $k) {
$result[$k] = trim($result[$k]);
}
return $result;
}
// }}}
// {{{ listFunctions()
/**
* list all functions in the current database
*
* @return mixed array of function names on success, a MDB2 error on failure
* @access public
*/
function listFunctions()
{
$db =& $this->getDBInstance();
if (PEAR::isError($db)) {
return $db;
}
$query = "SELECT name
FROM sysobjects
WHERE objectproperty(id, N'IsMSShipped') = 0
AND (objectproperty(id, N'IsTableFunction') = 1
OR objectproperty(id, N'IsScalarFunction') = 1)";
/*
SELECT ROUTINE_NAME
FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_TYPE = 'FUNCTION'
*/
$result = $db->queryCol($query);
if (PEAR::isError($result)) {
return $result;
}
if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
$result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
}
return $result;
}
// }}}
// {{{ listTableTriggers()
/**
* list all triggers in the database that reference a given table
*
* @param string table for which all referenced triggers should be found
* @return mixed array of trigger names on success, otherwise, false which
* could be a db error if the db is not instantiated or could
* be the results of the error that occured during the
* querying of the sysobject module.
* @access public * @access public
* @param string $table The name of the table from the
* previous database to query against.
* @return mixed Array of the triggers if the query
* is successful, otherwise, false which
* could be a db error if the db is not
* instantiated or could be the results
* of the error that occured during the
* query'ing of the sysobject module.
*/ */
function listTableTriggers($table = null) function listTableTriggers($table = null)
{ {
@@ -385,13 +541,16 @@ class MDB2_Driver_Manager_mssql extends MDB2_Driver_Manager_Common
} }
$table = $db->quote($table, 'text'); $table = $db->quote($table, 'text');
$query = "SELECT name FROM sysobjects WHERE xtype = 'TR'"; $query = "SELECT o.name
FROM sysobjects o
WHERE xtype = 'TR'
AND OBJECTPROPERTY(o.id, 'IsMSShipped') = 0";
if (!is_null($table)) { if (!is_null($table)) {
$query .= " AND object_name(parent_obj) = $table"; $query .= " AND object_name(parent_obj) = $table";
} }
$result = $db->queryCol($query); $result = $db->queryCol($query);
if (PEAR::isError($results)) { if (PEAR::isError($result)) {
return $result; return $result;
} }
@@ -403,15 +562,16 @@ class MDB2_Driver_Manager_mssql extends MDB2_Driver_Manager_Common
} }
return $result; return $result;
} }
// }}} // }}}
// {{{ listViews() // {{{ listViews()
/** /**
* This function is a simple method that lists * list all views in the current database
* all the views that are set on a db instance
* (The db connected to it)
* *
* @param string database, the current is default
* @return mixed array of view names on success, a MDB2 error on failure
* @access public * @access public
* @return mixed Error on failure or array of views for a database.
*/ */
function listViews() function listViews()
{ {
@@ -423,9 +583,15 @@ class MDB2_Driver_Manager_mssql extends MDB2_Driver_Manager_Common
$query = "SELECT name $query = "SELECT name
FROM sysobjects FROM sysobjects
WHERE xtype = 'V'"; WHERE xtype = 'V'";
/*
SELECT *
FROM sysobjects
WHERE objectproperty(id, N'IsMSShipped') = 0
AND objectproperty(id, N'IsView') = 1
*/
$result = $db->queryCol($query); $result = $db->queryCol($query);
if (PEAR::isError($results)) { if (PEAR::isError($result)) {
return $result; return $result;
} }
@@ -437,8 +603,74 @@ class MDB2_Driver_Manager_mssql extends MDB2_Driver_Manager_Common
} }
return $result; return $result;
} }
// }}}
// {{{ dropIndex()
/**
* drop existing index
*
* @param string $table name of table that should be used in method
* @param string $name name of the index to be dropped
* @return mixed MDB2_OK on success, a MDB2 error on failure
* @access public
*/
function dropIndex($table, $name)
{
$db =& $this->getDBInstance();
if (PEAR::isError($db)) {
return $db;
}
$table = $db->quoteIdentifier($table, true);
$name = $db->quoteIdentifier($db->getIndexName($name), true);
return $db->exec("DROP INDEX $table.$name");
}
// }}}
// {{{ listTableConstraints()
/**
* list all constraints in a table
*
* @param string $table name of table that should be used in method
* @return mixed array of constraint names on success, a MDB2 error on failure
* @access public
*/
function listTableConstraints($table)
{
$db =& $this->getDBInstance();
if (PEAR::isError($db)) {
return $db;
}
$table = $db->quoteIdentifier($table, true);
$query = "SELECT c.constraint_name
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS c
WHERE c.constraint_catalog = DB_NAME()
AND c.table_name = '$table'";
$constraints = $db->queryCol($query);
if (PEAR::isError($constraints)) {
return $constraints;
}
$result = array();
foreach ($constraints as $constraint) {
$constraint = $this->_fixIndexName($constraint);
if (!empty($constraint)) {
$result[$constraint] = true;
}
}
if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
$result = array_change_key_case($result, $db->options['field_case']);
}
return array_keys($result);
}
// }}} // }}}
// {{{ createSequence() // {{{ createSequence()
/** /**
* create sequence * create sequence
* *
@@ -464,11 +696,6 @@ class MDB2_Driver_Manager_mssql extends MDB2_Driver_Manager_Common
return $res; return $res;
} }
if ($start == 1) {
return MDB2_OK;
}
$query = "SET IDENTITY_INSERT $sequence_name ON ". $query = "SET IDENTITY_INSERT $sequence_name ON ".
"INSERT INTO $sequence_name ($seqcol_name) VALUES ($start)"; "INSERT INTO $sequence_name ($seqcol_name) VALUES ($start)";
$res = $db->exec($query); $res = $db->exec($query);
@@ -480,14 +707,16 @@ class MDB2_Driver_Manager_mssql extends MDB2_Driver_Manager_Common
$result = $db->exec("DROP TABLE $sequence_name"); $result = $db->exec("DROP TABLE $sequence_name");
if (PEAR::isError($result)) { if (PEAR::isError($result)) {
return $db->raiseError($result, null, null, return $db->raiseError($result, null, null,
'createSequence: could not drop inconsistent sequence table'); 'could not drop inconsistent sequence table', __FUNCTION__);
} }
return $db->raiseError($res, null, null, return $db->raiseError($res, null, null,
'createSequence: could not create sequence table'); 'could not create sequence table', __FUNCTION__);
} }
// }}} // }}}
// {{{ dropSequence() // {{{ dropSequence()
/** /**
* This function drops an existing sequence * This function drops an existing sequence
* *
@@ -505,12 +734,14 @@ class MDB2_Driver_Manager_mssql extends MDB2_Driver_Manager_Common
$sequence_name = $db->quoteIdentifier($db->getSequenceName($seq_name), true); $sequence_name = $db->quoteIdentifier($db->getSequenceName($seq_name), true);
return $db->exec("DROP TABLE $sequence_name"); return $db->exec("DROP TABLE $sequence_name");
} }
// }}} // }}}
// {{{ listSequences() // {{{ listSequences()
/** /**
* list all sequences in the current database * list all sequences in the current database
* *
* @return mixed data array on success, a MDB2 error on failure * @return mixed array of sequence names on success, a MDB2 error on failure
* @access public * @access public
*/ */
function listSequences() function listSequences()
@@ -537,7 +768,9 @@ class MDB2_Driver_Manager_mssql extends MDB2_Driver_Manager_Common
} }
return $result; return $result;
} }
// }}} // }}}
} }
// }}} // }}}
?> ?>

View File

@@ -2,7 +2,7 @@
// +----------------------------------------------------------------------+ // +----------------------------------------------------------------------+
// | PHP versions 4 and 5 | // | PHP versions 4 and 5 |
// +----------------------------------------------------------------------+ // +----------------------------------------------------------------------+
// | Copyright (c) 1998-2006 Manuel Lemos, Tomas V.V.Cox, | // | Copyright (c) 1998-2007 Manuel Lemos, Tomas V.V.Cox, |
// | Stig. S. Bakken, Lukas Smith | // | Stig. S. Bakken, Lukas Smith |
// | All rights reserved. | // | All rights reserved. |
// +----------------------------------------------------------------------+ // +----------------------------------------------------------------------+
@@ -139,8 +139,7 @@ class MDB2_Driver_Manager_mysql extends MDB2_Driver_Manager_Common
* @param array $options An associative array of table options: * @param array $options An associative array of table options:
* array( * array(
* 'comment' => 'Foo', * 'comment' => 'Foo',
* 'character_set' => 'utf8', * 'charset' => 'utf8',
* 'collate' => 'utf8_unicode_ci',
* 'collate' => 'utf8_unicode_ci', * 'collate' => 'utf8_unicode_ci',
* 'type' => 'innodb', * 'type' => 'innodb',
* ); * );
@@ -154,6 +153,7 @@ class MDB2_Driver_Manager_mysql extends MDB2_Driver_Manager_Common
if (PEAR::isError($db)) { if (PEAR::isError($db)) {
return $db; return $db;
} }
$query = $this->_getCreateTableQuery($name, $fields, $options); $query = $this->_getCreateTableQuery($name, $fields, $options);
if (PEAR::isError($query)) { if (PEAR::isError($query)) {
return $query; return $query;
@@ -379,7 +379,7 @@ class MDB2_Driver_Manager_mysql extends MDB2_Driver_Manager_Common
/** /**
* list all databases * list all databases
* *
* @return mixed data array on success, a MDB2 error on failure * @return mixed array of database names on success, a MDB2 error on failure
* @access public * @access public
*/ */
function listDatabases() function listDatabases()
@@ -405,7 +405,7 @@ class MDB2_Driver_Manager_mysql extends MDB2_Driver_Manager_Common
/** /**
* list all users * list all users
* *
* @return mixed data array on success, a MDB2 error on failure * @return mixed array of user names on success, a MDB2 error on failure
* @access public * @access public
*/ */
function listUsers() function listUsers()
@@ -415,7 +415,71 @@ class MDB2_Driver_Manager_mysql extends MDB2_Driver_Manager_Common
return $db; return $db;
} }
return $db->queryCol('SELECT DISTINCT USER FROM USER'); return $db->queryCol('SELECT DISTINCT USER FROM mysql.USER');
}
// }}}
// {{{ listFunctions()
/**
* list all functions in the current database
*
* @return mixed array of function names on success, a MDB2 error on failure
* @access public
*/
function listFunctions()
{
$db =& $this->getDBInstance();
if (PEAR::isError($db)) {
return $db;
}
$query = "SELECT name FROM mysql.proc";
/*
SELECT ROUTINE_NAME
FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_TYPE = 'FUNCTION'
*/
$result = $db->queryCol($query);
if (PEAR::isError($result)) {
return $result;
}
if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
$result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
}
return $result;
}
// }}}
// {{{ listTableTriggers()
/**
* list all triggers in the database that reference a given table
*
* @param string table for which all referenced triggers should be found
* @return mixed array of trigger names on success, a MDB2 error on failure
* @access public
*/
function listTableTriggers($table = null)
{
$db =& $this->getDBInstance();
if (PEAR::isError($db)) {
return $db;
}
$query = 'SHOW TRIGGERS';
if (!is_null($table)) {
$table = $db->quote($table, 'text');
$query .= " LIKE $table";
}
$result = $db->queryCol($query);
if (PEAR::isError($result)) {
return $result;
}
if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
$result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
}
return $result;
} }
// }}} // }}}
@@ -425,7 +489,7 @@ class MDB2_Driver_Manager_mysql extends MDB2_Driver_Manager_Common
* list all tables in the current database * list all tables in the current database
* *
* @param string database, the current is default * @param string database, the current is default
* @return mixed data array on success, a MDB2 error on failure * @return mixed array of table names on success, a MDB2 error on failure
* @access public * @access public
*/ */
function listTables($database = null) function listTables($database = null)
@@ -462,12 +526,12 @@ class MDB2_Driver_Manager_mysql extends MDB2_Driver_Manager_Common
// {{{ listViews() // {{{ listViews()
/** /**
* list the views in the database * list all views in the current database
* *
* @param string database, the current is default * @param string database, the current is default
* @return mixed MDB2_OK on success, a MDB2 error on failure * @return mixed array of view names on success, a MDB2 error on failure
* @access public * @access public
**/ */
function listViews($database = null) function listViews($database = null)
{ {
$db =& $this->getDBInstance(); $db =& $this->getDBInstance();
@@ -496,10 +560,10 @@ class MDB2_Driver_Manager_mysql extends MDB2_Driver_Manager_Common
// {{{ listTableFields() // {{{ listTableFields()
/** /**
* list all fields in a tables in the current database * list all fields in a table in the current database
* *
* @param string $table name of table that should be used in method * @param string $table name of table that should be used in method
* @return mixed data array on success, a MDB2 error on failure * @return mixed array of field names on success, a MDB2 error on failure
* @access public * @access public
*/ */
function listTableFields($table) function listTableFields($table)
@@ -609,7 +673,7 @@ class MDB2_Driver_Manager_mysql extends MDB2_Driver_Manager_Common
* list all indexes in a table * list all indexes in a table
* *
* @param string $table name of table that should be used in method * @param string $table name of table that should be used in method
* @return mixed data array on success, a MDB2 error on failure * @return mixed array of index names on success, a MDB2 error on failure
* @access public * @access public
*/ */
function listTableIndexes($table) function listTableIndexes($table)
@@ -691,6 +755,10 @@ class MDB2_Driver_Manager_mysql extends MDB2_Driver_Manager_Common
} elseif (!empty($definition['unique'])) { } elseif (!empty($definition['unique'])) {
$type = 'UNIQUE'; $type = 'UNIQUE';
} }
if (empty($type)) {
return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
'invalid definition, could not create constraint', __FUNCTION__);
}
$table = $db->quoteIdentifier($table, true); $table = $db->quoteIdentifier($table, true);
$query = "ALTER TABLE $table ADD $type $name"; $query = "ALTER TABLE $table ADD $type $name";
@@ -738,7 +806,7 @@ class MDB2_Driver_Manager_mysql extends MDB2_Driver_Manager_Common
* list all constraints in a table * list all constraints in a table
* *
* @param string $table name of table that should be used in method * @param string $table name of table that should be used in method
* @return mixed data array on success, a MDB2 error on failure * @return mixed array of constraint names on success, a MDB2 error on failure
* @access public * @access public
*/ */
function listTableConstraints($table) function listTableConstraints($table)
@@ -795,10 +863,17 @@ class MDB2_Driver_Manager_mysql extends MDB2_Driver_Manager_Common
* *
* @param string $seq_name name of the sequence to be created * @param string $seq_name name of the sequence to be created
* @param string $start start value of the sequence; default is 1 * @param string $start start value of the sequence; default is 1
* @param array $options An associative array of table options:
* array(
* 'comment' => 'Foo',
* 'charset' => 'utf8',
* 'collate' => 'utf8_unicode_ci',
* 'type' => 'innodb',
* );
* @return mixed MDB2_OK on success, a MDB2 error on failure * @return mixed MDB2_OK on success, a MDB2 error on failure
* @access public * @access public
*/ */
function createSequence($seq_name, $start = 1) function createSequence($seq_name, $start = 1, $options = array())
{ {
$db =& $this->getDBInstance(); $db =& $this->getDBInstance();
if (PEAR::isError($db)) { if (PEAR::isError($db)) {
@@ -808,8 +883,33 @@ class MDB2_Driver_Manager_mysql extends MDB2_Driver_Manager_Common
$sequence_name = $db->quoteIdentifier($db->getSequenceName($seq_name), true); $sequence_name = $db->quoteIdentifier($db->getSequenceName($seq_name), true);
$seqcol_name = $db->quoteIdentifier($db->options['seqcol_name'], true); $seqcol_name = $db->quoteIdentifier($db->options['seqcol_name'], true);
$options_strings = array();
if (!empty($options['comment'])) {
$options_strings['comment'] = 'COMMENT = '.$db->quote($options['comment'], 'text');
}
if (!empty($options['charset'])) {
$options_strings['charset'] = 'DEFAULT CHARACTER SET '.$options['charset'];
if (!empty($options['collate'])) {
$options_strings['charset'].= ' COLLATE '.$options['collate'];
}
}
$type = false;
if (!empty($options['type'])) {
$type = $options['type'];
} elseif ($db->options['default_table_type']) {
$type = $db->options['default_table_type'];
}
if ($type) {
$options_strings[] = "ENGINE = $type";
}
$query = "CREATE TABLE $sequence_name ($seqcol_name INT NOT NULL AUTO_INCREMENT, PRIMARY KEY ($seqcol_name))"; $query = "CREATE TABLE $sequence_name ($seqcol_name INT NOT NULL AUTO_INCREMENT, PRIMARY KEY ($seqcol_name))";
$query.= strlen($db->options['default_table_type']) ? ' TYPE='.$db->options['default_table_type'] : ''; if (!empty($options_strings)) {
$query .= ' '.implode(' ', $options_strings);
}
$res = $db->exec($query); $res = $db->exec($query);
if (PEAR::isError($res)) { if (PEAR::isError($res)) {
@@ -865,7 +965,7 @@ class MDB2_Driver_Manager_mysql extends MDB2_Driver_Manager_Common
* list all sequences in the current database * list all sequences in the current database
* *
* @param string database, the current is default * @param string database, the current is default
* @return mixed data array on success, a MDB2 error on failure * @return mixed array of sequence names on success, a MDB2 error on failure
* @access public * @access public
*/ */
function listSequences($database = null) function listSequences($database = null)

View File

@@ -2,7 +2,7 @@
// +----------------------------------------------------------------------+ // +----------------------------------------------------------------------+
// | PHP versions 4 and 5 | // | PHP versions 4 and 5 |
// +----------------------------------------------------------------------+ // +----------------------------------------------------------------------+
// | Copyright (c) 1998-2006 Manuel Lemos, Tomas V.V.Cox, | // | Copyright (c) 1998-2007 Manuel Lemos, Tomas V.V.Cox, |
// | Stig. S. Bakken, Lukas Smith | // | Stig. S. Bakken, Lukas Smith |
// | All rights reserved. | // | All rights reserved. |
// +----------------------------------------------------------------------+ // +----------------------------------------------------------------------+
@@ -139,8 +139,7 @@ class MDB2_Driver_Manager_mysqli extends MDB2_Driver_Manager_Common
* @param array $options An associative array of table options: * @param array $options An associative array of table options:
* array( * array(
* 'comment' => 'Foo', * 'comment' => 'Foo',
* 'character_set' => 'utf8', * 'charset' => 'utf8',
* 'collate' => 'utf8_unicode_ci',
* 'collate' => 'utf8_unicode_ci', * 'collate' => 'utf8_unicode_ci',
* 'type' => 'innodb', * 'type' => 'innodb',
* ); * );
@@ -380,7 +379,7 @@ class MDB2_Driver_Manager_mysqli extends MDB2_Driver_Manager_Common
/** /**
* list all databases * list all databases
* *
* @return mixed data array on success, a MDB2 error on failure * @return mixed array of database names on success, a MDB2 error on failure
* @access public * @access public
*/ */
function listDatabases() function listDatabases()
@@ -406,7 +405,7 @@ class MDB2_Driver_Manager_mysqli extends MDB2_Driver_Manager_Common
/** /**
* list all users * list all users
* *
* @return mixed data array on success, a MDB2 error on failure * @return mixed array of user names on success, a MDB2 error on failure
* @access public * @access public
*/ */
function listUsers() function listUsers()
@@ -416,7 +415,71 @@ class MDB2_Driver_Manager_mysqli extends MDB2_Driver_Manager_Common
return $db; return $db;
} }
return $db->queryCol('SELECT DISTINCT USER FROM USER'); return $db->queryCol('SELECT DISTINCT USER FROM mysql.USER');
}
// }}}
// {{{ listFunctions()
/**
* list all functions in the current database
*
* @return mixed array of function names on success, a MDB2 error on failure
* @access public
*/
function listFunctions()
{
$db =& $this->getDBInstance();
if (PEAR::isError($db)) {
return $db;
}
$query = "SELECT name FROM mysql.proc";
/*
SELECT ROUTINE_NAME
FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_TYPE = 'FUNCTION'
*/
$result = $db->queryCol($query);
if (PEAR::isError($result)) {
return $result;
}
if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
$result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
}
return $result;
}
// }}}
// {{{ listTableTriggers()
/**
* list all triggers in the database that reference a given table
*
* @param string table for which all referenced triggers should be found
* @return mixed array of trigger names on success, a MDB2 error on failure
* @access public
*/
function listTableTriggers($table = null)
{
$db =& $this->getDBInstance();
if (PEAR::isError($db)) {
return $db;
}
$query = 'SHOW TRIGGERS';
if (!is_null($table)) {
$table = $db->quote($table, 'text');
$query .= " LIKE $table";
}
$result = $db->queryCol($query);
if (PEAR::isError($result)) {
return $result;
}
if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
$result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
}
return $result;
} }
// }}} // }}}
@@ -426,7 +489,7 @@ class MDB2_Driver_Manager_mysqli extends MDB2_Driver_Manager_Common
* list all tables in the current database * list all tables in the current database
* *
* @param string database, the current is default * @param string database, the current is default
* @return mixed data array on success, a MDB2 error on failure * @return mixed array of table names on success, a MDB2 error on failure
* @access public * @access public
*/ */
function listTables($database = null) function listTables($database = null)
@@ -463,12 +526,12 @@ class MDB2_Driver_Manager_mysqli extends MDB2_Driver_Manager_Common
// {{{ listViews() // {{{ listViews()
/** /**
* list the views in the database * list all views in the current database
* *
* @param string database, the current is default * @param string database, the current is default
* @return mixed MDB2_OK on success, a MDB2 error on failure * @return mixed array of view names on success, a MDB2 error on failure
* @access public * @access public
**/ */
function listViews($database = null) function listViews($database = null)
{ {
$db =& $this->getDBInstance(); $db =& $this->getDBInstance();
@@ -497,10 +560,10 @@ class MDB2_Driver_Manager_mysqli extends MDB2_Driver_Manager_Common
// {{{ listTableFields() // {{{ listTableFields()
/** /**
* list all fields in a tables in the current database * list all fields in a table in the current database
* *
* @param string $table name of table that should be used in method * @param string $table name of table that should be used in method
* @return mixed data array on success, a MDB2 error on failure * @return mixed array of field names on success, a MDB2 error on failure
* @access public * @access public
*/ */
function listTableFields($table) function listTableFields($table)
@@ -610,7 +673,7 @@ class MDB2_Driver_Manager_mysqli extends MDB2_Driver_Manager_Common
* list all indexes in a table * list all indexes in a table
* *
* @param string $table name of table that should be used in method * @param string $table name of table that should be used in method
* @return mixed data array on success, a MDB2 error on failure * @return mixed array of index names on success, a MDB2 error on failure
* @access public * @access public
*/ */
function listTableIndexes($table) function listTableIndexes($table)
@@ -692,6 +755,10 @@ class MDB2_Driver_Manager_mysqli extends MDB2_Driver_Manager_Common
} elseif (!empty($definition['unique'])) { } elseif (!empty($definition['unique'])) {
$type = 'UNIQUE'; $type = 'UNIQUE';
} }
if (empty($type)) {
return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
'invalid definition, could not create constraint', __FUNCTION__);
}
$table = $db->quoteIdentifier($table, true); $table = $db->quoteIdentifier($table, true);
$query = "ALTER TABLE $table ADD $type $name"; $query = "ALTER TABLE $table ADD $type $name";
@@ -739,7 +806,7 @@ class MDB2_Driver_Manager_mysqli extends MDB2_Driver_Manager_Common
* list all constraints in a table * list all constraints in a table
* *
* @param string $table name of table that should be used in method * @param string $table name of table that should be used in method
* @return mixed data array on success, a MDB2 error on failure * @return mixed array of constraint names on success, a MDB2 error on failure
* @access public * @access public
*/ */
function listTableConstraints($table) function listTableConstraints($table)
@@ -796,10 +863,17 @@ class MDB2_Driver_Manager_mysqli extends MDB2_Driver_Manager_Common
* *
* @param string $seq_name name of the sequence to be created * @param string $seq_name name of the sequence to be created
* @param string $start start value of the sequence; default is 1 * @param string $start start value of the sequence; default is 1
* @param array $options An associative array of table options:
* array(
* 'comment' => 'Foo',
* 'charset' => 'utf8',
* 'collate' => 'utf8_unicode_ci',
* 'type' => 'innodb',
* );
* @return mixed MDB2_OK on success, a MDB2 error on failure * @return mixed MDB2_OK on success, a MDB2 error on failure
* @access public * @access public
*/ */
function createSequence($seq_name, $start = 1) function createSequence($seq_name, $start = 1, $options = array())
{ {
$db =& $this->getDBInstance(); $db =& $this->getDBInstance();
if (PEAR::isError($db)) { if (PEAR::isError($db)) {
@@ -809,10 +883,38 @@ class MDB2_Driver_Manager_mysqli extends MDB2_Driver_Manager_Common
$sequence_name = $db->quoteIdentifier($db->getSequenceName($seq_name), true); $sequence_name = $db->quoteIdentifier($db->getSequenceName($seq_name), true);
$seqcol_name = $db->quoteIdentifier($db->options['seqcol_name'], true); $seqcol_name = $db->quoteIdentifier($db->options['seqcol_name'], true);
$query = "CREATE TABLE $sequence_name ($seqcol_name INT NOT NULL AUTO_INCREMENT, PRIMARY KEY ($seqcol_name))"; $options_strings = array();
$query.= strlen($db->options['default_table_type']) ? ' TYPE='.$db->options['default_table_type'] : '';
$res = $db->exec($query);
if (!empty($options['comment'])) {
$options_strings['comment'] = 'COMMENT = '.$db->quote($options['comment'], 'text');
}
if (!empty($options['charset'])) {
$options_strings['charset'] = 'DEFAULT CHARACTER SET '.$options['charset'];
if (!empty($options['collate'])) {
$options_strings['charset'].= ' COLLATE '.$options['collate'];
}
}
$type = false;
if (!empty($options['type'])) {
$type = $options['type'];
} elseif ($db->options['default_table_type']) {
$type = $db->options['default_table_type'];
}
if ($type) {
$options_strings[] = "ENGINE = $type";
}
if (!empty($options_strings)) {
$query.= ' '.implode(' ', $options_strings);
}
$query = "CREATE TABLE $sequence_name ($seqcol_name INT NOT NULL AUTO_INCREMENT, PRIMARY KEY ($seqcol_name))";
if (!empty($options_strings)) {
$query .= ' '.implode(' ', $options_strings);
}
$res = $db->exec($query);
if (PEAR::isError($res)) { if (PEAR::isError($res)) {
return $res; return $res;
} }
@@ -866,7 +968,7 @@ class MDB2_Driver_Manager_mysqli extends MDB2_Driver_Manager_Common
* list all sequences in the current database * list all sequences in the current database
* *
* @param string database, the current is default * @param string database, the current is default
* @return mixed data array on success, a MDB2 error on failure * @return mixed array of sequence names on success, a MDB2 error on failure
* @access public * @access public
*/ */
function listSequences($database = null) function listSequences($database = null)

View File

@@ -63,7 +63,7 @@ class MDB2_Driver_Manager_pgsql extends MDB2_Driver_Manager_Common
* @param string $name name of the database that should be created * @param string $name name of the database that should be created
* @return mixed MDB2_OK on success, a MDB2 error on failure * @return mixed MDB2_OK on success, a MDB2 error on failure
* @access public * @access public
**/ */
function createDatabase($name) function createDatabase($name)
{ {
$db =& $this->getDBInstance(); $db =& $this->getDBInstance();
@@ -84,7 +84,7 @@ class MDB2_Driver_Manager_pgsql extends MDB2_Driver_Manager_Common
* @param string $name name of the database that should be dropped * @param string $name name of the database that should be dropped
* @return mixed MDB2_OK on success, a MDB2 error on failure * @return mixed MDB2_OK on success, a MDB2 error on failure
* @access public * @access public
**/ */
function dropDatabase($name) function dropDatabase($name)
{ {
$db =& $this->getDBInstance(); $db =& $this->getDBInstance();
@@ -299,9 +299,9 @@ class MDB2_Driver_Manager_pgsql extends MDB2_Driver_Manager_Common
/** /**
* list all databases * list all databases
* *
* @return mixed data array on success, a MDB2 error on failure * @return mixed array of database names on success, a MDB2 error on failure
* @access public * @access public
**/ */
function listDatabases() function listDatabases()
{ {
$db =& $this->getDBInstance(); $db =& $this->getDBInstance();
@@ -332,9 +332,9 @@ class MDB2_Driver_Manager_pgsql extends MDB2_Driver_Manager_Common
/** /**
* list all users * list all users
* *
* @return mixed data array on success, a MDB2 error on failure * @return mixed array of user names on success, a MDB2 error on failure
* @access public * @access public
**/ */
function listUsers() function listUsers()
{ {
$db =& $this->getDBInstance(); $db =& $this->getDBInstance();
@@ -357,11 +357,11 @@ class MDB2_Driver_Manager_pgsql extends MDB2_Driver_Manager_Common
// {{{ listViews() // {{{ listViews()
/** /**
* list the views in the database * list all views in the current database
* *
* @return mixed MDB2_OK on success, a MDB2 error on failure * @return mixed array of view names on success, a MDB2 error on failure
* @access public * @access public
**/ */
function listViews() function listViews()
{ {
$db =& $this->getDBInstance(); $db =& $this->getDBInstance();
@@ -369,7 +369,10 @@ class MDB2_Driver_Manager_pgsql extends MDB2_Driver_Manager_Common
return $db; return $db;
} }
$query = 'SELECT viewname FROM pg_views'; $query = "SELECT viewname
FROM pg_views
WHERE schemaname NOT IN ('pg_catalog', 'information_schema')
AND viewname !~ '^pg_'";
$result = $db->queryCol($query); $result = $db->queryCol($query);
if (PEAR::isError($result)) { if (PEAR::isError($result)) {
return $result; return $result;
@@ -386,10 +389,10 @@ class MDB2_Driver_Manager_pgsql extends MDB2_Driver_Manager_Common
/** /**
* list the views in the database that reference a given table * list the views in the database that reference a given table
* *
* @param string table for which all references views should be found * @param string table for which all referenced views should be found
* @return mixed MDB2_OK on success, a MDB2 error on failure * @return mixed array of view names on success, a MDB2 error on failure
* @access public * @access public
**/ */
function listTableViews($table) function listTableViews($table)
{ {
$db =& $this->getDBInstance(); $db =& $this->getDBInstance();
@@ -415,7 +418,7 @@ class MDB2_Driver_Manager_pgsql extends MDB2_Driver_Manager_Common
/** /**
* list all functions in the current database * list all functions in the current database
* *
* @return mixed data array on success, a MDB2 error on failure * @return mixed array of function names on success, a MDB2 error on failure
* @access public * @access public
*/ */
function listFunctions() function listFunctions()
@@ -447,15 +450,50 @@ class MDB2_Driver_Manager_pgsql extends MDB2_Driver_Manager_Common
return $result; return $result;
} }
// }}}
// {{{ listTableTriggers()
/**
* list all triggers in the database that reference a given table
*
* @param string table for which all referenced triggers should be found
* @return mixed array of trigger names on success, a MDB2 error on failure
* @access public
*/
function listTableTriggers($table = null)
{
$db =& $this->getDBInstance();
if (PEAR::isError($db)) {
return $db;
}
$query = 'SELECT trg.tgname AS trigger_name
FROM pg_trigger trg,
pg_class tbl
WHERE trg.tgrelid = tbl.oid';
if (!is_null($table)) {
$table = $db->quote(strtoupper($table), 'text');
$query .= " AND tbl.relname = $table";
}
$result = $db->queryCol($query);
if (PEAR::isError($result)) {
return $result;
}
if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
$result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
}
return $result;
}
// }}} // }}}
// {{{ listTables() // {{{ listTables()
/** /**
* list all tables in the current database * list all tables in the current database
* *
* @return mixed data array on success, a MDB2 error on failure * @return mixed array of table names on success, a MDB2 error on failure
* @access public * @access public
**/ */
function listTables() function listTables()
{ {
$db =& $this->getDBInstance(); $db =& $this->getDBInstance();
@@ -497,10 +535,10 @@ class MDB2_Driver_Manager_pgsql extends MDB2_Driver_Manager_Common
// {{{ listTableFields() // {{{ listTableFields()
/** /**
* list all fields in a tables in the current database * list all fields in a table in the current database
* *
* @param string $table name of table that should be used in method * @param string $table name of table that should be used in method
* @return mixed data array on success, a MDB2 error on failure * @return mixed array of field names on success, a MDB2 error on failure
* @access public * @access public
*/ */
function listTableFields($table) function listTableFields($table)
@@ -531,7 +569,7 @@ class MDB2_Driver_Manager_pgsql extends MDB2_Driver_Manager_Common
* list all indexes in a table * list all indexes in a table
* *
* @param string $table name of table that should be used in method * @param string $table name of table that should be used in method
* @return mixed data array on success, a MDB2 error on failure * @return mixed array of index names on success, a MDB2 error on failure
* @access public * @access public
*/ */
function listTableIndexes($table) function listTableIndexes($table)
@@ -571,7 +609,7 @@ class MDB2_Driver_Manager_pgsql extends MDB2_Driver_Manager_Common
* list all constraints in a table * list all constraints in a table
* *
* @param string $table name of table that should be used in method * @param string $table name of table that should be used in method
* @return mixed data array on success, a MDB2 error on failure * @return mixed array of constraint names on success, a MDB2 error on failure
* @access public * @access public
*/ */
function listTableConstraints($table) function listTableConstraints($table)
@@ -616,7 +654,7 @@ class MDB2_Driver_Manager_pgsql extends MDB2_Driver_Manager_Common
* @param string $start start value of the sequence; default is 1 * @param string $start start value of the sequence; default is 1
* @return mixed MDB2_OK on success, a MDB2 error on failure * @return mixed MDB2_OK on success, a MDB2 error on failure
* @access public * @access public
**/ */
function createSequence($seq_name, $start = 1) function createSequence($seq_name, $start = 1)
{ {
$db =& $this->getDBInstance(); $db =& $this->getDBInstance();
@@ -638,7 +676,7 @@ class MDB2_Driver_Manager_pgsql extends MDB2_Driver_Manager_Common
* @param string $seq_name name of the sequence to be dropped * @param string $seq_name name of the sequence to be dropped
* @return mixed MDB2_OK on success, a MDB2 error on failure * @return mixed MDB2_OK on success, a MDB2 error on failure
* @access public * @access public
**/ */
function dropSequence($seq_name) function dropSequence($seq_name)
{ {
$db =& $this->getDBInstance(); $db =& $this->getDBInstance();
@@ -656,9 +694,9 @@ class MDB2_Driver_Manager_pgsql extends MDB2_Driver_Manager_Common
/** /**
* list all sequences in the current database * list all sequences in the current database
* *
* @return mixed data array on success, a MDB2 error on failure * @return mixed array of sequence names on success, a MDB2 error on failure
* @access public * @access public
**/ */
function listSequences() function listSequences()
{ {
$db =& $this->getDBInstance(); $db =& $this->getDBInstance();

View File

@@ -45,8 +45,7 @@
// $Id$ // $Id$
// //
//require_once 'MDB2/Driver/Native/Common.php'; require_once 'MDB2/Driver/Native/Common.php';
require_once PEAR_PATH."/MDB2/Driver/Native/Common.php";
/** /**
* MDB2 MSSQL driver for the native module * MDB2 MSSQL driver for the native module
@@ -55,7 +54,7 @@ require_once PEAR_PATH."/MDB2/Driver/Native/Common.php";
* @category Database * @category Database
* @author Lukas Smith <smith@dybnet.de> * @author Lukas Smith <smith@dybnet.de>
*/ */
class MDB2_Driver_Native_mssql extends MDB2_Module_Common class MDB2_Driver_Native_mssql extends MDB2_Driver_Native_Common
{ {
} }
?> ?>

View File

@@ -45,8 +45,7 @@
// $Id$ // $Id$
// //
//require_once 'MDB2/Driver/Native/Common.php'; require_once 'MDB2/Driver/Native/Common.php';
require_once PEAR_PATH."/MDB2/Driver/Native/Common.php";
/** /**
* MDB2 MySQL driver for the native module * MDB2 MySQL driver for the native module

View File

@@ -45,8 +45,7 @@
// $Id$ // $Id$
// //
//require_once 'MDB2/Driver/Native/Common.php'; require_once 'MDB2/Driver/Native/Common.php';
require_once PEAR_PATH."/MDB2/Driver/Native/Common.php";
/** /**
* MDB2 MySQLi driver for the native module * MDB2 MySQLi driver for the native module

View File

@@ -44,8 +44,7 @@
// //
// $Id$ // $Id$
//require_once 'MDB2/Driver/Native/Common.php'; require_once 'MDB2/Driver/Native/Common.php';
require_once PEAR_PATH."/MDB2/Driver/Native/Common.php";
/** /**
* MDB2 PostGreSQL driver for the native module * MDB2 PostGreSQL driver for the native module

View File

@@ -73,11 +73,14 @@ class MDB2_Driver_Reverse_Common extends MDB2_Module_Common
// {{{ getTableFieldDefinition() // {{{ getTableFieldDefinition()
/** /**
* Get the stucture of a field into an array * Get the structure of a field into an array
* *
* @param string $table name of table that should be used in method * @param string $table name of table that should be used in method
* @param string $fields name of field that should be used in method * @param string $field name of field that should be used in method
* @return mixed data array on success, a MDB2 error on failure * @return mixed data array on success, a MDB2 error on failure.
* The returned array contains an array for each field definition,
* with all or some of these indices, depending on the field data type:
* [notnull] [nativetype] [length] [fixed] [default] [type] [mdb2type]
* @access public * @access public
*/ */
function getTableFieldDefinition($table, $field) function getTableFieldDefinition($table, $field)
@@ -95,11 +98,23 @@ class MDB2_Driver_Reverse_Common extends MDB2_Module_Common
// {{{ getTableIndexDefinition() // {{{ getTableIndexDefinition()
/** /**
* Get the stucture of an index into an array * Get the structure of an index into an array
* *
* @param string $table name of table that should be used in method * @param string $table name of table that should be used in method
* @param string $index name of index that should be used in method * @param string $index name of index that should be used in method
* @return mixed data array on success, a MDB2 error on failure * @return mixed data array on success, a MDB2 error on failure
* The returned array has this structure:
* </pre>
* array (
* [fields] => array (
* [field1name] => array() // one entry per each field covered
* [field2name] => array() // by the index
* [field3name] => array(
* [sorting] => ascending
* )
* )
* );
* </pre>
* @access public * @access public
*/ */
function getTableIndexDefinition($table, $index) function getTableIndexDefinition($table, $index)
@@ -117,11 +132,24 @@ class MDB2_Driver_Reverse_Common extends MDB2_Module_Common
// {{{ getTableConstraintDefinition() // {{{ getTableConstraintDefinition()
/** /**
* Get the stucture of an constraints into an array * Get the structure of an constraints into an array
* *
* @param string $table name of table that should be used in method * @param string $table name of table that should be used in method
* @param string $index name of index that should be used in method * @param string $index name of index that should be used in method
* @return mixed data array on success, a MDB2 error on failure * @return mixed data array on success, a MDB2 error on failure
* The returned array has this structure:
* <pre>
* array (
* [primary] => 1
* [fields] => array (
* [field1name] => array() // one entry per each field covered
* [field2name] => array() // by the index
* [field3name] => array(
* [sorting] => ascending
* )
* )
* );
* </pre>
* @access public * @access public
*/ */
function getTableConstraintDefinition($table, $index) function getTableConstraintDefinition($table, $index)
@@ -139,10 +167,16 @@ class MDB2_Driver_Reverse_Common extends MDB2_Module_Common
// {{{ getSequenceDefinition() // {{{ getSequenceDefinition()
/** /**
* Get the stucture of a sequence into an array * Get the structure of a sequence into an array
* *
* @param string $sequence name of sequence that should be used in method * @param string $sequence name of sequence that should be used in method
* @return mixed data array on success, a MDB2 error on failure * @return mixed data array on success, a MDB2 error on failure
* The returned array has this structure:
* <pre>
* array (
* [start] => n
* );
* </pre>
* @access public * @access public
*/ */
function getSequenceDefinition($sequence) function getSequenceDefinition($sequence)
@@ -173,10 +207,29 @@ class MDB2_Driver_Reverse_Common extends MDB2_Module_Common
// {{{ getTriggerDefinition() // {{{ getTriggerDefinition()
/** /**
* Get the stucture of an trigger into an array * Get the structure of a trigger into an array
*
* EXPERIMENTAL
*
* WARNING: this function is experimental and may change the returned value
* at any time until labelled as non-experimental
* *
* @param string $trigger name of trigger that should be used in method * @param string $trigger name of trigger that should be used in method
* @return mixed data array on success, a MDB2 error on failure * @return mixed data array on success, a MDB2 error on failure
* The returned array has this structure:
* <pre>
* array (
* [trigger_name] => 'trigger name',
* [table_name] => 'table name',
* [trigger_body] => 'trigger body definition',
* [trigger_type] => 'BEFORE' | 'AFTER',
* [trigger_event] => 'INSERT' | 'UPDATE' | 'DELETE'
* //or comma separated list of multiple events, when supported
* [trigger_enabled] => true|false
* [trigger_comment] => 'trigger comment',
* );
* </pre>
* The oci8 driver also returns a [when_clause] index.
* @access public * @access public
*/ */
function getTriggerDefinition($trigger) function getTriggerDefinition($trigger)

View File

@@ -2,7 +2,7 @@
// +----------------------------------------------------------------------+ // +----------------------------------------------------------------------+
// | PHP versions 4 and 5 | // | PHP versions 4 and 5 |
// +----------------------------------------------------------------------+ // +----------------------------------------------------------------------+
// | Copyright (c) 1998-2006 Manuel Lemos, Tomas V.V.Cox, | // | Copyright (c) 1998-2007 Manuel Lemos, Tomas V.V.Cox, |
// | Stig. S. Bakken, Lukas Smith, Frank M. Kromann | // | Stig. S. Bakken, Lukas Smith, Frank M. Kromann |
// | All rights reserved. | // | All rights reserved. |
// +----------------------------------------------------------------------+ // +----------------------------------------------------------------------+
@@ -39,14 +39,14 @@
// | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | // | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
// | POSSIBILITY OF SUCH DAMAGE. | // | POSSIBILITY OF SUCH DAMAGE. |
// +----------------------------------------------------------------------+ // +----------------------------------------------------------------------+
// | Author: Lukas Smith <smith@pooteeweet.org> | // | Authors: Lukas Smith <smith@pooteeweet.org> |
// | Lorenzo Alberton <l.alberton@quipo.it> |
// +----------------------------------------------------------------------+ // +----------------------------------------------------------------------+
// //
// $Id$ // $Id$
// //
//require_once 'MDB2/Driver/Reverse/Common.php'; require_once 'MDB2/Driver/Reverse/Common.php';
require_once PEAR_PATH."/MDB2/Driver/Reverse/Common.php";
/** /**
* MDB2 MSSQL driver for the schema reverse engineering module * MDB2 MSSQL driver for the schema reverse engineering module
@@ -54,9 +54,349 @@ require_once PEAR_PATH."/MDB2/Driver/Reverse/Common.php";
* @package MDB2 * @package MDB2
* @category Database * @category Database
* @author Lukas Smith <smith@dybnet.de> * @author Lukas Smith <smith@dybnet.de>
* @author Lorenzo Alberton <l.alberton@quipo.it>
*/ */
class MDB2_Driver_Reverse_mssql extends MDB2_Driver_Reverse_Common class MDB2_Driver_Reverse_mssql extends MDB2_Driver_Reverse_Common
{ {
// {{{ getTableFieldDefinition()
/**
* Get the structure of a field into an array
*
* @param string $table name of table that should be used in method
* @param string $field_name name of field that should be used in method
* @return mixed data array on success, a MDB2 error on failure
* @access public
*/
function getTableFieldDefinition($table, $field_name)
{
$db =& $this->getDBInstance();
if (PEAR::isError($db)) {
return $db;
}
$result = $db->loadModule('Datatype', null, true);
if (PEAR::isError($result)) {
return $result;
}
$table = $db->quoteIdentifier($table, true);
$fldname = $db->quoteIdentifier($field_name, true);
$query = "SELECT t.table_name,
c.column_name 'name',
c.data_type 'type',
CASE c.is_nullable WHEN 'YES' THEN 1 ELSE 0 END AS 'is_nullable',
c.column_default,
c.character_maximum_length 'length',
c.numeric_precision,
c.numeric_scale,
c.character_set_name,
c.collation_name
FROM INFORMATION_SCHEMA.TABLES t,
INFORMATION_SCHEMA.COLUMNS c
WHERE t.table_name = c.table_name
AND t.table_name = '$table'
AND c.column_name = '$fldname'
ORDER BY t.table_name";
$column = $db->queryRow($query, null, MDB2_FETCHMODE_ASSOC);
if (PEAR::isError($column)) {
return $column;
}
if (empty($column)) {
return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
'it was not specified an existing table column', __FUNCTION__);
}
if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
if ($db->options['field_case'] == CASE_LOWER) {
$column['name'] = strtolower($column['name']);
} else {
$column['name'] = strtoupper($column['name']);
}
} else {
$column = array_change_key_case($column, $db->options['field_case']);
}
$mapped_datatype = $db->datatype->mapNativeDatatype($column);
if (PEAR::IsError($mapped_datatype)) {
return $mapped_datatype;
}
list($types, $length, $unsigned, $fixed) = $mapped_datatype;
$notnull = true;
if ($column['is_nullable']) {
$notnull = false;
}
$default = false;
if (array_key_exists('column_default', $column)) {
$default = $column['column_default'];
if (is_null($default) && $notnull) {
$default = '';
} elseif (strlen($default) > 4
&& substr($default, 0, 1) == '('
&& substr($default, -1, 1) == ')'
) {
//mssql wraps the default value in parentheses: "((1234))", "(NULL)"
$default = trim($default, '()');
if ($default == 'NULL') {
$default = null;
}
}
}
$definition[0] = array(
'notnull' => $notnull,
'nativetype' => preg_replace('/^([a-z]+)[^a-z].*/i', '\\1', $column['type'])
);
if (!is_null($length)) {
$definition[0]['length'] = $length;
}
if (!is_null($unsigned)) {
$definition[0]['unsigned'] = $unsigned;
}
if (!is_null($fixed)) {
$definition[0]['fixed'] = $fixed;
}
if ($default !== false) {
$definition[0]['default'] = $default;
}
foreach ($types as $key => $type) {
$definition[$key] = $definition[0];
if ($type == 'clob' || $type == 'blob') {
unset($definition[$key]['default']);
}
$definition[$key]['type'] = $type;
$definition[$key]['mdb2type'] = $type;
}
return $definition;
}
// }}}
// {{{ getTableIndexDefinition()
/**
* Get the structure of an index into an array
*
* @param string $table name of table that should be used in method
* @param string $index_name name of index that should be used in method
* @return mixed data array on success, a MDB2 error on failure
* @access public
*/
function getTableIndexDefinition($table, $index_name)
{
$db =& $this->getDBInstance();
if (PEAR::isError($db)) {
return $db;
}
$table = $db->quoteIdentifier($table, true);
//$idxname = $db->quoteIdentifier($index_name, true);
$query = "SELECT OBJECT_NAME(i.id) tablename,
i.name indexname,
c.name field_name,
CASE INDEXKEY_PROPERTY(i.id, i.indid, ik.keyno, 'IsDescending')
WHEN 1 THEN 'DESC' ELSE 'ASC'
END 'collation',
ik.keyno 'position'
FROM sysindexes i
JOIN sysindexkeys ik ON ik.id = i.id AND ik.indid = i.indid
JOIN syscolumns c ON c.id = ik.id AND c.colid = ik.colid
WHERE OBJECT_NAME(i.id) = '$table'
AND i.name = '%s'
AND NOT EXISTS (
SELECT *
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE k
WHERE k.table_name = OBJECT_NAME(i.id)
AND k.constraint_name = i.name)
ORDER BY tablename, indexname, ik.keyno";
$index_name_mdb2 = $db->getIndexName($index_name);
$result = $db->queryRow(sprintf($query, $index_name_mdb2));
if (!PEAR::isError($result) && !is_null($result)) {
// apply 'idxname_format' only if the query succeeded, otherwise
// fallback to the given $index_name, without transformation
$index_name = $index_name_mdb2;
}
$result = $db->query(sprintf($query, $index_name));
if (PEAR::isError($result)) {
return $result;
}
$definition = array();
while (is_array($row = $result->fetchRow(MDB2_FETCHMODE_ASSOC))) {
$column_name = $row['field_name'];
if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
if ($db->options['field_case'] == CASE_LOWER) {
$column_name = strtolower($column_name);
} else {
$column_name = strtoupper($column_name);
}
}
$definition['fields'][$column_name] = array(
'position' => (int)$row['position'],
);
if (!empty($row['collation'])) {
$definition['fields'][$column_name]['sorting'] = ($row['collation'] == 'ASC'
? 'ascending' : 'descending');
}
}
$result->free();
if (empty($definition['fields'])) {
return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
'it was not specified an existing table index', __FUNCTION__);
}
return $definition;
}
// }}}
// {{{ getTableConstraintDefinition()
/**
* Get the structure of a constraint into an array
*
* @param string $table name of table that should be used in method
* @param string $constraint_name name of constraint that should be used in method
* @return mixed data array on success, a MDB2 error on failure
* @access public
*/
function getTableConstraintDefinition($table, $constraint_name)
{
$db =& $this->getDBInstance();
if (PEAR::isError($db)) {
return $db;
}
$table = $db->quoteIdentifier($table, true);
$query = "SELECT k.table_name,
k.column_name field_name,
CASE c.constraint_type WHEN 'PRIMARY KEY' THEN 1 ELSE 0 END 'primary',
CASE c.constraint_type WHEN 'UNIQUE' THEN 1 ELSE 0 END 'unique',
CASE c.constraint_type WHEN 'FOREIGN KEY' THEN 1 ELSE 0 END 'foreign',
CASE c.constraint_type WHEN 'CHECK' THEN 1 ELSE 0 END 'check',
k.ordinal_position
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE k
LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS c
ON k.table_name = c.table_name
AND k.constraint_name = c.constraint_name
AND k.table_schema = c.table_schema
WHERE k.constraint_catalog = DB_NAME()
AND k.table_name = '$table'
AND k.constraint_name = '%s'
ORDER BY k.constraint_name,
k.ordinal_position";
$constraint_name_mdb2 = $db->getIndexName($constraint_name);
$result = $db->queryRow(sprintf($query, $constraint_name_mdb2));
if (!PEAR::isError($result) && !is_null($result)) {
// apply 'idxname_format' only if the query succeeded, otherwise
// fallback to the given $index_name, without transformation
$constraint_name = $constraint_name_mdb2;
}
$result = $db->query(sprintf($query, $constraint_name));
if (PEAR::isError($result)) {
return $result;
}
$definition = array();
while (is_array($row = $result->fetchRow(MDB2_FETCHMODE_ASSOC))) {
$column_name = $row['field_name'];
if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
if ($db->options['field_case'] == CASE_LOWER) {
$column_name = strtolower($column_name);
} else {
$column_name = strtoupper($column_name);
}
}
$definition['fields'][$column_name] = array(
'position' => (int)$row['ordinal_position']
);
/*
if (!empty($row['collation'])) {
$definition['fields'][$column_name]['sorting'] = ($row['collation'] == 'ASC'
? 'ascending' : 'descending');
}
*/
$definition['primary'] = $row['primary'];
$definition['unique'] = $row['unique'];
$definition['foreign'] = $row['foreign'];
$definition['check'] = $row['check'];
}
$result->free();
if (empty($definition['fields'])) {
return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
$constraint_name . ' is not an existing table constraint', __FUNCTION__);
}
return $definition;
}
// }}}
// {{{ getTriggerDefinition()
/**
* Get the structure of a trigger into an array
*
* EXPERIMENTAL
*
* WARNING: this function is experimental and may change the returned value
* at any time until labelled as non-experimental
*
* @param string $trigger name of trigger that should be used in method
* @return mixed data array on success, a MDB2 error on failure
* @access public
*/
function getTriggerDefinition($trigger)
{
$db =& $this->getDBInstance();
if (PEAR::isError($db)) {
return $db;
}
$query = "SELECT sys1.name trigger_name,
sys2.name table_name,
c.text trigger_body,
c.encrypted is_encripted,
CASE
WHEN OBJECTPROPERTY(sys1.id, 'ExecIsTriggerDisabled') = 1
THEN 0 ELSE 1
END trigger_enabled,
CASE
WHEN OBJECTPROPERTY(sys1.id, 'ExecIsInsertTrigger') = 1
THEN 'INSERT'
WHEN OBJECTPROPERTY(sys1.id, 'ExecIsUpdateTrigger') = 1
THEN 'UPDATE'
WHEN OBJECTPROPERTY(sys1.id, 'ExecIsDeleteTrigger') = 1
THEN 'DELETE'
END trigger_event,
CASE WHEN OBJECTPROPERTY(sys1.id, 'ExecIsInsteadOfTrigger') = 1
THEN 'INSTEAD OF' ELSE 'AFTER'
END trigger_type,
'' trigger_comment
FROM sysobjects sys1
JOIN sysobjects sys2 ON sys1.parent_obj = sys2.id
JOIN syscomments c ON sys1.id = c.id
WHERE sys1.xtype = 'TR'
AND sys1.name = ". $db->quote($trigger, 'text');
$types = array(
'trigger_name' => 'text',
'table_name' => 'text',
'trigger_body' => 'text',
'trigger_type' => 'text',
'trigger_event' => 'text',
'trigger_comment' => 'text',
'trigger_enabled' => 'boolean',
'is_encripted' => 'boolean',
);
$def = $db->queryRow($query, $types, MDB2_FETCHMODE_ASSOC);
if (PEAR::isError($def)) {
return $def;
}
$trg_body = $db->queryCol('EXEC sp_helptext '. $db->quote($trigger, 'text'), 'text');
if (!PEAR::isError($trg_body)) {
$def['trigger_body'] = implode('', $trg_body);
}
return $def;
}
// }}} // }}}
// {{{ tableInfo() // {{{ tableInfo()
@@ -80,42 +420,19 @@ class MDB2_Driver_Reverse_mssql extends MDB2_Driver_Reverse_Common
*/ */
function tableInfo($result, $mode = null) function tableInfo($result, $mode = null)
{ {
if (is_string($result)) {
return parent::tableInfo($result, $mode);
}
$db =& $this->getDBInstance(); $db =& $this->getDBInstance();
if (PEAR::isError($db)) { if (PEAR::isError($db)) {
return $db; return $db;
} }
if (is_string($result)) { $resource = MDB2::isResultCommon($result) ? $result->getResource() : $result;
/* if (!is_resource($resource)) {
* Probably received a table name. return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
* Create a result resource identifier. 'Could not generate result resource', __FUNCTION__);
*/
$query = 'SELECT TOP 0 * FROM '.$db->quoteIdentifier($result);
$id =& $db->_doQuery($query, false);
if (PEAR::isError($id)) {
return $id;
}
$got_string = true;
} elseif (MDB2::isResultCommon($result)) {
/*
* Probably received a result object.
* Extract the result resource identifier.
*/
$id = $result->getResource();
$got_string = false;
} else {
/*
* Probably received a result resource identifier.
* Copy it.
* Deprecated. Here for compatibility only.
*/
$id = $result;
$got_string = false;
}
if (!is_resource($id)) {
return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA);
} }
if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) { if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
@@ -128,7 +445,7 @@ class MDB2_Driver_Reverse_mssql extends MDB2_Driver_Reverse_Common
$case_func = 'strval'; $case_func = 'strval';
} }
$count = @mssql_num_fields($id); $count = @mssql_num_fields($resource);
$res = array(); $res = array();
if ($mode) { if ($mode) {
@@ -138,13 +455,11 @@ class MDB2_Driver_Reverse_mssql extends MDB2_Driver_Reverse_Common
$db->loadModule('Datatype', null, true); $db->loadModule('Datatype', null, true);
for ($i = 0; $i < $count; $i++) { for ($i = 0; $i < $count; $i++) {
$res[$i] = array( $res[$i] = array(
'table' => $got_string ? $case_func($result) : '', 'table' => '',
'name' => $case_func(@mssql_field_name($id, $i)), 'name' => $case_func(@mssql_field_name($resource, $i)),
'type' => @mssql_field_type($id, $i), 'type' => @mssql_field_type($resource, $i),
'length' => @mssql_field_length($id, $i), 'length' => @mssql_field_length($resource, $i),
// We only support flags for table 'flags' => '',
'flags' => $got_string
? $this->_mssql_field_flags($result, @mssql_field_name($id, $i)) : '',
); );
$mdb2type_info = $db->datatype->mapNativeDatatype($res[$i]); $mdb2type_info = $db->datatype->mapNativeDatatype($res[$i]);
if (PEAR::isError($mdb2type_info)) { if (PEAR::isError($mdb2type_info)) {
@@ -159,10 +474,6 @@ class MDB2_Driver_Reverse_mssql extends MDB2_Driver_Reverse_Common
} }
} }
// free the result only if we were called on a table
if ($got_string) {
@mssql_free_result($id);
}
return $res; return $res;
} }
@@ -274,5 +585,7 @@ class MDB2_Driver_Reverse_mssql extends MDB2_Driver_Reverse_Common
array_push($array, $value); array_push($array, $value);
} }
} }
// }}}
} }
?> ?>

View File

@@ -2,7 +2,7 @@
// +----------------------------------------------------------------------+ // +----------------------------------------------------------------------+
// | PHP versions 4 and 5 | // | PHP versions 4 and 5 |
// +----------------------------------------------------------------------+ // +----------------------------------------------------------------------+
// | Copyright (c) 1998-2006 Manuel Lemos, Tomas V.V.Cox, | // | Copyright (c) 1998-2007 Manuel Lemos, Tomas V.V.Cox, |
// | Stig. S. Bakken, Lukas Smith | // | Stig. S. Bakken, Lukas Smith |
// | All rights reserved. | // | All rights reserved. |
// +----------------------------------------------------------------------+ // +----------------------------------------------------------------------+
@@ -45,8 +45,7 @@
// $Id$ // $Id$
// //
//require_once 'MDB2/Driver/Reverse/Common.php'; require_once 'MDB2/Driver/Reverse/Common.php';
require_once PEAR_PATH."/MDB2/Driver/Reverse/Common.php";
/** /**
* MDB2 MySQL driver for the schema reverse engineering module * MDB2 MySQL driver for the schema reverse engineering module
@@ -54,13 +53,14 @@ require_once PEAR_PATH."/MDB2/Driver/Reverse/Common.php";
* @package MDB2 * @package MDB2
* @category Database * @category Database
* @author Lukas Smith <smith@pooteeweet.org> * @author Lukas Smith <smith@pooteeweet.org>
* @author Lorenzo Alberton <l.alberton@quipo.it>
*/ */
class MDB2_Driver_Reverse_mysql extends MDB2_Driver_Reverse_Common class MDB2_Driver_Reverse_mysql extends MDB2_Driver_Reverse_Common
{ {
// {{{ getTableFieldDefinition() // {{{ getTableFieldDefinition()
/** /**
* Get the stucture of a field into an array * Get the structure of a field into an array
* *
* @param string $table name of table that should be used in method * @param string $table name of table that should be used in method
* @param string $field_name name of field that should be used in method * @param string $field_name name of field that should be used in method
@@ -98,7 +98,11 @@ class MDB2_Driver_Reverse_mysql extends MDB2_Driver_Reverse_Common
$column = array_change_key_case($column, $db->options['field_case']); $column = array_change_key_case($column, $db->options['field_case']);
} }
if ($field_name == $column['name']) { if ($field_name == $column['name']) {
list($types, $length, $unsigned, $fixed) = $db->datatype->mapNativeDatatype($column); $mapped_datatype = $db->datatype->mapNativeDatatype($column);
if (PEAR::IsError($mapped_datatype)) {
return $mapped_datatype;
}
list($types, $length, $unsigned, $fixed) = $mapped_datatype;
$notnull = false; $notnull = false;
if (empty($column['null']) || $column['null'] !== 'YES') { if (empty($column['null']) || $column['null'] !== 'YES') {
$notnull = true; $notnull = true;
@@ -119,7 +123,7 @@ class MDB2_Driver_Reverse_mysql extends MDB2_Driver_Reverse_Common
'notnull' => $notnull, 'notnull' => $notnull,
'nativetype' => preg_replace('/^([a-z]+)[^a-z].*/i', '\\1', $column['type']) 'nativetype' => preg_replace('/^([a-z]+)[^a-z].*/i', '\\1', $column['type'])
); );
if ($length > 0) { if (!is_null($length)) {
$definition[0]['length'] = $length; $definition[0]['length'] = $length;
} }
if (!is_null($unsigned)) { if (!is_null($unsigned)) {
@@ -154,27 +158,34 @@ class MDB2_Driver_Reverse_mysql extends MDB2_Driver_Reverse_Common
// {{{ getTableIndexDefinition() // {{{ getTableIndexDefinition()
/** /**
* Get the stucture of an index into an array * Get the structure of an index into an array
* *
* @param string $table name of table that should be used in method * @param string $table name of table that should be used in method
* @param string $index_name name of index that should be used in method * @param string $constraint_name name of constraint that should be used in method
* @return mixed data array on success, a MDB2 error on failure * @return mixed data array on success, a MDB2 error on failure
* @access public * @access public
*/ */
function getTableIndexDefinition($table, $index_name) function getTableIndexDefinition($table, $constraint_name)
{ {
$db =& $this->getDBInstance(); $db =& $this->getDBInstance();
if (PEAR::isError($db)) { if (PEAR::isError($db)) {
return $db; return $db;
} }
$index_name = $db->getIndexName($index_name);
$table = $db->quoteIdentifier($table, true); $table = $db->quoteIdentifier($table, true);
$query = "SHOW INDEX FROM $table /*!50002 WHERE Key_name = ".$db->quote($index_name)." */"; $query = "SHOW INDEX FROM $table /*!50002 WHERE Key_name = %s */";
$result = $db->query($query); $constraint_name_mdb2 = $db->getIndexName($constraint_name);
$result = $db->queryRow(sprintf($query, $db->quote($constraint_name_mdb2)));
if (!PEAR::isError($result) && !is_null($result)) {
// apply 'idxname_format' only if the query succeeded, otherwise
// fallback to the given $index_name, without transformation
$constraint_name = $constraint_name_mdb2;
}
$result = $db->query(sprintf($query, $db->quote($constraint_name)));
if (PEAR::isError($result)) { if (PEAR::isError($result)) {
return $result; return $result;
} }
$colpos = 1;
$definition = array(); $definition = array();
while (is_array($row = $result->fetchRow(MDB2_FETCHMODE_ASSOC))) { while (is_array($row = $result->fetchRow(MDB2_FETCHMODE_ASSOC))) {
$row = array_change_key_case($row, CASE_LOWER); $row = array_change_key_case($row, CASE_LOWER);
@@ -186,10 +197,10 @@ class MDB2_Driver_Reverse_mysql extends MDB2_Driver_Reverse_Common
$key_name = strtoupper($key_name); $key_name = strtoupper($key_name);
} }
} }
if ($index_name == $key_name) { if ($constraint_name == $key_name) {
if (!$row['non_unique']) { if (!$row['non_unique']) {
return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null, return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
'it was not specified an existing table index', __FUNCTION__); $constraint_name . ' is not an existing table constraint', __FUNCTION__);
} }
$column_name = $row['column_name']; $column_name = $row['column_name'];
if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) { if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
@@ -199,7 +210,9 @@ class MDB2_Driver_Reverse_mysql extends MDB2_Driver_Reverse_Common
$column_name = strtoupper($column_name); $column_name = strtoupper($column_name);
} }
} }
$definition['fields'][$column_name] = array(); $definition['fields'][$column_name] = array(
'position' => $colpos++
);
if (!empty($row['collation'])) { if (!empty($row['collation'])) {
$definition['fields'][$column_name]['sorting'] = ($row['collation'] == 'A' $definition['fields'][$column_name]['sorting'] = ($row['collation'] == 'A'
? 'ascending' : 'descending'); ? 'ascending' : 'descending');
@@ -209,7 +222,7 @@ class MDB2_Driver_Reverse_mysql extends MDB2_Driver_Reverse_Common
$result->free(); $result->free();
if (empty($definition['fields'])) { if (empty($definition['fields'])) {
return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null, return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
'it was not specified an existing table index', __FUNCTION__); $constraint_name . ' is not an existing table constraint', __FUNCTION__);
} }
return $definition; return $definition;
} }
@@ -218,7 +231,7 @@ class MDB2_Driver_Reverse_mysql extends MDB2_Driver_Reverse_Common
// {{{ getTableConstraintDefinition() // {{{ getTableConstraintDefinition()
/** /**
* Get the stucture of a constraint into an array * Get the structure of a constraint into an array
* *
* @param string $table name of table that should be used in method * @param string $table name of table that should be used in method
* @param string $index_name name of index that should be used in method * @param string $index_name name of index that should be used in method
@@ -232,15 +245,22 @@ class MDB2_Driver_Reverse_mysql extends MDB2_Driver_Reverse_Common
return $db; return $db;
} }
if (strtolower($index_name) != 'primary') {
$index_name = $db->getIndexName($index_name);
}
$table = $db->quoteIdentifier($table, true); $table = $db->quoteIdentifier($table, true);
$query = "SHOW INDEX FROM $table /*!50002 WHERE Key_name = ".$db->quote($index_name)." */"; $query = "SHOW INDEX FROM $table /*!50002 WHERE Key_name = %s */";
$result = $db->query($query); if (strtolower($index_name) != 'primary') {
$index_name_mdb2 = $db->getIndexName($index_name);
$result = $db->queryRow(sprintf($query, $db->quote($index_name_mdb2)));
if (!PEAR::isError($result) && !is_null($result)) {
// apply 'idxname_format' only if the query succeeded, otherwise
// fallback to the given $index_name, without transformation
$index_name = $index_name_mdb2;
}
}
$result = $db->query(sprintf($query, $db->quote($index_name)));
if (PEAR::isError($result)) { if (PEAR::isError($result)) {
return $result; return $result;
} }
$colpos = 1;
$definition = array(); $definition = array();
while (is_array($row = $result->fetchRow(MDB2_FETCHMODE_ASSOC))) { while (is_array($row = $result->fetchRow(MDB2_FETCHMODE_ASSOC))) {
$row = array_change_key_case($row, CASE_LOWER); $row = array_change_key_case($row, CASE_LOWER);
@@ -270,7 +290,9 @@ class MDB2_Driver_Reverse_mysql extends MDB2_Driver_Reverse_Common
$column_name = strtoupper($column_name); $column_name = strtoupper($column_name);
} }
} }
$definition['fields'][$column_name] = array(); $definition['fields'][$column_name] = array(
'position' => $colpos++
);
if (!empty($row['collation'])) { if (!empty($row['collation'])) {
$definition['fields'][$column_name]['sorting'] = ($row['collation'] == 'A' $definition['fields'][$column_name]['sorting'] = ($row['collation'] == 'A'
? 'ascending' : 'descending'); ? 'ascending' : 'descending');
@@ -285,6 +307,51 @@ class MDB2_Driver_Reverse_mysql extends MDB2_Driver_Reverse_Common
return $definition; return $definition;
} }
// }}}
// {{{ getTriggerDefinition()
/**
* Get the structure of a trigger into an array
*
* EXPERIMENTAL
*
* WARNING: this function is experimental and may change the returned value
* at any time until labelled as non-experimental
*
* @param string $trigger name of trigger that should be used in method
* @return mixed data array on success, a MDB2 error on failure
* @access public
*/
function getTriggerDefinition($trigger)
{
$db =& $this->getDBInstance();
if (PEAR::isError($db)) {
return $db;
}
$query = 'SELECT trigger_name,
event_object_table AS table_name,
action_statement AS trigger_body,
action_timing AS trigger_type,
event_manipulation AS trigger_event
FROM information_schema.triggers
WHERE trigger_name = '. $db->quote($trigger, 'text');
$types = array(
'trigger_name' => 'text',
'table_name' => 'text',
'trigger_body' => 'text',
'trigger_type' => 'text',
'trigger_event' => 'text',
);
$def = $db->queryRow($query, $types, MDB2_FETCHMODE_ASSOC);
if (PEAR::isError($def)) {
return $def;
}
$def['trigger_comment'] = '';
$def['trigger_enabled'] = true;
return $def;
}
// }}} // }}}
// {{{ tableInfo() // {{{ tableInfo()

View File

@@ -45,8 +45,7 @@
// $Id$ // $Id$
// //
//require_once 'MDB2/Driver/Reverse/Common.php'; require_once 'MDB2/Driver/Reverse/Common.php';
require_once PEAR_PATH."/MDB2/Driver/Reverse/Common.php";
/** /**
* MDB2 MySQLi driver for the schema reverse engineering module * MDB2 MySQLi driver for the schema reverse engineering module
@@ -54,6 +53,7 @@ require_once PEAR_PATH."/MDB2/Driver/Reverse/Common.php";
* @package MDB2 * @package MDB2
* @category Database * @category Database
* @author Lukas Smith <smith@pooteeweet.org> * @author Lukas Smith <smith@pooteeweet.org>
* @author Lorenzo Alberton <l.alberton@quipo.it>
*/ */
class MDB2_Driver_Reverse_mysqli extends MDB2_Driver_Reverse_Common class MDB2_Driver_Reverse_mysqli extends MDB2_Driver_Reverse_Common
{ {
@@ -114,7 +114,7 @@ class MDB2_Driver_Reverse_mysqli extends MDB2_Driver_Reverse_Common
// {{{ getTableFieldDefinition() // {{{ getTableFieldDefinition()
/** /**
* Get the stucture of a field into an array * Get the structure of a field into an array
* *
* @param string $table name of table that should be used in method * @param string $table name of table that should be used in method
* @param string $field_name name of field that should be used in method * @param string $field_name name of field that should be used in method
@@ -152,7 +152,11 @@ class MDB2_Driver_Reverse_mysqli extends MDB2_Driver_Reverse_Common
$column = array_change_key_case($column, $db->options['field_case']); $column = array_change_key_case($column, $db->options['field_case']);
} }
if ($field_name == $column['name']) { if ($field_name == $column['name']) {
list($types, $length, $unsigned, $fixed) = $db->datatype->mapNativeDatatype($column); $mapped_datatype = $db->datatype->mapNativeDatatype($column);
if (PEAR::IsError($mapped_datatype)) {
return $mapped_datatype;
}
list($types, $length, $unsigned, $fixed) = $mapped_datatype;
$notnull = false; $notnull = false;
if (empty($column['null']) || $column['null'] !== 'YES') { if (empty($column['null']) || $column['null'] !== 'YES') {
$notnull = true; $notnull = true;
@@ -173,7 +177,7 @@ class MDB2_Driver_Reverse_mysqli extends MDB2_Driver_Reverse_Common
'notnull' => $notnull, 'notnull' => $notnull,
'nativetype' => preg_replace('/^([a-z]+)[^a-z].*/i', '\\1', $column['type']) 'nativetype' => preg_replace('/^([a-z]+)[^a-z].*/i', '\\1', $column['type'])
); );
if ($length > 0) { if (!is_null($length)) {
$definition[0]['length'] = $length; $definition[0]['length'] = $length;
} }
if (!is_null($unsigned)) { if (!is_null($unsigned)) {
@@ -208,7 +212,7 @@ class MDB2_Driver_Reverse_mysqli extends MDB2_Driver_Reverse_Common
// {{{ getTableIndexDefinition() // {{{ getTableIndexDefinition()
/** /**
* Get the stucture of an index into an array * Get the structure of an index into an array
* *
* @param string $table name of table that should be used in method * @param string $table name of table that should be used in method
* @param string $index_name name of index that should be used in method * @param string $index_name name of index that should be used in method
@@ -222,13 +226,20 @@ class MDB2_Driver_Reverse_mysqli extends MDB2_Driver_Reverse_Common
return $db; return $db;
} }
$index_name = $db->getIndexName($index_name);
$table = $db->quoteIdentifier($table, true); $table = $db->quoteIdentifier($table, true);
$query = "SHOW INDEX FROM $table /*!50002 WHERE Key_name = ".$db->quote($index_name)." */"; $query = "SHOW INDEX FROM $table /*!50002 WHERE Key_name = %s */";
$result = $db->query($query); $index_name_mdb2 = $db->getIndexName($index_name);
$result = $db->queryRow(sprintf($query, $db->quote($index_name_mdb2)));
if (!PEAR::isError($result) && !is_null($result)) {
// apply 'idxname_format' only if the query succeeded, otherwise
// fallback to the given $index_name, without transformation
$index_name = $index_name_mdb2;
}
$result = $db->query(sprintf($query, $db->quote($index_name)));
if (PEAR::isError($result)) { if (PEAR::isError($result)) {
return $result; return $result;
} }
$colpos = 1;
$definition = array(); $definition = array();
while (is_array($row = $result->fetchRow(MDB2_FETCHMODE_ASSOC))) { while (is_array($row = $result->fetchRow(MDB2_FETCHMODE_ASSOC))) {
$row = array_change_key_case($row, CASE_LOWER); $row = array_change_key_case($row, CASE_LOWER);
@@ -253,7 +264,9 @@ class MDB2_Driver_Reverse_mysqli extends MDB2_Driver_Reverse_Common
$column_name = strtoupper($column_name); $column_name = strtoupper($column_name);
} }
} }
$definition['fields'][$column_name] = array(); $definition['fields'][$column_name] = array(
'position' => $colpos++
);
if (!empty($row['collation'])) { if (!empty($row['collation'])) {
$definition['fields'][$column_name]['sorting'] = ($row['collation'] == 'A' $definition['fields'][$column_name]['sorting'] = ($row['collation'] == 'A'
? 'ascending' : 'descending'); ? 'ascending' : 'descending');
@@ -272,29 +285,36 @@ class MDB2_Driver_Reverse_mysqli extends MDB2_Driver_Reverse_Common
// {{{ getTableConstraintDefinition() // {{{ getTableConstraintDefinition()
/** /**
* Get the stucture of a constraint into an array * Get the structure of a constraint into an array
* *
* @param string $table name of table that should be used in method * @param string $table name of table that should be used in method
* @param string $index_name name of index that should be used in method * @param string $constraint_name name of constraint that should be used in method
* @return mixed data array on success, a MDB2 error on failure * @return mixed data array on success, a MDB2 error on failure
* @access public * @access public
*/ */
function getTableConstraintDefinition($table, $index_name) function getTableConstraintDefinition($table, $constraint_name)
{ {
$db =& $this->getDBInstance(); $db =& $this->getDBInstance();
if (PEAR::isError($db)) { if (PEAR::isError($db)) {
return $db; return $db;
} }
if (strtolower($index_name) != 'primary') {
$index_name = $db->getIndexName($index_name);
}
$table = $db->quoteIdentifier($table, true); $table = $db->quoteIdentifier($table, true);
$query = "SHOW INDEX FROM $table /*!50002 WHERE Key_name = ".$db->quote($index_name)." */"; $query = "SHOW INDEX FROM $table /*!50002 WHERE Key_name = %s */";
$result = $db->query($query); if (strtolower($constraint_name) != 'primary') {
$constraint_name_mdb2 = $db->getIndexName($constraint_name);
$result = $db->queryRow(sprintf($query, $db->quote($constraint_name_mdb2)));
if (!PEAR::isError($result) && !is_null($result)) {
// apply 'idxname_format' only if the query succeeded, otherwise
// fallback to the given $index_name, without transformation
$constraint_name = $constraint_name_mdb2;
}
}
$result = $db->query(sprintf($query, $db->quote($constraint_name)));
if (PEAR::isError($result)) { if (PEAR::isError($result)) {
return $result; return $result;
} }
$colpos = 1;
$definition = array(); $definition = array();
while (is_array($row = $result->fetchRow(MDB2_FETCHMODE_ASSOC))) { while (is_array($row = $result->fetchRow(MDB2_FETCHMODE_ASSOC))) {
$row = array_change_key_case($row, CASE_LOWER); $row = array_change_key_case($row, CASE_LOWER);
@@ -306,10 +326,10 @@ class MDB2_Driver_Reverse_mysqli extends MDB2_Driver_Reverse_Common
$key_name = strtoupper($key_name); $key_name = strtoupper($key_name);
} }
} }
if ($index_name == $key_name) { if ($constraint_name == $key_name) {
if ($row['non_unique']) { if ($row['non_unique']) {
return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null, return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
'it was not specified an existing table constraint', __FUNCTION__); $constraint_name . ' is not an existing table constraint', __FUNCTION__);
} }
if ($row['key_name'] == 'PRIMARY') { if ($row['key_name'] == 'PRIMARY') {
$definition['primary'] = true; $definition['primary'] = true;
@@ -324,7 +344,9 @@ class MDB2_Driver_Reverse_mysqli extends MDB2_Driver_Reverse_Common
$column_name = strtoupper($column_name); $column_name = strtoupper($column_name);
} }
} }
$definition['fields'][$column_name] = array(); $definition['fields'][$column_name] = array(
'position' => $colpos++
);
if (!empty($row['collation'])) { if (!empty($row['collation'])) {
$definition['fields'][$column_name]['sorting'] = ($row['collation'] == 'A' $definition['fields'][$column_name]['sorting'] = ($row['collation'] == 'A'
? 'ascending' : 'descending'); ? 'ascending' : 'descending');
@@ -334,11 +356,56 @@ class MDB2_Driver_Reverse_mysqli extends MDB2_Driver_Reverse_Common
$result->free(); $result->free();
if (empty($definition['fields'])) { if (empty($definition['fields'])) {
return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null, return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
'it was not specified an existing table constraint', __FUNCTION__); $constraint_name . ' is not an existing table constraint', __FUNCTION__);
} }
return $definition; return $definition;
} }
// }}}
// {{{ getTriggerDefinition()
/**
* Get the structure of a trigger into an array
*
* EXPERIMENTAL
*
* WARNING: this function is experimental and may change the returned value
* at any time until labelled as non-experimental
*
* @param string $trigger name of trigger that should be used in method
* @return mixed data array on success, a MDB2 error on failure
* @access public
*/
function getTriggerDefinition($trigger)
{
$db =& $this->getDBInstance();
if (PEAR::isError($db)) {
return $db;
}
$query = 'SELECT trigger_name,
event_object_table AS table_name,
action_statement AS trigger_body,
action_timing AS trigger_type,
event_manipulation AS trigger_event
FROM information_schema.triggers
WHERE trigger_name = '. $db->quote($trigger, 'text');
$types = array(
'trigger_name' => 'text',
'table_name' => 'text',
'trigger_body' => 'text',
'trigger_type' => 'text',
'trigger_event' => 'text',
);
$def = $db->queryRow($query, $types, MDB2_FETCHMODE_ASSOC);
if (PEAR::isError($def)) {
return $def;
}
$def['trigger_comment'] = '';
$def['trigger_enabled'] = true;
return $def;
}
// }}} // }}}
// {{{ tableInfo() // {{{ tableInfo()

View File

@@ -2,7 +2,7 @@
// +----------------------------------------------------------------------+ // +----------------------------------------------------------------------+
// | PHP versions 4 and 5 | // | PHP versions 4 and 5 |
// +----------------------------------------------------------------------+ // +----------------------------------------------------------------------+
// | Copyright (c) 1998-2006 Manuel Lemos, Tomas V.V.Cox, | // | Copyright (c) 1998-2007 Manuel Lemos, Tomas V.V.Cox, |
// | Stig. S. Bakken, Lukas Smith | // | Stig. S. Bakken, Lukas Smith |
// | All rights reserved. | // | All rights reserved. |
// +----------------------------------------------------------------------+ // +----------------------------------------------------------------------+
@@ -39,13 +39,13 @@
// | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | // | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
// | POSSIBILITY OF SUCH DAMAGE. | // | POSSIBILITY OF SUCH DAMAGE. |
// +----------------------------------------------------------------------+ // +----------------------------------------------------------------------+
// | Author: Paul Cooper <pgc@ucecom.com> | // | Authors: Paul Cooper <pgc@ucecom.com> |
// | Lorenzo Alberton <l.alberton@quipo.it> |
// +----------------------------------------------------------------------+ // +----------------------------------------------------------------------+
// //
// $Id$ // $Id$
//require_once 'MDB2/Driver/Reverse/Common.php'; require_once 'MDB2/Driver/Reverse/Common.php';
require_once PEAR_PATH."/MDB2/Driver/Reverse/Common.php";
/** /**
* MDB2 PostGreSQL driver for the schema reverse engineering module * MDB2 PostGreSQL driver for the schema reverse engineering module
@@ -59,7 +59,7 @@ class MDB2_Driver_Reverse_pgsql extends MDB2_Driver_Reverse_Common
// {{{ getTableFieldDefinition() // {{{ getTableFieldDefinition()
/** /**
* Get the stucture of a field into an array * Get the structure of a field into an array
* *
* @param string $table name of table that should be used in method * @param string $table name of table that should be used in method
* @param string $field_name name of field that should be used in method * @param string $field_name name of field that should be used in method
@@ -78,13 +78,39 @@ class MDB2_Driver_Reverse_pgsql extends MDB2_Driver_Reverse_Common
return $result; return $result;
} }
$query = "SELECT $query = "SELECT a.attname AS name,
a.attname AS name, t.typname AS type, a.attlen AS length, a.attnotnull, t.typname AS type,
a.atttypmod, a.atthasdef, CASE a.attlen
WHEN -1 THEN
CASE t.typname
WHEN 'numeric' THEN (a.atttypmod / 65536)
WHEN 'decimal' THEN (a.atttypmod / 65536)
WHEN 'money' THEN (a.atttypmod / 65536)
ELSE CASE a.atttypmod
WHEN -1 THEN NULL
ELSE a.atttypmod - 4
END
END
ELSE a.attlen
END AS length,
CASE t.typname
WHEN 'numeric' THEN (a.atttypmod % 65536) - 4
WHEN 'decimal' THEN (a.atttypmod % 65536) - 4
WHEN 'money' THEN (a.atttypmod % 65536) - 4
ELSE 0
END AS scale,
a.attnotnull,
a.atttypmod,
a.atthasdef,
(SELECT substring(pg_get_expr(d.adbin, d.adrelid) for 128) (SELECT substring(pg_get_expr(d.adbin, d.adrelid) for 128)
FROM pg_attrdef d FROM pg_attrdef d
WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef) as default WHERE d.adrelid = a.attrelid
FROM pg_attribute a, pg_class c, pg_type t AND d.adnum = a.attnum
AND a.atthasdef
) as default
FROM pg_attribute a,
pg_class c,
pg_type t
WHERE c.relname = ".$db->quote($table, 'text')." WHERE c.relname = ".$db->quote($table, 'text')."
AND a.atttypid = t.oid AND a.atttypid = t.oid
AND c.oid = a.attrelid AND c.oid = a.attrelid
@@ -103,7 +129,11 @@ class MDB2_Driver_Reverse_pgsql extends MDB2_Driver_Reverse_Common
} }
$column = array_change_key_case($column, CASE_LOWER); $column = array_change_key_case($column, CASE_LOWER);
list($types, $length, $unsigned, $fixed) = $db->datatype->mapNativeDatatype($column); $mapped_datatype = $db->datatype->mapNativeDatatype($column);
if (PEAR::IsError($mapped_datatype)) {
return $mapped_datatype;
}
list($types, $length, $unsigned, $fixed) = $mapped_datatype;
$notnull = false; $notnull = false;
if (!empty($column['attnotnull']) && $column['attnotnull'] == 't') { if (!empty($column['attnotnull']) && $column['attnotnull'] == 't') {
$notnull = true; $notnull = true;
@@ -122,7 +152,7 @@ class MDB2_Driver_Reverse_pgsql extends MDB2_Driver_Reverse_Common
$autoincrement = true; $autoincrement = true;
} }
$definition[0] = array('notnull' => $notnull, 'nativetype' => $column['type']); $definition[0] = array('notnull' => $notnull, 'nativetype' => $column['type']);
if ($length > 0) { if (!is_null($length)) {
$definition[0]['length'] = $length; $definition[0]['length'] = $length;
} }
if (!is_null($unsigned)) { if (!is_null($unsigned)) {
@@ -151,7 +181,7 @@ class MDB2_Driver_Reverse_pgsql extends MDB2_Driver_Reverse_Common
// }}} // }}}
// {{{ getTableIndexDefinition() // {{{ getTableIndexDefinition()
/** /**
* Get the stucture of an index into an array * Get the structure of an index into an array
* *
* @param string $table name of table that should be used in method * @param string $table name of table that should be used in method
* @param string $index_name name of index that should be used in method * @param string $index_name name of index that should be used in method
@@ -165,12 +195,16 @@ class MDB2_Driver_Reverse_pgsql extends MDB2_Driver_Reverse_Common
return $db; return $db;
} }
$index_name = $db->getIndexName($index_name);
$query = 'SELECT relname, indkey FROM pg_index, pg_class'; $query = 'SELECT relname, indkey FROM pg_index, pg_class';
$query.= ' WHERE pg_class.oid = pg_index.indexrelid'; $query.= ' WHERE pg_class.oid = pg_index.indexrelid';
$query.= " AND indisunique != 't' AND indisprimary != 't'"; $query.= " AND indisunique != 't' AND indisprimary != 't'";
$query.= ' AND pg_class.relname = '.$db->quote($index_name, 'text'); $query.= ' AND pg_class.relname = %s';
$row = $db->queryRow($query, null, MDB2_FETCHMODE_ASSOC); $index_name_mdb2 = $db->getIndexName($index_name);
$row = $db->queryRow(sprintf($query, $db->quote($index_name_mdb2, 'text')), null, MDB2_FETCHMODE_ASSOC);
if (PEAR::isError($row) || empty($row)) {
// fallback to the given $index_name, without transformation
$row = $db->queryRow(sprintf($query, $db->quote($index_name, 'text')), null, MDB2_FETCHMODE_ASSOC);
}
if (PEAR::isError($row)) { if (PEAR::isError($row)) {
return $row; return $row;
} }
@@ -189,8 +223,12 @@ class MDB2_Driver_Reverse_pgsql extends MDB2_Driver_Reverse_Common
$index_column_numbers = explode(' ', $row['indkey']); $index_column_numbers = explode(' ', $row['indkey']);
$colpos = 1;
foreach ($index_column_numbers as $number) { foreach ($index_column_numbers as $number) {
$definition['fields'][$columns[($number - 1)]] = array('sorting' => 'ascending'); $definition['fields'][$columns[($number - 1)]] = array(
'position' => $colpos++,
'sorting' => 'ascending',
);
} }
return $definition; return $definition;
} }
@@ -198,33 +236,37 @@ class MDB2_Driver_Reverse_pgsql extends MDB2_Driver_Reverse_Common
// }}} // }}}
// {{{ getTableConstraintDefinition() // {{{ getTableConstraintDefinition()
/** /**
* Get the stucture of a constraint into an array * Get the structure of a constraint into an array
* *
* @param string $table name of table that should be used in method * @param string $table name of table that should be used in method
* @param string $index_name name of index that should be used in method * @param string $constraint_name name of constraint that should be used in method
* @return mixed data array on success, a MDB2 error on failure * @return mixed data array on success, a MDB2 error on failure
* @access public * @access public
*/ */
function getTableConstraintDefinition($table, $index_name) function getTableConstraintDefinition($table, $constraint_name)
{ {
$db =& $this->getDBInstance(); $db =& $this->getDBInstance();
if (PEAR::isError($db)) { if (PEAR::isError($db)) {
return $db; return $db;
} }
$index_name = $db->getIndexName($index_name);
$query = 'SELECT relname, indisunique, indisprimary, indkey FROM pg_index, pg_class'; $query = 'SELECT relname, indisunique, indisprimary, indkey FROM pg_index, pg_class';
$query.= ' WHERE pg_class.oid = pg_index.indexrelid'; $query.= ' WHERE pg_class.oid = pg_index.indexrelid';
$query.= " AND (indisunique = 't' OR indisprimary = 't')"; $query.= " AND (indisunique = 't' OR indisprimary = 't')";
$query.= ' AND pg_class.relname = '.$db->quote($index_name, 'text'); $query.= ' AND pg_class.relname = %s';
$row = $db->queryRow($query, null, MDB2_FETCHMODE_ASSOC); $constraint_name_mdb2 = $db->getIndexName($constraint_name);
$row = $db->queryRow(sprintf($query, $db->quote($constraint_name_mdb2, 'text')), null, MDB2_FETCHMODE_ASSOC);
if (PEAR::isError($row) || empty($row)) {
// fallback to the given $index_name, without transformation
$row = $db->queryRow(sprintf($query, $db->quote($constraint_name, 'text')), null, MDB2_FETCHMODE_ASSOC);
}
if (PEAR::isError($row)) { if (PEAR::isError($row)) {
return $row; return $row;
} }
if (empty($row)) { if (empty($row)) {
return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null, return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
'it was not specified an existing table constraint', __FUNCTION__); $constraint_name . ' is not an existing table constraint', __FUNCTION__);
} }
$row = array_change_key_case($row, CASE_LOWER); $row = array_change_key_case($row, CASE_LOWER);
@@ -241,12 +283,79 @@ class MDB2_Driver_Reverse_pgsql extends MDB2_Driver_Reverse_Common
$index_column_numbers = explode(' ', $row['indkey']); $index_column_numbers = explode(' ', $row['indkey']);
$colpos = 1;
foreach ($index_column_numbers as $number) { foreach ($index_column_numbers as $number) {
$definition['fields'][$columns[($number - 1)]] = array('sorting' => 'ascending'); $definition['fields'][$columns[($number - 1)]] = array(
'position' => $colpos++,
'sorting' => 'ascending',
);
} }
return $definition; return $definition;
} }
// }}}
// {{{ getTriggerDefinition()
/**
* Get the structure of a trigger into an array
*
* EXPERIMENTAL
*
* WARNING: this function is experimental and may change the returned value
* at any time until labelled as non-experimental
*
* @param string $trigger name of trigger that should be used in method
* @return mixed data array on success, a MDB2 error on failure
* @access public
*
* @TODO: add support for plsql functions and functions with args
*/
function getTriggerDefinition($trigger)
{
$db =& $this->getDBInstance();
if (PEAR::isError($db)) {
return $db;
}
$query = "SELECT trg.tgname AS trigger_name,
tbl.relname AS table_name,
CASE
WHEN p.proname IS NOT NULL THEN 'EXECUTE PROCEDURE ' || p.proname || '();'
ELSE ''
END AS trigger_body,
CASE trg.tgtype & cast(2 as int2)
WHEN 0 THEN 'AFTER'
ELSE 'BEFORE'
END AS trigger_type,
CASE trg.tgtype & cast(28 as int2)
WHEN 16 THEN 'UPDATE'
WHEN 8 THEN 'DELETE'
WHEN 4 THEN 'INSERT'
WHEN 20 THEN 'INSERT, UPDATE'
WHEN 28 THEN 'INSERT, UPDATE, DELETE'
WHEN 24 THEN 'UPDATE, DELETE'
WHEN 12 THEN 'INSERT, DELETE'
END AS trigger_event,
trg.tgenabled AS trigger_enabled,
obj_description(trg.oid, 'pg_trigger') AS trigger_comment
FROM pg_trigger trg,
pg_class tbl,
pg_proc p
WHERE trg.tgrelid = tbl.oid
AND trg.tgfoid = p.oid
AND trg.tgname = ". $db->quote($trigger, 'text');
$types = array(
'trigger_name' => 'text',
'table_name' => 'text',
'trigger_body' => 'text',
'trigger_type' => 'text',
'trigger_event' => 'text',
'trigger_comment' => 'text',
'trigger_enabled' => 'boolean',
);
return $db->queryRow($query, $types, MDB2_FETCHMODE_ASSOC);
}
// }}} // }}}
// {{{ tableInfo() // {{{ tableInfo()

View File

@@ -56,10 +56,9 @@
class MDB2_Driver_mssql extends MDB2_Driver_Common class MDB2_Driver_mssql extends MDB2_Driver_Common
{ {
// {{{ properties // {{{ properties
var $escape_quotes = "'"; var $string_quoting = array('start' => "'", 'end' => "'", 'escape' => "'", 'escape_pattern' => false);
var $escape_identifier = '';
var $identifier_quoting = array('start' => '[', 'end' => ']', 'escape' => ']');
// }}} // }}}
// {{{ constructor // {{{ constructor
@@ -77,6 +76,7 @@ class MDB2_Driver_mssql extends MDB2_Driver_Common
$this->supported['indexes'] = true; $this->supported['indexes'] = true;
$this->supported['affected_rows'] = true; $this->supported['affected_rows'] = true;
$this->supported['transactions'] = true; $this->supported['transactions'] = true;
$this->supported['savepoints'] = false;
$this->supported['summary_functions'] = true; $this->supported['summary_functions'] = true;
$this->supported['order_by_text'] = true; $this->supported['order_by_text'] = true;
$this->supported['current_id'] = 'emulated'; $this->supported['current_id'] = 'emulated';
@@ -88,6 +88,7 @@ class MDB2_Driver_mssql extends MDB2_Driver_Common
$this->supported['primary_key'] = true; $this->supported['primary_key'] = true;
$this->supported['result_introspection'] = true; $this->supported['result_introspection'] = true;
$this->supported['prepared_statements'] = 'emulated'; $this->supported['prepared_statements'] = 'emulated';
$this->supported['pattern_escaping'] = true;
$this->options['database_device'] = false; $this->options['database_device'] = false;
$this->options['database_size'] = false; $this->options['database_size'] = false;
@@ -118,19 +119,40 @@ class MDB2_Driver_mssql extends MDB2_Driver_Common
static $ecode_map; static $ecode_map;
if (empty($ecode_map)) { if (empty($ecode_map)) {
$ecode_map = array( $ecode_map = array(
102 => MDB2_ERROR_SYNTAX,
110 => MDB2_ERROR_VALUE_COUNT_ON_ROW, 110 => MDB2_ERROR_VALUE_COUNT_ON_ROW,
155 => MDB2_ERROR_NOSUCHFIELD, 155 => MDB2_ERROR_NOSUCHFIELD,
156 => MDB2_ERROR_SYNTAX,
170 => MDB2_ERROR_SYNTAX, 170 => MDB2_ERROR_SYNTAX,
207 => MDB2_ERROR_NOSUCHFIELD, 207 => MDB2_ERROR_NOSUCHFIELD,
208 => MDB2_ERROR_NOSUCHTABLE, 208 => MDB2_ERROR_NOSUCHTABLE,
245 => MDB2_ERROR_INVALID_NUMBER, 245 => MDB2_ERROR_INVALID_NUMBER,
319 => MDB2_ERROR_SYNTAX,
321 => MDB2_ERROR_NOSUCHFIELD,
325 => MDB2_ERROR_SYNTAX,
336 => MDB2_ERROR_SYNTAX,
515 => MDB2_ERROR_CONSTRAINT_NOT_NULL, 515 => MDB2_ERROR_CONSTRAINT_NOT_NULL,
547 => MDB2_ERROR_CONSTRAINT, 547 => MDB2_ERROR_CONSTRAINT,
1018 => MDB2_ERROR_SYNTAX,
1035 => MDB2_ERROR_SYNTAX,
1913 => MDB2_ERROR_ALREADY_EXISTS, 1913 => MDB2_ERROR_ALREADY_EXISTS,
2209 => MDB2_ERROR_SYNTAX,
2223 => MDB2_ERROR_SYNTAX,
2248 => MDB2_ERROR_SYNTAX,
2256 => MDB2_ERROR_SYNTAX,
2257 => MDB2_ERROR_SYNTAX,
2627 => MDB2_ERROR_CONSTRAINT, 2627 => MDB2_ERROR_CONSTRAINT,
2714 => MDB2_ERROR_ALREADY_EXISTS, 2714 => MDB2_ERROR_ALREADY_EXISTS,
3607 => MDB2_ERROR_DIVZERO,
3701 => MDB2_ERROR_NOSUCHTABLE, 3701 => MDB2_ERROR_NOSUCHTABLE,
7630 => MDB2_ERROR_SYNTAX,
8134 => MDB2_ERROR_DIVZERO, 8134 => MDB2_ERROR_DIVZERO,
9303 => MDB2_ERROR_SYNTAX,
9317 => MDB2_ERROR_SYNTAX,
9318 => MDB2_ERROR_SYNTAX,
9331 => MDB2_ERROR_SYNTAX,
9332 => MDB2_ERROR_SYNTAX,
15253 => MDB2_ERROR_SYNTAX,
); );
} }
if (isset($ecode_map[$native_code])) { if (isset($ecode_map[$native_code])) {
@@ -147,41 +169,48 @@ class MDB2_Driver_mssql extends MDB2_Driver_Common
} }
// }}} // }}}
// {{{ quoteIdentifier() // {{{ function escapePattern($text)
/** /**
* Quote a string so it can be safely used as a table / column name * Quotes pattern (% and _) characters in a string)
* *
* Quoting style depends on which database driver is being used. * @param string the input string to quote
* *
* @param string $str identifier name to be quoted * @return string quoted string
* @param bool $check_option check the 'quote_identifier' option
*
* @return string quoted identifier string
* *
* @access public * @access public
*/ */
function quoteIdentifier($str, $check_option = false) function escapePattern($text)
{ {
if ($check_option && !$this->options['quote_identifier']) { $text = str_replace("[", "[ [ ]", $text);
return $str; foreach ($this->wildcards as $wildcard) {
$text = str_replace($wildcard, '[' . $wildcard . ']', $text);
} }
return '[' . str_replace(']', ']]', $str) . ']'; return $text;
} }
// }}} // }}}
// {{{ beginTransaction() // {{{ beginTransaction()
/** /**
* Start a transaction. * Start a transaction or set a savepoint.
* *
* @param string name of a savepoint to set
* @return mixed MDB2_OK on success, a MDB2 error on failure * @return mixed MDB2_OK on success, a MDB2 error on failure
*
* @access public * @access public
*/ */
function beginTransaction() function beginTransaction($savepoint = null)
{ {
$this->debug('starting transaction', 'beginTransaction', false); $this->debug('Starting transaction/savepoint', __FUNCTION__, array('is_manip' => true, 'savepoint' => $savepoint));
if ($this->in_transaction) { if (!is_null($savepoint)) {
if (!$this->in_transaction) {
return $this->raiseError(MDB2_ERROR_INVALID, null, null,
'savepoint cannot be released when changes are auto committed', __FUNCTION__);
}
$query = 'SAVE TRANSACTION '.$savepoint;
return $this->_doQuery($query, true);
} elseif ($this->in_transaction) {
return MDB2_OK; //nothing to do return MDB2_OK; //nothing to do
} }
if (!$this->destructor_registered && $this->opened_persistent) { if (!$this->destructor_registered && $this->opened_persistent) {
@@ -201,18 +230,26 @@ class MDB2_Driver_mssql extends MDB2_Driver_Common
/** /**
* Commit the database changes done during a transaction that is in * Commit the database changes done during a transaction that is in
* progress. * progress or release a savepoint. This function may only be called when
* auto-committing is disabled, otherwise it will fail. Therefore, a new
* transaction is implicitly started after committing the pending changes.
* *
* @param string name of a savepoint to release
* @return mixed MDB2_OK on success, a MDB2 error on failure * @return mixed MDB2_OK on success, a MDB2 error on failure
*
* @access public * @access public
*/ */
function commit() function commit($savepoint = null)
{ {
$this->debug('commit transaction', 'commit', false); $this->debug('Committing transaction/savepoint', __FUNCTION__, array('is_manip' => true, 'savepoint' => $savepoint));
if (!$this->in_transaction) { if (!$this->in_transaction) {
return $this->raiseError(MDB2_ERROR_INVALID, null, null, return $this->raiseError(MDB2_ERROR_INVALID, null, null,
'commit: transaction changes are being auto committed'); 'commit/release savepoint cannot be done changes are auto committed', __FUNCTION__);
} }
if (!is_null($savepoint)) {
return MDB2_OK;
}
$result =& $this->_doQuery('COMMIT TRANSACTION', true); $result =& $this->_doQuery('COMMIT TRANSACTION', true);
if (PEAR::isError($result)) { if (PEAR::isError($result)) {
return $result; return $result;
@@ -225,19 +262,28 @@ class MDB2_Driver_mssql extends MDB2_Driver_Common
// {{{ rollback() // {{{ rollback()
/** /**
* Cancel any database changes done during a transaction that is in * Cancel any database changes done during a transaction or since a specific
* progress. * savepoint that is in progress. This function may only be called when
* auto-committing is disabled, otherwise it will fail. Therefore, a new
* transaction is implicitly started after canceling the pending changes.
* *
* @param string name of a savepoint to rollback to
* @return mixed MDB2_OK on success, a MDB2 error on failure * @return mixed MDB2_OK on success, a MDB2 error on failure
*
* @access public * @access public
*/ */
function rollback() function rollback($savepoint = null)
{ {
$this->debug('rolling back transaction', 'rollback', false); $this->debug('Rolling back transaction/savepoint', __FUNCTION__, array('is_manip' => true, 'savepoint' => $savepoint));
if (!$this->in_transaction) { if (!$this->in_transaction) {
return $this->raiseError(MDB2_ERROR_INVALID, null, null, return $this->raiseError(MDB2_ERROR_INVALID, null, null,
'rollback: transactions can not be rolled back when changes are auto committed'); 'rollback cannot be done changes are auto committed', __FUNCTION__);
} }
if (!is_null($savepoint)) {
$query = 'ROLLBACK TRANSACTION '.$savepoint;
return $this->_doQuery($query, true);
}
$result =& $this->_doQuery('ROLLBACK TRANSACTION', true); $result =& $this->_doQuery('ROLLBACK TRANSACTION', true);
if (PEAR::isError($result)) { if (PEAR::isError($result)) {
return $result; return $result;
@@ -267,7 +313,7 @@ class MDB2_Driver_mssql extends MDB2_Driver_Common
if (!PEAR::loadExtension($this->phptype)) { if (!PEAR::loadExtension($this->phptype)) {
return $this->raiseError(MDB2_ERROR_NOT_FOUND, null, null, return $this->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
'connect: extension '.$this->phptype.' is not compiled into PHP'); 'extension '.$this->phptype.' is not compiled into PHP', __FUNCTION__);
} }
$params = array( $params = array(
@@ -283,7 +329,8 @@ class MDB2_Driver_mssql extends MDB2_Driver_Common
$connection = @call_user_func_array($connect_function, $params); $connection = @call_user_func_array($connect_function, $params);
if ($connection <= 0) { if ($connection <= 0) {
return $this->raiseError(MDB2_ERROR_CONNECT_FAILED); return $this->raiseError(MDB2_ERROR_CONNECT_FAILED, null, null,
'unable to establish a connection', __FUNCTION__, __FUNCTION__);
} }
if (!empty($this->dsn['charset'])) { if (!empty($this->dsn['charset'])) {
@@ -293,16 +340,30 @@ class MDB2_Driver_mssql extends MDB2_Driver_Common
} }
} }
if ((bool)ini_get('mssql.datetimeconvert')) {
@ini_set('mssql.datetimeconvert', '0');
}
if (empty($this->dsn['disable_iso_date'])) {
@mssql_query('SET DATEFORMAT ymd', $connection);
}
$this->connection = $connection; $this->connection = $connection;
$this->connected_dsn = $this->dsn; $this->connected_dsn = $this->dsn;
$this->connected_database_name = ''; $this->connected_database_name = '';
$this->opened_persistent = $this->options['persistent']; $this->opened_persistent = $this->options['persistent'];
$this->dbsyntax = $this->dsn['dbsyntax'] ? $this->dsn['dbsyntax'] : $this->phptype; $this->dbsyntax = $this->dsn['dbsyntax'] ? $this->dsn['dbsyntax'] : $this->phptype;
if ((bool) ini_get('mssql.datetimeconvert')) { if ($this->database_name) {
@ini_set('mssql.datetimeconvert', '0'); if ($this->database_name != $this->connected_database_name) {
if (!@mssql_select_db($this->database_name, $connection)) {
$err = $this->raiseError(null, null, null,
'Could not select the database: '.$this->database_name, __FUNCTION__);
return $err;
}
$this->connected_database_name = $this->database_name;
}
} }
@mssql_query('SET DATEFORMAT ymd', $connection);
return MDB2_OK; return MDB2_OK;
} }
@@ -323,8 +384,18 @@ class MDB2_Driver_mssql extends MDB2_Driver_Common
{ {
if (is_resource($this->connection)) { if (is_resource($this->connection)) {
if ($this->in_transaction) { if ($this->in_transaction) {
$dsn = $this->dsn;
$database_name = $this->database_name;
$persistent = $this->options['persistent'];
$this->dsn = $this->connected_dsn;
$this->database_name = $this->connected_database_name;
$this->options['persistent'] = $this->opened_persistent;
$this->rollback(); $this->rollback();
$this->dsn = $dsn;
$this->database_name = $database_name;
$this->options['persistent'] = $persistent;
} }
if (!$this->opened_persistent || $force) { if (!$this->opened_persistent || $force) {
@mssql_close($this->connection); @mssql_close($this->connection);
} }
@@ -347,18 +418,16 @@ class MDB2_Driver_mssql extends MDB2_Driver_Common
function &_doQuery($query, $is_manip = false, $connection = null, $database_name = null) function &_doQuery($query, $is_manip = false, $connection = null, $database_name = null)
{ {
$this->last_query = $query; $this->last_query = $query;
$result = $this->debug($query, 'query', $is_manip); $result = $this->debug($query, 'query', array('is_manip' => $is_manip, 'when' => 'pre'));
if ($result) { if ($result) {
if (PEAR::isError($result)) { if (PEAR::isError($result)) {
return $result; return $result;
} }
$query = $result; $query = $result;
} }
if ($this->getOption('disable_query')) { if ($this->options['disable_query']) {
if ($is_manip) { $result = $is_manip ? 0 : null;
return 0; return $result;
}
return null;
} }
if (is_null($connection)) { if (is_null($connection)) {
@@ -375,7 +444,7 @@ class MDB2_Driver_mssql extends MDB2_Driver_Common
if ($database_name != $this->connected_database_name) { if ($database_name != $this->connected_database_name) {
if (!@mssql_select_db($database_name, $connection)) { if (!@mssql_select_db($database_name, $connection)) {
$err = $this->raiseError(null, null, null, $err = $this->raiseError(null, null, null,
'_doQuery: Could not select the database: '.$database_name); 'Could not select the database: '.$database_name, __FUNCTION__);
return $err; return $err;
} }
$this->connected_database_name = $database_name; $this->connected_database_name = $database_name;
@@ -384,11 +453,12 @@ class MDB2_Driver_mssql extends MDB2_Driver_Common
$result = @mssql_query($query, $connection); $result = @mssql_query($query, $connection);
if (!$result) { if (!$result) {
$err = $this->raiseError(null, null, null, $err =& $this->raiseError(null, null, null,
'_doQuery: Could not execute statement'); 'Could not execute statement', __FUNCTION__);
return $err; return $err;
} }
$this->debug($query, 'query', array('is_manip' => $is_manip, 'when' => 'post', 'result' => $result));
return $result; return $result;
} }
@@ -432,8 +502,8 @@ class MDB2_Driver_mssql extends MDB2_Driver_Common
if ($limit > 0) { if ($limit > 0) {
$fetch = $offset + $limit; $fetch = $offset + $limit;
if (!$is_manip) { if (!$is_manip) {
return preg_replace('/^([\s(])*SELECT(?!\s*TOP\s*\()/i', return preg_replace('/^([\s(])*SELECT( DISTINCT)?(?!\s*TOP\s*\()/i',
"\\1SELECT TOP $fetch", $query); "\\1SELECT\\2 TOP $fetch", $query);
} }
} }
return $query; return $query;
@@ -444,7 +514,7 @@ class MDB2_Driver_mssql extends MDB2_Driver_Common
/** /**
* return version information about the server * return version information about the server
* *
* @param string $native determines if the raw version string should be returned * @param bool $native determines if the raw version string should be returned
* @return mixed array/string with version information or MDB2 error object * @return mixed array/string with version information or MDB2 error object
* @access public * @access public
*/ */
@@ -503,7 +573,7 @@ class MDB2_Driver_mssql extends MDB2_Driver_Common
//return $tableExists; //return $tableExists;
return false; return false;
} }
return true; return mssql_result($tableExists, 0, 0);
} }
// }}} // }}}
@@ -525,9 +595,12 @@ class MDB2_Driver_mssql extends MDB2_Driver_Common
$sequence_name = $this->quoteIdentifier($this->getSequenceName($seq_name), true); $sequence_name = $this->quoteIdentifier($this->getSequenceName($seq_name), true);
$seqcol_name = $this->quoteIdentifier($this->options['seqcol_name'], true); $seqcol_name = $this->quoteIdentifier($this->options['seqcol_name'], true);
$this->expectError(MDB2_ERROR_NOSUCHTABLE); $this->expectError(MDB2_ERROR_NOSUCHTABLE);
if ($this->_checkSequence($sequence_name)) {
$query = "SET IDENTITY_INSERT $sequence_name ON ". $seq_val = $this->_checkSequence($sequence_name);
"INSERT INTO $sequence_name ($seqcol_name) VALUES (0)";
if ($seq_val) {
$query = "SET IDENTITY_INSERT $sequence_name OFF ".
"INSERT INTO $sequence_name ($seqcol_name) DEFAULT VALUES";
} else { } else {
$query = "INSERT INTO $sequence_name ($seqcol_name) VALUES (0)"; $query = "INSERT INTO $sequence_name ($seqcol_name) VALUES (0)";
} }
@@ -536,16 +609,12 @@ class MDB2_Driver_mssql extends MDB2_Driver_Common
if (PEAR::isError($result)) { if (PEAR::isError($result)) {
if ($ondemand && !$this->_checkSequence($sequence_name)) { if ($ondemand && !$this->_checkSequence($sequence_name)) {
$this->loadModule('Manager', null, true); $this->loadModule('Manager', null, true);
// Since we are creating the sequence on demand $result = $this->manager->createSequence($seq_name);
// we know the first id = 1 so initialize the
// sequence at 2
$result = $this->manager->createSequence($seq_name, 2);
if (PEAR::isError($result)) { if (PEAR::isError($result)) {
return $this->raiseError($result, null, null, return $this->raiseError($result, null, null,
'nextID: on demand sequence '.$seq_name.' could not be created'); 'on demand sequence '.$seq_name.' could not be created', __FUNCTION__);
} else { } else {
// First ID of a newly created sequence is 1 return $this->nextID($seq_name, false);
return 1;
} }
} }
return $result; return $result;
@@ -574,9 +643,9 @@ class MDB2_Driver_mssql extends MDB2_Driver_Common
function lastInsertID($table = null, $field = null) function lastInsertID($table = null, $field = null)
{ {
$server_info = $this->getServerVersion(); $server_info = $this->getServerVersion();
if (is_array($server_info) if (is_array($server_info) && !is_null($server_info['major'])
&& !is_null($server_info['major']) && $server_info['major'] >= 8
&& $server_info['major'] >= 8) { ) {
$query = "SELECT SCOPE_IDENTITY()"; $query = "SELECT SCOPE_IDENTITY()";
} else { } else {
$query = "SELECT @@IDENTITY"; $query = "SELECT @@IDENTITY";
@@ -665,21 +734,22 @@ class MDB2_Result_mssql extends MDB2_Result_Common
if (!$row) { if (!$row) {
if ($this->result === false) { if ($this->result === false) {
$err =& $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null, $err =& $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
'fetchRow: resultset has already been freed'); 'resultset has already been freed', __FUNCTION__);
return $err; return $err;
} }
$null = null; $null = null;
return $null; return $null;
} }
if ($this->db->options['portability'] & MDB2_PORTABILITY_EMPTY_TO_NULL) { $mode = $this->db->options['portability'] & MDB2_PORTABILITY_EMPTY_TO_NULL;
$this->db->_fixResultArrayValues($row, MDB2_PORTABILITY_EMPTY_TO_NULL); if ($mode) {
$this->db->_fixResultArrayValues($row, $mode);
}
if (!empty($this->types)) {
$row = $this->db->datatype->convertResultRow($this->types, $row, false);
} }
if (!empty($this->values)) { if (!empty($this->values)) {
$this->_assignBindColumns($row); $this->_assignBindColumns($row);
} }
if (!empty($this->types)) {
$row = $this->db->datatype->convertResultRow($this->types, $row);
}
if ($fetchmode === MDB2_FETCHMODE_OBJECT) { if ($fetchmode === MDB2_FETCHMODE_OBJECT) {
$object_class = $this->db->options['fetch_class']; $object_class = $this->db->options['fetch_class'];
if ($object_class == 'stdClass') { if ($object_class == 'stdClass') {
@@ -737,12 +807,12 @@ class MDB2_Result_mssql extends MDB2_Result_Common
if (is_null($cols)) { if (is_null($cols)) {
if ($this->result === false) { if ($this->result === false) {
return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null, return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
'numCols: resultset has already been freed'); 'resultset has already been freed', __FUNCTION__);
} elseif (is_null($this->result)) { } elseif (is_null($this->result)) {
return count($this->types); return count($this->types);
} }
return $this->db->raiseError(null, null, null, return $this->db->raiseError(null, null, null,
'numCols: Could not get column count'); 'Could not get column count', __FUNCTION__);
} }
return $cols; return $cols;
} }
@@ -760,7 +830,7 @@ class MDB2_Result_mssql extends MDB2_Result_Common
{ {
if ($this->result === false) { if ($this->result === false) {
return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null, return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
'nextResult: resultset has already been freed'); 'resultset has already been freed', __FUNCTION__);
} elseif (is_null($this->result)) { } elseif (is_null($this->result)) {
return false; return false;
} }
@@ -782,7 +852,7 @@ class MDB2_Result_mssql extends MDB2_Result_Common
$free = @mssql_free_result($this->result); $free = @mssql_free_result($this->result);
if ($free === false) { if ($free === false) {
return $this->db->raiseError(null, null, null, return $this->db->raiseError(null, null, null,
'free: Could not free result'); 'Could not free result', __FUNCTION__);
} }
} }
$this->result = false; $this->result = false;
@@ -814,12 +884,12 @@ class MDB2_BufferedResult_mssql extends MDB2_Result_mssql
if ($this->rownum != ($rownum - 1) && !@mssql_data_seek($this->result, $rownum)) { if ($this->rownum != ($rownum - 1) && !@mssql_data_seek($this->result, $rownum)) {
if ($this->result === false) { if ($this->result === false) {
return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null, return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
'seek: resultset has already been freed'); 'resultset has already been freed', __FUNCTION__);
} elseif (is_null($this->result)) { } elseif (is_null($this->result)) {
return MDB2_OK; return MDB2_OK;
} }
return $this->db->raiseError(MDB2_ERROR_INVALID, null, null, return $this->db->raiseError(MDB2_ERROR_INVALID, null, null,
'seek: tried to seek to an invalid row number ('.$rownum.')'); 'tried to seek to an invalid row number ('.$rownum.')', __FUNCTION__);
} }
$this->rownum = $rownum - 1; $this->rownum = $rownum - 1;
return MDB2_OK; return MDB2_OK;
@@ -857,12 +927,12 @@ class MDB2_BufferedResult_mssql extends MDB2_Result_mssql
if (is_null($rows)) { if (is_null($rows)) {
if ($this->result === false) { if ($this->result === false) {
return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null, return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
'numRows: resultset has already been freed'); 'resultset has already been freed', __FUNCTION__);
} elseif (is_null($this->result)) { } elseif (is_null($this->result)) {
return 0; return 0;
} }
return $this->db->raiseError(null, null, null, return $this->db->raiseError(null, null, null,
'numRows: Could not get row count'); 'Could not get row count', __FUNCTION__);
} }
if ($this->limit) { if ($this->limit) {
$rows -= $this->limit; $rows -= $this->limit;

View File

@@ -151,6 +151,9 @@ class MDB2_Driver_mysql extends MDB2_Driver_Common
1146 => MDB2_ERROR_NOSUCHTABLE, 1146 => MDB2_ERROR_NOSUCHTABLE,
1216 => MDB2_ERROR_CONSTRAINT, 1216 => MDB2_ERROR_CONSTRAINT,
1217 => MDB2_ERROR_CONSTRAINT, 1217 => MDB2_ERROR_CONSTRAINT,
1356 => MDB2_ERROR_DIVZERO,
1451 => MDB2_ERROR_CONSTRAINT,
1452 => MDB2_ERROR_CONSTRAINT,
); );
} }
if ($this->options['portability'] & MDB2_PORTABILITY_ERRORS) { if ($this->options['portability'] & MDB2_PORTABILITY_ERRORS) {
@@ -190,7 +193,7 @@ class MDB2_Driver_mysql extends MDB2_Driver_Common
$text = $this->escapePattern($text); $text = $this->escapePattern($text);
} }
$connection = $this->getConnection(); $connection = $this->getConnection();
if (DOLIPEAR::isError($connection)) { if (PEAR::isError($connection)) {
return $connection; return $connection;
} }
$text = @mysql_real_escape_string($text, $connection); $text = @mysql_real_escape_string($text, $connection);
@@ -211,6 +214,7 @@ class MDB2_Driver_mysql extends MDB2_Driver_Common
function beginTransaction($savepoint = null) function beginTransaction($savepoint = null)
{ {
$this->debug('Starting transaction/savepoint', __FUNCTION__, array('is_manip' => true, 'savepoint' => $savepoint)); $this->debug('Starting transaction/savepoint', __FUNCTION__, array('is_manip' => true, 'savepoint' => $savepoint));
$this->_getServerCapabilities();
if (!is_null($savepoint)) { if (!is_null($savepoint)) {
if (!$this->supports('savepoints')) { if (!$this->supports('savepoints')) {
return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
@@ -231,7 +235,7 @@ class MDB2_Driver_mysql extends MDB2_Driver_Common
} }
$query = $this->start_transaction ? 'START TRANSACTION' : 'SET AUTOCOMMIT = 1'; $query = $this->start_transaction ? 'START TRANSACTION' : 'SET AUTOCOMMIT = 1';
$result =& $this->_doQuery($query, true); $result =& $this->_doQuery($query, true);
if (DOLIPEAR::isError($result)) { if (PEAR::isError($result)) {
return $result; return $result;
} }
$this->in_transaction = true; $this->in_transaction = true;
@@ -278,13 +282,13 @@ class MDB2_Driver_mysql extends MDB2_Driver_Common
} }
$result =& $this->_doQuery('COMMIT', true); $result =& $this->_doQuery('COMMIT', true);
if (DOLIPEAR::isError($result)) { if (PEAR::isError($result)) {
return $result; return $result;
} }
if (!$this->start_transaction) { if (!$this->start_transaction) {
$query = 'SET AUTOCOMMIT = 0'; $query = 'SET AUTOCOMMIT = 0';
$result =& $this->_doQuery($query, true); $result =& $this->_doQuery($query, true);
if (DOLIPEAR::isError($result)) { if (PEAR::isError($result)) {
return $result; return $result;
} }
} }
@@ -324,13 +328,13 @@ class MDB2_Driver_mysql extends MDB2_Driver_Common
$query = 'ROLLBACK'; $query = 'ROLLBACK';
$result =& $this->_doQuery($query, true); $result =& $this->_doQuery($query, true);
if (DOLIPEAR::isError($result)) { if (PEAR::isError($result)) {
return $result; return $result;
} }
if (!$this->start_transaction) { if (!$this->start_transaction) {
$query = 'SET AUTOCOMMIT = 0'; $query = 'SET AUTOCOMMIT = 0';
$result =& $this->_doQuery($query, true); $result =& $this->_doQuery($query, true);
if (DOLIPEAR::isError($result)) { if (PEAR::isError($result)) {
return $result; return $result;
} }
} }
@@ -389,13 +393,14 @@ class MDB2_Driver_mysql extends MDB2_Driver_Common
if (is_resource($this->connection)) { if (is_resource($this->connection)) {
if (count(array_diff($this->connected_dsn, $this->dsn)) == 0 if (count(array_diff($this->connected_dsn, $this->dsn)) == 0
&& $this->opened_persistent == $this->options['persistent'] && $this->opened_persistent == $this->options['persistent']
&& $this->connected_database_name == $this->database_name
) { ) {
return MDB2_OK; return MDB2_OK;
} }
$this->disconnect(false); $this->disconnect(false);
} }
if (!DOLIPEAR::loadExtension($this->phptype)) { if (!PEAR::loadExtension($this->phptype)) {
return $this->raiseError(MDB2_ERROR_NOT_FOUND, null, null, return $this->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
'extension '.$this->phptype.' is not compiled into PHP', __FUNCTION__); 'extension '.$this->phptype.' is not compiled into PHP', __FUNCTION__);
} }
@@ -440,7 +445,7 @@ class MDB2_Driver_mysql extends MDB2_Driver_Common
if (!empty($this->dsn['charset'])) { if (!empty($this->dsn['charset'])) {
$result = $this->setCharset($this->dsn['charset'], $connection); $result = $this->setCharset($this->dsn['charset'], $connection);
if (DOLIPEAR::isError($result)) { if (PEAR::isError($result)) {
return $result; return $result;
} }
} }
@@ -483,35 +488,11 @@ class MDB2_Driver_mysql extends MDB2_Driver_Common
} }
} }
$this->supported['sub_selects'] = 'emulated'; $this->_getServerCapabilities();
$this->supported['prepared_statements'] = 'emulated';
$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', '<')) {
$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;
}
if (!version_compare($server_info['major'].'.'.$server_info['minor'].'.'.$server_info['patch'], '4.0.11', '<')) {
$this->start_transaction = true;
}
if (!version_compare($server_info['major'].'.'.$server_info['minor'].'.'.$server_info['patch'], '5.0.3', '<')) {
$this->varchar_max_length = 65532;
}
}
return MDB2_OK; return MDB2_OK;
} }
// }}} // }}}
// {{{ setCharset() // {{{ setCharset()
@@ -527,19 +508,12 @@ class MDB2_Driver_mysql extends MDB2_Driver_Common
{ {
if (is_null($connection)) { if (is_null($connection)) {
$connection = $this->getConnection(); $connection = $this->getConnection();
if (DOLIPEAR::isError($connection)) { if (PEAR::isError($connection)) {
return $connection; return $connection;
} }
} }
$query = "SET NAMES '".mysql_real_escape_string($charset, $connection)."'";
$query = "SET character_set_client = '".mysql_real_escape_string($charset, $connection)."'"; return $this->_doQuery($query, true, $connection);
$result = @mysql_query($query, $connection);
if (!$result) {
return $this->raiseError(null, null, null,
'Unable to set client charset: '.$charset, __FUNCTION__);
}
return MDB2_OK;
} }
// }}} // }}}
@@ -594,7 +568,7 @@ class MDB2_Driver_mysql extends MDB2_Driver_Common
$this->last_query = $query; $this->last_query = $query;
$result = $this->debug($query, 'query', array('is_manip' => $is_manip, 'when' => 'pre')); $result = $this->debug($query, 'query', array('is_manip' => $is_manip, 'when' => 'pre'));
if ($result) { if ($result) {
if (DOLIPEAR::isError($result)) { if (PEAR::isError($result)) {
return $result; return $result;
} }
$query = $result; $query = $result;
@@ -606,7 +580,7 @@ class MDB2_Driver_mysql extends MDB2_Driver_Common
if (is_null($connection)) { if (is_null($connection)) {
$connection = $this->getConnection(); $connection = $this->getConnection();
if (DOLIPEAR::isError($connection)) { if (PEAR::isError($connection)) {
return $connection; return $connection;
} }
} }
@@ -653,7 +627,7 @@ class MDB2_Driver_mysql extends MDB2_Driver_Common
{ {
if (is_null($connection)) { if (is_null($connection)) {
$connection = $this->getConnection(); $connection = $this->getConnection();
if (DOLIPEAR::isError($connection)) { if (PEAR::isError($connection)) {
return $connection; return $connection;
} }
} }
@@ -684,16 +658,31 @@ class MDB2_Driver_mysql extends MDB2_Driver_Common
} }
} }
if ($limit > 0 if ($limit > 0
&& !preg_match('/LIMIT\s*\d(\s*(,|OFFSET)\s*\d+)?/i', $query) && !preg_match('/LIMIT\s*\d(?:\s*(?:,|OFFSET)\s*\d+)?(?:[^\)]*)?$/i', $query)
) { ) {
$query = rtrim($query); $query = rtrim($query);
if (substr($query, -1) == ';') { if (substr($query, -1) == ';') {
$query = substr($query, 0, -1); $query = substr($query, 0, -1);
} }
// LIMIT doesn't always come last in the query
// @see http://dev.mysql.com/doc/refman/5.0/en/select.html
$after = '';
if (preg_match('/(\s+INTO\s+(?:OUT|DUMP)FILE\s.*)$/ims', $query, $matches)) {
$after = $matches[0];
$query = preg_replace('/(\s+INTO\s+(?:OUT|DUMP)FILE\s.*)$/ims', '', $query);
} elseif (preg_match('/(\s+FOR\s+UPDATE\s*)$/i', $query, $matches)) {
$after = $matches[0];
$query = preg_replace('/(\s+FOR\s+UPDATE\s*)$/im', '', $query);
} elseif (preg_match('/(\s+LOCK\s+IN\s+SHARE\s+MODE\s*)$/im', $query, $matches)) {
$after = $matches[0];
$query = preg_replace('/(\s+LOCK\s+IN\s+SHARE\s+MODE\s*)$/im', '', $query);
}
if ($is_manip) { if ($is_manip) {
return $query . " LIMIT $limit"; return $query . " LIMIT $limit" . $after;
} else { } else {
return $query . " LIMIT $offset, $limit"; return $query . " LIMIT $offset, $limit" . $after;
} }
} }
return $query; return $query;
@@ -705,14 +694,14 @@ class MDB2_Driver_mysql extends MDB2_Driver_Common
/** /**
* return version information about the server * return version information about the server
* *
* @param string $native determines if the raw version string should be returned * @param bool $native determines if the raw version string should be returned
* @return mixed array/string with version information or MDB2 error object * @return mixed array/string with version information or MDB2 error object
* @access public * @access public
*/ */
function getServerVersion($native = false) function getServerVersion($native = false)
{ {
$connection = $this->getConnection(); $connection = $this->getConnection();
if (DOLIPEAR::isError($connection)) { if (PEAR::isError($connection)) {
return $connection; return $connection;
} }
if ($this->connected_server_info) { if ($this->connected_server_info) {
@@ -745,6 +734,80 @@ class MDB2_Driver_mysql extends MDB2_Driver_Common
return $server_info; return $server_info;
} }
// }}}
// {{{ _getServerCapabilities()
/**
* Fetch some information about the server capabilities
* (transactions, subselects, prepared statements, etc).
*
* @access private
*/
function _getServerCapabilities()
{
static $already_checked = false;
if (!$already_checked) {
$already_checked = true;
//set defaults
$this->supported['sub_selects'] = 'emulated';
$this->supported['prepared_statements'] = 'emulated';
$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', '<')) {
$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;
}
if (!version_compare($server_info['major'].'.'.$server_info['minor'].'.'.$server_info['patch'], '4.0.11', '<')) {
$this->start_transaction = true;
}
if (!version_compare($server_info['major'].'.'.$server_info['minor'].'.'.$server_info['patch'], '5.0.3', '<')) {
$this->varchar_max_length = 65532;
}
}
}
}
// }}}
// {{{ function _skipUserDefinedVariable($query, $position)
/**
* Utility method, used by prepare() to avoid misinterpreting MySQL user
* defined variables (SELECT @x:=5) for placeholders.
* Check if the placeholder is a false positive, i.e. if it is an user defined
* variable instead. If so, skip it and advance the position, otherwise
* return the current position, which is valid
*
* @param string $query
* @param integer $position current string cursor position
* @return integer $new_position
* @access protected
*/
function _skipUserDefinedVariable($query, $position)
{
$found = strpos(strrev(substr($query, 0, $position)), '@');
if ($found === false) {
return $position;
}
$pos = strlen($query) - strlen(substr($query, $position)) - $found - 1;
$substring = substr($query, $pos, $position - $pos + 2);
if (preg_match('/^@\w+:=$/', $substring)) {
return $position + 1; //found an user defined variable: skip it
}
return $position;
}
// }}} // }}}
// {{{ prepare() // {{{ prepare()
@@ -783,7 +846,7 @@ class MDB2_Driver_mysql extends MDB2_Driver_Common
$query = $this->_modifyQuery($query, $is_manip, $limit, $offset); $query = $this->_modifyQuery($query, $is_manip, $limit, $offset);
$result = $this->debug($query, __FUNCTION__, array('is_manip' => $is_manip, 'when' => 'pre')); $result = $this->debug($query, __FUNCTION__, array('is_manip' => $is_manip, 'when' => 'pre'));
if ($result) { if ($result) {
if (DOLIPEAR::isError($result)) { if (PEAR::isError($result)) {
return $result; return $result;
} }
$query = $result; $query = $result;
@@ -810,7 +873,7 @@ class MDB2_Driver_mysql extends MDB2_Driver_Common
} }
$new_pos = $this->_skipDelimitedStrings($query, $position, $p_position); $new_pos = $this->_skipDelimitedStrings($query, $position, $p_position);
if (DOLIPEAR::isError($new_pos)) { if (PEAR::isError($new_pos)) {
return $new_pos; return $new_pos;
} }
if ($new_pos != $position) { if ($new_pos != $position) {
@@ -824,6 +887,12 @@ class MDB2_Driver_mysql extends MDB2_Driver_Common
$question = $colon = $placeholder_type; $question = $colon = $placeholder_type;
} }
if ($placeholder_type == ':') { if ($placeholder_type == ':') {
//make sure this is not part of an user defined variable
$new_pos = $this->_skipUserDefinedVariable($query, $position);
if ($new_pos != $position) {
$position = $new_pos;
continue; //evaluate again starting from the new position
}
$parameter = preg_replace('/^.{'.($position+1).'}([a-z0-9_]+).*$/si', '\\1', $query); $parameter = preg_replace('/^.{'.($position+1).'}([a-z0-9_]+).*$/si', '\\1', $query);
if ($parameter === '') { if ($parameter === '') {
$err =& $this->raiseError(MDB2_ERROR_SYNTAX, null, null, $err =& $this->raiseError(MDB2_ERROR_SYNTAX, null, null,
@@ -841,13 +910,13 @@ class MDB2_Driver_mysql extends MDB2_Driver_Common
} }
} }
$connection = $this->getConnection(); $connection = $this->getConnection();
if (DOLIPEAR::isError($connection)) { if (PEAR::isError($connection)) {
return $connection; return $connection;
} }
$statement_name = sprintf($this->options['statement_format'], $this->phptype, md5(time() + rand())); $statement_name = sprintf($this->options['statement_format'], $this->phptype, md5(time() + rand()));
$query = "PREPARE $statement_name FROM ".$this->quote($query, 'text'); $query = "PREPARE $statement_name FROM ".$this->quote($query, 'text');
$statement =& $this->_doQuery($query, true, $connection); $statement =& $this->_doQuery($query, true, $connection);
if (DOLIPEAR::isError($statement)) { if (PEAR::isError($statement)) {
return $statement; return $statement;
} }
@@ -957,13 +1026,13 @@ class MDB2_Driver_mysql extends MDB2_Driver_Common
} }
$connection = $this->getConnection(); $connection = $this->getConnection();
if (DOLIPEAR::isError($connection)) { if (PEAR::isError($connection)) {
return $connection; return $connection;
} }
$query = "REPLACE INTO $table ($query) VALUES ($values)"; $query = "REPLACE INTO $table ($query) VALUES ($values)";
$result =& $this->_doQuery($query, true, $connection); $result =& $this->_doQuery($query, true, $connection);
if (DOLIPEAR::isError($result)) { if (PEAR::isError($result)) {
return $result; return $result;
} }
return $this->_affectedRows($connection, $result); return $this->_affectedRows($connection, $result);
@@ -991,11 +1060,11 @@ class MDB2_Driver_mysql extends MDB2_Driver_Common
$this->expectError(MDB2_ERROR_NOSUCHTABLE); $this->expectError(MDB2_ERROR_NOSUCHTABLE);
$result =& $this->_doQuery($query, true); $result =& $this->_doQuery($query, true);
$this->popExpect(); $this->popExpect();
if (DOLIPEAR::isError($result)) { if (PEAR::isError($result)) {
if ($ondemand && $result->getCode() == MDB2_ERROR_NOSUCHTABLE) { if ($ondemand && $result->getCode() == MDB2_ERROR_NOSUCHTABLE) {
$this->loadModule('Manager', null, true); $this->loadModule('Manager', null, true);
$result = $this->manager->createSequence($seq_name); $result = $this->manager->createSequence($seq_name);
if (DOLIPEAR::isError($result)) { if (PEAR::isError($result)) {
return $this->raiseError($result, null, null, return $this->raiseError($result, null, null,
'on demand sequence '.$seq_name.' could not be created', __FUNCTION__); 'on demand sequence '.$seq_name.' could not be created', __FUNCTION__);
} else { } else {
@@ -1008,7 +1077,7 @@ class MDB2_Driver_mysql extends MDB2_Driver_Common
if (is_numeric($value)) { if (is_numeric($value)) {
$query = "DELETE FROM $sequence_name WHERE $seqcol_name < $value"; $query = "DELETE FROM $sequence_name WHERE $seqcol_name < $value";
$result =& $this->_doQuery($query, true); $result =& $this->_doQuery($query, true);
if (DOLIPEAR::isError($result)) { if (PEAR::isError($result)) {
$this->warnings[] = 'nextID: could not delete previous sequence table values from '.$seq_name; $this->warnings[] = 'nextID: could not delete previous sequence table values from '.$seq_name;
} }
} }
@@ -1076,7 +1145,7 @@ class MDB2_Result_mysql extends MDB2_Result_Common
{ {
if (!is_null($rownum)) { if (!is_null($rownum)) {
$seek = $this->seek($rownum); $seek = $this->seek($rownum);
if (DOLIPEAR::isError($seek)) { if (PEAR::isError($seek)) {
return $seek; return $seek;
} }
} }
@@ -1141,7 +1210,7 @@ class MDB2_Result_mysql extends MDB2_Result_Common
{ {
$columns = array(); $columns = array();
$numcols = $this->numCols(); $numcols = $this->numCols();
if (DOLIPEAR::isError($numcols)) { if (PEAR::isError($numcols)) {
return $numcols; return $numcols;
} }
for ($column = 0; $column < $numcols; $column++) { for ($column = 0; $column < $numcols; $column++) {
@@ -1250,7 +1319,7 @@ class MDB2_BufferedResult_mysql extends MDB2_Result_mysql
function valid() function valid()
{ {
$numrows = $this->numRows(); $numrows = $this->numRows();
if (DOLIPEAR::isError($numrows)) { if (PEAR::isError($numrows)) {
return $numrows; return $numrows;
} }
return $this->rownum < ($numrows - 1); return $this->rownum < ($numrows - 1);
@@ -1315,7 +1384,7 @@ class MDB2_Statement_mysql extends MDB2_Statement_Common
} }
$connection = $this->db->getConnection(); $connection = $this->db->getConnection();
if (DOLIPEAR::isError($connection)) { if (PEAR::isError($connection)) {
return $connection; return $connection;
} }
@@ -1348,9 +1417,13 @@ class MDB2_Statement_mysql extends MDB2_Statement_Common
$value = $data; $value = $data;
} }
} }
$param_query = 'SET @'.$parameter.' = '.$this->db->quote($value, $type); $quoted = $this->db->quote($value, $type);
if (PEAR::isError($quoted)) {
return $quoted;
}
$param_query = 'SET @'.$parameter.' = '.$quoted;
$result = $this->db->_doQuery($param_query, true, $connection); $result = $this->db->_doQuery($param_query, true, $connection);
if (DOLIPEAR::isError($result)) { if (PEAR::isError($result)) {
return $result; return $result;
} }
} }
@@ -1358,7 +1431,7 @@ class MDB2_Statement_mysql extends MDB2_Statement_Common
} }
$result = $this->db->_doQuery($query, $this->is_manip, $connection); $result = $this->db->_doQuery($query, $this->is_manip, $connection);
if (DOLIPEAR::isError($result)) { if (PEAR::isError($result)) {
return $result; return $result;
} }
@@ -1392,7 +1465,7 @@ class MDB2_Statement_mysql extends MDB2_Statement_Common
if (!is_null($this->statement)) { if (!is_null($this->statement)) {
$connection = $this->db->getConnection(); $connection = $this->db->getConnection();
if (DOLIPEAR::isError($connection)) { if (PEAR::isError($connection)) {
return $connection; return $connection;
} }
$query = 'DEALLOCATE PREPARE '.$this->statement; $query = 'DEALLOCATE PREPARE '.$this->statement;

View File

@@ -122,8 +122,8 @@ class MDB2_Driver_mysqli extends MDB2_Driver_Common
$native_code = @mysqli_errno($this->connection); $native_code = @mysqli_errno($this->connection);
$native_msg = @mysqli_error($this->connection); $native_msg = @mysqli_error($this->connection);
} else { } else {
$native_code = @mysqli_errno(); $native_code = @mysqli_connect_errno();
$native_msg = @mysqli_error(); $native_msg = @mysqli_connect_error();
} }
if (is_null($error)) { if (is_null($error)) {
static $ecode_map; static $ecode_map;
@@ -152,6 +152,9 @@ class MDB2_Driver_mysqli extends MDB2_Driver_Common
1146 => MDB2_ERROR_NOSUCHTABLE, 1146 => MDB2_ERROR_NOSUCHTABLE,
1216 => MDB2_ERROR_CONSTRAINT, 1216 => MDB2_ERROR_CONSTRAINT,
1217 => MDB2_ERROR_CONSTRAINT, 1217 => MDB2_ERROR_CONSTRAINT,
1356 => MDB2_ERROR_DIVZERO,
1451 => MDB2_ERROR_CONSTRAINT,
1452 => MDB2_ERROR_CONSTRAINT,
); );
} }
if ($this->options['portability'] & MDB2_PORTABILITY_ERRORS) { if ($this->options['portability'] & MDB2_PORTABILITY_ERRORS) {
@@ -212,6 +215,7 @@ class MDB2_Driver_mysqli extends MDB2_Driver_Common
function beginTransaction($savepoint = null) function beginTransaction($savepoint = null)
{ {
$this->debug('Starting transaction/savepoint', __FUNCTION__, array('is_manip' => true, 'savepoint' => $savepoint)); $this->debug('Starting transaction/savepoint', __FUNCTION__, array('is_manip' => true, 'savepoint' => $savepoint));
$this->_getServerCapabilities();
if (!is_null($savepoint)) { if (!is_null($savepoint)) {
if (!$this->supports('savepoints')) { if (!$this->supports('savepoints')) {
return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
@@ -429,7 +433,7 @@ class MDB2_Driver_mysqli extends MDB2_Driver_Common
if (!$connection) { if (!$connection) {
if (($err = @mysqli_connect_error()) != '') { if (($err = @mysqli_connect_error()) != '') {
return $this->raiseError(MDB2_ERROR_CONNECT_FAILED, return $this->raiseError(null,
null, null, $err, __FUNCTION__); null, null, $err, __FUNCTION__);
} else { } else {
return $this->raiseError(MDB2_ERROR_CONNECT_FAILED, null, null, return $this->raiseError(MDB2_ERROR_CONNECT_FAILED, null, null,
@@ -470,32 +474,7 @@ class MDB2_Driver_mysqli extends MDB2_Driver_Common
} }
} }
$this->supported['sub_selects'] = 'emulated'; $this->_getServerCapabilities();
$this->supported['prepared_statements'] = 'emulated';
$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', '<')) {
$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;
}
if (!version_compare($server_info['major'].'.'.$server_info['minor'].'.'.$server_info['patch'], '4.0.11', '<')) {
$this->start_transaction = true;
}
if (!version_compare($server_info['major'].'.'.$server_info['minor'].'.'.$server_info['patch'], '5.0.3', '<')) {
$this->varchar_max_length = 65532;
}
}
return MDB2_OK; return MDB2_OK;
} }
@@ -519,19 +498,8 @@ class MDB2_Driver_mysqli extends MDB2_Driver_Common
return $connection; return $connection;
} }
} }
$query = "SET NAMES '".mysqli_real_escape_string($connection, $charset)."'";
if (function_exists('mysqli_set_charset')) { return $this->_doQuery($query, true, $connection);
$result = @mysqli_set_charset($connection, $charset);
} else {
$query = "SET character_set_client = '".mysqli_real_escape_string($connection, $charset)."'";
$result = @mysqli_query($connection, $query);
}
if (!$result) {
return $this->raiseError(null, null, null,
'Unable to set client charset: '.$charset, __FUNCTION__);
}
return MDB2_OK;
} }
// }}} // }}}
@@ -694,12 +662,27 @@ class MDB2_Driver_mysqli extends MDB2_Driver_Common
} }
} }
if ($limit > 0 if ($limit > 0
&& !preg_match('/LIMIT\s*\d(\s*(,|OFFSET)\s*\d+)?/i', $query) && !preg_match('/LIMIT\s*\d(?:\s*(?:,|OFFSET)\s*\d+)?(?:[^\)]*)?$/i', $query)
) { ) {
$query = rtrim($query); $query = rtrim($query);
if (substr($query, -1) == ';') { if (substr($query, -1) == ';') {
$query = substr($query, 0, -1); $query = substr($query, 0, -1);
} }
// LIMIT doesn't always come last in the query
// @see http://dev.mysql.com/doc/refman/5.0/en/select.html
$after = '';
if (preg_match('/(\s+INTO\s+(?:OUT|DUMP)FILE\s.*)$/ims', $query, $matches)) {
$after = $matches[0];
$query = preg_replace('/(\s+INTO\s+(?:OUT|DUMP)FILE\s.*)$/ims', '', $query);
} elseif (preg_match('/(\s+FOR\s+UPDATE\s*)$/i', $query, $matches)) {
$after = $matches[0];
$query = preg_replace('/(\s+FOR\s+UPDATE\s*)$/im', '', $query);
} elseif (preg_match('/(\s+LOCK\s+IN\s+SHARE\s+MODE\s*)$/im', $query, $matches)) {
$after = $matches[0];
$query = preg_replace('/(\s+LOCK\s+IN\s+SHARE\s+MODE\s*)$/im', '', $query);
}
if ($is_manip) { if ($is_manip) {
return $query . " LIMIT $limit"; return $query . " LIMIT $limit";
} else { } else {
@@ -715,7 +698,7 @@ class MDB2_Driver_mysqli extends MDB2_Driver_Common
/** /**
* return version information about the server * return version information about the server
* *
* @param string $native determines if the raw version string should be returned * @param bool $native determines if the raw version string should be returned
* @return mixed array/string with version information or MDB2 error object * @return mixed array/string with version information or MDB2 error object
* @access public * @access public
*/ */
@@ -755,6 +738,80 @@ class MDB2_Driver_mysqli extends MDB2_Driver_Common
return $server_info; return $server_info;
} }
// }}}
// {{{ _getServerCapabilities()
/**
* Fetch some information about the server capabilities
* (transactions, subselects, prepared statements, etc).
*
* @access private
*/
function _getServerCapabilities()
{
static $already_checked = false;
if (!$already_checked) {
$already_checked = true;
//set defaults
$this->supported['sub_selects'] = 'emulated';
$this->supported['prepared_statements'] = 'emulated';
$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', '<')) {
$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;
}
if (!version_compare($server_info['major'].'.'.$server_info['minor'].'.'.$server_info['patch'], '4.0.11', '<')) {
$this->start_transaction = true;
}
if (!version_compare($server_info['major'].'.'.$server_info['minor'].'.'.$server_info['patch'], '5.0.3', '<')) {
$this->varchar_max_length = 65532;
}
}
}
}
// }}}
// {{{ function _skipUserDefinedVariable($query, $position)
/**
* Utility method, used by prepare() to avoid misinterpreting MySQL user
* defined variables (SELECT @x:=5) for placeholders.
* Check if the placeholder is a false positive, i.e. if it is an user defined
* variable instead. If so, skip it and advance the position, otherwise
* return the current position, which is valid
*
* @param string $query
* @param integer $position current string cursor position
* @return integer $new_position
* @access protected
*/
function _skipUserDefinedVariable($query, $position)
{
$found = strpos(strrev(substr($query, 0, $position)), '@');
if ($found === false) {
return $position;
}
$pos = strlen($query) - strlen(substr($query, $position)) - $found - 1;
$substring = substr($query, $pos, $position - $pos + 2);
if (preg_match('/^@\w+:=$/', $substring)) {
return $position + 1; //found an user defined variable: skip it
}
return $position;
}
// }}} // }}}
// {{{ prepare() // {{{ prepare()
@@ -834,6 +891,12 @@ class MDB2_Driver_mysqli extends MDB2_Driver_Common
$question = $colon = $placeholder_type; $question = $colon = $placeholder_type;
} }
if ($placeholder_type == ':') { if ($placeholder_type == ':') {
//make sure this is not part of an user defined variable
$new_pos = $this->_skipUserDefinedVariable($query, $position);
if ($new_pos != $position) {
$position = $new_pos;
continue; //evaluate again starting from the new position
}
$parameter = preg_replace('/^.{'.($position+1).'}([a-z0-9_]+).*$/si', '\\1', $query); $parameter = preg_replace('/^.{'.($position+1).'}([a-z0-9_]+).*$/si', '\\1', $query);
if ($parameter === '') { if ($parameter === '') {
$err =& $this->raiseError(MDB2_ERROR_SYNTAX, null, null, $err =& $this->raiseError(MDB2_ERROR_SYNTAX, null, null,
@@ -1208,7 +1271,6 @@ class MDB2_Result_mysqli extends MDB2_Result_Common
/** /**
* Move the internal result pointer to the next available result * Move the internal result pointer to the next available result
* *
* @param a valid result resource
* @return true on success, false if there is no more result set or an error object on failure * @return true on success, false if there is no more result set or an error object on failure
* @access public * @access public
*/ */
@@ -1433,7 +1495,11 @@ class MDB2_Statement_mysqli extends MDB2_Statement_Common
$value = $data; $value = $data;
} }
} }
$param_query = 'SET @'.$parameter.' = '.$this->db->quote($value, $type); $quoted = $this->db->quote($value, $type);
if (PEAR::isError($quoted)) {
return $quoted;
}
$param_query = 'SET @'.$parameter.' = '.$quoted;
$result = $this->db->_doQuery($param_query, true, $connection); $result = $this->db->_doQuery($param_query, true, $connection);
if (PEAR::isError($result)) { if (PEAR::isError($result)) {
return $result; return $result;

View File

@@ -674,7 +674,7 @@ class MDB2_Driver_pgsql extends MDB2_Driver_Common
function _modifyQuery($query, $is_manip, $limit, $offset) function _modifyQuery($query, $is_manip, $limit, $offset)
{ {
if ($limit > 0 if ($limit > 0
&& !preg_match('/LIMIT\s*\d(\s*(,|OFFSET)\s*\d+)?/i', $query) && !preg_match('/LIMIT\s*\d(?:\s*(?:,|OFFSET)\s*\d+)?(?:[^\)]*)?$/i', $query)
) { ) {
$query = rtrim($query); $query = rtrim($query);
if (substr($query, -1) == ';') { if (substr($query, -1) == ';') {
@@ -726,7 +726,7 @@ class MDB2_Driver_pgsql extends MDB2_Driver_Common
/** /**
* return version information about the server * return version information about the server
* *
* @param string $native determines if the raw version string should be returned * @param bool $native determines if the raw version string should be returned
* @return mixed array/string with version information or MDB2 error object * @return mixed array/string with version information or MDB2 error object
* @access public * @access public
*/ */
@@ -927,6 +927,48 @@ class MDB2_Driver_pgsql extends MDB2_Driver_Common
return $obj; return $obj;
} }
// }}}
// {{{ function getSequenceName($sqn)
/**
* adds sequence name formatting to a sequence name
*
* @param string name of the sequence
*
* @return string formatted sequence name
*
* @access public
*/
function getSequenceName($sqn)
{
list($table, $field) = explode('_', $sqn);
$query = "SELECT substring((SELECT substring(pg_get_expr(d.adbin, d.adrelid) for 128)
FROM pg_attrdef d
WHERE d.adrelid = a.attrelid
AND d.adnum = a.attnum
AND a.atthasdef
) 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
WHERE (c.relname = ".$this->quote($sqn, 'text');
if (!empty($field)) {
$query .= " OR (c.relname = ".$this->quote($table, 'text')." AND a.attname = ".$this->quote($field, 'text').")";
}
$query .= " )
AND NOT a.attisdropped
AND a.attnum > 0
AND pg_get_expr(d.adbin, d.adrelid) LIKE 'nextval%'
ORDER BY a.attnum";
$seqname = $this->queryOne($query);
if (!PEAR::isError($seqname) && !empty($seqname) && is_string($seqname)) {
return $seqname;
}
return sprintf($this->options['seqname_format'],
preg_replace('/[^\w\$.]/i', '_', $sqn));
}
// }}} // }}}
// {{{ nextID() // {{{ nextID()
@@ -975,6 +1017,9 @@ class MDB2_Driver_pgsql extends MDB2_Driver_Common
*/ */
function lastInsertID($table = null, $field = null) function lastInsertID($table = null, $field = null)
{ {
if (empty($table) && empty($field)) {
return $this->queryOne('SELECT lastval()', 'integer');
}
$seq = $table.(empty($field) ? '' : '_'.$field); $seq = $table.(empty($field) ? '' : '_'.$field);
$sequence_name = $this->getSequenceName($seq); $sequence_name = $this->getSequenceName($seq);
return $this->queryOne("SELECT currval('$sequence_name')", 'integer'); return $this->queryOne("SELECT currval('$sequence_name')", 'integer');
@@ -1138,7 +1183,6 @@ class MDB2_Result_pgsql extends MDB2_Result_Common
/** /**
* Move the internal result pointer to the next available result * Move the internal result pointer to the next available result
* *
* @param a valid result resource
* @return true on success, false if there is no more result set or an error object on failure * @return true on success, false if there is no more result set or an error object on failure
* @access public * @access public
*/ */

View File

@@ -198,6 +198,10 @@ class MDB2_Extended extends MDB2_Module_Common
return $db; return $db;
} }
if ($db->options['quote_identifier']) {
$table = $db->quoteIdentifier($table);
}
if (!empty($table_fields) && $db->options['quote_identifier']) { if (!empty($table_fields) && $db->options['quote_identifier']) {
foreach ($table_fields as $key => $field) { foreach ($table_fields as $key => $field) {
$table_fields[$key] = $db->quoteIdentifier($field); $table_fields[$key] = $db->quoteIdentifier($field);
@@ -527,32 +531,35 @@ class MDB2_Extended extends MDB2_Module_Common
* columns, a MDB2_ERROR_TRUNCATED error is returned. * columns, a MDB2_ERROR_TRUNCATED error is returned.
* *
* For example, if the table 'mytable' contains: * For example, if the table 'mytable' contains:
* * <pre>
* ID TEXT DATE * ID TEXT DATE
* -------------------------------- * --------------------------------
* 1 'one' 944679408 * 1 'one' 944679408
* 2 'two' 944679408 * 2 'two' 944679408
* 3 'three' 944679408 * 3 'three' 944679408
* * </pre>
* Then the call getAssoc('SELECT id,text FROM mytable') returns: * Then the call getAssoc('SELECT id,text FROM mytable') returns:
* <pre>
* array( * array(
* '1' => 'one', * '1' => 'one',
* '2' => 'two', * '2' => 'two',
* '3' => 'three', * '3' => 'three',
* ) * )
* * </pre>
* ...while the call getAssoc('SELECT id,text,date FROM mytable') returns: * ...while the call getAssoc('SELECT id,text,date FROM mytable') returns:
* <pre>
* array( * array(
* '1' => array('one', '944679408'), * '1' => array('one', '944679408'),
* '2' => array('two', '944679408'), * '2' => array('two', '944679408'),
* '3' => array('three', '944679408') * '3' => array('three', '944679408')
* ) * )
* </pre>
* *
* If the more than one row occurs with the same value in the * If the more than one row occurs with the same value in the
* first column, the last row overwrites all previous ones by * first column, the last row overwrites all previous ones by
* default. Use the $group parameter if you don't want to * default. Use the $group parameter if you don't want to
* overwrite like this. Example: * overwrite like this. Example:
* * <pre>
* getAssoc('SELECT category,id,name FROM mytable', null, null * getAssoc('SELECT category,id,name FROM mytable', null, null
* MDB2_FETCHMODE_ASSOC, false, true) returns: * MDB2_FETCHMODE_ASSOC, false, true) returns:
* array( * array(
@@ -563,6 +570,7 @@ class MDB2_Extended extends MDB2_Module_Common
* array('id' => '6', 'name' => 'number six') * array('id' => '6', 'name' => 'number six')
* ) * )
* ) * )
* </pre>
* *
* Keep in mind that database functions in PHP usually return string * Keep in mind that database functions in PHP usually return string
* values for results regardless of the database's internal type. * values for results regardless of the database's internal type.

View File

@@ -50,8 +50,7 @@
* @author Lukas Smith <smith@pooteeweet.org> * @author Lukas Smith <smith@pooteeweet.org>
*/ */
//require_once 'MDB2.php'; require_once 'MDB2.php';
require_once PEAR_PATH."/MDB2.php";
/** /**
* MDB2_LOB: user land stream wrapper implementation for LOB support * MDB2_LOB: user land stream wrapper implementation for LOB support