3 namespace pq\Gateway\Table
;
7 const IDENTITY_SQL
= <<<SQL
11 join pg_index i on c.oid = i.indrelid
12 join pg_attribute a on c.oid = a.attrelid
15 and a.attnum = any(i.indkey)
22 * A primary key implementation
24 class Identity
implements \Countable
, \IteratorAggregate
29 protected $columns = array();
32 * @param \pq\Gateway\Table $table
34 function __construct(Table
$table) {
35 $cache = $table->getMetadataCache();
36 if (!($this->columns
= $cache->get("$table#identity"))) {
37 $table->getQueryExecutor()->execute(
38 new \pq\Query\
Writer(IDENTITY_SQL
, array($table->getName())),
39 function($result) use($table, $cache) {
40 $this->columns
= array_map("current", $result->fetchAll(\pq\Result
::FETCH_ARRAY
));
41 $cache->set("$table#identity", $this->columns
);
48 * @implements \Countable
52 return count($this->columns
);
56 * @implements \IteratorAggregate
57 * @return \ArrayIterator
59 function getIterator() {
60 return new \
ArrayIterator($this->columns
);
64 * Get the column names which the primary key contains
67 function getColumns() {
68 return $this->columns
;