From 93a3e0bd71664ad4bcd6513be29751886419009c Mon Sep 17 00:00:00 2001 From: ioguix Date: Fri, 31 Aug 2007 19:46:23 +0000 Subject: [PATCH] add create table (like...) page for pg 7.4+ --- HISTORY | 1 + classes/database/Postgres.php | 6 +- classes/database/Postgres74.php | 39 +++++++++++- classes/database/Postgres82.php | 3 +- lang/english.php | 7 ++- lang/french.php | 7 ++- lang/recoded/english.php | 7 ++- lang/recoded/french.php | 7 ++- tables.php | 105 +++++++++++++++++++++++++++++++- 9 files changed, 172 insertions(+), 10 deletions(-) diff --git a/HISTORY b/HISTORY index b3c8b61f..08f16620 100644 --- a/HISTORY +++ b/HISTORY @@ -11,6 +11,7 @@ Features * Allow browsers to save different usernames and passwords for different servers. * Pagination selection available for reports * You can configure reports db, schema and table names +* Add support for creating a table using an exsting one (ioguix) Bugs * Fix inability to assign a field type/domain of a different schema diff --git a/classes/database/Postgres.php b/classes/database/Postgres.php index 984a7d18..ba53efcd 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.299 2007/07/20 04:38:34 xzilla Exp $ + * $Id: Postgres.php,v 1.300 2007/08/31 19:46:23 ioguix Exp $ */ // @@@ THOUGHT: What about inherits? ie. use of ONLY??? @@ -843,7 +843,7 @@ class Postgres extends ADODB_base { } return $this->endTransaction(); - } + } /** * Alters a table @@ -4661,6 +4661,8 @@ class Postgres extends ADODB_base { function hasAlterAggregate() { return false; } function hasSharedComments() {return false;} function hasAnalyze() {return false;} + function hasCreateTableLike() {return false;} + function hasCreateTableLikeWithConstraints() {return false;} } ?> diff --git a/classes/database/Postgres74.php b/classes/database/Postgres74.php index 872fa17a..9f45952b 100644 --- a/classes/database/Postgres74.php +++ b/classes/database/Postgres74.php @@ -4,7 +4,7 @@ * A class that implements the DB interface for Postgres * Note: This class uses ADODB and returns RecordSets. * - * $Id: Postgres74.php,v 1.58 2007/08/31 18:30:11 ioguix Exp $ + * $Id: Postgres74.php,v 1.59 2007/08/31 19:46:24 ioguix Exp $ */ include_once('./classes/database/Postgres73.php'); @@ -147,6 +147,42 @@ class Postgres74 extends Postgres73 { } } + /** + * Creates a new table in the database copying attribs and other properties from another table + * @param $name The name of the table + * @param $like The name of the table from which attribs are copying from + * @param $defaults if true, copy the defaults values as well + * @param $constraints if true, copy the constraints as well (CHECK on table & attr) + * @param $tablespace The tablespace name ('' means none/default) + */ + function createTableLike($name, $like, $defaults = false, $constraints = false, $tablespace = '') { + $this->fieldClean($name); + $this->fieldClean($like); + + $status = $this->beginTransaction(); + if ($status != 0) return -1; + + $sql = "CREATE TABLE \"{$name}\" (LIKE {$like}"; + + if ($defaults) $sql .= " INCLUDING DEFAULTS"; + if ($this->hasCreateTableLikeWithConstraints() && $constraints) $sql .= " INCLUDING CONSTRAINTS"; + + $sql .= ")"; + + if ($this->hasTablespaces() && $tablespace != '') { + $this->fieldClean($tablespace); + $sql .= " TABLESPACE \"{$tablespace}\""; + } + + $status = $this->execute($sql); + if ($status) { + $this->rollbackTransaction(); + return -1; + } + + return $this->endTransaction(); + } + // Group functions /** @@ -660,6 +696,7 @@ class Postgres74 extends Postgres73 { function hasReadOnlyQueries() { return true; } function hasAlterSequence() { return true; } function hasAlterAggregate() { return true; } + function hasCreateTableLike() { return true; } } ?> diff --git a/classes/database/Postgres82.php b/classes/database/Postgres82.php index 0ef5664b..624f71b6 100644 --- a/classes/database/Postgres82.php +++ b/classes/database/Postgres82.php @@ -3,7 +3,7 @@ /** * PostgreSQL 8.2 support * - * $Id: Postgres82.php,v 1.5 2007/03/28 18:52:34 soranzo Exp $ + * $Id: Postgres82.php,v 1.6 2007/08/31 19:46:24 ioguix Exp $ */ include_once('./classes/database/Postgres81.php'); @@ -199,6 +199,7 @@ class Postgres82 extends Postgres81 { // Capabilities function hasSharedComments() {return true;} + function hasCreateTableLikeWithConstraints() {return true;} } ?> diff --git a/lang/english.php b/lang/english.php index 2f92f82e..5fe8ef87 100755 --- a/lang/english.php +++ b/lang/english.php @@ -4,7 +4,7 @@ * English language file for phpPgAdmin. Use this as a basis * for new translations. * - * $Id: english.php,v 1.213 2007/07/19 03:11:52 xzilla Exp $ + * $Id: english.php,v 1.214 2007/08/31 19:46:24 ioguix Exp $ */ // Language and character set @@ -179,8 +179,13 @@ $lang['strnotables'] = 'No tables found.'; $lang['strnotable'] = 'No table found.'; $lang['strcreatetable'] = 'Create table'; + $lang['strcreatetablelike'] = 'Create table like'; + $lang['strcreatetablelikeparent'] = 'Source table'; + $lang['strcreatelikewithdefaults'] = 'INCLUDE DEFAULTS'; + $lang['strcreatelikewithconstraints'] = 'INCLUDE CONSTRAINTS'; $lang['strtablename'] = 'Table name'; $lang['strtableneedsname'] = 'You must give a name for your table.'; + $lang['strtablelikeneedslike'] = 'You must give a table to copy properties from.'; $lang['strtableneedsfield'] = 'You must specify at least one field.'; $lang['strtableneedscols'] = 'You must specify a valid number of columns.'; $lang['strtablecreated'] = 'Table created.'; diff --git a/lang/french.php b/lang/french.php index b195f80f..c669e4cc 100644 --- a/lang/french.php +++ b/lang/french.php @@ -4,7 +4,7 @@ * French Language file for phpPgAdmin. * @maintainer Pascal PEYRE [pascal.peyre@cir.fr] * - * $Id: french.php,v 1.27 2007/07/19 03:11:52 xzilla Exp $ + * $Id: french.php,v 1.28 2007/08/31 19:46:24 ioguix Exp $ */ // Language and character set @@ -179,8 +179,13 @@ $lang['strnotables'] = 'Aucune table trouvée.'; $lang['strnotable'] = 'Aucune table trouvée.'; $lang['strcreatetable'] = 'Créer une table'; + $lang['strcreatetablelike'] = 'Créer une table d\'après une table existante'; + $lang['strcreatetablelikeparent'] = 'Table modèle'; + $lang['strcreatelikewithdefaults'] = 'INCLUDE DEFAULTS'; + $lang['strcreatelikewithconstraints'] = 'INCLUDE CONSTRAINTS'; $lang['strtablename'] = 'Nom de la table'; $lang['strtableneedsname'] = 'Vous devez donner un nom pour votre table.'; + $lang['strtablelikeneedslike'] = 'Vous devez préciser une table modèle.'; $lang['strtableneedsfield'] = 'Vous devez spécifier au moins un champ.'; $lang['strtableneedscols'] = 'Vous devez indiquer un nombre valide de colonnes.'; $lang['strtablecreated'] = 'Table créée.'; diff --git a/lang/recoded/english.php b/lang/recoded/english.php index 970313ca..a51283f7 100644 --- a/lang/recoded/english.php +++ b/lang/recoded/english.php @@ -4,7 +4,7 @@ * English language file for phpPgAdmin. Use this as a basis * for new translations. * - * $Id: english.php,v 1.165 2007/07/19 03:11:52 xzilla Exp $ + * $Id: english.php,v 1.166 2007/08/31 19:46:24 ioguix Exp $ */ // Language and character set @@ -179,8 +179,13 @@ $lang['strnotables'] = 'No tables found.'; $lang['strnotable'] = 'No table found.'; $lang['strcreatetable'] = 'Create table'; + $lang['strcreatetablelike'] = 'Create table like'; + $lang['strcreatetablelikeparent'] = 'Source table'; + $lang['strcreatelikewithdefaults'] = 'INCLUDE DEFAULTS'; + $lang['strcreatelikewithconstraints'] = 'INCLUDE CONSTRAINTS'; $lang['strtablename'] = 'Table name'; $lang['strtableneedsname'] = 'You must give a name for your table.'; + $lang['strtablelikeneedslike'] = 'You must give a table to copy properties from.'; $lang['strtableneedsfield'] = 'You must specify at least one field.'; $lang['strtableneedscols'] = 'You must specify a valid number of columns.'; $lang['strtablecreated'] = 'Table created.'; diff --git a/lang/recoded/french.php b/lang/recoded/french.php index af0c16f6..17e478e5 100644 --- a/lang/recoded/french.php +++ b/lang/recoded/french.php @@ -4,7 +4,7 @@ * French Language file for phpPgAdmin. * @maintainer Pascal PEYRE [pascal.peyre@cir.fr] * - * $Id: french.php,v 1.26 2007/07/19 03:11:52 xzilla Exp $ + * $Id: french.php,v 1.27 2007/08/31 19:46:24 ioguix Exp $ */ // Language and character set @@ -179,8 +179,13 @@ $lang['strnotables'] = 'Aucune table trouvée.'; $lang['strnotable'] = 'Aucune table trouvée.'; $lang['strcreatetable'] = 'Créer une table'; + $lang['strcreatetablelike'] = 'Créer une table d\'après une table existante'; + $lang['strcreatetablelikeparent'] = 'Table modèle'; + $lang['strcreatelikewithdefaults'] = 'INCLUDE DEFAULTS'; + $lang['strcreatelikewithconstraints'] = 'INCLUDE CONSTRAINTS'; $lang['strtablename'] = 'Nom de la table'; $lang['strtableneedsname'] = 'Vous devez donner un nom pour votre table.'; + $lang['strtablelikeneedslike'] = 'Vous devez préciser une table modèle.'; $lang['strtableneedsfield'] = 'Vous devez spécifier au moins un champ.'; $lang['strtableneedscols'] = 'Vous devez indiquer un nombre valide de colonnes.'; $lang['strtablecreated'] = 'Table créée.'; diff --git a/tables.php b/tables.php index 31d8db68..bd9479e3 100644 --- a/tables.php +++ b/tables.php @@ -3,7 +3,7 @@ /** * List tables in a database * - * $Id: tables.php,v 1.101 2007/08/31 18:30:11 ioguix Exp $ + * $Id: tables.php,v 1.102 2007/08/31 19:46:23 ioguix Exp $ */ // Include application functions @@ -236,6 +236,97 @@ } } + /** + * Dsiplay a screen where user can create a table from an existing one. + */ + function doCreateLike($confirm, $msg = '') { + global $data, $misc, $lang; + + if (!$confirm) { + + include_once('./classes/Gui.php'); + + if (!isset($_REQUEST['name'])) $_REQUEST['name'] = ''; + if (!isset($_REQUEST['like'])) $_REQUEST['like'] = ''; + if (!isset($_REQUEST['tablespace'])) $_REQUEST['tablespace'] = ''; + + $misc->printTrail('schema'); + $misc->printTitle($lang['strcreatetable'], 'pg.table.create'); + $misc->printMsg($msg); + + $tbltmp = $data->getTables(true); + $tbltmp = $tbltmp->getArray(); + + $tables = array(); + if ( $data->hasSchemas() ) + foreach ($tbltmp as $a) $tables["{$a['nspname']}.{$a['relname']}"] = "{$a['nspname']}.{$a['relname']}"; + else + foreach ($tbltmp as $a) $tables[$a['relname']] = $a['relname']; + + unset($tbltmp); + + echo "
\n"; + echo "\n\t\n\t\t\n"; + echo "\t\t\n\t\n"; + echo "\t\n\t\t\n"; + echo "\t\t\n\t\n"; + if ($data->hasTablespaces()) { + $tblsp_ = $data->getTablespaces(); + if ($tblsp_->recordCount() > 0) { + $tblsp_ = $tblsp_->getArray(); + $tblsp = array(); + foreach($tblsp_ as $a) $tblsp[$a['spcname']] = $a['spcname']; + + echo "\t\n\t\t\n"; + echo "\t\t\n\t\n"; + } + } + echo "\t\n\t\t\n\t\t\n\t\n"; + echo "
{$lang['strname']}_maxNameLen}\" value=\"", htmlspecialchars($_REQUEST['name']), "\" />
{$lang['strcreatetablelikeparent']}"; + echo GUI::printCombo($tables, 'like', true, $_REQUEST['like'], false); + echo "
{$lang['strtablespace']}"; + echo GUI::printCombo($tblsp, 'tablespace', true, $_REQUEST['tablespace'], false); + echo "
{$lang['stroptions']}"; + echo ""; + if ($data->hasCreateTableLikeWithConstraints()) { + echo ""; + } + echo "
"; + + echo "\n"; + echo $misc->form; + echo "

