add identity and lock
[m6w6/pq-gateway] / lib / pq / Gateway / Table / Relations.php
index 0f946fa1154feca0dc0ac84be3a65b9f1caa3a73..ab74d4060b23bdd49b7d795aa8b29101b2486cb7 100644 (file)
@@ -13,32 +13,44 @@ select
     ,att2.attname                                               as "referencedColumn"
 from
      pg_constraint co
-    ,pg_class      cl2
     ,pg_class      cl1
-    ,pg_attribute  att2
+    ,pg_class      cl2
     ,pg_attribute  att1
+    ,pg_attribute  att2
 where
        (       cl1.relname = \$1
        or      cl2.relname = \$1)
 and co.confrelid != 0
-and co.confrelid  = cl2.oid
 and co.conrelid   = cl1.oid
-and co.confkey[1] = att2.attnum and cl2.oid = att2.attrelid
 and co.conkey[1]  = att1.attnum and cl1.oid = att1.attrelid
+and co.confrelid  = cl2.oid
+and co.confkey[1] = att2.attnum and cl2.oid = att2.attrelid
 order by 
         cl1.relname
        ,att1.attnum
 SQL;
 
+/**
+ * A foreighn key implementation
+ */
 class Relations
 {
-       public $references;
+       /**
+        * @var array
+        */
+       protected $references;
        
        function __construct(Table $table) {
-               // FIXME cache
-               $this->references = $table->getConnection()
-                       ->execParams(RELATION_SQL, array($table->getName()))
-                       ->map(array(0,1), array(2,3,4), \pq\Result::FETCH_OBJECT);
+               $cache = $table->getMetadataCache();
+               if (!($this->references = $cache->get("$table#relations"))) {
+                       $table->getQueryExecutor()->execute(
+                               new \pq\Query\Writer(RELATION_SQL, array($table->getName())),
+                               function($result) use($table, $cache) {
+                                       $this->references = $result->map(array(0,1), array(2,3,4), \pq\Result::FETCH_OBJECT);
+                                       $cache->set("$table#relations", $this->references);
+                               }
+                       );
+               }
        }
        
        function __isset($r) {