From 2b19f42e532c293e06e38924926545eb6d973f36 Mon Sep 17 00:00:00 2001 From: Jesper Skovgaard Nielsen Date: Thu, 31 Mar 2016 10:03:41 +0200 Subject: [PATCH] We should be able to fetch rows into classes --- .../Yesql/Exception/ClassNotFound.php | 7 +++++ src/Nulpunkt/Yesql/Statement/Select.php | 23 +++++++++++++-- test/Nulpunkt/Yesql/RepositoryTest.php | 29 +++++++++++++++++-- test/Nulpunkt/Yesql/test.sql | 5 +++- test/Nulpunkt/Yesql/unknown_rowclass.sql | 2 ++ test/Nulpunkt/Yesql/unknown_rowfunc.sql | 2 ++ .../{unknown.sql => unknown_statement.sql} | 0 7 files changed, 61 insertions(+), 7 deletions(-) create mode 100644 src/Nulpunkt/Yesql/Exception/ClassNotFound.php create mode 100644 test/Nulpunkt/Yesql/unknown_rowclass.sql create mode 100644 test/Nulpunkt/Yesql/unknown_rowfunc.sql rename test/Nulpunkt/Yesql/{unknown.sql => unknown_statement.sql} (100%) diff --git a/src/Nulpunkt/Yesql/Exception/ClassNotFound.php b/src/Nulpunkt/Yesql/Exception/ClassNotFound.php new file mode 100644 index 0000000..e824a36 --- /dev/null +++ b/src/Nulpunkt/Yesql/Exception/ClassNotFound.php @@ -0,0 +1,7 @@ +sql = $sql; $this->modline = $modline; $this->rowFunc = $this->getRowFunc(); + $this->rowClass = $this->getRowClass(); } public function execute($db, $args) @@ -28,11 +29,15 @@ public function execute($db, $args) $this->stmt->execute(); } - if ($this->oneOrMany() == 'one') { - return $this->prepareElement($this->stmt->fetch(\PDO::FETCH_ASSOC)); + if ($this->rowClass) { + $this->stmt->setFetchMode(\PDO::FETCH_CLASS, $this->rowClass); } else { - return array_map([$this, 'prepareElement'], $this->stmt->fetchAll(\PDO::FETCH_ASSOC)); + $this->stmt->setFetchMode(\PDO::FETCH_ASSOC); } + + $res = array_map([$this, 'prepareElement'], $this->stmt->fetchAll()); + + return $this->oneOrMany() == 'one' ? $res[0] : $res; } private function oneOrMany() @@ -41,6 +46,18 @@ private function oneOrMany() return isset($m[1]) ? $m[1] : "many"; } + private function getRowClass() + { + preg_match('/rowClass:\s*(\S+)/', $this->modline, $m); + $c = @$m[1]; + + if ($c && !class_exists($c)) { + throw new \Nulpunkt\Yesql\Exception\ClassNotFound("{$c} is not a class"); + } + + return $c; + } + private function getRowFunc() { preg_match('/rowFunc:\s*(\S+)/', $this->modline, $m); diff --git a/test/Nulpunkt/Yesql/RepositoryTest.php b/test/Nulpunkt/Yesql/RepositoryTest.php index 04ff565..dce2737 100644 --- a/test/Nulpunkt/Yesql/RepositoryTest.php +++ b/test/Nulpunkt/Yesql/RepositoryTest.php @@ -9,9 +9,14 @@ public function testWeCanGetOneRow() $this->assertEquals(['id' => 1, 'something' => 'a thing'], $this->repo->getById(1)); } - public function testWeCanGetOneRowIntoAnObject() + public function testWeCanGetOneRowIntoAnObjectManually() { - $this->assertInstanceOf('TestHelper\TestObject', $this->repo->getObjectById(1)); + $this->assertInstanceOf('TestHelper\TestObject', $this->repo->getObjectByIdManually(1)); + } + + public function testWeCanGetOneRowIntoAnObjectAutomagically() + { + $this->assertInstanceOf('TestHelper\TestObject', $this->repo->getObjectByIdAutomagically(1)); } public function testWeCanMapParamsInSelect() @@ -119,7 +124,25 @@ public function testWeComplainAboutUndefinedMethods() */ public function testWeComplainAboutSqlWeDontKnowWhatToDoAbout() { - $r = new Repository($this->getDatabase(), __DIR__ . "/unknown.sql"); + $r = new Repository($this->getDatabase(), __DIR__ . "/unknown_statement.sql"); + $r->describeSomething(); + } + + /** + * @expectedException Nulpunkt\Yesql\Exception\MethodMissing + */ + public function testWeComplainAboutNonExsistingRowFunc() + { + $r = new Repository($this->getDatabase(), __DIR__ . "/unknown_rowfunc.sql"); + $r->describeSomething(); + } + + /** + * @expectedException Nulpunkt\Yesql\Exception\ClassNotFound + */ + public function testWeComplainAboutNonExsistingRowClass() + { + $r = new Repository($this->getDatabase(), __DIR__ . "/unknown_rowclass.sql"); $r->describeSomething(); } diff --git a/test/Nulpunkt/Yesql/test.sql b/test/Nulpunkt/Yesql/test.sql index 636011d..c2ad626 100644 --- a/test/Nulpunkt/Yesql/test.sql +++ b/test/Nulpunkt/Yesql/test.sql @@ -2,7 +2,10 @@ -- This will make getById a method which fetch a test row from the database select * from test_table where id = ? --- name: getObjectById oneOrMany: one rowFunc: TestHelper\TestObject::fromRow +-- name: getObjectByIdManually oneOrMany: one rowFunc: TestHelper\TestObject::fromRow +select * from test_table where id = ? + +-- name: getObjectByIdAutomagically oneOrMany: one rowClass: TestHelper\TestObject select * from test_table where id = ? -- name: getByIdMapped oneOrMany: one inFunc: TestHelper\TestObject::mappedParams diff --git a/test/Nulpunkt/Yesql/unknown_rowclass.sql b/test/Nulpunkt/Yesql/unknown_rowclass.sql new file mode 100644 index 0000000..7264d93 --- /dev/null +++ b/test/Nulpunkt/Yesql/unknown_rowclass.sql @@ -0,0 +1,2 @@ +-- name: getObjectById rowClass: TestHelper\YeahNo +select * from test_table where id = ? diff --git a/test/Nulpunkt/Yesql/unknown_rowfunc.sql b/test/Nulpunkt/Yesql/unknown_rowfunc.sql new file mode 100644 index 0000000..6dcc13e --- /dev/null +++ b/test/Nulpunkt/Yesql/unknown_rowfunc.sql @@ -0,0 +1,2 @@ +-- name: getObjectById rowFunc: TestHelper\TestObject::lolNope +select * from test_table where id = ? diff --git a/test/Nulpunkt/Yesql/unknown.sql b/test/Nulpunkt/Yesql/unknown_statement.sql similarity index 100% rename from test/Nulpunkt/Yesql/unknown.sql rename to test/Nulpunkt/Yesql/unknown_statement.sql