Skip to content

Commit

Permalink
Merge pull request #2307 from maxgalbu/issue2229
Browse files Browse the repository at this point in the history
fix issue #2229 (cast and convert not working)
  • Loading branch information
Phalcon committed Apr 6, 2014
2 parents 9d53551 + 6c11b49 commit d995389
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
10 changes: 9 additions & 1 deletion ext/mvc/model/query.c
Original file line number Diff line number Diff line change
Expand Up @@ -893,15 +893,23 @@ 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);

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_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);
Expand Down
30 changes: 28 additions & 2 deletions unit-tests/ModelsQueryExecuteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand All @@ -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');
Expand Down

0 comments on commit d995389

Please sign in to comment.