Skip to content

Commit def185f

Browse files
committed
Merge pull request #17 from dmaechler/fix_simple_functions
adding limit paramerizable and get it functional, then fixing bug on order by case with params
2 parents 6ab7975 + 911d23e commit def185f

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

src/SQLParser/Query/Select.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -372,27 +372,29 @@ public function toSql(array $parameters = array(), Connection $dbConnection = nu
372372
throw new \Exception('There is no offset if no limit is provided. An error may have occurred during SQLParsing.');
373373
} else if (!empty($this->limit)) {
374374
$limit = NodeFactory::toSql($this->limit, $dbConnection, $parameters, ',', false, $indent + 2, $conditionsMode);
375-
if ($limit === '') {
375+
if ($limit === '' || substr(trim($limit), 0, 1) == ':') {
376376
$limit = null;
377377
}
378378
if (!empty($this->offset)) {
379379
$offset = NodeFactory::toSql($this->offset, $dbConnection, $parameters, ',', false, $indent + 2, $conditionsMode);
380-
if ($offset === '') {
380+
if ($offset === '' || substr(trim($offset), 0, 1) == ':') {
381381
$offset = null;
382382
}
383383
} else {
384384
$offset = null;
385385
}
386386

387-
388387
if ($limit === null && $offset !== null) {
389388
throw new \Exception('There is no offset if no limit is provided. An error may have occurred during SQLParsing.');
390389
}
391-
$sql .= "\nLIMIT ";
392-
if ($offset !== null) {
393-
$sql .= $offset.', ';
390+
391+
if($limit !== null) {
392+
$sql .= "\nLIMIT ";
393+
if ($offset !== null) {
394+
$sql .= $offset.', ';
395+
}
396+
$sql .= $limit;
394397
}
395-
$sql .= $limit;
396398
}
397399

398400
return $sql;

tests/Mouf/Database/MagicQueryTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,28 @@ public function testStandardSelect()
1212
{
1313
$magicQuery = new MagicQuery();
1414

15+
$sql = "SELECT id FROM users WHERE name LIKE :name LIMIT :offset, :limit";
16+
$this->assertEquals("SELECT id FROM users WHERE name LIKE 'foo'", self::simplifySql($magicQuery->build($sql, ['name' => 'foo'])));
17+
18+
$sql = "SELECT id FROM users WHERE name LIKE :name LIMIT 2, :limit";
19+
$this->assertEquals("SELECT id FROM users WHERE name LIKE 'foo' LIMIT 2, 10", self::simplifySql($magicQuery->build($sql, ['name' => 'foo', 'limit' => 10])));
20+
21+
try {
22+
$exceptionOccurred = false;
23+
$sql = "SELECT id FROM users WHERE name LIKE :name LIMIT 2, :limit";
24+
self::simplifySql($magicQuery->build($sql, ['name' => 'foo']));
25+
} catch(\Exception $e) {
26+
// We have no limit provided in the parameters so we test that the script return an exception for this case
27+
$exceptionOccurred = true;
28+
}
29+
$this->assertEquals(true, $exceptionOccurred);
30+
31+
$sql = "SELECT id FROM users WHERE name LIKE :name LIMIT :offset, 5";
32+
$this->assertEquals("SELECT id FROM users WHERE name LIKE 'foo' LIMIT 0, 5", self::simplifySql($magicQuery->build($sql, ['name' => 'foo', 'offset' => 0])));
33+
34+
$sql = "SELECT id FROM users WHERE name LIKE :name LIMIT :offset, 5";
35+
$this->assertEquals("SELECT id FROM users WHERE name LIKE 'foo' LIMIT 5", self::simplifySql($magicQuery->build($sql, ['name' => 'foo'])));
36+
1537
$sql = "SELECT id FROM users LIMIT 10";
1638
$this->assertEquals("SELECT id FROM users LIMIT 10", self::simplifySql($magicQuery->build($sql)));
1739

0 commit comments

Comments
 (0)