allow creating fkeys to tables in other schemas. functions for getting child and...
authorchriskl <chriskl>
Wed, 8 Oct 2003 02:14:24 +0000 (02:14 +0000)
committerchriskl <chriskl>
Wed, 8 Oct 2003 02:14:24 +0000 (02:14 +0000)
classes/database/Postgres.php
classes/database/Postgres73.php
constraints.php
indexes.php
lang/english.php
lang/recoded/english.php

index 59aa9e065af294187bb693be4d4ab07a5df634c0..c16c94039592643fee542011bf58a2f19d6448e4 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.150 2003/10/06 15:26:23 chriskl Exp $
+ * $Id: Postgres.php,v 1.151 2003/10/08 02:14:24 chriskl Exp $
  */
 
 // @@@ THOUGHT: What about inherits? ie. use of ONLY???
@@ -253,6 +253,18 @@ class Postgres extends BaseDB {
                return -99;
        }
 
+       // Schema functions
+       
+       /**
+        * Sets the current working schema.  This is a do nothing method for
+        * < 7.3 and is just here for polymorphism's sake.
+        * @param $schema The the name of the schema to work in
+        * @return 0 success
+        */
+       function setSchema($schema) {
+               return 0;
+       }
+       
        // Table functions
 
        /**
@@ -399,6 +411,8 @@ class Postgres extends BaseDB {
 
                $sql .= ")";
 
+               // @@@@ DUMP CLUSTERING INFORMATION
+
                // Inherits
                /*
                 * XXX: This is currently commented out as handling inheritance isn't this simple.
@@ -951,13 +965,14 @@ class Postgres extends BaseDB {
 
        /**
         * Return all tables in current database
+        * @param $all True to fetch all tables, false for just in current schema
         * @return All tables, sorted alphabetically 
         */
