Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

- Add support for MDB2's 'sqlsrv' driver (#1486395)

  • Loading branch information...
commit 2273d4117fd50ee44dcdaa28fd6444383dc403a0 1 parent 8f85e37
@alecpl alecpl authored
Showing with 20,083 additions and 15,670 deletions.
  1. +1 −0  CHANGELOG
  2. +1 −1  config/db.inc.php.dist
  3. +1 −0  program/include/rcmail.php
  4. +3 −1 program/include/rcube_mdb2.php
  5. +143 −106 program/lib/MDB2.php
  6. +1 −1  program/lib/MDB2/Date.php
  7. +31 −17 program/lib/MDB2/Driver/Datatype/Common.php
  8. +63 −3 program/lib/MDB2/Driver/Datatype/mssql.php
  9. +558 −552 program/lib/MDB2/Driver/Datatype/mysql.php
  10. +595 −589 program/lib/MDB2/Driver/Datatype/mysqli.php
  11. +575 −554 program/lib/MDB2/Driver/Datatype/pgsql.php
  12. +414 −408 program/lib/MDB2/Driver/Datatype/sqlite.php
  13. +451 −0 program/lib/MDB2/Driver/Datatype/sqlsrv.php
  14. +292 −292 program/lib/MDB2/Driver/Function/Common.php
  15. +192 −192 program/lib/MDB2/Driver/Function/mssql.php
  16. +135 −135 program/lib/MDB2/Driver/Function/mysql.php
  17. +143 −143 program/lib/MDB2/Driver/Function/mysqli.php
  18. +114 −114 program/lib/MDB2/Driver/Function/pgsql.php
  19. +162 −162 program/lib/MDB2/Driver/Function/sqlite.php
  20. +189 −0 program/lib/MDB2/Driver/Function/sqlsrv.php
  21. +1,013 −1,009 program/lib/MDB2/Driver/Manager/Common.php
  22. +6 −6 program/lib/MDB2/Driver/Manager/mssql.php
  23. +1,437 −1,417 program/lib/MDB2/Driver/Manager/mysql.php
  24. +1,437 −1,417 program/lib/MDB2/Driver/Manager/mysqli.php
  25. +100 −11 program/lib/MDB2/Driver/Manager/pgsql.php
  26. +1,361 −1,355 program/lib/MDB2/Driver/Manager/sqlite.php
  27. +1,383 −0 program/lib/MDB2/Driver/Manager/sqlsrv.php
  28. +1 −1  program/lib/MDB2/Driver/Native/Common.php
  29. +1 −1  program/lib/MDB2/Driver/Native/mssql.php
  30. +1 −1  program/lib/MDB2/Driver/Native/mysql.php
  31. +1 −1  program/lib/MDB2/Driver/Native/mysqli.php
  32. +1 −1  program/lib/MDB2/Driver/Native/pgsql.php
  33. +1 −1  program/lib/MDB2/Driver/Native/sqlite.php
  34. +57 −0 program/lib/MDB2/Driver/Native/sqlsrv.php
  35. +516 −512 program/lib/MDB2/Driver/Reverse/Common.php
  36. +652 −652 program/lib/MDB2/Driver/Reverse/mssql.php
  37. +535 −513 program/lib/MDB2/Driver/Reverse/mysql.php
  38. +599 −577 program/lib/MDB2/Driver/Reverse/mysqli.php
  39. +573 −529 program/lib/MDB2/Driver/Reverse/pgsql.php
  40. +608 −584 program/lib/MDB2/Driver/Reverse/sqlite.php
  41. +653 −0 program/lib/MDB2/Driver/Reverse/sqlsrv.php
  42. +1,173 −1,123 program/lib/MDB2/Driver/mssql.php
  43. +48 −26 program/lib/MDB2/Driver/mysql.php
  44. +66 −48 program/lib/MDB2/Driver/mysqli.php
  45. +1,546 −1,511 program/lib/MDB2/Driver/pgsql.php
  46. +1,085 −1,104 program/lib/MDB2/Driver/sqlite.php
  47. +1,165 −0 program/lib/MDB2/Driver/sqlsrv.php