\n"; + echo "

\n"; + echo "
\n"; + } + else { + global $_reload_browser; + + if (trim($_REQUEST['name']) == '') { + doCreateLike(false, $lang['strtableneedsname']); + return; + } + if (trim($_REQUEST['like']) == '') { + doCreateLike(false, $lang['strtablelikeneedslike']); + return; + } + + $status = $data->createTableLike($_REQUEST['name'], $_REQUEST['like'], isset($_REQUEST['withdefaults']), isset($_REQUEST['withconstraints']), $_REQUEST['tablespace']); + if ($status == 0) { + $_reload_browser = true; + doDefault($lang['strtablecreated']); + } + else { + doCreateLike(false, $lang['strtablecreatedbad']); + return; + } + } + } + /** * Ask for select parameters and perform select */ @@ -858,7 +949,10 @@ $misc->printTable($tables, $columns, $actions, $lang['strnotables']); - echo "

href}\">{$lang['strcreatetable']}

\n"; + echo "

href}\">{$lang['strcreatetable']} |\n"; + if ($data->hasCreateTableLike()) + echo "href}\">{$lang['strcreatetablelike']}\n"; + echo "

\n"; } /** @@ -938,6 +1032,13 @@ if (isset($_POST['cancel'])) doDefault(); else doCreate(); break; + case 'createlike': + doCreateLike(false); + break; + case 'confcreatelike': + if (isset($_POST['cancel'])) doDefault(); + else doCreateLike(true); + break; case 'selectrows': if (!isset($_POST['cancel'])) doSelectRows(false); else doDefault(); -- 2.39.5