From 2cc86362cf06fff91551d7ed956e618b3e075e14 Mon Sep 17 00:00:00 2001 From: maxgalbu Date: Mon, 7 Apr 2014 00:24:35 +0200 Subject: [PATCH 1/2] phql: fix issue 2229 --- ext/mvc/model/query.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/ext/mvc/model/query.c b/ext/mvc/model/query.c index 9034b2449c1..0739c8e5286 100644 --- a/ext/mvc/model/query.c +++ b/ext/mvc/model/query.c @@ -893,7 +893,6 @@ PHP_METHOD(Phalcon_Mvc_Model_Query, _getExpression){ case PHQL_T_INTEGER: case PHQL_T_DOUBLE: - case PHQL_T_RAW_QUALIFIED: PHALCON_OBS_VAR(value); phalcon_array_fetch_string(&value, expr, SL("value"), PH_NOISY); @@ -901,7 +900,16 @@ PHP_METHOD(Phalcon_Mvc_Model_Query, _getExpression){ add_assoc_stringl_ex(return_value, ISS(type), SL("literal"), 1); phalcon_array_update_string(&return_value, ISL(value), value, PH_COPY); break; + + case PHQL_T_RAW_QUALIFIED: + PHALCON_OBS_VAR(value); + phalcon_array_fetch_string(&value, expr, SL("name"), PH_NOISY); + array_init_size(return_value, 2); + add_assoc_stringl_ex(return_value, ISS(type), SL("literal"), 1); + phalcon_array_update_string(&return_value, ISL(value), value, PH_COPY); + break; + case PHQL_T_TRUE: array_init_size(return_value, 2); add_assoc_stringl_ex(return_value, ISS(type), SL("literal"), 1); From 6c11b49ca36eda517dd0779f8a224a2837dc4919 Mon Sep 17 00:00:00 2001 From: maxgalbu Date: Mon, 7 Apr 2014 00:25:11 +0200 Subject: [PATCH 2/2] phql: added tests for CAST and CONVERT functions --- unit-tests/ModelsQueryExecuteTest.php | 30 +++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/unit-tests/ModelsQueryExecuteTest.php b/unit-tests/ModelsQueryExecuteTest.php index 026bf6141dc..4b09c7c5351 100644 --- a/unit-tests/ModelsQueryExecuteTest.php +++ b/unit-tests/ModelsQueryExecuteTest.php @@ -328,6 +328,32 @@ public function _testSelectExecute($di) $this->assertEquals(count($result), 1); $this->assertEquals($result[0]->number, 1); + //Cast function + //TODO: CHAR in postgresql is acually a single char, but I can't specify a length + //for the field type like CHAR(10) because phalcon phql doesn't support it + $cast_type = "CHAR"; + if ($di->get("db") instanceof Phalcon\Db\Adapter\Pdo\Postgresql) + $cast_type = "TEXT"; + $result = $manager->executeQuery("SELECT CAST(year AS $cast_type) test FROM Robots LIMIT 1"); + $this->assertInstanceOf('Phalcon\Mvc\Model\Resultset\Simple', $result); + $this->assertInstanceOf('Phalcon\Mvc\Model\Row', $result[0]); + $this->assertEquals(Robots::findFirst()->year, $result[0]->test); + + //Convert using... is supported only by mysql (and it's in the sql standards, bad postgresql/sqlite!) + if ($di->get("db") instanceof Phalcon\Db\Adapter\Pdo\Mysql) + { + $result = $manager->executeQuery("SELECT CONVERT(year USING utf8) test FROM Robots LIMIT 1"); + $this->assertInstanceOf('Phalcon\Mvc\Model\Resultset\Simple', $result); + $this->assertInstanceOf('Phalcon\Mvc\Model\Row', $result[0]); + $this->assertEquals(Robots::findFirst()->year, $result[0]->test); + } + + //Nested Cast + $result = $manager->executeQuery("SELECT CAST(CAST(year AS $cast_type) AS DECIMAL) test FROM Robots LIMIT 1"); + $this->assertInstanceOf('Phalcon\Mvc\Model\Resultset\Simple', $result); + $this->assertInstanceOf('Phalcon\Mvc\Model\Row', $result[0]); + $this->assertEquals(Robots::findFirst()->year, $result[0]->test); + $result = $manager->executeQuery('SELECT r.id, r.* FROM Robots r'); $this->assertInstanceOf('Phalcon\Mvc\Model\Resultset\Complex', $result); $this->assertNotEquals(gettype($result[0]->id), 'object'); @@ -371,7 +397,7 @@ public function _testSelectExecute($di) $this->assertEquals($result[1]->r->id, 1); $this->assertEquals($result[1]->p->id, 2); - /** Joins with namespaces */ + //Joins with namespaces $result = $manager->executeQuery('SELECT Some\Robots.*, Some\RobotsParts.* FROM Some\Robots JOIN Some\RobotsParts ON Some\Robots.id = Some\RobotsParts.robots_id ORDER BY Some\Robots.id, Some\RobotsParts.id'); $this->assertInstanceOf('Phalcon\Mvc\Model\Resultset\Complex', $result); $this->assertEquals(gettype($result[0]->{'some\Robots'}), 'object'); @@ -384,7 +410,7 @@ public function _testSelectExecute($di) $this->assertEquals($result[1]->{'some\Robots'}->id, 1); $this->assertEquals($result[1]->{'some\RobotsParts'}->id, 2); - /** Joins with namespaces and aliases */ + //Joins with namespaces and aliases $result = $manager->executeQuery('SELECT r.*, p.* FROM Some\Robots r JOIN Some\RobotsParts p ON r.id = p.robots_id ORDER BY r.id, p.id'); $this->assertInstanceOf('Phalcon\Mvc\Model\Resultset\Complex', $result); $this->assertEquals(gettype($result[0]->r), 'object');