From a6bd3a125347d33707cf432d9de3df291c024010 Mon Sep 17 00:00:00 2001 From: chriskl Date: Thu, 1 May 2003 03:27:54 +0000 Subject: [PATCH] cascade drop for columns and constraints. remove unused functions. partial fix of insert all nulls bug. --- HISTORY | 7 ++++- classes/database/ADODB_base.php | 5 +-- classes/database/BaseDB.php | 34 +++++++++++++++----- classes/database/Postgres.php | 16 ++-------- classes/database/Postgres72.php | 6 ++-- classes/database/Postgres73.php | 55 +++++++-------------------------- constraints.php | 8 +++-- tblproperties.php | 12 ++++--- 8 files changed, 66 insertions(+), 77 deletions(-) diff --git a/HISTORY b/HISTORY index 35ac5307..e65bf063 100644 --- a/HISTORY +++ b/HISTORY @@ -1,6 +1,11 @@ phpPgAdmin History ------------------ +Version 3.0.0-dev-5 +------------------- + +* Cascade drop on columns and constraints + Version 3.0.0-dev-4 ------------------- @@ -17,7 +22,7 @@ Version 3.0.0-dev-3 ------------------- * French translation -* Russion translations +* Russian translations * Japanese translations * Trigger definitions * ADODB upgrade diff --git a/classes/database/ADODB_base.php b/classes/database/ADODB_base.php index 3b43228e..1570cc52 100644 --- a/classes/database/ADODB_base.php +++ b/classes/database/ADODB_base.php @@ -3,7 +3,7 @@ /* * Parent class of all ADODB objects. * - * $Id: ADODB_base.php,v 1.11 2003/03/27 13:47:15 chriskl Exp $ + * $Id: ADODB_base.php,v 1.12 2003/05/01 03:27:54 chriskl Exp $ */ include_once('libraries/errorhandler.inc.php'); @@ -180,9 +180,6 @@ class ADODB_base { else $values = ") VALUES ('{$value}'"; } $sql = $fields . $values . ')'; - } else { - // @@ THIS IS TOTALLY WRONG!!! - $sql = "INSERT INTO \"{$table}\" DEFAULT VALUES"; } // Check for failures diff --git a/classes/database/BaseDB.php b/classes/database/BaseDB.php index 91baf2bb..bb005210 100644 --- a/classes/database/BaseDB.php +++ b/classes/database/BaseDB.php @@ -4,7 +4,7 @@ * A class that implements the DB interface for Postgres * Note: This class uses ADODB and returns RecordSets. * - * $Id: BaseDB.php,v 1.13 2003/04/30 06:35:41 chriskl Exp $ + * $Id: BaseDB.php,v 1.14 2003/05/01 03:27:54 chriskl Exp $ */ include_once('classes/database/ADODB_base.php'); @@ -69,16 +69,34 @@ class BaseDB extends ADODB_base { * @return 0 success * @return -1 invalid parameters */ - function insertRow($table, $values, $nulls) { - if (!is_array($values) || !is_array($nulls)) return -1; + function insertRow($table, $vars, $nulls) { + if (!is_array($vars) || !is_array($nulls)) return -1; // @@ WE CANNOT USE insert AS WE NEED TO NOT QUOTE SOME THINGS // @@ WHAT ABOUT BOOLEANS?? else { - $temp = array(); - foreach($values as $k => $v) { - if (!isset($nulls[$k])) $temp[$k] = $v; - } - return $this->insert($table, $temp); + $this->fieldClean($table); + + // Build clause + if (sizeof($vars) > 0) { + $fields = ''; + $values = ''; + foreach($vars as $key => $value) { + $this->clean($key); + $this->clean($value); + + if ($fields) $fields .= ", \"{$key}\""; + else $fields = "INSERT INTO \"{$table}\" (\"{$key}\""; + + // Handle NULL values + if (isset($nulls[$key])) $tmp = 'NULL'; + else $tmp = "'{$value}'"; + + if ($values) $values .= ", {$tmp}"; + else $values = ") VALUES ({$tmp}"; + } + $sql = $fields . $values . ')'; + } + return $this->execute($sql); } } diff --git a/classes/database/Postgres.php b/classes/database/Postgres.php index 1dfa6960..2b62add2 100755 --- a/classes/database/Postgres.php +++ b/classes/database/Postgres.php @@ -4,7 +4,7 @@ * A class that implements the DB interface for Postgres * Note: This class uses ADODB and returns RecordSets. * - * $Id: Postgres.php,v 1.88 2003/04/30 07:42:58 chriskl Exp $ + * $Id: Postgres.php,v 1.89 2003/05/01 03:27:54 chriskl Exp $ */ // @@@ THOUGHT: What about inherits? ie. use of ONLY??? @@ -496,11 +496,11 @@ class Postgres extends BaseDB { * Drops a column from a table * @param $table The table from which to drop a column * @param $column The column to be dropped - * @param $behavior CASCADE or RESTRICT or empty + * @param $cascade True to cascade drop, false to restrict * @return 0 success * @return -99 not implemented */ - function dropColumn($table, $column, $behavior) { + function dropColumn($table, $column, $cascade) { return -99; } @@ -1091,16 +1091,6 @@ class Postgres extends BaseDB { return $this->execute($sql); } - /** - * Drops a column from a table - * @param $table The table from which to drop - * @param $column The column name to drop - * @return 0 success - */ - function dropColumnFromTable($table, $column) { - return -99; // Not implemented - } - /** * Sets default value of a column * @param $table The table from which to drop diff --git a/classes/database/Postgres72.php b/classes/database/Postgres72.php index de085795..dba7b2f4 100644 --- a/classes/database/Postgres72.php +++ b/classes/database/Postgres72.php @@ -4,7 +4,7 @@ * A class that implements the DB interface for Postgres * Note: This class uses ADODB and returns RecordSets. * - * $Id: Postgres72.php,v 1.36 2003/04/30 06:49:12 chriskl Exp $ + * $Id: Postgres72.php,v 1.37 2003/05/01 03:27:54 chriskl Exp $ */ @@ -124,13 +124,15 @@ class Postgres72 extends Postgres71 { * Removes a constraint from a relation * @param $constraint The constraint to drop * @param $relation The relation from which to drop + * @param $cascade True to cascade drop, false to restrict * @return 0 success */ - function dropConstraint($constraint, $relation) { + function dropConstraint($constraint, $relation, $cascade) { $this->fieldClean($constraint); $this->fieldClean($relation); $sql = "ALTER TABLE \"{$relation}\" DROP CONSTRAINT \"{$constraint}\" RESTRICT"; + if ($cascade) $sql .= " CASCADE"; return $this->execute($sql); } diff --git a/classes/database/Postgres73.php b/classes/database/Postgres73.php index 9fd7bdfd..d5eebe7b 100644 --- a/classes/database/Postgres73.php +++ b/classes/database/Postgres73.php @@ -4,7 +4,7 @@ * A class that implements the DB interface for Postgres * Note: This class uses ADODB and returns RecordSets. * - * $Id: Postgres73.php,v 1.38 2003/04/30 07:28:11 chriskl Exp $ + * $Id: Postgres73.php,v 1.39 2003/05/01 03:27:54 chriskl Exp $ */ // @@@ THOUGHT: What about inherits? ie. use of ONLY??? @@ -331,16 +331,15 @@ class Postgres73 extends Postgres72 { * Drops a column from a table * @param $table The table from which to drop a column * @param $column The column to be dropped - * @param $behavior CASCADE or RESTRICT or empty + * @param $cascade True to cascade drop, false to restrict * @return 0 success */ - function dropColumn($table, $column, $behavior) { - $this->clean($table); - $this->clean($column); - $this->clean($behavior); + function dropColumn($table, $column, $cascade) { + $this->fieldClean($table); + $this->fieldClean($column); - $sql = "ALTER TABLE \"{$table}\" DROP COLUMN \"{$column}\" " . - (($behavior == 'CASCADE') ? 'CASCADE' : 'RESTRICT'); + $sql = "ALTER TABLE \"{$table}\" DROP COLUMN \"{$column}\""; + if ($cascade) $sql .= " CASCADE"; return $this->execute($sql); } @@ -353,42 +352,10 @@ class Postgres73 extends Postgres72 { * @return 0 success */ function setColumnNull($table, $column, $state) { - $this->clean($table); - $this->clean($column); - - $sql = "ALTER TABLE \"{$table}\" ALTER COLUMN \"{$column}\" " . (($state) ? 'DROP' : 'SET') . " NOT NULL"; - - return $this->execute($sql); - } - - // Constraint functions - - /** - * Drops a unique constraint from a table - * @param $table The table from which to drop the unique key - * @param $name The name of the unique key - * @return 0 success - */ - function dropUniqueKey($table, $name) { $this->fieldClean($table); - $this->fieldClean($name); - - $sql = "ALTER TABLE \"{$table}\" DROP CONSTRAINT \"{$name}\""; + $this->fieldClean($column); - return $this->execute($sql); - } - - /** - * Drops a primary key constraint from a table - * @param $table The table from which to drop the primary key - * @param $name The name of the primary key - * @return 0 success - */ - function dropPrimaryKey($table, $name) { - $this->fieldClean($table); - $this->fieldClean($name); - - $sql = "ALTER TABLE \"{$table}\" DROP CONSTRAINT \"{$name}\""; + $sql = "ALTER TABLE \"{$table}\" ALTER COLUMN \"{$column}\" " . (($state) ? 'DROP' : 'SET') . " NOT NULL"; return $this->execute($sql); } @@ -728,13 +695,15 @@ class Postgres73 extends Postgres72 { * Removes a constraint from a relation * @param $constraint The constraint to drop * @param $relation The relation from which to drop + * @param $cascade True to cascade drop, false to restrict * @return 0 success */ - function dropConstraint($constraint, $relation) { + function dropConstraint($constraint, $relation, $cascade) { $this->fieldClean($constraint); $this->fieldClean($relation); $sql = "ALTER TABLE \"{$relation}\" DROP CONSTRAINT \"{$constraint}\""; + if ($cascade) $sql .= " CASCADE"; return $this->execute($sql); } diff --git a/constraints.php b/constraints.php index c8b51d27..1d9a1061 100644 --- a/constraints.php +++ b/constraints.php @@ -3,7 +3,7 @@ /** * List constraints on a table * - * $Id: constraints.php,v 1.14 2003/04/23 08:56:26 chriskl Exp $ + * $Id: constraints.php,v 1.15 2003/05/01 03:27:54 chriskl Exp $ */ // Include application functions @@ -344,11 +344,15 @@ echo "\n"; echo "\n"; echo $misc->form; + // Show cascade drop option if supportd + if ($localData->hasDropBehavior()) { + echo "

{$lang['strcascade']}

\n"; + } echo " \n"; echo "\n"; } else { - $status = $localData->dropConstraint($_POST['constraint'], $_POST['table']); + $status = $localData->dropConstraint($_POST['constraint'], $_POST['table'], isset($_POST['cascade'])); if ($status == 0) doDefault($lang['strconstraintdropped']); else diff --git a/tblproperties.php b/tblproperties.php index 96893ab8..e029eb01 100644 --- a/tblproperties.php +++ b/tblproperties.php @@ -3,7 +3,7 @@ /** * List tables in a database * - * $Id: tblproperties.php,v 1.13 2003/04/20 09:45:39 chriskl Exp $ + * $Id: tblproperties.php,v 1.14 2003/05/01 03:27:54 chriskl Exp $ */ // Include application functions @@ -214,11 +214,15 @@ echo "\n"; echo "\n"; echo $misc->form; - echo " \n"; + // Show cascade drop option if supportd + if ($localData->hasDropBehavior()) { + echo "

{$lang['strcascade']}

\n"; + } + echo " \n"; echo "\n"; } else { - $status = $localData->dropColumn($_POST['table'], $_POST['column'], 'RESTRICT'); + $status = $localData->dropColumn($_POST['table'], $_POST['column'], isset($_POST['cascade'])); if ($status == 0) doDefault($lang['strcolumndropped']); else @@ -289,7 +293,7 @@ doProperties(); break; case 'drop': - if ($_POST['choice'] == "{$lang['stryes']}") doDrop(false); + if (isset($_POST['yes'])) doDrop(false); else doDefault(); break; case 'confirm_drop': -- 2.39.5