Skip to content

Commit 476269c

Browse files
authored
Merge pull request #37 from dsavina/fix/order-by-aggregate-function
Fix : order by aggregate function
2 parents bc74046 + f98592f commit 476269c

File tree

5 files changed

+40
-6
lines changed

5 files changed

+40
-6
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ before_script:
99
- composer install --no-interaction
1010

1111
script:
12-
- phpunit
12+
- vendor/bin/phpunit
1313

1414
after_script:
1515
- php vendor/bin/coveralls -v

phpunit.xml.dist

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
>
1414

1515
<php>
16-
<var name="db_url" value="mysql://root:@localhost/"/>
16+
<var name="db_host" value="localhost" />
17+
<var name="db_username" value="root" />
18+
<var name="db_password" value="" />
19+
<var name="db_driver" value="pdo_mysql"/>
1720
</php>
1821

1922
<testsuites>

src/SQLParser/Node/AggregateFunction.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,25 @@ public function setAlias($alias)
105105
$this->alias = $alias;
106106
}
107107

108+
private $direction;
109+
110+
public function getDirection()
111+
{
112+
return $this->direction;
113+
}
114+
115+
/**
116+
* Sets the direction.
117+
*
118+
* @Important
119+
*
120+
* @param string $direction
121+
*/
122+
public function setDirection($direction)
123+
{
124+
$this->direction = $direction;
125+
}
126+
108127
/**
109128
* Returns a Mouf instance descriptor describing this object.
110129
*
@@ -145,6 +164,9 @@ public function toSql(array $parameters = array(), Connection $dbConnection = nu
145164

146165
$sql .= ' AS '.$alias;
147166
}
167+
if ($this->direction) {
168+
$sql .= ' '.$this->direction;
169+
}
148170
} else {
149171
$sql = null;
150172
}

src/SQLParser/Node/NodeFactory.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,12 +254,17 @@ public static function toObject(array $desc)
254254
$expr->setAlias($desc['alias']);
255255
}
256256

257+
if (isset($desc['direction'])) {
258+
$expr->setDirection($desc['direction']);
259+
}
260+
257261
// Debug:
258262
unset($desc['base_expr']);
259263
unset($desc['expr_type']);
260264
unset($desc['sub_tree']);
261265
unset($desc['alias']);
262266
unset($desc['delim']);
267+
unset($desc['direction']);
263268
if (!empty($desc)) {
264269
error_log('MagicQuery - NodeFactory: Unexpected parameters in aggregate function: '.var_export($desc, true));
265270
}

tests/Mouf/Database/MagicQueryTest.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ public function testStandardSelect()
152152

153153
$sql = 'SELECT * FROM users WHERE id IN (1, 3)';
154154
$this->assertEquals("SELECT * FROM users WHERE id IN (1, 3)", self::simplifySql($magicQuery->build($sql)));
155+
156+
$sql = 'SELECT country.* FROM country JOIN users ON country.id = users.country_id GROUP BY country.id ORDER BY COUNT(users.id) DESC';
157+
$this->assertEquals('SELECT country.* FROM country JOIN users ON (country.id = users.country_id) GROUP BY country.id ORDER BY COUNT(users.id) DESC', self::simplifySql($magicQuery->build($sql)));
155158
}
156159

157160
/**
@@ -177,12 +180,13 @@ public function testInvalidSql() {
177180
public function testWithCache()
178181
{
179182
global $db_url;
180-
$config = new \Doctrine\DBAL\Configuration();
181-
// TODO: put this in conf variable
182183
$connectionParams = array(
183-
'url' => $db_url,
184+
'user' => $GLOBALS['db_username'],
185+
'password' => $GLOBALS['db_password'],
186+
'host' => $GLOBALS['db_host'],
187+
'driver' => $GLOBALS['db_driver'],
184188
);
185-
$conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
189+
$conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams);
186190

187191
$cache = new ArrayCache();
188192

0 commit comments

Comments
 (0)