50% done on proper trigger definitions
authorchriskl <chriskl>
Tue, 25 Mar 2003 15:28:22 +0000 (15:28 +0000)
committerchriskl <chriskl>
Tue, 25 Mar 2003 15:28:22 +0000 (15:28 +0000)
classes/database/Postgres.php
classes/database/Postgres72.php
classes/database/Postgres73.php
triggers.php

index d1333a1dfde7ee280e399be2663130fd7c38759c..ddc1abb2110ffa6cf48a524457df6e82b95cd36f 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.64 2003/03/25 00:26:27 chriskl Exp $
+ * $Id: Postgres.php,v 1.65 2003/03/25 15:28:23 chriskl Exp $
  */
 
 // @@@ THOUGHT: What about inherits? ie. use of ONLY???
@@ -1646,6 +1646,98 @@ class Postgres extends BaseDB {
 
        // Trigger functions
 
+       /**
+        * A helper function for getTriggers that translates
+        * an array of attribute numbers to an array of field names.
+        * @param $trigger An array containing fields from the trigger table
+        * @return The trigger definition string
+        */
+       function &getTriggerDef($trigger) {
+               // Constants to figure out tgtype
+
+               define ('TRIGGER_TYPE_ROW', (1 << 0) );
+               define ('TRIGGER_TYPE_BEFORE', (1 << 1) );
+               define ('TRIGGER_TYPE_INSERT', (1 << 2) );
+               define ('TRIGGER_TYPE_DELETE', (1 << 3) );
+               define ('TRIGGER_TYPE_UPDATE', (1 << 4) );
+
+               $trigger['tgisconstraint'] = $this->phpBool($trigger['tgisconstraint']);
+               $trigger['tgdeferrable'] = $this->phpBool($trigger['tgdeferrable']);
+               $trigger['tginitdeferred'] = $this->phpBool($trigger['tginitdeferred']);
+
+               // Constraint trigger or normal trigger
+               if ($trigger['tgisconstraint'])
+                       $tgdef = 'CREATE CONSTRAINT TRIGGER ';
+               else
+                       $tgdef = 'CREATE TRIGGER ';
+
+               // Trigger type
+               $findx = 0;
+               if ($trigger['tgtype'] & TRIGGER_TYPE_BEFORE == TRIGGER_TYPE_BEFORE)
+                       $tgdef .= 'BEFORE';
+               else
+                       $tgdef .= 'AFTER';
+
+               if ($trigger['tgtype'] & TRIGGER_TYPE_INSERT == TRIGGER_TYPE_INSERT) {
+                       $tgdef .= ' INSERT';
+                       $findx++;
+               }
+               if ($trigger['tgtype'] & TRIGGER_TYPE_DELETE == TRIGGER_TYPE_DELETE) {
+                       if ($findx > 0)
+                               $tgdef .= ' OR DELETE';
+                       else
+                               $tgdef .= ' DELETE';
+                       $findx++;
+               }
+               if ($trigger['tgtype'] & TRIGGER_TYPE_UPDATE == TRIGGER_TYPE_UPDATE) {
+                       if ($findx > 0)
+                               $tgdef .= ' OR UPDATE';
+                       else
+                               $tgdef .= ' UPDATE';
+               }
+       
+               // Table name
+               $tgdef .= " ON \"{$trigger['tgname']}\" ";
+               
+               // Deferrability
+               if ($trigger['tgisconstraint']) {
+                       if ($trigger['tgconstrrelid'] != 0) {
+                               // Assume constrelname is not null
+                               $tgdef .= " FROM \"{$trigger['tgconstrrelname']}\" ";
+                       }
+                       if (!$trigger['tgdeferrable'])
+                               $tgdef .= 'NOT ';
+                       $tgdef .= 'DEFERRABLE INITIALLY ';
+                       if ($trigger['tginitdeferred'])
+                               $tgdef .= 'DEFERRED ';
+                       else
+                               $tgdef .= 'IMMEDIATE ';
+               }
+
+               // Row or statement
+               if ($trigger['tgtype'] & TRIGGER_TYPE_ROW == TRIGGER_TYPE_ROW)
+                       $tgdef .= 'FOR EACH ROW ';
+               else
+                       $tgdef .= 'FOR EACH STATEMENT ';
+
+               // Execute procedure
+               $tgdef .= "EXECUTE PROCEDURE \"{$trigger['tgfname']}\"(";
+               
+               // Parameters
+               $params = explode('\\000', $trigger['tgargs']);
+               for ($findx = 0; $findx < $trigger['tgnargs']; $findx++) {
+                       $param = str_replace('\'', '\\\'', $params[$findx]);
+                       $tgdef .= $param;
+                       if ($findx < ($trigger['tgnargs'] - 1))
+                               $tgdef .= ', ';
+               }
+
+               // Finish it off
+               $tgdef .= ')';
+
+               return $tgdef;
+       }
+
        /**
         * Grabs a list of triggers on a table
         * @param $table The name of a table whose triggers to retrieve
@@ -1654,11 +1746,10 @@ class Postgres extends BaseDB {
        function &getTriggers($table = '') {
                $this->clean($table);
 
-               $sql = "SELECT tgname,tgtype,tgargs,proname
-                       FROM pg_trigger, pg_proc
+               $sql = "SELECT t.*, p.proname
+                       FROM pg_trigger t, pg_proc p
                        WHERE tgrelid = (SELECT oid FROM pg_class WHERE relname='{$table}')
-                         AND pg_proc.oid = pg_trigger.tgfoid                   
-                         AND NOT tgisconstraint";
+                         AND pg_proc.oid = pg_trigger.tgfoid";
 
                return $this->selectSet($sql);
        }
index d639447cc9fcbc9a7800b1b6d0dd31994c5283d4..1024824029a097dd867d4baa03807409ab628851 100644 (file)
@@ -4,7 +4,7 @@
  * A class that implements the DB interface for Postgres
  * Note: This class uses ADODB and returns RecordSets.
  *
- * $Id: Postgres72.php,v 1.29 2003/03/25 00:26:28 chriskl Exp $
+ * $Id: Postgres72.php,v 1.30 2003/03/25 15:28:24 chriskl Exp $
  */
 
 
@@ -332,16 +332,17 @@ class Postgres72 extends Postgres71 {
         * @param $table The name of a table whose triggers to retrieve
         * @return A recordset
         */
+/*
        function &getTriggers($table = '') {
                $this->clean($table);
-               
+
                $sql = "SELECT t.tgname
                                        FROM pg_trigger t, pg_class c
                                        WHERE c.relname='{$table}' AND c.oid = t.tgrelid AND NOT t.tgisconstraint";
-                       
+
                return $this->selectSet($sql);
        }
-
+*/
        // Type functions
 
        /**
index 9789a6de8d7ee0c7675bed670d51ab24339fcc8b..f8e84d167829f1d3f0f4edf723b200fd8ed29859 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.30 2003/03/25 00:26:28 chriskl Exp $
+ * $Id: Postgres73.php,v 1.31 2003/03/25 15:28:24 chriskl Exp $
  */
 
 // @@@ THOUGHT: What about inherits? ie. use of ONLY???
@@ -383,7 +383,7 @@ class Postgres73 extends Postgres72 {
        }
 
        // Constraint functions
-       
+
        /**
         * Drops a unique constraint from a table
         * @param $table The table from which to drop the unique key
@@ -513,7 +513,7 @@ class Postgres73 extends Postgres72 {
                        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
+                       (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'))";
 
index 9459dd3f4a72d12c7265e50915f0cdbc7d006be8..655d32b3c310ce322d5c20e30ff210236b72bdb0 100644 (file)
@@ -3,21 +3,13 @@
        /**
         * List triggers on a table
         *
-        * $Id: triggers.php,v 1.8 2003/03/23 03:13:57 chriskl Exp $
+        * $Id: triggers.php,v 1.9 2003/03/25 15:28:22 chriskl Exp $
         */
 
        // Include application functions
        include_once('libraries/lib.inc.php');
        include_once('classes/class.select.php');
        
-       // Constants to figure out tgtype
-       
-       define ('TRIGGER_TYPE_ROW', (1 << 0) );
-       define ('TRIGGER_TYPE_BEFORE', (1 << 1) );
-       define ('TRIGGER_TYPE_INSERT', (1 << 2) );
-       define ('TRIGGER_TYPE_DELETE', (1 << 3) );
-       define ('TRIGGER_TYPE_UPDATE', (1 << 4) );
-               
        $action = (isset($_REQUEST['action'])) ? $_REQUEST['action'] : '';
        $PHP_SELF = $_SERVER['PHP_SELF'];
 
@@ -26,7 +18,7 @@
        function getTriggerExecTime($type) {
            $execTime = "AFTER";
                if ($type & TRIGGER_TYPE_BEFORE) $execTime = "BEFORE";
-               
+
                return $execTime;
        }