From 97d5ecdd3acc5c363d0f5e42c8f0d29f1342aa90 Mon Sep 17 00:00:00 2001 From: AlexeyDsov Date: Sun, 12 Feb 2012 11:10:45 +0400 Subject: [PATCH] added customisation of joiners for SQLFunction --- core/OSQL/SQLFunction.class.php | 43 +++++++++++++++++++++++++++++- test/main/CriteriaTest.class.php | 29 ++++++++++++++++++++ test/main/OsqlSelectTest.class.php | 12 ++++++++- 3 files changed, 82 insertions(+), 2 deletions(-) diff --git a/core/OSQL/SQLFunction.class.php b/core/OSQL/SQLFunction.class.php index d56ffc02e5..43fc8ee5cb 100644 --- a/core/OSQL/SQLFunction.class.php +++ b/core/OSQL/SQLFunction.class.php @@ -24,6 +24,7 @@ final class SQLFunction extends Castable implements MappableObject, Aliased private $aggregate = null; private $args = array(); + private $joiners = array(); /** * @return SQLFunction @@ -94,6 +95,25 @@ public function setAggregateDistinct() return $this; } + /** + * @return SQLFunction + **/ + public function setJoiner($joiner, $index = 0) + { + $this->joiners[$index] = $joiner; + + return $this; + } + + public function getJoiner($index) + { + if (array_key_exists($index, $this->joiners)) { + return $this->joiners[$index]; + } else { + return ', '; + } + } + /** * @return SQLFunction **/ @@ -111,10 +131,14 @@ public function toMapped(ProtoDAO $dao, JoinCapableQuery $query) } $sqlFunction = call_user_func_array(array('self', 'create'), $mapped); + /* @var $sqlFunction SQLFunction */ $sqlFunction->aggregate = $this->aggregate; $sqlFunction->castTo($this->cast); + foreach ($this->joiners as $index => $joiner) { + $sqlFunction->setJoiner($joiner, $index); + } return $sqlFunction; } @@ -151,7 +175,7 @@ public function toDialectString(Dialect $dialect) $out .= 'DISTINCT '; } - $out .= ($args == array() ? null : implode(', ', $args)).')'; + $out .= ($args == array() ? null : $this->joinArgs($args)).')'; $out = $this->cast @@ -163,5 +187,22 @@ public function toDialectString(Dialect $dialect) ? $out.' AS '.$dialect->quoteTable($this->alias) : $out; } + + private function joinArgs(array $args) { + if (count($args) == 0) { + return ''; + } elseif (count($this->joiners) == 0) { + return implode(', ', $args); + } else { + $joinedArgs = ''; + for ($i = 0; $i < count($args); $i++) { + $joinedArgs .= $args[$i]; + if ($i < count($args) - 1) { + $joinedArgs .= ' ' . $this->getJoiner($i) . ' '; + } + } + return $joinedArgs; + } + } } ?> \ No newline at end of file diff --git a/test/main/CriteriaTest.class.php b/test/main/CriteriaTest.class.php index 8c6c88ed5b..06133d7cce 100644 --- a/test/main/CriteriaTest.class.php +++ b/test/main/CriteriaTest.class.php @@ -214,6 +214,33 @@ public function testDialectStringObjects() ); } + public function testSqlFunction() + { + $criteria = + Criteria::create(TestCity::dao())-> + addProjection( + Projection::property( + SQLFunction::create( + 'count', + SQLFunction::create( + 'substring', + 'name', + DBValue::create('M....w'), + DBValue::create('#') + )-> + setJoiner('from')-> + setJoiner('for', 1) + ) + ->setAggregateDistinct() + ) + ); + + $this->assertEquals( + $criteria->toDialectString(PostgresDialect::me()), + 'SELECT count(DISTINCT substring("custom_table"."name" from \'M....w\' for \'#\')) FROM "custom_table"' + ); + } + public function testSleepWithEmptyDao() { $baseCriteria = @@ -258,6 +285,8 @@ public function testForgottenDao() $this->fail(); } catch (WrongStateException $e) {/*it's good*/} } + +// public function public static function orderDataProvider() { diff --git a/test/main/OsqlSelectTest.class.php b/test/main/OsqlSelectTest.class.php index f3267fc67f..4c21e4d280 100644 --- a/test/main/OsqlSelectTest.class.php +++ b/test/main/OsqlSelectTest.class.php @@ -16,7 +16,16 @@ public function testSelectGet() SQLFunction::create( 'count', DBField::create('field5', 'test_table') )-> + setAggregateDistinct()-> setAlias('alias5') + )-> + get( + SQLFunction::create( + 'substring', + DBField::create('field6', 'test_table'), + DBValue::create('a..b') + )-> + setJoiner('from', 0) ); $this->assertEquals( @@ -26,7 +35,8 @@ public function testSelectGet() .'"test_table"."field2", ' .'"test_table"."field3" AS "alias3", ' .'"test_table"."field4", ' - .'count("test_table"."field5") AS "alias5" ' + .'count(DISTINCT "test_table"."field5") AS "alias5", ' + .'substring("test_table"."field6" from \'a..b\') ' .'FROM "test_table"' ); }