📄 common.php
字号:
<?php// +----------------------------------------------------------------------+// | PHP versions 4 and 5 |// +----------------------------------------------------------------------+// | Copyright (c) 1998-2006 Manuel Lemos, Tomas V.V.Cox, |// | Stig. S. Bakken, Lukas Smith |// | All rights reserved. |// +----------------------------------------------------------------------+// | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB |// | API as well as database abstraction for PHP applications. |// | This LICENSE is in the BSD license style. |// | |// | Redistribution and use in source and binary forms, with or without |// | modification, are permitted provided that the following conditions |// | are met: |// | |// | Redistributions of source code must retain the above copyright |// | notice, this list of conditions and the following disclaimer. |// | |// | Redistributions in binary form must reproduce the above copyright |// | notice, this list of conditions and the following disclaimer in the |// | documentation and/or other materials provided with the distribution. |// | |// | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken, |// | Lukas Smith nor the names of his contributors may be used to endorse |// | or promote products derived from this software without specific prior|// | written permission. |// | |// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |// | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |// | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |// | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |// | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|// | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |// | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |// | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|// | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |// | POSSIBILITY OF SUCH DAMAGE. |// +----------------------------------------------------------------------+// | Author: Lukas Smith <smith@pooteeweet.org> |// +----------------------------------------------------------------------+//// $Id: Common.php,v 1.112 2006/10/14 13:42:00 lsmith Exp $require_once 'MDB2/LOB.php';/** * @package MDB2 * @category Database * @author Lukas Smith <smith@pooteeweet.org> *//** * MDB2_Driver_Common: Base class that is extended by each MDB2 driver * * @package MDB2 * @category Database * @author Lukas Smith <smith@pooteeweet.org> */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 string $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 refernce to a value to be converted * @param string $type specifies which type to convert to * @return object a 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 indepdenant MDB2 type * * @param mixed $value value to be converted * @param string $type specifies which type to convert to * @param bool $rtrim if to rtrim text values or not * @return mixed converted value * @access public */ function convertResult($value, $type, $rtrim = true) { if (is_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 bool $rtrim if to rtrim text values or not * @return mixed MDB2_OK on success, a MDB2 error on failure * @access public */ function convertResultRow($types, $row, $rtrim = true) { reset($types); $current_column = -1; foreach ($row as $key => $value) { ++$current_column; if (!isset($value)) { continue; } if (isset($types[$current_column])) { $type = $types[$current_column]; } elseif (isset($types[$key])) { $type = $types[$key]; } elseif (current($types)) { $type = current($types); next($types); } else { continue; } $value = $this->convertResult($row[$key], $type, $rtrim); if (PEAR::isError($value)) { return $value; } $row[$key] = $value; } return $row; } // }}} // {{{ 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)); } } 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. * @access protected */ function _getDeclaration($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 : $this->valid_default_values[$field['type']]; if ($field['default'] === '' && ($db->options['portability'] & MDB2_PORTABILITY_EMPTY_TO_NULL)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -