add basic trigger support. fix schema problems when getting indexes.
authorchriskl <chriskl>
Sun, 19 Jan 2003 02:47:25 +0000 (02:47 +0000)
committerchriskl <chriskl>
Sun, 19 Jan 2003 02:47:25 +0000 (02:47 +0000)
classes/database/Postgres.php
classes/database/Postgres73.php
triggers.php [new file with mode: 0644]

index a86809ae2201e7eda48c9975d4ee5e1c000f59f1..a42e4ded3954d9250f18766fdcef1846b0d4dae9 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.44 2003/01/18 09:07:50 chriskl Exp $
+ * $Id: Postgres.php,v 1.45 2003/01/19 02:47:25 chriskl Exp $
  */
 
 // @@@ THOUGHT: What about inherits? ie. use of ONLY???
@@ -1341,6 +1341,39 @@ class Postgres extends BaseDB {
                return $this->execute($sql);
        }
 
+       // Trigger functions
+
+       /**
+        * Grabs a list of triggers on a table
+        * @param $table The name of a table whose triggers to retrieve
+        * @return A recordset
+        */
+       function &getTriggers($table = '') {
+               $this->clean($table);
+
+               $sql = "SELECT tgname
+                       FROM pg_trigger
+                       WHERE tgrelid = (SELECT oid FROM pg_class WHERE relname='{$table}')
+                       AND NOT tgisconstraint";
+
+               return $this->selectSet($sql);
+       }
+
+       /**
+        * Drops a trigger
+        * @param $tgname The name of the trigger to drop
+        * @param $table The table from which to drop the trigger
+        * @return 0 success
+        */
+       function dropTrigger($tgname, $table) {
+               $this->fieldClean($tgname);
+               $this->fieldClean($table);
+
+               $sql = "DROP TRIGGER \"{$tgname}\" ON \"{$table}\"";
+
+               return $this->execute($sql);
+       }
+
        // Capabilities
        function hasTables() { return true; }
        function hasViews() { return true; }
index f536dd3d81fbb1d466ce63f20002130a53c441f1..917c19847ba61ecee14d07c015805fca3ceaa2c2 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.17 2003/01/18 06:38:37 chriskl Exp $
+ * $Id: Postgres73.php,v 1.18 2003/01/19 02:47:25 chriskl Exp $
  */
 
 // @@@ THOUGHT: What about inherits? ie. use of ONLY???
