+ *
+ * @var array
+ * @access public
+ * @see MDB2::connect()
+ * @see MDB2::factory()
+ * @see MDB2::singleton()
+ * @see MDB2_Driver_Common::setOption()
+ */
+ public $options = array(
+ 'ssl' => false,
+ 'field_case' => CASE_LOWER,
+ 'disable_query' => false,
+ 'result_class' => 'MDB2_Result_%s',
+ 'buffered_result_class' => 'MDB2_BufferedResult_%s',
+ 'result_wrap_class' => false,
+ 'result_buffering' => true,
+ 'fetch_class' => 'stdClass',
+ 'persistent' => false,
+ 'debug' => 0,
+ 'debug_handler' => 'MDB2_defaultDebugOutput',
+ 'debug_expanded_output' => false,
+ 'default_text_field_length' => 4096,
+ 'lob_buffer_length' => 8192,
+ 'log_line_break' => "\n",
+ 'idxname_format' => '%s_idx',
+ 'seqname_format' => '%s_seq',
+ 'savepoint_format' => 'MDB2_SAVEPOINT_%s',
+ 'statement_format' => 'MDB2_STATEMENT_%1$s_%2$s',
+ 'seqcol_name' => 'sequence',
+ 'quote_identifier' => false,
+ 'use_transactions' => true,
+ 'decimal_places' => 2,
+ 'portability' => MDB2_PORTABILITY_ALL,
+ 'modules' => array(
+ 'ex' => 'Extended',
+ 'dt' => 'Datatype',
+ 'mg' => 'Manager',
+ 'rv' => 'Reverse',
+ 'na' => 'Native',
+ 'fc' => 'Function',
+ ),
+ 'emulate_prepared' => false,
+ 'datatype_map' => array(),
+ 'datatype_map_callback' => array(),
+ 'nativetype_map_callback' => array(),
+ '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',
+ );
+
+ /**
+ * string array
+ * @var string
+ * @access public
+ */
+ public $string_quoting = array(
+ 'start' => "'",
+ 'end' => "'",
+ 'escape' => false,
+ 'escape_pattern' => false,
+ );
+
+ /**
+ * identifier quoting
+ * @var array
+ * @access public
+ */
+ public $identifier_quoting = array(
+ 'start' => '"',
+ 'end' => '"',
+ 'escape' => '"',
+ );
+
+ /**
+ * sql comments
+ * @var array
+ * @access protected
+ */
+ public $sql_comments = array(
+ array('start' => '--', 'end' => "\n", 'escape' => false),
+ array('start' => '/*', 'end' => '*/', 'escape' => false),
+ );
+
+ /**
+ * comparision wildcards
+ * @var array
+ * @access protected
+ */
+ protected $wildcards = array('%', '_');
+
+ /**
+ * column alias keyword
+ * @var string
+ * @access protected
+ */
+ public $as_keyword = ' AS ';
+
+ /**
+ * warnings
+ * @var array
+ * @access protected
+ */
+ public $warnings = array();
+
+ /**
+ * string with the debugging information
+ * @var string
+ * @access public
+ */
+ public $debug_output = '';
+
+ /**
+ * determine if there is an open transaction
+ * @var bool
+ * @access protected
+ */
+ public $in_transaction = false;
+
+ /**
+ * the smart transaction nesting depth
+ * @var int
+ * @access protected
+ */
+ public $nested_transaction_counter = null;
+
+ /**
+ * the first error that occured inside a nested transaction
+ * @var MDB2_Error|bool
+ * @access protected
+ */
+ protected $has_transaction_error = false;
+
+ /**
+ * result offset used in the next query
+ * @var int
+ * @access public
+ */
+ public $offset = 0;
+
+ /**
+ * result limit used in the next query
+ * @var int
+ * @access public
+ */
+ public $limit = 0;
+
+ /**
+ * Database backend used in PHP (mysql, odbc etc.)
+ * @var string
+ * @access public
+ */
+ public $phptype;
+
+ /**
+ * Database used with regards to SQL syntax etc.
+ * @var string
+ * @access public
+ */
+ public $dbsyntax;
+
+ /**
+ * the last query sent to the driver
+ * @var string
+ * @access public
+ */
+ public $last_query;
+
+ /**
+ * the default fetchmode used
+ * @var int
+ * @access public
+ */
+ public $fetchmode = MDB2_FETCHMODE_ORDERED;
+
+ /**
+ * array of module instances
+ * @var array
+ * @access protected
+ */
+ protected $modules = array();
+
+ /**
+ * determines of the PHP4 destructor emulation has been enabled yet
+ * @var array
+ * @access protected
+ */
+ protected $destructor_registered = true;
+
+ /**
+ * @var PEAR
+ */
+ protected $pear;
+
+ // }}}
+ // {{{ constructor: function __construct()
+
+ /**
+ * Constructor
+ */
+ function __construct()
+ {
+ end($GLOBALS['_MDB2_databases']);
+ $db_index = key($GLOBALS['_MDB2_databases']) + 1;
+ $GLOBALS['_MDB2_databases'][$db_index] = &$this;
+ $this->db_index = $db_index;
+ $this->pear = new PEAR;
+ }
+
+ // }}}
+ // {{{ destructor: function __destruct()
+
+ /**
+ * Destructor
+ */
+ function __destruct()
+ {
+ $this->disconnect(false);
+ }
+
+ // }}}
+ // {{{ function free()
+
+ /**
+ * Free the internal references so that the instance can be destroyed
+ *
+ * @return bool true on success, false if result is invalid
+ *
+ * @access public
+ */
+ function free()
+ {
+ unset($GLOBALS['_MDB2_databases'][$this->db_index]);
+ unset($this->db_index);
+ return MDB2_OK;
+ }
+
+ // }}}
+ // {{{ function __toString()
+
+ /**
+ * String conversation
+ *
+ * @return string representation of the object
+ *
+ * @access public
+ */
+ function __toString()
+ {
+ $info = get_class($this);
+ $info.= ': (phptype = '.$this->phptype.', dbsyntax = '.$this->dbsyntax.')';
+ if ($this->connection) {
+ $info.= ' [connected]';
+ }
+ return $info;
+ }
+
+ // }}}
+ // {{{ function errorInfo($error = null)
+
+ /**
+ * This method is used to collect information about an error
+ *
+ * @param mixed error code or resource
+ *
+ * @return array with MDB2 errorcode, native error code, native message
+ *
+ * @access public
+ */
+ function errorInfo($error = null)
+ {
+ return array($error, null, null);
+ }
+
+ // }}}
+ // {{{ function &raiseError($code = null, $mode = null, $options = null, $userinfo = null)
+
+ /**
+ * This method is used to communicate an error and invoke error
+ * callbacks etc. Basically a wrapper for PEAR::raiseError
+ * without the message string.
+ *
+ * @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
+ *
+ * @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,
+ $dummy1 = null,
+ $dummy2 = false
+ ) {
+ $userinfo = "[Error message: $userinfo]\n";
+ // The error is yet a MDB2 error object
+ if (MDB2::isError($code)) {
+ // because we use the static PEAR::raiseError, our global
+ // handler should be used if it is set
+ if ((null === $mode) && !empty($this->_default_error_mode)) {
+ $mode = $this->_default_error_mode;
+ $options = $this->_default_error_options;
+ }
+ if (null === $userinfo) {
+ $userinfo = $code->getUserinfo();
+ }
+ $code = $code->getCode();
+ } elseif ($code == MDB2_ERROR_NOT_FOUND) {
+ // extension not loaded: don't call $this->errorInfo() or the script
+ // will die
+ } elseif (isset($this->connection)) {
+ if (!empty($this->last_query)) {
+ $userinfo.= "[Last executed query: {$this->last_query}]\n";
+ }
+ $native_errno = $native_msg = null;
+ list($code, $native_errno, $native_msg) = $this->errorInfo($code);
+ if ((null !== $native_errno) && $native_errno !== '') {
+ $userinfo.= "[Native code: $native_errno]\n";
+ }
+ if ((null !== $native_msg) && $native_msg !== '') {
+ $userinfo.= "[Native message: ". strip_tags($native_msg) ."]\n";
+ }
+ if (null !== $method) {
+ $userinfo = $method.': '.$userinfo;
+ }
+ }
+
+ $err = $this->pear->raiseError(null, $code, $mode, $options, $userinfo, 'MDB2_Error', true);
+ if ($err->getMode() !== PEAR_ERROR_RETURN
+ && isset($this->nested_transaction_counter) && !$this->has_transaction_error) {
+ $this->has_transaction_error = $err;
+ }
+ return $err;
+ }
+
+ // }}}
+ // {{{ function resetWarnings()
+
+ /**
+ * reset the warning array
+ *
+ * @return void
+ *
+ * @access public
+ */
+ function resetWarnings()
+ {
+ $this->warnings = array();
+ }
+
+ // }}}
+ // {{{ function getWarnings()
+
+ /**
+ * Get all warnings in reverse order.
+ * This means that the last warning is the first element in the array
+ *
+ * @return array with warnings
+ *
+ * @access public
+ * @see resetWarnings()
+ */
+ function getWarnings()
+ {
+ return array_reverse($this->warnings);
+ }
+
+ // }}}
+ // {{{ function setFetchMode($fetchmode, $object_class = 'stdClass')
+
+ /**
+ * Sets which fetch mode should be used by default on queries
+ * on this connection
+ *
+ * @param int MDB2_FETCHMODE_ORDERED, MDB2_FETCHMODE_ASSOC
+ * or MDB2_FETCHMODE_OBJECT
+ * @param string the class name of the object to be returned
+ * by the fetch methods when the
+ * MDB2_FETCHMODE_OBJECT mode is selected.
+ * If no class is specified by default a cast
+ * to object from the assoc array row will be
+ * done. There is also the possibility to use
+ * and extend the 'MDB2_row' class.
+ *
+ * @return mixed MDB2_OK or MDB2 Error Object
+ *
+ * @access public
+ * @see MDB2_FETCHMODE_ORDERED, MDB2_FETCHMODE_ASSOC, MDB2_FETCHMODE_OBJECT
+ */
+ function setFetchMode($fetchmode, $object_class = 'stdClass')
+ {
+ switch ($fetchmode) {
+ case MDB2_FETCHMODE_OBJECT:
+ $this->options['fetch_class'] = $object_class;
+ case MDB2_FETCHMODE_ORDERED:
+ case MDB2_FETCHMODE_ASSOC:
+ $this->fetchmode = $fetchmode;
+ break;
+ default:
+ return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'invalid fetchmode mode', __FUNCTION__);
+ }
+
+ return MDB2_OK;
+ }
+
+ // }}}
+ // {{{ function setOption($option, $value)
+
+ /**
+ * set the option for the db class
+ *
+ * @param string option name
+ * @param mixed value for the option
+ *
+ * @return mixed MDB2_OK or MDB2 Error Object
+ *
+ * @access public
+ */
+ function setOption($option, $value)
+ {
+ if (array_key_exists($option, $this->options)) {
+ $this->options[$option] = $value;
+ return MDB2_OK;
+ }
+ return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ "unknown option $option", __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ function getOption($option)
+
+ /**
+ * Returns the value of an option
+ *
+ * @param string option name
+ *
+ * @return mixed the option value or error object
+ *
+ * @access public
+ */
+ function getOption($option)
+ {
+ if (array_key_exists($option, $this->options)) {
+ return $this->options[$option];
+ }
+ return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ "unknown option $option", __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ function debug($message, $scope = '', $is_manip = null)
+
+ /**
+ * set a debug message
+ *
+ * @param string message that should be appended to the debug variable
+ * @param string usually the method name that triggered the debug call:
+ * for example 'query', 'prepare', 'execute', 'parameters',
+ * 'beginTransaction', 'commit', 'rollback'
+ * @param array contains context information about the debug() call
+ * common keys are: is_manip, time, result etc.
+ *
+ * @return void
+ *
+ * @access public
+ */
+ function debug($message, $scope = '', $context = array())
+ {
+ if ($this->options['debug'] && $this->options['debug_handler']) {
+ if (!$this->options['debug_expanded_output']) {
+ if (!empty($context['when']) && $context['when'] !== 'pre') {
+ return null;
+ }
+ $context = empty($context['is_manip']) ? false : $context['is_manip'];
+ }
+ return call_user_func_array($this->options['debug_handler'], array(&$this, $scope, $message, $context));
+ }
+ return null;
+ }
+
+ // }}}
+ // {{{ function getDebugOutput()
+
+ /**
+ * output debug info
+ *
+ * @return string content of the debug_output class variable
+ *
+ * @access public
+ */
+ function getDebugOutput()
+ {
+ return $this->debug_output;
+ }
+
+ // }}}
+ // {{{ function escape($text)
+
+ /**
+ * Quotes a string so it can be safely used in a query. It will quote
+ * the text so it can safely be used within a query.
+ *
+ * @param string the input string to quote
+ * @param bool escape wildcards
+ *
+ * @return string quoted string
+ *
+ * @access public
+ */
+ function escape($text, $escape_wildcards = false)
+ {
+ if ($escape_wildcards) {
+ $text = $this->escapePattern($text);
+ }
+
+ $text = str_replace($this->string_quoting['end'], $this->string_quoting['escape'] . $this->string_quoting['end'], $text);
+ return $text;
+ }
+
+ // }}}
+ // {{{ function escapePattern($text)
+
+ /**
+ * Quotes pattern (% and _) characters in a string)
+ *
+ * @param string the input string to quote
+ *
+ * @return string quoted string
+ *
+ * @access public
+ */
+ function escapePattern($text)
+ {
+ if ($this->string_quoting['escape_pattern']) {
+ $text = str_replace($this->string_quoting['escape_pattern'], $this->string_quoting['escape_pattern'] . $this->string_quoting['escape_pattern'], $text);
+ foreach ($this->wildcards as $wildcard) {
+ $text = str_replace($wildcard, $this->string_quoting['escape_pattern'] . $wildcard, $text);
+ }
+ }
+ return $text;
+ }
+
+ // }}}
+ // {{{ function quoteIdentifier($str, $check_option = false)
+
+ /**
+ * Quote a string so it can be safely used as a table or column name
+ *
+ * Delimiting style depends on which database driver is being used.
+ *
+ * NOTE: just because you CAN use delimited identifiers doesn't mean
+ * you SHOULD use them. In general, they end up causing way more
+ * problems than they solve.
+ *
+ * NOTE: if you have table names containing periods, don't use this method
+ * (@see bug #11906)
+ *
+ * Portability is broken by using the following characters inside
+ * delimited identifiers:
+ * + backtick (`) -- due to MySQL
+ * + double quote (") -- due to Oracle
+ * + brackets ([ or ]) -- due to Access
+ *
+ * Delimited identifiers are known to generally work correctly under
+ * the following drivers:
+ * + mssql
+ * + mysql
+ * + mysqli
+ * + oci8
+ * + pgsql
+ * + sqlite
+ *
+ * InterBase doesn't seem to be able to use delimited identifiers
+ * via PHP 4. They work fine under PHP 5.
+ *
+ * @param string identifier name to be quoted
+ * @param bool check the 'quote_identifier' option
+ *
+ * @return string quoted identifier string
+ *
+ * @access public
+ */
+ function quoteIdentifier($str, $check_option = false)
+ {
+ if ($check_option && !$this->options['quote_identifier']) {
+ return $str;
+ }
+ $str = str_replace($this->identifier_quoting['end'], $this->identifier_quoting['escape'] . $this->identifier_quoting['end'], $str);
+ $parts = explode('.', $str);
+ foreach (array_keys($parts) as $k) {
+ $parts[$k] = $this->identifier_quoting['start'] . $parts[$k] . $this->identifier_quoting['end'];
+ }
+ return implode('.', $parts);
+ }
+
+ // }}}
+ // {{{ function getAsKeyword()
+
+ /**
+ * Gets the string to alias column
+ *
+ * @return string to use when aliasing a column
+ */
+ function getAsKeyword()
+ {
+ return $this->as_keyword;
+ }
+
+ // }}}
+ // {{{ function getConnection()
+
+ /**
+ * Returns a native connection
+ *
+ * @return mixed a valid MDB2 connection object,
+ * or a MDB2 error object on error
+ *
+ * @access public
+ */
+ function getConnection()
+ {
+ $result = $this->connect();
+ if (MDB2::isError($result)) {
+ return $result;
+ }
+ return $this->connection;
+ }
+
+ // }}}
+ // {{{ function _fixResultArrayValues(&$row, $mode)
+
+ /**
+ * Do all necessary conversions on result arrays to fix DBMS quirks
+ *
+ * @param array the array to be fixed (passed by reference)
+ * @param array bit-wise addition of the required portability modes
+ *
+ * @return void
+ *
+ * @access protected
+ */
+ function _fixResultArrayValues(&$row, $mode)
+ {
+ switch ($mode) {
+ case MDB2_PORTABILITY_EMPTY_TO_NULL:
+ foreach ($row as $key => $value) {
+ if ($value === '') {
+ $row[$key] = null;
+ }
+ }
+ break;
+ case MDB2_PORTABILITY_RTRIM:
+ foreach ($row as $key => $value) {
+ if (is_string($value)) {
+ $row[$key] = rtrim($value);
+ }
+ }
+ break;
+ case MDB2_PORTABILITY_FIX_ASSOC_FIELD_NAMES:
+ $tmp_row = array();
+ foreach ($row as $key => $value) {
+ $tmp_row[preg_replace('/^(?:.*\.)?([^.]+)$/', '\\1', $key)] = $value;
+ }
+ $row = $tmp_row;
+ break;
+ case (MDB2_PORTABILITY_RTRIM + MDB2_PORTABILITY_EMPTY_TO_NULL):
+ foreach ($row as $key => $value) {
+ if ($value === '') {
+ $row[$key] = null;
+ } elseif (is_string($value)) {
+ $row[$key] = rtrim($value);
+ }
+ }
+ break;
+ case (MDB2_PORTABILITY_RTRIM + MDB2_PORTABILITY_FIX_ASSOC_FIELD_NAMES):
+ $tmp_row = array();
+ foreach ($row as $key => $value) {
+ if (is_string($value)) {
+ $value = rtrim($value);
+ }
+ $tmp_row[preg_replace('/^(?:.*\.)?([^.]+)$/', '\\1', $key)] = $value;
+ }
+ $row = $tmp_row;
+ break;
+ case (MDB2_PORTABILITY_EMPTY_TO_NULL + MDB2_PORTABILITY_FIX_ASSOC_FIELD_NAMES):
+ $tmp_row = array();
+ foreach ($row as $key => $value) {
+ if ($value === '') {
+ $value = null;
+ }
+ $tmp_row[preg_replace('/^(?:.*\.)?([^.]+)$/', '\\1', $key)] = $value;
+ }
+ $row = $tmp_row;
+ break;
+ case (MDB2_PORTABILITY_RTRIM + MDB2_PORTABILITY_EMPTY_TO_NULL + MDB2_PORTABILITY_FIX_ASSOC_FIELD_NAMES):
+ $tmp_row = array();
+ foreach ($row as $key => $value) {
+ if ($value === '') {
+ $value = null;
+ } elseif (is_string($value)) {
+ $value = rtrim($value);
+ }
+ $tmp_row[preg_replace('/^(?:.*\.)?([^.]+)$/', '\\1', $key)] = $value;
+ }
+ $row = $tmp_row;
+ break;
+ }
+ }
+
+ // }}}
+ // {{{ function loadModule($module, $property = null, $phptype_specific = null)
+
+ /**
+ * loads a module
+ *
+ * @param string name of the module that should be loaded
+ * (only used for error messages)
+ * @param string name of the property into which the class will be loaded
+ * @param bool if the class to load for the module is specific to the
+ * phptype
+ *
+ * @return object on success a reference to the given module is returned
+ * and on failure a PEAR error
+ *
+ * @access public
+ */
+ function loadModule($module, $property = null, $phptype_specific = null)
+ {
+ if (!$property) {
+ $property = strtolower($module);
+ }
+
+ if (!isset($this->{$property})) {
+ $version = $phptype_specific;
+ if ($phptype_specific !== false) {
+ $version = true;
+ $class_name = 'MDB2_Driver_'.$module.'_'.$this->phptype;
+ $file_name = str_replace('_', DIRECTORY_SEPARATOR, $class_name).'.php';
+ }
+ if ($phptype_specific === false
+ || (!MDB2::classExists($class_name) && !MDB2::fileExists($file_name))
+ ) {
+ $version = false;
+ $class_name = 'MDB2_'.$module;
+ $file_name = str_replace('_', DIRECTORY_SEPARATOR, $class_name).'.php';
+ }
+
+ $err = MDB2::loadClass($class_name, $this->getOption('debug'));
+ if (MDB2::isError($err)) {
+ return $err;
+ }
+
+ // load module in a specific version
+ if ($version) {
+ if (method_exists($class_name, 'getClassName')) {
+ $class_name_new = call_user_func(array($class_name, 'getClassName'), $this->db_index);
+ if ($class_name != $class_name_new) {
+ $class_name = $class_name_new;
+ $err = MDB2::loadClass($class_name, $this->getOption('debug'));
+ if (MDB2::isError($err)) {
+ return $err;
+ }
+ }
+ }
+ }
+
+ if (!MDB2::classExists($class_name)) {
+ $err = $this->raiseError(MDB2_ERROR_LOADMODULE, null, null,
+ "unable to load module '$module' into property '$property'", __FUNCTION__);
+ return $err;
+ }
+ $this->{$property} = new $class_name($this->db_index);
+ $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
+ // version changed in between connects
+ $this->loaded_version_modules[] = $property;
+ }
+ }
+
+ return $this->{$property};
+ }
+
+ // }}}
+ // {{{ function __call($method, $params)
+
+ /**
+ * Calls a module method using the __call magic method
+ *
+ * @param string Method name.
+ * @param array Arguments.
+ *
+ * @return mixed Returned value.
+ */
+ function __call($method, $params)
+ {
+ $module = null;
+ if (preg_match('/^([a-z]+)([A-Z])(.*)$/', $method, $match)
+ && isset($this->options['modules'][$match[1]])
+ ) {
+ $module = $this->options['modules'][$match[1]];
+ $method = strtolower($match[2]).$match[3];
+ if (!isset($this->modules[$module]) || !is_object($this->modules[$module])) {
+ $result = $this->loadModule($module);
+ if (MDB2::isError($result)) {
+ return $result;
+ }
+ }
+ } else {
+ foreach ($this->modules as $key => $foo) {
+ if (is_object($this->modules[$key])
+ && method_exists($this->modules[$key], $method)
+ ) {
+ $module = $key;
+ break;
+ }
+ }
+ }
+ 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);
+ }
+
+ // }}}
+ // {{{ function beginTransaction($savepoint = null)
+
+ /**
+ * Start a transaction or set a savepoint.
+ *
+ * @param string name of a savepoint to set
+ * @return mixed MDB2_OK on success, a MDB2 error on failure
+ *
+ * @access public
+ */
+ function beginTransaction($savepoint = null)
+ {
+ $this->debug('Starting transaction', __FUNCTION__, array('is_manip' => true, 'savepoint' => $savepoint));
+ return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'transactions are not supported', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ function commit($savepoint = null)
+
+ /**
+ * Commit the database changes done during a transaction that is in
+ * progress or release a savepoint. This function may only be called when
+ * auto-committing is disabled, otherwise it will fail. Therefore, a new
+ * transaction is implicitly started after committing the pending changes.
+ *
+ * @param string name of a savepoint to release
+ * @return mixed MDB2_OK on success, a MDB2 error on failure
+ *
+ * @access public
+ */
+ function commit($savepoint = null)
+ {
+ $this->debug('Committing transaction/savepoint', __FUNCTION__, array('is_manip' => true, 'savepoint' => $savepoint));
+ return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'commiting transactions is not supported', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ function rollback($savepoint = null)
+
+ /**
+ * Cancel any database changes done during a transaction or since a specific
+ * savepoint that is in progress. This function may only be called when
+ * auto-committing is disabled, otherwise it will fail. Therefore, a new
+ * transaction is implicitly started after canceling the pending changes.
+ *
+ * @param string name of a savepoint to rollback to
+ * @return mixed MDB2_OK on success, a MDB2 error on failure
+ *
+ * @access public
+ */
+ function rollback($savepoint = null)
+ {
+ $this->debug('Rolling back transaction/savepoint', __FUNCTION__, array('is_manip' => true, 'savepoint' => $savepoint));
+ return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'rolling back transactions is not supported', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ function inTransaction($ignore_nested = false)
+
+ /**
+ * If a transaction is currently open.
+ *
+ * @param bool if the nested transaction count should be ignored
+ * @return int|bool - an integer with the nesting depth is returned if a
+ * nested transaction is open
+ * - true is returned for a normal open transaction
+ * - false is returned if no transaction is open
+ *
+ * @access public
+ */
+ function inTransaction($ignore_nested = false)
+ {
+ if (!$ignore_nested && isset($this->nested_transaction_counter)) {
+ return $this->nested_transaction_counter;
+ }
+ return $this->in_transaction;
+ }
+
+ // }}}
+ // {{{ function setTransactionIsolation($isolation)
+
+ /**
+ * Set the transacton isolation level.
+ *
+ * @param string standard isolation level
+ * READ UNCOMMITTED (allows dirty reads)
+ * READ COMMITTED (prevents dirty reads)
+ * REPEATABLE READ (prevents nonrepeatable reads)
+ * SERIALIZABLE (prevents phantom reads)
+ * @param array some transaction options:
+ * 'wait' => 'WAIT' | 'NO WAIT'
+ * 'rw' => 'READ WRITE' | 'READ ONLY'
+ * @return mixed MDB2_OK on success, a MDB2 error on failure
+ *
+ * @access public
+ * @since 2.1.1
+ */
+ function setTransactionIsolation($isolation, $options = array())
+ {
+ $this->debug('Setting transaction isolation level', __FUNCTION__, array('is_manip' => true));
+ return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'isolation level setting is not supported', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ function beginNestedTransaction($savepoint = false)
+
+ /**
+ * Start a nested transaction.
+ *
+ * @return mixed MDB2_OK on success/savepoint name, a MDB2 error on failure
+ *
+ * @access public
+ * @since 2.1.1
+ */
+ function beginNestedTransaction()
+ {
+ if ($this->in_transaction) {
+ ++$this->nested_transaction_counter;
+ $savepoint = sprintf($this->options['savepoint_format'], $this->nested_transaction_counter);
+ if ($this->supports('savepoints') && $savepoint) {
+ return $this->beginTransaction($savepoint);
+ }
+ return MDB2_OK;
+ }
+ $this->has_transaction_error = false;
+ $result = $this->beginTransaction();
+ $this->nested_transaction_counter = 1;
+ return $result;
+ }
+
+ // }}}
+ // {{{ function completeNestedTransaction($force_rollback = false, $release = false)
+
+ /**
+ * Finish a nested transaction by rolling back if an error occured or
+ * committing otherwise.
+ *
+ * @param bool if the transaction should be rolled back regardless
+ * even if no error was set within the nested transaction
+ * @return mixed MDB_OK on commit/counter decrementing, false on rollback
+ * and a MDB2 error on failure
+ *
+ * @access public
+ * @since 2.1.1
+ */
+ function completeNestedTransaction($force_rollback = false)
+ {
+ if ($this->nested_transaction_counter > 1) {
+ $savepoint = sprintf($this->options['savepoint_format'], $this->nested_transaction_counter);
+ if ($this->supports('savepoints') && $savepoint) {
+ if ($force_rollback || $this->has_transaction_error) {
+ $result = $this->rollback($savepoint);
+ if (!MDB2::isError($result)) {
+ $result = false;
+ $this->has_transaction_error = false;
+ }
+ } else {
+ $result = $this->commit($savepoint);
+ }
+ } else {
+ $result = MDB2_OK;
+ }
+ --$this->nested_transaction_counter;
+ return $result;
+ }
+
+ $this->nested_transaction_counter = null;
+ $result = MDB2_OK;
+
+ // transaction has not yet been rolled back
+ if ($this->in_transaction) {
+ if ($force_rollback || $this->has_transaction_error) {
+ $result = $this->rollback();
+ if (!MDB2::isError($result)) {
+ $result = false;
+ }
+ } else {
+ $result = $this->commit();
+ }
+ }
+ $this->has_transaction_error = false;
+ return $result;
+ }
+
+ // }}}
+ // {{{ function failNestedTransaction($error = null, $immediately = false)
+
+ /**
+ * Force setting nested transaction to failed.
+ *
+ * @param mixed value to return in getNestededTransactionError()
+ * @param bool if the transaction should be rolled back immediately
+ * @return bool MDB2_OK
+ *
+ * @access public
+ * @since 2.1.1
+ */
+ function failNestedTransaction($error = null, $immediately = false)
+ {
+ if (null !== $error) {
+ $error = $this->has_transaction_error ? $this->has_transaction_error : true;
+ } elseif (!$error) {
+ $error = true;
+ }
+ $this->has_transaction_error = $error;
+ if (!$immediately) {
+ return MDB2_OK;
+ }
+ return $this->rollback();
+ }
+
+ // }}}
+ // {{{ function getNestedTransactionError()
+
+ /**
+ * The first error that occured since the transaction start.
+ *
+ * @return MDB2_Error|bool MDB2 error object if an error occured or false.
+ *
+ * @access public
+ * @since 2.1.1
+ */
+ function getNestedTransactionError()
+ {
+ return $this->has_transaction_error;
+ }
+
+ // }}}
+ // {{{ connect()
+
+ /**
+ * Connect to the database
+ *
+ * @return true on success, MDB2 Error Object on failure
+ */
+ function connect()
+ {
+ return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ databaseExists()
+
+ /**
+ * check if given database name is exists?
+ *
+ * @param string $name name of the database that should be checked
+ *
+ * @return mixed true/false on success, a MDB2 error on failure
+ * @access public
+ */
+ function databaseExists($name)
+ {
+ return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ setCharset($charset, $connection = null)
+
+ /**
+ * Set the charset on the current connection
+ *
+ * @param string charset
+ * @param resource connection handle
+ *
+ * @return true on success, MDB2 Error Object on failure
+ */
+ function setCharset($charset, $connection = null)
+ {
+ return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ function disconnect($force = true)
+
+ /**
+ * Log out and disconnect from the database.
+ *
+ * @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
+ *
+ * @access public
+ */
+ function disconnect($force = true)
+ {
+ $this->connection = 0;
+ $this->connected_dsn = array();
+ $this->connected_database_name = '';
+ $this->opened_persistent = null;
+ $this->connected_server_info = '';
+ $this->in_transaction = null;
+ $this->nested_transaction_counter = null;
+ return MDB2_OK;
+ }
+
+ // }}}
+ // {{{ function setDatabase($name)
+
+ /**
+ * Select a different database
+ *
+ * @param string name of the database that should be selected
+ *
+ * @return string name of the database previously connected to
+ *
+ * @access public
+ */
+ function setDatabase($name)
+ {
+ $previous_database_name = (isset($this->database_name)) ? $this->database_name : '';
+ $this->database_name = $name;
+ if (!empty($this->connected_database_name) && ($this->connected_database_name != $this->database_name)) {
+ $this->disconnect(false);
+ }
+ return $previous_database_name;
+ }
+
+ // }}}
+ // {{{ function getDatabase()
+
+ /**
+ * Get the current database
+ *
+ * @return string name of the database
+ *
+ * @access public
+ */
+ function getDatabase()
+ {
+ return $this->database_name;
+ }
+
+ // }}}
+ // {{{ function setDSN($dsn)
+
+ /**
+ * set the DSN
+ *
+ * @param mixed DSN string or array
+ *
+ * @return MDB2_OK
+ *
+ * @access public
+ */
+ function setDSN($dsn)
+ {
+ $dsn_default = $GLOBALS['_MDB2_dsninfo_default'];
+ $dsn = MDB2::parseDSN($dsn);
+ if (array_key_exists('database', $dsn)) {
+ $this->database_name = $dsn['database'];
+ unset($dsn['database']);
+ }
+ $this->dsn = array_merge($dsn_default, $dsn);
+ return $this->disconnect(false);
+ }
+
+ // }}}
+ // {{{ function getDSN($type = 'string', $hidepw = false)
+
+ /**
+ * return the DSN as a string
+ *
+ * @param string format to return ("array", "string")
+ * @param string string to hide the password with
+ *
+ * @return mixed DSN in the chosen type
+ *
+ * @access public
+ */
+ function getDSN($type = 'string', $hidepw = false)
+ {
+ $dsn = array_merge($GLOBALS['_MDB2_dsninfo_default'], $this->dsn);
+ $dsn['phptype'] = $this->phptype;
+ $dsn['database'] = $this->database_name;
+ if ($hidepw) {
+ $dsn['password'] = $hidepw;
+ }
+ switch ($type) {
+ // expand to include all possible options
+ case 'string':
+ $dsn = $dsn['phptype'].
+ ($dsn['dbsyntax'] ? ('('.$dsn['dbsyntax'].')') : '').
+ '://'.$dsn['username'].':'.
+ $dsn['password'].'@'.$dsn['hostspec'].
+ ($dsn['port'] ? (':'.$dsn['port']) : '').
+ '/'.$dsn['database'];
+ break;
+ case 'array':
+ default:
+ break;
+ }
+ return $dsn;
+ }
+
+ // }}}
+ // {{{ _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)
+
+ /**
+ * execute a query as database administrator
+ *
+ * @param string the SQL query
+ * @param mixed array that contains the types of the columns in
+ * the result set
+ * @param bool if the query is a manipulation query
+ *
+ * @return mixed MDB2_OK on success, a MDB2 error on failure
+ *
+ * @access public
+ */
+ function standaloneQuery($query, $types = null, $is_manip = false)
+ {
+ $offset = $this->offset;
+ $limit = $this->limit;
+ $this->offset = $this->limit = 0;
+ $query = $this->_modifyQuery($query, $is_manip, $limit, $offset);
+
+ $connection = $this->getConnection();
+ if (MDB2::isError($connection)) {
+ return $connection;
+ }
+
+ $result = $this->_doQuery($query, $is_manip, $connection, false);
+ if (MDB2::isError($result)) {
+ return $result;
+ }
+
+ if ($is_manip) {
+ $affected_rows = $this->_affectedRows($connection, $result);
+ return $affected_rows;
+ }
+ $result = $this->_wrapResult($result, $types, true, true, $limit, $offset);
+ return $result;
+ }
+
+ // }}}
+ // {{{ function _modifyQuery($query, $is_manip, $limit, $offset)
+
+ /**
+ * Changes a query string for various DBMS specific reasons
+ *
+ * @param string query to modify
+ * @param bool if it is a DML query
+ * @param int limit the number of rows
+ * @param int start reading from given offset
+ *
+ * @return string modified query
+ *
+ * @access protected
+ */
+ function _modifyQuery($query, $is_manip, $limit, $offset)
+ {
+ return $query;
+ }
+
+ // }}}
+ // {{{ function &_doQuery($query, $is_manip = false, $connection = null, $database_name = null)
+
+ /**
+ * Execute a query
+ * @param string query
+ * @param bool if the query is a manipulation query
+ * @param resource connection handle
+ * @param string database name
+ *
+ * @return result or error object
+ *
+ * @access protected
+ */
+ function _doQuery($query, $is_manip = false, $connection = null, $database_name = null)
+ {
+ $this->last_query = $query;
+ $result = $this->debug($query, 'query', array('is_manip' => $is_manip, 'when' => 'pre'));
+ if ($result) {
+ if (MDB2::isError($result)) {
+ return $result;
+ }
+ $query = $result;
+ }
+ $err = MDB2_Driver_Common::raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ return $err;
+ }
+
+ // }}}
+ // {{{ function _affectedRows($connection, $result = null)
+
+ /**
+ * Returns the number of rows affected
+ *
+ * @param resource result handle
+ * @param resource connection handle
+ *
+ * @return mixed MDB2 Error Object or the number of rows affected
+ *
+ * @access private
+ */
+ function _affectedRows($connection, $result = null)
+ {
+ return MDB2_Driver_Common::raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ function &exec($query)
+
+ /**
+ * Execute a manipulation query to the database and return the number of affected rows
+ *
+ * @param string the SQL query
+ *
+ * @return mixed number of affected rows on success, a MDB2 error on failure
+ *
+ * @access public
+ */
+ function exec($query)
+ {
+ $offset = $this->offset;
+ $limit = $this->limit;
+ $this->offset = $this->limit = 0;
+ $query = $this->_modifyQuery($query, true, $limit, $offset);
+
+ $connection = $this->getConnection();
+ if (MDB2::isError($connection)) {
+ return $connection;
+ }
+
+ $result = $this->_doQuery($query, true, $connection, $this->database_name);
+ if (MDB2::isError($result)) {
+ return $result;
+ }
+
+ $affectedRows = $this->_affectedRows($connection, $result);
+ return $affectedRows;
+ }
+
+ // }}}
+ // {{{ function &query($query, $types = null, $result_class = true, $result_wrap_class = false)
+
+ /**
+ * Send a query to the database and return any results
+ *
+ * @param string the SQL query
+ * @param mixed array that contains the types of the columns in
+ * the result set
+ * @param mixed string which specifies which result class to use
+ * @param mixed string which specifies which class to wrap results in
+ *
+ * @return mixed an MDB2_Result handle on success, a MDB2 error on failure
+ *
+ * @access public
+ */
+ function query($query, $types = null, $result_class = true, $result_wrap_class = true)
+ {
+ $offset = $this->offset;
+ $limit = $this->limit;
+ $this->offset = $this->limit = 0;
+ $query = $this->_modifyQuery($query, false, $limit, $offset);
+
+ $connection = $this->getConnection();
+ if (MDB2::isError($connection)) {
+ return $connection;
+ }
+
+ $result = $this->_doQuery($query, false, $connection, $this->database_name);
+ if (MDB2::isError($result)) {
+ return $result;
+ }
+
+ $result = $this->_wrapResult($result, $types, $result_class, $result_wrap_class, $limit, $offset);
+ return $result;
+ }
+
+ // }}}
+ // {{{ 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
+ *
+ * @param resource result handle
+ * @param mixed array that contains the types of the columns in
+ * the result set
+ * @param mixed string which specifies which result class to use
+ * @param mixed string which specifies which class to wrap results in
+ * @param string number of rows to select
+ * @param string first row to select
+ *
+ * @return mixed an MDB2_Result, a MDB2 error on failure
+ *
+ * @access protected
+ */
+ function _wrapResult($result_resource, $types = array(), $result_class = true,
+ $result_wrap_class = true, $limit = null, $offset = null)
+ {
+ if ($types === true) {
+ if ($this->supports('result_introspection')) {
+ $this->loadModule('Reverse', null, true);
+ $tableInfo = $this->reverse->tableInfo($result_resource);
+ if (MDB2::isError($tableInfo)) {
+ return $tableInfo;
+ }
+ $types = array();
+ $types_assoc = array();
+ foreach ($tableInfo as $field) {
+ $types[] = $field['mdb2type'];
+ $types_assoc[$field['name']] = $field['mdb2type'];
+ }
+ } else {
+ $types = null;
+ }
+ }
+
+ if ($result_class === true) {
+ $result_class = $this->options['result_buffering']
+ ? $this->options['buffered_result_class'] : $this->options['result_class'];
+ }
+
+ if ($result_class) {
+ $class_name = sprintf($result_class, $this->phptype);
+ if (!MDB2::classExists($class_name)) {
+ $err = MDB2_Driver_Common::raiseError(MDB2_ERROR_NOT_FOUND, null, null,
+ 'result class does not exist '.$class_name, __FUNCTION__);
+ return $err;
+ }
+ $result = new $class_name($this, $result_resource, $limit, $offset);
+ if (!MDB2::isResultCommon($result)) {
+ $err = MDB2_Driver_Common::raiseError(MDB2_ERROR_NOT_FOUND, null, null,
+ 'result class is not extended from MDB2_Result_Common', __FUNCTION__);
+ return $err;
+ }
+
+ if (!empty($types)) {
+ $err = $result->setResultTypes($types);
+ if (MDB2::isError($err)) {
+ $result->free();
+ return $err;
+ }
+ }
+ if (!empty($types_assoc)) {
+ $err = $result->setResultTypes($types_assoc);
+ if (MDB2::isError($err)) {
+ $result->free();
+ return $err;
+ }
+ }
+
+ if ($result_wrap_class === true) {
+ $result_wrap_class = $this->options['result_wrap_class'];
+ }
+ if ($result_wrap_class) {
+ if (!MDB2::classExists($result_wrap_class)) {
+ $err = MDB2_Driver_Common::raiseError(MDB2_ERROR_NOT_FOUND, null, null,
+ 'result wrap class does not exist '.$result_wrap_class, __FUNCTION__);
+ return $err;
+ }
+ $result = new $result_wrap_class($result, $this->fetchmode);
+ }
+
+ return $result;
+ }
+
+ return $result_resource;
+ }
+
+ // }}}
+ // {{{ function getServerVersion($native = false)
+
+ /**
+ * return version information about the server
+ *
+ * @param bool determines if the raw version string should be returned
+ *
+ * @return mixed array with version information or row string
+ *
+ * @access public
+ */
+ function getServerVersion($native = false)
+ {
+ return MDB2_Driver_Common::raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ function setLimit($limit, $offset = null)
+
+ /**
+ * set the range of the next query
+ *
+ * @param string number of rows to select
+ * @param string first row to select
+ *
+ * @return mixed MDB2_OK on success, a MDB2 error on failure
+ *
+ * @access public
+ */
+ function setLimit($limit, $offset = null)
+ {
+ if (!$this->supports('limit_queries')) {
+ return MDB2_Driver_Common::raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'limit is not supported by this driver', __FUNCTION__);
+ }
+ $limit = (int)$limit;
+ if ($limit < 0) {
+ return MDB2_Driver_Common::raiseError(MDB2_ERROR_SYNTAX, null, null,
+ 'it was not specified a valid selected range row limit', __FUNCTION__);
+ }
+ $this->limit = $limit;
+ if (null !== $offset) {
+ $offset = (int)$offset;
+ if ($offset < 0) {
+ return MDB2_Driver_Common::raiseError(MDB2_ERROR_SYNTAX, null, null,
+ 'it was not specified a valid first selected range row', __FUNCTION__);
+ }
+ $this->offset = $offset;
+ }
+ return MDB2_OK;
+ }
+
+ // }}}
+ // {{{ function subSelect($query, $type = false)
+
+ /**
+ * simple subselect emulation: leaves the query untouched for all RDBMS
+ * that support subselects
+ *
+ * @param string the SQL query for the subselect that may only
+ * return a column
+ * @param string determines type of the field
+ *
+ * @return string the query
+ *
+ * @access public
+ */
+ function subSelect($query, $type = false)
+ {
+ if ($this->supports('sub_selects') === true) {
+ return $query;
+ }
+
+ if (!$this->supports('sub_selects')) {
+ return MDB2_Driver_Common::raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ $col = $this->queryCol($query, $type);
+ if (MDB2::isError($col)) {
+ return $col;
+ }
+ if (!is_array($col) || count($col) == 0) {
+ return 'NULL';
+ }
+ if ($type) {
+ $this->loadModule('Datatype', null, true);
+ return $this->datatype->implodeArray($col, $type);
+ }
+ return implode(', ', $col);
+ }
+
+ // }}}
+ // {{{ function replace($table, $fields)
+
+ /**
+ * Execute a SQL REPLACE query. A REPLACE query is identical to a INSERT
+ * query, except that if there is already a row in the table with the same
+ * key field values, the old row is deleted before the new row is inserted.
+ *
+ * The REPLACE type of query does not make part of the SQL standards. Since
+ * practically only MySQL and SQLite implement it natively, this type of
+ * query isemulated through this method for other DBMS using standard types
+ * of queries inside a transaction to assure the atomicity of the operation.
+ *
+ * @param string name of the table on which the REPLACE query will
+ * be executed.
+ * @param array associative array that describes the fields and the
+ * values that will be inserted or updated in the specified table. The
+ * indexes of the array are the names of all the fields of the table.
+ * The values of the array are also associative arrays that describe
+ * the values and other properties of the table fields.
+ *
+ * Here follows a list of field properties that need to be specified:
+ *
+ * value
+ * Value to be assigned to the specified field. This value may be
+ * of specified in database independent type format as this
+ * function can perform the necessary datatype conversions.
+ *
+ * Default: this property is required unless the Null property is
+ * set to 1.
+ *
+ * type
+ * Name of the type of the field. Currently, all types MDB2
+ * are supported except for clob and blob.
+ *
+ * Default: no type conversion
+ *
+ * null
+ * bool property that indicates that the value for this field
+ * should be set to null.
+ *
+ * The default value for fields missing in INSERT queries may be
+ * specified the definition of a table. Often, the default value
+ * is already null, but since the REPLACE may be emulated using
+ * an UPDATE query, make sure that all fields of the table are
+ * listed in this function argument array.
+ *
+ * Default: 0
+ *
+ * key
+ * bool property that indicates that this field should be
+ * handled as a primary key or at least as part of the compound
+ * unique index of the table that will determine the row that will
+ * updated if it exists or inserted a new row otherwise.
+ *
+ * This function will fail if no key field is specified or if the
+ * value of a key field is set to null because fields that are
+ * part of unique index they may not be null.
+ *
+ * Default: 0
+ *
+ * @return mixed MDB2_OK on success, a MDB2 error on failure
+ *
+ * @access public
+ */
+ function replace($table, $fields)
+ {
+ if (!$this->supports('replace')) {
+ return MDB2_Driver_Common::raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'replace query is not supported', __FUNCTION__);
+ }
+ $count = count($fields);
+ $condition = $values = array();
+ for ($colnum = 0, reset($fields); $colnum < $count; next($fields), $colnum++) {
+ $name = key($fields);
+ if (isset($fields[$name]['null']) && $fields[$name]['null']) {
+ $value = 'NULL';
+ } else {
+ $type = isset($fields[$name]['type']) ? $fields[$name]['type'] : null;
+ $value = $this->quote($fields[$name]['value'], $type);
+ }
+ $values[$name] = $value;
+ if (isset($fields[$name]['key']) && $fields[$name]['key']) {
+ if ($value === 'NULL') {
+ return MDB2_Driver_Common::raiseError(MDB2_ERROR_CANNOT_REPLACE, null, null,
+ 'key value '.$name.' may not be NULL', __FUNCTION__);
+ }
+ $condition[] = $this->quoteIdentifier($name, true) . '=' . $value;
+ }
+ }
+ if (empty($condition)) {
+ return MDB2_Driver_Common::raiseError(MDB2_ERROR_CANNOT_REPLACE, null, null,
+ 'not specified which fields are keys', __FUNCTION__);
+ }
+
+ $result = null;
+ $in_transaction = $this->in_transaction;
+ if (!$in_transaction && MDB2::isError($result = $this->beginTransaction())) {
+ return $result;
+ }
+
+ $connection = $this->getConnection();
+ if (MDB2::isError($connection)) {
+ return $connection;
+ }
+
+ $condition = ' WHERE '.implode(' AND ', $condition);
+ $query = 'DELETE FROM ' . $this->quoteIdentifier($table, true) . $condition;
+ $result = $this->_doQuery($query, true, $connection);
+ if (!MDB2::isError($result)) {
+ $affected_rows = $this->_affectedRows($connection, $result);
+ $insert = '';
+ foreach ($values as $key => $value) {
+ $insert .= ($insert?', ':'') . $this->quoteIdentifier($key, true);
+ }
+ $values = implode(', ', $values);
+ $query = 'INSERT INTO '. $this->quoteIdentifier($table, true) . "($insert) VALUES ($values)";
+ $result = $this->_doQuery($query, true, $connection);
+ if (!MDB2::isError($result)) {
+ $affected_rows += $this->_affectedRows($connection, $result);;
+ }
+ }
+
+ if (!$in_transaction) {
+ if (MDB2::isError($result)) {
+ $this->rollback();
+ } else {
+ $result = $this->commit();
+ }
+ }
+
+ if (MDB2::isError($result)) {
+ return $result;
+ }
+
+ return $affected_rows;
+ }
+
+ // }}}
+ // {{{ function &prepare($query, $types = null, $result_types = null, $lobs = array())
+
+ /**
+ * Prepares a query for multiple execution with execute().
+ * With some database backends, this is emulated.
+ * prepare() requires a generic query as string like
+ * 'INSERT INTO numbers VALUES(?,?)' or
+ * 'INSERT INTO numbers VALUES(:foo,:bar)'.
+ * The ? and :name and are placeholders which can be set using
+ * bindParam() and the query can be sent off using the execute() method.
+ * The allowed format for :name can be set with the 'bindname_format' option.
+ *
+ * @param string the query to prepare
+ * @param mixed array that contains the types of the placeholders
+ * @param mixed array that contains the types of the columns in
+ * the result set or MDB2_PREPARE_RESULT, if set to
+ * MDB2_PREPARE_MANIP the query is handled as a manipulation query
+ * @param mixed key (field) value (parameter) pair for all lob placeholders
+ *
+ * @return mixed resource handle for the prepared query on success,
+ * a MDB2 error on failure
+ *
+ * @access public
+ * @see bindParam, execute
+ */
+ function prepare($query, $types = null, $result_types = null, $lobs = array())
+ {
+ $is_manip = ($result_types === MDB2_PREPARE_MANIP);
+ $offset = $this->offset;
+ $limit = $this->limit;
+ $this->offset = $this->limit = 0;
+ $result = $this->debug($query, __FUNCTION__, array('is_manip' => $is_manip, 'when' => 'pre'));
+ if ($result) {
+ if (MDB2::isError($result)) {
+ return $result;
+ }
+ $query = $result;
+ }
+ $placeholder_type_guess = $placeholder_type = null;
+ $question = '?';
+ $colon = ':';
+ $positions = array();
+ $position = 0;
+ while ($position < strlen($query)) {
+ $q_position = strpos($query, $question, $position);
+ $c_position = strpos($query, $colon, $position);
+ if ($q_position && $c_position) {
+ $p_position = min($q_position, $c_position);
+ } elseif ($q_position) {
+ $p_position = $q_position;
+ } elseif ($c_position) {
+ $p_position = $c_position;
+ } else {
+ break;
+ }
+ if (null === $placeholder_type) {
+ $placeholder_type_guess = $query[$p_position];
+ }
+
+ $new_pos = $this->_skipDelimitedStrings($query, $position, $p_position);
+ if (MDB2::isError($new_pos)) {
+ return $new_pos;
+ }
+ if ($new_pos != $position) {
+ $position = $new_pos;
+ continue; //evaluate again starting from the new position
+ }
+
+ if ($query[$position] == $placeholder_type_guess) {
+ if (null === $placeholder_type) {
+ $placeholder_type = $query[$p_position];
+ $question = $colon = $placeholder_type;
+ if (!empty($types) && is_array($types)) {
+ if ($placeholder_type == ':') {
+ if (is_int(key($types))) {
+ $types_tmp = $types;
+ $types = array();
+ $count = -1;
+ }
+ } else {
+ $types = array_values($types);
+ }
+ }
+ }
+ if ($placeholder_type == ':') {
+ $regexp = '/^.{'.($position+1).'}('.$this->options['bindname_format'].').*$/s';
+ $parameter = preg_replace($regexp, '\\1', $query);
+ if ($parameter === '') {
+ $err = MDB2_Driver_Common::raiseError(MDB2_ERROR_SYNTAX, null, null,
+ 'named parameter name must match "bindname_format" option', __FUNCTION__);
+ return $err;
+ }
+ $positions[$p_position] = $parameter;
+ $query = substr_replace($query, '?', $position, strlen($parameter)+1);
+ // use parameter name in type array
+ if (isset($count) && isset($types_tmp[++$count])) {
+ $types[$parameter] = $types_tmp[$count];
+ }
+ } else {
+ $positions[$p_position] = count($positions);
+ }
+ $position = $p_position + 1;
+ } else {
+ $position = $p_position;
+ }
+ }
+ $class_name = 'MDB2_Statement_'.$this->phptype;
+ $statement = null;
+ $obj = new $class_name($this, $statement, $positions, $query, $types, $result_types, $is_manip, $limit, $offset);
+ $this->debug($query, __FUNCTION__, array('is_manip' => $is_manip, 'when' => 'post', 'result' => $obj));
+ return $obj;
+ }
+
+ // }}}
+ // {{{ function _skipDelimitedStrings($query, $position, $p_position)
+
+ /**
+ * Utility method, used by prepare() to avoid replacing placeholders within delimited strings.
+ * Check if the placeholder is contained within a delimited string.
+ * 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
+ * @param integer $p_position placeholder position
+ *
+ * @return mixed integer $new_position on success
+ * MDB2_Error on failure
+ *
+ * @access protected
+ */
+ function _skipDelimitedStrings($query, $position, $p_position)
+ {
+ $ignores = array();
+ $ignores[] = $this->string_quoting;
+ $ignores[] = $this->identifier_quoting;
+ $ignores = array_merge($ignores, $this->sql_comments);
+
+ foreach ($ignores as $ignore) {
+ if (!empty($ignore['start'])) {
+ if (is_int($start_quote = strpos($query, $ignore['start'], $position)) && $start_quote < $p_position) {
+ $end_quote = $start_quote;
+ do {
+ if (!is_int($end_quote = strpos($query, $ignore['end'], $end_quote + 1))) {
+ if ($ignore['end'] === "\n") {
+ $end_quote = strlen($query) - 1;
+ } else {
+ $err = MDB2_Driver_Common::raiseError(MDB2_ERROR_SYNTAX, null, null,
+ 'query with an unterminated text string specified', __FUNCTION__);
+ return $err;
+ }
+ }
+ } while ($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;
+ }
+ }
+ }
+ return $position;
+ }
+
+ // }}}
+ // {{{ function quote($value, $type = null, $quote = true)
+
+ /**
+ * Convert a text value into a DBMS specific format that is suitable to
+ * compose query statements.
+ *
+ * @param string text string value that is intended to be converted.
+ * @param string type to which the value should be converted to
+ * @param bool quote
+ * @param bool escape wildcards
+ *
+ * @return string text string that represents the given argument value in
+ * a DBMS specific format.
+ *
+ * @access public
+ */
+ function quote($value, $type = null, $quote = true, $escape_wildcards = false)
+ {
+ $result = $this->loadModule('Datatype', null, true);
+ if (MDB2::isError($result)) {
+ return $result;
+ }
+
+ return $this->datatype->quote($value, $type, $quote, $escape_wildcards);
+ }
+
+ // }}}
+ // {{{ function getDeclaration($type, $name, $field)
+
+ /**
+ * Obtain DBMS specific SQL code portion needed to declare
+ * of the given type
+ *
+ * @param string type to which the value should be converted to
+ * @param string name the field to be declared.
+ * @param string definition of the field
+ *
+ * @return string DBMS specific SQL code portion that should be used to
+ * declare the specified field.
+ *
+ * @access public
+ */
+ function getDeclaration($type, $name, $field)
+ {
+ $result = $this->loadModule('Datatype', null, true);
+ if (MDB2::isError($result)) {
+ return $result;
+ }
+ return $this->datatype->getDeclaration($type, $name, $field);
+ }
+
+ // }}}
+ // {{{ function compareDefinition($current, $previous)
+
+ /**
+ * Obtain an array of changes that may need to applied
+ *
+ * @param array new definition
+ * @param array old definition
+ *
+ * @return array containing all changes that will need to be applied
+ *
+ * @access public
+ */
+ function compareDefinition($current, $previous)
+ {
+ $result = $this->loadModule('Datatype', null, true);
+ if (MDB2::isError($result)) {
+ return $result;
+ }
+ return $this->datatype->compareDefinition($current, $previous);
+ }
+
+ // }}}
+ // {{{ function supports($feature)
+
+ /**
+ * Tell whether a DB implementation or its backend extension
+ * supports a given feature.
+ *
+ * @param string name of the feature (see the MDB2 class doc)
+ *
+ * @return bool|string if this DB implementation supports a given feature
+ * false means no, true means native,
+ * 'emulated' means emulated
+ *
+ * @access public
+ */
+ function supports($feature)
+ {
+ if (array_key_exists($feature, $this->supported)) {
+ return $this->supported[$feature];
+ }
+ return MDB2_Driver_Common::raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ "unknown support feature $feature", __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ 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)
+ {
+ return sprintf($this->options['seqname_format'],
+ preg_replace('/[^a-z0-9_\-\$.]/i', '_', $sqn));
+ }
+
+ // }}}
+ // {{{ function getIndexName($idx)
+
+ /**
+ * adds index name formatting to a index name
+ *
+ * @param string name of the index
+ *
+ * @return string formatted index name
+ *
+ * @access public
+ */
+ function getIndexName($idx)
+ {
+ return sprintf($this->options['idxname_format'],
+ preg_replace('/[^a-z0-9_\-\$.]/i', '_', $idx));
+ }
+
+ // }}}
+ // {{{ function nextID($seq_name, $ondemand = true)
+
+ /**
+ * Returns the next free id of a sequence
+ *
+ * @param string name of the sequence
+ * @param bool when true missing sequences are automatic created
+ *
+ * @return mixed MDB2 Error Object or id
+ *
+ * @access public
+ */
+ function nextID($seq_name, $ondemand = true)
+ {
+ return MDB2_Driver_Common::raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ function lastInsertID($table = null, $field = null)
+
+ /**
+ * Returns the autoincrement ID if supported or $id or fetches the current
+ * ID in a sequence called: $table.(empty($field) ? '' : '_'.$field)
+ *
+ * @param string name of the table into which a new row was inserted
+ * @param string name of the field into which a new row was inserted
+ *
+ * @return mixed MDB2 Error Object or id
+ *
+ * @access public
+ */
+ function lastInsertID($table = null, $field = null)
+ {
+ return MDB2_Driver_Common::raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ function currID($seq_name)
+
+ /**
+ * Returns the current id of a sequence
+ *
+ * @param string name of the sequence
+ *
+ * @return mixed MDB2 Error Object or id
+ *
+ * @access public
+ */
+ function currID($seq_name)
+ {
+ $this->warnings[] = 'database does not support getting current
+ sequence value, the sequence value was incremented';
+ return $this->nextID($seq_name);
+ }
+
+ // }}}
+ // {{{ function queryOne($query, $type = null, $colnum = 0)
+
+ /**
+ * Execute the specified query, fetch the value from the first column of
+ * the first row of the result set and then frees
+ * the result set.
+ *
+ * @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
+ *
+ * @access public
+ */
+ function queryOne($query, $type = null, $colnum = 0)
+ {
+ $result = $this->query($query, $type);
+ if (!MDB2::isResultCommon($result)) {
+ return $result;
+ }
+
+ $one = $result->fetchOne($colnum);
+ $result->free();
+ return $one;
+ }
+
+ // }}}
+ // {{{ function queryRow($query, $types = null, $fetchmode = MDB2_FETCHMODE_DEFAULT)
+
+ /**
+ * Execute the specified query, fetch the values from the first
+ * row of the result set into an array and then frees
+ * the result set.
+ *
+ * @param string the SELECT query statement to be executed.
+ * @param array optional array argument that specifies a list of
+ * expected datatypes of the result set columns, so that the eventual
+ * conversions may be performed. The default list of datatypes is
+ * empty, meaning that no conversion is performed.
+ * @param int how the array data should be indexed
+ *
+ * @return mixed MDB2_OK or data array on success, a MDB2 error on failure
+ *
+ * @access public
+ */
+ function queryRow($query, $types = null, $fetchmode = MDB2_FETCHMODE_DEFAULT)
+ {
+ $result = $this->query($query, $types);
+ if (!MDB2::isResultCommon($result)) {
+ return $result;
+ }
+
+ $row = $result->fetchRow($fetchmode);
+ $result->free();
+ return $row;
+ }
+
+ // }}}
+ // {{{ function queryCol($query, $type = null, $colnum = 0)
+
+ /**
+ * 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 $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
+ * @access public
+ */
+ function queryCol($query, $type = null, $colnum = 0)
+ {
+ $result = $this->query($query, $type);
+ if (!MDB2::isResultCommon($result)) {
+ return $result;
+ }
+
+ $col = $result->fetchCol($colnum);
+ $result->free();
+ return $col;
+ }
+
+ // }}}
+ // {{{ function queryAll($query, $types = null, $fetchmode = MDB2_FETCHMODE_DEFAULT, $rekey = false, $force_array = false, $group = false)
+
+ /**
+ * Execute the specified query, fetch all the rows of the result set into
+ * a two dimensional array and then frees the result set.
+ *
+ * @param string the SELECT query statement to be executed.
+ * @param array optional array argument that specifies a list of
+ * expected datatypes of the result set columns, so that the eventual
+ * conversions may be performed. The default list of datatypes is
+ * empty, meaning that no conversion is performed.
+ * @param int how the array data should be indexed
+ * @param bool if set to true, the $all will have the first
+ * column as its first dimension
+ * @param bool used only when the query returns exactly
+ * two columns. If true, the values of the returned array will be
+ * one-element arrays instead of scalars.
+ * @param bool if true, the values of the returned array is
+ * wrapped in another array. If the same key value (in the first
+ * column) repeats itself, the values will be appended to this array
+ * instead of overwriting the existing values.
+ *
+ * @return mixed MDB2_OK or data array on success, a MDB2 error on failure
+ *
+ * @access public
+ */
+ function queryAll($query, $types = null, $fetchmode = MDB2_FETCHMODE_DEFAULT,
+ $rekey = false, $force_array = false, $group = false)
+ {
+ $result = $this->query($query, $types);
+ if (!MDB2::isResultCommon($result)) {
+ return $result;
+ }
+
+ $all = $result->fetchAll($fetchmode, $rekey, $force_array, $group);
+ $result->free();
+ return $all;
+ }
+
+ // }}}
+ // {{{ function delExpect($error_code)
+
+ /**
+ * This method deletes all occurences of the specified element from
+ * the expected error codes stack.
+ *
+ * @param mixed $error_code error code that should be deleted
+ * @return mixed list of error codes that were deleted or error
+ *
+ * @uses PEAR::delExpect()
+ */
+ public function delExpect($error_code)
+ {
+ return $this->pear->delExpect($error_code);
+ }
+
+ // }}}
+ // {{{ function expectError($code)
+
+ /**
+ * This method is used to tell which errors you expect to get.
+ * Expected errors are always returned with error mode
+ * PEAR_ERROR_RETURN. Expected error codes are stored in a stack,
+ * and this method pushes a new element onto it. The list of
+ * expected errors are in effect until they are popped off the
+ * stack with the popExpect() method.
+ *
+ * Note that this method can not be called statically
+ *
+ * @param mixed $code a single error code or an array of error codes to expect
+ *
+ * @return int the new depth of the "expected errors" stack
+ *
+ * @uses PEAR::expectError()
+ */
+ public function expectError($code = '*')
+ {
+ return $this->pear->expectError($code);
+ }
+
+ // }}}
+ // {{{ function getStaticProperty($class, $var)
+
+ /**
+ * If you have a class that's mostly/entirely static, and you need static
+ * properties, you can use this method to simulate them. Eg. in your method(s)
+ * do this: $myVar = &PEAR::getStaticProperty('myclass', 'myVar');
+ * You MUST use a reference, or they will not persist!
+ *
+ * @param string $class The calling classname, to prevent clashes
+ * @param string $var The variable to retrieve.
+ * @return mixed A reference to the variable. If not set it will be
+ * auto initialised to NULL.
+ *
+ * @uses PEAR::getStaticProperty()
+ */
+ public function &getStaticProperty($class, $var)
+ {
+ $tmp = $this->pear->getStaticProperty($class, $var);
+ return $tmp;
+ }
+
+ // }}}
+ // {{{ function loadExtension($ext)
+
+ /**
+ * OS independant PHP extension load. Remember to take care
+ * on the correct extension name for case sensitive OSes.
+ *
+ * @param string $ext The extension name
+ * @return bool Success or not on the dl() call
+ *
+ * @uses PEAR::loadExtension()
+ */
+ public function loadExtension($ext)
+ {
+ return $this->pear->loadExtension($ext);
+ }
+
+ // }}}
+ // {{{ function popErrorHandling()
+
+ /**
+ * Pop the last error handler used
+ *
+ * @return bool Always true
+ *
+ * @see PEAR::pushErrorHandling
+ * @uses PEAR::popErrorHandling()
+ */
+ public function popErrorHandling()
+ {
+ return $this->pear->popErrorHandling();
+ }
+
+ // }}}
+ // {{{ function popExpect()
+
+ /**
+ * This method pops one element off the expected error codes
+ * stack.
+ *
+ * @return array the list of error codes that were popped
+ *
+ * @uses PEAR::popExpect()
+ */
+ public function popExpect()
+ {
+ return $this->pear->popExpect();
+ }
+
+ // }}}
+ // {{{ function pushErrorHandling($mode, $options = null)
+
+ /**
+ * Push a new error handler on top of the error handler options stack. With this
+ * you can easily override the actual error handler for some code and restore
+ * it later with popErrorHandling.
+ *
+ * @param mixed $mode (same as setErrorHandling)
+ * @param mixed $options (same as setErrorHandling)
+ *
+ * @return bool Always true
+ *
+ * @see PEAR::setErrorHandling
+ * @uses PEAR::pushErrorHandling()
+ */
+ public function pushErrorHandling($mode, $options = null)
+ {
+ return $this->pear->pushErrorHandling($mode, $options);
+ }
+
+ // }}}
+ // {{{ function registerShutdownFunc($func, $args = array())
+
+ /**
+ * Use this function to register a shutdown method for static
+ * classes.
+ *
+ * @param mixed $func The function name (or array of class/method) to call
+ * @param mixed $args The arguments to pass to the function
+ * @return void
+ *
+ * @uses PEAR::registerShutdownFunc()
+ */
+ public function registerShutdownFunc($func, $args = array())
+ {
+ return $this->pear->registerShutdownFunc($func, $args);
+ }
+
+ // }}}
+ // {{{ function setErrorHandling($mode = null, $options = null)
+
+ /**
+ * Sets how errors generated by this object should be handled.
+ * Can be invoked both in objects and statically. If called
+ * statically, setErrorHandling sets the default behaviour for all
+ * PEAR objects. If called in an object, setErrorHandling sets
+ * the default behaviour for that object.
+ *
+ * @param int $mode
+ * One of PEAR_ERROR_RETURN, PEAR_ERROR_PRINT,
+ * PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE,
+ * PEAR_ERROR_CALLBACK or PEAR_ERROR_EXCEPTION.
+ *
+ * @param mixed $options
+ * When $mode is PEAR_ERROR_TRIGGER, this is the error level (one
+ * of E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR).
+ *
+ * When $mode is PEAR_ERROR_CALLBACK, this parameter is expected
+ * to be the callback function or method. A callback
+ * function is a string with the name of the function, a
+ * callback method is an array of two elements: the element
+ * at index 0 is the object, and the element at index 1 is
+ * the name of the method to call in the object.
+ *
+ * When $mode is PEAR_ERROR_PRINT or PEAR_ERROR_DIE, this is
+ * a printf format string used when printing the error
+ * message.
+ *
+ * @access public
+ * @return void
+ * @see PEAR_ERROR_RETURN
+ * @see PEAR_ERROR_PRINT
+ * @see PEAR_ERROR_TRIGGER
+ * @see PEAR_ERROR_DIE
+ * @see PEAR_ERROR_CALLBACK
+ * @see PEAR_ERROR_EXCEPTION
+ *
+ * @since PHP 4.0.5
+ * @uses PEAR::setErrorHandling($mode, $options)
+ */
+ public function setErrorHandling($mode = null, $options = null)
+ {
+ return $this->pear->setErrorHandling($mode, $options);
+ }
+
+ /**
+ * @uses PEAR::staticPopErrorHandling()
+ */
+ public function staticPopErrorHandling()
+ {
+ return $this->pear->staticPopErrorHandling();
+ }
+
+ // }}}
+ // {{{ function staticPushErrorHandling($mode, $options = null)
+
+ /**
+ * @uses PEAR::staticPushErrorHandling($mode, $options)
+ */
+ public function staticPushErrorHandling($mode, $options = null)
+ {
+ return $this->pear->staticPushErrorHandling($mode, $options);
+ }
+
+ // }}}
+ // {{{ function &throwError($message = null, $code = null, $userinfo = null)
+
+ /**
+ * Simpler form of raiseError with fewer options. In most cases
+ * message, code and userinfo are enough.
+ *
+ * @param mixed $message a text error message or a PEAR error object
+ *
+ * @param int $code a numeric error code (it is up to your class
+ * to define these if you want to use codes)
+ *
+ * @param string $userinfo If you need to pass along for example debug
+ * information, this parameter is meant for that.
+ *
+ * @return object a PEAR error object
+ * @see PEAR::raiseError
+ * @uses PEAR::&throwError()
+ */
+ public function &throwError($message = null, $code = null, $userinfo = null)
+ {
+ $tmp = $this->pear->throwError($message, $code, $userinfo);
+ return $tmp;
+ }
+
+ // }}}
+}
+
+// }}}
+// {{{ class MDB2_Result
+
+/**
+ * The dummy class that all user space result classes should extend from
+ *
+ * @package MDB2
+ * @category Database
+ * @author Lukas Smith
+ */
+class MDB2_Result
+{
+}
+
+// }}}
+// {{{ class MDB2_Result_Common extends MDB2_Result
+
+/**
+ * The common result class for MDB2 result objects
+ *
+ * @package MDB2
+ * @category Database
+ * @author Lukas Smith
+ */
+class MDB2_Result_Common extends MDB2_Result
+{
+ // {{{ Variables (Properties)
+
+ public $db;
+ public $result;
+ public $rownum = -1;
+ public $types = array();
+ public $types_assoc = array();
+ public $values = array();
+ public $offset;
+ public $offset_count = 0;
+ public $limit;
+ public $column_names;
+
+ // }}}
+ // {{{ constructor: function __construct($db, &$result, $limit = 0, $offset = 0)
+
+ /**
+ * Constructor
+ */
+ function __construct($db, &$result, $limit = 0, $offset = 0)
+ {
+ $this->db = $db;
+ $this->result = $result;
+ $this->offset = $offset;
+ $this->limit = max(0, $limit - 1);
+ }
+
+ // }}}
+ // {{{ function setResultTypes($types)
+
+ /**
+ * Define the list of types to be associated with the columns of a given
+ * result set.
+ *
+ * This function may be called before invoking fetchRow(), fetchOne(),
+ * fetchCol() and fetchAll() so that the necessary data type
+ * conversions are performed on the data to be retrieved by them. If this
+ * function is not called, the type of all result set columns is assumed
+ * to be text, thus leading to not perform any conversions.
+ *
+ * @param array variable that lists the
+ * data types to be expected in the result set columns. If this array
+ * contains less types than the number of columns that are returned
+ * in the result set, the remaining columns are assumed to be of the
+ * type text. Currently, the types clob and blob are not fully
+ * supported.
+ *
+ * @return mixed MDB2_OK on success, a MDB2 error on failure
+ *
+ * @access public
+ */
+ function setResultTypes($types)
+ {
+ $load = $this->db->loadModule('Datatype', null, true);
+ if (MDB2::isError($load)) {
+ return $load;
+ }
+ $types = $this->db->datatype->checkResultTypes($types);
+ if (MDB2::isError($types)) {
+ return $types;
+ }
+ foreach ($types as $key => $value) {
+ if (is_numeric($key)) {
+ $this->types[$key] = $value;
+ } else {
+ $this->types_assoc[$key] = $value;
+ }
+ }
+ return MDB2_OK;
+ }
+
+ // }}}
+ // {{{ function seek($rownum = 0)
+
+ /**
+ * Seek to a specific row in a result set
+ *
+ * @param int number of the row where the data can be found
+ *
+ * @return mixed MDB2_OK on success, a MDB2 error on failure
+ *
+ * @access public
+ */
+ function seek($rownum = 0)
+ {
+ $target_rownum = $rownum - 1;
+ if ($this->rownum > $target_rownum) {
+ return MDB2::raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'seeking to previous rows not implemented', __FUNCTION__);
+ }
+ while ($this->rownum < $target_rownum) {
+ $this->fetchRow();
+ }
+ return MDB2_OK;
+ }
+
+ // }}}
+ // {{{ function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT, $rownum = null)
+
+ /**
+ * Fetch and return a row of data
+ *
+ * @param int how the array data should be indexed
+ * @param int number of the row where the data can be found
+ *
+ * @return int data array on success, a MDB2 error on failure
+ *
+ * @access public
+ */
+ function fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT, $rownum = null)
+ {
+ $err = MDB2::raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ return $err;
+ }
+
+ // }}}
+ // {{{ function fetchOne($colnum = 0)
+
+ /**
+ * fetch single column from the next row from a result set
+ *
+ * @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)
+ {
+ $fetchmode = is_numeric($colnum) ? MDB2_FETCHMODE_ORDERED : MDB2_FETCHMODE_ASSOC;
+ $row = $this->fetchRow($fetchmode, $rownum);
+ if (!is_array($row) || MDB2::isError($row)) {
+ return $row;
+ }
+ if (!array_key_exists($colnum, $row)) {
+ return MDB2::raiseError(MDB2_ERROR_TRUNCATED, null, null,
+ 'column is not defined in the result set: '.$colnum, __FUNCTION__);
+ }
+ return $row[$colnum];
+ }
+
+ // }}}
+ // {{{ function fetchCol($colnum = 0)
+
+ /**
+ * Fetch and return a column from the current row pointer position
+ *
+ * @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)
+ {
+ $column = array();
+ $fetchmode = is_numeric($colnum) ? MDB2_FETCHMODE_ORDERED : MDB2_FETCHMODE_ASSOC;
+ $row = $this->fetchRow($fetchmode);
+ if (is_array($row)) {
+ if (!array_key_exists($colnum, $row)) {
+ return MDB2::raiseError(MDB2_ERROR_TRUNCATED, null, null,
+ 'column is not defined in the result set: '.$colnum, __FUNCTION__);
+ }
+ do {
+ $column[] = $row[$colnum];
+ } while (is_array($row = $this->fetchRow($fetchmode)));
+ }
+ if (MDB2::isError($row)) {
+ return $row;
+ }
+ return $column;
+ }
+
+ // }}}
+ // {{{ function fetchAll($fetchmode = MDB2_FETCHMODE_DEFAULT, $rekey = false, $force_array = false, $group = false)
+
+ /**
+ * Fetch and return all rows from the current row pointer position
+ *
+ * @param int $fetchmode the fetch mode to use:
+ * + MDB2_FETCHMODE_ORDERED
+ * + MDB2_FETCHMODE_ASSOC
+ * + MDB2_FETCHMODE_ORDERED | MDB2_FETCHMODE_FLIPPED
+ * + MDB2_FETCHMODE_ASSOC | MDB2_FETCHMODE_FLIPPED
+ * @param bool if set to true, the $all will have the first
+ * column as its first dimension
+ * @param bool used only when the query returns exactly
+ * two columns. If true, the values of the returned array will be
+ * one-element arrays instead of scalars.
+ * @param bool if true, the values of the returned array is
+ * wrapped in another array. If the same key value (in the first
+ * column) repeats itself, the values will be appended to this array
+ * instead of overwriting the existing values.
+ *
+ * @return mixed data array on success, a MDB2 error on failure
+ *
+ * @access public
+ * @see getAssoc()
+ */
+ function fetchAll($fetchmode = MDB2_FETCHMODE_DEFAULT, $rekey = false,
+ $force_array = false, $group = false)
+ {
+ $all = array();
+ $row = $this->fetchRow($fetchmode);
+ if (MDB2::isError($row)) {
+ return $row;
+ } elseif (!$row) {
+ return $all;
+ }
+
+ $shift_array = $rekey ? false : null;
+ if (null !== $shift_array) {
+ if (is_object($row)) {
+ $colnum = count(get_object_vars($row));
+ } else {
+ $colnum = count($row);
+ }
+ if ($colnum < 2) {
+ return MDB2::raiseError(MDB2_ERROR_TRUNCATED, null, null,
+ 'rekey feature requires atleast 2 column', __FUNCTION__);
+ }
+ $shift_array = (!$force_array && $colnum == 2);
+ }
+
+ if ($rekey) {
+ do {
+ if (is_object($row)) {
+ $arr = get_object_vars($row);
+ $key = reset($arr);
+ unset($row->{$key});
+ } else {
+ if ( $fetchmode == MDB2_FETCHMODE_ASSOC
+ || $fetchmode == MDB2_FETCHMODE_OBJECT
+ ) {
+ $key = reset($row);
+ unset($row[key($row)]);
+ } else {
+ $key = array_shift($row);
+ }
+ if ($shift_array) {
+ $row = array_shift($row);
+ }
+ }
+ if ($group) {
+ $all[$key][] = $row;
+ } else {
+ $all[$key] = $row;
+ }
+ } while (($row = $this->fetchRow($fetchmode)));
+ } elseif ($fetchmode == MDB2_FETCHMODE_FLIPPED) {
+ do {
+ foreach ($row as $key => $val) {
+ $all[$key][] = $val;
+ }
+ } while (($row = $this->fetchRow($fetchmode)));
+ } else {
+ do {
+ $all[] = $row;
+ } while (($row = $this->fetchRow($fetchmode)));
+ }
+
+ return $all;
+ }
+
+ // }}}
+ // {{{ function rowCount()
+ /**
+ * Returns the actual row number that was last fetched (count from 0)
+ * @return int
+ *
+ * @access public
+ */
+ function rowCount()
+ {
+ return $this->rownum + 1;
+ }
+
+ // }}}
+ // {{{ function numRows()
+
+ /**
+ * Returns the number of rows in a result object
+ *
+ * @return mixed MDB2 Error Object or the number of rows
+ *
+ * @access public
+ */
+ function numRows()
+ {
+ return MDB2::raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ function nextResult()
+
+ /**
+ * Move the internal result pointer to the next available result
+ *
+ * @return true on success, false if there is no more result set or an error object on failure
+ *
+ * @access public
+ */
+ function nextResult()
+ {
+ return MDB2::raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ function getColumnNames()
+
+ /**
+ * Retrieve the names of columns returned by the DBMS in a query result or
+ * from the cache.
+ *
+ * @param bool If set to true the values are the column names,
+ * otherwise the names of the columns are the keys.
+ * @return mixed Array variable that holds the names of columns or an
+ * MDB2 error on failure.
+ * Some DBMS may not return any columns when the result set
+ * does not contain any rows.
+ *
+ * @access public
+ */
+ function getColumnNames($flip = false)
+ {
+ if (!isset($this->column_names)) {
+ $result = $this->_getColumnNames();
+ if (MDB2::isError($result)) {
+ return $result;
+ }
+ $this->column_names = $result;
+ }
+ if ($flip) {
+ return array_flip($this->column_names);
+ }
+ return $this->column_names;
+ }
+
+ // }}}
+ // {{{ function _getColumnNames()
+
+ /**
+ * Retrieve the names of columns returned by the DBMS in a query result.
+ *
+ * @return mixed Array variable that holds the names of columns as keys
+ * or an MDB2 error on failure.
+ * Some DBMS may not return any columns when the result set
+ * does not contain any rows.
+ *
+ * @access private
+ */
+ function _getColumnNames()
+ {
+ return MDB2::raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ function numCols()
+
+ /**
+ * Count the number of columns returned by the DBMS in a query result.
+ *
+ * @return mixed integer value with the number of columns, a MDB2 error
+ * on failure
+ *
+ * @access public
+ */
+ function numCols()
+ {
+ return MDB2::raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ function getResource()
+
+ /**
+ * return the resource associated with the result object
+ *
+ * @return resource
+ *
+ * @access public
+ */
+ function getResource()
+ {
+ return $this->result;
+ }
+
+ // }}}
+ // {{{ function bindColumn($column, &$value, $type = null)
+
+ /**
+ * Set bind variable to a column.
+ *
+ * @param int column number or name
+ * @param mixed variable reference
+ * @param string specifies the type of the field
+ *
+ * @return mixed MDB2_OK on success, a MDB2 error on failure
+ *
+ * @access public
+ */
+ function bindColumn($column, &$value, $type = null)
+ {
+ if (!is_numeric($column)) {
+ $column_names = $this->getColumnNames();
+ if ($this->db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
+ if ($this->db->options['field_case'] == CASE_LOWER) {
+ $column = strtolower($column);
+ } else {
+ $column = strtoupper($column);
+ }
+ }
+ $column = $column_names[$column];
+ }
+ $this->values[$column] =& $value;
+ if (null !== $type) {
+ $this->types[$column] = $type;
+ }
+ return MDB2_OK;
+ }
+
+ // }}}
+ // {{{ function _assignBindColumns($row)
+
+ /**
+ * Bind a variable to a value in the result row.
+ *
+ * @param array row data
+ *
+ * @return mixed MDB2_OK on success, a MDB2 error on failure
+ *
+ * @access private
+ */
+ function _assignBindColumns($row)
+ {
+ $row = array_values($row);
+ foreach ($row as $column => $value) {
+ if (array_key_exists($column, $this->values)) {
+ $this->values[$column] = $value;
+ }
+ }
+ return MDB2_OK;
+ }
+
+ // }}}
+ // {{{ function free()
+
+ /**
+ * Free the internal resources associated with result.
+ *
+ * @return bool true on success, false if result is invalid
+ *
+ * @access public
+ */
+ function free()
+ {
+ $this->result = false;
+ return MDB2_OK;
+ }
+
+ // }}}
+}
+
+// }}}
+// {{{ class MDB2_Row
+
+/**
+ * The simple class that accepts row data as an array
+ *
+ * @package MDB2
+ * @category Database
+ * @author Lukas Smith
+ */
+class MDB2_Row
+{
+ // {{{ constructor: function __construct(&$row)
+
+ /**
+ * constructor
+ *
+ * @param resource row data as array
+ */
+ function __construct(&$row)
+ {
+ foreach ($row as $key => $value) {
+ $this->$key = &$row[$key];
+ }
+ }
+
+ // }}}
+}
+
+// }}}
+// {{{ class MDB2_Statement_Common
+
+/**
+ * The common statement class for MDB2 statement objects
+ *
+ * @package MDB2
+ * @category Database
+ * @author Lukas Smith
+ */
+class MDB2_Statement_Common
+{
+ // {{{ Variables (Properties)
+
+ var $db;
+ var $statement;
+ var $query;
+ var $result_types;
+ var $types;
+ var $values = array();
+ var $limit;
+ var $offset;
+ var $is_manip;
+
+ // }}}
+ // {{{ constructor: function __construct($db, $statement, $positions, $query, $types, $result_types, $is_manip = false, $limit = null, $offset = null)
+
+ /**
+ * Constructor
+ */
+ function __construct($db, $statement, $positions, $query, $types, $result_types, $is_manip = false, $limit = null, $offset = null)
+ {
+ $this->db = $db;
+ $this->statement = $statement;
+ $this->positions = $positions;
+ $this->query = $query;
+ $this->types = (array)$types;
+ $this->result_types = (array)$result_types;
+ $this->limit = $limit;
+ $this->is_manip = $is_manip;
+ $this->offset = $offset;
+ }
+
+ // }}}
+ // {{{ function bindValue($parameter, &$value, $type = null)
+
+ /**
+ * Set the value of a parameter of a prepared query.
+ *
+ * @param int the order number of the parameter in the query
+ * statement. The order number of the first parameter is 1.
+ * @param mixed value that is meant to be assigned to specified
+ * parameter. The type of the value depends on the $type argument.
+ * @param string specifies the type of the field
+ *
+ * @return mixed MDB2_OK on success, a MDB2 error on failure
+ *
+ * @access public
+ */
+ function bindValue($parameter, $value, $type = null)
+ {
+ if (!is_numeric($parameter)) {
+ if (strpos($parameter, ':') === 0) {
+ $parameter = substr($parameter, 1);
+ }
+ }
+ if (!in_array($parameter, $this->positions)) {
+ return MDB2::raiseError(MDB2_ERROR_NOT_FOUND, null, null,
+ 'Unable to bind to missing placeholder: '.$parameter, __FUNCTION__);
+ }
+ $this->values[$parameter] = $value;
+ if (null !== $type) {
+ $this->types[$parameter] = $type;
+ }
+ return MDB2_OK;
+ }
+
+ // }}}
+ // {{{ function bindValueArray($values, $types = null)
+
+ /**
+ * Set the values of multiple a parameter of a prepared query in bulk.
+ *
+ * @param array specifies all necessary information
+ * for bindValue() the array elements must use keys corresponding to
+ * the number of the position of the parameter.
+ * @param array specifies the types of the fields
+ *
+ * @return mixed MDB2_OK on success, a MDB2 error on failure
+ *
+ * @access public
+ * @see bindParam()
+ */
+ function bindValueArray($values, $types = null)
+ {
+ $types = is_array($types) ? array_values($types) : array_fill(0, count($values), null);
+ $parameters = array_keys($values);
+ $this->db->pushErrorHandling(PEAR_ERROR_RETURN);
+ $this->db->expectError(MDB2_ERROR_NOT_FOUND);
+ foreach ($parameters as $key => $parameter) {
+ $err = $this->bindValue($parameter, $values[$parameter], $types[$key]);
+ if (MDB2::isError($err)) {
+ if ($err->getCode() == MDB2_ERROR_NOT_FOUND) {
+ //ignore (extra value for missing placeholder)
+ continue;
+ }
+ $this->db->popExpect();
+ $this->db->popErrorHandling();
+ return $err;
+ }
+ }
+ $this->db->popExpect();
+ $this->db->popErrorHandling();
+ return MDB2_OK;
+ }
+
+ // }}}
+ // {{{ function bindParam($parameter, &$value, $type = null)
+
+ /**
+ * Bind a variable to a parameter of a prepared query.
+ *
+ * @param int the order number of the parameter in the query
+ * statement. The order number of the first parameter is 1.
+ * @param mixed variable that is meant to be bound to specified
+ * parameter. The type of the value depends on the $type argument.
+ * @param string specifies the type of the field
+ *
+ * @return mixed MDB2_OK on success, a MDB2 error on failure
+ *
+ * @access public
+ */
+ function bindParam($parameter, &$value, $type = null)
+ {
+ if (!is_numeric($parameter)) {
+ if (strpos($parameter, ':') === 0) {
+ $parameter = substr($parameter, 1);
+ }
+ }
+ if (!in_array($parameter, $this->positions)) {
+ return MDB2::raiseError(MDB2_ERROR_NOT_FOUND, null, null,
+ 'Unable to bind to missing placeholder: '.$parameter, __FUNCTION__);
+ }
+ $this->values[$parameter] =& $value;
+ if (null !== $type) {
+ $this->types[$parameter] = $type;
+ }
+ return MDB2_OK;
+ }
+
+ // }}}
+ // {{{ function bindParamArray(&$values, $types = null)
+
+ /**
+ * Bind the variables of multiple a parameter of a prepared query in bulk.
+ *
+ * @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 array specifies the types of the fields
+ *
+ * @return mixed MDB2_OK on success, a MDB2 error on failure
+ *
+ * @access public
+ * @see bindParam()
+ */
+ function bindParamArray(&$values, $types = null)
+ {
+ $types = is_array($types) ? array_values($types) : array_fill(0, count($values), null);
+ $parameters = array_keys($values);
+ foreach ($parameters as $key => $parameter) {
+ $err = $this->bindParam($parameter, $values[$parameter], $types[$key]);
+ if (MDB2::isError($err)) {
+ return $err;
+ }
+ }
+ return MDB2_OK;
+ }
+
+ // }}}
+ // {{{ function &execute($values = null, $result_class = true, $result_wrap_class = false)
+
+ /**
+ * 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
+ *
+ * @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 MDB2::raiseError(MDB2_ERROR, null, null,
+ 'Prepared statement has already been freed', __FUNCTION__);
+ }
+
+ $values = (array)$values;
+ if (!empty($values)) {
+ $err = $this->bindValueArray($values);
+ if (MDB2::isError($err)) {
+ return MDB2::raiseError(MDB2_ERROR, null, null,
+ 'Binding Values failed with message: ' . $err->getMessage(), __FUNCTION__);
+ }
+ }
+ $result = $this->_execute($result_class, $result_wrap_class);
+ return $result;
+ }
+
+ // }}}
+ // {{{ function _execute($result_class = true, $result_wrap_class = false)
+
+ /**
+ * Execute a prepared query statement helper method.
+ *
+ * @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 private
+ */
+ function _execute($result_class = true, $result_wrap_class = false)
+ {
+ $this->last_query = $this->query;
+ $query = '';
+ $last_position = 0;
+ foreach ($this->positions as $current_position => $parameter) {
+ if (!array_key_exists($parameter, $this->values)) {
+ return MDB2::raiseError(MDB2_ERROR_NOT_FOUND, null, null,
+ 'Unable to bind to missing placeholder: '.$parameter, __FUNCTION__);
+ }
+ $value = $this->values[$parameter];
+ $query.= substr($this->query, $last_position, $current_position - $last_position);
+ if (!isset($value)) {
+ $value_quoted = 'NULL';
+ } else {
+ $type = !empty($this->types[$parameter]) ? $this->types[$parameter] : null;
+ $value_quoted = $this->db->quote($value, $type);
+ if (MDB2::isError($value_quoted)) {
+ return $value_quoted;
+ }
+ }
+ $query.= $value_quoted;
+ $last_position = $current_position + 1;
+ }
+ $query.= substr($this->query, $last_position);
+
+ $this->db->offset = $this->offset;
+ $this->db->limit = $this->limit;
+ if ($this->is_manip) {
+ $result = $this->db->exec($query);
+ } else {
+ $result = $this->db->query($query, $this->result_types, $result_class, $result_wrap_class);
+ }
+ return $result;
+ }
+
+ // }}}
+ // {{{ function free()
+
+ /**
+ * Release resources allocated for the specified prepared query.
+ *
+ * @return mixed MDB2_OK on success, a MDB2 error on failure
+ *
+ * @access public
+ */
+ function free()
+ {
+ if (null === $this->positions) {
+ return MDB2::raiseError(MDB2_ERROR, null, null,
+ 'Prepared statement has already been freed', __FUNCTION__);
+ }
+
+ $this->statement = null;
+ $this->positions = null;
+ $this->query = null;
+ $this->types = null;
+ $this->result_types = null;
+ $this->limit = null;
+ $this->is_manip = null;
+ $this->offset = null;
+ $this->values = null;
+
+ return MDB2_OK;
+ }
+
+ // }}}
+}
+
+// }}}
+// {{{ class MDB2_Module_Common
+
+/**
+ * The common modules class for MDB2 module objects
+ *
+ * @package MDB2
+ * @category Database
+ * @author Lukas Smith
+ */
+class MDB2_Module_Common
+{
+ // {{{ Variables (Properties)
+
+ /**
+ * contains the key to the global MDB2 instance array of the associated
+ * MDB2 instance
+ *
+ * @var int
+ * @access protected
+ */
+ protected $db_index;
+
+ // }}}
+ // {{{ constructor: function __construct($db_index)
+
+ /**
+ * Constructor
+ */
+ function __construct($db_index)
+ {
+ $this->db_index = $db_index;
+ }
+
+ // }}}
+ // {{{ function getDBInstance()
+
+ /**
+ * Get the instance of MDB2 associated with the module instance
+ *
+ * @return object MDB2 instance or a MDB2 error on failure
+ *
+ * @access public
+ */
+ function getDBInstance()
+ {
+ if (isset($GLOBALS['_MDB2_databases'][$this->db_index])) {
+ $result = $GLOBALS['_MDB2_databases'][$this->db_index];
+ } else {
+ $result = MDB2::raiseError(MDB2_ERROR_NOT_FOUND, null, null,
+ 'could not find MDB2 instance');
+ }
+ return $result;
+ }
+
+ // }}}
+}
+
+// }}}
+// {{{ function MDB2_closeOpenTransactions()
+
+/**
+ * Close any open transactions form persistent connections
+ *
+ * @return void
+ *
+ * @access public
+ */
+
+function MDB2_closeOpenTransactions()
+{
+ reset($GLOBALS['_MDB2_databases']);
+ while (next($GLOBALS['_MDB2_databases'])) {
+ $key = key($GLOBALS['_MDB2_databases']);
+ if ($GLOBALS['_MDB2_databases'][$key]->opened_persistent
+ && $GLOBALS['_MDB2_databases'][$key]->in_transaction
+ ) {
+ $GLOBALS['_MDB2_databases'][$key]->rollback();
+ }
+ }
+}
+
+// }}}
+// {{{ function MDB2_defaultDebugOutput(&$db, $scope, $message, $is_manip = null)
+
+/**
+ * default debug output handler
+ *
+ * @param object reference to an MDB2 database object
+ * @param string usually the method name that triggered the debug call:
+ * for example 'query', 'prepare', 'execute', 'parameters',
+ * 'beginTransaction', 'commit', 'rollback'
+ * @param string message that should be appended to the debug variable
+ * @param array contains context information about the debug() call
+ * common keys are: is_manip, time, result etc.
+ *
+ * @return void|string optionally return a modified message, this allows
+ * rewriting a query before being issued or prepared
+ *
+ * @access public
+ */
+function MDB2_defaultDebugOutput(&$db, $scope, $message, $context = array())
+{
+ $db->debug_output.= $scope.'('.$db->db_index.'): ';
+ $db->debug_output.= $message.$db->getOption('log_line_break');
+ return $message;
+}
+
+// }}}
+?>
diff --git a/3rdparty/MDB2/Date.php b/3rdparty/MDB2/Date.php
index d874531150f75a43c3763d380b74ae670a53ea48..ca88eaa347e6cc06a55f9d0fe56a76cc66d6373c 100644
--- a/3rdparty/MDB2/Date.php
+++ b/3rdparty/MDB2/Date.php
@@ -1,183 +1,183 @@
- |
-// +----------------------------------------------------------------------+
-//
-// $Id: Date.php 208329 2006-03-01 12:15:38Z lsmith $
-//
-
-/**
- * @package MDB2
- * @category Database
- * @author Lukas Smith
- */
-
-/**
- * Several methods to convert the MDB2 native timestamp format (ISO based)
- * to and from data structures that are convenient to worth with in side of php.
- * For more complex date arithmetic please take a look at the Date package in PEAR
- *
- * @package MDB2
- * @category Database
- * @author Lukas Smith
- */
-class MDB2_Date
-{
- // {{{ mdbNow()
-
- /**
- * return the current datetime
- *
- * @return string current datetime in the MDB2 format
- * @access public
- */
- function mdbNow()
- {
- return date('Y-m-d H:i:s');
- }
- // }}}
-
- // {{{ mdbToday()
-
- /**
- * return the current date
- *
- * @return string current date in the MDB2 format
- * @access public
- */
- function mdbToday()
- {
- return date('Y-m-d');
- }
- // }}}
-
- // {{{ mdbTime()
-
- /**
- * return the current time
- *
- * @return string current time in the MDB2 format
- * @access public
- */
- function mdbTime()
- {
- return date('H:i:s');
- }
- // }}}
-
- // {{{ date2Mdbstamp()
-
- /**
- * convert a date into a MDB2 timestamp
- *
- * @param int hour of the date
- * @param int minute of the date
- * @param int second of the date
- * @param int month of the date
- * @param int day of the date
- * @param int year of the date
- *
- * @return string a valid MDB2 timestamp
- * @access public
- */
- function date2Mdbstamp($hour = null, $minute = null, $second = null,
- $month = null, $day = null, $year = null)
- {
- return MDB2_Date::unix2Mdbstamp(mktime($hour, $minute, $second, $month, $day, $year, -1));
- }
- // }}}
-
- // {{{ unix2Mdbstamp()
-
- /**
- * convert a unix timestamp into a MDB2 timestamp
- *
- * @param int a valid unix timestamp
- *
- * @return string a valid MDB2 timestamp
- * @access public
- */
- function unix2Mdbstamp($unix_timestamp)
- {
- return date('Y-m-d H:i:s', $unix_timestamp);
- }
- // }}}
-
- // {{{ mdbstamp2Unix()
-
- /**
- * convert a MDB2 timestamp into a unix timestamp
- *
- * @param int a valid MDB2 timestamp
- * @return string unix timestamp with the time stored in the MDB2 format
- *
- * @access public
- */
- function mdbstamp2Unix($mdb_timestamp)
- {
- $arr = MDB2_Date::mdbstamp2Date($mdb_timestamp);
-
- return mktime($arr['hour'], $arr['minute'], $arr['second'], $arr['month'], $arr['day'], $arr['year'], -1);
- }
- // }}}
-
- // {{{ mdbstamp2Date()
-
- /**
- * convert a MDB2 timestamp into an array containing all
- * values necessary to pass to php's date() function
- *
- * @param int a valid MDB2 timestamp
- *
- * @return array with the time split
- * @access public
- */
- function mdbstamp2Date($mdb_timestamp)
- {
- list($arr['year'], $arr['month'], $arr['day'], $arr['hour'], $arr['minute'], $arr['second']) =
- sscanf($mdb_timestamp, "%04u-%02u-%02u %02u:%02u:%02u");
- return $arr;
- }
- // }}}
-}
-
-?>
+ |
+// +----------------------------------------------------------------------+
+//
+// $Id$
+//
+
+/**
+ * @package MDB2
+ * @category Database
+ * @author Lukas Smith
+ */
+
+/**
+ * Several methods to convert the MDB2 native timestamp format (ISO based)
+ * to and from data structures that are convenient to worth with in side of php.
+ * For more complex date arithmetic please take a look at the Date package in PEAR
+ *
+ * @package MDB2
+ * @category Database
+ * @author Lukas Smith
+ */
+class MDB2_Date
+{
+ // {{{ mdbNow()
+
+ /**
+ * return the current datetime
+ *
+ * @return string current datetime in the MDB2 format
+ * @access public
+ */
+ function mdbNow()
+ {
+ return date('Y-m-d H:i:s');
+ }
+ // }}}
+
+ // {{{ mdbToday()
+
+ /**
+ * return the current date
+ *
+ * @return string current date in the MDB2 format
+ * @access public
+ */
+ function mdbToday()
+ {
+ return date('Y-m-d');
+ }
+ // }}}
+
+ // {{{ mdbTime()
+
+ /**
+ * return the current time
+ *
+ * @return string current time in the MDB2 format
+ * @access public
+ */
+ function mdbTime()
+ {
+ return date('H:i:s');
+ }
+ // }}}
+
+ // {{{ date2Mdbstamp()
+
+ /**
+ * convert a date into a MDB2 timestamp
+ *
+ * @param int hour of the date
+ * @param int minute of the date
+ * @param int second of the date
+ * @param int month of the date
+ * @param int day of the date
+ * @param int year of the date
+ *
+ * @return string a valid MDB2 timestamp
+ * @access public
+ */
+ function date2Mdbstamp($hour = null, $minute = null, $second = null,
+ $month = null, $day = null, $year = null)
+ {
+ return MDB2_Date::unix2Mdbstamp(mktime($hour, $minute, $second, $month, $day, $year, -1));
+ }
+ // }}}
+
+ // {{{ unix2Mdbstamp()
+
+ /**
+ * convert a unix timestamp into a MDB2 timestamp
+ *
+ * @param int a valid unix timestamp
+ *
+ * @return string a valid MDB2 timestamp
+ * @access public
+ */
+ function unix2Mdbstamp($unix_timestamp)
+ {
+ return date('Y-m-d H:i:s', $unix_timestamp);
+ }
+ // }}}
+
+ // {{{ mdbstamp2Unix()
+
+ /**
+ * convert a MDB2 timestamp into a unix timestamp
+ *
+ * @param int a valid MDB2 timestamp
+ * @return string unix timestamp with the time stored in the MDB2 format
+ *
+ * @access public
+ */
+ function mdbstamp2Unix($mdb_timestamp)
+ {
+ $arr = MDB2_Date::mdbstamp2Date($mdb_timestamp);
+
+ return mktime($arr['hour'], $arr['minute'], $arr['second'], $arr['month'], $arr['day'], $arr['year'], -1);
+ }
+ // }}}
+
+ // {{{ mdbstamp2Date()
+
+ /**
+ * convert a MDB2 timestamp into an array containing all
+ * values necessary to pass to php's date() function
+ *
+ * @param int a valid MDB2 timestamp
+ *
+ * @return array with the time split
+ * @access public
+ */
+ function mdbstamp2Date($mdb_timestamp)
+ {
+ list($arr['year'], $arr['month'], $arr['day'], $arr['hour'], $arr['minute'], $arr['second']) =
+ sscanf($mdb_timestamp, "%04u-%02u-%02u %02u:%02u:%02u");
+ return $arr;
+ }
+ // }}}
+}
+
+?>
diff --git a/3rdparty/MDB2/Driver/Datatype/Common.php b/3rdparty/MDB2/Driver/Datatype/Common.php
index db0cb45dd892c023b452c2e4088faf4c394ce8d4..3b02c86acdaeb9e0aaacd352a20b76bbb7df6541 100644
--- a/3rdparty/MDB2/Driver/Datatype/Common.php
+++ b/3rdparty/MDB2/Driver/Datatype/Common.php
@@ -1,1841 +1,1842 @@
- |
-// +----------------------------------------------------------------------+
-//
-// $Id: Common.php 300551 2010-06-17 21:54:16Z quipo $
-
-require_once 'MDB2/LOB.php';
-
-/**
- * @package MDB2
- * @category Database
- * @author Lukas Smith
- */
-
-/**
- * MDB2_Driver_Common: Base class that is extended by each MDB2 driver
- *
- * To load this module in the MDB2 object:
- * $mdb->loadModule('Datatype');
- *
- * @package MDB2
- * @category Database
- * @author Lukas Smith
- */
-class MDB2_Driver_Datatype_Common extends MDB2_Module_Common
-{
- var $valid_default_values = array(
- 'text' => '',
- 'boolean' => true,
- 'integer' => 0,
- 'decimal' => 0.0,
- 'float' => 0.0,
- 'timestamp' => '1970-01-01 00:00:00',
- 'time' => '00:00:00',
- 'date' => '1970-01-01',
- 'clob' => '',
- 'blob' => '',
- );
-
- /**
- * contains all LOB objects created with this MDB2 instance
- * @var array
- * @access protected
- */
- var $lobs = array();
-
- // }}}
- // {{{ getValidTypes()
-
- /**
- * Get the list of valid types
- *
- * This function returns an array of valid types as keys with the values
- * being possible default values for all native datatypes and mapped types
- * for custom datatypes.
- *
- * @return mixed array on success, a MDB2 error on failure
- * @access public
- */
- function getValidTypes()
- {
- $types = $this->valid_default_values;
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
- if (!empty($db->options['datatype_map'])) {
- foreach ($db->options['datatype_map'] as $type => $mapped_type) {
- if (array_key_exists($mapped_type, $types)) {
- $types[$type] = $types[$mapped_type];
- } elseif (!empty($db->options['datatype_map_callback'][$type])) {
- $parameter = array('type' => $type, 'mapped_type' => $mapped_type);
- $default = call_user_func_array($db->options['datatype_map_callback'][$type], array(&$db, __FUNCTION__, $parameter));
- $types[$type] = $default;
- }
- }
- }
- return $types;
- }
-
- // }}}
- // {{{ checkResultTypes()
-
- /**
- * Define the list of types to be associated with the columns of a given
- * result set.
- *
- * This function may be called before invoking fetchRow(), fetchOne()
- * fetchCole() and fetchAll() so that the necessary data type
- * conversions are performed on the data to be retrieved by them. If this
- * function is not called, the type of all result set columns is assumed
- * to be text, thus leading to not perform any conversions.
- *
- * @param array $types array variable that lists the
- * data types to be expected in the result set columns. If this array
- * contains less types than the number of columns that are returned
- * in the result set, the remaining columns are assumed to be of the
- * type text. Currently, the types clob and blob are not fully
- * supported.
- * @return mixed MDB2_OK on success, a MDB2 error on failure
- * @access public
- */
- function checkResultTypes($types)
- {
- $types = is_array($types) ? $types : array($types);
- foreach ($types as $key => $type) {
- if (!isset($this->valid_default_values[$type])) {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
- if (empty($db->options['datatype_map'][$type])) {
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
- $type.' for '.$key.' is not a supported column type', __FUNCTION__);
- }
- }
- }
- return $types;
- }
-
- // }}}
- // {{{ _baseConvertResult()
-
- /**
- * General type conversion method
- *
- * @param mixed $value reference to a value to be converted
- * @param string $type specifies which type to convert to
- * @param boolean $rtrim [optional] when TRUE [default], apply rtrim() to text
- * @return object an MDB2 error on failure
- * @access protected
- */
- function _baseConvertResult($value, $type, $rtrim = true)
- {
- switch ($type) {
- case 'text':
- if ($rtrim) {
- $value = rtrim($value);
- }
- return $value;
- case 'integer':
- return intval($value);
- case 'boolean':
- return !empty($value);
- case 'decimal':
- return $value;
- case 'float':
- return doubleval($value);
- case 'date':
- return $value;
- case 'time':
- return $value;
- case 'timestamp':
- return $value;
- case 'clob':
- case 'blob':
- $this->lobs[] = array(
- 'buffer' => null,
- 'position' => 0,
- 'lob_index' => null,
- 'endOfLOB' => false,
- 'resource' => $value,
- 'value' => null,
- 'loaded' => false,
- );
- end($this->lobs);
- $lob_index = key($this->lobs);
- $this->lobs[$lob_index]['lob_index'] = $lob_index;
- return fopen('MDB2LOB://'.$lob_index.'@'.$this->db_index, 'r+');
- }
-
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- return $db->raiseError(MDB2_ERROR_INVALID, null, null,
- 'attempt to convert result value to an unknown type :' . $type, __FUNCTION__);
- }
-
- // }}}
- // {{{ convertResult()
-
- /**
- * Convert a value to a RDBMS indipendent MDB2 type
- *
- * @param mixed $value value to be converted
- * @param string $type specifies which type to convert to
- * @param boolean $rtrim [optional] when TRUE [default], apply rtrim() to text
- * @return mixed converted value
- * @access public
- */
- function convertResult($value, $type, $rtrim = true)
- {
- if (null === $value) {
- return null;
- }
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
- if (!empty($db->options['datatype_map'][$type])) {
- $type = $db->options['datatype_map'][$type];
- if (!empty($db->options['datatype_map_callback'][$type])) {
- $parameter = array('type' => $type, 'value' => $value, 'rtrim' => $rtrim);
- return call_user_func_array($db->options['datatype_map_callback'][$type], array(&$db, __FUNCTION__, $parameter));
- }
- }
- return $this->_baseConvertResult($value, $type, $rtrim);
- }
-
- // }}}
- // {{{ 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
- *
- * @param array $types
- * @param array $row specifies the types to convert to
- * @param bool $rtrim if to rtrim text values or not
- * @return mixed MDB2_OK on success, a MDB2 error on failure
- * @access public
- */
- 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);
- foreach (array_keys($sorted_types) as $k) {
- if (null === $sorted_types[$k]) {
- $sorted_types[$k] = current($types);
- next($types);
- }
- }
- }
- return $sorted_types;
- }
-
- // }}}
- // {{{ getDeclaration()
-
- /**
- * Obtain DBMS specific SQL code portion needed to declare
- * of the given type
- *
- * @param string $type type to which the value should be converted to
- * @param string $name name the field to be declared.
- * @param string $field definition of the field
- * @return string DBMS specific SQL code portion that should be used to
- * declare the specified field.
- * @access public
- */
- function getDeclaration($type, $name, $field)
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- if (!empty($db->options['datatype_map'][$type])) {
- $type = $db->options['datatype_map'][$type];
- if (!empty($db->options['datatype_map_callback'][$type])) {
- $parameter = array('type' => $type, 'name' => $name, 'field' => $field);
- 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")) {
- return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
- 'type not defined: '.$type, __FUNCTION__);
- }
- return $this->{"_get{$type}Declaration"}($name, $field);
- }
-
- // }}}
- // {{{ 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':
- $length = !empty($field['length']) ? $field['length'] : $db->options['default_text_field_length'];
- $fixed = !empty($field['fixed']) ? $field['fixed'] : false;
- return $fixed ? ($length ? 'CHAR('.$length.')' : 'CHAR('.$db->options['default_text_field_length'].')')
- : ($length ? 'VARCHAR('.$length.')' : 'TEXT');
- case 'clob':
- return 'TEXT';
- case 'blob':
- return 'TEXT';
- case 'integer':
- return 'INT';
- case 'boolean':
- return 'INT';
- case 'date':
- return 'CHAR ('.strlen('YYYY-MM-DD').')';
- case 'time':
- return 'CHAR ('.strlen('HH:MM:SS').')';
- case 'timestamp':
- return 'CHAR ('.strlen('YYYY-MM-DD HH:MM:SS').')';
- case 'float':
- return 'TEXT';
- case 'decimal':
- return 'TEXT';
- }
- 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.
- * 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, or a MDB2_Error on failure
- * @access protected
- */
- function _getDeclaration($name, $field)
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($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']);
-
- $notnull = empty($field['notnull']) ? '' : ' NOT NULL';
- $default = '';
- if (array_key_exists('default', $field)) {
- if ($field['default'] === '') {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
- $valid_default_values = $this->getValidTypes();
- $field['default'] = $valid_default_values[$field['type']];
- if ($field['default'] === '' && ($db->options['portability'] & MDB2_PORTABILITY_EMPTY_TO_NULL)) {
- $field['default'] = ' ';
- }
- }
- if (null !== $field['default']) {
- $default = ' DEFAULT ' . $this->quote($field['default'], $field['type']);
- }
- }
- if (empty($default) && empty($notnull)) {
- $default = ' DEFAULT NULL';
- }
-
- $collation = empty($field['collation']) ? '' :
- ' '.$this->_getCollationFieldDeclaration($field['collation']);
-
- return $charset.$default.$notnull.$collation;
- }
-
- // }}}
- // {{{ _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 '';
- }
-
- // }}}
- // {{{ _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 '';
- }
-
- // }}}
- // {{{ _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 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:
- *
- * 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)
- {
- if (!empty($field['unsigned'])) {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- $db->warnings[] = "unsigned integer field \"$name\" is being declared as signed integer";
- }
- return $this->_getDeclaration($name, $field);
- }
-
- // }}}
- // {{{ _getTextDeclaration()
-
- /**
- * Obtain DBMS specific SQL code portion needed to declare an text 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 _getTextDeclaration($name, $field)
- {
- return $this->_getDeclaration($name, $field);
- }
-
- // }}}
- // {{{ _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']) ? '' : ' 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']) ? '' : ' NOT NULL';
- $name = $db->quoteIdentifier($name, true);
- return $name.' '.$this->getTypeDeclaration($field).$notnull;
- }
-
- // }}}
- // {{{ _getBooleanDeclaration()
-
- /**
- * Obtain DBMS specific SQL code portion needed to declare a boolean 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:
- *
- * default
- * Boolean value to be used as default for this field.
- *
- * notnullL
- * 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 _getBooleanDeclaration($name, $field)
- {
- return $this->_getDeclaration($name, $field);
- }
-
- // }}}
- // {{{ _getDateDeclaration()
-
- /**
- * Obtain DBMS specific SQL code portion needed to declare a date 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:
- *
- * default
- * Date 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 _getDateDeclaration($name, $field)
- {
- return $this->_getDeclaration($name, $field);
- }
-
- // }}}
- // {{{ _getTimestampDeclaration()
-
- /**
- * Obtain DBMS specific SQL code portion needed to declare a timestamp
- * 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:
- *
- * default
- * Timestamp 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 _getTimestampDeclaration($name, $field)
- {
- return $this->_getDeclaration($name, $field);
- }
-
- // }}}
- // {{{ _getTimeDeclaration()
-
- /**
- * Obtain DBMS specific SQL code portion needed to declare a time
- * 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:
- *
- * default
- * Time 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 _getTimeDeclaration($name, $field)
- {
- return $this->_getDeclaration($name, $field);
- }
-
- // }}}
- // {{{ _getFloatDeclaration()
-
- /**
- * Obtain DBMS specific SQL code portion needed to declare a float 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:
- *
- * 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)
- {
- return $this->_getDeclaration($name, $field);
- }
-
- // }}}
- // {{{ _getDecimalDeclaration()
-
- /**
- * Obtain DBMS specific SQL code portion needed to declare a decimal 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:
- *
- * 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)
- {
- return $this->_getDeclaration($name, $field);
- }
-
- // }}}
- // {{{ compareDefinition()
-
- /**
- * Obtain an array of changes that may need to applied
- *
- * @param array $current new definition
- * @param array $previous old definition
- * @return array containing all changes that will need to be applied
- * @access public
- */
- function compareDefinition($current, $previous)
- {
- $type = !empty($current['type']) ? $current['type'] : null;
-
- if (!method_exists($this, "_compare{$type}Definition")) {
- $db = $this->getDBInstance();
- if (PEAR::isError($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,
- 'type "'.$current['type'].'" is not yet supported', __FUNCTION__);
- }
-
- if (empty($previous['type']) || $previous['type'] != $type) {
- return $current;
- }
-
- $change = $this->{"_compare{$type}Definition"}($current, $previous);
-
- if ($previous['type'] != $type) {
- $change['type'] = true;
- }
-
- $previous_notnull = !empty($previous['notnull']) ? $previous['notnull'] : false;
- $notnull = !empty($current['notnull']) ? $current['notnull'] : false;
- if ($previous_notnull != $notnull) {
- $change['notnull'] = true;
- }
-
- $previous_default = array_key_exists('default', $previous) ? $previous['default'] :
- ($previous_notnull ? '' : null);
- $default = array_key_exists('default', $current) ? $current['default'] :
- ($notnull ? '' : null);
- if ($previous_default !== $default) {
- $change['default'] = true;
- }
-
- return $change;
- }
-
- // }}}
- // {{{ _compareIntegerDefinition()
-
- /**
- * Obtain an array of changes that may need to applied to an integer field
- *
- * @param array $current new definition
- * @param array $previous old definition
- * @return array containing all changes that will need to be applied
- * @access protected
- */
- function _compareIntegerDefinition($current, $previous)
- {
- $change = array();
- $previous_unsigned = !empty($previous['unsigned']) ? $previous['unsigned'] : false;
- $unsigned = !empty($current['unsigned']) ? $current['unsigned'] : false;
- if ($previous_unsigned != $unsigned) {
- $change['unsigned'] = true;
- }
- $previous_autoincrement = !empty($previous['autoincrement']) ? $previous['autoincrement'] : false;
- $autoincrement = !empty($current['autoincrement']) ? $current['autoincrement'] : false;
- if ($previous_autoincrement != $autoincrement) {
- $change['autoincrement'] = true;
- }
- return $change;
- }
-
- // }}}
- // {{{ _compareTextDefinition()
-
- /**
- * Obtain an array of changes that may need to applied to an text field
- *
- * @param array $current new definition
- * @param array $previous old definition
- * @return array containing all changes that will need to be applied
- * @access protected
- */
- function _compareTextDefinition($current, $previous)
- {
- $change = array();
- $previous_length = !empty($previous['length']) ? $previous['length'] : 0;
- $length = !empty($current['length']) ? $current['length'] : 0;
- if ($previous_length != $length) {
- $change['length'] = true;
- }
- $previous_fixed = !empty($previous['fixed']) ? $previous['fixed'] : 0;
- $fixed = !empty($current['fixed']) ? $current['fixed'] : 0;
- if ($previous_fixed != $fixed) {
- $change['fixed'] = true;
- }
- return $change;
- }
-
- // }}}
- // {{{ _compareCLOBDefinition()
-
- /**
- * Obtain an array of changes that may need to applied to an CLOB field
- *
- * @param array $current new definition
- * @param array $previous old definition
- * @return array containing all changes that will need to be applied
- * @access protected
- */
- function _compareCLOBDefinition($current, $previous)
- {
- return $this->_compareTextDefinition($current, $previous);
- }
-
- // }}}
- // {{{ _compareBLOBDefinition()
-
- /**
- * Obtain an array of changes that may need to applied to an BLOB field
- *
- * @param array $current new definition
- * @param array $previous old definition
- * @return array containing all changes that will need to be applied
- * @access protected
- */
- function _compareBLOBDefinition($current, $previous)
- {
- return $this->_compareTextDefinition($current, $previous);
- }
-
- // }}}
- // {{{ _compareDateDefinition()
-
- /**
- * Obtain an array of changes that may need to applied to an date field
- *
- * @param array $current new definition
- * @param array $previous old definition
- * @return array containing all changes that will need to be applied
- * @access protected
- */
- function _compareDateDefinition($current, $previous)
- {
- return array();
- }
-
- // }}}
- // {{{ _compareTimeDefinition()
-
- /**
- * Obtain an array of changes that may need to applied to an time field
- *
- * @param array $current new definition
- * @param array $previous old definition
- * @return array containing all changes that will need to be applied
- * @access protected
- */
- function _compareTimeDefinition($current, $previous)
- {
- return array();
- }
-
- // }}}
- // {{{ _compareTimestampDefinition()
-
- /**
- * Obtain an array of changes that may need to applied to an timestamp field
- *
- * @param array $current new definition
- * @param array $previous old definition
- * @return array containing all changes that will need to be applied
- * @access protected
- */
- function _compareTimestampDefinition($current, $previous)
- {
- return array();
- }
-
- // }}}
- // {{{ _compareBooleanDefinition()
-
- /**
- * Obtain an array of changes that may need to applied to an boolean field
- *
- * @param array $current new definition
- * @param array $previous old definition
- * @return array containing all changes that will need to be applied
- * @access protected
- */
- function _compareBooleanDefinition($current, $previous)
- {
- return array();
- }
-
- // }}}
- // {{{ _compareFloatDefinition()
-
- /**
- * Obtain an array of changes that may need to applied to an float field
- *
- * @param array $current new definition
- * @param array $previous old definition
- * @return array containing all changes that will need to be applied
- * @access protected
- */
- function _compareFloatDefinition($current, $previous)
- {
- return array();
- }
-
- // }}}
- // {{{ _compareDecimalDefinition()
-
- /**
- * Obtain an array of changes that may need to applied to an decimal field
- *
- * @param array $current new definition
- * @param array $previous old definition
- * @return array containing all changes that will need to be applied
- * @access protected
- */
- function _compareDecimalDefinition($current, $previous)
- {
- return array();
- }
-
- // }}}
- // {{{ quote()
-
- /**
- * Convert a text value into a DBMS specific format that is suitable to
- * compose query statements.
- *
- * @param string $value text string value that is intended to be converted.
- * @param string $type type to which the value should be converted to
- * @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
- * a DBMS specific format.
- * @access public
- */
- function quote($value, $type = null, $quote = true, $escape_wildcards = false)
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- if ((null === $value)
- || ($value === '' && $db->options['portability'] & MDB2_PORTABILITY_EMPTY_TO_NULL)
- ) {
- if (!$quote) {
- return null;
- }
- return 'NULL';
- }
-
- if (null === $type) {
- switch (gettype($value)) {
- case 'integer':
- $type = 'integer';
- break;
- case 'double':
- // todo: default to decimal as float is quite unusual
- // $type = 'float';
- $type = 'decimal';
- break;
- case 'boolean':
- $type = 'boolean';
- break;
- case 'array':
- $value = serialize($value);
- case 'object':
- $type = 'text';
- break;
- default:
- if (preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}$/', $value)) {
- $type = 'timestamp';
- } elseif (preg_match('/^\d{2}:\d{2}$/', $value)) {
- $type = 'time';
- } elseif (preg_match('/^\d{4}-\d{2}-\d{2}$/', $value)) {
- $type = 'date';
- } else {
- $type = 'text';
- }
- break;
- }
- } elseif (!empty($db->options['datatype_map'][$type])) {
- $type = $db->options['datatype_map'][$type];
- if (!empty($db->options['datatype_map_callback'][$type])) {
- $parameter = array('type' => $type, 'value' => $value, 'quote' => $quote, 'escape_wildcards' => $escape_wildcards);
- return call_user_func_array($db->options['datatype_map_callback'][$type], array(&$db, __FUNCTION__, $parameter));
- }
- }
-
- if (!method_exists($this, "_quote{$type}")) {
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
- 'type not defined: '.$type, __FUNCTION__);
- }
- $value = $this->{"_quote{$type}"}($value, $quote, $escape_wildcards);
- if ($quote && $escape_wildcards && $db->string_quoting['escape_pattern']
- && $db->string_quoting['escape'] !== $db->string_quoting['escape_pattern']
- ) {
- $value.= $this->patternEscapeString();
- }
- return $value;
- }
-
- // }}}
- // {{{ _quoteInteger()
-
- /**
- * Convert a text value into a DBMS specific format that is suitable to
- * compose query statements.
- *
- * @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 $escape_wildcards if to escape escape wildcards
- * @return string text string that represents the given argument value in
- * a DBMS specific format.
- * @access protected
- */
- function _quoteInteger($value, $quote, $escape_wildcards)
- {
- return (int)$value;
- }
-
- // }}}
- // {{{ _quoteText()
-
- /**
- * Convert a text value into a DBMS specific format that is suitable to
- * compose query statements.
- *
- * @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 $escape_wildcards if to escape escape wildcards
- * @return string text string that already contains any DBMS specific
- * escaped character sequences.
- * @access protected
- */
- function _quoteText($value, $quote, $escape_wildcards)
- {
- if (!$quote) {
- return $value;
- }
-
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- $value = $db->escape($value, $escape_wildcards);
- if (PEAR::isError($value)) {
- return $value;
- }
- return "'".$value."'";
- }
-
- // }}}
- // {{{ _readFile()
-
- /**
- * Convert a text value into a DBMS specific format that is suitable to
- * compose query statements.
- *
- * @param string $value text string value that is intended to be converted.
- * @return string text string that represents the given argument value in
- * a DBMS specific format.
- * @access protected
- */
- function _readFile($value)
- {
- $close = false;
- if (preg_match('/^(\w+:\/\/)(.*)$/', $value, $match)) {
- $close = true;
- if (strtolower($match[1]) == 'file://') {
- $value = $match[2];
- }
- $value = @fopen($value, 'r');
- }
-
- if (is_resource($value)) {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- $fp = $value;
- $value = '';
- while (!@feof($fp)) {
- $value.= @fread($fp, $db->options['lob_buffer_length']);
- }
- if ($close) {
- @fclose($fp);
- }
- }
-
- return $value;
- }
-
- // }}}
- // {{{ _quoteLOB()
-
- /**
- * Convert a text value into a DBMS specific format that is suitable to
- * compose query statements.
- *
- * @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 $escape_wildcards if to escape escape wildcards
- * @return string text string that represents the given argument value in
- * a DBMS specific format.
- * @access protected
- */
- function _quoteLOB($value, $quote, $escape_wildcards)
- {
- $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);
- }
-
- // }}}
- // {{{ _quoteCLOB()
-
- /**
- * Convert a text value into a DBMS specific format that is suitable to
- * compose query statements.
- *
- * @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 $escape_wildcards if to escape escape wildcards
- * @return string text string that represents the given argument value in
- * a DBMS specific format.
- * @access protected
- */
- function _quoteCLOB($value, $quote, $escape_wildcards)
- {
- return $this->_quoteLOB($value, $quote, $escape_wildcards);
- }
-
- // }}}
- // {{{ _quoteBLOB()
-
- /**
- * Convert a text value into a DBMS specific format that is suitable to
- * compose query statements.
- *
- * @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 $escape_wildcards if to escape escape wildcards
- * @return string text string that represents the given argument value in
- * a DBMS specific format.
- * @access protected
- */
- function _quoteBLOB($value, $quote, $escape_wildcards)
- {
- return $this->_quoteLOB($value, $quote, $escape_wildcards);
- }
-
- // }}}
- // {{{ _quoteBoolean()
-
- /**
- * Convert a text value into a DBMS specific format that is suitable to
- * compose query statements.
- *
- * @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 $escape_wildcards if to escape escape wildcards
- * @return string text string that represents the given argument value in
- * a DBMS specific format.
- * @access protected
- */
- function _quoteBoolean($value, $quote, $escape_wildcards)
- {
- return ($value ? 1 : 0);
- }
-
- // }}}
- // {{{ _quoteDate()
-
- /**
- * Convert a text value into a DBMS specific format that is suitable to
- * compose query statements.
- *
- * @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 $escape_wildcards if to escape escape wildcards
- * @return string text string that represents the given argument value in
- * a DBMS specific format.
- * @access protected
- */
- function _quoteDate($value, $quote, $escape_wildcards)
- {
- if ($value === 'CURRENT_DATE') {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
- if (isset($db->function) && is_a($db->function, 'MDB2_Driver_Function_Common')) {
- return $db->function->now('date');
- }
- return 'CURRENT_DATE';
- }
- return $this->_quoteText($value, $quote, $escape_wildcards);
- }
-
- // }}}
- // {{{ _quoteTimestamp()
-
- /**
- * Convert a text value into a DBMS specific format that is suitable to
- * compose query statements.
- *
- * @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 $escape_wildcards if to escape escape wildcards
- * @return string text string that represents the given argument value in
- * a DBMS specific format.
- * @access protected
- */
- function _quoteTimestamp($value, $quote, $escape_wildcards)
- {
- if ($value === 'CURRENT_TIMESTAMP') {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
- if (isset($db->function) && is_a($db->function, 'MDB2_Driver_Function_Common')) {
- return $db->function->now('timestamp');
- }
- return 'CURRENT_TIMESTAMP';
- }
- return $this->_quoteText($value, $quote, $escape_wildcards);
- }
-
- // }}}
- // {{{ _quoteTime()
-
- /**
- * Convert a text value into a DBMS specific format that is suitable to
- * compose query statements.
- *
- * @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 $escape_wildcards if to escape escape wildcards
- * @return string text string that represents the given argument value in
- * a DBMS specific format.
- * @access protected
- */
- function _quoteTime($value, $quote, $escape_wildcards)
- {
- if ($value === 'CURRENT_TIME') {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
- if (isset($db->function) && is_a($db->function, 'MDB2_Driver_Function_Common')) {
- return $db->function->now('time');
- }
- return 'CURRENT_TIME';
- }
- return $this->_quoteText($value, $quote, $escape_wildcards);
- }
-
- // }}}
- // {{{ _quoteFloat()
-
- /**
- * Convert a text value into a DBMS specific format that is suitable to
- * compose query statements.
- *
- * @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 $escape_wildcards if to escape escape wildcards
- * @return string text string that represents the given argument value in
- * a DBMS specific format.
- * @access protected
- */
- function _quoteFloat($value, $quote, $escape_wildcards)
- {
- if (preg_match('/^(.*)e([-+])(\d+)$/i', $value, $matches)) {
- $decimal = $this->_quoteDecimal($matches[1], $quote, $escape_wildcards);
- $sign = $matches[2];
- $exponent = str_pad($matches[3], 2, '0', STR_PAD_LEFT);
- $value = $decimal.'E'.$sign.$exponent;
- } else {
- $value = $this->_quoteDecimal($value, $quote, $escape_wildcards);
- }
- return $value;
- }
-
- // }}}
- // {{{ _quoteDecimal()
-
- /**
- * Convert a text value into a DBMS specific format that is suitable to
- * compose query statements.
- *
- * @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 $escape_wildcards if to escape escape wildcards
- * @return string text string that represents the given argument value in
- * a DBMS specific format.
- * @access protected
- */
- function _quoteDecimal($value, $quote, $escape_wildcards)
- {
- $value = (string)$value;
- $value = preg_replace('/[^\d\.,\-+eE]/', '', $value);
- if (preg_match('/[^\.\d]/', $value)) {
- if (strpos($value, ',')) {
- // 1000,00
- if (!strpos($value, '.')) {
- // convert the last "," to a "."
- $value = strrev(str_replace(',', '.', strrev($value)));
- // 1.000,00
- } elseif (strpos($value, '.') && strpos($value, '.') < strpos($value, ',')) {
- $value = str_replace('.', '', $value);
- // convert the last "," to a "."
- $value = strrev(str_replace(',', '.', strrev($value)));
- // 1,000.00
- } else {
- $value = str_replace(',', '', $value);
- }
- }
- }
- return $value;
- }
-
- // }}}
- // {{{ writeLOBToFile()
-
- /**
- * retrieve LOB from the database
- *
- * @param resource $lob stream handle
- * @param string $file name of the file into which the LOb should be fetched
- * @return mixed MDB2_OK on success, a MDB2 error on failure
- * @access protected
- */
- function writeLOBToFile($lob, $file)
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- if (preg_match('/^(\w+:\/\/)(.*)$/', $file, $match)) {
- if ($match[1] == 'file://') {
- $file = $match[2];
- }
- }
-
- $fp = @fopen($file, 'wb');
- while (!@feof($lob)) {
- $result = @fread($lob, $db->options['lob_buffer_length']);
- $read = strlen($result);
- if (@fwrite($fp, $result, $read) != $read) {
- @fclose($fp);
- return $db->raiseError(MDB2_ERROR, null, null,
- 'could not write to the output file', __FUNCTION__);
- }
- }
- @fclose($fp);
- return MDB2_OK;
- }
-
- // }}}
- // {{{ _retrieveLOB()
-
- /**
- * retrieve LOB from the database
- *
- * @param array $lob array
- * @return mixed MDB2_OK on success, a MDB2 error on failure
- * @access protected
- */
- function _retrieveLOB(&$lob)
- {
- if (null === $lob['value']) {
- $lob['value'] = $lob['resource'];
- }
- $lob['loaded'] = true;
- return MDB2_OK;
- }
-
- // }}}
- // {{{ readLOB()
-
- /**
- * Read data from large object input stream.
- *
- * @param resource $lob stream handle
- * @param string $data reference to a variable that will hold data
- * to be read from the large object input stream
- * @param integer $length value that indicates the largest ammount ofdata
- * to be read from the large object input stream.
- * @return mixed the effective number of bytes read from the large object
- * input stream on sucess or an MDB2 error object.
- * @access public
- * @see endOfLOB()
- */
- function _readLOB($lob, $length)
- {
- return substr($lob['value'], $lob['position'], $length);
- }
-
- // }}}
- // {{{ _endOfLOB()
-
- /**
- * Determine whether it was reached the end of the large object and
- * therefore there is no more data to be read for the its input stream.
- *
- * @param array $lob array
- * @return mixed true or false on success, a MDB2 error on failure
- * @access protected
- */
- function _endOfLOB($lob)
- {
- return $lob['endOfLOB'];
- }
-
- // }}}
- // {{{ destroyLOB()
-
- /**
- * Free any resources allocated during the lifetime of the large object
- * handler object.
- *
- * @param resource $lob stream handle
- * @access public
- */
- function destroyLOB($lob)
- {
- $lob_data = stream_get_meta_data($lob);
- $lob_index = $lob_data['wrapper_data']->lob_index;
- fclose($lob);
- if (isset($this->lobs[$lob_index])) {
- $this->_destroyLOB($this->lobs[$lob_index]);
- unset($this->lobs[$lob_index]);
- }
- return MDB2_OK;
- }
-
- // }}}
- // {{{ _destroyLOB()
-
- /**
- * Free any resources allocated during the lifetime of the large object
- * handler object.
- *
- * @param array $lob array
- * @access private
- */
- function _destroyLOB(&$lob)
- {
- return MDB2_OK;
- }
-
- // }}}
- // {{{ implodeArray()
-
- /**
- * apply a type to all values of an array and return as a comma seperated string
- * useful for generating IN statements
- *
- * @access public
- *
- * @param array $array data array
- * @param string $type determines type of the field
- *
- * @return string comma seperated values
- */
- function implodeArray($array, $type = false)
- {
- if (!is_array($array) || empty($array)) {
- return 'NULL';
- }
- if ($type) {
- foreach ($array as $value) {
- $return[] = $this->quote($value, $type);
- }
- } else {
- $return = $array;
- }
- return implode(', ', $return);
- }
-
- // }}}
- // {{{ 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) {
- $operator = strtoupper($operator);
- switch ($operator) {
- // case insensitive
- case 'ILIKE':
- 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 = (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,
- 'not a supported operator type:'. $operator, __FUNCTION__);
- }
- }
- $match.= "'";
- foreach ($pattern as $key => $value) {
- if ($key % 2) {
- $match.= $value;
- } else {
- $escaped = $db->escape($value);
- if (PEAR::isError($escaped)) {
- return $escaped;
- }
- $match.= $db->escapePattern($escaped);
- }
- }
- $match.= "'";
- $match.= $this->patternEscapeString();
- return $match;
- }
-
- // }}}
- // {{{ patternEscapeString()
-
- /**
- * build string to define pattern escape character
- *
- * @access public
- *
- * @return string define pattern escape character
- */
- function patternEscapeString()
- {
- return '';
- }
-
- // }}}
- // {{{ 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;
- }
-
- // 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,
- 'method not implemented', __FUNCTION__);
- }
-
- // }}}
- // {{{ mapPrepareDatatype()
-
- /**
- * Maps an mdb2 datatype to mysqli prepare type
- *
- * @param string $type
- * @return string
- * @access public
- */
- function mapPrepareDatatype($type)
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- if (!empty($db->options['datatype_map'][$type])) {
- $type = $db->options['datatype_map'][$type];
- if (!empty($db->options['datatype_map_callback'][$type])) {
- $parameter = array('type' => $type);
- return call_user_func_array($db->options['datatype_map_callback'][$type], array(&$db, __FUNCTION__, $parameter));
- }
- }
-
- return $type;
- }
-}
-?>
+ |
+// +----------------------------------------------------------------------+
+//
+// $Id$
+
+require_once 'MDB2/LOB.php';
+
+/**
+ * @package MDB2
+ * @category Database
+ * @author Lukas Smith
+ */
+
+/**
+ * MDB2_Driver_Common: Base class that is extended by each MDB2 driver
+ *
+ * To load this module in the MDB2 object:
+ * $mdb->loadModule('Datatype');
+ *
+ * @package MDB2
+ * @category Database
+ * @author Lukas Smith
+ */
+class MDB2_Driver_Datatype_Common extends MDB2_Module_Common
+{
+ var $valid_default_values = array(
+ 'text' => '',
+ 'boolean' => true,
+ 'integer' => 0,
+ 'decimal' => 0.0,
+ 'float' => 0.0,
+ 'timestamp' => '1970-01-01 00:00:00',
+ 'time' => '00:00:00',
+ 'date' => '1970-01-01',
+ 'clob' => '',
+ 'blob' => '',
+ );
+
+ /**
+ * contains all LOB objects created with this MDB2 instance
+ * @var array
+ * @access protected
+ */
+ var $lobs = array();
+
+ // }}}
+ // {{{ getValidTypes()
+
+ /**
+ * Get the list of valid types
+ *
+ * This function returns an array of valid types as keys with the values
+ * being possible default values for all native datatypes and mapped types
+ * for custom datatypes.
+ *
+ * @return mixed array on success, a MDB2 error on failure
+ * @access public
+ */
+ function getValidTypes()
+ {
+ $types = $this->valid_default_values;
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+ if (!empty($db->options['datatype_map'])) {
+ foreach ($db->options['datatype_map'] as $type => $mapped_type) {
+ if (array_key_exists($mapped_type, $types)) {
+ $types[$type] = $types[$mapped_type];
+ } elseif (!empty($db->options['datatype_map_callback'][$type])) {
+ $parameter = array('type' => $type, 'mapped_type' => $mapped_type);
+ $default = call_user_func_array($db->options['datatype_map_callback'][$type], array(&$db, __FUNCTION__, $parameter));
+ $types[$type] = $default;
+ }
+ }
+ }
+ return $types;
+ }
+
+ // }}}
+ // {{{ checkResultTypes()
+
+ /**
+ * Define the list of types to be associated with the columns of a given
+ * result set.
+ *
+ * This function may be called before invoking fetchRow(), fetchOne()
+ * fetchCole() and fetchAll() so that the necessary data type
+ * conversions are performed on the data to be retrieved by them. If this
+ * function is not called, the type of all result set columns is assumed
+ * to be text, thus leading to not perform any conversions.
+ *
+ * @param array $types array variable that lists the
+ * data types to be expected in the result set columns. If this array
+ * contains less types than the number of columns that are returned
+ * in the result set, the remaining columns are assumed to be of the
+ * type text. Currently, the types clob and blob are not fully
+ * supported.
+ * @return mixed MDB2_OK on success, a MDB2 error on failure
+ * @access public
+ */
+ function checkResultTypes($types)
+ {
+ $types = is_array($types) ? $types : array($types);
+ foreach ($types as $key => $type) {
+ if (!isset($this->valid_default_values[$type])) {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+ if (empty($db->options['datatype_map'][$type])) {
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ $type.' for '.$key.' is not a supported column type', __FUNCTION__);
+ }
+ }
+ }
+ return $types;
+ }
+
+ // }}}
+ // {{{ _baseConvertResult()
+
+ /**
+ * General type conversion method
+ *
+ * @param mixed $value reference to a value to be converted
+ * @param string $type specifies which type to convert to
+ * @param boolean $rtrim [optional] when TRUE [default], apply rtrim() to text
+ * @return object an MDB2 error on failure
+ * @access protected
+ */
+ function _baseConvertResult($value, $type, $rtrim = true)
+ {
+ switch ($type) {
+ case 'text':
+ if ($rtrim) {
+ $value = rtrim($value);
+ }
+ return $value;
+ case 'integer':
+ return intval($value);
+ case 'boolean':
+ return !empty($value);
+ case 'decimal':
+ return $value;
+ case 'float':
+ return doubleval($value);
+ case 'date':
+ return $value;
+ case 'time':
+ return $value;
+ case 'timestamp':
+ return $value;
+ case 'clob':
+ case 'blob':
+ $this->lobs[] = array(
+ 'buffer' => null,
+ 'position' => 0,
+ 'lob_index' => null,
+ 'endOfLOB' => false,
+ 'resource' => $value,
+ 'value' => null,
+ 'loaded' => false,
+ );
+ end($this->lobs);
+ $lob_index = key($this->lobs);
+ $this->lobs[$lob_index]['lob_index'] = $lob_index;
+ return fopen('MDB2LOB://'.$lob_index.'@'.$this->db_index, 'r+');
+ }
+
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ return $db->raiseError(MDB2_ERROR_INVALID, null, null,
+ 'attempt to convert result value to an unknown type :' . $type, __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ convertResult()
+
+ /**
+ * Convert a value to a RDBMS indipendent MDB2 type
+ *
+ * @param mixed $value value to be converted
+ * @param string $type specifies which type to convert to
+ * @param boolean $rtrim [optional] when TRUE [default], apply rtrim() to text
+ * @return mixed converted value
+ * @access public
+ */
+ function convertResult($value, $type, $rtrim = true)
+ {
+ if (null === $value) {
+ return null;
+ }
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+ if (!empty($db->options['datatype_map'][$type])) {
+ $type = $db->options['datatype_map'][$type];
+ if (!empty($db->options['datatype_map_callback'][$type])) {
+ $parameter = array('type' => $type, 'value' => $value, 'rtrim' => $rtrim);
+ return call_user_func_array($db->options['datatype_map_callback'][$type], array(&$db, __FUNCTION__, $parameter));
+ }
+ }
+ return $this->_baseConvertResult($value, $type, $rtrim);
+ }
+
+ // }}}
+ // {{{ 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);
+ $keys = array_keys($row);
+ if (is_int($keys[0])) {
+ $types = $this->_sortResultFieldTypes($keys, $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
+ *
+ * @param array $types
+ * @param array $row specifies the types to convert to
+ * @param bool $rtrim if to rtrim text values or not
+ * @return mixed MDB2_OK on success, a MDB2 error on failure
+ * @access public
+ */
+ 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);
+ foreach (array_keys($sorted_types) as $k) {
+ if (null === $sorted_types[$k]) {
+ $sorted_types[$k] = current($types);
+ next($types);
+ }
+ }
+ }
+ return $sorted_types;
+ }
+
+ // }}}
+ // {{{ getDeclaration()
+
+ /**
+ * Obtain DBMS specific SQL code portion needed to declare
+ * of the given type
+ *
+ * @param string $type type to which the value should be converted to
+ * @param string $name name the field to be declared.
+ * @param string $field definition of the field
+ * @return string DBMS specific SQL code portion that should be used to
+ * declare the specified field.
+ * @access public
+ */
+ function getDeclaration($type, $name, $field)
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ if (!empty($db->options['datatype_map'][$type])) {
+ $type = $db->options['datatype_map'][$type];
+ if (!empty($db->options['datatype_map_callback'][$type])) {
+ $parameter = array('type' => $type, 'name' => $name, 'field' => $field);
+ 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")) {
+ return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
+ 'type not defined: '.$type, __FUNCTION__);
+ }
+ return $this->{"_get{$type}Declaration"}($name, $field);
+ }
+
+ // }}}
+ // {{{ 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':
+ $length = !empty($field['length']) ? $field['length'] : $db->options['default_text_field_length'];
+ $fixed = !empty($field['fixed']) ? $field['fixed'] : false;
+ return $fixed ? ($length ? 'CHAR('.$length.')' : 'CHAR('.$db->options['default_text_field_length'].')')
+ : ($length ? 'VARCHAR('.$length.')' : 'TEXT');
+ case 'clob':
+ return 'TEXT';
+ case 'blob':
+ return 'TEXT';
+ case 'integer':
+ return 'INT';
+ case 'boolean':
+ return 'INT';
+ case 'date':
+ return 'CHAR ('.strlen('YYYY-MM-DD').')';
+ case 'time':
+ return 'CHAR ('.strlen('HH:MM:SS').')';
+ case 'timestamp':
+ return 'CHAR ('.strlen('YYYY-MM-DD HH:MM:SS').')';
+ case 'float':
+ return 'TEXT';
+ case 'decimal':
+ return 'TEXT';
+ }
+ 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.
+ * 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, or a MDB2_Error on failure
+ * @access protected
+ */
+ function _getDeclaration($name, $field)
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($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']);
+
+ $notnull = empty($field['notnull']) ? '' : ' NOT NULL';
+ $default = '';
+ if (array_key_exists('default', $field)) {
+ if ($field['default'] === '') {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+ $valid_default_values = $this->getValidTypes();
+ $field['default'] = $valid_default_values[$field['type']];
+ if ($field['default'] === '' && ($db->options['portability'] & MDB2_PORTABILITY_EMPTY_TO_NULL)) {
+ $field['default'] = ' ';
+ }
+ }
+ if (null !== $field['default']) {
+ $default = ' DEFAULT ' . $this->quote($field['default'], $field['type']);
+ }
+ }
+
+ $collation = empty($field['collation']) ? '' :
+ ' '.$this->_getCollationFieldDeclaration($field['collation']);
+
+ return $charset.$default.$notnull.$collation;
+ }
+
+ // }}}
+ // {{{ _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 '';
+ }
+
+ // }}}
+ // {{{ _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 '';
+ }
+
+ // }}}
+ // {{{ _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 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:
+ *
+ * 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)
+ {
+ if (!empty($field['unsigned'])) {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ $db->warnings[] = "unsigned integer field \"$name\" is being declared as signed integer";
+ }
+ return $this->_getDeclaration($name, $field);
+ }
+
+ // }}}
+ // {{{ _getTextDeclaration()
+
+ /**
+ * Obtain DBMS specific SQL code portion needed to declare an text 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 _getTextDeclaration($name, $field)
+ {
+ return $this->_getDeclaration($name, $field);
+ }
+
+ // }}}
+ // {{{ _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']) ? '' : ' 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']) ? '' : ' NOT NULL';
+ $name = $db->quoteIdentifier($name, true);
+ return $name.' '.$this->getTypeDeclaration($field).$notnull;
+ }
+
+ // }}}
+ // {{{ _getBooleanDeclaration()
+
+ /**
+ * Obtain DBMS specific SQL code portion needed to declare a boolean 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:
+ *
+ * default
+ * Boolean value to be used as default for this field.
+ *
+ * notnullL
+ * 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 _getBooleanDeclaration($name, $field)
+ {
+ return $this->_getDeclaration($name, $field);
+ }
+
+ // }}}
+ // {{{ _getDateDeclaration()
+
+ /**
+ * Obtain DBMS specific SQL code portion needed to declare a date 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:
+ *
+ * default
+ * Date 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 _getDateDeclaration($name, $field)
+ {
+ return $this->_getDeclaration($name, $field);
+ }
+
+ // }}}
+ // {{{ _getTimestampDeclaration()
+
+ /**
+ * Obtain DBMS specific SQL code portion needed to declare a timestamp
+ * 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:
+ *
+ * default
+ * Timestamp 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 _getTimestampDeclaration($name, $field)
+ {
+ return $this->_getDeclaration($name, $field);
+ }
+
+ // }}}
+ // {{{ _getTimeDeclaration()
+
+ /**
+ * Obtain DBMS specific SQL code portion needed to declare a time
+ * 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:
+ *
+ * default
+ * Time 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 _getTimeDeclaration($name, $field)
+ {
+ return $this->_getDeclaration($name, $field);
+ }
+
+ // }}}
+ // {{{ _getFloatDeclaration()
+
+ /**
+ * Obtain DBMS specific SQL code portion needed to declare a float 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:
+ *
+ * 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)
+ {
+ return $this->_getDeclaration($name, $field);
+ }
+
+ // }}}
+ // {{{ _getDecimalDeclaration()
+
+ /**
+ * Obtain DBMS specific SQL code portion needed to declare a decimal 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:
+ *
+ * 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)
+ {
+ return $this->_getDeclaration($name, $field);
+ }
+
+ // }}}
+ // {{{ compareDefinition()
+
+ /**
+ * Obtain an array of changes that may need to applied
+ *
+ * @param array $current new definition
+ * @param array $previous old definition
+ * @return array containing all changes that will need to be applied
+ * @access public
+ */
+ function compareDefinition($current, $previous)
+ {
+ $type = !empty($current['type']) ? $current['type'] : null;
+
+ if (!method_exists($this, "_compare{$type}Definition")) {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($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,
+ 'type "'.$current['type'].'" is not yet supported', __FUNCTION__);
+ }
+
+ if (empty($previous['type']) || $previous['type'] != $type) {
+ return $current;
+ }
+
+ $change = $this->{"_compare{$type}Definition"}($current, $previous);
+
+ if ($previous['type'] != $type) {
+ $change['type'] = true;
+ }
+
+ $previous_notnull = !empty($previous['notnull']) ? $previous['notnull'] : false;
+ $notnull = !empty($current['notnull']) ? $current['notnull'] : false;
+ if ($previous_notnull != $notnull) {
+ $change['notnull'] = true;
+ }
+
+ $previous_default = array_key_exists('default', $previous) ? $previous['default'] :
+ ($previous_notnull ? '' : null);
+ $default = array_key_exists('default', $current) ? $current['default'] :
+ ($notnull ? '' : null);
+ if ($previous_default !== $default) {
+ $change['default'] = true;
+ }
+
+ return $change;
+ }
+
+ // }}}
+ // {{{ _compareIntegerDefinition()
+
+ /**
+ * Obtain an array of changes that may need to applied to an integer field
+ *
+ * @param array $current new definition
+ * @param array $previous old definition
+ * @return array containing all changes that will need to be applied
+ * @access protected
+ */
+ function _compareIntegerDefinition($current, $previous)
+ {
+ $change = array();
+ $previous_unsigned = !empty($previous['unsigned']) ? $previous['unsigned'] : false;
+ $unsigned = !empty($current['unsigned']) ? $current['unsigned'] : false;
+ if ($previous_unsigned != $unsigned) {
+ $change['unsigned'] = true;
+ }
+ $previous_autoincrement = !empty($previous['autoincrement']) ? $previous['autoincrement'] : false;
+ $autoincrement = !empty($current['autoincrement']) ? $current['autoincrement'] : false;
+ if ($previous_autoincrement != $autoincrement) {
+ $change['autoincrement'] = true;
+ }
+ return $change;
+ }
+
+ // }}}
+ // {{{ _compareTextDefinition()
+
+ /**
+ * Obtain an array of changes that may need to applied to an text field
+ *
+ * @param array $current new definition
+ * @param array $previous old definition
+ * @return array containing all changes that will need to be applied
+ * @access protected
+ */
+ function _compareTextDefinition($current, $previous)
+ {
+ $change = array();
+ $previous_length = !empty($previous['length']) ? $previous['length'] : 0;
+ $length = !empty($current['length']) ? $current['length'] : 0;
+ if ($previous_length != $length) {
+ $change['length'] = true;
+ }
+ $previous_fixed = !empty($previous['fixed']) ? $previous['fixed'] : 0;
+ $fixed = !empty($current['fixed']) ? $current['fixed'] : 0;
+ if ($previous_fixed != $fixed) {
+ $change['fixed'] = true;
+ }
+ return $change;
+ }
+
+ // }}}
+ // {{{ _compareCLOBDefinition()
+
+ /**
+ * Obtain an array of changes that may need to applied to an CLOB field
+ *
+ * @param array $current new definition
+ * @param array $previous old definition
+ * @return array containing all changes that will need to be applied
+ * @access protected
+ */
+ function _compareCLOBDefinition($current, $previous)
+ {
+ return $this->_compareTextDefinition($current, $previous);
+ }
+
+ // }}}
+ // {{{ _compareBLOBDefinition()
+
+ /**
+ * Obtain an array of changes that may need to applied to an BLOB field
+ *
+ * @param array $current new definition
+ * @param array $previous old definition
+ * @return array containing all changes that will need to be applied
+ * @access protected
+ */
+ function _compareBLOBDefinition($current, $previous)
+ {
+ return $this->_compareTextDefinition($current, $previous);
+ }
+
+ // }}}
+ // {{{ _compareDateDefinition()
+
+ /**
+ * Obtain an array of changes that may need to applied to an date field
+ *
+ * @param array $current new definition
+ * @param array $previous old definition
+ * @return array containing all changes that will need to be applied
+ * @access protected
+ */
+ function _compareDateDefinition($current, $previous)
+ {
+ return array();
+ }
+
+ // }}}
+ // {{{ _compareTimeDefinition()
+
+ /**
+ * Obtain an array of changes that may need to applied to an time field
+ *
+ * @param array $current new definition
+ * @param array $previous old definition
+ * @return array containing all changes that will need to be applied
+ * @access protected
+ */
+ function _compareTimeDefinition($current, $previous)
+ {
+ return array();
+ }
+
+ // }}}
+ // {{{ _compareTimestampDefinition()
+
+ /**
+ * Obtain an array of changes that may need to applied to an timestamp field
+ *
+ * @param array $current new definition
+ * @param array $previous old definition
+ * @return array containing all changes that will need to be applied
+ * @access protected
+ */
+ function _compareTimestampDefinition($current, $previous)
+ {
+ return array();
+ }
+
+ // }}}
+ // {{{ _compareBooleanDefinition()
+
+ /**
+ * Obtain an array of changes that may need to applied to an boolean field
+ *
+ * @param array $current new definition
+ * @param array $previous old definition
+ * @return array containing all changes that will need to be applied
+ * @access protected
+ */
+ function _compareBooleanDefinition($current, $previous)
+ {
+ return array();
+ }
+
+ // }}}
+ // {{{ _compareFloatDefinition()
+
+ /**
+ * Obtain an array of changes that may need to applied to an float field
+ *
+ * @param array $current new definition
+ * @param array $previous old definition
+ * @return array containing all changes that will need to be applied
+ * @access protected
+ */
+ function _compareFloatDefinition($current, $previous)
+ {
+ return array();
+ }
+
+ // }}}
+ // {{{ _compareDecimalDefinition()
+
+ /**
+ * Obtain an array of changes that may need to applied to an decimal field
+ *
+ * @param array $current new definition
+ * @param array $previous old definition
+ * @return array containing all changes that will need to be applied
+ * @access protected
+ */
+ function _compareDecimalDefinition($current, $previous)
+ {
+ return array();
+ }
+
+ // }}}
+ // {{{ quote()
+
+ /**
+ * Convert a text value into a DBMS specific format that is suitable to
+ * compose query statements.
+ *
+ * @param string $value text string value that is intended to be converted.
+ * @param string $type type to which the value should be converted to
+ * @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
+ * a DBMS specific format.
+ * @access public
+ */
+ function quote($value, $type = null, $quote = true, $escape_wildcards = false)
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ if ((null === $value)
+ || ($value === '' && $db->options['portability'] & MDB2_PORTABILITY_EMPTY_TO_NULL)
+ ) {
+ if (!$quote) {
+ return null;
+ }
+ return 'NULL';
+ }
+
+ if (null === $type) {
+ switch (gettype($value)) {
+ case 'integer':
+ $type = 'integer';
+ break;
+ case 'double':
+ // todo: default to decimal as float is quite unusual
+ // $type = 'float';
+ $type = 'decimal';
+ break;
+ case 'boolean':
+ $type = 'boolean';
+ break;
+ case 'array':
+ $value = serialize($value);
+ case 'object':
+ $type = 'text';
+ break;
+ default:
+ if (preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}$/', $value)) {
+ $type = 'timestamp';
+ } elseif (preg_match('/^\d{2}:\d{2}$/', $value)) {
+ $type = 'time';
+ } elseif (preg_match('/^\d{4}-\d{2}-\d{2}$/', $value)) {
+ $type = 'date';
+ } else {
+ $type = 'text';
+ }
+ break;
+ }
+ } elseif (!empty($db->options['datatype_map'][$type])) {
+ $type = $db->options['datatype_map'][$type];
+ if (!empty($db->options['datatype_map_callback'][$type])) {
+ $parameter = array('type' => $type, 'value' => $value, 'quote' => $quote, 'escape_wildcards' => $escape_wildcards);
+ return call_user_func_array($db->options['datatype_map_callback'][$type], array(&$db, __FUNCTION__, $parameter));
+ }
+ }
+
+ if (!method_exists($this, "_quote{$type}")) {
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'type not defined: '.$type, __FUNCTION__);
+ }
+ $value = $this->{"_quote{$type}"}($value, $quote, $escape_wildcards);
+ if ($quote && $escape_wildcards && $db->string_quoting['escape_pattern']
+ && $db->string_quoting['escape'] !== $db->string_quoting['escape_pattern']
+ ) {
+ $value.= $this->patternEscapeString();
+ }
+ return $value;
+ }
+
+ // }}}
+ // {{{ _quoteInteger()
+
+ /**
+ * Convert a text value into a DBMS specific format that is suitable to
+ * compose query statements.
+ *
+ * @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 $escape_wildcards if to escape escape wildcards
+ * @return string text string that represents the given argument value in
+ * a DBMS specific format.
+ * @access protected
+ */
+ function _quoteInteger($value, $quote, $escape_wildcards)
+ {
+ return (int)$value;
+ }
+
+ // }}}
+ // {{{ _quoteText()
+
+ /**
+ * Convert a text value into a DBMS specific format that is suitable to
+ * compose query statements.
+ *
+ * @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 $escape_wildcards if to escape escape wildcards
+ * @return string text string that already contains any DBMS specific
+ * escaped character sequences.
+ * @access protected
+ */
+ function _quoteText($value, $quote, $escape_wildcards)
+ {
+ if (!$quote) {
+ return $value;
+ }
+
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ $value = $db->escape($value, $escape_wildcards);
+ if (PEAR::isError($value)) {
+ return $value;
+ }
+ return "'".$value."'";
+ }
+
+ // }}}
+ // {{{ _readFile()
+
+ /**
+ * Convert a text value into a DBMS specific format that is suitable to
+ * compose query statements.
+ *
+ * @param string $value text string value that is intended to be converted.
+ * @return string text string that represents the given argument value in
+ * a DBMS specific format.
+ * @access protected
+ */
+ function _readFile($value)
+ {
+ $close = false;
+ if (preg_match('/^(\w+:\/\/)(.*)$/', $value, $match)) {
+ $close = true;
+ if (strtolower($match[1]) == 'file://') {
+ $value = $match[2];
+ }
+ $value = @fopen($value, 'r');
+ }
+
+ if (is_resource($value)) {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ $fp = $value;
+ $value = '';
+ while (!@feof($fp)) {
+ $value.= @fread($fp, $db->options['lob_buffer_length']);
+ }
+ if ($close) {
+ @fclose($fp);
+ }
+ }
+
+ return $value;
+ }
+
+ // }}}
+ // {{{ _quoteLOB()
+
+ /**
+ * Convert a text value into a DBMS specific format that is suitable to
+ * compose query statements.
+ *
+ * @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 $escape_wildcards if to escape escape wildcards
+ * @return string text string that represents the given argument value in
+ * a DBMS specific format.
+ * @access protected
+ */
+ function _quoteLOB($value, $quote, $escape_wildcards)
+ {
+ $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);
+ }
+
+ // }}}
+ // {{{ _quoteCLOB()
+
+ /**
+ * Convert a text value into a DBMS specific format that is suitable to
+ * compose query statements.
+ *
+ * @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 $escape_wildcards if to escape escape wildcards
+ * @return string text string that represents the given argument value in
+ * a DBMS specific format.
+ * @access protected
+ */
+ function _quoteCLOB($value, $quote, $escape_wildcards)
+ {
+ return $this->_quoteLOB($value, $quote, $escape_wildcards);
+ }
+
+ // }}}
+ // {{{ _quoteBLOB()
+
+ /**
+ * Convert a text value into a DBMS specific format that is suitable to
+ * compose query statements.
+ *
+ * @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 $escape_wildcards if to escape escape wildcards
+ * @return string text string that represents the given argument value in
+ * a DBMS specific format.
+ * @access protected
+ */
+ function _quoteBLOB($value, $quote, $escape_wildcards)
+ {
+ return $this->_quoteLOB($value, $quote, $escape_wildcards);
+ }
+
+ // }}}
+ // {{{ _quoteBoolean()
+
+ /**
+ * Convert a text value into a DBMS specific format that is suitable to
+ * compose query statements.
+ *
+ * @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 $escape_wildcards if to escape escape wildcards
+ * @return string text string that represents the given argument value in
+ * a DBMS specific format.
+ * @access protected
+ */
+ function _quoteBoolean($value, $quote, $escape_wildcards)
+ {
+ return ($value ? 1 : 0);
+ }
+
+ // }}}
+ // {{{ _quoteDate()
+
+ /**
+ * Convert a text value into a DBMS specific format that is suitable to
+ * compose query statements.
+ *
+ * @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 $escape_wildcards if to escape escape wildcards
+ * @return string text string that represents the given argument value in
+ * a DBMS specific format.
+ * @access protected
+ */
+ function _quoteDate($value, $quote, $escape_wildcards)
+ {
+ if ($value === 'CURRENT_DATE') {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+ if (isset($db->function) && is_object($this->function) && is_a($db->function, 'MDB2_Driver_Function_Common')) {
+ return $db->function->now('date');
+ }
+ return 'CURRENT_DATE';
+ }
+ return $this->_quoteText($value, $quote, $escape_wildcards);
+ }
+
+ // }}}
+ // {{{ _quoteTimestamp()
+
+ /**
+ * Convert a text value into a DBMS specific format that is suitable to
+ * compose query statements.
+ *
+ * @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 $escape_wildcards if to escape escape wildcards
+ * @return string text string that represents the given argument value in
+ * a DBMS specific format.
+ * @access protected
+ */
+ function _quoteTimestamp($value, $quote, $escape_wildcards)
+ {
+ if ($value === 'CURRENT_TIMESTAMP') {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+ if (isset($db->function) && is_object($this->function) && is_a($db->function, 'MDB2_Driver_Function_Common')) {
+ return $db->function->now('timestamp');
+ }
+ return 'CURRENT_TIMESTAMP';
+ }
+ return $this->_quoteText($value, $quote, $escape_wildcards);
+ }
+
+ // }}}
+ // {{{ _quoteTime()
+
+ /**
+ * Convert a text value into a DBMS specific format that is suitable to
+ * compose query statements.
+ *
+ * @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 $escape_wildcards if to escape escape wildcards
+ * @return string text string that represents the given argument value in
+ * a DBMS specific format.
+ * @access protected
+ */
+ function _quoteTime($value, $quote, $escape_wildcards)
+ {
+ if ($value === 'CURRENT_TIME') {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+ if (isset($db->function) && is_object($this->function) && is_a($db->function, 'MDB2_Driver_Function_Common')) {
+ return $db->function->now('time');
+ }
+ return 'CURRENT_TIME';
+ }
+ return $this->_quoteText($value, $quote, $escape_wildcards);
+ }
+
+ // }}}
+ // {{{ _quoteFloat()
+
+ /**
+ * Convert a text value into a DBMS specific format that is suitable to
+ * compose query statements.
+ *
+ * @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 $escape_wildcards if to escape escape wildcards
+ * @return string text string that represents the given argument value in
+ * a DBMS specific format.
+ * @access protected
+ */
+ function _quoteFloat($value, $quote, $escape_wildcards)
+ {
+ if (preg_match('/^(.*)e([-+])(\d+)$/i', $value, $matches)) {
+ $decimal = $this->_quoteDecimal($matches[1], $quote, $escape_wildcards);
+ $sign = $matches[2];
+ $exponent = str_pad($matches[3], 2, '0', STR_PAD_LEFT);
+ $value = $decimal.'E'.$sign.$exponent;
+ } else {
+ $value = $this->_quoteDecimal($value, $quote, $escape_wildcards);
+ }
+ return $value;
+ }
+
+ // }}}
+ // {{{ _quoteDecimal()
+
+ /**
+ * Convert a text value into a DBMS specific format that is suitable to
+ * compose query statements.
+ *
+ * @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 $escape_wildcards if to escape escape wildcards
+ * @return string text string that represents the given argument value in
+ * a DBMS specific format.
+ * @access protected
+ */
+ function _quoteDecimal($value, $quote, $escape_wildcards)
+ {
+ $value = (string)$value;
+ $value = preg_replace('/[^\d\.,\-+eE]/', '', $value);
+ if (preg_match('/[^\.\d]/', $value)) {
+ if (strpos($value, ',')) {
+ // 1000,00
+ if (!strpos($value, '.')) {
+ // convert the last "," to a "."
+ $value = strrev(str_replace(',', '.', strrev($value)));
+ // 1.000,00
+ } elseif (strpos($value, '.') && strpos($value, '.') < strpos($value, ',')) {
+ $value = str_replace('.', '', $value);
+ // convert the last "," to a "."
+ $value = strrev(str_replace(',', '.', strrev($value)));
+ // 1,000.00
+ } else {
+ $value = str_replace(',', '', $value);
+ }
+ }
+ }
+ return $value;
+ }
+
+ // }}}
+ // {{{ writeLOBToFile()
+
+ /**
+ * retrieve LOB from the database
+ *
+ * @param resource $lob stream handle
+ * @param string $file name of the file into which the LOb should be fetched
+ * @return mixed MDB2_OK on success, a MDB2 error on failure
+ * @access protected
+ */
+ function writeLOBToFile($lob, $file)
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ if (preg_match('/^(\w+:\/\/)(.*)$/', $file, $match)) {
+ if ($match[1] == 'file://') {
+ $file = $match[2];
+ }
+ }
+
+ $fp = @fopen($file, 'wb');
+ while (!@feof($lob)) {
+ $result = @fread($lob, $db->options['lob_buffer_length']);
+ $read = strlen($result);
+ if (@fwrite($fp, $result, $read) != $read) {
+ @fclose($fp);
+ return $db->raiseError(MDB2_ERROR, null, null,
+ 'could not write to the output file', __FUNCTION__);
+ }
+ }
+ @fclose($fp);
+ return MDB2_OK;
+ }
+
+ // }}}
+ // {{{ _retrieveLOB()
+
+ /**
+ * retrieve LOB from the database
+ *
+ * @param array $lob array
+ * @return mixed MDB2_OK on success, a MDB2 error on failure
+ * @access protected
+ */
+ function _retrieveLOB(&$lob)
+ {
+ if (null === $lob['value']) {
+ $lob['value'] = $lob['resource'];
+ }
+ $lob['loaded'] = true;
+ return MDB2_OK;
+ }
+
+ // }}}
+ // {{{ readLOB()
+
+ /**
+ * Read data from large object input stream.
+ *
+ * @param resource $lob stream handle
+ * @param string $data reference to a variable that will hold data
+ * to be read from the large object input stream
+ * @param integer $length value that indicates the largest ammount ofdata
+ * to be read from the large object input stream.
+ * @return mixed the effective number of bytes read from the large object
+ * input stream on sucess or an MDB2 error object.
+ * @access public
+ * @see endOfLOB()
+ */
+ function _readLOB($lob, $length)
+ {
+ return substr($lob['value'], $lob['position'], $length);
+ }
+
+ // }}}
+ // {{{ _endOfLOB()
+
+ /**
+ * Determine whether it was reached the end of the large object and
+ * therefore there is no more data to be read for the its input stream.
+ *
+ * @param array $lob array
+ * @return mixed true or false on success, a MDB2 error on failure
+ * @access protected
+ */
+ function _endOfLOB($lob)
+ {
+ return $lob['endOfLOB'];
+ }
+
+ // }}}
+ // {{{ destroyLOB()
+
+ /**
+ * Free any resources allocated during the lifetime of the large object
+ * handler object.
+ *
+ * @param resource $lob stream handle
+ * @access public
+ */
+ function destroyLOB($lob)
+ {
+ $lob_data = stream_get_meta_data($lob);
+ $lob_index = $lob_data['wrapper_data']->lob_index;
+ fclose($lob);
+ if (isset($this->lobs[$lob_index])) {
+ $this->_destroyLOB($this->lobs[$lob_index]);
+ unset($this->lobs[$lob_index]);
+ }
+ return MDB2_OK;
+ }
+
+ // }}}
+ // {{{ _destroyLOB()
+
+ /**
+ * Free any resources allocated during the lifetime of the large object
+ * handler object.
+ *
+ * @param array $lob array
+ * @access private
+ */
+ function _destroyLOB(&$lob)
+ {
+ return MDB2_OK;
+ }
+
+ // }}}
+ // {{{ implodeArray()
+
+ /**
+ * apply a type to all values of an array and return as a comma seperated string
+ * useful for generating IN statements
+ *
+ * @access public
+ *
+ * @param array $array data array
+ * @param string $type determines type of the field
+ *
+ * @return string comma seperated values
+ */
+ function implodeArray($array, $type = false)
+ {
+ if (!is_array($array) || empty($array)) {
+ return 'NULL';
+ }
+ if ($type) {
+ foreach ($array as $value) {
+ $return[] = $this->quote($value, $type);
+ }
+ } else {
+ $return = $array;
+ }
+ return implode(', ', $return);
+ }
+
+ // }}}
+ // {{{ 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) {
+ $operator = strtoupper($operator);
+ switch ($operator) {
+ // case insensitive
+ case 'ILIKE':
+ 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 = (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,
+ 'not a supported operator type:'. $operator, __FUNCTION__);
+ }
+ }
+ $match.= "'";
+ foreach ($pattern as $key => $value) {
+ if ($key % 2) {
+ $match.= $value;
+ } else {
+ $escaped = $db->escape($value);
+ if (PEAR::isError($escaped)) {
+ return $escaped;
+ }
+ $match.= $db->escapePattern($escaped);
+ }
+ }
+ $match.= "'";
+ $match.= $this->patternEscapeString();
+ return $match;
+ }
+
+ // }}}
+ // {{{ patternEscapeString()
+
+ /**
+ * build string to define pattern escape character
+ *
+ * @access public
+ *
+ * @return string define pattern escape character
+ */
+ function patternEscapeString()
+ {
+ return '';
+ }
+
+ // }}}
+ // {{{ 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;
+ }
+
+ // 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,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ mapPrepareDatatype()
+
+ /**
+ * Maps an mdb2 datatype to mysqli prepare type
+ *
+ * @param string $type
+ * @return string
+ * @access public
+ */
+ function mapPrepareDatatype($type)
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ if (!empty($db->options['datatype_map'][$type])) {
+ $type = $db->options['datatype_map'][$type];
+ if (!empty($db->options['datatype_map_callback'][$type])) {
+ $parameter = array('type' => $type);
+ return call_user_func_array($db->options['datatype_map_callback'][$type], array(&$db, __FUNCTION__, $parameter));
+ }
+ }
+
+ return $type;
+ }
+}
+?>
diff --git a/3rdparty/MDB2/Driver/Datatype/mysql.php b/3rdparty/MDB2/Driver/Datatype/mysql.php
index 299901ae6e713b043c121dfccdb650361a3f7700..d23eed23ff7c5f4aada016fbdd72b814c6df2d49 100644
--- a/3rdparty/MDB2/Driver/Datatype/mysql.php
+++ b/3rdparty/MDB2/Driver/Datatype/mysql.php
@@ -1,562 +1,602 @@
- |
-// +----------------------------------------------------------------------+
-//
-// $Id: mysql.php 295587 2010-02-28 17:16:38Z quipo $
-//
-
-require_once 'MDB2/Driver/Datatype/Common.php';
-
-/**
- * MDB2 MySQL driver
- *
- * @package MDB2
- * @category Database
- * @author Lukas Smith
- */
-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';
- if (empty($default) && empty($notnull)) {
- $default = ' DEFAULT NULL';
- }
- $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';
- }
- $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);
- }
-
- // }}}
-}
-
+ |
+// +----------------------------------------------------------------------+
+//
+// $Id$
+//
+
+require_once 'MDB2/Driver/Datatype/Common.php';
+
+/**
+ * MDB2 MySQL driver
+ *
+ * @package MDB2
+ * @category Database
+ * @author Lukas Smith
+ */
+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;
+ }
+
+ // }}}
+ // {{{ getDeclaration()
+
+ /**
+ * Obtain DBMS specific SQL code portion needed to declare
+ * of the given type
+ *
+ * @param string $type type to which the value should be converted to
+ * @param string $name name the field to be declared.
+ * @param string $field definition of the field
+ *
+ * @return string DBMS-specific SQL code portion that should be used to
+ * declare the specified field.
+ * @access public
+ */
+ function getDeclaration($type, $name, $field)
+ {
+ // MySQL DDL syntax forbids combining NOT NULL with DEFAULT NULL.
+ // To get a default of NULL for NOT NULL columns, omit it.
+ if ( isset($field['notnull'])
+ && !empty($field['notnull'])
+ && array_key_exists('default', $field) // do not use isset() here!
+ && null === $field['default']
+ ) {
+ unset($field['default']);
+ }
+ return parent::getDeclaration($type, $name, $field);
+ }
+
+ // }}}
+ // {{{ 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':
+ $l = '';
+ if (!empty($field['length'])) {
+ $l = '(' . $field['length'];
+ if (!empty($field['scale'])) {
+ $l .= ',' . $field['scale'];
+ }
+ $l .= ')';
+ }
+ return 'DOUBLE' . $l;
+ 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';
+ if (empty($default) && empty($notnull)) {
+ $default = ' DEFAULT NULL';
+ }
+ $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';
+ }
+ $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']);
+ if ($decimal !== false) {
+ $length = $length.','.$decimal;
+ }
+ 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);
+ }
+
+ // }}}
+}
+
?>
\ No newline at end of file
diff --git a/3rdparty/MDB2/Driver/Datatype/pgsql.php b/3rdparty/MDB2/Driver/Datatype/pgsql.php
index fec2c9799f507e13e4afef546d9c181028e0d785..db2fa279024bcae8be1700edb36cb7fea624164a 100644
--- a/3rdparty/MDB2/Driver/Datatype/pgsql.php
+++ b/3rdparty/MDB2/Driver/Datatype/pgsql.php
@@ -1,575 +1,579 @@
- |
-// +----------------------------------------------------------------------+
-//
-// $Id: pgsql.php 298763 2010-04-29 08:49:41Z afz $
-
-require_once 'MDB2/Driver/Datatype/Common.php';
-
-/**
- * MDB2 PostGreSQL driver
- *
- * @package MDB2
- * @category Database
- * @author Paul Cooper
- */
-class MDB2_Driver_Datatype_pgsql extends MDB2_Driver_Datatype_Common
-{
- // {{{ _baseConvertResult()
-
- /**
- * General type conversion method
- *
- * @param mixed $value refernce to a value to be converted
- * @param string $type specifies which type to convert to
- * @param boolean $rtrim [optional] when TRUE [default], apply rtrim() to text
- * @return object a MDB2 error on failure
- * @access protected
- */
- function _baseConvertResult($value, $type, $rtrim = true)
- {
- if (null === $value) {
- return null;
- }
- switch ($type) {
- case 'boolean':
- return $value == 't';
- case 'float':
- return doubleval($value);
- case 'date':
- return $value;
- case 'time':
- return substr($value, 0, strlen('HH:MM:SS'));
- case 'timestamp':
- return substr($value, 0, strlen('YYYY-MM-DD HH:MM:SS'));
- case 'blob':
- $value = pg_unescape_bytea($value);
- return parent::_baseConvertResult($value, $type, $rtrim);
- }
- return parent::_baseConvertResult($value, $type, $rtrim);
- }
-
- // }}}
- // {{{ 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':
- $length = !empty($field['length']) ? $field['length'] : false;
- $fixed = !empty($field['fixed']) ? $field['fixed'] : false;
- return $fixed ? ($length ? 'CHAR('.$length.')' : 'CHAR('.$db->options['default_text_field_length'].')')
- : ($length ? 'VARCHAR('.$length.')' : 'TEXT');
- case 'clob':
- return 'TEXT';
- case 'blob':
- return 'BYTEA';
- case 'integer':
- if (!empty($field['autoincrement'])) {
- if (!empty($field['length'])) {
- $length = $field['length'];
- if ($length > 4) {
- return 'BIGSERIAL PRIMARY KEY';
- }
- }
- return 'SERIAL PRIMARY KEY';
- }
- if (!empty($field['length'])) {
- $length = $field['length'];
- if ($length <= 2) {
- return 'SMALLINT';
- } elseif ($length == 3 || $length == 4) {
- return 'INT';
- } elseif ($length > 4) {
- return 'BIGINT';
- }
- }
- return 'INT';
- case 'boolean':
- return 'BOOLEAN';
- case 'date':
- return 'DATE';
- case 'time':
- return 'TIME without time zone';
- case 'timestamp':
- return 'TIMESTAMP without time zone';
- case 'float':
- return 'FLOAT8';
- case 'decimal':
- $length = !empty($field['length']) ? $field['length'] : 18;
- $scale = !empty($field['scale']) ? $field['scale'] : $db->options['decimal_places'];
- return 'NUMERIC('.$length.','.$scale.')';
- }
- }
-
- // }}}
- // {{{ _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 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:
- *
- * 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;
- }
-
- if (!empty($field['unsigned'])) {
- $db->warnings[] = "unsigned integer field \"$name\" is being declared as signed integer";
- }
- if (!empty($field['autoincrement'])) {
- $name = $db->quoteIdentifier($name, true);
- return $name.' '.$this->getTypeDeclaration($field);
- }
- $default = '';
- if (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';
- if (empty($default) && empty($notnull)) {
- $default = ' DEFAULT NULL';
- }
- $name = $db->quoteIdentifier($name, true);
- return $name.' '.$this->getTypeDeclaration($field).$default.$notnull;
- }
-
- // }}}
- // {{{ _quoteCLOB()
-
- /**
- * Convert a text value into a DBMS specific format that is suitable to
- * compose query statements.
- *
- * @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 $escape_wildcards if to escape escape wildcards
- * @return string text string that represents the given argument value in
- * a DBMS specific format.
- * @access protected
- */
- function _quoteCLOB($value, $quote, $escape_wildcards)
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
- $value = $this->_readFile($value, $db->options['lob_allow_url_include']);
- if (PEAR::isError($value)) {
- return $value;
- }
- return $this->_quoteText($value, $quote, $escape_wildcards);
- }
-
- // }}}
- // {{{ _quoteBLOB()
-
- /**
- * Convert a text value into a DBMS specific format that is suitable to
- * compose query statements.
- *
- * @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 $escape_wildcards if to escape escape wildcards
- * @return string text string that represents the given argument value in
- * a DBMS specific format.
- * @access protected
- */
- function _quoteBLOB($value, $quote, $escape_wildcards)
- {
- if (!$quote) {
- return $value;
- }
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
- $value = $this->_readFile($value, $db->options['lob_allow_url_include']);
- if (PEAR::isError($value)) {
- return $value;
- }
- if (version_compare(PHP_VERSION, '5.2.0RC6', '>=')) {
- $connection = $db->getConnection();
- if (PEAR::isError($connection)) {
- return $connection;
- }
- $value = @pg_escape_bytea($connection, $value);
- } else {
- $value = @pg_escape_bytea($value);
- }
- return "'".$value."'";
- }
-
- // }}}
- // {{{ _quoteBoolean()
-
- /**
- * Convert a text value into a DBMS specific format that is suitable to
- * compose query statements.
- *
- * @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 $escape_wildcards if to escape escape wildcards
- * @return string text string that represents the given argument value in
- * a DBMS specific format.
- * @access protected
- */
- function _quoteBoolean($value, $quote, $escape_wildcards)
- {
- $value = $value ? 't' : 'f';
- if (!$quote) {
- return $value;
- }
- return "'".$value."'";
- }
-
- // }}}
- // {{{ 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.'ILIKE ';
- break;
- case 'NOT ILIKE':
- $match = $field.'NOT ILIKE ';
- 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;
- }
-
- // }}}
- // {{{ patternEscapeString()
-
- /**
- * build string to define escape pattern string
- *
- * @access public
- *
- *
- * @return string define escape pattern
- */
- function patternEscapeString()
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
- return ' ESCAPE '.$this->quote($db->string_quoting['escape_pattern']);
- }
-
- // }}}
- // {{{ _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']);
- $length = $field['length'];
- $type = array();
- $unsigned = $fixed = null;
- switch ($db_type) {
- case 'smallint':
- case 'int2':
- $type[] = 'integer';
- $unsigned = false;
- $length = 2;
- if ($length == '2') {
- $type[] = 'boolean';
- if (preg_match('/^(is|has)/', $field['name'])) {
- $type = array_reverse($type);
- }
- }
- break;
- case 'int':
- case 'int4':
- case 'integer':
- case 'serial':
- case 'serial4':
- $type[] = 'integer';
- $unsigned = false;
- $length = 4;
- break;
- case 'bigint':
- case 'int8':
- case 'bigserial':
- case 'serial8':
- $type[] = 'integer';
- $unsigned = false;
- $length = 8;
- break;
- case 'bool':
- case 'boolean':
- $type[] = 'boolean';
- $length = null;
- break;
- case 'text':
- case 'varchar':
- $fixed = false;
- case 'unknown':
- case 'char':
- case 'bpchar':
- $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';
- $type = array_reverse($type);
- }
- if ($fixed !== false) {
- $fixed = true;
- }
- break;
- case 'date':
- $type[] = 'date';
- $length = null;
- break;
- case 'datetime':
- case 'timestamp':
- case 'timestamptz':
- $type[] = 'timestamp';
- $length = null;
- break;
- case 'time':
- $type[] = 'time';
- $length = null;
- break;
- case 'float':
- case 'float4':
- case 'float8':
- case 'double':
- case 'real':
- $type[] = 'float';
- break;
- case 'decimal':
- case 'money':
- case 'numeric':
- $type[] = 'decimal';
- if (isset($field['scale'])) {
- $length = $length.','.$field['scale'];
- }
- break;
- case 'tinyblob':
- case 'mediumblob':
- case 'longblob':
- case 'blob':
- case 'bytea':
- $type[] = 'blob';
- $length = null;
- break;
- case 'oid':
- $type[] = 'blob';
- $type[] = 'clob';
- $length = null;
- 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);
- }
-
- // }}}
- // {{{ mapPrepareDatatype()
-
- /**
- * Maps an mdb2 datatype to native prepare type
- *
- * @param string $type
- * @return string
- * @access public
- */
- function mapPrepareDatatype($type)
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- if (!empty($db->options['datatype_map'][$type])) {
- $type = $db->options['datatype_map'][$type];
- if (!empty($db->options['datatype_map_callback'][$type])) {
- $parameter = array('type' => $type);
- return call_user_func_array($db->options['datatype_map_callback'][$type], array(&$db, __FUNCTION__, $parameter));
- }
- }
-
- switch ($type) {
- case 'integer':
- return 'int';
- case 'boolean':
- return 'bool';
- case 'decimal':
- case 'float':
- return 'numeric';
- case 'clob':
- return 'text';
- case 'blob':
- return 'bytea';
- default:
- break;
- }
- return $type;
- }
- // }}}
-}
+ |
+// +----------------------------------------------------------------------+
+//
+// $Id$
+
+require_once 'MDB2/Driver/Datatype/Common.php';
+
+/**
+ * MDB2 PostGreSQL driver
+ *
+ * @package MDB2
+ * @category Database
+ * @author Paul Cooper
+ */
+class MDB2_Driver_Datatype_pgsql extends MDB2_Driver_Datatype_Common
+{
+ // {{{ _baseConvertResult()
+
+ /**
+ * General type conversion method
+ *
+ * @param mixed $value refernce to a value to be converted
+ * @param string $type specifies which type to convert to
+ * @param boolean $rtrim [optional] when TRUE [default], apply rtrim() to text
+ * @return object a MDB2 error on failure
+ * @access protected
+ */
+ function _baseConvertResult($value, $type, $rtrim = true)
+ {
+ if (null === $value) {
+ return null;
+ }
+ switch ($type) {
+ case 'boolean':
+ return $value == 't';
+ case 'float':
+ return doubleval($value);
+ case 'date':
+ return $value;
+ case 'time':
+ return substr($value, 0, strlen('HH:MM:SS'));
+ case 'timestamp':
+ return substr($value, 0, strlen('YYYY-MM-DD HH:MM:SS'));
+ case 'blob':
+ $value = pg_unescape_bytea($value);
+ return parent::_baseConvertResult($value, $type, $rtrim);
+ }
+ return parent::_baseConvertResult($value, $type, $rtrim);
+ }
+
+ // }}}
+ // {{{ 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':
+ $length = !empty($field['length']) ? $field['length'] : false;
+ $fixed = !empty($field['fixed']) ? $field['fixed'] : false;
+ return $fixed ? ($length ? 'CHAR('.$length.')' : 'CHAR('.$db->options['default_text_field_length'].')')
+ : ($length ? 'VARCHAR('.$length.')' : 'TEXT');
+ case 'clob':
+ return 'TEXT';
+ case 'blob':
+ return 'BYTEA';
+ case 'integer':
+ if (!empty($field['autoincrement'])) {
+ if (!empty($field['length'])) {
+ $length = $field['length'];
+ if ($length > 4) {
+ return 'BIGSERIAL PRIMARY KEY';
+ }
+ }
+ return 'SERIAL PRIMARY KEY';
+ }
+ if (!empty($field['length'])) {
+ $length = $field['length'];
+ if ($length <= 2) {
+ return 'SMALLINT';
+ } elseif ($length == 3 || $length == 4) {
+ return 'INT';
+ } elseif ($length > 4) {
+ return 'BIGINT';
+ }
+ }
+ return 'INT';
+ case 'boolean':
+ return 'BOOLEAN';
+ case 'date':
+ return 'DATE';
+ case 'time':
+ return 'TIME without time zone';
+ case 'timestamp':
+ return 'TIMESTAMP without time zone';
+ case 'float':
+ return 'FLOAT8';
+ case 'decimal':
+ $length = !empty($field['length']) ? $field['length'] : 18;
+ $scale = !empty($field['scale']) ? $field['scale'] : $db->options['decimal_places'];
+ return 'NUMERIC('.$length.','.$scale.')';
+ }
+ }
+
+ // }}}
+ // {{{ _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 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:
+ *
+ * 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;
+ }
+
+ if (!empty($field['unsigned'])) {
+ $db->warnings[] = "unsigned integer field \"$name\" is being declared as signed integer";
+ }
+ if (!empty($field['autoincrement'])) {
+ $name = $db->quoteIdentifier($name, true);
+ return $name.' '.$this->getTypeDeclaration($field);
+ }
+ $default = '';
+ if (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';
+ if (empty($default) && empty($notnull)) {
+ $default = ' DEFAULT NULL';
+ }
+ $name = $db->quoteIdentifier($name, true);
+ return $name.' '.$this->getTypeDeclaration($field).$default.$notnull;
+ }
+
+ // }}}
+ // {{{ _quoteCLOB()
+
+ /**
+ * Convert a text value into a DBMS specific format that is suitable to
+ * compose query statements.
+ *
+ * @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 $escape_wildcards if to escape escape wildcards
+ * @return string text string that represents the given argument value in
+ * a DBMS specific format.
+ * @access protected
+ */
+ function _quoteCLOB($value, $quote, $escape_wildcards)
+ {
+ $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);
+ }
+
+ // }}}
+ // {{{ _quoteBLOB()
+
+ /**
+ * Convert a text value into a DBMS specific format that is suitable to
+ * compose query statements.
+ *
+ * @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 $escape_wildcards if to escape escape wildcards
+ * @return string text string that represents the given argument value in
+ * a DBMS specific format.
+ * @access protected
+ */
+ function _quoteBLOB($value, $quote, $escape_wildcards)
+ {
+ if (!$quote) {
+ 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;
+ }
+ }
+ if (version_compare(PHP_VERSION, '5.2.0RC6', '>=')) {
+ $connection = $db->getConnection();
+ if (PEAR::isError($connection)) {
+ return $connection;
+ }
+ $value = @pg_escape_bytea($connection, $value);
+ } else {
+ $value = @pg_escape_bytea($value);
+ }
+ return "'".$value."'";
+ }
+
+ // }}}
+ // {{{ _quoteBoolean()
+
+ /**
+ * Convert a text value into a DBMS specific format that is suitable to
+ * compose query statements.
+ *
+ * @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 $escape_wildcards if to escape escape wildcards
+ * @return string text string that represents the given argument value in
+ * a DBMS specific format.
+ * @access protected
+ */
+ function _quoteBoolean($value, $quote, $escape_wildcards)
+ {
+ $value = $value ? 't' : 'f';
+ if (!$quote) {
+ return $value;
+ }
+ return "'".$value."'";
+ }
+
+ // }}}
+ // {{{ 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.'ILIKE ';
+ break;
+ case 'NOT ILIKE':
+ $match = $field.'NOT ILIKE ';
+ 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;
+ }
+
+ // }}}
+ // {{{ patternEscapeString()
+
+ /**
+ * build string to define escape pattern string
+ *
+ * @access public
+ *
+ *
+ * @return string define escape pattern
+ */
+ function patternEscapeString()
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+ return ' ESCAPE '.$this->quote($db->string_quoting['escape_pattern']);
+ }
+
+ // }}}
+ // {{{ _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']);
+ $length = $field['length'];
+ $type = array();
+ $unsigned = $fixed = null;
+ switch ($db_type) {
+ case 'smallint':
+ case 'int2':
+ $type[] = 'integer';
+ $unsigned = false;
+ $length = 2;
+ if ($length == '2') {
+ $type[] = 'boolean';
+ if (preg_match('/^(is|has)/', $field['name'])) {
+ $type = array_reverse($type);
+ }
+ }
+ break;
+ case 'int':
+ case 'int4':
+ case 'integer':
+ case 'serial':
+ case 'serial4':
+ $type[] = 'integer';
+ $unsigned = false;
+ $length = 4;
+ break;
+ case 'bigint':
+ case 'int8':
+ case 'bigserial':
+ case 'serial8':
+ $type[] = 'integer';
+ $unsigned = false;
+ $length = 8;
+ break;
+ case 'bool':
+ case 'boolean':
+ $type[] = 'boolean';
+ $length = null;
+ break;
+ case 'text':
+ case 'varchar':
+ $fixed = false;
+ case 'unknown':
+ case 'char':
+ case 'bpchar':
+ $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';
+ $type = array_reverse($type);
+ }
+ if ($fixed !== false) {
+ $fixed = true;
+ }
+ break;
+ case 'date':
+ $type[] = 'date';
+ $length = null;
+ break;
+ case 'datetime':
+ case 'timestamp':
+ case 'timestamptz':
+ $type[] = 'timestamp';
+ $length = null;
+ break;
+ case 'time':
+ $type[] = 'time';
+ $length = null;
+ break;
+ case 'float':
+ case 'float4':
+ case 'float8':
+ case 'double':
+ case 'real':
+ $type[] = 'float';
+ break;
+ case 'decimal':
+ case 'money':
+ case 'numeric':
+ $type[] = 'decimal';
+ if (isset($field['scale'])) {
+ $length = $length.','.$field['scale'];
+ }
+ break;
+ case 'tinyblob':
+ case 'mediumblob':
+ case 'longblob':
+ case 'blob':
+ case 'bytea':
+ $type[] = 'blob';
+ $length = null;
+ break;
+ case 'oid':
+ $type[] = 'blob';
+ $type[] = 'clob';
+ $length = null;
+ 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);
+ }
+
+ // }}}
+ // {{{ mapPrepareDatatype()
+
+ /**
+ * Maps an mdb2 datatype to native prepare type
+ *
+ * @param string $type
+ * @return string
+ * @access public
+ */
+ function mapPrepareDatatype($type)
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ if (!empty($db->options['datatype_map'][$type])) {
+ $type = $db->options['datatype_map'][$type];
+ if (!empty($db->options['datatype_map_callback'][$type])) {
+ $parameter = array('type' => $type);
+ return call_user_func_array($db->options['datatype_map_callback'][$type], array(&$db, __FUNCTION__, $parameter));
+ }
+ }
+
+ switch ($type) {
+ case 'integer':
+ return 'int';
+ case 'boolean':
+ return 'bool';
+ case 'decimal':
+ case 'float':
+ return 'numeric';
+ case 'clob':
+ return 'text';
+ case 'blob':
+ return 'bytea';
+ default:
+ break;
+ }
+ return $type;
+ }
+ // }}}
+}
?>
\ No newline at end of file
diff --git a/3rdparty/MDB2/Driver/Datatype/sqlite.php b/3rdparty/MDB2/Driver/Datatype/sqlite.php
index e4711e46b32d4214de0301de80bcc8e71519d3b9..50475a36282e97db22b3cb21f39ad18bde901fde 100644
--- a/3rdparty/MDB2/Driver/Datatype/sqlite.php
+++ b/3rdparty/MDB2/Driver/Datatype/sqlite.php
@@ -1,418 +1,418 @@
- |
-// +----------------------------------------------------------------------+
-//
-// $Id: sqlite.php 295587 2010-02-28 17:16:38Z quipo $
-//
-
-require_once 'MDB2/Driver/Datatype/Common.php';
-
-/**
- * MDB2 SQLite driver
- *
- * @package MDB2
- * @category Database
- * @author Lukas Smith
- */
-class MDB2_Driver_Datatype_sqlite extends MDB2_Driver_Datatype_Common
-{
- // {{{ _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':
- $length = !empty($field['length'])
- ? $field['length'] : false;
- $fixed = !empty($field['fixed']) ? $field['fixed'] : false;
- return $fixed ? ($length ? 'CHAR('.$length.')' : 'CHAR('.$db->options['default_text_field_length'].')')
- : ($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 <= 2) {
- return 'SMALLINT';
- } elseif ($length == 3 || $length == 4) {
- return 'INTEGER';
- } elseif ($length > 4) {
- return 'BIGINT';
- }
- }
- return 'INTEGER';
- case 'boolean':
- return 'BOOLEAN';
- case 'date':
- return 'DATE';
- case 'time':
- return 'TIME';
- case 'timestamp':
- return 'DATETIME';
- case 'float':
- return 'DOUBLE'.($db->options['fixed_float'] ? '('.
- ($db->options['fixed_float']+2).','.$db->options['fixed_float'].')' : '');
- 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 = ' 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';
- if (empty($default) && empty($notnull)) {
- $default = ' DEFAULT NULL';
- }
- $name = $db->quoteIdentifier($name, true);
- return $name.' '.$this->getTypeDeclaration($field).$unsigned.$default.$notnull.$autoinc;
- }
-
- // }}}
- // {{{ 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()
-
- /**
- * 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']);
- $length = !empty($field['length']) ? $field['length'] : null;
- $unsigned = !empty($field['unsigned']) ? $field['unsigned'] : null;
- $fixed = null;
- $type = array();
- switch ($db_type) {
- case 'boolean':
- $type[] = 'boolean';
- break;
- 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':
- case 'serial':
- $type[] = 'integer';
- $unsigned = preg_match('/ unsigned/i', $field['type']);
- $length = 4;
- break;
- case 'bigint':
- case 'bigserial':
- $type[] = 'integer';
- $unsigned = preg_match('/ unsigned/i', $field['type']);
- $length = 8;
- break;
- case 'clob':
- $type[] = 'clob';
- $fixed = false;
- break;
- case 'tinytext':
- case 'mediumtext':
- case 'longtext':
- case 'text':
- case 'varchar':
- case 'varchar2':
- $fixed = false;
- 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';
- $type = array_reverse($type);
- }
- if ($fixed !== false) {
- $fixed = true;
- }
- 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';
- break;
- case 'decimal':
- case 'numeric':
- $type[] = 'decimal';
- $length = $length.','.$field['decimal'];
- break;
- case 'tinyblob':
- case 'mediumblob':
- case 'longblob':
- case 'blob':
- $type[] = 'blob';
- $length = null;
- 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);
- }
-
- // }}}
-}
-
+ |
+// +----------------------------------------------------------------------+
+//
+// $Id$
+//
+
+require_once 'MDB2/Driver/Datatype/Common.php';
+
+/**
+ * MDB2 SQLite driver
+ *
+ * @package MDB2
+ * @category Database
+ * @author Lukas Smith
+ */
+class MDB2_Driver_Datatype_sqlite extends MDB2_Driver_Datatype_Common
+{
+ // {{{ _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':
+ $length = !empty($field['length'])
+ ? $field['length'] : false;
+ $fixed = !empty($field['fixed']) ? $field['fixed'] : false;
+ return $fixed ? ($length ? 'CHAR('.$length.')' : 'CHAR('.$db->options['default_text_field_length'].')')
+ : ($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 <= 2) {
+ return 'SMALLINT';
+ } elseif ($length == 3 || $length == 4) {
+ return 'INTEGER';
+ } elseif ($length > 4) {
+ return 'BIGINT';
+ }
+ }
+ return 'INTEGER';
+ case 'boolean':
+ return 'BOOLEAN';
+ case 'date':
+ return 'DATE';
+ case 'time':
+ return 'TIME';
+ case 'timestamp':
+ return 'DATETIME';
+ case 'float':
+ return 'DOUBLE'.($db->options['fixed_float'] ? '('.
+ ($db->options['fixed_float']+2).','.$db->options['fixed_float'].')' : '');
+ 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 = ' 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';
+ if (empty($default) && empty($notnull)) {
+ $default = ' DEFAULT NULL';
+ }
+ $name = $db->quoteIdentifier($name, true);
+ return $name.' '.$this->getTypeDeclaration($field).$unsigned.$default.$notnull.$autoinc;
+ }
+
+ // }}}
+ // {{{ 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()
+
+ /**
+ * 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']);
+ $length = !empty($field['length']) ? $field['length'] : null;
+ $unsigned = !empty($field['unsigned']) ? $field['unsigned'] : null;
+ $fixed = null;
+ $type = array();
+ switch ($db_type) {
+ case 'boolean':
+ $type[] = 'boolean';
+ break;
+ 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':
+ case 'serial':
+ $type[] = 'integer';
+ $unsigned = preg_match('/ unsigned/i', $field['type']);
+ $length = 4;
+ break;
+ case 'bigint':
+ case 'bigserial':
+ $type[] = 'integer';
+ $unsigned = preg_match('/ unsigned/i', $field['type']);
+ $length = 8;
+ break;
+ case 'clob':
+ $type[] = 'clob';
+ $fixed = false;
+ break;
+ case 'tinytext':
+ case 'mediumtext':
+ case 'longtext':
+ case 'text':
+ case 'varchar':
+ case 'varchar2':
+ $fixed = false;
+ 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';
+ $type = array_reverse($type);
+ }
+ if ($fixed !== false) {
+ $fixed = true;
+ }
+ 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';
+ break;
+ case 'decimal':
+ case 'numeric':
+ $type[] = 'decimal';
+ $length = $length.','.$field['decimal'];
+ break;
+ case 'tinyblob':
+ case 'mediumblob':
+ case 'longblob':
+ case 'blob':
+ $type[] = 'blob';
+ $length = null;
+ 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);
+ }
+
+ // }}}
+}
+
?>
\ No newline at end of file
diff --git a/3rdparty/MDB2/Driver/Function/Common.php b/3rdparty/MDB2/Driver/Function/Common.php
index 5d247bec3599ea09e9095c5c38e7b1e4f194f38f..5a780fd48e851d5027b55217a696661e0e33dce6 100644
--- a/3rdparty/MDB2/Driver/Function/Common.php
+++ b/3rdparty/MDB2/Driver/Function/Common.php
@@ -1,293 +1,293 @@
- |
-// +----------------------------------------------------------------------+
-//
-// $Id: Common.php 295587 2010-02-28 17:16:38Z quipo $
-//
-
-/**
- * @package MDB2
- * @category Database
- * @author Lukas Smith
- */
-
-/**
- * Base class for the function modules that is extended by each MDB2 driver
- *
- * To load this module in the MDB2 object:
- * $mdb->loadModule('Function');
- *
- * @package MDB2
- * @category Database
- * @author Lukas Smith
- */
-class MDB2_Driver_Function_Common extends MDB2_Module_Common
-{
- // {{{ executeStoredProc()
-
- /**
- * Execute a stored procedure and return any results
- *
- * @param string $name string that identifies the function to execute
- * @param mixed $params array that contains the paramaters to pass the stored proc
- * @param mixed $types array that contains the types of the columns in
- * the result set
- * @param mixed $result_class string which specifies which result class to use
- * @param mixed $result_wrap_class string which specifies which class to wrap results in
- *
- * @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure
- * @access public
- */
- function executeStoredProc($name, $params = null, $types = null, $result_class = true, $result_wrap_class = false)
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- $error = $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
- 'method not implemented', __FUNCTION__);
- return $error;
- }
-
- // }}}
- // {{{ functionTable()
-
- /**
- * return string for internal table used when calling only a function
- *
- * @return string for internal table used when calling only a function
- * @access public
- */
- function functionTable()
- {
- return '';
- }
-
- // }}}
- // {{{ now()
-
- /**
- * Return string to call a variable with the current timestamp inside an SQL statement
- * There are three special variables for current date and time:
- * - CURRENT_TIMESTAMP (date and time, TIMESTAMP type)
- * - CURRENT_DATE (date, DATE type)
- * - CURRENT_TIME (time, TIME type)
- *
- * @param string $type 'timestamp' | 'time' | 'date'
- *
- * @return string to call a variable with the current timestamp
- * @access public
- */
- function now($type = 'timestamp')
- {
- switch ($type) {
- case 'time':
- return 'CURRENT_TIME';
- case 'date':
- return 'CURRENT_DATE';
- case 'timestamp':
- default:
- return 'CURRENT_TIMESTAMP';
- }
- }
-
- // }}}
- // {{{ unixtimestamp()
-
- /**
- * return string to call a function to get the unix timestamp from a iso timestamp
- *
- * @param string $expression
- *
- * @return string to call a variable with the timestamp
- * @access public
- */
- function unixtimestamp($expression)
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- $error = $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
- 'method not implemented', __FUNCTION__);
- return $error;
- }
-
- // }}}
- // {{{ substring()
-
- /**
- * return string to call a function to get a substring inside an SQL statement
- *
- * @return string to call a function to get a substring
- * @access public
- */
- function substring($value, $position = 1, $length = null)
- {
- if (null !== $length) {
- return "SUBSTRING($value FROM $position FOR $length)";
- }
- return "SUBSTRING($value FROM $position)";
- }
-
- // }}}
- // {{{ replace()
-
- /**
- * return string to call a function to get replace inside an SQL statement.
- *
- * @return string to call a function to get a replace
- * @access public
- */
- function replace($str, $from_str, $to_str)
- {
- return "REPLACE($str, $from_str , $to_str)";
- }
-
- // }}}
- // {{{ concat()
-
- /**
- * Returns string to concatenate two or more string parameters
- *
- * @param string $value1
- * @param string $value2
- * @param string $values...
- *
- * @return string to concatenate two strings
- * @access public
- */
- function concat($value1, $value2)
- {
- $args = func_get_args();
- return "(".implode(' || ', $args).")";
- }
-
- // }}}
- // {{{ random()
-
- /**
- * return string to call a function to get random value inside an SQL statement
- *
- * @return return string to generate float between 0 and 1
- * @access public
- */
- function random()
- {
- return 'RAND()';
- }
-
- // }}}
- // {{{ lower()
-
- /**
- * return string to call a function to lower the case of an expression
- *
- * @param string $expression
- *
- * @return return string to lower case of an expression
- * @access public
- */
- function lower($expression)
- {
- return "LOWER($expression)";
- }
-
- // }}}
- // {{{ upper()
-
- /**
- * return string to call a function to upper the case of an expression
- *
- * @param string $expression
- *
- * @return return string to upper case of an expression
- * @access public
- */
- function upper($expression)
- {
- return "UPPER($expression)";
- }
-
- // }}}
- // {{{ length()
-
- /**
- * return string to call a function to get the length of a string expression
- *
- * @param string $expression
- *
- * @return return string to get the string expression length
- * @access public
- */
- function length($expression)
- {
- return "LENGTH($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;
- }
-
- // }}}
-}
+ |
+// +----------------------------------------------------------------------+
+//
+// $Id$
+//
+
+/**
+ * @package MDB2
+ * @category Database
+ * @author Lukas Smith
+ */
+
+/**
+ * Base class for the function modules that is extended by each MDB2 driver
+ *
+ * To load this module in the MDB2 object:
+ * $mdb->loadModule('Function');
+ *
+ * @package MDB2
+ * @category Database
+ * @author Lukas Smith
+ */
+class MDB2_Driver_Function_Common extends MDB2_Module_Common
+{
+ // {{{ executeStoredProc()
+
+ /**
+ * Execute a stored procedure and return any results
+ *
+ * @param string $name string that identifies the function to execute
+ * @param mixed $params array that contains the paramaters to pass the stored proc
+ * @param mixed $types array that contains the types of the columns in
+ * the result set
+ * @param mixed $result_class string which specifies which result class to use
+ * @param mixed $result_wrap_class string which specifies which class to wrap results in
+ *
+ * @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure
+ * @access public
+ */
+ function executeStoredProc($name, $params = null, $types = null, $result_class = true, $result_wrap_class = false)
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ $error = $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ return $error;
+ }
+
+ // }}}
+ // {{{ functionTable()
+
+ /**
+ * return string for internal table used when calling only a function
+ *
+ * @return string for internal table used when calling only a function
+ * @access public
+ */
+ function functionTable()
+ {
+ return '';
+ }
+
+ // }}}
+ // {{{ now()
+
+ /**
+ * Return string to call a variable with the current timestamp inside an SQL statement
+ * There are three special variables for current date and time:
+ * - CURRENT_TIMESTAMP (date and time, TIMESTAMP type)
+ * - CURRENT_DATE (date, DATE type)
+ * - CURRENT_TIME (time, TIME type)
+ *
+ * @param string $type 'timestamp' | 'time' | 'date'
+ *
+ * @return string to call a variable with the current timestamp
+ * @access public
+ */
+ function now($type = 'timestamp')
+ {
+ switch ($type) {
+ case 'time':
+ return 'CURRENT_TIME';
+ case 'date':
+ return 'CURRENT_DATE';
+ case 'timestamp':
+ default:
+ return 'CURRENT_TIMESTAMP';
+ }
+ }
+
+ // }}}
+ // {{{ unixtimestamp()
+
+ /**
+ * return string to call a function to get the unix timestamp from a iso timestamp
+ *
+ * @param string $expression
+ *
+ * @return string to call a variable with the timestamp
+ * @access public
+ */
+ function unixtimestamp($expression)
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ $error = $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ return $error;
+ }
+
+ // }}}
+ // {{{ substring()
+
+ /**
+ * return string to call a function to get a substring inside an SQL statement
+ *
+ * @return string to call a function to get a substring
+ * @access public
+ */
+ function substring($value, $position = 1, $length = null)
+ {
+ if (null !== $length) {
+ return "SUBSTRING($value FROM $position FOR $length)";
+ }
+ return "SUBSTRING($value FROM $position)";
+ }
+
+ // }}}
+ // {{{ replace()
+
+ /**
+ * return string to call a function to get replace inside an SQL statement.
+ *
+ * @return string to call a function to get a replace
+ * @access public
+ */
+ function replace($str, $from_str, $to_str)
+ {
+ return "REPLACE($str, $from_str , $to_str)";
+ }
+
+ // }}}
+ // {{{ concat()
+
+ /**
+ * Returns string to concatenate two or more string parameters
+ *
+ * @param string $value1
+ * @param string $value2
+ * @param string $values...
+ *
+ * @return string to concatenate two strings
+ * @access public
+ */
+ function concat($value1, $value2)
+ {
+ $args = func_get_args();
+ return "(".implode(' || ', $args).")";
+ }
+
+ // }}}
+ // {{{ random()
+
+ /**
+ * return string to call a function to get random value inside an SQL statement
+ *
+ * @return return string to generate float between 0 and 1
+ * @access public
+ */
+ function random()
+ {
+ return 'RAND()';
+ }
+
+ // }}}
+ // {{{ lower()
+
+ /**
+ * return string to call a function to lower the case of an expression
+ *
+ * @param string $expression
+ *
+ * @return return string to lower case of an expression
+ * @access public
+ */
+ function lower($expression)
+ {
+ return "LOWER($expression)";
+ }
+
+ // }}}
+ // {{{ upper()
+
+ /**
+ * return string to call a function to upper the case of an expression
+ *
+ * @param string $expression
+ *
+ * @return return string to upper case of an expression
+ * @access public
+ */
+ function upper($expression)
+ {
+ return "UPPER($expression)";
+ }
+
+ // }}}
+ // {{{ length()
+
+ /**
+ * return string to call a function to get the length of a string expression
+ *
+ * @param string $expression
+ *
+ * @return return string to get the string expression length
+ * @access public
+ */
+ function length($expression)
+ {
+ return "LENGTH($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;
+ }
+
+ // }}}
+}
?>
\ No newline at end of file
diff --git a/3rdparty/MDB2/Driver/Function/mysql.php b/3rdparty/MDB2/Driver/Function/mysql.php
index bf91536e1f39a1289b739d56c23bc26cbc4be4de..90fdafc973c1e869137d6c5f9a7d7e3175e4b501 100644
--- a/3rdparty/MDB2/Driver/Function/mysql.php
+++ b/3rdparty/MDB2/Driver/Function/mysql.php
@@ -1,136 +1,136 @@
- |
-// +----------------------------------------------------------------------+
-//
-// $Id: mysql.php 295587 2010-02-28 17:16:38Z quipo $
-//
-
-require_once 'MDB2/Driver/Function/Common.php';
-
-/**
- * MDB2 MySQL driver for the function modules
- *
- * @package MDB2
- * @category Database
- * @author Lukas Smith
- */
-class MDB2_Driver_Function_mysql extends MDB2_Driver_Function_Common
-{
- // }}}
- // {{{ executeStoredProc()
-
- /**
- * Execute a stored procedure and return any results
- *
- * @param string $name string that identifies the function to execute
- * @param mixed $params array that contains the paramaters to pass the stored proc
- * @param mixed $types array that contains the types of the columns in
- * the result set
- * @param mixed $result_class string which specifies which result class to use
- * @param mixed $result_wrap_class string which specifies which class to wrap results in
- * @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure
- * @access public
- */
- function executeStoredProc($name, $params = null, $types = null, $result_class = true, $result_wrap_class = false)
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- $query = 'CALL '.$name;
- $query .= $params ? '('.implode(', ', $params).')' : '()';
- return $db->query($query, $types, $result_class, $result_wrap_class);
- }
-
- // }}}
- // {{{ unixtimestamp()
-
- /**
- * return string to call a function to get the unix timestamp from a iso timestamp
- *
- * @param string $expression
- *
- * @return string to call a variable with the timestamp
- * @access public
- */
- function unixtimestamp($expression)
- {
- return 'UNIX_TIMESTAMP('. $expression.')';
- }
-
- // }}}
- // {{{ concat()
-
- /**
- * Returns string to concatenate two or more string parameters
- *
- * @param string $value1
- * @param string $value2
- * @param string $values...
- * @return string to concatenate two strings
- * @access public
- **/
- function concat($value1, $value2)
- {
- $args = func_get_args();
- return "CONCAT(".implode(', ', $args).")";
- }
-
- // }}}
- // {{{ guid()
-
- /**
- * Returns global unique identifier
- *
- * @return string to get global unique identifier
- * @access public
- */
- function guid()
- {
- return 'UUID()';
- }
-
- // }}}
-}
+ |
+// +----------------------------------------------------------------------+
+//
+// $Id$
+//
+
+require_once 'MDB2/Driver/Function/Common.php';
+
+/**
+ * MDB2 MySQL driver for the function modules
+ *
+ * @package MDB2
+ * @category Database
+ * @author Lukas Smith
+ */
+class MDB2_Driver_Function_mysql extends MDB2_Driver_Function_Common
+{
+ // }}}
+ // {{{ executeStoredProc()
+
+ /**
+ * Execute a stored procedure and return any results
+ *
+ * @param string $name string that identifies the function to execute
+ * @param mixed $params array that contains the paramaters to pass the stored proc
+ * @param mixed $types array that contains the types of the columns in
+ * the result set
+ * @param mixed $result_class string which specifies which result class to use
+ * @param mixed $result_wrap_class string which specifies which class to wrap results in
+ * @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure
+ * @access public
+ */
+ function executeStoredProc($name, $params = null, $types = null, $result_class = true, $result_wrap_class = false)
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ $query = 'CALL '.$name;
+ $query .= $params ? '('.implode(', ', $params).')' : '()';
+ return $db->query($query, $types, $result_class, $result_wrap_class);
+ }
+
+ // }}}
+ // {{{ unixtimestamp()
+
+ /**
+ * return string to call a function to get the unix timestamp from a iso timestamp
+ *
+ * @param string $expression
+ *
+ * @return string to call a variable with the timestamp
+ * @access public
+ */
+ function unixtimestamp($expression)
+ {
+ return 'UNIX_TIMESTAMP('. $expression.')';
+ }
+
+ // }}}
+ // {{{ concat()
+
+ /**
+ * Returns string to concatenate two or more string parameters
+ *
+ * @param string $value1
+ * @param string $value2
+ * @param string $values...
+ * @return string to concatenate two strings
+ * @access public
+ **/
+ function concat($value1, $value2)
+ {
+ $args = func_get_args();
+ return "CONCAT(".implode(', ', $args).")";
+ }
+
+ // }}}
+ // {{{ guid()
+
+ /**
+ * Returns global unique identifier
+ *
+ * @return string to get global unique identifier
+ * @access public
+ */
+ function guid()
+ {
+ return 'UUID()';
+ }
+
+ // }}}
+}
?>
\ No newline at end of file
diff --git a/3rdparty/MDB2/Driver/Function/pgsql.php b/3rdparty/MDB2/Driver/Function/pgsql.php
index e232e4feec15108e9f45187f2299b77c2223573c..7cc34a2d7042d52c51bed166e799d53d647fdffe 100644
--- a/3rdparty/MDB2/Driver/Function/pgsql.php
+++ b/3rdparty/MDB2/Driver/Function/pgsql.php
@@ -1,132 +1,132 @@
- |
-// +----------------------------------------------------------------------+
-//
-// $Id: pgsql.php 296139 2010-03-13 04:15:22Z afz $
-
-require_once 'MDB2/Driver/Function/Common.php';
-
-/**
- * MDB2 MySQL driver for the function modules
- *
- * @package MDB2
- * @category Database
- * @author Lukas Smith
- */
-class MDB2_Driver_Function_pgsql extends MDB2_Driver_Function_Common
-{
- // {{{ executeStoredProc()
-
- /**
- * Execute a stored procedure and return any results
- *
- * @param string $name string that identifies the function to execute
- * @param mixed $params array that contains the paramaters to pass the stored proc
- * @param mixed $types array that contains the types of the columns in
- * the result set
- * @param mixed $result_class string which specifies which result class to use
- * @param mixed $result_wrap_class string which specifies which class to wrap results in
- * @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure
- * @access public
- */
- function executeStoredProc($name, $params = null, $types = null, $result_class = true, $result_wrap_class = false)
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- $query = 'SELECT * FROM '.$name;
- $query .= $params ? '('.implode(', ', $params).')' : '()';
- return $db->query($query, $types, $result_class, $result_wrap_class);
- }
- // }}}
- // {{{ unixtimestamp()
-
- /**
- * return string to call a function to get the unix timestamp from a iso timestamp
- *
- * @param string $expression
- *
- * @return string to call a variable with the timestamp
- * @access public
- */
- function unixtimestamp($expression)
- {
- return 'EXTRACT(EPOCH FROM DATE_TRUNC(\'seconds\', CAST ((' . $expression . ') AS TIMESTAMP)))';
- }
-
- // }}}
- // {{{ substring()
-
- /**
- * return string to call a function to get a substring inside an SQL statement
- *
- * @return string to call a function to get a substring
- * @access public
- */
- function substring($value, $position = 1, $length = null)
- {
- if (null !== $length) {
- return "SUBSTRING(CAST($value AS VARCHAR) FROM $position FOR $length)";
- }
- return "SUBSTRING(CAST($value AS VARCHAR) FROM $position)";
- }
-
- // }}}
- // {{{ random()
-
- /**
- * return string to call a function to get random value inside an SQL statement
- *
- * @return return string to generate float between 0 and 1
- * @access public
- */
- function random()
- {
- return 'RANDOM()';
- }
-
- // }}}
-}
+ |
+// +----------------------------------------------------------------------+
+//
+// $Id$
+
+require_once 'MDB2/Driver/Function/Common.php';
+
+/**
+ * MDB2 MySQL driver for the function modules
+ *
+ * @package MDB2
+ * @category Database
+ * @author Lukas Smith
+ */
+class MDB2_Driver_Function_pgsql extends MDB2_Driver_Function_Common
+{
+ // {{{ executeStoredProc()
+
+ /**
+ * Execute a stored procedure and return any results
+ *
+ * @param string $name string that identifies the function to execute
+ * @param mixed $params array that contains the paramaters to pass the stored proc
+ * @param mixed $types array that contains the types of the columns in
+ * the result set
+ * @param mixed $result_class string which specifies which result class to use
+ * @param mixed $result_wrap_class string which specifies which class to wrap results in
+ * @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure
+ * @access public
+ */
+ function executeStoredProc($name, $params = null, $types = null, $result_class = true, $result_wrap_class = false)
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ $query = 'SELECT * FROM '.$name;
+ $query .= $params ? '('.implode(', ', $params).')' : '()';
+ return $db->query($query, $types, $result_class, $result_wrap_class);
+ }
+ // }}}
+ // {{{ unixtimestamp()
+
+ /**
+ * return string to call a function to get the unix timestamp from a iso timestamp
+ *
+ * @param string $expression
+ *
+ * @return string to call a variable with the timestamp
+ * @access public
+ */
+ function unixtimestamp($expression)
+ {
+ return 'EXTRACT(EPOCH FROM DATE_TRUNC(\'seconds\', CAST ((' . $expression . ') AS TIMESTAMP)))';
+ }
+
+ // }}}
+ // {{{ substring()
+
+ /**
+ * return string to call a function to get a substring inside an SQL statement
+ *
+ * @return string to call a function to get a substring
+ * @access public
+ */
+ function substring($value, $position = 1, $length = null)
+ {
+ if (null !== $length) {
+ return "SUBSTRING(CAST($value AS VARCHAR) FROM $position FOR $length)";
+ }
+ return "SUBSTRING(CAST($value AS VARCHAR) FROM $position)";
+ }
+
+ // }}}
+ // {{{ random()
+
+ /**
+ * return string to call a function to get random value inside an SQL statement
+ *
+ * @return return string to generate float between 0 and 1
+ * @access public
+ */
+ function random()
+ {
+ return 'RANDOM()';
+ }
+
+ // }}}
+}
?>
\ No newline at end of file
diff --git a/3rdparty/MDB2/Driver/Function/sqlite.php b/3rdparty/MDB2/Driver/Function/sqlite.php
index 0a46a4f06a5233228c8a9c3d7de957952c2c476f..65ade4fec07f999bcd69661719845a89041e0797 100644
--- a/3rdparty/MDB2/Driver/Function/sqlite.php
+++ b/3rdparty/MDB2/Driver/Function/sqlite.php
@@ -1,162 +1,162 @@
- |
-// +----------------------------------------------------------------------+
-//
-// $Id: sqlite.php 295587 2010-02-28 17:16:38Z quipo $
-//
-
-require_once 'MDB2/Driver/Function/Common.php';
-
-/**
- * MDB2 SQLite driver for the function modules
- *
- * @package MDB2
- * @category Database
- * @author Lukas Smith
- */
-class MDB2_Driver_Function_sqlite extends MDB2_Driver_Function_Common
-{
- // {{{ constructor
-
- /**
- * Constructor
- */
- function __construct($db_index)
- {
- parent::__construct($db_index);
- // create all sorts of UDFs
- }
-
- // {{{ now()
-
- /**
- * Return string to call a variable with the current timestamp inside an SQL statement
- * There are three special variables for current date and time.
- *
- * @return string to call a variable with the current timestamp
- * @access public
- */
- function now($type = 'timestamp')
- {
- switch ($type) {
- case 'time':
- return 'time(\'now\')';
- case 'date':
- return 'date(\'now\')';
- case 'timestamp':
- default:
- return 'datetime(\'now\')';
- }
- }
-
- // }}}
- // {{{ unixtimestamp()
-
- /**
- * return string to call a function to get the unix timestamp from a iso timestamp
- *
- * @param string $expression
- *
- * @return string to call a variable with the timestamp
- * @access public
- */
- function unixtimestamp($expression)
- {
- return 'strftime("%s",'. $expression.', "utc")';
- }
-
- // }}}
- // {{{ substring()
-
- /**
- * return string to call a function to get a substring inside an SQL statement
- *
- * @return string to call a function to get a substring
- * @access public
- */
- function substring($value, $position = 1, $length = null)
- {
- if (null !== $length) {
- return "substr($value, $position, $length)";
- }
- return "substr($value, $position, length($value))";
- }
-
- // }}}
- // {{{ random()
-
- /**
- * return string to call a function to get random value inside an SQL statement
- *
- * @return return string to generate float between 0 and 1
- * @access public
- */
- function random()
- {
- return '((RANDOM()+2147483648)/4294967296)';
- }
-
- // }}}
- // {{{ replace()
-
- /**
- * return string to call a function to get a replacement inside an SQL statement.
- *
- * @return string to call a function to get a replace
- * @access public
- */
- function replace($str, $from_str, $to_str)
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- $error = $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
- 'method not implemented', __FUNCTION__);
- return $error;
- }
-
- // }}}
-}
-?>
+ |
+// +----------------------------------------------------------------------+
+//
+// $Id$
+//
+
+require_once 'MDB2/Driver/Function/Common.php';
+
+/**
+ * MDB2 SQLite driver for the function modules
+ *
+ * @package MDB2
+ * @category Database
+ * @author Lukas Smith
+ */
+class MDB2_Driver_Function_sqlite extends MDB2_Driver_Function_Common
+{
+ // {{{ constructor
+
+ /**
+ * Constructor
+ */
+ function __construct($db_index)
+ {
+ parent::__construct($db_index);
+ // create all sorts of UDFs
+ }
+
+ // {{{ now()
+
+ /**
+ * Return string to call a variable with the current timestamp inside an SQL statement
+ * There are three special variables for current date and time.
+ *
+ * @return string to call a variable with the current timestamp
+ * @access public
+ */
+ function now($type = 'timestamp')
+ {
+ switch ($type) {
+ case 'time':
+ return 'time(\'now\')';
+ case 'date':
+ return 'date(\'now\')';
+ case 'timestamp':
+ default:
+ return 'datetime(\'now\')';
+ }
+ }
+
+ // }}}
+ // {{{ unixtimestamp()
+
+ /**
+ * return string to call a function to get the unix timestamp from a iso timestamp
+ *
+ * @param string $expression
+ *
+ * @return string to call a variable with the timestamp
+ * @access public
+ */
+ function unixtimestamp($expression)
+ {
+ return 'strftime("%s",'. $expression.', "utc")';
+ }
+
+ // }}}
+ // {{{ substring()
+
+ /**
+ * return string to call a function to get a substring inside an SQL statement
+ *
+ * @return string to call a function to get a substring
+ * @access public
+ */
+ function substring($value, $position = 1, $length = null)
+ {
+ if (null !== $length) {
+ return "substr($value, $position, $length)";
+ }
+ return "substr($value, $position, length($value))";
+ }
+
+ // }}}
+ // {{{ random()
+
+ /**
+ * return string to call a function to get random value inside an SQL statement
+ *
+ * @return return string to generate float between 0 and 1
+ * @access public
+ */
+ function random()
+ {
+ return '((RANDOM()+2147483648)/4294967296)';
+ }
+
+ // }}}
+ // {{{ replace()
+
+ /**
+ * return string to call a function to get a replacement inside an SQL statement.
+ *
+ * @return string to call a function to get a replace
+ * @access public
+ */
+ function replace($str, $from_str, $to_str)
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ $error = $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ return $error;
+ }
+
+ // }}}
+}
+?>
diff --git a/3rdparty/MDB2/Driver/Manager/Common.php b/3rdparty/MDB2/Driver/Manager/Common.php
index fadd72934d626a170704645b0c50e4182614cedf..2e99c332a231432930f98b00bceea71c84ac6661 100644
--- a/3rdparty/MDB2/Driver/Manager/Common.php
+++ b/3rdparty/MDB2/Driver/Manager/Common.php
@@ -1,1014 +1,1038 @@
- |
-// | Lorenzo Alberton |
-// +----------------------------------------------------------------------+
-//
-// $Id: Common.php 295587 2010-02-28 17:16:38Z quipo $
-//
-
-/**
- * @package MDB2
- * @category Database
- * @author Lukas Smith
- * @author Lorenzo Alberton
- */
-
-/**
- * Base class for the management modules that is extended by each MDB2 driver
- *
- * To load this module in the MDB2 object:
- * $mdb->loadModule('Manager');
- *
- * @package MDB2
- * @category Database
- * @author Lukas Smith
- */
-class MDB2_Driver_Manager_Common extends MDB2_Module_Common
-{
- // {{{ splitTableSchema()
-
- /**
- * Split the "[owner|schema].table" notation into an array
- *
- * @param string $table [schema and] table name
- *
- * @return array array(schema, table)
- * @access private
- */
- function splitTableSchema($table)
- {
- $ret = array();
- if (strpos($table, '.') !== false) {
- return explode('.', $table);
- }
- return array(null, $table);
- }
-
- // }}}
- // {{{ getFieldDeclarationList()
-
- /**
- * Get declaration of a number of field in bulk
- *
- * @param array $fields a multidimensional associative array.
- * The first dimension determines the field name, while the second
- * dimension is keyed with the name of the properties
- * of the field being declared as array indexes. Currently, the types
- * of supported field properties are as follows:
- *
- * default
- * Boolean value to be used as default for this field.
- *
- * notnull
- * Boolean flag that indicates whether this field is constrained
- * to not be set to null.
- *
- * @return mixed string on success, a MDB2 error on failure
- * @access public
- */
- function getFieldDeclarationList($fields)
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- if (!is_array($fields) || empty($fields)) {
- return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
- 'missing any fields', __FUNCTION__);
- }
- foreach ($fields as $field_name => $field) {
- $query = $db->getDeclaration($field['type'], $field_name, $field);
- if (PEAR::isError($query)) {
- return $query;
- }
- $query_fields[] = $query;
- }
- return implode(', ', $query_fields);
- }
-
- // }}}
- // {{{ _fixSequenceName()
-
- /**
- * Removes any formatting in an sequence name using the 'seqname_format' option
- *
- * @param string $sqn string that containts name of a potential sequence
- * @param bool $check if only formatted sequences should be returned
- * @return string name of the sequence with possible formatting removed
- * @access protected
- */
- function _fixSequenceName($sqn, $check = false)
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- $seq_pattern = '/^'.preg_replace('/%s/', '([a-z0-9_]+)', $db->options['seqname_format']).'$/i';
- $seq_name = preg_replace($seq_pattern, '\\1', $sqn);
- if ($seq_name && !strcasecmp($sqn, $db->getSequenceName($seq_name))) {
- return $seq_name;
- }
- if ($check) {
- return false;
- }
- return $sqn;
- }
-
- // }}}
- // {{{ _fixIndexName()
-
- /**
- * Removes any formatting in an index name using the 'idxname_format' option
- *
- * @param string $idx string that containts name of anl index
- * @return string name of the index with eventual formatting removed
- * @access protected
- */
- function _fixIndexName($idx)
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- $idx_pattern = '/^'.preg_replace('/%s/', '([a-z0-9_]+)', $db->options['idxname_format']).'$/i';
- $idx_name = preg_replace($idx_pattern, '\\1', $idx);
- if ($idx_name && !strcasecmp($idx, $db->getIndexName($idx_name))) {
- return $idx_name;
- }
- return $idx;
- }
-
- // }}}
- // {{{ createDatabase()
-
- /**
- * create a new database
- *
- * @param string $name name of the database that should be created
- * @param array $options array with charset, collation info
- *
- * @return mixed MDB2_OK on success, a MDB2 error on failure
- * @access public
- */
- function createDatabase($database, $options = array())
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
- 'method not implemented', __FUNCTION__);
- }
-
- // }}}
- // {{{ alterDatabase()
-
- /**
- * alter an existing database
- *
- * @param string $name name of the database that should be created
- * @param array $options array with charset, collation info
- *
- * @return mixed MDB2_OK on success, a MDB2 error on failure
- * @access public
- */
- function alterDatabase($database, $options = array())
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
- 'method not implemented', __FUNCTION__);
- }
-
- // }}}
- // {{{ dropDatabase()
-
- /**
- * drop an existing database
- *
- * @param string $name name of the database that should be dropped
- * @return mixed MDB2_OK on success, a MDB2 error on failure
- * @access public
- */
- function dropDatabase($database)
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
- 'method not implemented', __FUNCTION__);
- }
-
- // }}}
- // {{{ _getCreateTableQuery()
-
- /**
- * Create a basic SQL query for a new table creation
- *
- * @param string $name Name of the database that should be created
- * @param array $fields Associative array that contains the definition of each field of the new table
- * @param array $options An associative array of table options
- *
- * @return mixed string (the SQL query) on success, a MDB2 error on failure
- * @see createTable()
- */
- function _getCreateTableQuery($name, $fields, $options = array())
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- if (!$name) {
- return $db->raiseError(MDB2_ERROR_CANNOT_CREATE, null, null,
- 'no valid table name specified', __FUNCTION__);
- }
- if (empty($fields)) {
- return $db->raiseError(MDB2_ERROR_CANNOT_CREATE, null, null,
- 'no fields specified for table "'.$name.'"', __FUNCTION__);
- }
- $query_fields = $this->getFieldDeclarationList($fields);
- if (PEAR::isError($query_fields)) {
- return $query_fields;
- }
- if (!empty($options['primary'])) {
- $query_fields.= ', PRIMARY KEY ('.implode(', ', array_keys($options['primary'])).')';
- }
-
- $name = $db->quoteIdentifier($name, true);
- $result = 'CREATE ';
- if (!empty($options['temporary'])) {
- $result .= $this->_getTemporaryTableQuery();
- }
- $result .= " TABLE $name ($query_fields)";
- return $result;
- }
-
- // }}}
- // {{{ _getTemporaryTableQuery()
-
- /**
- * A method to return the required SQL string that fits between CREATE ... TABLE
- * to create the table as a temporary table.
- *
- * Should be overridden in driver classes to return the correct string for the
- * specific database type.
- *
- * The default is to return the string "TEMPORARY" - this will result in a
- * SQL error for any database that does not support temporary tables, or that
- * requires a different SQL command from "CREATE TEMPORARY TABLE".
- *
- * @return string The string required to be placed between "CREATE" and "TABLE"
- * to generate a temporary table, if possible.
- */
- function _getTemporaryTableQuery()
- {
- return 'TEMPORARY';
- }
-
- // }}}
- // {{{ createTable()
-
- /**
- * create a new table
- *
- * @param string $name Name of the database that should be created
- * @param array $fields Associative array that contains the definition of each field of the new table
- * The indexes of the array entries are the names of the fields of the table an
- * the array entry values are associative arrays like those that are meant to be
- * passed with the field definitions to get[Type]Declaration() functions.
- * array(
- * 'id' => array(
- * 'type' => 'integer',
- * 'unsigned' => 1
- * 'notnull' => 1
- * 'default' => 0
- * ),
- * 'name' => array(
- * 'type' => 'text',
- * 'length' => 12
- * ),
- * 'password' => array(
- * 'type' => 'text',
- * 'length' => 12
- * )
- * );
- * @param array $options An associative array of table options:
- * array(
- * 'comment' => 'Foo',
- * 'temporary' => true|false,
- * );
- * @return mixed MDB2_OK on success, a MDB2 error on failure
- * @access public
- */
- function createTable($name, $fields, $options = array())
- {
- $query = $this->_getCreateTableQuery($name, $fields, $options);
- if (PEAR::isError($query)) {
- return $query;
- }
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
- $result = $db->exec($query);
- if (PEAR::isError($result)) {
- return $result;
- }
- return MDB2_OK;
- }
-
- // }}}
- // {{{ dropTable()
-
- /**
- * drop an existing table
- *
- * @param string $name name of the table that should be dropped
- * @return mixed MDB2_OK on success, a MDB2 error on failure
- * @access public
- */
- function dropTable($name)
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- $name = $db->quoteIdentifier($name, true);
- return $db->exec("DROP TABLE $name");
- }
-
- // }}}
- // {{{ truncateTable()
-
- /**
- * Truncate an existing table (if the TRUNCATE TABLE syntax is not supported,
- * it falls back to a DELETE FROM TABLE query)
- *
- * @param string $name name of the table that should be truncated
- * @return mixed MDB2_OK on success, a MDB2 error on failure
- * @access public
- */
- function truncateTable($name)
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- $name = $db->quoteIdentifier($name, true);
- return $db->exec("DELETE FROM $name");
- }
-
- // }}}
- // {{{ vacuum()
-
- /**
- * Optimize (vacuum) all the tables in the db (or only the specified table)
- * and optionally run ANALYZE.
- *
- * @param string $table table name (all the tables if empty)
- * @param array $options an array with driver-specific options:
- * - timeout [int] (in seconds) [mssql-only]
- * - analyze [boolean] [pgsql and mysql]
- * - full [boolean] [pgsql-only]
- * - freeze [boolean] [pgsql-only]
- *
- * @return mixed MDB2_OK success, a MDB2 error on failure
- * @access public
- */
- function vacuum($table = null, $options = array())
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
- 'method not implemented', __FUNCTION__);
- }
-
- // }}}
- // {{{ alterTable()
-
- /**
- * alter an existing table
- *
- * @param string $name name of the table that is intended to be changed.
- * @param array $changes associative array that contains the details of each type
- * of change that is intended to be performed. The types of
- * changes that are currently supported are defined as follows:
- *
- * name
- *
- * New name for the table.
- *
- * add
- *
- * Associative array with the names of fields to be added as
- * indexes of the array. The value of each entry of the array
- * should be set to another associative array with the properties
- * of the fields to be added. The properties of the fields should
- * be the same as defined by the MDB2 parser.
- *
- *
- * remove
- *
- * Associative array with the names of fields to be removed as indexes
- * of the array. Currently the values assigned to each entry are ignored.
- * An empty array should be used for future compatibility.
- *
- * rename
- *
- * Associative array with the names of fields to be renamed as indexes
- * of the array. The value of each entry of the array should be set to
- * another associative array with the entry named name with the new
- * field name and the entry named Declaration that is expected to contain
- * the portion of the field declaration already in DBMS specific SQL code
- * as it is used in the CREATE TABLE statement.
- *
- * change
- *
- * Associative array with the names of the fields to be changed as indexes
- * of the array. Keep in mind that if it is intended to change either the
- * name of a field and any other properties, the change array entries
- * should have the new names of the fields as array indexes.
- *
- * The value of each entry of the array should be set to another associative
- * array with the properties of the fields to that are meant to be changed as
- * array entries. These entries should be assigned to the new values of the
- * respective properties. The properties of the fields should be the same
- * as defined by the MDB2 parser.
- *
- * Example
- * array(
- * 'name' => 'userlist',
- * 'add' => array(
- * 'quota' => array(
- * 'type' => 'integer',
- * 'unsigned' => 1
- * )
- * ),
- * 'remove' => array(
- * 'file_limit' => array(),
- * 'time_limit' => array()
- * ),
- * 'change' => array(
- * 'name' => array(
- * 'length' => '20',
- * 'definition' => array(
- * 'type' => 'text',
- * 'length' => 20,
- * ),
- * )
- * ),
- * 'rename' => array(
- * 'sex' => array(
- * 'name' => 'gender',
- * 'definition' => array(
- * 'type' => 'text',
- * 'length' => 1,
- * 'default' => 'M',
- * ),
- * )
- * )
- * )
- *
- * @param boolean $check indicates whether the function should just check if the DBMS driver
- * can perform the requested table alterations if the value is true or
- * actually perform them otherwise.
- * @access public
- *
- * @return mixed MDB2_OK on success, a MDB2 error on failure
- */
- function alterTable($name, $changes, $check)
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
- 'method not implemented', __FUNCTION__);
- }
-
- // }}}
- // {{{ listDatabases()
-
- /**
- * list all databases
- *
- * @return mixed array of database names on success, a MDB2 error on failure
- * @access public
- */
- function listDatabases()
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
- 'method not implementedd', __FUNCTION__);
- }
-
- // }}}
- // {{{ listUsers()
-
- /**
- * list all users
- *
- * @return mixed array of user names on success, a MDB2 error on failure
- * @access public
- */
- function listUsers()
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
- 'method not implemented', __FUNCTION__);
- }
-
- // }}}
- // {{{ listViews()
-
- /**
- * list all views in the current database
- *
- * @param string database, the current is default
- * NB: not all the drivers can get the view names from
- * a database other than the current one
- * @return mixed array of view names on success, a MDB2 error on failure
- * @access public
- */
- function listViews($database = null)
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
- 'method not implemented', __FUNCTION__);
- }
-
- // }}}
- // {{{ listTableViews()
-
- /**
- * list the views in the database that reference a given table
- *
- * @param string table for which all referenced views should be found
- * @return mixed array of view names on success, a MDB2 error on failure
- * @access public
- */
- function listTableViews($table)
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
- 'method not implemented', __FUNCTION__);
- }
-
- // }}}
- // {{{ listTableTriggers()
-
- /**
- * list all triggers in the database that reference a given table
- *
- * @param string table for which all referenced triggers should be found
- * @return mixed array of trigger names on success, a MDB2 error on failure
- * @access public
- */
- function listTableTriggers($table = null)
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
- 'method not implemented', __FUNCTION__);
- }
-
- // }}}
- // {{{ listFunctions()
-
- /**
- * list all functions in the current database
- *
- * @return mixed array of function names on success, a MDB2 error on failure
- * @access public
- */
- function listFunctions()
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
- 'method not implemented', __FUNCTION__);
- }
-
- // }}}
- // {{{ listTables()
-
- /**
- * list all tables in the current database
- *
- * @param string database, the current is default.
- * NB: not all the drivers can get the table names from
- * a database other than the current one
- * @return mixed array of table names on success, a MDB2 error on failure
- * @access public
- */
- function listTables($database = null)
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
- 'method not implemented', __FUNCTION__);
- }
-
- // }}}
- // {{{ listTableFields()
-
- /**
- * list all fields in a table in the current database
- *
- * @param string $table name of table that should be used in method
- * @return mixed array of field names on success, a MDB2 error on failure
- * @access public
- */
- function listTableFields($table)
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
- 'method not implemented', __FUNCTION__);
- }
-
- // }}}
- // {{{ createIndex()
-
- /**
- * Get the stucture of a field into an array
- *
- * @param string $table name of the table on which the index is to be created
- * @param string $name name of the index to be created
- * @param array $definition associative array that defines properties of the index to be created.
- * Currently, only one property named FIELDS is supported. This property
- * is also an associative with the names of the index fields as array
- * indexes. Each entry of this array is set to another type of associative
- * array that specifies properties of the index that are specific to
- * each field.
- *
- * Currently, only the sorting property is supported. It should be used
- * to define the sorting direction of the index. It may be set to either
- * ascending or descending.
- *
- * Not all DBMS support index sorting direction configuration. The DBMS
- * drivers of those that do not support it ignore this property. Use the
- * function supports() to determine whether the DBMS driver can manage indexes.
- *
- * Example
- * array(
- * 'fields' => array(
- * 'user_name' => array(
- * 'sorting' => 'ascending'
- * ),
- * 'last_login' => array()
- * )
- * )
- * @return mixed MDB2_OK on success, a MDB2 error on failure
- * @access public
- */
- function createIndex($table, $name, $definition)
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- $table = $db->quoteIdentifier($table, true);
- $name = $db->quoteIdentifier($db->getIndexName($name), true);
- $query = "CREATE INDEX $name ON $table";
- $fields = array();
- foreach (array_keys($definition['fields']) as $field) {
- $fields[] = $db->quoteIdentifier($field, true);
- }
- $query .= ' ('. implode(', ', $fields) . ')';
- return $db->exec($query);
- }
-
- // }}}
- // {{{ dropIndex()
-
- /**
- * drop existing index
- *
- * @param string $table name of table that should be used in method
- * @param string $name name of the index to be dropped
- * @return mixed MDB2_OK on success, a MDB2 error on failure
- * @access public
- */
- function dropIndex($table, $name)
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- $name = $db->quoteIdentifier($db->getIndexName($name), true);
- return $db->exec("DROP INDEX $name");
- }
-
- // }}}
- // {{{ listTableIndexes()
-
- /**
- * list all indexes in a table
- *
- * @param string $table name of table that should be used in method
- * @return mixed array of index names on success, a MDB2 error on failure
- * @access public
- */
- function listTableIndexes($table)
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
- 'method not implemented', __FUNCTION__);
- }
-
- // }}}
- // {{{ _getAdvancedFKOptions()
-
- /**
- * Return the FOREIGN KEY query section dealing with non-standard options
- * as MATCH, INITIALLY DEFERRED, ON UPDATE, ...
- *
- * @param array $definition
- * @return string
- * @access protected
- */
- function _getAdvancedFKOptions($definition)
- {
- return '';
- }
-
- // }}}
- // {{{ createConstraint()
-
- /**
- * create a constraint on a table
- *
- * @param string $table name of the table on which the constraint is to be created
- * @param string $name name of the constraint to be created
- * @param array $definition associative array that defines properties of the constraint to be created.
- * The full structure of the array looks like this:
- *
- * @return mixed MDB2_OK on success, a MDB2 error on failure
- * @access public
- */
- function createConstraint($table, $name, $definition)
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
- $table = $db->quoteIdentifier($table, true);
- $name = $db->quoteIdentifier($db->getIndexName($name), true);
- $query = "ALTER TABLE $table ADD CONSTRAINT $name";
- if (!empty($definition['primary'])) {
- $query.= ' PRIMARY KEY';
- } elseif (!empty($definition['unique'])) {
- $query.= ' UNIQUE';
- } elseif (!empty($definition['foreign'])) {
- $query.= ' FOREIGN KEY';
- }
- $fields = array();
- foreach (array_keys($definition['fields']) as $field) {
- $fields[] = $db->quoteIdentifier($field, true);
- }
- $query .= ' ('. implode(', ', $fields) . ')';
- if (!empty($definition['foreign'])) {
- $query.= ' REFERENCES ' . $db->quoteIdentifier($definition['references']['table'], true);
- $referenced_fields = array();
- foreach (array_keys($definition['references']['fields']) as $field) {
- $referenced_fields[] = $db->quoteIdentifier($field, true);
- }
- $query .= ' ('. implode(', ', $referenced_fields) . ')';
- $query .= $this->_getAdvancedFKOptions($definition);
- }
- return $db->exec($query);
- }
-
- // }}}
- // {{{ dropConstraint()
-
- /**
- * drop existing constraint
- *
- * @param string $table name of table that should be used in method
- * @param string $name name of the constraint to be dropped
- * @param string $primary hint if the constraint is primary
- * @return mixed MDB2_OK on success, a MDB2 error on failure
- * @access public
- */
- function dropConstraint($table, $name, $primary = false)
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- $table = $db->quoteIdentifier($table, true);
- $name = $db->quoteIdentifier($db->getIndexName($name), true);
- return $db->exec("ALTER TABLE $table DROP CONSTRAINT $name");
- }
-
- // }}}
- // {{{ listTableConstraints()
-
- /**
- * list all constraints in a table
- *
- * @param string $table name of table that should be used in method
- * @return mixed array of constraint names on success, a MDB2 error on failure
- * @access public
- */
- function listTableConstraints($table)
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
- 'method not implemented', __FUNCTION__);
- }
-
- // }}}
- // {{{ createSequence()
-
- /**
- * create sequence
- *
- * @param string $seq_name name of the sequence to be created
- * @param string $start start value of the sequence; default is 1
- * @return mixed MDB2_OK on success, a MDB2 error on failure
- * @access public
- */
- function createSequence($seq_name, $start = 1)
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
- 'method not implemented', __FUNCTION__);
- }
-
- // }}}
- // {{{ dropSequence()
-
- /**
- * drop existing sequence
- *
- * @param string $seq_name name of the sequence to be dropped
- * @return mixed MDB2_OK on success, a MDB2 error on failure
- * @access public
- */
- function dropSequence($name)
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
- 'method not implemented', __FUNCTION__);
- }
-
- // }}}
- // {{{ listSequences()
-
- /**
- * list all sequences in the current database
- *
- * @param string database, the current is default
- * NB: not all the drivers can get the sequence names from
- * a database other than the current one
- * @return mixed array of sequence names on success, a MDB2 error on failure
- * @access public
- */
- function listSequences($database = null)
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
- 'method not implemented', __FUNCTION__);
- }
-
- // }}}
-}
-?>
\ No newline at end of file
+ |
+// | Lorenzo Alberton |
+// +----------------------------------------------------------------------+
+//
+// $Id$
+//
+
+/**
+ * @package MDB2
+ * @category Database
+ * @author Lukas Smith
+ * @author Lorenzo Alberton
+ */
+
+/**
+ * Base class for the management modules that is extended by each MDB2 driver
+ *
+ * To load this module in the MDB2 object:
+ * $mdb->loadModule('Manager');
+ *
+ * @package MDB2
+ * @category Database
+ * @author Lukas Smith
+ */
+class MDB2_Driver_Manager_Common extends MDB2_Module_Common
+{
+ // {{{ splitTableSchema()
+
+ /**
+ * Split the "[owner|schema].table" notation into an array
+ *
+ * @param string $table [schema and] table name
+ *
+ * @return array array(schema, table)
+ * @access private
+ */
+ function splitTableSchema($table)
+ {
+ $ret = array();
+ if (strpos($table, '.') !== false) {
+ return explode('.', $table);
+ }
+ return array(null, $table);
+ }
+
+ // }}}
+ // {{{ getFieldDeclarationList()
+
+ /**
+ * Get declaration of a number of field in bulk
+ *
+ * @param array $fields a multidimensional associative array.
+ * The first dimension determines the field name, while the second
+ * dimension is keyed with the name of the properties
+ * of the field being declared as array indexes. Currently, the types
+ * of supported field properties are as follows:
+ *
+ * default
+ * Boolean value to be used as default for this field.
+ *
+ * notnull
+ * Boolean flag that indicates whether this field is constrained
+ * to not be set to null.
+ *
+ * @return mixed string on success, a MDB2 error on failure
+ * @access public
+ */
+ function getFieldDeclarationList($fields)
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ if (!is_array($fields) || empty($fields)) {
+ return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
+ 'missing any fields', __FUNCTION__);
+ }
+ foreach ($fields as $field_name => $field) {
+ $query = $db->getDeclaration($field['type'], $field_name, $field);
+ if (PEAR::isError($query)) {
+ return $query;
+ }
+ $query_fields[] = $query;
+ }
+ return implode(', ', $query_fields);
+ }
+
+ // }}}
+ // {{{ _fixSequenceName()
+
+ /**
+ * Removes any formatting in an sequence name using the 'seqname_format' option
+ *
+ * @param string $sqn string that containts name of a potential sequence
+ * @param bool $check if only formatted sequences should be returned
+ * @return string name of the sequence with possible formatting removed
+ * @access protected
+ */
+ function _fixSequenceName($sqn, $check = false)
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ $seq_pattern = '/^'.preg_replace('/%s/', '([a-z0-9_]+)', $db->options['seqname_format']).'$/i';
+ $seq_name = preg_replace($seq_pattern, '\\1', $sqn);
+ if ($seq_name && !strcasecmp($sqn, $db->getSequenceName($seq_name))) {
+ return $seq_name;
+ }
+ if ($check) {
+ return false;
+ }
+ return $sqn;
+ }
+
+ // }}}
+ // {{{ _fixIndexName()
+
+ /**
+ * Removes any formatting in an index name using the 'idxname_format' option
+ *
+ * @param string $idx string that containts name of anl index
+ * @return string name of the index with eventual formatting removed
+ * @access protected
+ */
+ function _fixIndexName($idx)
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ $idx_pattern = '/^'.preg_replace('/%s/', '([a-z0-9_]+)', $db->options['idxname_format']).'$/i';
+ $idx_name = preg_replace($idx_pattern, '\\1', $idx);
+ if ($idx_name && !strcasecmp($idx, $db->getIndexName($idx_name))) {
+ return $idx_name;
+ }
+ return $idx;
+ }
+
+ // }}}
+ // {{{ createDatabase()
+
+ /**
+ * create a new database
+ *
+ * @param string $name name of the database that should be created
+ * @param array $options array with charset, collation info
+ *
+ * @return mixed MDB2_OK on success, a MDB2 error on failure
+ * @access public
+ */
+ function createDatabase($database, $options = array())
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ alterDatabase()
+
+ /**
+ * alter an existing database
+ *
+ * @param string $name name of the database that should be created
+ * @param array $options array with charset, collation info
+ *
+ * @return mixed MDB2_OK on success, a MDB2 error on failure
+ * @access public
+ */
+ function alterDatabase($database, $options = array())
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ dropDatabase()
+
+ /**
+ * drop an existing database
+ *
+ * @param string $name name of the database that should be dropped
+ * @return mixed MDB2_OK on success, a MDB2 error on failure
+ * @access public
+ */
+ function dropDatabase($database)
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ _getCreateTableQuery()
+
+ /**
+ * Create a basic SQL query for a new table creation
+ *
+ * @param string $name Name of the database that should be created
+ * @param array $fields Associative array that contains the definition of each field of the new table
+ * @param array $options An associative array of table options
+ *
+ * @return mixed string (the SQL query) on success, a MDB2 error on failure
+ * @see createTable()
+ */
+ function _getCreateTableQuery($name, $fields, $options = array())
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ if (!$name) {
+ return $db->raiseError(MDB2_ERROR_CANNOT_CREATE, null, null,
+ 'no valid table name specified', __FUNCTION__);
+ }
+ if (empty($fields)) {
+ return $db->raiseError(MDB2_ERROR_CANNOT_CREATE, null, null,
+ 'no fields specified for table "'.$name.'"', __FUNCTION__);
+ }
+ $query_fields = $this->getFieldDeclarationList($fields);
+ if (PEAR::isError($query_fields)) {
+ return $query_fields;
+ }
+ if (!empty($options['primary'])) {
+ $query_fields.= ', PRIMARY KEY ('.implode(', ', array_keys($options['primary'])).')';
+ }
+
+ $name = $db->quoteIdentifier($name, true);
+ $result = 'CREATE ';
+ if (!empty($options['temporary'])) {
+ $result .= $this->_getTemporaryTableQuery();
+ }
+ $result .= " TABLE $name ($query_fields)";
+ return $result;
+ }
+
+ // }}}
+ // {{{ _getTemporaryTableQuery()
+
+ /**
+ * A method to return the required SQL string that fits between CREATE ... TABLE
+ * to create the table as a temporary table.
+ *
+ * Should be overridden in driver classes to return the correct string for the
+ * specific database type.
+ *
+ * The default is to return the string "TEMPORARY" - this will result in a
+ * SQL error for any database that does not support temporary tables, or that
+ * requires a different SQL command from "CREATE TEMPORARY TABLE".
+ *
+ * @return string The string required to be placed between "CREATE" and "TABLE"
+ * to generate a temporary table, if possible.
+ */
+ function _getTemporaryTableQuery()
+ {
+ return 'TEMPORARY';
+ }
+
+ // }}}
+ // {{{ createTable()
+
+ /**
+ * create a new table
+ *
+ * @param string $name Name of the database that should be created
+ * @param array $fields Associative array that contains the definition of each field of the new table
+ * The indexes of the array entries are the names of the fields of the table an
+ * the array entry values are associative arrays like those that are meant to be
+ * passed with the field definitions to get[Type]Declaration() functions.
+ * array(
+ * 'id' => array(
+ * 'type' => 'integer',
+ * 'unsigned' => 1
+ * 'notnull' => 1
+ * 'default' => 0
+ * ),
+ * 'name' => array(
+ * 'type' => 'text',
+ * 'length' => 12
+ * ),
+ * 'password' => array(
+ * 'type' => 'text',
+ * 'length' => 12
+ * )
+ * );
+ * @param array $options An associative array of table options:
+ * array(
+ * 'comment' => 'Foo',
+ * 'temporary' => true|false,
+ * );
+ * @return mixed MDB2_OK on success, a MDB2 error on failure
+ * @access public
+ */
+ function createTable($name, $fields, $options = array())
+ {
+ $query = $this->_getCreateTableQuery($name, $fields, $options);
+ if (PEAR::isError($query)) {
+ return $query;
+ }
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+ $result = $db->exec($query);
+ if (PEAR::isError($result)) {
+ return $result;
+ }
+ return MDB2_OK;
+ }
+
+ // }}}
+ // {{{ dropTable()
+
+ /**
+ * drop an existing table
+ *
+ * @param string $name name of the table that should be dropped
+ * @return mixed MDB2_OK on success, a MDB2 error on failure
+ * @access public
+ */
+ function dropTable($name)
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ $name = $db->quoteIdentifier($name, true);
+ $result = $db->exec("DROP TABLE $name");
+ if (MDB2::isError($result)) {
+ return $result;
+ }
+ return MDB2_OK;
+ }
+
+ // }}}
+ // {{{ truncateTable()
+
+ /**
+ * Truncate an existing table (if the TRUNCATE TABLE syntax is not supported,
+ * it falls back to a DELETE FROM TABLE query)
+ *
+ * @param string $name name of the table that should be truncated
+ * @return mixed MDB2_OK on success, a MDB2 error on failure
+ * @access public
+ */
+ function truncateTable($name)
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ $name = $db->quoteIdentifier($name, true);
+ $result = $db->exec("DELETE FROM $name");
+ if (MDB2::isError($result)) {
+ return $result;
+ }
+ return MDB2_OK;
+ }
+
+ // }}}
+ // {{{ vacuum()
+
+ /**
+ * Optimize (vacuum) all the tables in the db (or only the specified table)
+ * and optionally run ANALYZE.
+ *
+ * @param string $table table name (all the tables if empty)
+ * @param array $options an array with driver-specific options:
+ * - timeout [int] (in seconds) [mssql-only]
+ * - analyze [boolean] [pgsql and mysql]
+ * - full [boolean] [pgsql-only]
+ * - freeze [boolean] [pgsql-only]
+ *
+ * @return mixed MDB2_OK success, a MDB2 error on failure
+ * @access public
+ */
+ function vacuum($table = null, $options = array())
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ alterTable()
+
+ /**
+ * alter an existing table
+ *
+ * @param string $name name of the table that is intended to be changed.
+ * @param array $changes associative array that contains the details of each type
+ * of change that is intended to be performed. The types of
+ * changes that are currently supported are defined as follows:
+ *
+ * name
+ *
+ * New name for the table.
+ *
+ * add
+ *
+ * Associative array with the names of fields to be added as
+ * indexes of the array. The value of each entry of the array
+ * should be set to another associative array with the properties
+ * of the fields to be added. The properties of the fields should
+ * be the same as defined by the MDB2 parser.
+ *
+ *
+ * remove
+ *
+ * Associative array with the names of fields to be removed as indexes
+ * of the array. Currently the values assigned to each entry are ignored.
+ * An empty array should be used for future compatibility.
+ *
+ * rename
+ *
+ * Associative array with the names of fields to be renamed as indexes
+ * of the array. The value of each entry of the array should be set to
+ * another associative array with the entry named name with the new
+ * field name and the entry named Declaration that is expected to contain
+ * the portion of the field declaration already in DBMS specific SQL code
+ * as it is used in the CREATE TABLE statement.
+ *
+ * change
+ *
+ * Associative array with the names of the fields to be changed as indexes
+ * of the array. Keep in mind that if it is intended to change either the
+ * name of a field and any other properties, the change array entries
+ * should have the new names of the fields as array indexes.
+ *
+ * The value of each entry of the array should be set to another associative
+ * array with the properties of the fields to that are meant to be changed as
+ * array entries. These entries should be assigned to the new values of the
+ * respective properties. The properties of the fields should be the same
+ * as defined by the MDB2 parser.
+ *
+ * Example
+ * array(
+ * 'name' => 'userlist',
+ * 'add' => array(
+ * 'quota' => array(
+ * 'type' => 'integer',
+ * 'unsigned' => 1
+ * )
+ * ),
+ * 'remove' => array(
+ * 'file_limit' => array(),
+ * 'time_limit' => array()
+ * ),
+ * 'change' => array(
+ * 'name' => array(
+ * 'length' => '20',
+ * 'definition' => array(
+ * 'type' => 'text',
+ * 'length' => 20,
+ * ),
+ * )
+ * ),
+ * 'rename' => array(
+ * 'sex' => array(
+ * 'name' => 'gender',
+ * 'definition' => array(
+ * 'type' => 'text',
+ * 'length' => 1,
+ * 'default' => 'M',
+ * ),
+ * )
+ * )
+ * )
+ *
+ * @param boolean $check indicates whether the function should just check if the DBMS driver
+ * can perform the requested table alterations if the value is true or
+ * actually perform them otherwise.
+ * @access public
+ *
+ * @return mixed MDB2_OK on success, a MDB2 error on failure
+ */
+ function alterTable($name, $changes, $check)
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ listDatabases()
+
+ /**
+ * list all databases
+ *
+ * @return mixed array of database names on success, a MDB2 error on failure
+ * @access public
+ */
+ function listDatabases()
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implementedd', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ listUsers()
+
+ /**
+ * list all users
+ *
+ * @return mixed array of user names on success, a MDB2 error on failure
+ * @access public
+ */
+ function listUsers()
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ listViews()
+
+ /**
+ * list all views in the current database
+ *
+ * @param string database, the current is default
+ * NB: not all the drivers can get the view names from
+ * a database other than the current one
+ * @return mixed array of view names on success, a MDB2 error on failure
+ * @access public
+ */
+ function listViews($database = null)
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ listTableViews()
+
+ /**
+ * list the views in the database that reference a given table
+ *
+ * @param string table for which all referenced views should be found
+ * @return mixed array of view names on success, a MDB2 error on failure
+ * @access public
+ */
+ function listTableViews($table)
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ listTableTriggers()
+
+ /**
+ * list all triggers in the database that reference a given table
+ *
+ * @param string table for which all referenced triggers should be found
+ * @return mixed array of trigger names on success, a MDB2 error on failure
+ * @access public
+ */
+ function listTableTriggers($table = null)
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ listFunctions()
+
+ /**
+ * list all functions in the current database
+ *
+ * @return mixed array of function names on success, a MDB2 error on failure
+ * @access public
+ */
+ function listFunctions()
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ listTables()
+
+ /**
+ * list all tables in the current database
+ *
+ * @param string database, the current is default.
+ * NB: not all the drivers can get the table names from
+ * a database other than the current one
+ * @return mixed array of table names on success, a MDB2 error on failure
+ * @access public
+ */
+ function listTables($database = null)
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ listTableFields()
+
+ /**
+ * list all fields in a table in the current database
+ *
+ * @param string $table name of table that should be used in method
+ * @return mixed array of field names on success, a MDB2 error on failure
+ * @access public
+ */
+ function listTableFields($table)
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ createIndex()
+
+ /**
+ * Get the stucture of a field into an array
+ *
+ * @param string $table name of the table on which the index is to be created
+ * @param string $name name of the index to be created
+ * @param array $definition associative array that defines properties of the index to be created.
+ * Currently, only one property named FIELDS is supported. This property
+ * is also an associative with the names of the index fields as array
+ * indexes. Each entry of this array is set to another type of associative
+ * array that specifies properties of the index that are specific to
+ * each field.
+ *
+ * Currently, only the sorting property is supported. It should be used
+ * to define the sorting direction of the index. It may be set to either
+ * ascending or descending.
+ *
+ * Not all DBMS support index sorting direction configuration. The DBMS
+ * drivers of those that do not support it ignore this property. Use the
+ * function supports() to determine whether the DBMS driver can manage indexes.
+ *
+ * Example
+ * array(
+ * 'fields' => array(
+ * 'user_name' => array(
+ * 'sorting' => 'ascending'
+ * ),
+ * 'last_login' => array()
+ * )
+ * )
+ * @return mixed MDB2_OK on success, a MDB2 error on failure
+ * @access public
+ */
+ function createIndex($table, $name, $definition)
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ $table = $db->quoteIdentifier($table, true);
+ $name = $db->quoteIdentifier($db->getIndexName($name), true);
+ $query = "CREATE INDEX $name ON $table";
+ $fields = array();
+ foreach (array_keys($definition['fields']) as $field) {
+ $fields[] = $db->quoteIdentifier($field, true);
+ }
+ $query .= ' ('. implode(', ', $fields) . ')';
+ $result = $db->exec($query);
+ if (MDB2::isError($result)) {
+ return $result;
+ }
+ return MDB2_OK;
+ }
+
+ // }}}
+ // {{{ dropIndex()
+
+ /**
+ * drop existing index
+ *
+ * @param string $table name of table that should be used in method
+ * @param string $name name of the index to be dropped
+ * @return mixed MDB2_OK on success, a MDB2 error on failure
+ * @access public
+ */
+ function dropIndex($table, $name)
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ $name = $db->quoteIdentifier($db->getIndexName($name), true);
+ $result = $db->exec("DROP INDEX $name");
+ if (MDB2::isError($result)) {
+ return $result;
+ }
+ return MDB2_OK;
+ }
+
+ // }}}
+ // {{{ listTableIndexes()
+
+ /**
+ * list all indexes in a table
+ *
+ * @param string $table name of table that should be used in method
+ * @return mixed array of index names on success, a MDB2 error on failure
+ * @access public
+ */
+ function listTableIndexes($table)
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ _getAdvancedFKOptions()
+
+ /**
+ * Return the FOREIGN KEY query section dealing with non-standard options
+ * as MATCH, INITIALLY DEFERRED, ON UPDATE, ...
+ *
+ * @param array $definition
+ * @return string
+ * @access protected
+ */
+ function _getAdvancedFKOptions($definition)
+ {
+ return '';
+ }
+
+ // }}}
+ // {{{ createConstraint()
+
+ /**
+ * create a constraint on a table
+ *
+ * @param string $table name of the table on which the constraint is to be created
+ * @param string $name name of the constraint to be created
+ * @param array $definition associative array that defines properties of the constraint to be created.
+ * The full structure of the array looks like this:
+ *
- * @access public
- */
- function getTableConstraintDefinition($table, $index)
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
- 'method not implemented', __FUNCTION__);
- }
-
- // }}}
- // {{{ getSequenceDefinition()
-
- /**
- * Get the structure of a sequence into an array
- *
- * @param string $sequence name of sequence that should be used in method
- * @return mixed data array on success, a MDB2 error on failure
- * The returned array has this structure:
- *
- * array (
- * [start] => n
- * );
- *
- * @access public
- */
- function getSequenceDefinition($sequence)
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- $start = $db->currId($sequence);
- if (PEAR::isError($start)) {
- return $start;
- }
- if ($db->supports('current_id')) {
- $start++;
- } else {
- $db->warnings[] = 'database does not support getting current
- sequence value, the sequence value was incremented';
- }
- $definition = array();
- if ($start != 1) {
- $definition = array('start' => $start);
- }
- return $definition;
- }
-
- // }}}
- // {{{ getTriggerDefinition()
-
- /**
- * Get the structure of a trigger into an array
- *
- * EXPERIMENTAL
- *
- * WARNING: this function is experimental and may change the returned value
- * at any time until labelled as non-experimental
- *
- * @param string $trigger name of trigger that should be used in method
- * @return mixed data array on success, a MDB2 error on failure
- * The returned array has this structure:
- *
- * The oci8 driver also returns a [when_clause] index.
- * @access public
- */
- function getTriggerDefinition($trigger)
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
- 'method not implemented', __FUNCTION__);
- }
-
- // }}}
- // {{{ tableInfo()
-
- /**
- * Returns information about a table or a result set
- *
- * The format of the resulting array depends on which $mode
- * you select. The sample output below is based on this query:
- *
- * SELECT tblFoo.fldID, tblFoo.fldPhone, tblBar.fldId
- * FROM tblFoo
- * JOIN tblBar ON tblFoo.fldId = tblBar.fldId
- *
In addition to the information found in the default output,
- * a notation of the number of columns is provided by the
- * num_fields element while the order
- * element provides an array with the column names as the keys and
- * their location index number (corresponding to the keys in the
- * the default output) as the values.
- *
- *
If a result set has identical field names, the last one is
- * used.
Similar to MDB2_TABLEINFO_ORDER but adds more
- * dimensions to the array in which the table names are keys and
- * the field names are sub-keys. This is helpful for queries that
- * join tables which have identical field names.
- *
- * The flags element contains a space separated list
- * of extra information about the field. This data is inconsistent
- * between DBMS's due to the way each DBMS works.
- * + primary_key
- * + unique_key
- * + multiple_key
- * + not_null
- *
- * Most DBMS's only provide the table and flags
- * elements if $result is a table name. The following DBMS's
- * provide full information from queries:
- * + fbsql
- * + mysql
- *
- * If the 'portability' option has MDB2_PORTABILITY_FIX_CASE
- * turned on, the names of tables and fields will be lower or upper cased.
- *
- * @param object|string $result MDB2_result object from a query or a
- * string containing the name of a table.
- * While this also accepts a query result
- * resource identifier, this behavior is
- * deprecated.
- * @param int $mode either unused or one of the tableInfo modes:
- * MDB2_TABLEINFO_ORDERTABLE,
- * MDB2_TABLEINFO_ORDER or
- * MDB2_TABLEINFO_FULL (which does both).
- * These are bitwise, so the first two can be
- * combined using |.
- *
- * @return array an associative array with the information requested.
- * A MDB2_Error object on failure.
- *
- * @see MDB2_Driver_Common::setOption()
- */
- function tableInfo($result, $mode = null)
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- if (!is_string($result)) {
- return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
- 'method not implemented', __FUNCTION__);
- }
-
- $db->loadModule('Manager', null, true);
- $fields = $db->manager->listTableFields($result);
- if (PEAR::isError($fields)) {
- return $fields;
- }
-
- $flags = array();
-
- $idxname_format = $db->getOption('idxname_format');
- $db->setOption('idxname_format', '%s');
-
- $indexes = $db->manager->listTableIndexes($result);
- if (PEAR::isError($indexes)) {
- $db->setOption('idxname_format', $idxname_format);
- return $indexes;
- }
-
- foreach ($indexes as $index) {
- $definition = $this->getTableIndexDefinition($result, $index);
- if (PEAR::isError($definition)) {
- $db->setOption('idxname_format', $idxname_format);
- return $definition;
- }
- if (count($definition['fields']) > 1) {
- foreach ($definition['fields'] as $field => $sort) {
- $flags[$field] = 'multiple_key';
- }
- }
- }
-
- $constraints = $db->manager->listTableConstraints($result);
- if (PEAR::isError($constraints)) {
- return $constraints;
- }
-
- foreach ($constraints as $constraint) {
- $definition = $this->getTableConstraintDefinition($result, $constraint);
- if (PEAR::isError($definition)) {
- $db->setOption('idxname_format', $idxname_format);
- return $definition;
- }
- $flag = !empty($definition['primary'])
- ? 'primary_key' : (!empty($definition['unique'])
- ? 'unique_key' : false);
- if ($flag) {
- foreach ($definition['fields'] as $field => $sort) {
- if (empty($flags[$field]) || $flags[$field] != 'primary_key') {
- $flags[$field] = $flag;
- }
- }
- }
- }
-
- $res = array();
-
- if ($mode) {
- $res['num_fields'] = count($fields);
- }
-
- foreach ($fields as $i => $field) {
- $definition = $this->getTableFieldDefinition($result, $field);
- if (PEAR::isError($definition)) {
- $db->setOption('idxname_format', $idxname_format);
- return $definition;
- }
- $res[$i] = $definition[0];
- $res[$i]['name'] = $field;
- $res[$i]['table'] = $result;
- $res[$i]['type'] = preg_replace('/^([a-z]+).*$/i', '\\1', trim($definition[0]['nativetype']));
- // 'primary_key', 'unique_key', 'multiple_key'
- $res[$i]['flags'] = empty($flags[$field]) ? '' : $flags[$field];
- // not_null', 'unsigned', 'auto_increment', 'default_[rawencodedvalue]'
- if (!empty($res[$i]['notnull'])) {
- $res[$i]['flags'].= ' not_null';
- }
- if (!empty($res[$i]['unsigned'])) {
- $res[$i]['flags'].= ' unsigned';
- }
- if (!empty($res[$i]['auto_increment'])) {
- $res[$i]['flags'].= ' autoincrement';
- }
- if (!empty($res[$i]['default'])) {
- $res[$i]['flags'].= ' default_'.rawurlencode($res[$i]['default']);
- }
-
- if ($mode & MDB2_TABLEINFO_ORDER) {
- $res['order'][$res[$i]['name']] = $i;
- }
- if ($mode & MDB2_TABLEINFO_ORDERTABLE) {
- $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i;
- }
- }
-
- $db->setOption('idxname_format', $idxname_format);
- return $res;
- }
-}
+ |
+// +----------------------------------------------------------------------+
+//
+// $Id$
+//
+
+/**
+ * @package MDB2
+ * @category Database
+ */
+
+/**
+ * These are constants for the tableInfo-function
+ * they are bitwised or'ed. so if there are more constants to be defined
+ * in the future, adjust MDB2_TABLEINFO_FULL accordingly
+ */
+
+define('MDB2_TABLEINFO_ORDER', 1);
+define('MDB2_TABLEINFO_ORDERTABLE', 2);
+define('MDB2_TABLEINFO_FULL', 3);
+
+/**
+ * Base class for the schema reverse engineering module that is extended by each MDB2 driver
+ *
+ * To load this module in the MDB2 object:
+ * $mdb->loadModule('Reverse');
+ *
+ * @package MDB2
+ * @category Database
+ * @author Lukas Smith
+ */
+class MDB2_Driver_Reverse_Common extends MDB2_Module_Common
+{
+ // {{{ splitTableSchema()
+
+ /**
+ * Split the "[owner|schema].table" notation into an array
+ *
+ * @param string $table [schema and] table name
+ *
+ * @return array array(schema, table)
+ * @access private
+ */
+ function splitTableSchema($table)
+ {
+ $ret = array();
+ if (strpos($table, '.') !== false) {
+ return explode('.', $table);
+ }
+ return array(null, $table);
+ }
+
+ // }}}
+ // {{{ getTableFieldDefinition()
+
+ /**
+ * Get the structure of a field into an array
+ *
+ * @param string $table name of table that should be used in method
+ * @param string $field name of field that should be used in method
+ * @return mixed data array on success, a MDB2 error on failure.
+ * The returned array contains an array for each field definition,
+ * with all or some of these indices, depending on the field data type:
+ * [notnull] [nativetype] [length] [fixed] [default] [type] [mdb2type]
+ * @access public
+ */
+ function getTableFieldDefinition($table, $field)
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ getTableIndexDefinition()
+
+ /**
+ * Get the structure of an index into an array
+ *
+ * @param string $table name of table that should be used in method
+ * @param string $index name of index that should be used in method
+ * @return mixed data array on success, a MDB2 error on failure
+ * The returned array has this structure:
+ *
+ * array (
+ * [fields] => array (
+ * [field1name] => array() // one entry per each field covered
+ * [field2name] => array() // by the index
+ * [field3name] => array(
+ * [sorting] => ascending
+ * )
+ * )
+ * );
+ *
+ * @access public
+ */
+ function getTableIndexDefinition($table, $index)
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ getTableConstraintDefinition()
+
+ /**
+ * Get the structure of an constraints into an array
+ *
+ * @param string $table name of table that should be used in method
+ * @param string $index name of index that should be used in method
+ * @return mixed data array on success, a MDB2 error on failure
+ * The returned array has this structure:
+ *
+ * @access public
+ */
+ function getTableConstraintDefinition($table, $index)
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ getSequenceDefinition()
+
+ /**
+ * Get the structure of a sequence into an array
+ *
+ * @param string $sequence name of sequence that should be used in method
+ * @return mixed data array on success, a MDB2 error on failure
+ * The returned array has this structure:
+ *
+ * array (
+ * [start] => n
+ * );
+ *
+ * @access public
+ */
+ function getSequenceDefinition($sequence)
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ $start = $db->currId($sequence);
+ if (PEAR::isError($start)) {
+ return $start;
+ }
+ if ($db->supports('current_id')) {
+ $start++;
+ } else {
+ $db->warnings[] = 'database does not support getting current
+ sequence value, the sequence value was incremented';
+ }
+ $definition = array();
+ if ($start != 1) {
+ $definition = array('start' => $start);
+ }
+ return $definition;
+ }
+
+ // }}}
+ // {{{ getTriggerDefinition()
+
+ /**
+ * Get the structure of a trigger into an array
+ *
+ * EXPERIMENTAL
+ *
+ * WARNING: this function is experimental and may change the returned value
+ * at any time until labelled as non-experimental
+ *
+ * @param string $trigger name of trigger that should be used in method
+ * @return mixed data array on success, a MDB2 error on failure
+ * The returned array has this structure:
+ *
+ * The oci8 driver also returns a [when_clause] index.
+ * @access public
+ */
+ function getTriggerDefinition($trigger)
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
+ 'method not implemented', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ tableInfo()
+
+ /**
+ * Returns information about a table or a result set
+ *
+ * The format of the resulting array depends on which $mode
+ * you select. The sample output below is based on this query:
+ *
+ * SELECT tblFoo.fldID, tblFoo.fldPhone, tblBar.fldId
+ * FROM tblFoo
+ * JOIN tblBar ON tblFoo.fldId = tblBar.fldId
+ *
In addition to the information found in the default output,
+ * a notation of the number of columns is provided by the
+ * num_fields element while the order
+ * element provides an array with the column names as the keys and
+ * their location index number (corresponding to the keys in the
+ * the default output) as the values.
+ *
+ *
If a result set has identical field names, the last one is
+ * used.
Similar to MDB2_TABLEINFO_ORDER but adds more
+ * dimensions to the array in which the table names are keys and
+ * the field names are sub-keys. This is helpful for queries that
+ * join tables which have identical field names.
- *
- * If the more than one row occurs with the same value in the
- * first column, the last row overwrites all previous ones by
- * default. Use the $group parameter if you don't want to
- * overwrite like this. Example:
- *
- *
- * Keep in mind that database functions in PHP usually return string
- * values for results regardless of the database's internal type.
- *
- * @param string the SQL query
- * @param array that contains the types of the columns in the result set
- * @param array if supplied, prepare/execute will be used
- * with this array as execute parameters
- * @param array that contains the types of the values defined in $params
- * @param bool $force_array used only when the query returns
- * exactly two columns. If TRUE, the values of the returned array
- * will be one-element arrays instead of scalars.
- * @param bool $group if TRUE, the values of the returned array
- * is wrapped in another array. If the same key value (in the first
- * column) repeats itself, the values will be appended to this array
- * instead of overwriting the existing values.
- *
- * @return array|MDB2_Error data on success, a MDB2 error on failure
- * @access public
- */
- function getAssoc($query, $types = null, $params = array(), $param_types = null,
- $fetchmode = MDB2_FETCHMODE_DEFAULT, $force_array = false, $group = false)
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- settype($params, 'array');
- if (empty($params)) {
- return $db->queryAll($query, $types, $fetchmode, true, $force_array, $group);
- }
-
- $stmt = $db->prepare($query, $param_types, $types);
- if (PEAR::isError($stmt)) {
- return $stmt;
- }
-
- $result = $stmt->execute($params);
- if (!MDB2::isResultCommon($result)) {
- return $result;
- }
-
- $all = $result->fetchAll($fetchmode, true, $force_array, $group);
- $stmt->free();
- $result->free();
- return $all;
- }
-
- // }}}
- // {{{ executeMultiple()
-
- /**
- * This function does several execute() calls on the same statement handle.
- * $params must be an array indexed numerically from 0, one execute call is
- * done for every 'row' in the array.
- *
- * If an error occurs during execute(), executeMultiple() does not execute
- * the unfinished rows, but rather returns that error.
- *
- * @param resource query handle from prepare()
- * @param array numeric array containing the data to insert into the query
- *
- * @return bool|MDB2_Error true on success, a MDB2 error on failure
- * @access public
- * @see prepare(), execute()
- */
- function executeMultiple($stmt, $params = null)
- {
- for ($i = 0, $j = count($params); $i < $j; $i++) {
- $result = $stmt->execute($params[$i]);
- if (PEAR::isError($result)) {
- return $result;
- }
- }
- return MDB2_OK;
- }
-
- // }}}
- // {{{ getBeforeID()
-
- /**
- * Returns the next free id of a sequence if the RDBMS
- * does not support auto increment
- *
- * @param string name of the table into which a new row was inserted
- * @param string name of the field into which a new row was inserted
- * @param bool when true the sequence is automatic created, if it not exists
- * @param bool if the returned value should be quoted
- *
- * @return int|MDB2_Error id on success, a MDB2 error on failure
- * @access public
- */
- function getBeforeID($table, $field = null, $ondemand = true, $quote = true)
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- if ($db->supports('auto_increment') !== true) {
- $seq = $table.(empty($field) ? '' : '_'.$field);
- $id = $db->nextID($seq, $ondemand);
- if (!$quote || PEAR::isError($id)) {
- return $id;
- }
- return $db->quote($id, 'integer');
- } elseif (!$quote) {
- return null;
- }
- return 'NULL';
- }
-
- // }}}
- // {{{ getAfterID()
-
- /**
- * Returns the autoincrement ID if supported or $id
- *
- * @param mixed value as returned by getBeforeId()
- * @param string name of the table into which a new row was inserted
- * @param string name of the field into which a new row was inserted
- *
- * @return int|MDB2_Error id on success, a MDB2 error on failure
- * @access public
- */
- function getAfterID($id, $table, $field = null)
- {
- $db = $this->getDBInstance();
- if (PEAR::isError($db)) {
- return $db;
- }
-
- if ($db->supports('auto_increment') !== true) {
- return $id;
- }
- return $db->lastInsertID($table, $field);
- }
-
- // }}}
-}
+ |
+// +----------------------------------------------------------------------+
+//
+// $Id$
+
+/**
+ * @package MDB2
+ * @category Database
+ * @author Lukas Smith
+ */
+
+/**
+ * Used by autoPrepare()
+ */
+define('MDB2_AUTOQUERY_INSERT', 1);
+define('MDB2_AUTOQUERY_UPDATE', 2);
+define('MDB2_AUTOQUERY_DELETE', 3);
+define('MDB2_AUTOQUERY_SELECT', 4);
+
+/**
+ * MDB2_Extended: class which adds several high level methods to MDB2
+ *
+ * @package MDB2
+ * @category Database
+ * @author Lukas Smith
+ */
+class MDB2_Extended extends MDB2_Module_Common
+{
+ // {{{ autoPrepare()
+
+ /**
+ * Generate an insert, update or delete query and call prepare() on it
+ *
+ * @param string table
+ * @param array the fields names
+ * @param int type of query to build
+ * MDB2_AUTOQUERY_INSERT
+ * MDB2_AUTOQUERY_UPDATE
+ * MDB2_AUTOQUERY_DELETE
+ * MDB2_AUTOQUERY_SELECT
+ * @param string (in case of update and delete queries, this string will be put after the sql WHERE statement)
+ * @param array that contains the types of the placeholders
+ * @param mixed array that contains the types of the columns in
+ * the result set or MDB2_PREPARE_RESULT, if set to
+ * MDB2_PREPARE_MANIP the query is handled as a manipulation query
+ *
+ * @return resource handle for the query
+ * @see buildManipSQL
+ * @access public
+ */
+ function autoPrepare($table, $table_fields, $mode = MDB2_AUTOQUERY_INSERT,
+ $where = false, $types = null, $result_types = MDB2_PREPARE_MANIP)
+ {
+ $query = $this->buildManipSQL($table, $table_fields, $mode, $where);
+ if (PEAR::isError($query)) {
+ return $query;
+ }
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+ $lobs = array();
+ foreach ((array)$types as $param => $type) {
+ if (($type == 'clob') || ($type == 'blob')) {
+ $lobs[$param] = $table_fields[$param];
+ }
+ }
+ return $db->prepare($query, $types, $result_types, $lobs);
+ }
+
+ // }}}
+ // {{{ autoExecute()
+
+ /**
+ * Generate an insert, update or delete query and call prepare() and execute() on it
+ *
+ * @param string name of the table
+ * @param array assoc ($key=>$value) where $key is a field name and $value its value
+ * @param int type of query to build
+ * MDB2_AUTOQUERY_INSERT
+ * MDB2_AUTOQUERY_UPDATE
+ * MDB2_AUTOQUERY_DELETE
+ * MDB2_AUTOQUERY_SELECT
+ * @param string (in case of update and delete queries, this string will be put after the sql WHERE statement)
+ * @param array that contains the types of the placeholders
+ * @param string which specifies which result class to use
+ * @param mixed array that contains the types of the columns in
+ * the result set or MDB2_PREPARE_RESULT, if set to
+ * MDB2_PREPARE_MANIP the query is handled as a manipulation query
+ *
+ * @return bool|MDB2_Error true on success, a MDB2 error on failure
+ * @see buildManipSQL
+ * @see autoPrepare
+ * @access public
+ */
+ function autoExecute($table, $fields_values, $mode = MDB2_AUTOQUERY_INSERT,
+ $where = false, $types = null, $result_class = true, $result_types = MDB2_PREPARE_MANIP)
+ {
+ $fields_values = (array)$fields_values;
+ if ($mode == MDB2_AUTOQUERY_SELECT) {
+ if (is_array($result_types)) {
+ $keys = array_keys($result_types);
+ } elseif (!empty($fields_values)) {
+ $keys = $fields_values;
+ } else {
+ $keys = array();
+ }
+ } else {
+ $keys = array_keys($fields_values);
+ }
+ $params = array_values($fields_values);
+ if (empty($params)) {
+ $query = $this->buildManipSQL($table, $keys, $mode, $where);
+
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+ if ($mode == MDB2_AUTOQUERY_SELECT) {
+ $result = $db->query($query, $result_types, $result_class);
+ } else {
+ $result = $db->exec($query);
+ }
+ } else {
+ $stmt = $this->autoPrepare($table, $keys, $mode, $where, $types, $result_types);
+ if (PEAR::isError($stmt)) {
+ return $stmt;
+ }
+ $result = $stmt->execute($params, $result_class);
+ $stmt->free();
+ }
+ return $result;
+ }
+
+ // }}}
+ // {{{ buildManipSQL()
+
+ /**
+ * Make automaticaly an sql query for prepare()
+ *
+ * Example : buildManipSQL('table_sql', array('field1', 'field2', 'field3'), MDB2_AUTOQUERY_INSERT)
+ * will return the string : INSERT INTO table_sql (field1,field2,field3) VALUES (?,?,?)
+ * NB : - This belongs more to a SQL Builder class, but this is a simple facility
+ * - Be carefull ! If you don't give a $where param with an UPDATE/DELETE query, all
+ * the records of the table will be updated/deleted !
+ *
+ * @param string name of the table
+ * @param ordered array containing the fields names
+ * @param int type of query to build
+ * MDB2_AUTOQUERY_INSERT
+ * MDB2_AUTOQUERY_UPDATE
+ * MDB2_AUTOQUERY_DELETE
+ * MDB2_AUTOQUERY_SELECT
+ * @param string (in case of update and delete queries, this string will be put after the sql WHERE statement)
+ *
+ * @return string sql query for prepare()
+ * @access public
+ */
+ function buildManipSQL($table, $table_fields, $mode, $where = false)
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ if ($db->options['quote_identifier']) {
+ $table = $db->quoteIdentifier($table);
+ }
+
+ if (!empty($table_fields) && $db->options['quote_identifier']) {
+ foreach ($table_fields as $key => $field) {
+ $table_fields[$key] = $db->quoteIdentifier($field);
+ }
+ }
+
+ if ((false !== $where) && (null !== $where)) {
+ if (is_array($where)) {
+ $where = implode(' AND ', $where);
+ }
+ $where = ' WHERE '.$where;
+ }
+
+ switch ($mode) {
+ case MDB2_AUTOQUERY_INSERT:
+ if (empty($table_fields)) {
+ return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
+ 'Insert requires table fields', __FUNCTION__);
+ }
+ $cols = implode(', ', $table_fields);
+ $values = '?'.str_repeat(', ?', (count($table_fields) - 1));
+ return 'INSERT INTO '.$table.' ('.$cols.') VALUES ('.$values.')';
+ break;
+ case MDB2_AUTOQUERY_UPDATE:
+ if (empty($table_fields)) {
+ return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
+ 'Update requires table fields', __FUNCTION__);
+ }
+ $set = implode(' = ?, ', $table_fields).' = ?';
+ $sql = 'UPDATE '.$table.' SET '.$set.$where;
+ return $sql;
+ break;
+ case MDB2_AUTOQUERY_DELETE:
+ $sql = 'DELETE FROM '.$table.$where;
+ return $sql;
+ break;
+ case MDB2_AUTOQUERY_SELECT:
+ $cols = !empty($table_fields) ? implode(', ', $table_fields) : '*';
+ $sql = 'SELECT '.$cols.' FROM '.$table.$where;
+ return $sql;
+ break;
+ }
+ return $db->raiseError(MDB2_ERROR_SYNTAX, null, null,
+ 'Non existant mode', __FUNCTION__);
+ }
+
+ // }}}
+ // {{{ limitQuery()
+
+ /**
+ * Generates a limited query
+ *
+ * @param string query
+ * @param array that contains the types of the columns in the result set
+ * @param integer the numbers of rows to fetch
+ * @param integer the row to start to fetching
+ * @param string which specifies which result class to use
+ * @param mixed string which specifies which class to wrap results in
+ *
+ * @return MDB2_Result|MDB2_Error result set on success, a MDB2 error on failure
+ * @access public
+ */
+ function limitQuery($query, $types, $limit, $offset = 0, $result_class = true,
+ $result_wrap_class = false)
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ $result = $db->setLimit($limit, $offset);
+ if (PEAR::isError($result)) {
+ return $result;
+ }
+ return $db->query($query, $types, $result_class, $result_wrap_class);
+ }
+
+ // }}}
+ // {{{ execParam()
+
+ /**
+ * Execute a parameterized DML statement.
+ *
+ * @param string the SQL query
+ * @param array if supplied, prepare/execute will be used
+ * with this array as execute parameters
+ * @param array that contains the types of the values defined in $params
+ *
+ * @return int|MDB2_Error affected rows on success, a MDB2 error on failure
+ * @access public
+ */
+ function execParam($query, $params = array(), $param_types = null)
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ settype($params, 'array');
+ if (empty($params)) {
+ return $db->exec($query);
+ }
+
+ $stmt = $db->prepare($query, $param_types, MDB2_PREPARE_MANIP);
+ if (PEAR::isError($stmt)) {
+ return $stmt;
+ }
+
+ $result = $stmt->execute($params);
+ if (PEAR::isError($result)) {
+ return $result;
+ }
+
+ $stmt->free();
+ return $result;
+ }
+
+ // }}}
+ // {{{ getOne()
+
+ /**
+ * Fetch the first column of the first row of data returned from a query.
+ * Takes care of doing the query and freeing the results when finished.
+ *
+ * @param string the SQL query
+ * @param string that contains the type of the column in the result set
+ * @param array if supplied, prepare/execute will be used
+ * with this array as execute parameters
+ * @param array that contains the types of the values defined in $params
+ * @param int|string which column to return
+ *
+ * @return scalar|MDB2_Error data on success, a MDB2 error on failure
+ * @access public
+ */
+ function getOne($query, $type = null, $params = array(),
+ $param_types = null, $colnum = 0)
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ settype($params, 'array');
+ settype($type, 'array');
+ if (empty($params)) {
+ return $db->queryOne($query, $type, $colnum);
+ }
+
+ $stmt = $db->prepare($query, $param_types, $type);
+ if (PEAR::isError($stmt)) {
+ return $stmt;
+ }
+
+ $result = $stmt->execute($params);
+ if (!MDB2::isResultCommon($result)) {
+ return $result;
+ }
+
+ $one = $result->fetchOne($colnum);
+ $stmt->free();
+ $result->free();
+ return $one;
+ }
+
+ // }}}
+ // {{{ getRow()
+
+ /**
+ * Fetch the first row of data returned from a query. Takes care
+ * of doing the query and freeing the results when finished.
+ *
+ * @param string the SQL query
+ * @param array that contains the types of the columns in the result set
+ * @param array if supplied, prepare/execute will be used
+ * with this array as execute parameters
+ * @param array that contains the types of the values defined in $params
+ * @param int the fetch mode to use
+ *
+ * @return array|MDB2_Error data on success, a MDB2 error on failure
+ * @access public
+ */
+ function getRow($query, $types = null, $params = array(),
+ $param_types = null, $fetchmode = MDB2_FETCHMODE_DEFAULT)
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ settype($params, 'array');
+ if (empty($params)) {
+ return $db->queryRow($query, $types, $fetchmode);
+ }
+
+ $stmt = $db->prepare($query, $param_types, $types);
+ if (PEAR::isError($stmt)) {
+ return $stmt;
+ }
+
+ $result = $stmt->execute($params);
+ if (!MDB2::isResultCommon($result)) {
+ return $result;
+ }
+
+ $row = $result->fetchRow($fetchmode);
+ $stmt->free();
+ $result->free();
+ return $row;
+ }
+
+ // }}}
+ // {{{ getCol()
+
+ /**
+ * Fetch a single column from a result set and return it as an
+ * indexed array.
+ *
+ * @param string the SQL query
+ * @param string that contains the type of the column in the result set
+ * @param array if supplied, prepare/execute will be used
+ * with this array as execute parameters
+ * @param array that contains the types of the values defined in $params
+ * @param int|string which column to return
+ *
+ * @return array|MDB2_Error data on success, a MDB2 error on failure
+ * @access public
+ */
+ function getCol($query, $type = null, $params = array(),
+ $param_types = null, $colnum = 0)
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ settype($params, 'array');
+ settype($type, 'array');
+ if (empty($params)) {
+ return $db->queryCol($query, $type, $colnum);
+ }
+
+ $stmt = $db->prepare($query, $param_types, $type);
+ if (PEAR::isError($stmt)) {
+ return $stmt;
+ }
+
+ $result = $stmt->execute($params);
+ if (!MDB2::isResultCommon($result)) {
+ return $result;
+ }
+
+ $col = $result->fetchCol($colnum);
+ $stmt->free();
+ $result->free();
+ return $col;
+ }
+
+ // }}}
+ // {{{ getAll()
+
+ /**
+ * Fetch all the rows returned from a query.
+ *
+ * @param string the SQL query
+ * @param array that contains the types of the columns in the result set
+ * @param array if supplied, prepare/execute will be used
+ * with this array as execute parameters
+ * @param array that contains the types of the values defined in $params
+ * @param int the fetch mode to use
+ * @param bool if set to true, the $all will have the first
+ * column as its first dimension
+ * @param bool $force_array used only when the query returns exactly
+ * two columns. If true, the values of the returned array will be
+ * one-element arrays instead of scalars.
+ * @param bool $group if true, the values of the returned array is
+ * wrapped in another array. If the same key value (in the first
+ * column) repeats itself, the values will be appended to this array
+ * instead of overwriting the existing values.
+ *
+ * @return array|MDB2_Error data on success, a MDB2 error on failure
+ * @access public
+ */
+ function getAll($query, $types = null, $params = array(),
+ $param_types = null, $fetchmode = MDB2_FETCHMODE_DEFAULT,
+ $rekey = false, $force_array = false, $group = false)
+ {
+ $db = $this->getDBInstance();
+ if (PEAR::isError($db)) {
+ return $db;
+ }
+
+ settype($params, 'array');
+ if (empty($params)) {
+ return $db->queryAll($query, $types, $fetchmode, $rekey, $force_array, $group);
+ }
+
+ $stmt = $db->prepare($query, $param_types, $types);
+ if (PEAR::isError($stmt)) {
+ return $stmt;
+ }
+
+ $result = $stmt->execute($params);
+ if (!MDB2::isResultCommon($result)) {
+ return $result;
+ }
+
+ $all = $result->fetchAll($fetchmode, $rekey, $force_array, $group);
+ $stmt->free();
+ $result->free();
+ return $all;
+ }
+
+ // }}}
+ // {{{ getAssoc()
+
+ /**
+ * Fetch the entire result set of a query and return it as an
+ * associative array using the first column as the key.
+ *
+ * If the result set contains more than two columns, the value
+ * will be an array of the values from column 2-n. If the result
+ * set contains only two columns, the returned value will be a
+ * scalar with the value of the second column (unless forced to an
+ * array with the $force_array parameter). A MDB2 error code is
+ * returned on errors. If the result set contains fewer than two
+ * columns, a MDB2_ERROR_TRUNCATED error is returned.
+ *
+ * For example, if the table 'mytable' contains:
+ *
+ * ID TEXT DATE
+ * --------------------------------
+ * 1 'one' 944679408
+ * 2 'two' 944679408
+ * 3 'three' 944679408
+ *
+ * Then the call getAssoc('SELECT id,text FROM mytable') returns:
+ *
+ *
+ * If the more than one row occurs with the same value in the
+ * first column, the last row overwrites all previous ones by
+ * default. Use the $group parameter if you don't want to
+ * overwrite like this. Example:
+ *