Index: manager_pgsql.php =================================================================== RCS file: /repository/pear/MDB/manager_pgsql.php,v retrieving revision 1.3 diff -u -r1.3 manager_pgsql.php --- manager_pgsql.php 10 Jul 2002 11:17:42 -0000 1.3 +++ manager_pgsql.php 15 Jul 2002 10:41:13 -0000 @@ -53,16 +53,76 @@ class MDB_manager_pgsql_class extends MDB_manager_database_class { + + // }}} + // {{{ createDatabase() + /** + * create a new database + * + * @param MDB object $db Metabase Database Object + * @param string $name name of the database that should be created + * + * @access public + * + * @return mixed DB_OK on success, a DB error on failure + */ function createDatabase(&$db, $name) { return($db->_standaloneQuery("CREATE DATABASE $name")); } + // }}} + // {{{ dropDatabase() + /** + * drop an existing database + * + * @param MDB object $db Metabase Database Object + * @param string $name name of the database that should be dropped + * + * @access public + * + * @return mixed DB_OK on success, a DB error on failure + */ function dropDatabase(&$db, $name) { return($db->_standaloneQuery("DROP DATABASE $name")); } + // }}} + // {{{ createTable() + /** + * create a new table + * + * @param MDB object $db Metabase Database Object + * @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. + * + * Example + * array( + * + * "id" => array( + * "type" => "integer", + * "unsigned" => 1 + * "notnull" => 1 + * "default" => 0 + * ), + * "name" => array( + * "type"=>"text", + * "length"=>12 + * ), + * "password"=>array( + * "type"=>"text", + * "length"=>12 + * ) + * ); + * + * @access public + * + * @return mixed DB_OK on success, a DB error on failure + */ function createTable(&$db, $name, $fields) { if (!isset($name) || !strcmp($name, '')) { @@ -75,47 +135,145 @@ if (!$this->getFieldList($db, $fields, $query_fields)) { return $db->raiseError(DB_ERROR_CANNOT_CREATE, '', '', 'unkown error'); } - return ($db->query("CREATE TABLE $name ($query_fields)")); - } + // }}} + // {{{ alterTable() + /** + * alter an existing table + * + * @param MDB object $db Metabase Database Object + * @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. + * + * AddedFields + * + * 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. + * + * Additionally, there should be an 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. + * + * RemovedFields + * + * 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. + * + * RenamedFields + * + * 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. + * + * ChangedFields + * + * 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 ChangedFields 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. + * + * If the default property is meant to be added, removed or changed, there + * should also be an entry with index ChangedDefault assigned to 1. Similarly, + * if the notnull constraint is to be added or removed, there should also be + * an entry with index ChangedNotNull assigned to 1. + * + * Additionally, there should be an entry named Declaration that is expected + * to contain the portion of the field changed declaration already in DBMS + * specific SQL code as it is used in the CREATE TABLE statement. + * Example + * array( + * "name" => "userlist", + * "AddedFields" => array( + * "quota" => array( + * "type" => "integer", + * "unsigned" => 1 + * "Declaration" => "quota INT" + * ) + * ), + * "RemovedFields" => array( + * "file_limit" => array(), + * "time_limit" => array() + * ), + * "ChangedFields" => array( + * "gender" => array( + * "default" => "M", + * "ChangeDefault" => 1, + * "Declaration" => "gender CHAR(1) DEFAULT 'M'" + * ) + * ), + * "RenamedFields" => array( + * "sex" => array( + * "name" => "gender", + * "Declaration" => "gender CHAR(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 DB_OK on success, a DB error on failure + */ function alterTable($name, &$changes, $check) { - if($check) { - for($change=0, Reset($changes); $change < count($changes); Next($changes), $change++) { - switch(Key($changes)) { + if ($check) { + for ($change = 0, reset($changes); $change < count($changes); next($changes), $change++) { + switch(key($changes)) { case "AddedFields": break; case "RemovedFields": - return($this->SetError("Alter table", "database server does not support dropping table columns")); + return($this->raiseError(DB_ERROR_UNSUPPORTED, '', '', "database server does not support dropping table columns")); case "name": case "RenamedFields": case "ChangedFields": default: - return($this->SetError("Alter table", "change type \"" . Key($changes) . "\" not yet supported")); + return($this->raiseError(DB_ERROR_UNSUPPORTED, '', '', "change type \"" . key($changes) . "\" not yet supported")); } } return (DB_OK); } else { - if(IsSet($changes[$change="name"]) - || IsSet($changes[$change="RenamedFields"]) - || IsSet($changes[$change="ChangedFields"])) - return($this->SetError("Alter table", "change type \"$change\" not yet supported")); + if (isSet($changes[$change="name"]) + || isSet($changes[$change="RenamedFields"]) + || isSet($changes[$change="ChangedFields"])) { + return($this->raiseError(DB_ERROR_UNSUPPORTED, '', '', "change type \"$change\" not yet supported")); + } $query = ""; - if(IsSet($changes["AddedFields"])) { + if (isSet($changes["AddedFields"])) { $fields = $changes["AddedFields"]; - for($field = 0, Reset($fields); $field < count($fields); Next($fields), $field++) { - if(!$this->Query("ALTER TABLE $name ADD " . $fields[Key($fields)]["Declaration"])) { - return(0); + for ($field = 0, reset($fields); $field < count($fields); next($fields), $field++) { + if(!$this->db->query("ALTER TABLE $name ADD " . $fields[key($fields)]["Declaration"])) { + $this->db->pgsqlError(); } } } - if(IsSet($changes["RemovedFields"])) { + if (isSet($changes["RemovedFields"])) { $fields = $changes["RemovedFields"]; - for($field = 0, Reset($fields); $field < count($fields); Next($fields), $field++) { - if(!$this->Query("ALTER TABLE $name DROP " . Key($fields))) { - return(0); + for ($field = 0, reset($fields); $field < count($fields); next($fields), $field++) { + if (!$this->query("ALTER TABLE $name DROP " . key($fields))) { + $this->db->pgsqlError(); } } } @@ -213,12 +371,37 @@ return $db->fetchCol($result, $viewss); } + // }}} + // {{{ createSequence() + /** + * create sequence + * + * @param MDB object $db Metabase Database Object + * @param string $seq_name name of the sequence to be created + * @param string $start start value of the sequence; default is 1 + * + * @access public + * + * @return mixed DB_OK on success, a DB error on failure + */ function createSequence(&$db, $seq_name, $start) { $seqname = $db->getSequenceName($seq_name); return($db->query("CREATE SEQUENCE $seqname INCREMENT 1" . ($start < 1 ? " MINVALUE $start" : "")." START $start")); } + // }}} + // {{{ dropSequence() + /** + * drop existing sequence + * + * @param MDB object $db Metabase Database Object + * @param string $seq_name name of the sequence to be dropped + * + * @access public + * + * @return mixed DB_OK on success, a DB error on failure + */ function dropSequence(&$db, $seq_name) { $seqname = $db->getSequenceName($seq_name);