@@ -354,7 +354,8 @@ class Postgres73 extends Postgres72 {
 
                $sql = "SELECT c2.relname, i.indisprimary, i.indisunique, pg_catalog.pg_get_indexdef(i.indexrelid) as pg_get_indexdef
                        FROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i
-                       WHERE c.relname = '{$table}' AND c.oid = i.indrelid AND i.indexrelid = c2.oid
+                       WHERE c.relname = '{$table}' AND pg_catalog.pg_table_is_visible(c.oid) 
+                       AND c.oid = i.indrelid AND i.indexrelid = c2.oid
                        AND NOT i.indisprimary
                        ORDER BY i.indisunique DESC, c2.relname";
 
@@ -368,14 +369,16 @@ class Postgres73 extends Postgres72 {
         */
        function &getTriggers($table = '') {
                $this->clean($table);
-               
+
                $sql = "SELECT t.tgname
                        FROM pg_catalog.pg_trigger t
-                       WHERE t.tgrelid = (SELECT oid FROM pg_catalog.pg_class WHERE relname='{$table}') and (not tgisconstraint  OR NOT EXISTS
-                       (SELECT 1 FROM pg_catalog.pg_depend d    JOIN pg_catalog.pg_constraint c 
-                       ON (d.refclassid = c.tableoid AND d.refobjid = c.oid)    
+                       WHERE t.tgrelid = (SELECT oid FROM pg_catalog.pg_class WHERE relname='{$table}'
+                       AND relnamespace=(SELECT oid FROM pg_catalog.pg_namespace WHERE nspname='{$this->_schema}'))
+                       AND (NOT tgisconstraint OR NOT EXISTS
+                       (SELECT 1 FROM pg_catalog.pg_depend d    JOIN pg_catalog.pg_constraint c
+                       ON (d.refclassid = c.tableoid AND d.refobjid = c.oid)
                        WHERE d.classid = t.tableoid AND d.objid = t.oid AND d.deptype = 'i' AND c.contype = 'f'))";
-                       
+
                return $this->selectSet($sql);
        }
 
diff --git a/triggers.php b/triggers.php
new file mode 100644 (file)
index 0000000..4f13872
--- /dev/null
@@ -0,0 +1,108 @@
+<?php
+
+       /**
+        * List triggers on a table
+        *
+        * $Id: triggers.php,v 1.1 2003/01/19 02:47:25 chriskl Exp $
+        */
+
+       // Include application functions
+       include_once('conf/config.inc.php');
+
+       $action = (isset($_REQUEST['action'])) ? $_REQUEST['action'] : '';
+       $PHP_SELF = $_SERVER['PHP_SELF'];
+
+       /**
+        * Show confirmation of drop and perform actual drop
+        */
+       function doDrop($confirm) {
+               global $localData, $misc;
+               global $PHP_SELF, $strDrop, $strConfDropTrigger, $strTriggerDropped, $strTriggerDroppedBad, $strYes, $strNo;
+
+               if ($confirm) {
+                       echo "<h2>", htmlspecialchars($_REQUEST['database']), ": Tables: ",
+                               htmlspecialchars($_REQUEST['table']), ": " , htmlspecialchars($_REQUEST['trigger']), ": {$strDrop}</h2>\n";
+
+                       echo "<p>", sprintf($strConfDropTrigger, htmlspecialchars($_REQUEST['trigger']),
+                               htmlspecialchars($_REQUEST['table'])), "</p>\n";
+
+                       echo "<form action=\"$PHP_SELF\" method=\"post\">\n";
+                       echo "<input type=\"hidden\" name=\"action\" value=\"drop\">\n";
+                       echo "<input type=\"hidden\" name=\"table\" value=\"", htmlspecialchars($_REQUEST['table']), "\">\n";
+                       echo "<input type=\"hidden\" name=\"trigger\" value=\"", htmlspecialchars($_REQUEST['trigger']), "\">\n";
+                       echo $misc->form;
+                       echo "<input type=\"submit\" name=\"choice\" value=\"{$strYes}\"> <input type=\"submit\" name=\"choice\" value=\"{$strNo}\">\n";
+                       echo "</form>\n";
+               }
+               else {
+                       $status = $localData->dropTrigger($_POST['trigger'], $_POST['table']);
+                       if ($status == 0)
+                               doDefault($strTriggerDropped);
+                       else
+                               doDefault($strTriggerDroppedBad);
+               }
+
+       }
+
+       /**
+        * List all the triggers on the table
+        */
+       function doDefault($msg = '') {
+               global $data, $localData, $misc;
+               global $PHP_SELF;
+               global $strTriggers, $strName, $strActions, $strNoTriggers, $strCreateTrigger, $strDrop;
+
+               $misc->printTableNav();
+               echo "<h2>", htmlspecialchars($_REQUEST['database']), ": ", htmlspecialchars($_REQUEST['table']), ": {$strTriggers}</h2>\n";
+               $misc->printMsg($msg);
+
+               $triggers = &$localData->getTriggers($_REQUEST['table']);
+
+               if ($triggers->recordCount() > 0) {
+                       echo "<table>\n";
+                       echo "<tr><th class=\"data\">{$strName}</th><th class=\"data\">{$strActions}</th>\n";
+                       $i = 0;
+                       
+                       while (!$triggers->EOF) {
+                               $id = ( ($i % 2 ) == 0 ? '1' : '2' );
+                               echo "<tr><td class=\"data{$id}\">", htmlspecialchars( $triggers->f[$data->tgFields['tgname']]), "</td>";
+                               echo "<td class=\"data{$id}\">";
+                               echo "<a href=\"$PHP_SELF?action=confirm_drop&{$misc->href}&trigger=", htmlspecialchars( $triggers->f[$data->tgFields['tgname']]),
+                                       "&table=", htmlspecialchars($_REQUEST['table']), "\">{$strDrop}</td></tr>\n";
+
+                               $triggers->moveNext();
+                               $i++;
+                       }
+
+                       echo "</table>\n";
+                       }
+               else
+                       echo "<p>{$strNoTriggers}</p>\n";
+               
+               //echo "<p><a class=\"navlink\" href=\"$PHP_SELF?action=create&{$misc->href}&table=", htmlspecialchars($_REQUEST['table']), "\">{$strCreateTrigger}</a></p>\n";
+       }
+
+       $misc->printHeader($strTables . ' - ' . $_REQUEST['table'] . ' - ' . $strTriggers);
+
+       switch ($action) {
+               case 'save_create':
+                       doSaveCreate();
+                       break;
+               case 'create':
+                       doCreate();
+                       break;
+               case 'drop':
+                       if ($_POST['choice'] == $strYes) doDrop(false);
+                       else doDefault();
+                       break;
+               case 'confirm_drop':
+                       doDrop(true);
+                       break;
+               default:
+                       doDefault();
+                       break;
+       }
+       
+       $misc->printFooter();
+
+?>