Sorry, we could not display the entire diff because it was too big.
View
1  CHANGELOG
@@ -1,6 +1,7 @@
CHANGELOG RoundCube Webmail
===========================
+- Add support for MDB2's 'sqlsrv' driver (#1486395)
- Use jQuery-1.4
- Removed problematic browser-caching of messages
- Fix incompatybility with suhosin.executor.disable_emodifier (#1486321)
View
2  config/db.inc.php.dist
@@ -17,7 +17,7 @@ $rcmail_config = array();
// PEAR database DSN for read/write operations
// format is db_provider://user:password@host/database
// For examples see http://pear.php.net/manual/en/package.database.mdb2.intro-dsn.php
-// currently supported db_providers: mysql, mysqli, pgsql, sqlite, mssql
+// currently supported db_providers: mysql, mysqli, pgsql, sqlite, mssql or sqlsrv
$rcmail_config['db_dsnw'] = 'mysql://roundcube:pass@localhost/roundcubemail';
// postgres example: 'pgsql://roundcube:pass@localhost/roundcubemail';
View
1  program/include/rcmail.php
@@ -1051,6 +1051,7 @@ public static function setcookie($name, $value, $exp = 0)
return;
$cookie = session_get_cookie_params();
+
setcookie($name, $value, $exp, $cookie['path'], $cookie['domain'],
rcube_https_check(), true);
}
View
4 program/include/rcube_mdb2.php
@@ -106,7 +106,7 @@ function dsn_connect($dsn)
if (!filesize($dsn_array['database']) && !empty($this->sqlite_initials))
$this->_sqlite_create_database($dbh, $this->sqlite_initials);
}
- else if ($this->db_provider!='mssql')
+ else if ($this->db_provider!='mssql' && $this->db_provider!='sqlsrv')
$dbh->setCharset('utf8');
return $dbh;
@@ -467,6 +467,7 @@ function now()
switch($this->db_provider)
{
case 'mssql':
+ case 'sqlsrv':
return "getdate()";
default:
@@ -511,6 +512,7 @@ function unixtimestamp($field)
break;
case 'mssql':
+ case 'sqlsrv':
return "DATEDIFF(second, '19700101', $field) + DATEDIFF(second, GETDATE(), GETUTCDATE())";
default:
View
249 program/lib/MDB2.php
@@ -43,7 +43,7 @@
// | Author: Lukas Smith <smith@pooteeweet.org> |
// +----------------------------------------------------------------------+
//
-// $Id: MDB2.php,v 1.318 2008/03/08 14:18:38 quipo Exp $
+// $Id: MDB2.php 292663 2009-12-26 18:21:46Z quipo $
//
/**
@@ -101,6 +101,7 @@
define('MDB2_ERROR_LOADMODULE', -34);
define('MDB2_ERROR_INSUFFICIENT_DATA', -35);
define('MDB2_ERROR_NO_PERMISSION', -36);
+define('MDB2_ERROR_DISCONNECT_FAILED', -37);
// }}}
// {{{ Verbose constants
@@ -342,6 +343,11 @@ function loadClass($class_name, $debug)
$err =& MDB2::raiseError(MDB2_ERROR_NOT_FOUND, null, null, $msg);
return $err;
}
+ if (!MDB2::classExists($class_name)) {
+ $msg = "unable to load class '$class_name' from file '$file_name'";
+ $err =& MDB2::raiseError(MDB2_ERROR_NOT_FOUND, null, null, $msg);
+ return $err;
+ }
}
return MDB2_OK;
}
@@ -403,7 +409,7 @@ function &factory($dsn, $options = false)
// {{{ function &connect($dsn, $options = false)
/**
- * Create a new MDB2 connection object and connect to the specified
+ * Create a new MDB2_Driver_* connection object and connect to the specified
* database
*
* IMPORTANT: In order for MDB2 to work properly it is necessary that
@@ -417,15 +423,15 @@ function &factory($dsn, $options = false)
* $db = MDB2::connect($dsn);
* ^^
*
- * @param mixed 'data source name', see the MDB2::parseDSN
- * method for a description of the dsn format.
- * Can also be specified as an array of the
- * format returned by MDB2::parseDSN.
- * @param array An associative array of option names and
- * their values.
+ * @param mixed $dsn 'data source name', see the MDB2::parseDSN
+ * method for a description of the dsn format.
+ * Can also be specified as an array of the
+ * format returned by MDB2::parseDSN.
+ * @param array $options An associative array of option names and
+ * their values.
*
- * @return mixed a newly created MDB2 connection object, or a MDB2
- * error object on error
+ * @return mixed a newly created MDB2 connection object, or a MDB2
+ * error object on error
*
* @access public
* @see MDB2::parseDSN
@@ -566,7 +572,7 @@ function loadFile($file)
*/
function apiVersion()
{
- return '2.5.0b1';
+ return '@package_version@';
}
// }}}
@@ -627,7 +633,7 @@ function &raiseError($code = null,
function isError($data, $code = null)
{
if (is_a($data, 'MDB2_Error')) {
- if (is_null($code)) {
+ if (null === $code) {
return true;
} elseif (is_string($code)) {
return $data->getMessage() === $code;
@@ -767,10 +773,11 @@ function errorMessage($value = null)
MDB2_ERROR_TRUNCATED => 'truncated',
MDB2_ERROR_DEADLOCK => 'deadlock detected',
MDB2_ERROR_NO_PERMISSION => 'no permission',
+ MDB2_ERROR_DISCONNECT_FAILED => 'disconnect failed',
);
}
- if (is_null($value)) {
+ if (null === $value) {
return $errorMessages;
}
@@ -824,7 +831,7 @@ function errorMessage($value = null)
*/
function parseDSN($dsn)
{
- $parsed = array();
+ $parsed = $GLOBALS['_MDB2_dsninfo_default'];
if (is_array($dsn)) {
$dsn = array_merge($parsed, $dsn);
@@ -843,7 +850,6 @@ function parseDSN($dsn)
$dsn = null;
}
-
// Get phptype and dbsyntax
// $str => phptype(dbsyntax)
if (preg_match('|^(.+?)\((.*?)\)$|', $str, $arr)) {
@@ -855,7 +861,7 @@ function parseDSN($dsn)
}
if (!count($dsn)) {
- return array_merge($GLOBALS['_MDB2_dsninfo_default'], $parsed);
+ return $parsed;
}
// Get (if found): username and password
@@ -892,7 +898,9 @@ function parseDSN($dsn)
//"username/password@[//]host[:port][/service_name]"
//e.g. "scott/tiger@//mymachine:1521/oracle"
$proto_opts = $dsn;
- $dsn = substr($proto_opts, strrpos($proto_opts, '/') + 1);
+ $pos = strrpos($proto_opts, '/');
+ $dsn = substr($proto_opts, $pos + 1);
+ $proto_opts = substr($proto_opts, 0, $pos);
} elseif (strpos($dsn, '/') !== false) {
list($proto_opts, $dsn) = explode('/', $dsn, 2);
} else {
@@ -930,7 +938,7 @@ function parseDSN($dsn)
}
foreach ($opts as $opt) {
list($key, $value) = explode('=', $opt);
- if (!isset($parsed[$key])) {
+ if (!array_key_exists($key, $parsed) || false === $parsed[$key]) {
// don't allow params overwrite
$parsed[$key] = rawurldecode($value);
}
@@ -938,7 +946,7 @@ function parseDSN($dsn)
}
}
- return array_merge($GLOBALS['_MDB2_dsninfo_default'], $parsed);
+ return $parsed;
}
// }}}
@@ -1001,7 +1009,7 @@ class MDB2_Error extends PEAR_Error
function MDB2_Error($code = MDB2_ERROR, $mode = PEAR_ERROR_RETURN,
$level = E_USER_NOTICE, $debuginfo = null, $dummy = null)
{
- if (is_null($code)) {
+ if (null === $code) {
$code = MDB2_ERROR;
}
$this->PEAR_Error('MDB2 Error: '.MDB2::errorMessage($code), $code,
@@ -1146,8 +1154,11 @@ class MDB2_Driver_Common extends PEAR
* <li>$options['emulate_prepared'] -> boolean: force prepared statements to be emulated</li>
* <li>$options['datatype_map'] -> array: map user defined datatypes to other primitive datatypes</li>
* <li>$options['datatype_map_callback'] -> array: callback function/method that should be called</li>
- * <li>$options['bindname_format'] -> string: regular expression pattern for named parameters
+ * <li>$options['bindname_format'] -> string: regular expression pattern for named parameters</li>
+ * <li>$options['multi_query'] -> boolean: determines if queries returning multiple result sets should be executed</li>
* <li>$options['max_identifiers_length'] -> integer: max identifier length</li>
+ * <li>$options['default_fk_action_onupdate'] -> string: default FOREIGN KEY ON UPDATE action ['RESTRICT'|'NO ACTION'|'SET DEFAULT'|'SET NULL'|'CASCADE']</li>
+ * <li>$options['default_fk_action_ondelete'] -> string: default FOREIGN KEY ON DELETE action ['RESTRICT'|'NO ACTION'|'SET DEFAULT'|'SET NULL'|'CASCADE']</li>
* </ul>
*
* @var array
@@ -1197,6 +1208,8 @@ class MDB2_Driver_Common extends PEAR
'lob_allow_url_include' => false,
'bindname_format' => '(?:\d+)|(?:[a-zA-Z][a-zA-Z0-9_]*)',
'max_identifiers_length' => 30,
+ 'default_fk_action_onupdate' => 'RESTRICT',
+ 'default_fk_action_ondelete' => 'RESTRICT',
);
/**
@@ -1427,36 +1440,44 @@ function errorInfo($error = null)
* callbacks etc. Basically a wrapper for PEAR::raiseError
* without the message string.
*
- * @param mixed integer error code, or a PEAR error object (all other
- * parameters are ignored if this parameter is an object
- * @param int error mode, see PEAR_Error docs
- * @param mixed If error mode is PEAR_ERROR_TRIGGER, this is the
- * error level (E_USER_NOTICE etc). If error mode is
- * PEAR_ERROR_CALLBACK, this is the callback function,
- * either as a function name, or as an array of an
- * object and method name. For other error modes this
- * parameter is ignored.
- * @param string Extra debug information. Defaults to the last
- * query and native error code.
- * @param string name of the method that triggered the error
- *
- * @return PEAR_Error instance of a PEAR Error object
+ * @param mixed $code integer error code, or a PEAR error object (all
+ * other parameters are ignored if this parameter is
+ * an object
+ * @param int $mode error mode, see PEAR_Error docs
+ * @param mixed $options If error mode is PEAR_ERROR_TRIGGER, this is the
+ * error level (E_USER_NOTICE etc). If error mode is
+ * PEAR_ERROR_CALLBACK, this is the callback function,
+ * either as a function name, or as an array of an
+ * object and method name. For other error modes this
+ * parameter is ignored.
+ * @param string $userinfo Extra debug information. Defaults to the last
+ * query and native error code.
+ * @param string $method name of the method that triggered the error
+ * @param string $dummy1 not used
+ * @param bool $dummy2 not used
*
- * @access public
- * @see PEAR_Error
+ * @return PEAR_Error instance of a PEAR Error object
+ * @access public
+ * @see PEAR_Error
*/
- function &raiseError($code = null, $mode = null, $options = null, $userinfo = null, $method = null)
- {
+ function &raiseError($code = null,
+ $mode = null,
+ $options = null,
+ $userinfo = null,
+ $method = null,
+ $dummy1 = null,
+ $dummy2 = false
+ ) {
$userinfo = "[Error message: $userinfo]\n";
// The error is yet a MDB2 error object
if (PEAR::isError($code)) {
// because we use the static PEAR::raiseError, our global
// handler should be used if it is set
- if (is_null($mode) && !empty($this->_default_error_mode)) {
+ if ((null === $mode) && !empty($this->_default_error_mode)) {
$mode = $this->_default_error_mode;
$options = $this->_default_error_options;
}
- if (is_null($userinfo)) {
+ if (null === $userinfo) {
$userinfo = $code->getUserinfo();
}
$code = $code->getCode();
@@ -1469,13 +1490,13 @@ function &raiseError($code = null, $mode = null, $options = null, $userinfo = nu
}
$native_errno = $native_msg = null;
list($code, $native_errno, $native_msg) = $this->errorInfo($code);
- if (!is_null($native_errno) && $native_errno !== '') {
+ if ((null !== $native_errno) && $native_errno !== '') {
$userinfo.= "[Native code: $native_errno]\n";
}
- if (!is_null($native_msg) && $native_msg !== '') {
+ if ((null !== $native_msg) && $native_msg !== '') {
$userinfo.= "[Native message: ". strip_tags($native_msg) ."]\n";
}
- if (!is_null($method)) {
+ if (null !== $method) {
$userinfo = $method.': '.$userinfo;
}
}
@@ -1926,7 +1947,7 @@ function &loadModule($module, $property = null, $phptype_specific = null)
return $err;
}
$this->{$property} = new $class_name($this->db_index);
- $this->modules[$module] = $this->{$property};
+ $this->modules[$module] =& $this->{$property};
if ($version) {
// this will be used in the connect method to determine if the module
// needs to be loaded with a different version if the server
@@ -1973,7 +1994,7 @@ function __call($method, $params)
}
}
}
- if (!is_null($module)) {
+ if (null !== $module) {
return call_user_func_array(array(&$this->modules[$module], $method), $params);
}
trigger_error(sprintf('Call to undefined function: %s::%s().', get_class($this), $method), E_USER_ERROR);
@@ -2183,7 +2204,7 @@ function completeNestedTransaction($force_rollback = false)
*/
function failNestedTransaction($error = null, $immediately = false)
{
- if (is_null($error)) {
+ if (null !== $error) {
$error = $this->has_transaction_error ? $this->has_transaction_error : true;
} elseif (!$error) {
$error = true;
@@ -2265,11 +2286,10 @@ function setCharset($charset, $connection = null)
/**
* Log out and disconnect from the database.
*
- * @param bool if the disconnect should be forced even if the
- * connection is opened persistently
+ * @param boolean $force whether the disconnect should be forced even if the
+ * connection is opened persistently
*
- * @return mixed true on success, false if not connected and error
- * object on error
+ * @return mixed true on success, false if not connected and error object on error
*
* @access public
*/
@@ -2385,6 +2405,26 @@ function getDSN($type = 'string', $hidepw = false)
}
// }}}
+ // {{{ _isNewLinkSet()
+
+ /**
+ * Check if the 'new_link' option is set
+ *
+ * @return boolean
+ *
+ * @access protected
+ */
+ function _isNewLinkSet()
+ {
+ return (isset($this->dsn['new_link'])
+ && ($this->dsn['new_link'] === true
+ || (is_string($this->dsn['new_link']) && preg_match('/^true$/i', $this->dsn['new_link']))
+ || (is_numeric($this->dsn['new_link']) && 0 != (int)$this->dsn['new_link'])
+ )
+ );
+ }
+
+ // }}}
// {{{ function &standaloneQuery($query, $types = null, $is_manip = false)
/**
@@ -2563,7 +2603,7 @@ function &query($query, $types = null, $result_class = true, $result_wrap_class
}
// }}}
- // {{{ function &_wrapResult($result, $types = array(), $result_class = true, $result_wrap_class = false, $limit = null, $offset = null)
+ // {{{ function &_wrapResult($result_resource, $types = array(), $result_class = true, $result_wrap_class = false, $limit = null, $offset = null)
/**
* wrap a result set into the correct class
@@ -2634,7 +2674,7 @@ function &_wrapResult($result_resource, $types = array(), $result_class = true,
'result wrap class does not exist '.$result_wrap_class, __FUNCTION__);
return $err;
}
- $result = new $result_wrap_class($result, $this->fetchmode);
+ $result = new $result_wrap_class($result_resource, $this->fetchmode);
}
return $result;
}
@@ -2682,7 +2722,7 @@ function setLimit($limit, $offset = null)
'it was not specified a valid selected range row limit', __FUNCTION__);
}
$this->limit = $limit;
- if (!is_null($offset)) {
+ if (null !== $offset) {
$offset = (int)$offset;
if ($offset < 0) {
return $this->raiseError(MDB2_ERROR_SYNTAX, null, null,
@@ -2739,8 +2779,7 @@ function subSelect($query, $type = false)
/**
* Execute a SQL REPLACE query. A REPLACE query is identical to a INSERT
* query, except that if there is already a row in the table with the same
- * key field values, the REPLACE query just updates its values instead of
- * inserting a new row.
+ * key field values, the old row is deleted before the new row is inserted.
*
* The REPLACE type of query does not make part of the SQL standards. Since
* practically only MySQL and SQLite implement it natively, this type of
@@ -2912,11 +2951,10 @@ function &prepare($query, $types = null, $result_types = null, $lobs = array())
$query = $result;
}
$placeholder_type_guess = $placeholder_type = null;
- $question = '?';
- $colon = ':';
+ $question = '?';
+ $colon = ':';
$positions = array();
- $position = 0;
-
+ $position = 0;
while ($position < strlen($query)) {
$q_position = strpos($query, $question, $position);
$c_position = strpos($query, $colon, $position);
@@ -2929,7 +2967,7 @@ function &prepare($query, $types = null, $result_types = null, $lobs = array())
} else {
break;
}
- if (is_null($placeholder_type)) {
+ if (null === $placeholder_type) {
$placeholder_type_guess = $query[$p_position];
}
@@ -2943,7 +2981,7 @@ function &prepare($query, $types = null, $result_types = null, $lobs = array())
}
if ($query[$position] == $placeholder_type_guess) {
- if (is_null($placeholder_type)) {
+ if (null === $placeholder_type) {
$placeholder_type = $query[$p_position];
$question = $colon = $placeholder_type;
if (!empty($types) && is_array($types)) {
@@ -3007,8 +3045,8 @@ function &prepare($query, $types = null, $result_types = null, $lobs = array())
*/
function _skipDelimitedStrings($query, $position, $p_position)
{
- $ignores[] = $this->string_quoting;
- $ignores[] = $this->identifier_quoting;
+ $ignores = $this->string_quoting;
+ $ignores[] = $this->identifier_quoting;
$ignores = array_merge($ignores, $this->sql_comments);
foreach ($ignores as $ignore) {
@@ -3026,10 +3064,12 @@ function _skipDelimitedStrings($query, $position, $p_position)
}
}
} while ($ignore['escape']
- } while ($ignore['escape']
- && $end_quote-1 != $start_quote
- && $query[($end_quote - 1)] == $ignore['escape']
- && ($ignore['escape_pattern'] !== $ignore['escape']
+ && $end_quote-1 != $start_quote
+ && $query[($end_quote - 1)] == $ignore['escape']
+ && ( $ignore['escape_pattern'] !== $ignore['escape']
+ || $query[($end_quote - 2)] != $ignore['escape'])
+ );
+
$position = $end_quote + 1;
return $position;
}
@@ -3169,7 +3209,7 @@ function getSequenceName($sqn)
function getIndexName($idx)
{
return sprintf($this->options['idxname_format'],
- return sprintf($this->options['idxname_format'],
+ preg_replace('/[^a-z0-9_\-\$.]/i', '_', $idx));
}
// }}}
@@ -3238,12 +3278,12 @@ function currID($seq_name)
* the first row of the result set and then frees
* the result set.
*
- *
- * @param string the SELECT query statement to be executed.
- * @param string optional argument that specifies the expected
- * datatype of the result set field, so that an eventual conversion
- * may be performed. The default datatype is text, meaning that no
- * conversion is performed
+ * @param string $query the SELECT query statement to be executed.
+ * @param string $type optional argument that specifies the expected
+ * datatype of the result set field, so that an eventual
+ * conversion may be performed. The default datatype is
+ * text, meaning that no conversion is performed
+ * @param mixed $colnum the column number (or name) to fetch
*
* @return mixed MDB2_OK or field value on success, a MDB2 error on failure
*
@@ -3299,15 +3339,14 @@ function queryRow($query, $types = null, $fetchmode = MDB2_FETCHMODE_DEFAULT)
* Execute the specified query, fetch the value from the first column of
* each row of the result set into an array and then frees the result set.
*
- *
- * @param string the SELECT query statement to be executed.
- * @param string optional argument that specifies the expected
- * datatype of the result set field, so that an eventual conversion
- * may be performed. The default datatype is text, meaning that no
- * conversion is performed
+ * @param string $query the SELECT query statement to be executed.
+ * @param string $type optional argument that specifies the expected
+ * datatype of the result set field, so that an eventual
+ * conversion may be performed. The default datatype is text,
+ * meaning that no conversion is performed
+ * @param mixed $colnum the column number (or name) to fetch
*
* @return mixed MDB2_OK or data array on success, a MDB2 error on failure
- * @return mixed MDB2_OK or data array on success, a MDB2 error on failure
* @access public
*/
function queryCol($query, $type = null, $colnum = 0)
@@ -3517,11 +3556,10 @@ function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT, $rownum = null)
/**
* fetch single column from the next row from a result set
*
- *
- * @param int the column number to fetch
- * @param int number of the row where the data can be found
- *
+ * @param int|string the column number (or name) to fetch
+ * @param int number of the row where the data can be found
*
+ * @return string data on success, a MDB2 error on failure
* @access public
*/
function fetchOne($colnum = 0, $rownum = null)
@@ -3544,10 +3582,9 @@ function fetchOne($colnum = 0, $rownum = null)
/**
* Fetch and return a column from the current row pointer position
*
- *
- * @param int the column number to fetch
- *
+ * @param int|string the column number (or name) to fetch
*
+ * @return mixed data array on success, a MDB2 error on failure
* @access public
*/
function fetchCol($colnum = 0)
@@ -3608,7 +3645,7 @@ function fetchAll($fetchmode = MDB2_FETCHMODE_DEFAULT, $rekey = false,
}
$shift_array = $rekey ? false : null;
- $shift_array = $rekey ? false : null;
+ if (null !== $shift_array) {
if (is_object($row)) {
$colnum = count(get_object_vars($row));
} else {
@@ -3814,7 +3851,7 @@ function bindColumn($column, &$value, $type = null)
$column = $column_names[$column];
}
$this->values[$column] =& $value;
- $this->values[$column] =& $value;
+ if (null !== $type) {
$this->types[$column] = $type;
}
return MDB2_OK;
@@ -3984,7 +4021,7 @@ function bindValue($parameter, $value, $type = null)
'Unable to bind to missing placeholder: '.$parameter, __FUNCTION__);
}
$this->values[$parameter] = $value;
- $this->values[$parameter] = $value;
+ if (null !== $type) {
$this->types[$parameter] = $type;
}
return MDB2_OK;
@@ -4053,7 +4090,7 @@ function bindParam($parameter, &$value, $type = null)
'Unable to bind to missing placeholder: '.$parameter, __FUNCTION__);
}
$this->values[$parameter] =& $value;
- $this->values[$parameter] =& $value;
+ if (null !== $type) {
$this->types[$parameter] = $type;
}
return MDB2_OK;
@@ -4094,19 +4131,19 @@ function bindParamArray(&$values, $types = null)
/**
* Execute a prepared query statement.
*
- *
- * @param array specifies all necessary information
- * for bindParam() the array elements must use keys corresponding to
- * the number of the position of the parameter.
- * @param mixed specifies which result class to use
- * @param mixed specifies which class to wrap results in
- *
+ * @param array specifies all necessary information
+ * for bindParam() the array elements must use keys corresponding
+ * to the number of the position of the parameter.
+ * @param mixed specifies which result class to use
+ * @param mixed specifies which class to wrap results in
*
- *
+ * @return mixed MDB2_Result or integer (affected rows) on success,
+ * a MDB2 error on failure
+ * @access public
*/
function &execute($values = null, $result_class = true, $result_wrap_class = false)
{
- {
+ if (null === $this->positions) {
return $this->db->raiseError(MDB2_ERROR, null, null,
'Prepared statement has already been freed', __FUNCTION__);
}
@@ -4132,8 +4169,8 @@ function &execute($values = null, $result_class = true, $result_wrap_class = fal
* @param mixed specifies which result class to use
* @param mixed specifies which class to wrap results in
*
- *
- * @return mixed MDB2_Result or integer on success, a MDB2 error on failure
+ * @return mixed MDB2_Result or integer (affected rows) on success,
+ * a MDB2 error on failure
* @access private
*/
function &_execute($result_class = true, $result_wrap_class = false)
@@ -4184,7 +4221,7 @@ function &_execute($result_class = true, $result_wrap_class = false)
*/
function free()
{
- {
+ if (null === $this->positions) {
return $this->db->raiseError(MDB2_ERROR, null, null,
'Prepared statement has already been freed', __FUNCTION__);
}
@@ -4325,4 +4362,4 @@ function MDB2_defaultDebugOutput(&$db, $scope, $message, $context = array())
}
// }}}
-// }}}
+?>
View
2  program/lib/MDB2/Date.php
@@ -42,7 +42,7 @@
// | Author: Lukas Smith <smith@pooteeweet.org> |
// +----------------------------------------------------------------------+
//
-// $Id: Date.php,v 1.10 2006/03/01 12:15:32 lsmith Exp $
+// $Id: Date.php 208329 2006-03-01 12:15:38Z lsmith $
//
/**
View
48 program/lib/MDB2/Driver/Datatype/Common.php
@@ -42,7 +42,7 @@
// | Author: Lukas Smith <smith@pooteeweet.org> |
// +----------------------------------------------------------------------+
//
-// $Id: Common.php,v 1.137 2008/02/17 18:53:40 afz Exp $
+// $Id: Common.php 292715 2009-12-28 14:06:34Z quipo $
require_once 'MDB2/LOB.php';
@@ -232,7 +232,7 @@ function _baseConvertResult($value, $type, $rtrim = true)
*/
function convertResult($value, $type, $rtrim = true)
{
- if (is_null($value)) {
+ if (null === $value) {
return null;
}
$db =& $this->getDBInstance();
@@ -313,7 +313,7 @@ function _sortResultFieldTypes($columns, $types)
if (count($types)) {
reset($types);
foreach (array_keys($sorted_types) as $k) {
- if (is_null($sorted_types[$k])) {
+ if (null === $sorted_types[$k]) {
$sorted_types[$k] = current($types);
next($types);
}
@@ -511,7 +511,7 @@ function _getDeclarationOptions($field)
$field['default'] = ' ';
}
}
- if (!is_null($field['default'])) {
+ if (null !== $field['default']) {
$default = ' DEFAULT ' . $this->quote($field['default'], $field['type']);
}
}
@@ -1119,7 +1119,7 @@ function quote($value, $type = null, $quote = true, $escape_wildcards = false)
return $db;
}
- if (is_null($value)
+ if ((null === $value)
|| ($value === '' && $db->options['portability'] & MDB2_PORTABILITY_EMPTY_TO_NULL)
) {
if (!$quote) {
@@ -1128,7 +1128,7 @@ function quote($value, $type = null, $quote = true, $escape_wildcards = false)
return 'NULL';
}
- if (is_null($type)) {
+ if (null === $type) {
switch (gettype($value)) {
case 'integer':
$type = 'integer';
@@ -1288,9 +1288,15 @@ function _readFile($value)
*/
function _quoteLOB($value, $quote, $escape_wildcards)
{
- $value = $this->_readFile($value);
- if (PEAR::isError($value)) {
- return $value;
+ $db =& $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+ if ($db->options['lob_allow_url_include']) {
+ $value = $this->_readFile($value);
+ if (PEAR::isError($value)) {
+ return $value;
+ }
}
return $this->_quoteText($value, $quote, $escape_wildcards);
}
@@ -1554,7 +1560,7 @@ function writeLOBToFile($lob, $file)
*/
function _retrieveLOB(&$lob)
{
- if (is_null($lob['value'])) {
+ if (null === $lob['value']) {
$lob['value'] = $lob['resource'];
}
$lob['loaded'] = true;
@@ -1687,21 +1693,32 @@ function matchPattern($pattern, $operator = null, $field = null)
}
$match = '';
- if (!is_null($operator)) {
+ if (null !== $operator) {
$operator = strtoupper($operator);
switch ($operator) {
// case insensitive
case 'ILIKE':
- if (is_null($field)) {
+ if (null === $field) {
return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
'case insensitive LIKE matching requires passing the field name', __FUNCTION__);
}
$db->loadModule('Function', null, true);
$match = $db->function->lower($field).' LIKE ';
break;
+ case 'NOT ILIKE':
+ if (null === $field) {
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'case insensitive NOT ILIKE matching requires passing the field name', __FUNCTION__);
+ }
+ $db->loadModule('Function', null, true);
+ $match = $db->function->lower($field).' NOT LIKE ';
+ break;
// case sensitive
case 'LIKE':
- $match = is_null($field) ? 'LIKE ' : $field.' LIKE ';
+ $match = (null === $field) ? 'LIKE ' : ($field.' LIKE ');
+ break;
+ case 'NOT LIKE':
+ $match = (null === $field) ? 'NOT LIKE ' : ($field.' NOT LIKE ');
break;
default:
return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
@@ -1713,9 +1730,6 @@ function matchPattern($pattern, $operator = null, $field = null)
if ($key % 2) {
$match.= $value;
} else {
- if ($operator === 'ILIKE') {
- $value = strtolower($value);
- }
$escaped = $db->escape($value);
if (PEAR::isError($escaped)) {
return $escaped;
@@ -1821,4 +1835,4 @@ function mapPrepareDatatype($type)
return $type;
}
}
-?>
+?>
View
66 program/lib/MDB2/Driver/Datatype/mssql.php
@@ -44,7 +44,7 @@
// | Daniel Convissor <danielc@php.net> |
// +----------------------------------------------------------------------+
//
-// $Id: mssql.php,v 1.65 2008/02/19 14:54:17 afz Exp $
+// $Id: mssql.php 292715 2009-12-28 14:06:34Z quipo $
//
require_once 'MDB2/Driver/Datatype/Common.php';
@@ -71,7 +71,7 @@ class MDB2_Driver_Datatype_mssql extends MDB2_Driver_Datatype_Common
*/
function _baseConvertResult($value, $type, $rtrim = true)
{
- if (is_null($value)) {
+ if (null === $value) {
return null;
}
switch ($type) {
@@ -228,7 +228,7 @@ function _getIntegerDeclaration($name, $field)
if ($field['default'] === '') {
$field['default'] = 0;
}
- if (is_null($field['default'])) {
+ if (null === $field['default']) {
$default = ' DEFAULT (null)';
} else {
$default = ' DEFAULT (' . $this->quote($field['default'], 'integer') . ')';
@@ -339,6 +339,65 @@ function _quoteBLOB($value, $quote, $escape_wildcards)
}
// }}}
+ // {{{ matchPattern()
+
+ /**
+ * build a pattern matching string
+ *
+ * @access public
+ *
+ * @param array $pattern even keys are strings, odd are patterns (% and _)
+ * @param string $operator optional pattern operator (LIKE, ILIKE and maybe others in the future)
+ * @param string $field optional field name that is being matched against
+ * (might be required when emulating ILIKE)
+ *
+ * @return string SQL pattern
+ */
+ function matchPattern($pattern, $operator = null, $field = null)
+ {
+ $db =& $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ $match = '';
+ if (null !== $operator) {
+ $field = (null === $field) ? '' : $field.' ';
+ $operator = strtoupper($operator);
+ switch ($operator) {
+ // case insensitive
+ case 'ILIKE':
+ $match = $field.'LIKE ';
+ break;
+ case 'NOT ILIKE':
+ $match = $field.'NOT LIKE ';
+ break;
+ // case sensitive
+ case 'LIKE':
+ $match = $field.'LIKE ';
+ break;
+ case 'NOT LIKE':
+ $match = $field.'NOT LIKE ';
+ break;
+ default:
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'not a supported operator type:'. $operator, __FUNCTION__);
+ }
+ }
+ $match.= "'";
+ foreach ($pattern as $key => $value) {
+ if ($key % 2) {
+ $match.= $value;
+ } else {
+ $match.= $db->escapePattern($db->escape($value));
+ }
+ }
+ $match.= "'";
+ $match.= $this->patternEscapeString();
+ return $match;
+ }
+
+ // }}}
// {{{ _mapNativeDatatype()
/**
@@ -376,6 +435,7 @@ function _mapNativeDatatype($field)
$type[0] = 'integer';
$length = 8;
break;
+ case 'smalldatetime':
case 'datetime':
$type[0] = 'timestamp';
break;
View
1,110 program/lib/MDB2/Driver/Datatype/mysql.php
@@ -1,553 +1,559 @@
-<?php
-// vim: set et ts=4 sw=4 fdm=marker:
-// +----------------------------------------------------------------------+
-// | PHP versions 4 and 5 |
-// +----------------------------------------------------------------------+
-// | Copyright (c) 1998-2008 Manuel Lemos, Tomas V.V.Cox, |
-// | Stig. S. Bakken, Lukas Smith |
-// | All rights reserved. |
-// +----------------------------------------------------------------------+
-// | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB |
-// | API as well as database abstraction for PHP applications. |
-// | This LICENSE is in the BSD license style. |
-// | |
-// | Redistribution and use in source and binary forms, with or without |
-// | modification, are permitted provided that the following conditions |
-// | are met: |
-// | |
-// | Redistributions of source code must retain the above copyright |
-// | notice, this list of conditions and the following disclaimer. |
-// | |
-// | Redistributions in binary form must reproduce the above copyright |
-// | notice, this list of conditions and the following disclaimer in the |
-// | documentation and/or other materials provided with the distribution. |
-// | |
-// | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken, |
-// | Lukas Smith nor the names of his contributors may be used to endorse |
-// | or promote products derived from this software without specific prior|
-// | written permission. |
-// | |
-// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
-// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
-// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
-// | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
-// | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
-// | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
-// | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
-// | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
-// | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
-// | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
-// | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
-// | POSSIBILITY OF SUCH DAMAGE. |
-// +----------------------------------------------------------------------+
-// | Author: Lukas Smith <smith@pooteeweet.org> |
-// +----------------------------------------------------------------------+
-//
-// $Id: mysql.php,v 1.65 2008/02/22 19:23:49 quipo Exp $
-//
-
-require_once 'MDB2/Driver/Datatype/Common.php';
-
-/**
- * MDB2 MySQL driver
- *
- * @package MDB2
- * @category Database
- * @author Lukas Smith <smith@pooteeweet.org>
- */
-class MDB2_Driver_Datatype_mysql extends MDB2_Driver_Datatype_Common
-{
- // {{{ _getCharsetFieldDeclaration()
-
- /**
- * Obtain DBMS specific SQL code portion needed to set the CHARACTER SET
- * of a field declaration to be used in statements like CREATE TABLE.
- *
- * @param string $charset name of the charset
- * @return string DBMS specific SQL code portion needed to set the CHARACTER SET
- * of a field declaration.
- */
- function _getCharsetFieldDeclaration($charset)
- {
- return 'CHARACTER SET '.$charset;
- }
-
- // }}}
- // {{{ _getCollationFieldDeclaration()
-
- /**
- * Obtain DBMS specific SQL code portion needed to set the COLLATION
- * of a field declaration to be used in statements like CREATE TABLE.
- *
- * @param string $collation name of the collation
- * @return string DBMS specific SQL code portion needed to set the COLLATION
- * of a field declaration.
- */
- function _getCollationFieldDeclaration($collation)
- {
- return 'COLLATE '.$collation;
- }
-
- // }}}
- // {{{ getTypeDeclaration()
-
- /**
- * Obtain DBMS specific SQL code portion needed to declare an text type
- * field to be used in statements like CREATE TABLE.
- *
- * @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 public
- */
- function getTypeDeclaration($field)
- {
- $db =& $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- switch ($field['type']) {
- case 'text':
- if (empty($field['length']) && array_key_exists('default', $field)) {
- $field['length'] = $db->varchar_max_length;
- }
- $length = !empty($field['length']) ? $field['length'] : false;
- $fixed = !empty($field['fixed']) ? $field['fixed'] : false;
- return $fixed ? ($length ? 'CHAR('.$length.')' : 'CHAR(255)')
- : ($length ? 'VARCHAR('.$length.')' : 'TEXT');
- case 'clob':
- if (!empty($field['length'])) {
- $length = $field['length'];
- if ($length <= 255) {
- return 'TINYTEXT';
- } elseif ($length <= 65532) {
- return 'TEXT';
- } elseif ($length <= 16777215) {
- return 'MEDIUMTEXT';
- }
- }
- return 'LONGTEXT';
- case 'blob':
- if (!empty($field['length'])) {
- $length = $field['length'];
- if ($length <= 255) {
- return 'TINYBLOB';
- } elseif ($length <= 65532) {
- return 'BLOB';
- } elseif ($length <= 16777215) {
- return 'MEDIUMBLOB';
- }
- }
- return 'LONGBLOB';
- case 'integer':
- if (!empty($field['length'])) {
- $length = $field['length'];
- if ($length <= 1) {
- return 'TINYINT';
- } elseif ($length == 2) {
- return 'SMALLINT';
- } elseif ($length == 3) {
- return 'MEDIUMINT';
- } elseif ($length == 4) {
- return 'INT';
- } elseif ($length > 4) {
- return 'BIGINT';
- }
- }
- return 'INT';
- case 'boolean':
- return 'TINYINT(1)';
- case 'date':
- return 'DATE';
- case 'time':
- return 'TIME';
- case 'timestamp':
- return 'DATETIME';
- case 'float':
- return 'DOUBLE';
- case 'decimal':
- $length = !empty($field['length']) ? $field['length'] : 18;
- $scale = !empty($field['scale']) ? $field['scale'] : $db->options['decimal_places'];
- return 'DECIMAL('.$length.','.$scale.')';
- }
- return '';
- }
-
- // }}}
- // {{{ _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 = ' AUTO_INCREMENT 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');
- }
-
- $notnull = empty($field['notnull']) ? '' : ' NOT NULL';
- $unsigned = empty($field['unsigned']) ? '' : ' UNSIGNED';
- $name = $db->quoteIdentifier($name, true);
- return $name.' '.$this->getTypeDeclaration($field).$unsigned.$default.$notnull.$autoinc;
- }
-
- // }}}
- // {{{ _getFloatDeclaration()
-
- /**
- * Obtain DBMS specific SQL code portion needed to declare an float type
- * field to be used in statements like CREATE TABLE.
- *
- * @param string $name name the field to be declared.
- * @param string $field associative array with the name of the properties
- * of the field being declared as array indexes.
- * Currently, the types of supported field
- * properties are as follows:
- *
- * unsigned
- * Boolean flag that indicates whether the field
- * should be declared as unsigned float if
- * possible.
- *
- * default
- * float value to be used as default for this
- * field.
- *
- * notnull
- * Boolean flag that indicates whether this field is
- * constrained to not be set to null.
- * @return string DBMS specific SQL code portion that should be used to
- * declare the specified field.
- * @access protected
- */
- function _getFloatDeclaration($name, $field)
- {
- // Since AUTO_INCREMENT can be used for integer or floating-point types,
- // reuse the INTEGER declaration
- // @see http://bugs.mysql.com/bug.php?id=31032
- return $this->_getIntegerDeclaration($name, $field);
- }
-
- // }}}
- // {{{ _getDecimalDeclaration()
-
- /**
- * Obtain DBMS specific SQL code portion needed to declare an decimal type
- * field to be used in statements like CREATE TABLE.
- *
- * @param string $name name the field to be declared.
- * @param string $field associative array with the name of the properties
- * of the field being declared as array indexes.
- * Currently, the types of supported field
- * properties are as follows:
- *
- * unsigned
- * Boolean flag that indicates whether the field
- * should be declared as unsigned integer if
- * possible.
- *
- * default
- * Decimal value to be used as default for this
- * field.
- *
- * notnull
- * Boolean flag that indicates whether this field is
- * constrained to not be set to null.
- * @return string DBMS specific SQL code portion that should be used to
- * declare the specified field.
- * @access protected
- */
- function _getDecimalDeclaration($name, $field)
- {
- $db =& $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- $default = '';
- if (array_key_exists('default', $field)) {
- if ($field['default'] === '') {
- $field['default'] = empty($field['notnull']) ? null : 0;
- }
- $default = ' DEFAULT '.$this->quote($field['default'], 'integer');
- } elseif (empty($field['notnull'])) {
- $default = ' DEFAULT NULL';
- }
-
- $notnull = empty($field['notnull']) ? '' : ' NOT NULL';
- $unsigned = empty($field['unsigned']) ? '' : ' UNSIGNED';
- $name = $db->quoteIdentifier($name, true);
- return $name.' '.$this->getTypeDeclaration($field).$unsigned.$default.$notnull;
- }
-
- // }}}
- // {{{ matchPattern()
-
- /**
- * build a pattern matching string
- *
- * @access public
- *
- * @param array $pattern even keys are strings, odd are patterns (% and _)
- * @param string $operator optional pattern operator (LIKE, ILIKE and maybe others in the future)
- * @param string $field optional field name that is being matched against
- * (might be required when emulating ILIKE)
- *
- * @return string SQL pattern
- */
- function matchPattern($pattern, $operator = null, $field = null)
- {
- $db =& $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- $match = '';
- if (!is_null($operator)) {
- $field = is_null($field) ? '' : $field.' ';
- $operator = strtoupper($operator);
- switch ($operator) {
- // case insensitive
- case 'ILIKE':
- $match = $field.'LIKE ';
- break;
- // case sensitive
- case 'LIKE':
- $match = $field.'LIKE BINARY ';
- break;
- default:
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
- 'not a supported operator type:'. $operator, __FUNCTION__);
- }
- }
- $match.= "'";
- foreach ($pattern as $key => $value) {
- if ($key % 2) {
- $match.= $value;
- } else {
- $match.= $db->escapePattern($db->escape($value));
- }
- }
- $match.= "'";
- $match.= $this->patternEscapeString();
- return $match;
- }
-
- // }}}
- // {{{ _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_type = strtolower($field['type']);
- $db_type = strtok($db_type, '(), ');
- if ($db_type == 'national') {
- $db_type = strtok('(), ');
- }
- if (!empty($field['length'])) {
- $length = strtok($field['length'], ', ');
- $decimal = strtok(', ');
- } else {
- $length = strtok('(), ');
- $decimal = strtok('(), ');
- }
- $type = array();
- $unsigned = $fixed = null;
- switch ($db_type) {
- case 'tinyint':
- $type[] = 'integer';
- $type[] = 'boolean';
- if (preg_match('/^(is|has)/', $field['name'])) {
- $type = array_reverse($type);
- }
- $unsigned = preg_match('/ unsigned/i', $field['type']);
- $length = 1;
- break;
- case 'smallint':
- $type[] = 'integer';
- $unsigned = preg_match('/ unsigned/i', $field['type']);
- $length = 2;
- break;
- case 'mediumint':
- $type[] = 'integer';
- $unsigned = preg_match('/ unsigned/i', $field['type']);
- $length = 3;
- break;
- case 'int':
- case 'integer':
- $type[] = 'integer';
- $unsigned = preg_match('/ unsigned/i', $field['type']);
- $length = 4;
- break;
- case 'bigint':
- $type[] = 'integer';
- $unsigned = preg_match('/ unsigned/i', $field['type']);
- $length = 8;
- break;
- case 'tinytext':
- case 'mediumtext':
- case 'longtext':
- case 'text':
- case 'varchar':
- $fixed = false;
- case 'string':
- case 'char':
- $type[] = 'text';
- if ($length == '1') {
- $type[] = 'boolean';
- if (preg_match('/^(is|has)/', $field['name'])) {
- $type = array_reverse($type);
- }
- } elseif (strstr($db_type, 'text')) {
- $type[] = 'clob';
- if ($decimal == 'binary') {
- $type[] = 'blob';
- }
- $type = array_reverse($type);
- }
- if ($fixed !== false) {
- $fixed = true;
- }
- break;
- case 'enum':
- $type[] = 'text';
- preg_match_all('/\'.+\'/U', $field['type'], $matches);
- $length = 0;
- $fixed = false;
- if (is_array($matches)) {
- foreach ($matches[0] as $value) {
- $length = max($length, strlen($value)-2);
- }
- if ($length == '1' && count($matches[0]) == 2) {
- $type[] = 'boolean';
- if (preg_match('/^(is|has)/', $field['name'])) {
- $type = array_reverse($type);
- }
- }
- }
- $type[] = 'integer';
- case 'set':
- $fixed = false;
- $type[] = 'text';
- $type[] = 'integer';
- break;
- case 'date':
- $type[] = 'date';
- $length = null;
- break;
- case 'datetime':
- case 'timestamp':
- $type[] = 'timestamp';
- $length = null;
- break;
- case 'time':
- $type[] = 'time';
- $length = null;
- break;
- case 'float':
- case 'double':
- case 'real':
- $type[] = 'float';
- $unsigned = preg_match('/ unsigned/i', $field['type']);
- break;
- case 'unknown':
- case 'decimal':
- case 'numeric':
- $type[] = 'decimal';
- $unsigned = preg_match('/ unsigned/i', $field['type']);
- if ($decimal !== false) {
- $length = $length.','.$decimal;
- }
- break;
- case 'tinyblob':
- case 'mediumblob':
- case 'longblob':
- case 'blob':
- $type[] = 'blob';
- $length = null;
- break;
- case 'binary':
- case 'varbinary':
- $type[] = 'blob';
- break;
- case 'year':
- $type[] = 'integer';
- $type[] = 'date';
- $length = null;
- break;
- default:
- $db =& $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
- 'unknown database attribute type: '.$db_type, __FUNCTION__);
- }
-
- if ((int)$length <= 0) {
- $length = null;
- }
-
- return array($type, $length, $unsigned, $fixed);
- }
-
- // }}}
-}
-
+<?php
+// vim: set et ts=4 sw=4 fdm=marker:
+// +----------------------------------------------------------------------+
+// | PHP versions 4 and 5 |
+// +----------------------------------------------------------------------+
+// | Copyright (c) 1998-2008 Manuel Lemos, Tomas V.V.Cox, |
+// | Stig. S. Bakken, Lukas Smith |
+// | All rights reserved. |
+// +----------------------------------------------------------------------+
+// | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB |
+// | API as well as database abstraction for PHP applications. |
+// | This LICENSE is in the BSD license style. |
+// | |
+// | Redistribution and use in source and binary forms, with or without |
+// | modification, are permitted provided that the following conditions |
+// | are met: |
+// | |
+// | Redistributions of source code must retain the above copyright |
+// | notice, this list of conditions and the following disclaimer. |
+// | |
+// | Redistributions in binary form must reproduce the above copyright |
+// | notice, this list of conditions and the following disclaimer in the |
+// | documentation and/or other materials provided with the distribution. |
+// | |
+// | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken, |
+// | Lukas Smith nor the names of his contributors may be used to endorse |
+// | or promote products derived from this software without specific prior|
+// | written permission. |
+// | |
+// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
+// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
+// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
+// | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
+// | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
+// | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
+// | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
+// | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
+// | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
+// | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
+// | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
+// | POSSIBILITY OF SUCH DAMAGE. |
+// +----------------------------------------------------------------------+
+// | Author: Lukas Smith <smith@pooteeweet.org> |
+// +----------------------------------------------------------------------+
+//
+// $Id: mysql.php 292715 2009-12-28 14:06:34Z quipo $
+//
+
+require_once 'MDB2/Driver/Datatype/Common.php';
+
+/**
+ * MDB2 MySQL driver
+ *
+ * @package MDB2
+ * @category Database
+ * @author Lukas Smith <smith@pooteeweet.org>
+ */
+class MDB2_Driver_Datatype_mysql extends MDB2_Driver_Datatype_Common
+{
+ // {{{ _getCharsetFieldDeclaration()
+
+ /**
+ * Obtain DBMS specific SQL code portion needed to set the CHARACTER SET
+ * of a field declaration to be used in statements like CREATE TABLE.
+ *
+ * @param string $charset name of the charset
+ * @return string DBMS specific SQL code portion needed to set the CHARACTER SET
+ * of a field declaration.
+ */
+ function _getCharsetFieldDeclaration($charset)
+ {
+ return 'CHARACTER SET '.$charset;
+ }
+
+ // }}}
+ // {{{ _getCollationFieldDeclaration()
+
+ /**
+ * Obtain DBMS specific SQL code portion needed to set the COLLATION
+ * of a field declaration to be used in statements like CREATE TABLE.
+ *
+ * @param string $collation name of the collation
+ * @return string DBMS specific SQL code portion needed to set the COLLATION
+ * of a field declaration.
+ */
+ function _getCollationFieldDeclaration($collation)
+ {
+ return 'COLLATE '.$collation;
+ }
+
+ // }}}
+ // {{{ getTypeDeclaration()
+
+ /**
+ * Obtain DBMS specific SQL code portion needed to declare an text type
+ * field to be used in statements like CREATE TABLE.
+ *
+ * @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 public
+ */
+ function getTypeDeclaration($field)
+ {
+ $db =& $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ switch ($field['type']) {
+ case 'text':
+ if (empty($field['length']) && array_key_exists('default', $field)) {
+ $field['length'] = $db->varchar_max_length;
+ }
+ $length = !empty($field['length']) ? $field['length'] : false;
+ $fixed = !empty($field['fixed']) ? $field['fixed'] : false;
+ return $fixed ? ($length ? 'CHAR('.$length.')' : 'CHAR(255)')
+ : ($length ? 'VARCHAR('.$length.')' : 'TEXT');
+ case 'clob':
+ if (!empty($field['length'])) {
+ $length = $field['length'];
+ if ($length <= 255) {
+ return 'TINYTEXT';
+ } elseif ($length <= 65532) {
+ return 'TEXT';
+ } elseif ($length <= 16777215) {
+ return 'MEDIUMTEXT';
+ }
+ }
+ return 'LONGTEXT';
+ case 'blob':
+ if (!empty($field['length'])) {
+ $length = $field['length'];
+ if ($length <= 255) {
+ return 'TINYBLOB';
+ } elseif ($length <= 65532) {
+ return 'BLOB';
+ } elseif ($length <= 16777215) {
+ return 'MEDIUMBLOB';
+ }
+ }
+ return 'LONGBLOB';
+ case 'integer':
+ if (!empty($field['length'])) {
+ $length = $field['length'];
+ if ($length <= 1) {
+ return 'TINYINT';
+ } elseif ($length == 2) {
+ return 'SMALLINT';
+ } elseif ($length == 3) {
+ return 'MEDIUMINT';
+ } elseif ($length == 4) {
+ return 'INT';
+ } elseif ($length > 4) {
+ return 'BIGINT';
+ }
+ }
+ return 'INT';
+ case 'boolean':
+ return 'TINYINT(1)';
+ case 'date':
+ return 'DATE';
+ case 'time':
+ return 'TIME';
+ case 'timestamp':
+ return 'DATETIME';
+ case 'float':
+ return 'DOUBLE';
+ case 'decimal':
+ $length = !empty($field['length']) ? $field['length'] : 18;
+ $scale = !empty($field['scale']) ? $field['scale'] : $db->options['decimal_places'];
+ return 'DECIMAL('.$length.','.$scale.')';
+ }
+ return '';
+ }
+
+ // }}}
+ // {{{ _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 = ' AUTO_INCREMENT 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');
+ }
+
+ $notnull = empty($field['notnull']) ? '' : ' NOT NULL';
+ $unsigned = empty($field['unsigned']) ? '' : ' UNSIGNED';
+ $name = $db->quoteIdentifier($name, true);
+ return $name.' '.$this->getTypeDeclaration($field).$unsigned.$default.$notnull.$autoinc;
+ }
+
+ // }}}
+ // {{{ _getFloatDeclaration()
+
+ /**
+ * Obtain DBMS specific SQL code portion needed to declare an float type
+ * field to be used in statements like CREATE TABLE.
+ *
+ * @param string $name name the field to be declared.
+ * @param string $field associative array with the name of the properties
+ * of the field being declared as array indexes.
+ * Currently, the types of supported field
+ * properties are as follows:
+ *
+ * unsigned
+ * Boolean flag that indicates whether the field
+ * should be declared as unsigned float if
+ * possible.
+ *
+ * default
+ * float value to be used as default for this
+ * field.
+ *
+ * notnull
+ * Boolean flag that indicates whether this field is
+ * constrained to not be set to null.
+ * @return string DBMS specific SQL code portion that should be used to
+ * declare the specified field.
+ * @access protected
+ */
+ function _getFloatDeclaration($name, $field)
+ {
+ // Since AUTO_INCREMENT can be used for integer or floating-point types,
+ // reuse the INTEGER declaration
+ // @see http://bugs.mysql.com/bug.php?id=31032
+ return $this->_getIntegerDeclaration($name, $field);
+ }
+
+ // }}}
+ // {{{ _getDecimalDeclaration()
+
+ /**
+ * Obtain DBMS specific SQL code portion needed to declare an decimal type
+ * field to be used in statements like CREATE TABLE.
+ *
+ * @param string $name name the field to be declared.
+ * @param string $field associative array with the name of the properties
+ * of the field being declared as array indexes.
+ * Currently, the types of supported field
+ * properties are as follows:
+ *
+ * unsigned
+ * Boolean flag that indicates whether the field
+ * should be declared as unsigned integer if
+ * possible.
+ *
+ * default
+ * Decimal value to be used as default for this
+ * field.
+ *
+ * notnull
+ * Boolean flag that indicates whether this field is
+ * constrained to not be set to null.
+ * @return string DBMS specific SQL code portion that should be used to
+ * declare the specified field.
+ * @access protected
+ */
+ function _getDecimalDeclaration($name, $field)
+ {
+ $db =& $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ $default = '';
+ if (array_key_exists('default', $field)) {
+ if ($field['default'] === '') {
+ $field['default'] = empty($field['notnull']) ? null : 0;
+ }
+ $default = ' DEFAULT '.$this->quote($field['default'], 'integer');
+ } elseif (empty($field['notnull'])) {
+ $default = ' DEFAULT NULL';
+ }
+
+ $notnull = empty($field['notnull']) ? '' : ' NOT NULL';
+ $unsigned = empty($field['unsigned']) ? '' : ' UNSIGNED';
+ $name = $db->quoteIdentifier($name, true);
+ return $name.' '.$this->getTypeDeclaration($field).$unsigned.$default.$notnull;
+ }
+
+ // }}}
+ // {{{ matchPattern()
+
+ /**
+ * build a pattern matching string
+ *
+ * @access public
+ *
+ * @param array $pattern even keys are strings, odd are patterns (% and _)
+ * @param string $operator optional pattern operator (LIKE, ILIKE and maybe others in the future)
+ * @param string $field optional field name that is being matched against
+ * (might be required when emulating ILIKE)
+ *
+ * @return string SQL pattern
+ */
+ function matchPattern($pattern, $operator = null, $field = null)
+ {
+ $db =& $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ $match = '';
+ if (null !== $operator) {
+ $field = (null === $field) ? '' : $field.' ';
+ $operator = strtoupper($operator);
+ switch ($operator) {
+ // case insensitive
+ case 'ILIKE':
+ $match = $field.'LIKE ';
+ break;
+ case 'NOT ILIKE':
+ $match = $field.'NOT LIKE ';
+ break;
+ // case sensitive
+ case 'LIKE':
+ $match = $field.'LIKE BINARY ';
+ break;
+ case 'NOT LIKE':
+ $match = $field.'NOT LIKE BINARY ';
+ break;
+ default:
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'not a supported operator type:'. $operator, __FUNCTION__);
+ }
+ }
+ $match.= "'";
+ foreach ($pattern as $key => $value) {
+ if ($key % 2) {
+ $match.= $value;
+ } else {
+ $match.= $db->escapePattern($db->escape($value));
+ }
+ }
+ $match.= "'";
+ $match.= $this->patternEscapeString();
+ return $match;
+ }
+
+ // }}}
+ // {{{ _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_type = strtolower($field['type']);
+ $db_type = strtok($db_type, '(), ');
+ if ($db_type == 'national') {
+ $db_type = strtok('(), ');
+ }
+ if (!empty($field['length'])) {
+ $length = strtok($field['length'], ', ');
+ $decimal = strtok(', ');
+ } else {
+ $length = strtok('(), ');
+ $decimal = strtok('(), ');
+ }
+ $type = array();
+ $unsigned = $fixed = null;
+ switch ($db_type) {
+ case 'tinyint':
+ $type[] = 'integer';
+ $type[] = 'boolean';
+ if (preg_match('/^(is|has)/', $field['name'])) {
+ $type = array_reverse($type);
+ }
+ $unsigned = preg_match('/ unsigned/i', $field['type']);
+ $length = 1;
+ break;
+ case 'smallint':
+ $type[] = 'integer';
+ $unsigned = preg_match('/ unsigned/i', $field['type']);
+ $length = 2;
+ break;
+ case 'mediumint':
+ $type[] = 'integer';
+ $unsigned = preg_match('/ unsigned/i', $field['type']);
+ $length = 3;
+ break;
+ case 'int':
+ case 'integer':
+ $type[] = 'integer';
+ $unsigned = preg_match('/ unsigned/i', $field['type']);
+ $length = 4;
+ break;
+ case 'bigint':
+ $type[] = 'integer';
+ $unsigned = preg_match('/ unsigned/i', $field['type']);
+ $length = 8;
+ break;
+ case 'tinytext':
+ case 'mediumtext':
+ case 'longtext':
+ case 'text':
+ case 'varchar':
+ $fixed = false;
+ case 'string':
+ case 'char':
+ $type[] = 'text';
+ if ($length == '1') {
+ $type[] = 'boolean';
+ if (preg_match('/^(is|has)/', $field['name'])) {
+ $type = array_reverse($type);
+ }
+ } elseif (strstr($db_type, 'text')) {
+ $type[] = 'clob';
+ if ($decimal == 'binary') {
+ $type[] = 'blob';
+ }