Support for storing triggers.
authorchriskl <chriskl>
Thu, 16 Jun 2005 14:12:47 +0000 (14:12 +0000)
committerchriskl <chriskl>
Thu, 16 Jun 2005 14:12:47 +0000 (14:12 +0000)
classes/database/Postgres.php
classes/database/Postgres73.php
classes/database/Postgres74.php
classes/plugins/Slony.php
lang/english.php
lang/recoded/english.php
plugin_slony.php

index 303d968a660b47ce6b2144057858129003d34140..2fe398051dddb556a20e770bfbf2419df1e22eef 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.261.2.2 2005/06/09 15:01:02 chriskl Exp $
+ * $Id: Postgres.php,v 1.261.2.3 2005/06/16 14:12:48 chriskl Exp $
  */
 
 // @@@ THOUGHT: What about inherits? ie. use of ONLY???
@@ -2136,14 +2136,17 @@ class Postgres extends ADODB_base {
        /**
         * Grabs a list of indexes for a table
         * @param $table The name of a table whose indexes to retrieve
+        * @param $unique Only get unique/pk indexes
         * @return A recordset
         */
-       function &getIndexes($table = '') {
+       function &getIndexes($table = '', $unique = false) {
                $this->clean($table);
                $sql = "SELECT c2.relname AS indname, i.indisprimary, i.indisunique, pg_get_indexdef(i.indexrelid) AS inddef
                        FROM pg_class c, pg_class c2, pg_index i
                        WHERE c.relname = '{$table}' AND c.oid = i.indrelid AND i.indexrelid = c2.oid
-                       ORDER BY c2.relname";
+               ";
+               if ($unique) $sql .= " AND i.indisunique ";
+               $sql .= " ORDER BY c2.relname";
 
                return $this->selectSet($sql);
        }
index 227bbad31d93d8d340eab741cc11892d3a908ab0..d1bfaf88bf721ff2e15f68074fc413e215254098 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.143.2.1 2005/06/09 15:01:05 chriskl Exp $
+ * $Id: Postgres73.php,v 1.143.2.2 2005/06/16 14:12:49 chriskl Exp $
  */
 
 // @@@ THOUGHT: What about inherits? ie. use of ONLY???
@@ -622,9 +622,10 @@ class Postgres73 extends Postgres72 {
        /**
         * Grabs a list of indexes for a table
         * @param $table The name of a table whose indexes to retrieve
+        * @param $unique Only get unique/pk indexes
         * @return A recordset
         */
-       function &getIndexes($table = '') {
+       function &getIndexes($table = '', $unique = false) {
                $this->clean($table);
                
                $sql = "SELECT c2.relname AS indname, i.indisprimary, i.indisunique, i.indisclustered,
@@ -632,7 +633,9 @@ class Postgres73 extends Postgres72 {
                        FROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i
                        WHERE c.relname = '{$table}' AND pg_catalog.pg_table_is_visible(c.oid) 
                        AND c.oid = i.indrelid AND i.indexrelid = c2.oid
-                       ORDER BY c2.relname";
+               ";
+               if ($unique) $sql .= " AND i.indisunique ";
+               $sql .= " ORDER BY c2.relname";
 
                return $this->selectSet($sql);
        }
index 94c010bf12673ae67ce236d9d736c8e6f79ca9ca..2e2248a844963878eb9b03187b13754e5c2266a7 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.44 2005/04/30 18:02:01 soranzo Exp $
+ * $Id: Postgres74.php,v 1.44.2.1 2005/06/16 14:12:49 chriskl Exp $
  */
 
 include_once('./classes/database/Postgres73.php');
@@ -186,9 +186,10 @@ class Postgres74 extends Postgres73 {
        /**
         * Grabs a list of indexes for a table
         * @param $table The name of a table whose indexes to retrieve
+        * @param $unique Only get unique/pk indexes
         * @return A recordset
         */
-       function &getIndexes($table = '') {
+       function &getIndexes($table = '', $unique = false) {
                $this->clean($table);
 
                $sql = "SELECT c2.relname AS indname, i.indisprimary, i.indisunique, i.indisclustered,
@@ -196,7 +197,9 @@ class Postgres74 extends Postgres73 {
                        FROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i
                        WHERE c.relname = '{$table}' AND pg_catalog.pg_table_is_visible(c.oid) 
                        AND c.oid = i.indrelid AND i.indexrelid = c2.oid
-                       ORDER BY c2.relname";
+               ";
+               if ($unique) $sql .= " AND i.indisunique ";
+               $sql .= " ORDER BY c2.relname";
 
                return $this->selectSet($sql);
        }
index 182b67b77654f626537cb22c04df8064b1814935..f498a7d1061dbc5acdf739d8c96c7b536531de85 100755 (executable)
@@ -3,7 +3,7 @@
 /**
  * A class that implements the Slony 1.0.x support plugin
  *
- * $Id: Slony.php,v 1.1.2.23 2005/06/15 14:32:58 chriskl Exp $
+ * $Id: Slony.php,v 1.1.2.24 2005/06/16 14:12:49 chriskl Exp $
  */
 
 include_once('./classes/plugins/Plugin.php');
@@ -457,44 +457,71 @@ class Slony extends Plugin {
        /**
         * Adds a table to a replication set
         */
-       function addTable($set_id, $tab_id, $fqname, $idxname, $comment, $addtablekey) {
+       function addTable($set_id, $tab_id, $nspname, $relname, $idxname, $comment, $storedtriggers) {
                global $data;
 
                $schema = $this->slony_schema;
                $data->fieldClean($schema);
                $data->clean($set_id);
                $data->clean($tab_id);
+               $fqname = $nspname . '.' . $relname;
                $data->clean($fqname);
+               $data->clean($nspname);
+               $data->clean($relname);
                $data->clean($idxname);
                $data->clean($comment);
 
-               if ($addtablekey) {
+               $hastriggers = (sizeof($storedtriggers) > 0);
+               if ($hastriggers) {
                        // Begin a transaction
                        $status = $data->beginTransaction();
                        if ($status != 0) return -1;
-
-                       // Add the table key
-                       $sql = "SELECT \"{$schema}\".tableaddkey('{$fqname}')";
-                       $status = $data->execute($sql);
-                       if ($status != 0) {
-                               $data->rollbackTransaction();
-                               return -2;
-                       }
                }
 
                if ($tab_id != '')
                        $sql = "SELECT \"{$schema}\".setaddtable('{$set_id}', '{$tab_id}', '{$fqname}', '{$idxname}', '{$comment}')";
-               else
-                       $sql = "SELECT \"{$schema}\".setaddtable('{$set_id}', (SELECT COALESCE(MAX(tab_id), 0) + 1 FROM \"{$schema}\".sl_table), '{$fqname}', '{$idxname}', '{$comment}')";
+               else {
+                       $sql = "SELECT \"{$schema}\".setaddtable('{$set_id}', (SELECT COALESCE(MAX(tab_id), 0) + 1 FROM \"{$schema}\".sl_table), '{$fqname}', '{$idxname}', '{$comment}')";                     
+               }
 
                $status = $data->execute($sql); 
-               
-               if ($status != 0 && $addtablekey) {
-                       $data->rollbackTransaction();
+               if ($status != 0) {
+                       if ($hastriggers) $data->rollbackTransaction();
                        return -3;
+               }               
+
+               // If we are storing triggers, we need to know the tab_id that was assigned to the table
+               if ($tab_id == '' && $hastriggers) {
+                       $sql = "SELECT tab_id
+                                               FROM \"{$schema}\".sl_table
+                                               WHERE tab_set='{$set_id}'
+                                               AND tab_reloid=(SELECT pc.oid FROM pg_catalog.pg_class pc, pg_namespace pn 
+                                                                                               WHERE pc.relnamespace=pn.oid AND pc.relname='{$relname}'
+                                                                                               AND pn.nspname='{$nspname}')";
+                       $tab_id = $data->selectField($sql, 'tab_id');
+                       if ($tab_id === -1) {
+                               $data->rollbackTransaction();
+                               return -4;
+                       }
                }
                
-               return $status;
+               // Store requested triggers
+               if ($hastriggers) {
+                       foreach ($storedtriggers as $tgname) {
+                               $data->clean($tgname);
+                               $sql = "SELECT \"{$schema}\".storetrigger('{$tab_id}', '{$tgname}')";
+                               $status = $data->execute($sql); 
+                               if ($status != 0) {
+                                       $data->rollbackTransaction();
+                                       return -5;
+                               }               
+                       }                               
+               }
+
+               if ($hastriggers)
+                       return $data->endTransaction();
+               else
+                       return $status;
        }
                
        /**
index b2c74d11988454ad8b411c192632cc3d9e3bd5f1..d92e5063f53840d922e94c026b07fe66d636e53a 100755 (executable)
@@ -4,7 +4,7 @@
         * English language file for phpPgAdmin.  Use this as a basis
         * for new translations.
         *
-        * $Id: english.php,v 1.176.2.6 2005/06/15 19:05:10 soranzo Exp $
+        * $Id: english.php,v 1.176.2.7 2005/06/16 14:12:49 chriskl Exp $
         */
 
        // Language and character set
        $lang['strscriptneedsbody'] = 'You must supply a script to be executed on all nodes.';
        $lang['strscriptexecuted'] = 'Replication set DDL script executed.';
        $lang['strscriptexecutedbad'] = 'Failed executing replication set DDL script.';
+       $lang['strtabletriggerstoretain'] = 'The following triggers will NOT be disabled by Slony:';
 
        // Slony tables in replication sets
        $lang['straddtable'] = 'Add table';
index 55f24a01d03c175959b046968fb00cf62974208e..df044c98bc2d965f2c27f8984fcd4f643ac742de 100644 (file)
@@ -4,7 +4,7 @@
         * English language file for phpPgAdmin.  Use this as a basis
         * for new translations.
         *
-        * $Id: english.php,v 1.128.2.7 2005/06/15 19:05:11 soranzo Exp $
+        * $Id: english.php,v 1.128.2.8 2005/06/16 14:12:49 chriskl Exp $
         */
 
        // Language and character set
        $lang['strscriptneedsbody'] = 'You must supply a script to be executed on all nodes.';
        $lang['strscriptexecuted'] = 'Replication set DDL script executed.';
        $lang['strscriptexecutedbad'] = 'Failed executing replication set DDL script.';
+       $lang['strtabletriggerstoretain'] = 'The following triggers will NOT be disabled by Slony:';
 
        // Slony tables in replication sets
        $lang['straddtable'] = 'Add table';
index 0283affa9cf938d44960d28a82306289a05a98a3..c0fed27b8e67ffb5c17bd0d1b1361c76c5472fb0 100755 (executable)
@@ -3,7 +3,7 @@
        /**
         * Slony database tab plugin
         *
-        * $Id: plugin_slony.php,v 1.1.2.24 2005/06/15 19:05:09 soranzo Exp $
+        * $Id: plugin_slony.php,v 1.1.2.25 2005/06/16 14:12:47 chriskl Exp $
         */
 
        // Include application functions
                                        $key = array('schemaname' => $tables->f['nspname'], 'tablename' => $tables->f['relname']);
                                        $key = serialize($key);
                                        echo "<option value=\"", htmlspecialchars($key), "\">";
-                                       if ($data->hasSchemas()) {
-                                                       echo htmlspecialchars($tables->f['nspname']), '.';
-                                       }
+                                       echo htmlspecialchars($tables->f['nspname']), '.';
                                        echo htmlspecialchars($tables->f['relname']), "</option>\n";
                                        $tables->moveNext();    
                                }
                                        htmlspecialchars($_POST['comment']), "</textarea></td>\n\t</tr>\n";
                                        
                                echo "\t</tr>\n";
-                               echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['stradduniq']}</th>\n";
-                               echo "\t\t<td class=\"data\"><input type=\"checkbox\" name=\"addtablekey\"", 
-                                       (isset($_REQUEST['addtablekey']) ? ' checked="checked"' : ''), " /></td>\n\t</tr>\n";
                                echo "</table>\n";
                                echo "<p>\n";
                                echo "<input type=\"hidden\" name=\"action\" value=\"add_table\" />\n";
                                // Unserialize table and fetch. This is a bit messy
                                // because the table could be in another schema.
                                $_REQUEST['target'] = unserialize($_REQUEST['target']);
-                               if ($data->hasSchemas())
-                                       $data->setSchema($_REQUEST['target']['schemaname']);
-                               $indexes = &$data->getIndexes($_REQUEST['target']['tablename']);
+                               $data->setSchema($_REQUEST['target']['schemaname']);
+                               // Get indexes
+                               $indexes = &$data->getIndexes($_REQUEST['target']['tablename'], true);
                                if ($indexes->recordCount() == 0) {
-                                       doTables($lang['strtableneedsuniquekey']);
-                                       return; 
+                                       doAddTable(1, $lang['strtableneedsuniquekey']);
+                                       return;
+                               }
+                               
+                               // Get triggers
+                               $triggers = &$data->getTriggers($_REQUEST['target']['tablename']);                              
+
+                               // If only one index and no triggers then jump to next step
+                               if ($indexes->recordCount() == 1 && $triggers->recordCount() == 0) {
+                                       $_REQUEST['idxname'] = $indexes->f['indname'];
+                                       $_REQUEST['nspname'] = $_REQUEST['target']['schemaname'];
+                                       $_REQUEST['relname'] = $_REQUEST['target']['tablename'];
+                                       $_REQUEST['target'] = serialize($_REQUEST['target']);
+                                       doAddTable(3);
+                                       return;
                                }
                                
                                $misc->printTrail('slony_sets');
                                echo "<form action=\"$PHP_SELF\" method=\"post\">\n";
                                echo $misc->form;
                                echo "<table>\n";
-                               echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strindex']}</th>\n";
-                               echo "<td class=\"data1\" colspan=\"3\"><select name=\"idxname\">";
-                               while (!$indexes->EOF) {
-                                       echo "<option value=\"", htmlspecialchars($indexes->f['indname']), "\">";
-                                       echo htmlspecialchars($indexes->f['indname']), "</option>\n";
-                                       $indexes->moveNext();   
+                               if ($indexes->recordCount() > 1) {
+                                       echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strindex']}</th>\n";
+                                       echo "<td class=\"data1\" colspan=\"3\"><select name=\"idxname\">";
+                                       while (!$indexes->EOF) {
+                                               echo "<option value=\"", htmlspecialchars($indexes->f['indname']), "\">";
+                                               echo htmlspecialchars($indexes->f['indname']), "</option>\n";
+                                               $indexes->moveNext();   
+                                       }
+                                       echo "</select></td></tr>\n";
+                               }
+                               else {
+                                       echo "<input type=\"hidden\" name=\"idxname\" value=\"", htmlspecialchars($indexes->f['indname']), "\" />\n";
+                               }
+                               if ($triggers->recordCount() > 0) {
+                                       echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strtriggers']}</th>\n";
+                                       echo "<td class=\"data1\" colspan=\"3\"><p>{$lang['strtabletriggerstoretain']}</p>\n";
+                                       while (!$triggers->EOF) {
+                                               echo "<input type=\"checkbox\" name=\"storedtriggers[", htmlspecialchars($triggers->f['tgname']), "]\">";
+                                               echo htmlspecialchars($triggers->f['tgname']), "<br/>\n";
+                                               $triggers->moveNext();  
+                                       }
+                                       echo "</select></td></tr>\n";                                   
                                }
-                               echo "</select></td></tr>\n";                                   
-                               echo "\t</tr>\n";
                                echo "</table>\n";
                                echo "<p>\n";
                                echo "<input type=\"hidden\" name=\"action\" value=\"add_table\" />\n";
                                echo "<input type=\"hidden\" name=\"set_id\" value=\"", htmlspecialchars($_REQUEST['set_id']), "\" />\n";
                                echo "<input type=\"hidden\" name=\"tab_id\" value=\"", htmlspecialchars($_REQUEST['tab_id']), "\" />\n";
                                echo "<input type=\"hidden\" name=\"comment\" value=\"", htmlspecialchars($_REQUEST['comment']), "\" />\n";
-                               if (isset($_REQUEST['addtablekey'])) {
-                                       echo "<input type=\"hidden\" name=\"addtablekey\" value=\"on\" />\n";
-                               }
-                               echo "<input type=\"hidden\" name=\"fqname\" value=\"", htmlspecialchars($_REQUEST['target']['schemaname']),
-                                       '.', htmlspecialchars($_REQUEST['target']['tablename']), "\" />\n";
+                               echo "<input type=\"hidden\" name=\"nspname\" value=\"", htmlspecialchars($_REQUEST['target']['schemaname']), "\" />\n";
+                               echo "<input type=\"hidden\" name=\"relname\" value=\"", htmlspecialchars($_REQUEST['target']['tablename']), "\" />\n";
+                               echo "<input type=\"hidden\" name=\"target\" value=\"", htmlspecialchars(serialize($_REQUEST['target'])), "\" />\n";
                                echo "<input type=\"hidden\" name=\"stage\" value=\"3\" />\n";
                                echo "<input type=\"submit\" value=\"{$lang['stradd']}\" />\n";
                                echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n";
                                echo "</form>\n";
                                break;
                        case 3:
-                               $status = $slony->addTable($_REQUEST['set_id'], $_REQUEST['tab_id'], $_REQUEST['fqname'], 
-                                                                                                               $_REQUEST['idxname'], $_REQUEST['comment'], isset($_REQUEST['addtablekey']));
+                               if (!isset($_REQUEST['storedtriggers'])) $_REQUEST['storedtriggers'] = array();
+                               $status = $slony->addTable($_REQUEST['set_id'], $_REQUEST['tab_id'], $_REQUEST['nspname'], $_REQUEST['relname'], 
+                                                                                                               $_REQUEST['idxname'], $_REQUEST['comment'], array_keys($_REQUEST['storedtriggers']));
                                if ($status == 0)
                                        doTables($lang['strtableaddedtorepset']);
                                else
-                                       doAddTable(1, $lang['strtableaddedtorepsetbad']);
+                                       doAddTable(2, $lang['strtableaddedtorepsetbad']);
                                break;
                }
        }
                                        $key = array('schemaname' => $sequences->f['nspname'], 'sequencename' => $sequences->f['seqname']);
                                        $key = serialize($key);
                                        echo "<option value=\"", htmlspecialchars($key), "\">";
-                                       if ($data->hasSchemas()) {
-                                                       echo htmlspecialchars($sequences->f['nspname']), '.';
-                                       }
+                                       echo htmlspecialchars($sequences->f['nspname']), '.';
                                        echo htmlspecialchars($sequences->f['seqname']), "</option>\n";
                                        $sequences->moveNext(); 
                                }