cvs: pear /MDB2/MDB2/Driver/Datatype ibase.php
| From: | Lorenzo Alberton | Date: | Thu, 07 Apr 2005 22:58:02 +0000 |
| Subject: | cvs: pear /MDB2/MDB2/Driver/Datatype ibase.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-31030@lists.php.net to get a copy of this message | ||
quipo Thu Apr 7 18:58:02 2005 EDT
Modified files:
/pear/MDB2/MDB2/Driver/Datatype ibase.php
Log:
- added mapNativeDatatype()
- getTypeDeclaration() => _getTypeDeclaration()
- dropped useless $db references
- fixed typos in the comments
http://cvs.php.net/diff.php/pear/MDB2/MDB2/Driver/Datatype/ibase.php?r1=1.25&r2=1.26&ty=u Index: pear/MDB2/MDB2/Driver/Datatype/ibase.php diff -u pear/MDB2/MDB2/Driver/Datatype/ibase.php:1.25 pear/MDB2/MDB2/Driver/Datatype/ibase.php:1.26 --- pear/MDB2/MDB2/Driver/Datatype/ibase.php:1.25 Fri Mar 25 13:12:19 2005 +++ pear/MDB2/MDB2/Driver/Datatype/ibase.php Thu Apr 7 18:58:01 2005 @@ -41,30 +41,31 @@ // | POSSIBILITY OF SUCH DAMAGE. | // +----------------------------------------------------------------------+ // | Author: Lukas Smith <smith@backendmedia.com> | +// | Lorenzo Alberton <l.alberton@quipo.it> | // +----------------------------------------------------------------------+ // -// $Id: ibase.php,v 1.25 2005/03/25 18:12:19 lsmith Exp $ -// +// $Id: ibase.php,v 1.26 2005/04/07 22:58:01 quipo Exp $ require_once 'MDB2/Driver/Datatype/Common.php'; /** - * MDB2 MySQL driver + * MDB2 Firebird/Interbase driver * * @package MDB2 * @category Database * @author Lukas Smith <smith@backendmedia.com> + * @author Lorenzo Alberton <l.alberton@quipo.it> */ class MDB2_Driver_Datatype_ibase extends MDB2_Driver_Datatype_Common { // {{{ convertResult() /** - * convert a value to a RDBMS indepdenant MDB2 type + * convert a value to a RDBMS independent MDB2 type * * @param mixed $value value to be converted * @param int $type constant that specifies which type to convert to - * @return mixed converted value + * @return mixed converted value or a MDB2 error on failure * @access public */ function convertResult($value, $type) @@ -142,11 +143,11 @@ // {{{ _getTextDeclaration() /** - * Obtain DBMS specific SQL code portion needed to declare an text type + * Obtain DBMS specific SQL code portion needed to declare a 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 + * @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: * @@ -167,9 +168,8 @@ */ function _getTextDeclaration($name, $field) { - $db =& $GLOBALS['_MDB2_databases'][$this->db_index]; - $type = $this->getTypeDeclaration($field); - $default = isset($field['default']) ? ' DEFAULT TIME'. + $type = $this->_getTypeDeclaration($field); + $default = isset($field['default']) ? ' DEFAULT'. $this->quote($field['default'], 'text') : ''; $notnull = isset($field['notnull']) ? ' NOT NULL' : ''; return $name.' '.$type.$default.$notnull; @@ -179,7 +179,7 @@ // {{{ _getCLOBDeclaration() /** - * Obtain DBMS specific SQL code portion needed to declare an character + * Obtain DBMS specific SQL code portion needed to declare a character * large object type field to be used in statements like CREATE TABLE. * * @param string $name name the field to be declared. @@ -201,16 +201,15 @@ */ function _getCLOBDeclaration($name, $field) { - $db =& $GLOBALS['_MDB2_databases'][$this->db_index]; $notnull = isset($field['notnull']) ? ' NOT NULL' : ''; - return $name.' '.$this->getTypeDeclaration($field).$notnull; + return $name.' '.$this->_getTypeDeclaration($field).$notnull; } // }}} // {{{ _getBLOBDeclaration() /** - * Obtain DBMS specific SQL code portion needed to declare an binary large + * Obtain DBMS specific SQL code portion needed to declare a binary large * object type field to be used in statements like CREATE TABLE. * * @param string $name name the field to be declared. @@ -232,9 +231,8 @@ */ function _getBLOBDeclaration($name, $field) { - $db =& $GLOBALS['_MDB2_databases'][$this->db_index]; $notnull = isset($field['notnull']) ? ' NOT NULL' : ''; - return $name.' '.$this->getTypeDeclaration($field).$notnull; + return $name.' '.$this->_getTypeDeclaration($field).$notnull; } // }}} @@ -261,44 +259,10 @@ */ function _getDateDeclaration($name, $field) { - $db =& $GLOBALS['_MDB2_databases'][$this->db_index]; $default = isset($field['default']) ? ' DEFAULT '. $this->quote($field['default'], 'date') : ''; $notnull = isset($field['notnull']) ? ' NOT NULL' : ''; - return $name.' '.$this->getTypeDeclaration($field).$default.$notnull; - } - - // }}} - // {{{ _getTimestampDeclaration() - - /** - * Obtain DBMS specific SQL code portion needed to declare an timestamp - * 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: - * - * default - * Time stamp 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 private - */ - function _getTimestampDeclaration($name, $field) - { - $db =& $GLOBALS['_MDB2_databases'][$this->db_index]; - $default = isset($field['default']) ? ' DEFAULT '. - $this->quote($field['default'], 'timestamp') : ''; - $notnull = isset($field['notnull']) ? ' NOT NULL' : ''; - return $name.' '.$this->getTypeDeclaration($field).$default.$notnull; + return $name.' '.$this->_getTypeDeclaration($field).$default.$notnull; } // }}} @@ -325,11 +289,40 @@ */ function _getTimeDeclaration($name, $field) { - $db =& $GLOBALS['_MDB2_databases'][$this->db_index]; $default = isset($field['default']) ? ' DEFAULT '. $this->quote($field['default'], 'time') : ''; $notnull = isset($field['notnull']) ? ' NOT NULL' : ''; - return $name.' '.$this->getTypeDeclaration($field).$default.$notnull; + return $name.' '.$this->_getTypeDeclaration($field).$default.$notnull; + } + + // }}} + // {{{ _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 private + */ + function _getTimestampDeclaration($name, $field) + { + $default = isset($field['default']) ? ' DEFAULT '. + $this->quote($field['default'], 'timestamp') : ''; + $notnull = isset($field['notnull']) ? ' NOT NULL' : ''; + return $name.' '.$this->_getTypeDeclaration($field).$default.$notnull; } // }}} @@ -356,11 +349,10 @@ */ function _getFloatDeclaration($name, $field) { - $db =& $GLOBALS['_MDB2_databases'][$this->db_index]; $default = isset($field['default']) ? ' DEFAULT '. $this->quote($field['default'], 'float') : ''; $notnull = isset($field['notnull']) ? ' NOT NULL' : ''; - return $name.' '.$this->getTypeDeclaration($field).$default.$notnull; + return $name.' '.$this->_getTypeDeclaration($field).$default.$notnull; } // }}} @@ -387,12 +379,10 @@ */ function _getDecimalDeclaration($name, $field) { - - $db =& $GLOBALS['_MDB2_databases'][$this->db_index]; $default = isset($field['default']) ? ' DEFAULT '. $this->quote($field['default'], 'decimal') : ''; $notnull = isset($field['notnull']) ? ' NOT NULL' : ''; - return $name.' '.$this->getTypeDeclaration($field).$default.$notnull; + return $name.' '.$this->_getTypeDeclaration($field).$default.$notnull; } // }}} @@ -511,10 +501,10 @@ // {{{ _retrieveLOB() /** - * fetch a lob value from a result set + * retrieve LOB from the database * * @param int $lob handle to a lob created by the createLob() function - * @return mixed MDB2_OK on success, a MDB error on failure + * @return mixed MDB2_OK on success, a MDB2 error on failure * @access private */ function _retrieveLOB($lob) @@ -522,7 +512,7 @@ $db =& $GLOBALS['_MDB2_databases'][$this->db_index]; if (!isset($db->lobs[$lob])) { - return $db->raiseError(MDB2_ERROR, NULL, NULL, + return $db->raiseError(MDB2_ERROR_INVALID, null, null, '_retrieveLOB: it was not specified a valid lob'); } @@ -535,7 +525,6 @@ '_retrieveLOB: Could not open fetched large object field' . @ibase_errmsg()); } } - return MDB2_OK; } @@ -571,7 +560,7 @@ * read from the large object input stream * @param int $length integer value that indicates the largest ammount of * data to be read from the large object input stream. - * @return mixed length on success, a MDB error on failure + * @return mixed length on success, a MDB2 error on failure * @access private */ function _readResultLOB($lob, &$data, $length) @@ -613,5 +602,74 @@ } // }}} + // {{{ 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 and the length + * @access public + */ + function mapNativeDatatype($field) + { + $db =& $GLOBALS['_MDB2_databases'][$this->db_index]; + $db_type = preg_replace('/\d/','', strtolower($field['typname']) ); + $length = $field['attlen']; + if ($length == '-1') { + $length = $field['atttypmod']-4; + } + if ((int)$length <= 0) { + $length = null; + } + $type = array(); + switch ($db_type) { + case 'smallint': + case 'integer': + $type[] = 'integer'; + if ($length == '1') { + $type[] = 'boolean'; + } + break; + case 'char': + case 'varchar': + $type[] = 'text'; + if ($length == '1') { + $type[] = 'boolean'; + } + break; + case 'date': + $type[] = 'date'; + $length = null; + break; + case 'timestamp': + $type[] = 'timestamp'; + $length = null; + break; + case 'time': + $type[] = 'time'; + $length = null; + break; + case 'float': + case 'double precision': + $type[] = 'float'; + break; + case 'decimal': + case 'numeric': + $type[] = 'decimal'; + break; + case 'blob': + $type[] = 'blob'; + $length = null; + break; + default: + return $db->raiseError(MDB2_ERROR, null, null, + 'getTableFieldDefinition: unknown database attribute type'); + } + + return array($type, $length); + } + + // }}} } ?> \ No newline at end of file
http://cvs.php.net/diff.php/pear/MDB2/MDB2/Driver/Datatype/ibase.php?r1=1.25&r2=1.26&ty=u Index: pear/MDB2/MDB2/Driver/Datatype/ibase.php diff -u pear/MDB2/MDB2/Driver/Datatype/ibase.php:1.25 pear/MDB2/MDB2/Driver/Datatype/ibase.php:1.26 --- pear/MDB2/MDB2/Driver/Datatype/ibase.php:1.25 Fri Mar 25 13:12:19 2005 +++ pear/MDB2/MDB2/Driver/Datatype/ibase.php Thu Apr 7 18:58:01 2005 @@ -41,30 +41,31 @@ // | POSSIBILITY OF SUCH DAMAGE. | // +----------------------------------------------------------------------+ // | Author: Lukas Smith <smith@backendmedia.com> | +// | Lorenzo Alberton <l.alberton@quipo.it> | // +----------------------------------------------------------------------+ // -// $Id: ibase.php,v 1.25 2005/03/25 18:12:19 lsmith Exp $ -// +// $Id: ibase.php,v 1.26 2005/04/07 22:58:01 quipo Exp $ require_once 'MDB2/Driver/Datatype/Common.php'; /** - * MDB2 MySQL driver + * MDB2 Firebird/Interbase driver * * @package MDB2 * @category Database * @author Lukas Smith <smith@backendmedia.com> + * @author Lorenzo Alberton <l.alberton@quipo.it> */ class MDB2_Driver_Datatype_ibase extends MDB2_Driver_Datatype_Common { // {{{ convertResult() /** - * convert a value to a RDBMS indepdenant MDB2 type + * convert a value to a RDBMS independent MDB2 type * * @param mixed $value value to be converted * @param int $type constant that specifies which type to convert to - * @return mixed converted value + * @return mixed converted value or a MDB2 error on failure * @access public */ function convertResult($value, $type) @@ -142,11 +143,11 @@ // {{{ _getTextDeclaration() /** - * Obtain DBMS specific SQL code portion needed to declare an text type + * Obtain DBMS specific SQL code portion needed to declare a 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 + * @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: * @@ -167,9 +168,8 @@ */ function _getTextDeclaration($name, $field) { - $db =& $GLOBALS['_MDB2_databases'][$this->db_index]; - $type = $this->getTypeDeclaration($field); - $default = isset($field['default']) ? ' DEFAULT TIME'. + $type = $this->_getTypeDeclaration($field); + $default = isset($field['default']) ? ' DEFAULT'. $this->quote($field['default'], 'text') : ''; $notnull = isset($field['notnull']) ? ' NOT NULL' : ''; return $name.' '.$type.$default.$notnull; @@ -179,7 +179,7 @@ // {{{ _getCLOBDeclaration() /** - * Obtain DBMS specific SQL code portion needed to declare an character + * Obtain DBMS specific SQL code portion needed to declare a character * large object type field to be used in statements like CREATE TABLE. * * @param string $name name the field to be declared. @@ -201,16 +201,15 @@ */ function _getCLOBDeclaration($name, $field) { - $db =& $GLOBALS['_MDB2_databases'][$this->db_index]; $notnull = isset($field['notnull']) ? ' NOT NULL' : ''; - return $name.' '.$this->getTypeDeclaration($field).$notnull; + return $name.' '.$this->_getTypeDeclaration($field).$notnull; } // }}} // {{{ _getBLOBDeclaration() /** - * Obtain DBMS specific SQL code portion needed to declare an binary large + * Obtain DBMS specific SQL code portion needed to declare a binary large * object type field to be used in statements like CREATE TABLE. * * @param string $name name the field to be declared. @@ -232,9 +231,8 @@ */ function _getBLOBDeclaration($name, $field) { - $db =& $GLOBALS['_MDB2_databases'][$this->db_index]; $notnull = isset($field['notnull']) ? ' NOT NULL' : ''; - return $name.' '.$this->getTypeDeclaration($field).$notnull; + return $name.' '.$this->_getTypeDeclaration($field).$notnull; } // }}} @@ -261,44 +259,10 @@ */ function _getDateDeclaration($name, $field) { - $db =& $GLOBALS['_MDB2_databases'][$this->db_index]; $default = isset($field['default']) ? ' DEFAULT '. $this->quote($field['default'], 'date') : ''; $notnull = isset($field['notnull']) ? ' NOT NULL' : ''; - return $name.' '.$this->getTypeDeclaration($field).$default.$notnull; - } - - // }}} - // {{{ _getTimestampDeclaration() - - /** - * Obtain DBMS specific SQL code portion needed to declare an timestamp - * 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: - * - * default - * Time stamp 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 private - */ - function _getTimestampDeclaration($name, $field) - { - $db =& $GLOBALS['_MDB2_databases'][$this->db_index]; - $default = isset($field['default']) ? ' DEFAULT '. - $this->quote($field['default'], 'timestamp') : ''; - $notnull = isset($field['notnull']) ? ' NOT NULL' : ''; - return $name.' '.$this->getTypeDeclaration($field).$default.$notnull; + return $name.' '.$this->_getTypeDeclaration($field).$default.$notnull; } // }}} @@ -325,11 +289,40 @@ */ function _getTimeDeclaration($name, $field) { - $db =& $GLOBALS['_MDB2_databases'][$this->db_index]; $default = isset($field['default']) ? ' DEFAULT '. $this->quote($field['default'], 'time') : ''; $notnull = isset($field['notnull']) ? ' NOT NULL' : ''; - return $name.' '.$this->getTypeDeclaration($field).$default.$notnull; + return $name.' '.$this->_getTypeDeclaration($field).$default.$notnull; + } + + // }}} + // {{{ _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 private + */ + function _getTimestampDeclaration($name, $field) + { + $default = isset($field['default']) ? ' DEFAULT '. + $this->quote($field['default'], 'timestamp') : ''; + $notnull = isset($field['notnull']) ? ' NOT NULL' : ''; + return $name.' '.$this->_getTypeDeclaration($field).$default.$notnull; } // }}} @@ -356,11 +349,10 @@ */ function _getFloatDeclaration($name, $field) { - $db =& $GLOBALS['_MDB2_databases'][$this->db_index]; $default = isset($field['default']) ? ' DEFAULT '. $this->quote($field['default'], 'float') : ''; $notnull = isset($field['notnull']) ? ' NOT NULL' : ''; - return $name.' '.$this->getTypeDeclaration($field).$default.$notnull; + return $name.' '.$this->_getTypeDeclaration($field).$default.$notnull; } // }}} @@ -387,12 +379,10 @@ */ function _getDecimalDeclaration($name, $field) { - - $db =& $GLOBALS['_MDB2_databases'][$this->db_index]; $default = isset($field['default']) ? ' DEFAULT '. $this->quote($field['default'], 'decimal') : ''; $notnull = isset($field['notnull']) ? ' NOT NULL' : ''; - return $name.' '.$this->getTypeDeclaration($field).$default.$notnull; + return $name.' '.$this->_getTypeDeclaration($field).$default.$notnull; } // }}} @@ -511,10 +501,10 @@ // {{{ _retrieveLOB() /** - * fetch a lob value from a result set + * retrieve LOB from the database * * @param int $lob handle to a lob created by the createLob() function - * @return mixed MDB2_OK on success, a MDB error on failure + * @return mixed MDB2_OK on success, a MDB2 error on failure * @access private */ function _retrieveLOB($lob) @@ -522,7 +512,7 @@ $db =& $GLOBALS['_MDB2_databases'][$this->db_index]; if (!isset($db->lobs[$lob])) { - return $db->raiseError(MDB2_ERROR, NULL, NULL, + return $db->raiseError(MDB2_ERROR_INVALID, null, null, '_retrieveLOB: it was not specified a valid lob'); } @@ -535,7 +525,6 @@ '_retrieveLOB: Could not open fetched large object field' . @ibase_errmsg()); } } - return MDB2_OK; } @@ -571,7 +560,7 @@ * read from the large object input stream * @param int $length integer value that indicates the largest ammount of * data to be read from the large object input stream. - * @return mixed length on success, a MDB error on failure + * @return mixed length on success, a MDB2 error on failure * @access private */ function _readResultLOB($lob, &$data, $length) @@ -613,5 +602,74 @@ } // }}} + // {{{ 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 and the length + * @access public + */ + function mapNativeDatatype($field) + { + $db =& $GLOBALS['_MDB2_databases'][$this->db_index]; + $db_type = preg_replace('/\d/','', strtolower($field['typname']) ); + $length = $field['attlen']; + if ($length == '-1') { + $length = $field['atttypmod']-4; + } + if ((int)$length <= 0) { + $length = null; + } + $type = array(); + switch ($db_type) { + case 'smallint': + case 'integer': + $type[] = 'integer'; + if ($length == '1') { + $type[] = 'boolean'; + } + break; + case 'char': + case 'varchar': + $type[] = 'text'; + if ($length == '1') { + $type[] = 'boolean'; + } + break; + case 'date': + $type[] = 'date'; + $length = null; + break; + case 'timestamp': + $type[] = 'timestamp'; + $length = null; + break; + case 'time': + $type[] = 'time'; + $length = null; + break; + case 'float': + case 'double precision': + $type[] = 'float'; + break; + case 'decimal': + case 'numeric': + $type[] = 'decimal'; + break; + case 'blob': + $type[] = 'blob'; + $length = null; + break; + default: + return $db->raiseError(MDB2_ERROR, null, null, + 'getTableFieldDefinition: unknown database attribute type'); + } + + return array($type, $length); + } + + // }}} } ?> \ No newline at end of file