cvs: pear /DB_Table/DB/Table/Manager ibase.php pgsql.php sqlite.php

From: Date: Sun, 26 Feb 2006 15:54:56 +0000
Subject: cvs: pear /DB_Table/DB/Table/Manager ibase.php pgsql.php sqlite.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-38642@lists.php.net to get a copy of this message
wiesemann Sun Feb 26 15:54:56 2006 UTC Modified files: /pear/DB_Table/DB/Table/Manager pgsql.php sqlite.php ibase.php Log: - methods as a PEAR::DB workaround for ibase, pgsql, sqlite

http://cvs.php.net/viewcvs.cgi/pear/DB_Table/DB/Table/Manager/pgsql.php?r1=1.1&r2=1.2&diff_format=u Index: pear/DB_Table/DB/Table/Manager/pgsql.php diff -u pear/DB_Table/DB/Table/Manager/pgsql.php:1.1 pear/DB_Table/DB/Table/Manager/pgsql.php:1.2 --- pear/DB_Table/DB/Table/Manager/pgsql.php:1.1 Fri Feb 17 20:27:47 2006 +++ pear/DB_Table/DB/Table/Manager/pgsql.php Sun Feb 26 15:54:56 2006 @@ -13,7 +13,7 @@ * * @license http://www.gnu.org/copyleft/lesser.html LGPL * -* @version $Id: pgsql.php,v 1.1 2006/02/17 20:27:47 wiesemann Exp $ +* @version $Id: pgsql.php,v 1.2 2006/02/26 15:54:56 wiesemann Exp $ * */ @@ -193,6 +193,213 @@ return array_flip($columns); } + + /** + * 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 DB_OK on success, a PEAR error on failure + * @access public + */ + function dropIndex($table, $name) + { + $name = $this->_db->quoteIdentifier($name); + return $this->_db->query("DROP INDEX $name"); + } + + + /** + * 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 + * @return mixed DB_OK on success, a PEAR error on failure + * @access public + */ + function dropConstraint($table, $name) + { + $table = $this->_db->quoteIdentifier($table); + $name = $this->_db->quoteIdentifier($name); + return $this->_db->query("ALTER TABLE $table DROP CONSTRAINT $name"); + } + + + /** + * 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 Metabase 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 Metabase 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 (ignored in DB_Table) + * @access public + * + * @return mixed DB_OK on success, a PEAR error on failure + */ + function alterTable($name, $changes, $check) + { + foreach ($changes as $change_name => $change) { + switch ($change_name) { + case 'add': + case 'remove': + case 'change': + case 'name': + case 'rename': + break; + default: + return DB_Table::throwError(DB_TABLE_ERR_ALTER_TABLE_IMPOS); + } + } + + if (array_key_exists('add', $changes)) { + foreach ($changes['add'] as $field_name => $field) { + $query = 'ADD ' . $field_name . ' ' . $field; + $result = $this->_db->query("ALTER TABLE $name $query"); + if (PEAR::isError($result)) { + return $result; + } + } + } + + if (array_key_exists('remove', $changes)) { + foreach ($changes['remove'] as $field_name => $field) { + $field_name = $this->_db->quoteIdentifier($field_name); + $query = 'DROP ' . $field_name; + $result = $this->_db->query("ALTER TABLE $name $query"); + if (PEAR::isError($result)) { + return $result; + } + } + } + + if (array_key_exists('change', $changes)) { + foreach ($changes['change'] as $field_name => $field) { + $field_name = $this->_db->quoteIdentifier($field_name); + if (array_key_exists('type', $field)) { + $query = "ALTER $field_name TYPE " . $field['definition']; + $result = $this->_db->query("ALTER TABLE $name $query"); + if (PEAR::isError($result)) { + return $result; + } + } +/* default / notnull changes not (yet) supported in DB_Table + if (array_key_exists('default', $field)) { + $query = "ALTER $field_name SET DEFAULT ".$db->quote($field['definition']['default'], $field['definition']['type']); + $result = $db->exec("ALTER TABLE $name $query"); + if (PEAR::isError($result)) { + return $result; + } + } + if (array_key_exists('notnull', $field)) { + $query.= "ALTER $field_name ".($field['definition']['notnull'] ? "SET" : "DROP").' NOT NULL'; + $result = $db->exec("ALTER TABLE $name $query"); + if (PEAR::isError($result)) { + return $result; + } + } +*/ + } + } + + if (array_key_exists('rename', $changes)) { + foreach ($changes['rename'] as $field_name => $field) { + $field_name = $this->_db->quoteIdentifier($field_name); + $result = $this->_db->query("ALTER TABLE $name RENAME COLUMN $field_name TO ".$this->_db->quoteIdentifier($field['name'])); + if (PEAR::isError($result)) { + return $result; + } + } + } + + $name = $this->_db->quoteIdentifier($name); + if (array_key_exists('name', $changes)) { + $change_name = $this->_db->quoteIdentifier($changes['name']); + $result = $this->_db->query("ALTER TABLE $name RENAME TO ".$change_name); + if (PEAR::isError($result)) { + return $result; + } + } + + return DB_OK; + } + } ?> \ No newline at end of file http://cvs.php.net/viewcvs.cgi/pear/DB_Table/DB/Table/Manager/sqlite.php?r1=1.1&r2=1.2&diff_format=u Index: pear/DB_Table/DB/Table/Manager/sqlite.php diff -u pear/DB_Table/DB/Table/Manager/sqlite.php:1.1 pear/DB_Table/DB/Table/Manager/sqlite.php:1.2 --- pear/DB_Table/DB/Table/Manager/sqlite.php:1.1 Fri Feb 17 20:27:47 2006 +++ pear/DB_Table/DB/Table/Manager/sqlite.php Sun Feb 26 15:54:56 2006 @@ -13,7 +13,7 @@ * * @license http://www.gnu.org/copyleft/lesser.html LGPL * -* @version $Id: sqlite.php,v 1.1 2006/02/17 20:27:47 wiesemann Exp $ +* @version $Id: sqlite.php,v 1.2 2006/02/26 15:54:56 wiesemann Exp $ * */ @@ -187,6 +187,201 @@ return $definition; } + + /** + * 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 DB_OK on success, a PEAR error on failure + * @access public + */ + function dropIndex($table, $name) + { + return $this->_db->query("DROP INDEX $name"); + } + + + /** + * 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 + * @return mixed DB_OK on success, a PEAR error on failure + * @access public + */ + function dropConstraint($table, $name) + { + if (strtolower($name) == 'primary') { + return DB_Table::throwError( + DB_TABLE_ERR_ALTER_INDEX_IMPOS, + '(primary)' + ); + } + + return $this->_db->query("DROP INDEX $name"); + } + + + /** + * 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 Metabase 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 Metabase 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 (ignored in DB_Table) + * @access public + * + * @return mixed DB_OK on success, a PEAR error on failure + */ + function alterTable($name, $changes, $check) + { + $version = $this->_getServerVersion(); + foreach ($changes as $change_name => $change) { + switch ($change_name) { + case 'add': + if ($version['major'] >= 3 && $version['minor'] >= 1) { + break; + } + case 'name': + if ($version['major'] >= 3 && $version['minor'] >= 1) { + break; + } + case 'remove': + case 'change': + case 'rename': + default: + return DB_Table::throwError(DB_TABLE_ERR_ALTER_TABLE_IMPOS); + } + } + + $query = ''; + if (array_key_exists('name', $changes)) { + $change_name = $this->_db->quoteIdentifier($changes['name']); + $query .= 'RENAME TO ' . $change_name; + } + + if (array_key_exists('add', $changes)) { + foreach ($changes['add'] as $field_name => $field) { + if ($query) { + $query.= ', '; + } + $query.= 'ADD COLUMN ' . $field_name . ' ' . $field; + } + } + + if (!$query) { + return DB_OK; + } + + $name = $this->_db->quoteIdentifier($name); + return $this->_db->query("ALTER TABLE $name $query"); + } + + + /** + * return version information about the server + * + * @param string $native determines if the raw version string should be returned + * @return mixed array/string with version information or MDB2 error object + * @access private + */ + function _getServerVersion($native = false) + { + if (!function_exists('sqlite_libversion')) { + return 0; // error + } + $server_info = sqlite_libversion(); + if (!$native) { + $tmp = explode('.', $server_info, 3); + $server_info = array( + 'major' => isset($tmp[0]) ? $tmp[0] : null, + 'minor' => isset($tmp[1]) ? $tmp[1] : null, + 'patch' => isset($tmp[2]) ? $tmp[2] : null, + 'extra' => null, + 'native' => $server_info, + ); + } + return $server_info; + } + } ?> \ No newline at end of file http://cvs.php.net/viewcvs.cgi/pear/DB_Table/DB/Table/Manager/ibase.php?r1=1.1&r2=1.2&diff_format=u Index: pear/DB_Table/DB/Table/Manager/ibase.php diff -u pear/DB_Table/DB/Table/Manager/ibase.php:1.1 pear/DB_Table/DB/Table/Manager/ibase.php:1.2 --- pear/DB_Table/DB/Table/Manager/ibase.php:1.1 Fri Feb 17 20:27:47 2006 +++ pear/DB_Table/DB/Table/Manager/ibase.php Sun Feb 26 15:54:56 2006 @@ -13,7 +13,7 @@ * * @license http://www.gnu.org/copyleft/lesser.html LGPL * -* @version $Id: ibase.php,v 1.1 2006/02/17 20:27:47 wiesemann Exp $ +* @version $Id: ibase.php,v 1.2 2006/02/26 15:54:56 wiesemann Exp $ * */ @@ -209,6 +209,190 @@ return $definition; } + + /** + * 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 DB_OK on success, a PEAR error on failure + * @access public + */ + function dropIndex($table, $name) + { + $name = $this->_db->quoteIdentifier($name); + return $this->_db->query("DROP INDEX $name"); + } + + + /** + * 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 + * @return mixed DB_OK on success, a PEAR error on failure + * @access public + */ + function dropConstraint($table, $name) + { + $table = $this->_db->quoteIdentifier($table); + $name = $this->_db->quoteIdentifier($name); + return $this->_db->query("ALTER TABLE $table DROP CONSTRAINT $name"); + } + + + /** + * 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 Metabase 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 Metabase 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 (ignored in DB_Table) + * @access public + * + * @return mixed DB_OK on success, a PEAR error on failure + */ + function alterTable($name, $changes, $check) + { + foreach ($changes as $change_name => $change) { + switch ($change_name) { + case 'add': + case 'remove': + case 'rename': + case 'change': + break; + default: + return DB_Table::throwError(DB_TABLE_ERR_ALTER_TABLE_IMPOS); + } + } + + $query = ''; + if (array_key_exists('add', $changes)) { + foreach ($changes['add'] as $field_name => $field) { + if ($query) { + $query.= ', '; + } + $query.= 'ADD ' . $field_name . ' ' . $field; + } + } + + if (array_key_exists('remove', $changes)) { + foreach ($changes['remove'] as $field_name => $field) { + if ($query) { + $query.= ', '; + } + $field_name = $this->_db->quoteIdentifier($field_name); + $query.= 'DROP ' . $field_name; + } + } + + if (array_key_exists('rename', $changes)) { + foreach ($changes['rename'] as $field_name => $field) { + if ($query) { + $query.= ', '; + } + $field_name = $this->_db->quoteIdentifier($field_name); + $query.= 'ALTER ' . $field_name . ' TO ' . $this->_db->quoteIdentifier($field['name']); + } + } + + if (array_key_exists('change', $changes)) { + // missing support to change DEFAULT and NULLability + foreach ($changes['change'] as $field_name => $field) { + if ($query) { + $query.= ', '; + } + $field_name = $this->_db->quoteIdentifier($field_name); + $query.= 'ALTER ' . $field_name.' TYPE ' . $field['definition']; + } + } + + if (!strlen($query)) { + return DB_OK; + } + + $name = $this->_db->quoteIdentifier($name); + $result = $this->_db->query("ALTER TABLE $name $query"); + return $result; + } + } ?> \ No newline at end of file
« previous php.pear.cvs (#38642) next »