Add constraintExists function, mild cleanup of other nearby funcs.
authorGreg Sabino Mullane <greg@users.mediawiki.org>
Thu, 19 Apr 2007 01:35:15 +0000 (01:35 +0000)
committerGreg Sabino Mullane <greg@users.mediawiki.org>
Thu, 19 Apr 2007 01:35:15 +0000 (01:35 +0000)
includes/DatabasePostgres.php

index 4e28dc1..f2b982d 100644 (file)
@@ -880,7 +880,7 @@ class DatabasePostgres extends Database {
        }
 
        function triggerExists($table, $trigger) {
-       global $wgDBmwschema;
+               global $wgDBmwschema;
 
                $q = <<<END
        SELECT 1 FROM pg_class, pg_namespace, pg_trigger
@@ -892,14 +892,15 @@ END;
                                $this->addQuotes($wgDBmwschema),
                                $this->addQuotes($table),
                                $this->addQuotes($trigger)));
-               $row = $this->fetchRow($res);
-               $exists = !!$row;
+               if (!$res)
+                       return NULL;
+               $rows = pg_num_rows($res);
                $this->freeResult($res);
-               return $exists;
+               return $rows;
        }
 
        function ruleExists($table, $rule) {
-       global $wgDBmwschema;
+               global $wgDBmwschema;
                $exists = $this->selectField("pg_rules", "rulename",
                                array(  "rulename" => $rule,
                                        "tablename" => $table,
@@ -907,6 +908,21 @@ END;
                return $exists === $rule;
        }
 
+       function constraintExists($table, $constraint) {
+               global $wgDBmwschema;
+               $SQL = sprintf("SELECT 1 FROM information_schema.table_constraints ".
+                          "WHERE constraint_schema = %s AND table_name = %s AND constraint_name = %s",
+                       $this->addQuotes($wgDBmwschema),        
+                       $this->addQuotes($table),       
+                       $this->addQuotes($constraint));
+               $res = $this->query($SQL);
+               if (!$res)
+                       return NULL;
+               $rows = pg_num_rows($res);
+               $this->freeResult($res);
+               return $rows;
+       }
+
        /**
         * Query whether a given schema exists. Returns the name of the owner
         */