Permalink
Browse files

Merge pull request #741 from jails/pr1

Close #558
  • Loading branch information...
2 parents 806ba12 + 1144187 commit 613361ff8e7834b7fe4619507103ce89553f0d3e @nateabele nateabele committed Dec 18, 2012
Showing with 37 additions and 5 deletions.
  1. +2 −2 data/source/Database.php
  2. +35 −3 tests/cases/data/source/DatabaseTest.php
View
@@ -718,8 +718,8 @@ public function _schema($query, $fields = null) {
}
$unalias = function ($value) {
- if (!is_string($value)) {
- return $value;
+ if (is_object($value) && isset($value->scalar)) {
+ $value = $value->scalar;
}
$aliasing = preg_split("/\s+as\s+/i", $value);
return isset($aliasing[1]) ? $aliasing[1] : $value;
@@ -1393,7 +1393,7 @@ public function testJoin() {
$this->assertEqual($expected, $joins);
}
- public function testExportedFieldsWithJoinedStrategy () {
+ public function testExportedFieldsWithJoinedStrategy() {
$query = new Query(array(
'model' => $this->_gallery,
'with' => array('Image.ImageTag.Tag')
@@ -1438,8 +1438,7 @@ public function testExportedFieldsWithJoinedStrategy () {
$this->assertEqual($expected, $result['fields']);
}
- public function testExportedFieldsWithJoinedStrategyAndRecursiveRelation () {
-
+ public function testExportedFieldsWithJoinedStrategyAndRecursiveRelation() {
$query = new Query(array(
'model' => $this->_gallery,
'with' => array('Parent.Parent')
@@ -1466,6 +1465,39 @@ public function testExportedFieldsWithJoinedStrategyAndRecursiveRelation () {
$expected = '{ParentOfParent}.{name}, {ParentOfParent}.{id}, {Gallery}.{id}, {Parent}.{id}';
$this->assertEqual($expected, $result['fields']);
}
+
+ public function testCustomField() {
+ $field = "(CASE `title` WHEN 'Lotus Flower' THEN 'Found' ELSE 'Not Found' END) as extra";
+ $query = new Query(array(
+ 'type' => 'read',
+ 'model' => $this->_gallery,
+ 'fields' => array('*', $field)
+ ));
+ $result = $this->db->read($query);
+ $expected = 'SELECT (CASE `title` WHEN \'Lotus Flower\' THEN \'Found\' ELSE \'Not Found\' ';
+ $expected .= 'END) as extra, {Gallery}.* FROM {mock_gallery} AS {Gallery};';
+ $this->assertEqual($expected, $this->db->sql);
+ $map = array('' => array('extra', 'id', 'title'));
+ $this->assertEqual($map, $query->map());
+
+ $query = new Query(array(
+ 'type' => 'read',
+ 'model' => $this->_gallery,
+ 'fields' => array('*', (object) $field)
+ ));
+ $result = $this->db->read($query);
+ $this->assertEqual($expected, $this->db->sql);
+ $this->assertEqual($map, $query->map());
+
+ $query = new Query(array(
+ 'type' => 'read',
+ 'model' => $this->_gallery,
+ 'fields' => array('*', array($field))
+ ));
+ $result = $this->db->read($query);
+ $this->assertEqual($expected, $this->db->sql);
+ $this->assertEqual($map, $query->map());
+ }
}
?>

0 comments on commit 613361f

Please sign in to comment.