X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fpq-gateway;a=blobdiff_plain;f=lib%2Fpq%2FGateway%2FTable%2FAttributes.php;fp=lib%2Fpq%2FGateway%2FTable%2FAttributes.php;h=39c55c948e00fbdfddb7058780f275c81c321411;hp=0000000000000000000000000000000000000000;hb=409fca54acfee2db6c62540a8f67b1adfa695a38;hpb=0b0830c9c6254d596808d461755309200582825a diff --git a/lib/pq/Gateway/Table/Attributes.php b/lib/pq/Gateway/Table/Attributes.php new file mode 100644 index 0000000..39c55c9 --- /dev/null +++ b/lib/pq/Gateway/Table/Attributes.php @@ -0,0 +1,72 @@ + 0 +SQL; + +class Attributes +{ + /** + * @var array + */ + protected $columns = array(); + + /** + * @param \pq\Gateway\Table $table + */ + function __construct(Table $table) { + $cache = $table->getMetadataCache(); + if (!($this->columns = $cache->get("$table#attributes"))) { + $table->getQueryExecutor()->execute( + new \pq\Query\Writer(ATTRIBUTES_SQL, array($table->getName())), + function($result) use($table, $cache) { + foreach ($result->fetchAll(\pq\Result::FETCH_OBJECT) as $c) { + $this->columns[$c->index] = $this->columns[$c->name] = $c; + } + $cache->set("$table#attributes", $this->columns); + } + ); + } + } + + /** + * @implements \Countable + * @return int + */ + function count() { + return count($this->columns); + } + + /** + * Get all columns + * @return array + */ + function getColumns() { + return $this->columns; + } + + /** + * Get a single column + * @param string $c + * @return object + */ + function getColumn($c) { + if (!isset($this->columns[$c])) { + throw new \OutOfBoundsException("Unknown column $c"); + } + return $this->columns[$c]; + } +}