From: Michael Wallner Date: Fri, 15 Mar 2013 08:16:37 +0000 (+0100) Subject: autoload; cache; tests; X-Git-Tag: v1.0.0~6 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=0e2eb1f13ef60ce9a8709354136c42f7d87b2345;p=m6w6%2Fpq-gateway autoload; cache; tests; --- diff --git a/lib/autoload.php b/lib/autoload.php new file mode 100644 index 0000000..d7b0cd8 --- /dev/null +++ b/lib/autoload.php @@ -0,0 +1,7 @@ +conn = $conn ?: static::$defaultConnection ?: new \pq\Connection; } + /** + * Get the complete PostgreSQL connection string + * @return string + */ + function __toString() { + return sprintf("postgresql://%s:%s@%s:%d/%s?%s#%s", + $this->conn->user, + $this->conn->pass, + $this->conn->host, + $this->conn->port, + $this->conn->db, + $this->conn->options, + $this->getName() + ); + } + /** * Set the rowset prototype * @param mixed $rowset @@ -133,6 +159,26 @@ class Table return $this->exec; } + /** + * Get the metadata cache + * @return \pq\Gateway\Table\CacheInterface + */ + function getMetadataCache() { + if (!isset($this->metadatCache)) { + $this->metadataCache = static::$defaultMetadataCache ?: new Table\StaticCache; + } + return $this->metadataCache; + } + + /** + * Set the metadata cache + * @param \pq\Gateway\Table\CacheInterface $cache + */ + function setMetadataCache(Table\CacheInterface $cache) { + $this->metadataCache = $cache; + return $this; + } + /** * Get foreign key relations * @param string $to fkey @@ -187,7 +233,6 @@ class Table * @return mixed */ protected function execute(QueryWriter $query) { - echo $query,"\n",json_encode($query->getParams()),"\n"; return $this->getQueryExecutor()->execute($query, array($this, "onResult")); } diff --git a/lib/pq/Gateway/Table/CacheInterface.php b/lib/pq/Gateway/Table/CacheInterface.php new file mode 100644 index 0000000..c06b11b --- /dev/null +++ b/lib/pq/Gateway/Table/CacheInterface.php @@ -0,0 +1,30 @@ +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:references"))) { + $this->references = $table->getConnection() + ->execParams(RELATION_SQL, array($table->getName())) + ->map(array(0,1), array(2,3,4), \pq\Result::FETCH_OBJECT); + $cache->set("$table:references", $this->references); + } } function __isset($r) { diff --git a/lib/pq/Gateway/Table/StaticCache.php b/lib/pq/Gateway/Table/StaticCache.php new file mode 100644 index 0000000..7f31346 --- /dev/null +++ b/lib/pq/Gateway/Table/StaticCache.php @@ -0,0 +1,51 @@ +conn = new \pq\Connection(PQ_DSN); - $this->conn->exec(PQ_TEST_DROP_TABLE); - $this->conn->exec(PQ_TEST_CREATE_TABLE); - $this->conn->exec(PQ_TEST_CREATE_DATA); - $this->table = new Table(PQ_TEST_TABLE_NAME, $this->conn); + $this->conn = new \pq\Connection(PQ_TEST_DSN); + $this->conn->exec(PQ_TEST_TABLE_CREATE); + $this->conn->exec(PQ_TEST_REFTABLE_CREATE); + $this->conn->exec(PQ_TEST_DATA); + Table::$defaultConnection = $this->conn; + $this->table = new Table("test"); } protected function tearDown() { - $this->conn->exec(PQ_TEST_DROP_TABLE); + $this->conn->exec(PQ_TEST_REFTABLE_DROP); + $this->conn->exec(PQ_TEST_TABLE_DROP); } /** - * This is very bad test… + * This is a very bad test… */ public function testBasic() { $row = $this->table->find(null, "id desc", 1)->current(); diff --git a/tests/lib/pq/Gateway/RowTest.php b/tests/lib/pq/Gateway/RowTest.php index 28d2728..fc08094 100644 --- a/tests/lib/pq/Gateway/RowTest.php +++ b/tests/lib/pq/Gateway/RowTest.php @@ -17,16 +17,17 @@ class RowTest extends \PHPUnit_Framework_TestCase { protected $table; protected function setUp() { - $this->conn = new \pq\Connection(PQ_DSN); - $this->conn->exec(PQ_TEST_DROP_TABLE); - $this->conn->exec(PQ_TEST_CREATE_TABLE); - $this->conn->exec(PQ_TEST_CREATE_DATA); - - $this->table = new Table(PQ_TEST_TABLE_NAME, $this->conn); + $this->conn = new \pq\Connection(PQ_TEST_DSN); + $this->conn->exec(PQ_TEST_TABLE_CREATE); + $this->conn->exec(PQ_TEST_REFTABLE_CREATE); + $this->conn->exec(PQ_TEST_DATA); + Table::$defaultConnection = $this->conn; + $this->table = new Table("test"); } protected function tearDown() { - $this->conn->exec(PQ_TEST_DROP_TABLE); + $this->conn->exec(PQ_TEST_REFTABLE_DROP); + $this->conn->exec(PQ_TEST_TABLE_DROP); } function testBasic() { diff --git a/tests/lib/pq/Gateway/RowsetTest.php b/tests/lib/pq/Gateway/RowsetTest.php index 7ef1a0e..48ac624 100644 --- a/tests/lib/pq/Gateway/RowsetTest.php +++ b/tests/lib/pq/Gateway/RowsetTest.php @@ -17,16 +17,17 @@ class RowsetTest extends \PHPUnit_Framework_TestCase { protected $table; protected function setUp() { - $this->conn = new \pq\Connection(PQ_DSN); - $this->conn->exec(PQ_TEST_DROP_TABLE); - $this->conn->exec(PQ_TEST_CREATE_TABLE); - $this->conn->exec(PQ_TEST_CREATE_DATA); - - $this->table = new Table(PQ_TEST_TABLE_NAME, $this->conn); + $this->conn = new \pq\Connection(PQ_TEST_DSN); + $this->conn->exec(PQ_TEST_TABLE_CREATE); + $this->conn->exec(PQ_TEST_REFTABLE_CREATE); + $this->conn->exec(PQ_TEST_DATA); + Table::$defaultConnection = $this->conn; + $this->table = new Table("test"); } protected function tearDown() { - $this->conn->exec(PQ_TEST_DROP_TABLE); + $this->conn->exec(PQ_TEST_REFTABLE_DROP); + $this->conn->exec(PQ_TEST_TABLE_DROP); } public function test__invoke() { diff --git a/tests/lib/pq/Gateway/TableTest.php b/tests/lib/pq/Gateway/TableTest.php index 65c93f7..a5fb56f 100644 --- a/tests/lib/pq/Gateway/TableTest.php +++ b/tests/lib/pq/Gateway/TableTest.php @@ -17,21 +17,19 @@ class TableTest extends \PHPUnit_Framework_TestCase { protected $table; protected function setUp() { - $this->conn = new \pq\Connection(PQ_DSN); - $this->conn->exec(PQ_TEST_DROP_TABLE); - $this->conn->exec(PQ_TEST_CREATE_TABLE); + $this->conn = new \pq\Connection(PQ_TEST_DSN); + $this->conn->exec(PQ_TEST_TABLE_CREATE); + $this->conn->exec(PQ_TEST_REFTABLE_CREATE); + $this->conn->exec(PQ_TEST_DATA); Table::$defaultConnection = $this->conn; - $this->table = new Table(PQ_TEST_TABLE_NAME); + $this->table = new Table("test"); } protected function tearDown() { - $this->conn->exec(PQ_TEST_DROP_TABLE); + $this->conn->exec(PQ_TEST_REFTABLE_DROP); + $this->conn->exec(PQ_TEST_TABLE_DROP); } - protected function createTestData() { - $this->conn->exec(PQ_TEST_CREATE_DATA); - } - public function testSetRowsetPrototype() { $prop = new \ReflectionProperty("\\pq\\Gateway\\Table", "rowset"); $prop->setAccessible(true); @@ -48,7 +46,7 @@ class TableTest extends \PHPUnit_Framework_TestCase { } public function testGetName() { - $this->assertSame(PQ_TEST_TABLE_NAME, $this->table->getName()); + $this->assertSame("test", $this->table->getName()); } public function testFind() { diff --git a/tests/setup.inc b/tests/setup.inc index 798d4a4..ee9733c 100644 --- a/tests/setup.inc +++ b/tests/setup.inc @@ -1,27 +1,43 @@