cvs: pear /MDB common.php manager_common.php manager_mysql.php mysql.php
| From: | Lukas Smith | Date: | Thu, 30 May 2002 14:35:04 +0000 |
| Subject: | cvs: pear /MDB common.php manager_common.php manager_mysql.php mysql.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-4302@lists.php.net to get a copy of this message | ||
lsmith Thu May 30 10:35:04 2002 EDT
Added files:
/pear/MDB manager_mysql.php manager_common.php
Modified files:
/pear/MDB mysql.php common.php
Log:
moved all database management methods into a seperate class that is loaded on demand
Index: pear/MDB/mysql.php diff -u pear/MDB/mysql.php:1.11 pear/MDB/mysql.php:1.12 --- pear/MDB/mysql.php:1.11 Wed May 29 11:06:05 2002 +++ pear/MDB/mysql.php Thu May 30 10:35:02 2002 @@ -2,7 +2,7 @@ /* * mysql.php * - * @(#) $Header: /usr/repository/pear/MDB/mysql.php,v 1.11 2002/05/29 15:06:05 lsmith Exp $ + * @(#) $Header: /usr/repository/pear/MDB/mysql.php,v 1.12 2002/05/30 14:35:02 lsmith Exp $ * */ @@ -24,6 +24,11 @@ var $columns = array(); var $fixed_float = 0; var $escape_quotes = "\\"; + var $sequence_prefix="_sequence_"; + var $dummy_primary_key="dummy_primary_key"; + var $manager_class_name="MDB_manager_mysql_class"; + var $manager_include="manager_mysql.php"; + var $manager_included_constant="MDB_MANAGER_MYSQL_INCLUDED"; // new for PEAR var $errorcode_map = array( 1004 => DB_ERROR_CANNOT_CREATE, @@ -44,7 +49,7 @@ function connect() { - $port = (isset($this->options["Port"]) ? $this->options["Port"] : ""); + $port = (isset($this->options["port"]) ? $this->options["port"] : ""); if ($this->connection != 0) { if (!strcmp($this->connected_host, $this->host) && !strcmp($this->connected_user, $this->user) @@ -73,8 +78,8 @@ return ($this->raiseError(DB_ERROR_CONNECT_FAILED, '', '', $php_errormsg)); } - if (isset($this->options["FixedFloat"])) { - $this->fixed_float = $this->options["FixedFloat"]; + if (isset($this->options["fixedfloat"])) { + $this->fixed_float = $this->options["fixedfloat"]; } else { if (($result = mysql_query("SELECT VERSION()", $this->connection))) { $version = explode(".",mysql_result($result,0,0)); @@ -96,7 +101,7 @@ $this->affected_rows = -1; return (0); } - $this->RegisterTransactionShutdown(0); + $this->registerTransactionShutdown(0); } $this->connected_host = $this->host; $this->connected_user = $this->user; @@ -140,9 +145,12 @@ return $result; } - if (($select = (strtolower(substr(ltrim($query),0,strlen("select"))) == "select")) && $limit > 0) { - $query .= " LIMIT $first, $limit"; + if (gettype($space = strpos($query_string = strtolower(ltrim($query)), " ")) == "integer") { + $query_string=substr($query_string,0,$space); } + if (($select = ($query_string == "select" || $query_string == "show")) && $limit>0) { + $query.=" LIMIT $first,$limit"; +} if (mysql_select_db($this->database_name, $this->connection) && ($result = mysql_query($query, $this->connection))) { @@ -313,40 +321,17 @@ return (mysql_free_result($result)); } - function createDatabase($name) - { - if (MDB::isError($result = $this->connect())) { - return $result; - } - if (!mysql_create_db($name, $this->connection)) { - return $this->mysqlRaiseError(DB_ERROR_CANNOT_CREATE); - } - - return (1); - } - - function dropDatabase($name) - { - if (MDB::isError($result = $this->connect())) { - return $result; - } - if (!mysql_drop_db($name, $this->connection)) { - return $this->mysqlRaiseError(DB_ERROR_CANNOT_DROP); - } - return (1); - } - function getClobDeclaration($name, &$field) { if (isset($field["length"])) { $length = $field["length"]; - if ($length<= 255) { + if ($length <= 255) { $type = "TINYTEXT"; } else { - if ($length<= 65535) { + if ($length <= 65535) { $type = "TEXT"; } else { - if ($length<= 16777215) { + if ($length <= 16777215) { $type = "MEDIUMTEXT"; } else { $type = "LONGTEXT"; @@ -363,22 +348,22 @@ { if (isset($field["length"])) { $length = $field["length"]; - if ($length<= 255) { - $type = "TINYBLob"; + if ($length <= 255) { + $type = "TINYBLOB"; } else { - if ($length<= 65535) { - $type = "BLob"; + if ($length <= 65535) { + $type = "BLOB"; } else { - if ($length<= 16777215) { - $type = "MEDIUMBLob"; + if ($length <= 16777215) { + $type = "MEDIUMBLOB"; } else { - $type = "LONGBLob"; + $type = "LONGBLOB"; } } } } else { - $type = "LONGBLob"; + $type = "LONGBLOB"; } return ("$name $type".(isset($field["notnull"]) ? " NOT NULL" : "")); } @@ -499,158 +484,17 @@ return (mysql_num_fields($result)); } - function createTable($name, &$fields) - { - if (!isset($name) || !strcmp($name, "")) { - return $this->raiseError(DB_ERROR_CANNOT_CREATE, "", "", "no valid table name specified"); - } - if (count($fields) == 0) { - return $this->raiseError(DB_ERROR_CANNOT_CREATE, "", "", 'no fields specified for table "'.$name.'"'); - } - $query_fields = ""; - if (!$this->getFieldList($fields, $query_fields)) { - // XXX needs more checking - return $this->raiseError(DB_ERROR_CANNOT_CREATE, "", "", 'unkown error'); - } - if (isset($this->supported["Transactions"])) { - $query_fields .= ", dummy_primary_key INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (dummy_primary_key)"; - } - return ($this->Query("CREATE TABLE $name ($query_fields)".(isset($this->supported["Transactions"]) ? " TYPE = BDB" : ""))); - } - - function alterTable($name, &$changes, $check) - { - if ($check) { - for($change = 0,reset($changes); - $change < count($changes); - next($changes), $change++) - { - switch(Key($changes)) { - case "AddedFields": - case "RemovedFields": - case "ChangedFields": - case "RenamedFields": - case "name": - break; - default: - return $this->raiseError(DB_ERROR_CANNOT_ALTER, "", "", - 'Alter table: change type "'.Key($changes).'" not yet supported'); - } - } - return (1); - } else { - $query = (isset($changes["name"]) ? "RENAME AS ".$changes["name"] : ""); - if (isset($changes["AddedFields"])) { - $fields = $changes["AddedFields"]; - for($field = 0,reset($fields); - $field<count($fields); - next($fields), $field++) - { - if (strcmp($query, "")) { - $query .= ","; - } - $query .= "ADD ".$fields[key($fields)]["Declaration"]; - } - } - if (isset($changes["RemovedFields"])) { - $fields = $changes["RemovedFields"]; - for($field = 0,reset($fields); - $field<count($fields); - next($fields), $field++) - { - if (strcmp($query, "")) { - $query .= ","; - } - $query .= "DROP ".Key($fields); - } - } - $renamed_fields = array(); - if (isset($changes["RenamedFields"])) { - $fields = $changes["RenamedFields"]; - for($field = 0,reset($fields); - $field<count($fields); - next($fields), $field++) - { - $renamed_fields[$fields[key($fields)]["name"]] = key($fields); - } - } - if (isset($changes["ChangedFields"])) { - $fields = $changes["ChangedFields"]; - for($field = 0,reset($fields); - $field<count($fields); - next($fields), $field++) - { - if (strcmp($query, "")) { - $query .= ","; - } - if (isset($renamed_fields[key($fields)])) { - $field_name = $renamed_fields[key($fields)]; - unset($renamed_fields[key($fields)]); - } else { - $field_name = key($fields); - } - $query .= "CHANGE $field_name ".$fields[key($fields)]["Declaration"]; - } - } - if (count($renamed_fields)) - { - for($field = 0,reset($renamed_fields); - $field<count($renamed_fields); - next($renamed_fields), $field++) - { - if (strcmp($query, "")) { - $query .= ","; - } - $old_field_name = $renamed_fields[Key($renamed_fields)]; - $query .= "CHANGE $old_field_name ".$changes["RenamedFields"][$old_field_name]["Declaration"]; - } - } - return ($this->Query("ALTER TABLE $name $query")); - } - } - - function createSequence($name, $start) - { - $res = $this->query("CREATE TABLE _sequence_$name - (sequence INT DEFAULT 0 NOT NULL AUTO_INCREMENT, PRIMARY KEY (sequence))"); - if (MDB::isError($res)) { - return $res; - } - if ($start == 1) - return 1; - - $res = $this->query("INSERT INTO _sequence_$name (sequence) VALUES (".($start-1).")"); - if (!MDB::isError($res)) { - return 1; - } - - // Handle error - $result = $this->query("DROP TABLE _sequence_$name"); - if (MDB::isError($result)) { - return $this->raiseError(DB_ERROR, "", "", - 'Create sequence: could not drop inconsistent sequence table ('. - $result->getMessage().' ('.$result->getUserinfo().'))'); - } - return $this->raiseError(DB_ERROR, "", "", - 'Create sequence: could not create sequence table ('. - $res->getMessage().' ('.$res->getUserinfo().'))'); - } - - function dropSequence($name) - { - return ($this->query("DROP TABLE _sequence_$name")); - } - // renamed for PEAR // used to be: getSequenceNextValue function nextId($name) { - $res = $this->query("INSERT INTO _sequence_$name (sequence) VALUES (NULL)"); + $sequence_name = $this->sequence_prefix.$name; + $res = $this->query("INSERT INTO $sequence_name (sequence) VALUES (NULL)"); if (MDB::isError($res)) { return $res; } $value = intval(mysql_insert_id()); - $res = $this->query("DELETE FROM _sequence_$name WHERE sequence<$value"); + $res = $this->query("DELETE FROM $sequence_name WHERE sequence < $value"); if (MDB::isError($res)) { // XXX warning or error? // $this->warning = "could delete previous sequence table values"; @@ -664,34 +508,14 @@ // used to be: getSequenceCurrentValue function currId($name) { - $result = $this->Query("SELECT MAX(sequence) FROM _sequence_$name"); + $sequence_name = $this->sequence_prefix.$name; + $result = $this->Query("SELECT MAX(sequence) FROM $sequence_name"); if (MDB::isError($result)) return $result; $value = intval($this->fetch($result,0,0)); $this->FreeResult($result); return ($value); - } - - function createIndex($table, $name, $definition) - { - $query = "ALTER TABLE $table ADD ".(isset($definition["unique"]) ? "UNIQUE" : "INDEX")." $name ("; - for($field = 0,reset($definition["FIELDS"]); - $field<count($definition["FIELDS"]); - $field++,next($definition["FIELDS"])) - { - if ($field>0) { - $query .= ","; - } - $query .= Key($definition["FIELDS"]); - } - $query .= ")"; - return ($this->Query($query)); - } - - function dropIndex($table, $name) - { - return ($this->Query("ALTER TABLE $table DROP INDEX $name")); } function autoCommitTransactions($auto_commit) Index: pear/MDB/common.php diff -u pear/MDB/common.php:1.12 pear/MDB/common.php:1.13 --- pear/MDB/common.php:1.12 Wed May 29 11:03:39 2002 +++ pear/MDB/common.php Thu May 30 10:35:02 2002 @@ -17,13 +17,11 @@ // | | // +----------------------------------------------------------------------+ // -// $Id: common.php,v 1.12 2002/05/29 15:03:39 lsmith Exp $ +// $Id: common.php,v 1.13 2002/05/30 14:35:02 lsmith Exp $ // // Base class for DB implementations. // -require_once("MDB.php"); - define("MDB_TYPE_TEXT",0); define("MDB_TYPE_BOOLEAN",1); define("MDB_TYPE_INTEGER",2); @@ -110,6 +108,11 @@ var $pass_debug_handle = 0; var $fetchmodes = array(); var $error_handler = ""; + var $manager; + var $include_path = ""; + var $manager_included_constant = ""; + var $manager_include = ""; + var $manager_class_name = ""; // new for PEAR var $last_query = ""; @@ -204,104 +207,161 @@ return(0); } + function loadExtension($scope, $extension, $included_constant, $include) + { + if (strlen($included_constant) == 0 + || !defined($included_constant)) + { + $include_path = $this->include_path; + $length = strlen($include_path); + $separator = ""; + if ($length) { + $directory_separator = (defined("DIRECTORY_SEPARATOR") ? DIRECTORY_SEPARATOR : "/"); + if ($include_path[$length-1] != $directory_separator) { + $separator=$directory_separator; + } + } + if (!file_exists($include_path.$separator.$include)) { + $directory = 0; + if (!strcmp($include_path, "") + || ($directory = @opendir($include_path))) + { + if ($directory) { + closedir($directory); + } + return($this->SetError($scope,"it was not specified an existing $extension file ($include)")); + } else { + return($this->SetError($scope,"it was not specified a valid $extension include path")); + } + } + include($include_path.$separator.$include); + } + return(1); + } + + function loadManager($scope) + { + if (isset($this->manager)) { + return(1); + } + if (!$this->loadExtension($scope, "database manager", "MDB_MANAGER_DATABASE_INCLUDED", "manager_common.php")) { + return(0); + } + if (strlen($this->manager_class_name)) { + if(strlen($this->manager_include) == 0) + return($this->SetError($scope, "it was not configured a valid database manager include file")); + if(!$this->loadExtension($scope, "database manager", $this->manager_included_constant, $this->manager_include)) + return(0); + $class_name = $this->manager_class_name; + } else { + $class_name = "MDB_manager_database_class"; + } + $this->manager = new $class_name; + return(1); + } + function createDatabase($database) { - return $this->raiseError(DB_ERROR_UNSUPPORTED, "", "", "Create database: database creation is not supported"); + if(!$this->loadManager("Create database")) { + return(0); + } + return($this->manager->createDatabase($this, $database)); } function dropDatabase($database) { - return $this->raiseError(DB_ERROR_UNSUPPORTED, "", "", "Drop database: database dropping is not supported"); + if(!$this->loadManager("Drop database")) { + return(0); + } + return($this->manager->dropDatabase($this, $database)); } - function getField(&$field, $field_name, &$query) + function createTable($name, &$fields) { - if (!strcmp($field_name, "")) { - //return($this->setError("Get field", "it was not specified a valid field name (\"$field_name\")")); - return $this->raiseError(DB_ERROR_NOSUCHFIELD, "", "", "Get field: it was not specified a valid field name (\"$field_name\")"); + if(!$this->loadManager("Create table")) { + return(0); } - switch($field["type"]) { - case "integer": - $query = $this->getIntegerDeclaration($field_name, $field); - break; - case "text": - $query = $this->getTextDeclaration($field_name, $field); - break; - case "clob": - $query = $this->getCLOBDeclaration($field_name, $field); - break; - case "blob": - $query = $this->getBLOBDeclaration($field_name, $field); - break; - case "boolean": - $query = $this->getBooleanDeclaration($field_name, $field); - break; - case "date": - $query = $this->getDateDeclaration($field_name, $field); - break; - case "timestamp": - $query = $this->getTimestampDeclaration($field_name, $field); - break; - case "time": - $query = $this->getTimeDeclaration($field_name, $field); - break; - case "float": - $query = $this->getFloatDeclaration($field_name, $field); - break; - case "decimal": - $query = $this->getDecimalDeclaration($field_name, $field); - break; - default: - //return($this->setError("Get field", "type \"".$field["type"]."\" is not yet supported")); - return $this->raiseError(DB_ERROR_UNSUPPORTED, "", "", "Get field: type \"".$field["type"]."\" is not yet supported"); - break; + return($this->manager->createTable($this, $name, $fields)); + } + + function dropTable($name) + { + if(!$this->loadManager("Drop table")) { + return(0); } - return(1); + return($this->manager->dropTable($this, $name)); } - function getFieldList(&$fields, &$query_fields) + function alterTable($name, &$changes, $check) { - for($query_fields = "", reset($fields), $field_number = 0; - $field_number < count($fields); - $field_number++,next($fields)) - { - if ($field_number>0) { - $query_fields.= ", "; - } - $field_name = key($fields); - if (!$this->getField($fields[$field_name], $field_name, $query)) { - return(0); - } - $query_fields .= $query; + if(!$this->loadManager("Alter table")) { + return(0); } - return(1); + return($this->manager->alterTable($this, $name, $changes, $check)); } - function createTable($name, &$fields) + function listTables(&$tables) { - if (!isset($name) || !strcmp($name, "")) { - return $this->raiseError(DB_ERROR_CANNOT_CREATE, "", "", "no valid table name specified"); + if(!$this->loadManager("List tables")) { + return(0); } - if (count($fields) == 0) { - return $this->raiseError(DB_ERROR_CANNOT_CREATE, "", "", 'no fields specified for table "'.$name.'"'); + return($this->manager->listTables($this, $tables)); + } + + function listTableFields($table, &$fields) + { + if(!$this->loadManager("List table fields")) { + return(0); } - $query_fields = ""; - if (!$this->getFieldList($fields, $query_fields)) { - // XXX needs more checking - return $this->raiseError(DB_ERROR_CANNOT_CREATE, "", "", 'unkown error'); + return($this->manager->listTableFields($this, $table, $fields)); + } + + function getTableFieldDefinition($table, $field, &$definition) + { + if(!$this->loadManager("Get table field definition")) { + return(0); } - return($this->query("CREATE TABLE $name ($query_fields)")); + return($this->manager->getTableFieldDefinition($this, $table, $field, $definition)); } - function dropTable($name) + function createIndex($table, $name, &$definition) + { + if(!$this->loadManager("Create index")) { + return(0); + } + return($this->manager->createIndex($this, $table, $name, $definition)); + } + + function dropIndex($table, $name) { - return($this->query("DROP TABLE $name")); + if(!$this->loadManager("Drop index")) { + return(0); + } + return($this->manager->dropIndex($this, $table ,$name)); } - function alterTable($name, &$changes, $check) + function createSequence($name, $start) { - return $this->raiseError(DB_ERROR_UNSUPPORTED, "", "", - "Alter table: database table alterations are not supported"); + if(!$this->loadManager("Create sequence")) { + return(0); + } + return($this->manager->createSequence($this, $name, $start)); + } + + function dropSequence($name) + { + if(!$this->loadManager("Drop sequence")) { + return(0); + } + return($this->manager->dropSequence($this, $name)); + } + + function listSequences(&$sequences) + { + if(!$this->loadManager("List sequences")) { + return(0); + } + return($this->manager->listSequences($this, $sequences)); } function query($query) @@ -885,6 +945,48 @@ return(1); } + function fetchDateResult($result, $row, $field) + { + $value = $this->fetch($result,$row,$field); + $this->convertResult($value, MDB_TYPE_DATE); + return($value); + } + + function fetchTimestampResult($result, $row, $field) + { + $value = $this->fetch($result,$row,$field); + $this->convertResult($value, MDB_TYPE_TIMESTAMP); + return($value); + } + + function fetchTimeResult($result, $row, $field) + { + $value = $this->fetch($result, $row, $field); + $this->convertResult($value, MDB_TYPE_TIME); + return($value); + } + + function fetchBooleanResult($result, $row, $field) + { + $value = $this->fetch($result, $row, $field); + $this->convertResult($value, MDB_TYPE_BOOLEAN); + return($value); + } + + function fetchFloatResult($result, $row, $field) + { + $value = $this->fetch($result, $row, $field); + $this->convertResult($value, MDB_TYPE_FLOAT); + return($value); + } + + function fetchDecimalResult($result, $row, $field) + { + $value = $this->fetch($result,$row,$field); + $this->convertResult($value, MDB_TYPE_DECIMAL); + return($value); + } + // renamed for PEAR // used to be: NumberOfColumns function numRows($result) @@ -1052,18 +1154,6 @@ return(isset($this->supported[$feature])); } - function createSequence($name, $start) - { - return $this->raiseError(DB_ERROR_NOT_CAPABLE, "", "", - 'Create Sequence: sequence creation not supported'); - } - - function dropSequence($name) - { - return $this->raiseError(DB_ERROR_NOT_CAPABLE, "", "", - 'Drop Sequence: sequence dropping not supported'); - } - // renamed for PEAR // used to be: getSequencenextValue function nextId($name) @@ -1101,39 +1191,6 @@ 'Rollback transaction: rolling back transactions are not supported'); } - function createIndex($table, $name, $definition) - { - $query = "CREATE"; - if (isset($definition["unique"])) { - $query .= " UNIQUE"; - } - $query .= " INDEX $name ON $table ("; - for($field = 0,reset($definition["FIELDS"]); $field<count($definition["FIELDS"]); $field++,next($definition["FIELDS"])) { - if ($field>0) { - $query.= ", "; - } - $field_name = Key($definition["FIELDS"]); - $query.= $field_name; - if ($this->Support("IndexSorting") && isset($definition["FIELDS"][$field_name]["sorting"])) { - switch($definition["FIELDS"][$field_name]["sorting"]) { - case "ascending": - $query.= " ASC"; - break; - case "descending": - $query.= " DESC"; - break; - } - } - } - $query.= ")"; - return($this->query($query)); - } - - function dropIndex($table, $name) - { - return($this->query("DROP INDEX $name")); - } - function setup() { return(""); @@ -1680,5 +1737,4 @@ return $this->raiseError(DB_ERROR_NOT_CAPABLE); } }; - ?> Index: pear/MDB/manager_mysql.php +++ pear/MDB/manager_mysql.php <? if(!defined("MDB_MANAGER_MYSQL_INCLUDED")) { define("MDB_MANAGER_MYSQL_INCLUDED",1); /* * manager_mysql.php * * @(#) $Header: /usr/repository/pear/MDB/manager_mysql.php,v 1.1 2002/05/30 14:35:02 lsmith Exp $ * */ class MDB_manager_mysql_class extends MDB_manager_database_class { function createDatabase(&$db,$name) { if (MDB::isError($result = $db->connect())) { return $result; } if (!mysql_create_db($name, $db->connection)) { return $db->mysqlRaiseError(DB_ERROR_CANNOT_CREATE); } return (1); } function dropDatabase(&$db,$name) { if (MDB::isError($result = $db->connect())) { return $result; } if (!mysql_drop_db($name, $this->connection)) { return $$db->mysqlRaiseError(DB_ERROR_CANNOT_DROP); } return (1); } function createTable(&$db, $name, &$fields) { if (!isset($name) || !strcmp($name, "")) { return $db->raiseError(DB_ERROR_CANNOT_CREATE, "", "", "no valid table name specified"); } if (count($fields) == 0) { return $db->raiseError(DB_ERROR_CANNOT_CREATE, "", "", 'no fields specified for table "'.$name.'"'); } $query_fields = ""; if (!$this->getFieldList($db, $fields, $query_fields)) { // XXX needs more checking return $db->raiseError(DB_ERROR_CANNOT_CREATE, "", "", 'unkown error'); } if (isset($db->supported["Transactions"])) { $query_fields .= ", dummy_primary_key INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (dummy_primary_key)"; } return ($db->query("CREATE TABLE $name ($query_fields)".(isset($db->supported["Transactions"]) ? " TYPE = BDB" : ""))); } function alterTable(&$db, $name, &$changes, $check) { if ($check) { for($change = 0,reset($changes); $change < count($changes); next($changes), $change++) { switch(Key($changes)) { case "AddedFields": case "RemovedFields": case "ChangedFields": case "RenamedFields": case "name": break; default: return $db->raiseError(DB_ERROR_CANNOT_ALTER, "", "", 'Alter table: change type "'.Key($changes).'" not yet supported'); } } return (1); } else { $query = (isset($changes["name"]) ? "RENAME AS ".$changes["name"] : ""); if (isset($changes["AddedFields"])) { $fields = $changes["AddedFields"]; for($field = 0,reset($fields); $field<count($fields); next($fields), $field++) { if (strcmp($query, "")) { $query .= ","; } $query .= "ADD ".$fields[key($fields)]["Declaration"]; } } if (isset($changes["RemovedFields"])) { $fields = $changes["RemovedFields"]; for($field = 0,reset($fields); $field<count($fields); next($fields), $field++) { if (strcmp($query, "")) { $query .= ","; } $query .= "DROP ".Key($fields); } } $renamed_fields = array(); if (isset($changes["RenamedFields"])) { $fields = $changes["RenamedFields"]; for($field = 0,reset($fields); $field<count($fields); next($fields), $field++) { $renamed_fields[$fields[key($fields)]["name"]] = key($fields); } } if (isset($changes["ChangedFields"])) { $fields = $changes["ChangedFields"]; for($field = 0,reset($fields); $field<count($fields); next($fields), $field++) { if (strcmp($query, "")) { $query .= ","; } if (isset($renamed_fields[key($fields)])) { $field_name = $renamed_fields[key($fields)]; unset($renamed_fields[key($fields)]); } else { $field_name = key($fields); } $query .= "CHANGE $field_name ".$fields[key($fields)]["Declaration"]; } } if (count($renamed_fields)) { for($field = 0,reset($renamed_fields); $field<count($renamed_fields); next($renamed_fields), $field++) { if (strcmp($query, "")) { $query .= ","; } $old_field_name = $renamed_fields[Key($renamed_fields)]; $query .= "CHANGE $old_field_name ".$changes["RenamedFields"][$old_field_name]["Declaration"]; } } return ($db->Query("ALTER TABLE $name $query")); } } function listTables(&$db, &$tables) { if(!$db->queryColumn("SHOW TABLES",$table_names)) { return(0); } $prefix_length = strlen($db->sequence_prefix); for($tables = array(), $table = 0; $table < count($table_names); ++$table) { if (substr($table_names[$table], 0, $prefix_length) != $db->sequence_prefix) $tables[] = $table_names[$table]; } return(1); } function listTableFields(&$db, $table, &$fields) { if(!($result=$db->query("SHOW COLUMNS FROM $table"))) { return(0); } if(!$db->getColumnNames($result, $columns)) { $db->freeResult($result); return(0); } if(!isset($columns["field"])) { $db->freeResult($result); return($db->setError("List table fields","show columns does not return the table field names")); } $field_column=$columns["field"]; for($fields=array(), $field = 0; !$db->endOfResult($result); ++$field) { $field_name = $db->fetch($result, $field, $field_column); if ($field_name != $db->dummy_primary_key) $fields[] = $field_name; } $db->freeResult($result); return(1); } function getTableFieldDefinition(&$db, $table, $field, &$definition) { $field_name = strtolower($field); if ($field_name == $db->dummy_primary_key) { return($db->SetError("List table fields",$db->dummy_primary_key." is an hidden column")); } if (!($result=$db->query("SHOW COLUMNS FROM $table"))) { return(0); } if (!$db->getColumnNames($result,$columns)) { $db->freeResult($result); return(0); } if (!isset($columns[$column = "field"]) && !isset($columns[$column = "type"])) { $db->freeResult($result); return($db->setError("List table fields","show columns does not return the column $column")); } $field_column = $columns["field"]; $type_column = $columns["type"]; while ($success = DB_OK === $db->fetchInto($result, $row)) { if ($field_name == strtolower($row[$field_column])) { $db_type = strtolower($row[$type_column]); $db_type = strtok($db_type, "(), "); if ($db_type == "national") { $db_type = strtok("(), "); } $length = strtok("(), "); $decimal = strtok("(), "); $type = array(); switch($db_type) { case "tinyint": case "smallint": case "mediumint": case "int": case "integer": case "bigint": $type[0] = "integer"; if($length == "1") { $type[1] = "boolean"; } break; case "tinytext": case "mediumtext": case "longtext": case "text": case "char": case "varchar": $type[0] = "text"; if($decimal == "binary") { $type[1] = "blob"; } elseif($length == "1") { $type[1] = "boolean"; } elseif(strstr($db_type, "text")) $type[1] = "clob"; break; case "enum": case "set": $type[0] = "text"; $type[1] = "integer"; break; case "date": $type[0] = "date"; break; case "datetime": case "timestamp": $type[0] = "timestamp"; break; case "time": $type[0] = "time"; break; case "float": case "double": case "real": $type[0] = "float"; break; case "decimal": case "numeric": $type[0] = "decimal"; break; case "tinyblob": case "mediumblob": case "longblob": case "blob": $type[0] = "blob"; break; case "year": $type[0] = "integer"; $type[1] = "date"; break; default: return($db->SetError("List table fields","unknown database attribute type")); } unset($notnull); if (isset($columns["null"]) && $row[$columns["null"]] != "YES") { $notnull=1; } unset($default); if (isset($columns["default"]) && isset($row[$columns["default"]])) { $default=$row[$columns["default"]]; } for($definition = array(), $datatype = 0; $datatype < count($type); $datatype++) { $definition[$datatype] = array("type" => $type[$datatype]); if(isset($notnull)) { $definition[$datatype]["notnull"] = 1; } if(isset($default)) { $definition[$datatype]["default"] = $default; } if(strlen($length)) { $definition[$datatype]["length"] = $length; } } $db->freeResult($result); return(1); } } $db->freeResult($result); if($success == DB_OK) { return(0); } return($db->setError("List table fields","it was not specified an existing table column")); } function createSequence(&$db, $name, $start) { $sequence_name = $db->sequence_prefix.$name; $res = $db->query("CREATE TABLE $sequence_name (sequence INT DEFAULT 0 NOT NULL AUTO_INCREMENT, PRIMARY KEY (sequence))"); if (MDB::isError($res)) { return $res; } if ($start == 1) { return 1; } $res = $db->query("INSERT INTO $sequence_name (sequence) VALUES (".($start-1).")"); if (!MDB::isError($res)) { return 1; } // Handle error $result = $db->query("DROP TABLE $sequence_name"); if (MDB::isError($result)) { return $db->raiseError(DB_ERROR, "", "", 'Create sequence: could not drop inconsistent sequence table ('. $result->getMessage().' ('.$result->getUserinfo().'))'); } return $db->raiseError(DB_ERROR, "", "", 'Create sequence: could not create sequence table ('. $res->getMessage().' ('.$res->getUserinfo().'))'); } function dropSequence(&$db, $name) { $sequence_name = $db->sequence_prefix.$name; return ($db->query("DROP TABLE $sequence_name")); } function createIndex(&$db, $table, $name, $definition) { $query = "ALTER TABLE $table ADD ".(isset($definition["unique"]) ? "UNIQUE" : "INDEX")." $name ("; for($field = 0,reset($definition["FIELDS"]); $field < count($definition["FIELDS"]); $field++, next($definition["FIELDS"])) { if ($field>0) { $query .= ","; } $query .= Key($definition["FIELDS"]); } $query .= ")"; return ($db->query($query)); } function dropIndex(&$db, $table, $name) { return ($db->query("ALTER TABLE $table DROP INDEX $name")); } function listSequences(&$db, &$sequences) { if (!$db->queryColumn("SHOW TABLES",$table_names)) { return(0); } $prefix_length = strlen($db->sequence_prefix); for($sequences = array(),$table = 0;$table < count($table_names); ++$table) { if (substr($table_names[$table], 0, $prefix_length) == $db->sequence_prefix) { $sequences[] = substr($table_names[$table], $prefix_length); } } return(1); } }; } ?> Index: pear/MDB/manager_common.php +++ pear/MDB/manager_common.php <? if(!defined("MDB_MANAGER_DATABASE_INCLUDED")) { define("MDB_MANAGER_DATABASE_INCLUDED",1); /* * manager_database.php * * @(#) $Header: /usr/repository/pear/MDB/manager_common.php,v 1.1 2002/05/30 14:35:02 lsmith Exp $ * */ class MDB_manager_database_class extends PEAR { /* PRIVATE METHODS */ function getField(&$db, &$field, $field_name, &$query) { if (!strcmp($field_name, "")) { //return($this->setError("Get field", "it was not specified a valid field name (\"$field_name\")")); return $db->raiseError(DB_ERROR_NOSUCHFIELD, "", "", "Get field: it was not specified a valid field name (\"$field_name\")"); } switch($field["type"]) { case "integer": $query = $db->getIntegerDeclaration($field_name, $field); break; case "text": $query = $db->getTextDeclaration($field_name, $field); break; case "clob": $query = $db->getCLOBDeclaration($field_name, $field); break; case "blob": $query = $db->getBLOBDeclaration($field_name, $field); break; case "boolean": $query = $db->getBooleanDeclaration($field_name, $field); break; case "date": $query = $db->getDateDeclaration($field_name, $field); break; case "timestamp": $query = $db->getTimestampDeclaration($field_name, $field); break; case "time": $query = $db->getTimeDeclaration($field_name, $field); break; case "float": $query = $db->getFloatDeclaration($field_name, $field); break; case "decimal": $query = $db->getDecimalDeclaration($field_name, $field); break; default: //return($this->setError("Get field", "type \"".$field["type"]."\" is not yet supported")); return $db->raiseError(DB_ERROR_UNSUPPORTED, "", "", "Get field: type \"".$field["type"]."\" is not yet supported"); break; } return(1); } function getFieldList(&$db, &$fields, &$query_fields) { for($query_fields = "", reset($fields), $field_number = 0; $field_number < count($fields); $field_number++,next($fields)) { if ($field_number>0) { $query_fields.= ", "; } $field_name = key($fields); if (!$this->getField(&$db, $fields[$field_name], $field_name, $query)) { return(0); } $query_fields .= $query; } return(1); } /* PUBLIC METHODS */ function createDatabase(&$db, $database) { return $db->raiseError(DB_ERROR_UNSUPPORTED, "", "", "Create database: database creation is not supported"); } function dropDatabase(&$db, $database) { return $db->raiseError(DB_ERROR_UNSUPPORTED, "", "", "Drop database: database dropping is not supported"); } function createTable(&$db, $name, &$fields) { if (!isset($name) || !strcmp($name, "")) { return $db->raiseError(DB_ERROR_CANNOT_CREATE, "", "", "no valid table name specified"); } if (count($fields) == 0) { return $db->raiseError(DB_ERROR_CANNOT_CREATE, "", "", 'no fields specified for table "'.$name.'"'); } $query_fields = ""; if (!$db->getFieldList($db, $fields, $query_fields)) { // XXX needs more checking return $db->raiseError(DB_ERROR_CANNOT_CREATE, "", "", 'unkown error'); } return($db->query("CREATE TABLE $name ($query_fields)")); } function dropTable(&$db, $name) { return($db->query("DROP TABLE $name")); } function alterTable(&$db, $name, &$changes, $check) { return $db->raiseError(DB_ERROR_UNSUPPORTED, "", "", "Alter table: database table alterations are not supported"); } function listTables(&$db, &$tables) { return $db->raiseError(DB_ERROR_NOT_CAPABLE, "", "", 'List tables: list tables is not supported'); } function listTableFields(&$db, $table, &$fields) { return $db->raiseError(DB_ERROR_NOT_CAPABLE, "", "", 'List table fields: list table fields is not supported'); } function getTableFieldDefinition(&$db, $table, $field, $definition) { return $db->raiseError(DB_ERROR_NOT_CAPABLE, "", "", 'Get table field definition: table field definition is not supported'); } function createIndex(&$db, $table, $name, $definition) { $query = "CREATE"; if (isset($definition["unique"])) { $query .= " UNIQUE"; } $query .= " INDEX $name ON $table ("; for($field = 0,reset($definition["FIELDS"]); $field<count($definition["FIELDS"]); $field++,next($definition["FIELDS"])) { if ($field>0) { $query.= ", "; } $field_name = Key($definition["FIELDS"]); $query.= $field_name; if ($db->Support("IndexSorting") && isset($definition["FIELDS"][$field_name]["sorting"])) { switch($definition["FIELDS"][$field_name]["sorting"]) { case "ascending": $query.= " ASC"; break; case "descending": $query.= " DESC"; break; } } } $query.= ")"; return($db->query($query)); } function dropIndex(&$db, $table, $name) { return($db->query("DROP INDEX $name")); } function createSequence(&$db, $name, $start) { return $db->raiseError(DB_ERROR_NOT_CAPABLE, "", "", 'Create Sequence: sequence creation not supported'); } function dropSequence(&$db, $name) { return $db->raiseError(DB_ERROR_NOT_CAPABLE, "", "", 'Drop Sequence: sequence dropping not supported'); } function listSequences(&$db, &$sequences) { return $db->raiseError(DB_ERROR_NOT_CAPABLE, "", "", 'List sequences: List sequences is not supported'); } }; } ?>
Index: pear/MDB/mysql.php diff -u pear/MDB/mysql.php:1.11 pear/MDB/mysql.php:1.12 --- pear/MDB/mysql.php:1.11 Wed May 29 11:06:05 2002 +++ pear/MDB/mysql.php Thu May 30 10:35:02 2002 @@ -2,7 +2,7 @@ /* * mysql.php * - * @(#) $Header: /usr/repository/pear/MDB/mysql.php,v 1.11 2002/05/29 15:06:05 lsmith Exp $ + * @(#) $Header: /usr/repository/pear/MDB/mysql.php,v 1.12 2002/05/30 14:35:02 lsmith Exp $ * */ @@ -24,6 +24,11 @@ var $columns = array(); var $fixed_float = 0; var $escape_quotes = "\\"; + var $sequence_prefix="_sequence_"; + var $dummy_primary_key="dummy_primary_key"; + var $manager_class_name="MDB_manager_mysql_class"; + var $manager_include="manager_mysql.php"; + var $manager_included_constant="MDB_MANAGER_MYSQL_INCLUDED"; // new for PEAR var $errorcode_map = array( 1004 => DB_ERROR_CANNOT_CREATE, @@ -44,7 +49,7 @@ function connect() { - $port = (isset($this->options["Port"]) ? $this->options["Port"] : ""); + $port = (isset($this->options["port"]) ? $this->options["port"] : ""); if ($this->connection != 0) { if (!strcmp($this->connected_host, $this->host) && !strcmp($this->connected_user, $this->user) @@ -73,8 +78,8 @@ return ($this->raiseError(DB_ERROR_CONNECT_FAILED, '', '', $php_errormsg)); } - if (isset($this->options["FixedFloat"])) { - $this->fixed_float = $this->options["FixedFloat"]; + if (isset($this->options["fixedfloat"])) { + $this->fixed_float = $this->options["fixedfloat"]; } else { if (($result = mysql_query("SELECT VERSION()", $this->connection))) { $version = explode(".",mysql_result($result,0,0)); @@ -96,7 +101,7 @@ $this->affected_rows = -1; return (0); } - $this->RegisterTransactionShutdown(0); + $this->registerTransactionShutdown(0); } $this->connected_host = $this->host; $this->connected_user = $this->user; @@ -140,9 +145,12 @@ return $result; } - if (($select = (strtolower(substr(ltrim($query),0,strlen("select"))) == "select")) && $limit > 0) { - $query .= " LIMIT $first, $limit"; + if (gettype($space = strpos($query_string = strtolower(ltrim($query)), " ")) == "integer") { + $query_string=substr($query_string,0,$space); } + if (($select = ($query_string == "select" || $query_string == "show")) && $limit>0) { + $query.=" LIMIT $first,$limit"; +} if (mysql_select_db($this->database_name, $this->connection) && ($result = mysql_query($query, $this->connection))) { @@ -313,40 +321,17 @@ return (mysql_free_result($result)); } - function createDatabase($name) - { - if (MDB::isError($result = $this->connect())) { - return $result; - } - if (!mysql_create_db($name, $this->connection)) { - return $this->mysqlRaiseError(DB_ERROR_CANNOT_CREATE); - } - - return (1); - } - - function dropDatabase($name) - { - if (MDB::isError($result = $this->connect())) { - return $result; - } - if (!mysql_drop_db($name, $this->connection)) { - return $this->mysqlRaiseError(DB_ERROR_CANNOT_DROP); - } - return (1); - } - function getClobDeclaration($name, &$field) { if (isset($field["length"])) { $length = $field["length"]; - if ($length<= 255) { + if ($length <= 255) { $type = "TINYTEXT"; } else { - if ($length<= 65535) { + if ($length <= 65535) { $type = "TEXT"; } else { - if ($length<= 16777215) { + if ($length <= 16777215) { $type = "MEDIUMTEXT"; } else { $type = "LONGTEXT"; @@ -363,22 +348,22 @@ { if (isset($field["length"])) { $length = $field["length"]; - if ($length<= 255) { - $type = "TINYBLob"; + if ($length <= 255) { + $type = "TINYBLOB"; } else { - if ($length<= 65535) { - $type = "BLob"; + if ($length <= 65535) { + $type = "BLOB"; } else { - if ($length<= 16777215) { - $type = "MEDIUMBLob"; + if ($length <= 16777215) { + $type = "MEDIUMBLOB"; } else { - $type = "LONGBLob"; + $type = "LONGBLOB"; } } } } else { - $type = "LONGBLob"; + $type = "LONGBLOB"; } return ("$name $type".(isset($field["notnull"]) ? " NOT NULL" : "")); } @@ -499,158 +484,17 @@ return (mysql_num_fields($result)); } - function createTable($name, &$fields) - { - if (!isset($name) || !strcmp($name, "")) { - return $this->raiseError(DB_ERROR_CANNOT_CREATE, "", "", "no valid table name specified"); - } - if (count($fields) == 0) { - return $this->raiseError(DB_ERROR_CANNOT_CREATE, "", "", 'no fields specified for table "'.$name.'"'); - } - $query_fields = ""; - if (!$this->getFieldList($fields, $query_fields)) { - // XXX needs more checking - return $this->raiseError(DB_ERROR_CANNOT_CREATE, "", "", 'unkown error'); - } - if (isset($this->supported["Transactions"])) { - $query_fields .= ", dummy_primary_key INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (dummy_primary_key)"; - } - return ($this->Query("CREATE TABLE $name ($query_fields)".(isset($this->supported["Transactions"]) ? " TYPE = BDB" : ""))); - } - - function alterTable($name, &$changes, $check) - { - if ($check) { - for($change = 0,reset($changes); - $change < count($changes); - next($changes), $change++) - { - switch(Key($changes)) { - case "AddedFields": - case "RemovedFields": - case "ChangedFields": - case "RenamedFields": - case "name": - break; - default: - return $this->raiseError(DB_ERROR_CANNOT_ALTER, "", "", - 'Alter table: change type "'.Key($changes).'" not yet supported'); - } - } - return (1); - } else { - $query = (isset($changes["name"]) ? "RENAME AS ".$changes["name"] : ""); - if (isset($changes["AddedFields"])) { - $fields = $changes["AddedFields"]; - for($field = 0,reset($fields); - $field<count($fields); - next($fields), $field++) - { - if (strcmp($query, "")) { - $query .= ","; - } - $query .= "ADD ".$fields[key($fields)]["Declaration"]; - } - } - if (isset($changes["RemovedFields"])) { - $fields = $changes["RemovedFields"]; - for($field = 0,reset($fields); - $field<count($fields); - next($fields), $field++) - { - if (strcmp($query, "")) { - $query .= ","; - } - $query .= "DROP ".Key($fields); - } - } - $renamed_fields = array(); - if (isset($changes["RenamedFields"])) { - $fields = $changes["RenamedFields"]; - for($field = 0,reset($fields); - $field<count($fields); - next($fields), $field++) - { - $renamed_fields[$fields[key($fields)]["name"]] = key($fields); - } - } - if (isset($changes["ChangedFields"])) { - $fields = $changes["ChangedFields"]; - for($field = 0,reset($fields); - $field<count($fields); - next($fields), $field++) - { - if (strcmp($query, "")) { - $query .= ","; - } - if (isset($renamed_fields[key($fields)])) { - $field_name = $renamed_fields[key($fields)]; - unset($renamed_fields[key($fields)]); - } else { - $field_name = key($fields); - } - $query .= "CHANGE $field_name ".$fields[key($fields)]["Declaration"]; - } - } - if (count($renamed_fields)) - { - for($field = 0,reset($renamed_fields); - $field<count($renamed_fields); - next($renamed_fields), $field++) - { - if (strcmp($query, "")) { - $query .= ","; - } - $old_field_name = $renamed_fields[Key($renamed_fields)]; - $query .= "CHANGE $old_field_name ".$changes["RenamedFields"][$old_field_name]["Declaration"]; - } - } - return ($this->Query("ALTER TABLE $name $query")); - } - } - - function createSequence($name, $start) - { - $res = $this->query("CREATE TABLE _sequence_$name - (sequence INT DEFAULT 0 NOT NULL AUTO_INCREMENT, PRIMARY KEY (sequence))"); - if (MDB::isError($res)) { - return $res; - } - if ($start == 1) - return 1; - - $res = $this->query("INSERT INTO _sequence_$name (sequence) VALUES (".($start-1).")"); - if (!MDB::isError($res)) { - return 1; - } - - // Handle error - $result = $this->query("DROP TABLE _sequence_$name"); - if (MDB::isError($result)) { - return $this->raiseError(DB_ERROR, "", "", - 'Create sequence: could not drop inconsistent sequence table ('. - $result->getMessage().' ('.$result->getUserinfo().'))'); - } - return $this->raiseError(DB_ERROR, "", "", - 'Create sequence: could not create sequence table ('. - $res->getMessage().' ('.$res->getUserinfo().'))'); - } - - function dropSequence($name) - { - return ($this->query("DROP TABLE _sequence_$name")); - } - // renamed for PEAR // used to be: getSequenceNextValue function nextId($name) { - $res = $this->query("INSERT INTO _sequence_$name (sequence) VALUES (NULL)"); + $sequence_name = $this->sequence_prefix.$name; + $res = $this->query("INSERT INTO $sequence_name (sequence) VALUES (NULL)"); if (MDB::isError($res)) { return $res; } $value = intval(mysql_insert_id()); - $res = $this->query("DELETE FROM _sequence_$name WHERE sequence<$value"); + $res = $this->query("DELETE FROM $sequence_name WHERE sequence < $value"); if (MDB::isError($res)) { // XXX warning or error? // $this->warning = "could delete previous sequence table values"; @@ -664,34 +508,14 @@ // used to be: getSequenceCurrentValue function currId($name) { - $result = $this->Query("SELECT MAX(sequence) FROM _sequence_$name"); + $sequence_name = $this->sequence_prefix.$name; + $result = $this->Query("SELECT MAX(sequence) FROM $sequence_name"); if (MDB::isError($result)) return $result; $value = intval($this->fetch($result,0,0)); $this->FreeResult($result); return ($value); - } - - function createIndex($table, $name, $definition) - { - $query = "ALTER TABLE $table ADD ".(isset($definition["unique"]) ? "UNIQUE" : "INDEX")." $name ("; - for($field = 0,reset($definition["FIELDS"]); - $field<count($definition["FIELDS"]); - $field++,next($definition["FIELDS"])) - { - if ($field>0) { - $query .= ","; - } - $query .= Key($definition["FIELDS"]); - } - $query .= ")"; - return ($this->Query($query)); - } - - function dropIndex($table, $name) - { - return ($this->Query("ALTER TABLE $table DROP INDEX $name")); } function autoCommitTransactions($auto_commit) Index: pear/MDB/common.php diff -u pear/MDB/common.php:1.12 pear/MDB/common.php:1.13 --- pear/MDB/common.php:1.12 Wed May 29 11:03:39 2002 +++ pear/MDB/common.php Thu May 30 10:35:02 2002 @@ -17,13 +17,11 @@ // | | // +----------------------------------------------------------------------+ // -// $Id: common.php,v 1.12 2002/05/29 15:03:39 lsmith Exp $ +// $Id: common.php,v 1.13 2002/05/30 14:35:02 lsmith Exp $ // // Base class for DB implementations. // -require_once("MDB.php"); - define("MDB_TYPE_TEXT",0); define("MDB_TYPE_BOOLEAN",1); define("MDB_TYPE_INTEGER",2); @@ -110,6 +108,11 @@ var $pass_debug_handle = 0; var $fetchmodes = array(); var $error_handler = ""; + var $manager; + var $include_path = ""; + var $manager_included_constant = ""; + var $manager_include = ""; + var $manager_class_name = ""; // new for PEAR var $last_query = ""; @@ -204,104 +207,161 @@ return(0); } + function loadExtension($scope, $extension, $included_constant, $include) + { + if (strlen($included_constant) == 0 + || !defined($included_constant)) + { + $include_path = $this->include_path; + $length = strlen($include_path); + $separator = ""; + if ($length) { + $directory_separator = (defined("DIRECTORY_SEPARATOR") ? DIRECTORY_SEPARATOR : "/"); + if ($include_path[$length-1] != $directory_separator) { + $separator=$directory_separator; + } + } + if (!file_exists($include_path.$separator.$include)) { + $directory = 0; + if (!strcmp($include_path, "") + || ($directory = @opendir($include_path))) + { + if ($directory) { + closedir($directory); + } + return($this->SetError($scope,"it was not specified an existing $extension file ($include)")); + } else { + return($this->SetError($scope,"it was not specified a valid $extension include path")); + } + } + include($include_path.$separator.$include); + } + return(1); + } + + function loadManager($scope) + { + if (isset($this->manager)) { + return(1); + } + if (!$this->loadExtension($scope, "database manager", "MDB_MANAGER_DATABASE_INCLUDED", "manager_common.php")) { + return(0); + } + if (strlen($this->manager_class_name)) { + if(strlen($this->manager_include) == 0) + return($this->SetError($scope, "it was not configured a valid database manager include file")); + if(!$this->loadExtension($scope, "database manager", $this->manager_included_constant, $this->manager_include)) + return(0); + $class_name = $this->manager_class_name; + } else { + $class_name = "MDB_manager_database_class"; + } + $this->manager = new $class_name; + return(1); + } + function createDatabase($database) { - return $this->raiseError(DB_ERROR_UNSUPPORTED, "", "", "Create database: database creation is not supported"); + if(!$this->loadManager("Create database")) { + return(0); + } + return($this->manager->createDatabase($this, $database)); } function dropDatabase($database) { - return $this->raiseError(DB_ERROR_UNSUPPORTED, "", "", "Drop database: database dropping is not supported"); + if(!$this->loadManager("Drop database")) { + return(0); + } + return($this->manager->dropDatabase($this, $database)); } - function getField(&$field, $field_name, &$query) + function createTable($name, &$fields) { - if (!strcmp($field_name, "")) { - //return($this->setError("Get field", "it was not specified a valid field name (\"$field_name\")")); - return $this->raiseError(DB_ERROR_NOSUCHFIELD, "", "", "Get field: it was not specified a valid field name (\"$field_name\")"); + if(!$this->loadManager("Create table")) { + return(0); } - switch($field["type"]) { - case "integer": - $query = $this->getIntegerDeclaration($field_name, $field); - break; - case "text": - $query = $this->getTextDeclaration($field_name, $field); - break; - case "clob": - $query = $this->getCLOBDeclaration($field_name, $field); - break; - case "blob": - $query = $this->getBLOBDeclaration($field_name, $field); - break; - case "boolean": - $query = $this->getBooleanDeclaration($field_name, $field); - break; - case "date": - $query = $this->getDateDeclaration($field_name, $field); - break; - case "timestamp": - $query = $this->getTimestampDeclaration($field_name, $field); - break; - case "time": - $query = $this->getTimeDeclaration($field_name, $field); - break; - case "float": - $query = $this->getFloatDeclaration($field_name, $field); - break; - case "decimal": - $query = $this->getDecimalDeclaration($field_name, $field); - break; - default: - //return($this->setError("Get field", "type \"".$field["type"]."\" is not yet supported")); - return $this->raiseError(DB_ERROR_UNSUPPORTED, "", "", "Get field: type \"".$field["type"]."\" is not yet supported"); - break; + return($this->manager->createTable($this, $name, $fields)); + } + + function dropTable($name) + { + if(!$this->loadManager("Drop table")) { + return(0); } - return(1); + return($this->manager->dropTable($this, $name)); } - function getFieldList(&$fields, &$query_fields) + function alterTable($name, &$changes, $check) { - for($query_fields = "", reset($fields), $field_number = 0; - $field_number < count($fields); - $field_number++,next($fields)) - { - if ($field_number>0) { - $query_fields.= ", "; - } - $field_name = key($fields); - if (!$this->getField($fields[$field_name], $field_name, $query)) { - return(0); - } - $query_fields .= $query; + if(!$this->loadManager("Alter table")) { + return(0); } - return(1); + return($this->manager->alterTable($this, $name, $changes, $check)); } - function createTable($name, &$fields) + function listTables(&$tables) { - if (!isset($name) || !strcmp($name, "")) { - return $this->raiseError(DB_ERROR_CANNOT_CREATE, "", "", "no valid table name specified"); + if(!$this->loadManager("List tables")) { + return(0); } - if (count($fields) == 0) { - return $this->raiseError(DB_ERROR_CANNOT_CREATE, "", "", 'no fields specified for table "'.$name.'"'); + return($this->manager->listTables($this, $tables)); + } + + function listTableFields($table, &$fields) + { + if(!$this->loadManager("List table fields")) { + return(0); } - $query_fields = ""; - if (!$this->getFieldList($fields, $query_fields)) { - // XXX needs more checking - return $this->raiseError(DB_ERROR_CANNOT_CREATE, "", "", 'unkown error'); + return($this->manager->listTableFields($this, $table, $fields)); + } + + function getTableFieldDefinition($table, $field, &$definition) + { + if(!$this->loadManager("Get table field definition")) { + return(0); } - return($this->query("CREATE TABLE $name ($query_fields)")); + return($this->manager->getTableFieldDefinition($this, $table, $field, $definition)); } - function dropTable($name) + function createIndex($table, $name, &$definition) + { + if(!$this->loadManager("Create index")) { + return(0); + } + return($this->manager->createIndex($this, $table, $name, $definition)); + } + + function dropIndex($table, $name) { - return($this->query("DROP TABLE $name")); + if(!$this->loadManager("Drop index")) { + return(0); + } + return($this->manager->dropIndex($this, $table ,$name)); } - function alterTable($name, &$changes, $check) + function createSequence($name, $start) { - return $this->raiseError(DB_ERROR_UNSUPPORTED, "", "", - "Alter table: database table alterations are not supported"); + if(!$this->loadManager("Create sequence")) { + return(0); + } + return($this->manager->createSequence($this, $name, $start)); + } + + function dropSequence($name) + { + if(!$this->loadManager("Drop sequence")) { + return(0); + } + return($this->manager->dropSequence($this, $name)); + } + + function listSequences(&$sequences) + { + if(!$this->loadManager("List sequences")) { + return(0); + } + return($this->manager->listSequences($this, $sequences)); } function query($query) @@ -885,6 +945,48 @@ return(1); } + function fetchDateResult($result, $row, $field) + { + $value = $this->fetch($result,$row,$field); + $this->convertResult($value, MDB_TYPE_DATE); + return($value); + } + + function fetchTimestampResult($result, $row, $field) + { + $value = $this->fetch($result,$row,$field); + $this->convertResult($value, MDB_TYPE_TIMESTAMP); + return($value); + } + + function fetchTimeResult($result, $row, $field) + { + $value = $this->fetch($result, $row, $field); + $this->convertResult($value, MDB_TYPE_TIME); + return($value); + } + + function fetchBooleanResult($result, $row, $field) + { + $value = $this->fetch($result, $row, $field); + $this->convertResult($value, MDB_TYPE_BOOLEAN); + return($value); + } + + function fetchFloatResult($result, $row, $field) + { + $value = $this->fetch($result, $row, $field); + $this->convertResult($value, MDB_TYPE_FLOAT); + return($value); + } + + function fetchDecimalResult($result, $row, $field) + { + $value = $this->fetch($result,$row,$field); + $this->convertResult($value, MDB_TYPE_DECIMAL); + return($value); + } + // renamed for PEAR // used to be: NumberOfColumns function numRows($result) @@ -1052,18 +1154,6 @@ return(isset($this->supported[$feature])); } - function createSequence($name, $start) - { - return $this->raiseError(DB_ERROR_NOT_CAPABLE, "", "", - 'Create Sequence: sequence creation not supported'); - } - - function dropSequence($name) - { - return $this->raiseError(DB_ERROR_NOT_CAPABLE, "", "", - 'Drop Sequence: sequence dropping not supported'); - } - // renamed for PEAR // used to be: getSequencenextValue function nextId($name) @@ -1101,39 +1191,6 @@ 'Rollback transaction: rolling back transactions are not supported'); } - function createIndex($table, $name, $definition) - { - $query = "CREATE"; - if (isset($definition["unique"])) { - $query .= " UNIQUE"; - } - $query .= " INDEX $name ON $table ("; - for($field = 0,reset($definition["FIELDS"]); $field<count($definition["FIELDS"]); $field++,next($definition["FIELDS"])) { - if ($field>0) { - $query.= ", "; - } - $field_name = Key($definition["FIELDS"]); - $query.= $field_name; - if ($this->Support("IndexSorting") && isset($definition["FIELDS"][$field_name]["sorting"])) { - switch($definition["FIELDS"][$field_name]["sorting"]) { - case "ascending": - $query.= " ASC"; - break; - case "descending": - $query.= " DESC"; - break; - } - } - } - $query.= ")"; - return($this->query($query)); - } - - function dropIndex($table, $name) - { - return($this->query("DROP INDEX $name")); - } - function setup() { return(""); @@ -1680,5 +1737,4 @@ return $this->raiseError(DB_ERROR_NOT_CAPABLE); } }; - ?> Index: pear/MDB/manager_mysql.php +++ pear/MDB/manager_mysql.php <? if(!defined("MDB_MANAGER_MYSQL_INCLUDED")) { define("MDB_MANAGER_MYSQL_INCLUDED",1); /* * manager_mysql.php * * @(#) $Header: /usr/repository/pear/MDB/manager_mysql.php,v 1.1 2002/05/30 14:35:02 lsmith Exp $ * */ class MDB_manager_mysql_class extends MDB_manager_database_class { function createDatabase(&$db,$name) { if (MDB::isError($result = $db->connect())) { return $result; } if (!mysql_create_db($name, $db->connection)) { return $db->mysqlRaiseError(DB_ERROR_CANNOT_CREATE); } return (1); } function dropDatabase(&$db,$name) { if (MDB::isError($result = $db->connect())) { return $result; } if (!mysql_drop_db($name, $this->connection)) { return $$db->mysqlRaiseError(DB_ERROR_CANNOT_DROP); } return (1); } function createTable(&$db, $name, &$fields) { if (!isset($name) || !strcmp($name, "")) { return $db->raiseError(DB_ERROR_CANNOT_CREATE, "", "", "no valid table name specified"); } if (count($fields) == 0) { return $db->raiseError(DB_ERROR_CANNOT_CREATE, "", "", 'no fields specified for table "'.$name.'"'); } $query_fields = ""; if (!$this->getFieldList($db, $fields, $query_fields)) { // XXX needs more checking return $db->raiseError(DB_ERROR_CANNOT_CREATE, "", "", 'unkown error'); } if (isset($db->supported["Transactions"])) { $query_fields .= ", dummy_primary_key INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (dummy_primary_key)"; } return ($db->query("CREATE TABLE $name ($query_fields)".(isset($db->supported["Transactions"]) ? " TYPE = BDB" : ""))); } function alterTable(&$db, $name, &$changes, $check) { if ($check) { for($change = 0,reset($changes); $change < count($changes); next($changes), $change++) { switch(Key($changes)) { case "AddedFields": case "RemovedFields": case "ChangedFields": case "RenamedFields": case "name": break; default: return $db->raiseError(DB_ERROR_CANNOT_ALTER, "", "", 'Alter table: change type "'.Key($changes).'" not yet supported'); } } return (1); } else { $query = (isset($changes["name"]) ? "RENAME AS ".$changes["name"] : ""); if (isset($changes["AddedFields"])) { $fields = $changes["AddedFields"]; for($field = 0,reset($fields); $field<count($fields); next($fields), $field++) { if (strcmp($query, "")) { $query .= ","; } $query .= "ADD ".$fields[key($fields)]["Declaration"]; } } if (isset($changes["RemovedFields"])) { $fields = $changes["RemovedFields"]; for($field = 0,reset($fields); $field<count($fields); next($fields), $field++) { if (strcmp($query, "")) { $query .= ","; } $query .= "DROP ".Key($fields); } } $renamed_fields = array(); if (isset($changes["RenamedFields"])) { $fields = $changes["RenamedFields"]; for($field = 0,reset($fields); $field<count($fields); next($fields), $field++) { $renamed_fields[$fields[key($fields)]["name"]] = key($fields); } } if (isset($changes["ChangedFields"])) { $fields = $changes["ChangedFields"]; for($field = 0,reset($fields); $field<count($fields); next($fields), $field++) { if (strcmp($query, "")) { $query .= ","; } if (isset($renamed_fields[key($fields)])) { $field_name = $renamed_fields[key($fields)]; unset($renamed_fields[key($fields)]); } else { $field_name = key($fields); } $query .= "CHANGE $field_name ".$fields[key($fields)]["Declaration"]; } } if (count($renamed_fields)) { for($field = 0,reset($renamed_fields); $field<count($renamed_fields); next($renamed_fields), $field++) { if (strcmp($query, "")) { $query .= ","; } $old_field_name = $renamed_fields[Key($renamed_fields)]; $query .= "CHANGE $old_field_name ".$changes["RenamedFields"][$old_field_name]["Declaration"]; } } return ($db->Query("ALTER TABLE $name $query")); } } function listTables(&$db, &$tables) { if(!$db->queryColumn("SHOW TABLES",$table_names)) { return(0); } $prefix_length = strlen($db->sequence_prefix); for($tables = array(), $table = 0; $table < count($table_names); ++$table) { if (substr($table_names[$table], 0, $prefix_length) != $db->sequence_prefix) $tables[] = $table_names[$table]; } return(1); } function listTableFields(&$db, $table, &$fields) { if(!($result=$db->query("SHOW COLUMNS FROM $table"))) { return(0); } if(!$db->getColumnNames($result, $columns)) { $db->freeResult($result); return(0); } if(!isset($columns["field"])) { $db->freeResult($result); return($db->setError("List table fields","show columns does not return the table field names")); } $field_column=$columns["field"]; for($fields=array(), $field = 0; !$db->endOfResult($result); ++$field) { $field_name = $db->fetch($result, $field, $field_column); if ($field_name != $db->dummy_primary_key) $fields[] = $field_name; } $db->freeResult($result); return(1); } function getTableFieldDefinition(&$db, $table, $field, &$definition) { $field_name = strtolower($field); if ($field_name == $db->dummy_primary_key) { return($db->SetError("List table fields",$db->dummy_primary_key." is an hidden column")); } if (!($result=$db->query("SHOW COLUMNS FROM $table"))) { return(0); } if (!$db->getColumnNames($result,$columns)) { $db->freeResult($result); return(0); } if (!isset($columns[$column = "field"]) && !isset($columns[$column = "type"])) { $db->freeResult($result); return($db->setError("List table fields","show columns does not return the column $column")); } $field_column = $columns["field"]; $type_column = $columns["type"]; while ($success = DB_OK === $db->fetchInto($result, $row)) { if ($field_name == strtolower($row[$field_column])) { $db_type = strtolower($row[$type_column]); $db_type = strtok($db_type, "(), "); if ($db_type == "national") { $db_type = strtok("(), "); } $length = strtok("(), "); $decimal = strtok("(), "); $type = array(); switch($db_type) { case "tinyint": case "smallint": case "mediumint": case "int": case "integer": case "bigint": $type[0] = "integer"; if($length == "1") { $type[1] = "boolean"; } break; case "tinytext": case "mediumtext": case "longtext": case "text": case "char": case "varchar": $type[0] = "text"; if($decimal == "binary") { $type[1] = "blob"; } elseif($length == "1") { $type[1] = "boolean"; } elseif(strstr($db_type, "text")) $type[1] = "clob"; break; case "enum": case "set": $type[0] = "text"; $type[1] = "integer"; break; case "date": $type[0] = "date"; break; case "datetime": case "timestamp": $type[0] = "timestamp"; break; case "time": $type[0] = "time"; break; case "float": case "double": case "real": $type[0] = "float"; break; case "decimal": case "numeric": $type[0] = "decimal"; break; case "tinyblob": case "mediumblob": case "longblob": case "blob": $type[0] = "blob"; break; case "year": $type[0] = "integer"; $type[1] = "date"; break; default: return($db->SetError("List table fields","unknown database attribute type")); } unset($notnull); if (isset($columns["null"]) && $row[$columns["null"]] != "YES") { $notnull=1; } unset($default); if (isset($columns["default"]) && isset($row[$columns["default"]])) { $default=$row[$columns["default"]]; } for($definition = array(), $datatype = 0; $datatype < count($type); $datatype++) { $definition[$datatype] = array("type" => $type[$datatype]); if(isset($notnull)) { $definition[$datatype]["notnull"] = 1; } if(isset($default)) { $definition[$datatype]["default"] = $default; } if(strlen($length)) { $definition[$datatype]["length"] = $length; } } $db->freeResult($result); return(1); } } $db->freeResult($result); if($success == DB_OK) { return(0); } return($db->setError("List table fields","it was not specified an existing table column")); } function createSequence(&$db, $name, $start) { $sequence_name = $db->sequence_prefix.$name; $res = $db->query("CREATE TABLE $sequence_name (sequence INT DEFAULT 0 NOT NULL AUTO_INCREMENT, PRIMARY KEY (sequence))"); if (MDB::isError($res)) { return $res; } if ($start == 1) { return 1; } $res = $db->query("INSERT INTO $sequence_name (sequence) VALUES (".($start-1).")"); if (!MDB::isError($res)) { return 1; } // Handle error $result = $db->query("DROP TABLE $sequence_name"); if (MDB::isError($result)) { return $db->raiseError(DB_ERROR, "", "", 'Create sequence: could not drop inconsistent sequence table ('. $result->getMessage().' ('.$result->getUserinfo().'))'); } return $db->raiseError(DB_ERROR, "", "", 'Create sequence: could not create sequence table ('. $res->getMessage().' ('.$res->getUserinfo().'))'); } function dropSequence(&$db, $name) { $sequence_name = $db->sequence_prefix.$name; return ($db->query("DROP TABLE $sequence_name")); } function createIndex(&$db, $table, $name, $definition) { $query = "ALTER TABLE $table ADD ".(isset($definition["unique"]) ? "UNIQUE" : "INDEX")." $name ("; for($field = 0,reset($definition["FIELDS"]); $field < count($definition["FIELDS"]); $field++, next($definition["FIELDS"])) { if ($field>0) { $query .= ","; } $query .= Key($definition["FIELDS"]); } $query .= ")"; return ($db->query($query)); } function dropIndex(&$db, $table, $name) { return ($db->query("ALTER TABLE $table DROP INDEX $name")); } function listSequences(&$db, &$sequences) { if (!$db->queryColumn("SHOW TABLES",$table_names)) { return(0); } $prefix_length = strlen($db->sequence_prefix); for($sequences = array(),$table = 0;$table < count($table_names); ++$table) { if (substr($table_names[$table], 0, $prefix_length) == $db->sequence_prefix) { $sequences[] = substr($table_names[$table], $prefix_length); } } return(1); } }; } ?> Index: pear/MDB/manager_common.php +++ pear/MDB/manager_common.php <? if(!defined("MDB_MANAGER_DATABASE_INCLUDED")) { define("MDB_MANAGER_DATABASE_INCLUDED",1); /* * manager_database.php * * @(#) $Header: /usr/repository/pear/MDB/manager_common.php,v 1.1 2002/05/30 14:35:02 lsmith Exp $ * */ class MDB_manager_database_class extends PEAR { /* PRIVATE METHODS */ function getField(&$db, &$field, $field_name, &$query) { if (!strcmp($field_name, "")) { //return($this->setError("Get field", "it was not specified a valid field name (\"$field_name\")")); return $db->raiseError(DB_ERROR_NOSUCHFIELD, "", "", "Get field: it was not specified a valid field name (\"$field_name\")"); } switch($field["type"]) { case "integer": $query = $db->getIntegerDeclaration($field_name, $field); break; case "text": $query = $db->getTextDeclaration($field_name, $field); break; case "clob": $query = $db->getCLOBDeclaration($field_name, $field); break; case "blob": $query = $db->getBLOBDeclaration($field_name, $field); break; case "boolean": $query = $db->getBooleanDeclaration($field_name, $field); break; case "date": $query = $db->getDateDeclaration($field_name, $field); break; case "timestamp": $query = $db->getTimestampDeclaration($field_name, $field); break; case "time": $query = $db->getTimeDeclaration($field_name, $field); break; case "float": $query = $db->getFloatDeclaration($field_name, $field); break; case "decimal": $query = $db->getDecimalDeclaration($field_name, $field); break; default: //return($this->setError("Get field", "type \"".$field["type"]."\" is not yet supported")); return $db->raiseError(DB_ERROR_UNSUPPORTED, "", "", "Get field: type \"".$field["type"]."\" is not yet supported"); break; } return(1); } function getFieldList(&$db, &$fields, &$query_fields) { for($query_fields = "", reset($fields), $field_number = 0; $field_number < count($fields); $field_number++,next($fields)) { if ($field_number>0) { $query_fields.= ", "; } $field_name = key($fields); if (!$this->getField(&$db, $fields[$field_name], $field_name, $query)) { return(0); } $query_fields .= $query; } return(1); } /* PUBLIC METHODS */ function createDatabase(&$db, $database) { return $db->raiseError(DB_ERROR_UNSUPPORTED, "", "", "Create database: database creation is not supported"); } function dropDatabase(&$db, $database) { return $db->raiseError(DB_ERROR_UNSUPPORTED, "", "", "Drop database: database dropping is not supported"); } function createTable(&$db, $name, &$fields) { if (!isset($name) || !strcmp($name, "")) { return $db->raiseError(DB_ERROR_CANNOT_CREATE, "", "", "no valid table name specified"); } if (count($fields) == 0) { return $db->raiseError(DB_ERROR_CANNOT_CREATE, "", "", 'no fields specified for table "'.$name.'"'); } $query_fields = ""; if (!$db->getFieldList($db, $fields, $query_fields)) { // XXX needs more checking return $db->raiseError(DB_ERROR_CANNOT_CREATE, "", "", 'unkown error'); } return($db->query("CREATE TABLE $name ($query_fields)")); } function dropTable(&$db, $name) { return($db->query("DROP TABLE $name")); } function alterTable(&$db, $name, &$changes, $check) { return $db->raiseError(DB_ERROR_UNSUPPORTED, "", "", "Alter table: database table alterations are not supported"); } function listTables(&$db, &$tables) { return $db->raiseError(DB_ERROR_NOT_CAPABLE, "", "", 'List tables: list tables is not supported'); } function listTableFields(&$db, $table, &$fields) { return $db->raiseError(DB_ERROR_NOT_CAPABLE, "", "", 'List table fields: list table fields is not supported'); } function getTableFieldDefinition(&$db, $table, $field, $definition) { return $db->raiseError(DB_ERROR_NOT_CAPABLE, "", "", 'Get table field definition: table field definition is not supported'); } function createIndex(&$db, $table, $name, $definition) { $query = "CREATE"; if (isset($definition["unique"])) { $query .= " UNIQUE"; } $query .= " INDEX $name ON $table ("; for($field = 0,reset($definition["FIELDS"]); $field<count($definition["FIELDS"]); $field++,next($definition["FIELDS"])) { if ($field>0) { $query.= ", "; } $field_name = Key($definition["FIELDS"]); $query.= $field_name; if ($db->Support("IndexSorting") && isset($definition["FIELDS"][$field_name]["sorting"])) { switch($definition["FIELDS"][$field_name]["sorting"]) { case "ascending": $query.= " ASC"; break; case "descending": $query.= " DESC"; break; } } } $query.= ")"; return($db->query($query)); } function dropIndex(&$db, $table, $name) { return($db->query("DROP INDEX $name")); } function createSequence(&$db, $name, $start) { return $db->raiseError(DB_ERROR_NOT_CAPABLE, "", "", 'Create Sequence: sequence creation not supported'); } function dropSequence(&$db, $name) { return $db->raiseError(DB_ERROR_NOT_CAPABLE, "", "", 'Drop Sequence: sequence dropping not supported'); } function listSequences(&$db, &$sequences) { return $db->raiseError(DB_ERROR_NOT_CAPABLE, "", "", 'List sequences: List sequences is not supported'); } }; } ?>