3e4d7fbd8280a622d352ff1e9e607c3b3fe74fc9
3 namespace pq\Gateway\Table
;
7 const RELATION_SQL
= <<<SQL
12 else substring(att1.attname from '^.*(?=_'||att2.attname||'$)')
14 ,cl1.relname as "foreignTable"
15 ,att1.attname as "foreignColumn"
16 ,cl2.relname as "referencedTable"
17 ,att2.attname as "referencedColumn"
28 and co.conrelid = cl1.oid
29 and co.conkey[1] = att1.attnum and cl1.oid = att1.attrelid
30 and co.confrelid = cl2.oid
31 and co.confkey[1] = att2.attnum and cl2.oid = att2.attrelid
38 * A foreighn key implementation
45 protected $references;
47 function __construct(Table
$table) {
48 $cache = $table->getMetadataCache();
49 if (!($this->references
= $cache->get("$table#relations"))) {
50 $table->getQueryExecutor()->execute(
51 new \pq\Query\
Writer(RELATION_SQL
, array($table->getName())),
52 function($result) use($table, $cache) {
53 $this->references
= $result->map(array(0,1), array(1,2,3,4), \pq\Result
::FETCH_OBJECT
);
54 $cache->set("$table#relations", $this->references
);
60 function __isset($r) {
61 return isset($this->references
->$r);
65 return $this->references
->$r;
68 function __set($r, $v) {
69 $this->references
->$r = $v;
73 unset($this->references
->$r);