From 83953f257f77606dfd7157ec1a663053b0f8fd7b Mon Sep 17 00:00:00 2001 From: chriskl Date: Thu, 16 Jun 2005 14:12:47 +0000 Subject: [PATCH] Support for storing triggers. --- classes/database/Postgres.php | 9 ++-- classes/database/Postgres73.php | 9 ++-- classes/database/Postgres74.php | 9 ++-- classes/plugins/Slony.php | 61 ++++++++++++++++++------- lang/english.php | 3 +- lang/recoded/english.php | 3 +- plugin_slony.php | 81 ++++++++++++++++++++------------- 7 files changed, 116 insertions(+), 59 deletions(-) diff --git a/classes/database/Postgres.php b/classes/database/Postgres.php index 303d968a..2fe39805 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.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); } diff --git a/classes/database/Postgres73.php b/classes/database/Postgres73.php index 227bbad3..d1bfaf88 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.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); } diff --git a/classes/database/Postgres74.php b/classes/database/Postgres74.php index 94c010bf..2e2248a8 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.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); } diff --git a/classes/plugins/Slony.php b/classes/plugins/Slony.php index 182b67b7..f498a7d1 100755 --- a/classes/plugins/Slony.php +++ b/classes/plugins/Slony.php @@ -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; } /** diff --git a/lang/english.php b/lang/english.php index b2c74d11..d92e5063 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.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 @@ -725,6 +725,7 @@ $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'; diff --git a/lang/recoded/english.php b/lang/recoded/english.php index 55f24a01..df044c98 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.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 @@ -725,6 +725,7 @@ $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'; diff --git a/plugin_slony.php b/plugin_slony.php index 0283affa..c0fed27b 100755 --- a/plugin_slony.php +++ b/plugin_slony.php @@ -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 @@ -1553,9 +1553,7 @@ $key = array('schemaname' => $tables->f['nspname'], 'tablename' => $tables->f['relname']); $key = serialize($key); echo "\n"; $tables->moveNext(); } @@ -1568,9 +1566,6 @@ htmlspecialchars($_POST['comment']), "\n\t\n"; echo "\t\n"; - echo "\t\n\t\t{$lang['stradduniq']}\n"; - echo "\t\t\n\t\n"; echo "\n"; echo "

\n"; echo "\n"; @@ -1585,12 +1580,25 @@ // 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'); @@ -1600,26 +1608,38 @@ echo "

\n"; echo $misc->form; echo "\n"; - echo "\t\n\t\t\n"; - echo "\n\t\t\n"; + echo "\n"; + } + else { + echo "f['indname']), "\" />\n"; + } + if ($triggers->recordCount() > 0) { + echo "\t\n\t\t\n"; + echo "\n"; } - echo "\n"; - echo "\t\n"; echo "
{$lang['strindex']}
{$lang['strindex']}
{$lang['strtriggers']}

{$lang['strtabletriggerstoretain']}

\n"; + while (!$triggers->EOF) { + echo "f['tgname']), "]\">"; + echo htmlspecialchars($triggers->f['tgname']), "
\n"; + $triggers->moveNext(); + } + echo "
\n"; echo "

\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; - if (isset($_REQUEST['addtablekey'])) { - echo "\n"; - } - echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; echo "\n"; echo "\n"; echo "\n"; @@ -1627,12 +1647,13 @@ echo "

\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; } } @@ -1805,9 +1826,7 @@ $key = array('schemaname' => $sequences->f['nspname'], 'sequencename' => $sequences->f['seqname']); $key = serialize($key); echo "\n"; $sequences->moveNext(); } -- 2.39.5