X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fpq-gateway;a=blobdiff_plain;f=lib%2Fpq%2FGateway%2FTable%2FRelations.php;fp=lib%2Fpq%2FGateway%2FTable%2FRelations.php;h=700f3fcad524829daf7b009632ac6f669ea8a75a;hp=3e4d7fbd8280a622d352ff1e9e607c3b3fe74fc9;hb=ccdcd2977c8bdc7c74b89b2fb7c2ea46479692b8;hpb=80c4d645e1650304b1dbcae85dbf17a6593896e4 diff --git a/lib/pq/Gateway/Table/Relations.php b/lib/pq/Gateway/Table/Relations.php index 3e4d7fb..700f3fc 100644 --- a/lib/pq/Gateway/Table/Relations.php +++ b/lib/pq/Gateway/Table/Relations.php @@ -4,13 +4,17 @@ namespace pq\Gateway\Table; use \pq\Gateway\Table; +/* + * case when att1.attname like '%\_'||att2.attname then + substring(att1.attname from '^.*(?=_'||att2.attname||'$)') + else + att1.attname + end + */ const RELATION_SQL = <<getMetadataCache(); - if (!($this->references = $cache->get("$table#relations"))) { + 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(1,2,3,4), \pq\Result::FETCH_OBJECT); - $cache->set("$table#relations", $this->references); + $rel = $result->map([3,0], null, \pq\Result::FETCH_ASSOC); + foreach ($rel as $table => $reference) { + foreach ($reference as $name => $ref) { + $this->references[$table][$name] = new Reference($ref); + } + } + $cache->set("$table:relations", $this->references); } ); } } function __isset($r) { - return isset($this->references->$r); + return isset($this->references[$r]); } function __get($r) { - return $this->references->$r; + return $this->references[$r]; } function __set($r, $v) { - $this->references->$r = $v; + $this->references[$r] = $v; } function __unset($r){ - unset($this->references->$r); + unset($this->references[$r]); + } + + /** + * Get a reference to a table + * @param string $table + * @param string $ref + * @return \pq\Gateway\Table\Reference + */ + function getReference($table, $ref = null) { + if (isset($this->references[$table])) { + if (!strlen($ref)) { + return current($this->references[$table]); + } + if (isset($this->references[$table][$ref])) { + return $this->references[$table][$ref]; + } + } + } + + /** + * Implements \Countable + * @return int + */ + function count() { + return array_sum(array_map("count", $this->references)); + } + + /** + * Implements \IteratorAggregate + * @return \RecursiveArrayIterator + */ + function getIterator() { + return new \RecursiveArrayIterator($this->references); } }