-       function &getTables() {
+       function &getTables($all = false) {
                global $conf;
-               if (!$conf['show_system']) $where = "WHERE tablename NOT LIKE 'pg_%' ";
+               if (!$conf['show_system'] || $all) $where = "WHERE tablename NOT LIKE 'pg_%' ";
                else $where = '';
-               $sql = "SELECT tablename, tableowner FROM pg_tables {$where}ORDER BY tablename";
+               $sql = "SELECT NULL AS schemaname, tablename, tableowner FROM pg_tables {$where}ORDER BY tablename";
                return $this->selectSet($sql);
        }
 
@@ -1592,7 +1607,8 @@ class Postgres extends BaseDB {
 
        /**
         * Adds a foreign key constraint to a table
-        * @param $table The table to which to add the foreign key
+        * @param $targschema The schema that houses the target table to which to add the foreign key
+        * @param $targtable The table to which to add the foreign key
         * @param $target The table that contains the target columns
         * @param $sfields (array) An array of source fields over which to add the foreign key
         * @param $tfields (array) An array of target fields over which to add the foreign key
@@ -1602,11 +1618,12 @@ class Postgres extends BaseDB {
         * @return 0 success
         * @return -1 no fields given
         */
-       function addForeignKey($table, $target, $sfields, $tfields, $upd_action, $del_action, $name = '') {
+       function addForeignKey($table, $targschema, $targtable, $sfields, $tfields, $upd_action, $del_action, $name = '') {
                if (!is_array($sfields) || sizeof($sfields) == 0 ||
                        !is_array($tfields) || sizeof($tfields) == 0) return -1;
                $this->fieldClean($table);
-               $this->fieldClean($target);
+               $this->fieldClean($targschema);
+               $this->fieldClean($targtable);
                $this->fieldArrayClean($sfields);
                $this->fieldArrayClean($tfields);
                $this->fieldClean($name);
@@ -1614,7 +1631,12 @@ class Postgres extends BaseDB {
                $sql = "ALTER TABLE \"{$table}\" ADD ";
                if ($name != '') $sql .= "CONSTRAINT \"{$name}\" ";
                $sql .= "FOREIGN KEY (\"" . join('","', $sfields) . "\") ";
-               $sql .= "REFERENCES \"{$target}\"(\"" . join('","', $tfields) . "\") ";
+               $sql .= "REFERENCES ";
+               // Target table needs to be fully qualified
+               if ($this->hasSchemas()) {
+                       $sql .= "\"{$targschema}\".";
+               }               
+               $sql .= "\"{$targtable}\"(\"" . join('","', $tfields) . "\") ";
                if ($upd_action != 'NO ACTION') $sql .= " ON UPDATE {$upd_action}";
                if ($del_action != 'NO ACTION') $sql .= " ON DELETE {$del_action}";
 
index 4572992b5d67f00b5dcd914c4872f5752b0880ac..fd1cb4c11cbb2cd24dd8cb3a855bfad697bb8f8e 100644 (file)
@@ -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.65 2003/10/06 15:26:23 chriskl Exp $
+ * $Id: Postgres73.php,v 1.66 2003/10/08 02:14:24 chriskl Exp $
  */
 
 // @@@ THOUGHT: What about inherits? ie. use of ONLY???
@@ -309,12 +309,20 @@ class Postgres73 extends Postgres72 {
        }
        
        /**
-        * Return all tables in current database
+        * Return all tables in current database (and schema)
+        * @param $all True to fetch all tables, false for just in current schema
         * @return All tables, sorted alphabetically 
         */
-       function &getTables() {
-               $sql = "SELECT tablename, tableowner FROM pg_catalog.pg_tables
-                       WHERE schemaname='{$this->_schema}' ORDER BY tablename";
+       function &getTables($all = false) {
+               if ($all) {
+                       // Exclude pg_catalog and information_schema tables
+                       $sql = "SELECT schemaname, tablename, tableowner FROM pg_catalog.pg_tables 
+                               WHERE schemaname NOT IN ('pg_catalog', 'information_schema', 'pg_toast')
+                               ORDER BY schemaname, tablename";
+               } else {
+                       $sql = "SELECT tablename, tableowner FROM pg_catalog.pg_tables
+                               WHERE schemaname='{$this->_schema}' ORDER BY tablename";
+               }               
 
                return $this->selectSet($sql);
        }
@@ -404,6 +412,57 @@ class Postgres73 extends Postgres72 {
                return $this->execute($sql);
        }
 
+       // Inheritance functions\r
+       \r
+       /**\r
+        * Finds the names and schemas of parent tables (in order)\r
+        * @param $table The table to find the parents for\r
+        * @return A recordset\r
+        */\r
+       function &getTableParents($table) {\r
+               $this->clean($table);\r
+               \r
+               $sql = "\r
+                       SELECT \r
+                               pn.nspname AS schemaname, relname\r
+                       FROM\r
+                               pg_catalog.pg_class pc, pg_catalog.pg_inherits pi, pg_catalog.pg_namespace pn\r
+                       WHERE\r
+                               pc.oid=pi.inhparent
+                               AND pc.relnamespace=pn.oid\r
+                               AND pi.inhrelid = (SELECT oid from pg_catalog.pg_class WHERE relname='{$table}'
+                                       AND relnamespace = (SELECT oid FROM pg_catalog.pg_namespace WHERE nspname = '{$this->_schema}'))\r
+                       ORDER BY\r
+                               pi.inhseqno\r
+               ";\r
+               \r
+               return $this->selectSet($sql);                                  \r
+       }       \r
+\r
+\r
+       /**\r
+        * Finds the names and schemas of child tables\r
+        * @param $table The table to find the children for\r
+        * @return A recordset\r
+        */\r
+       function &getTableChildren($table) {\r
+               $this->clean($table);\r
+               \r
+               $sql = "\r
+                       SELECT \r
+                               pn.nspname AS schemaname, relname\r
+                       FROM\r
+                               pg_catalog.pg_class pc, pg_catalog.pg_inherits pi, pg_catalog.pg_namespace pn\r
+                       WHERE\r
+                               pc.oid=pi.inhrelid
+                               AND pc.relnamespace=pn.oid\r
+                               AND pi.inhparent = (SELECT oid from pg_catalog.pg_class WHERE relname='{$table}'
+                                       AND relnamespace = (SELECT oid FROM pg_catalog.pg_namespace WHERE nspname = '{$this->_schema}'))\r
+               ";\r
+               \r
+               return $this->selectSet($sql);                                  \r
+       }       
+
        // View functions
        
        /**
index a94131ffbc8b61d4996671f7e82dbcb76ef83e29..69f1026d2684909426bdf49da08b2d59269f09e7 100644 (file)
@@ -3,7 +3,7 @@
        /**
         * List constraints on a table
         *
-        * $Id: constraints.php,v 1.21 2003/10/03 07:38:54 chriskl Exp $
+        * $Id: constraints.php,v 1.22 2003/10/08 02:14:24 chriskl Exp $
         */
 
        // Include application functions
         * Show confirmation of cluster index and perform actual cluster
         */
        function doClusterIndex($confirm) {
-               global $localData, $database, $misc;
+               global $localData, $database, $misc, $action;
                global $PHP_SELF, $lang;
 
                if ($confirm) {
+                       // Default analyze to on
+                       if ($action == 'confirm_cluster_constraint') $_REQUEST['analyze'] = 'on';
+                       
                        echo "<h2>", $misc->printVal($_REQUEST['database']), ": {$lang['strtables']}: ",
                                $misc->printVal($_REQUEST['table']), ": " , $misc->printVal($_REQUEST['constraint']), ": {$lang['strcluster']}</h2>\n";
 
@@ -31,7 +34,7 @@
                        echo "<input type=\"hidden\" name=\"table\" value=\"", htmlspecialchars($_REQUEST['table']), "\" />\n";
                        echo "<input type=\"hidden\" name=\"constraint\" value=\"", htmlspecialchars($_REQUEST['constraint']), "\" />\n";
                        echo $misc->form;
-                       echo "<p><input type=\"checkbox\" name=\"analyze\" /> {$lang['stranalyze']}</p>\n";
+                       echo "<p><input type=\"checkbox\" name=\"analyze\"", (isset($_REQUEST['analyze']) ? ' checked="checked"' : ''), " /> {$lang['stranalyze']}</p>\n";
                        echo "<input type=\"submit\" name=\"cluster\" value=\"{$lang['strcluster']}\" />\n";
                        echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n";
                        echo "</form>\n";
                                // Initialise variables
                                if (!isset($_POST['upd_action'])) $_POST['upd_action'] = null;
                                if (!isset($_POST['del_action'])) $_POST['del_action'] = null;
+                               $_REQUEST['target'] = unserialize($_REQUEST['target']);
                                
                                echo "<h2>", $misc->printVal($_REQUEST['database']), ": {$lang['strtables']}: ",
                                        $misc->printVal($_REQUEST['table']), ": {$lang['straddfk']}</h2>\n";
                                $misc->printMsg($msg);
 
-                               $attrs = &$localData->getTableAttributes($_REQUEST['target']);
+                               // Unserialize target and fetch appropriate table.  This is a bit messy
+                               // because the table could be in another schema.
+                               if ($localData->hasSchemas()) {
+                                       $localData->setSchema($_REQUEST['target']['schemaname']);
+                               }
+                               $attrs = &$localData->getTableAttributes($_REQUEST['target']['tablename']);
+                               if ($localData->hasSchemas()) {
+                                       $localData->setSchema($_REQUEST['schema']);
+                               }
 
                                $selColumns = new XHTML_select('TableColumnList', true, 10);
                                $selColumns->set_style('width: 10em;');
                                echo $misc->form;
                                echo "<input type=\"hidden\" name=\"table\" value=\"", htmlspecialchars($_REQUEST['table']), "\" />\n";
                                echo "<input type=\"hidden\" name=\"name\" value=\"", htmlspecialchars($_REQUEST['name']), "\" />\n";
-                               echo "<input type=\"hidden\" name=\"target\" value=\"", htmlspecialchars($_REQUEST['target']), "\" />\n";
+                               echo "<input type=\"hidden\" name=\"target\" value=\"", htmlspecialchars(serialize($_REQUEST['target'])), "\" />\n";
                                echo "<input type=\"hidden\" name=\"SourceColumnList\" value=\"", htmlspecialchars($_REQUEST['SourceColumnList']), "\" />\n";
                                echo "<input type=\"hidden\" name=\"stage\" value=\"3\" />\n";
-                               echo "<input type=\"submit\" value=\"{$lang['stradd']}\" /> <input type=\"reset\" value=\"{$lang['strreset']}\" /></p>\n";
+                               echo "<input type=\"submit\" value=\"{$lang['stradd']}\" />\n";
+                               echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
                                echo "</form>\n";
-
-                               echo "<p><a class=\"navlink\" href=\"$PHP_SELF?{$misc->href}&table=", urlencode($_REQUEST['table']),
-                                       "\">{$lang['strshowallconstraints']}</a></p>\n";
                                break;
                        case 3:
+                               // Unserialize target
+                               $_POST['target'] = unserialize($_POST['target']);
+
                                // Check that they've given at least one column
                                if (isset($_POST['SourceColumnList'])) $temp = unserialize($_POST['SourceColumnList']);
                                if (!isset($_POST['IndexColumnList']) || !is_array($_POST['IndexColumnList'])
                                                || sizeof($_POST['IndexColumnList']) == 0 || !isset($temp) 
                                                || !is_array($temp) || sizeof($temp) == 0) addForeignKey(2, $lang['strfkneedscols']);
                                else {
-                                       $status = $localData->addForeignKey($_POST['table'], $_POST['target'], unserialize($_POST['SourceColumnList'])
-                                               $_POST['IndexColumnList'], $_POST['upd_action'], $_POST['del_action'], $_POST['name']);
+                                       $status = $localData->addForeignKey($_POST['table'], $_POST['target']['schemaname'], $_POST['target']['tablename']
+                                               unserialize($_POST['SourceColumnList']), $_POST['IndexColumnList'], $_POST['upd_action'], $_POST['del_action'], $_POST['name']);
                                        if ($status == 0)
                                                doDefault($lang['strfkadded']);
                                        else
                                echo "<tr>";
                                echo "<td class=\"data1\" colspan=\"3\"><select name=\"target\">";
                                while (!$tables->EOF) {
-                                       echo "<option value=\"", htmlspecialchars($tables->f['tablename']), "\">",
-                                               htmlspecialchars($tables->f['tablename']), "</option>\n";
+                                       $key = array('schemaname' => $tables->f['schemaname'], 'tablename' => $tables->f['tablename']);
+                                       $key = serialize($key);
+                                       echo "<option value=\"", htmlspecialchars($key), "\">";
+                                       if ($localData->hasSchemas() && $tables->f['schemaname'] != $_REQUEST['schema']) {
+                                                       echo htmlspecialchars($tables->f['schemaname']), '.';
+                                       }
+                                       echo htmlspecialchars($tables->f['tablename']), "</option>\n";
                                        $tables->moveNext();    
                                }
                                echo "</select>\n";
                                echo $misc->form;
                                echo "<input type=\"hidden\" name=\"table\" value=\"", htmlspecialchars($_REQUEST['table']), "\" />\n";
                                echo "<input type=\"hidden\" name=\"stage\" value=\"2\" />\n";
-                               echo "<input type=\"submit\" value=\"{$lang['stradd']}\" /> <input type=\"reset\" value=\"{$lang['strreset']}\" /></p>\n";
+                               echo "<input type=\"submit\" value=\"{$lang['stradd']}\" />\n";
+                               echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
                                echo "</form>\n";
-
-                               echo "<p><a class=\"navlink\" href=\"$PHP_SELF?{$misc->href}&table=", urlencode($_REQUEST['table']),
-                                       "\">{$lang['strshowallconstraints']}</a></p>\n";
                                break;
                }
 
                        echo $misc->form;
                        echo "<input type=\"hidden\" name=\"table\" value=\"", htmlspecialchars($_REQUEST['table']), "\" />\n";
                        echo "<input type=\"hidden\" name=\"type\" value=\"", htmlspecialchars($type), "\" />\n";
-                       echo "<input type=\"submit\" value=\"{$lang['stradd']}\" /> <input type=\"reset\" value=\"{$lang['strreset']}\" /></p>\n";
+                       echo "<input type=\"submit\" value=\"{$lang['stradd']}\" />\n";
+                       echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
                        echo "</form>\n";
-                       
-                       echo "<p><a class=\"navlink\" href=\"$PHP_SELF?{$misc->href}&table=", urlencode($_REQUEST['table']), 
-                               "\">{$lang['strshowallconstraints']}</a></p>\n";
                }
                else {
                        if ($_POST['type'] == 'primary') {
index b37bb54487391c4302455582878e3a21e57ece62..302ea206ec62b7ea578c9c5c03dcd422d8b933f1 100644 (file)
@@ -3,7 +3,7 @@
        /**
         * List indexes on a table
         *
-        * $Id: indexes.php,v 1.18 2003/10/03 07:38:54 chriskl Exp $
+        * $Id: indexes.php,v 1.19 2003/10/08 02:14:24 chriskl Exp $
         */
 
        // Include application functions
         * Show confirmation of cluster index and perform actual cluster
         */
        function doClusterIndex($confirm) {
-               global $localData, $database, $misc;
+               global $localData, $database, $misc, $action;
                global $PHP_SELF, $lang;
 
                if ($confirm) {
+                       // Default analyze to on
+                       if ($action == 'confirm_cluster_index') $_REQUEST['analyze'] = 'on';
+                       
                        echo "<h2>", $misc->printVal($_REQUEST['database']), ": {$lang['strtables']}: ",
                                $misc->printVal($_REQUEST['table']), ": " , $misc->printVal($_REQUEST['index']), ": {$lang['strcluster']}</h2>\n";
 
@@ -31,7 +34,7 @@
                        echo "<input type=\"hidden\" name=\"table\" value=\"", htmlspecialchars($_REQUEST['table']), "\" />\n";
                        echo "<input type=\"hidden\" name=\"index\" value=\"", htmlspecialchars($_REQUEST['index']), "\" />\n";
                        echo $misc->form;
-                       echo "<p><input type=\"checkbox\" name=\"analyze\" /> {$lang['stranalyze']}</p>\n";
+                       echo "<p><input type=\"checkbox\" name=\"analyze\"", (isset($_REQUEST['analyze']) ? ' checked="checked"' : ''), " /> {$lang['stranalyze']}</p>\n";
                        echo "<input type=\"submit\" name=\"cluster\" value=\"{$lang['strcluster']}\" />\n";
                        echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n";
                        echo "</form>\n";
index b443d746c0ca238d36e46e9732241aca57faecec..0e21d0325d6161793c043c5f718caf24754cc528 100755 (executable)
@@ -4,7 +4,7 @@
         * English language file for phpPgAdmin.  Use this as a basis
         * for new translations.
         *
-        * $Id: english.php,v 1.109 2003/10/06 18:10:18 soranzo Exp $
+        * $Id: english.php,v 1.110 2003/10/08 02:14:24 chriskl Exp $
         */
 
        // Language and character set
 
        // Info
        $lang['strreferringtables'] = 'Referring Tables';
+       $lang['strparenttables'] = 'Parent Tables';
+       $lang['strchildtables'] = 'Child Tables';
 
        // Miscellaneous
        $lang['strtopbar'] = '%s running on %s:%s -- You are logged in as user "%s", %s';
index 3237487ef0d4804a17a870745e8daff5fb4b21a7..c5b5fecb1acc7acf768c28adb7546d97c2edac44 100644 (file)
@@ -4,7 +4,7 @@
         * English language file for phpPgAdmin.  Use this as a basis
         * for new translations.
         *
-        * $Id: english.php,v 1.61 2003/10/06 18:10:18 soranzo Exp $
+        * $Id: english.php,v 1.62 2003/10/08 02:14:24 chriskl Exp $
         */
 
        // Language and character set
 
        // Info
        $lang['strreferringtables'] = 'Referring Tables';
+       $lang['strparenttables'] = 'Parent Tables';
+       $lang['strchildtables'] = 'Child Tables';
 
        // Miscellaneous
        $lang['strtopbar'] = '%s running on %s:%s -- You are logged in as user &quot;%s&quot;, %s';