add create table (like...) page for pg 7.4+
authorioguix <ioguix>
Fri, 31 Aug 2007 19:46:23 +0000 (19:46 +0000)
committerioguix <ioguix>
Fri, 31 Aug 2007 19:46:23 +0000 (19:46 +0000)
HISTORY
classes/database/Postgres.php
classes/database/Postgres74.php
classes/database/Postgres82.php
lang/english.php
lang/french.php
lang/recoded/english.php
lang/recoded/french.php
tables.php

diff --git a/HISTORY b/HISTORY
index b3c8b61fbfe57d4b5a3e3669fb678680c0bae1e5..08f166209a47b0993587bf6087164414f9f809b7 100644 (file)
--- 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
index 984a7d183dc1e3f603274a10a55630be6cc90ff6..ba53efcd03b7f7f5a0659a663c38c32de3821591 100755 (executable)
@@ -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;}
 }
 
 ?>
index 872fa17aeeaf8169c09b8b9bdc7ccb09ed134366..9f45952bef0360ab5f280d595c9482904b3688e4 100644 (file)
@@ -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; }
 }
 
 ?>
index 0ef5664be6a3c48e4f239f4a45b3f0b5b3911383..624f71b651d6eb503a4ca5a6a9de3f38dfb7b3ca 100644 (file)
@@ -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;}
 }
 
 ?>
index 2f92f82ea29d952f537c79d81e04b406fffb24ec..5fe8ef872c0ac1f4cf00586fba1a1f8e23ab38c6 100755 (executable)
@@ -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
        $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.';
index b195f80f9e37945ff802d563ae56f26e426077e2..c669e4ccd9c4b11735a824998374b4892cf4e8f1 100644 (file)
@@ -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
     $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.';
index 970313cac3739a297431bcbe952033708ddc0907..a51283f7bd1ecb45d3458699baab97d71e1ce023 100644 (file)
@@ -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
        $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.';
index af0c16f68962faeda25b1c36c46a960d979ecc7b..17e478e5668d30c1ea0a3aecda483f85d2182b82 100644 (file)
@@ -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
     $lang['strnotables'] = 'Aucune table trouv&#233;e.';
     $lang['strnotable'] = 'Aucune table trouv&#233;e.';
     $lang['strcreatetable'] = 'Cr&#233;er une table';
+       $lang['strcreatetablelike']  =  'Cr&#233;er une table d\'apr&#232;s une table existante';
+       $lang['strcreatetablelikeparent']  =  'Table mod&#232;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&#233;ciser une table mod&#232;le.';
     $lang['strtableneedsfield'] = 'Vous devez sp&#233;cifier au moins un champ.';
     $lang['strtableneedscols'] = 'Vous devez indiquer un nombre valide de colonnes.';
     $lang['strtablecreated'] = 'Table cr&#233;&#233;e.';
index 31d8db685f75fa45f1ae3013824172fe40ad3d9d..bd9479e301e8d3cad30273ab12676a7c63ea615d 100644 (file)
@@ -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
                }
        }
 
+       /**
+        * 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 "<form action=\"tables.php\" method=\"post\">\n";
+                       echo "<table>\n\t<tr>\n\t\t<th class=\"data left required\">{$lang['strname']}</th>\n";
+                       echo "\t\t<td class=\"data\"><input name=\"name\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"", htmlspecialchars($_REQUEST['name']), "\" /></td>\n\t</tr>\n";
+                       echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strcreatetablelikeparent']}</th>\n";
+                       echo "\t\t<td class=\"data\">";
+                       echo GUI::printCombo($tables, 'like', true, $_REQUEST['like'], false);
+                       echo "</td>\n\t</tr>\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<tr>\n\t\t<th class=\"data left\">{$lang['strtablespace']}</th>\n";
+                                       echo "\t\t<td class=\"data\">";
+                                       echo GUI::printCombo($tblsp, 'tablespace', true, $_REQUEST['tablespace'], false);
+                                       echo "</td>\n\t</tr>\n";
+                               }
+                       }
+                       echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['stroptions']}</th>\n\t\t<td class=\"data\">";
+                       echo "<label for=\"withdefaults\"><input type=\"checkbox\" id=\"withdefaults\" name=\"withdefaults\"", 
+                               isset($_REQUEST['withdefaults']) ? ' checked="checked"' : '', 
+                               "/>{$lang['strcreatelikewithdefaults']}</label>";
+                       if ($data->hasCreateTableLikeWithConstraints()) {
+                               echo "<label for=\"withconstraints\"><input type=\"checkbox\" id=\"withconstraints\" name=\"withconstraints\"",
+                                       isset($_REQUEST['withconstraints']) ? ' checked="checked"' : '',
+                                       "/>{$lang['strcreatelikewithconstraints']}</label>";
+                       }
+                       echo "</td>\n\t</tr>\n";
+                       echo "</table>";
+
+                       echo "<input type=\"hidden\" name=\"action\" value=\"confcreatelike\" />\n";
+                       echo $misc->form;
+                       echo "<p><input type=\"submit\" value=\"{$lang['strcreate']}\" />\n";
+                       echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
+                       echo "</form>\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
         */
 
                $misc->printTable($tables, $columns, $actions, $lang['strnotables']);
 
-               echo "<p><a class=\"navlink\" href=\"tables.php?action=create&amp;{$misc->href}\">{$lang['strcreatetable']}</a></p>\n";
+               echo "<p><a class=\"navlink\" href=\"tables.php?action=create&amp;{$misc->href}\">{$lang['strcreatetable']}</a> |\n";
+               if ($data->hasCreateTableLike())
+                       echo "<a class=\"navlink\" href=\"tables.php?action=createlike&amp;{$misc->href}\">{$lang['strcreatetablelike']}</a>\n";
+               echo "</p>\n";
        }
        
        /**
                        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();