Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion core/OSQL/SQLFunction.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ final class SQLFunction extends Castable implements MappableObject, Aliased
private $aggregate = null;

private $args = array();
private $joiners = array();

/**
* @return SQLFunction
Expand Down Expand Up @@ -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
**/
Expand All @@ -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;
}
Expand Down Expand Up @@ -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
Expand All @@ -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;
}
}
}
?>
29 changes: 29 additions & 0 deletions test/main/CriteriaTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -258,6 +285,8 @@ public function testForgottenDao()
$this->fail();
} catch (WrongStateException $e) {/*it's good*/}
}

// public function

public static function orderDataProvider()
{
Expand Down
12 changes: 11 additions & 1 deletion test/main/OsqlSelectTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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"'
);
}
Expand Down