-
Notifications
You must be signed in to change notification settings - Fork 429
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SQLite Greatest and Least functions
GREATEST(2,3) -> MAX(2,3) LEAST(2,3) -> MIN(2,3)
- Loading branch information
1 parent
469f653
commit 38bbac0
Showing
5 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace DoctrineExtensions\Query\Sqlite; | ||
|
||
use Doctrine\ORM\Query\AST\Functions\FunctionNode; | ||
use Doctrine\ORM\Query\Lexer; | ||
use Doctrine\ORM\Query\Parser; | ||
use Doctrine\ORM\Query\SqlWalker; | ||
|
||
/** | ||
* Class Greatest | ||
* @package DoctrineExtensions\Query\Mysql | ||
* | ||
* @author Vas N <phpvas@gmail.com> | ||
* @author Guven Atbakan <guven@atbakan.com> | ||
*/ | ||
class Greatest extends FunctionNode | ||
{ | ||
private $field = null; | ||
|
||
private $values = []; | ||
|
||
/** | ||
* @param Parser $parser | ||
*/ | ||
public function parse(Parser $parser) | ||
{ | ||
$parser->match(Lexer::T_IDENTIFIER); | ||
$parser->match(Lexer::T_OPEN_PARENTHESIS); | ||
$this->field = $parser->ArithmeticExpression(); | ||
$lexer = $parser->getLexer(); | ||
|
||
while (count($this->values) < 1 || | ||
$lexer->lookahead['type'] != Lexer::T_CLOSE_PARENTHESIS) { | ||
$parser->match(Lexer::T_COMMA); | ||
$this->values[] = $parser->ArithmeticExpression(); | ||
} | ||
|
||
$parser->match(Lexer::T_CLOSE_PARENTHESIS); | ||
} | ||
|
||
/** | ||
* @param SqlWalker $sqlWalker | ||
* @return string | ||
*/ | ||
public function getSql(SqlWalker $sqlWalker) | ||
{ | ||
$query = 'MAX('; | ||
|
||
$query .= $this->field->dispatch($sqlWalker); | ||
|
||
$query .= ', '; | ||
|
||
for ($i = 0; $i < count($this->values); $i++) { | ||
if ($i > 0) { | ||
$query .= ', '; | ||
} | ||
|
||
$query .= $this->values[$i]->dispatch($sqlWalker); | ||
} | ||
|
||
$query .= ')'; | ||
|
||
return $query; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace DoctrineExtensions\Query\Sqlite; | ||
|
||
use Doctrine\ORM\Query\AST\Functions\FunctionNode; | ||
use Doctrine\ORM\Query\Lexer; | ||
use Doctrine\ORM\Query\Parser; | ||
use Doctrine\ORM\Query\SqlWalker; | ||
|
||
/** | ||
* Class Least | ||
* @package DoctrineExtensions\Query\Mysql | ||
* | ||
* @author Vas N <phpvas@gmail.com> | ||
*/ | ||
class Least extends FunctionNode | ||
{ | ||
private $field = null; | ||
|
||
private $values = []; | ||
|
||
/** | ||
* @param Parser $parser | ||
*/ | ||
public function parse(Parser $parser) | ||
{ | ||
$parser->match(Lexer::T_IDENTIFIER); | ||
$parser->match(Lexer::T_OPEN_PARENTHESIS); | ||
$this->field = $parser->ArithmeticExpression(); | ||
$lexer = $parser->getLexer(); | ||
|
||
while (count($this->values) < 1 || | ||
$lexer->lookahead['type'] != Lexer::T_CLOSE_PARENTHESIS) { | ||
$parser->match(Lexer::T_COMMA); | ||
$this->values[] = $parser->ArithmeticExpression(); | ||
} | ||
|
||
$parser->match(Lexer::T_CLOSE_PARENTHESIS); | ||
} | ||
|
||
/** | ||
* @param SqlWalker $sqlWalker | ||
* @return string | ||
*/ | ||
public function getSql(SqlWalker $sqlWalker) | ||
{ | ||
$query = 'MIN('; | ||
|
||
$query .= $this->field->dispatch($sqlWalker); | ||
|
||
$query .= ', '; | ||
|
||
for ($i = 0; $i < count($this->values); $i++) { | ||
if ($i > 0) { | ||
$query .= ', '; | ||
} | ||
|
||
$query .= $this->values[$i]->dispatch($sqlWalker); | ||
} | ||
|
||
$query .= ')'; | ||
|
||
return $query; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace DoctrineExtensions\Tests\Query\Sqlite; | ||
|
||
use DoctrineExtensions\Tests\Query\SqliteTestCase; | ||
|
||
class GreatestTest extends SqliteTestCase | ||
{ | ||
public function testGreatest() | ||
{ | ||
$this->assertDqlProducesSql( | ||
"SELECT Greatest(2, 3) from DoctrineExtensions\Tests\Entities\Blank b", | ||
'SELECT MAX(2, 3) AS sclr_0 FROM Blank b0_' | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace DoctrineExtensions\Tests\Query\Sqlite; | ||
|
||
use DoctrineExtensions\Tests\Query\SqliteTestCase; | ||
|
||
class LeastTest extends SqliteTestCase | ||
{ | ||
public function testLeast() | ||
{ | ||
$this->assertDqlProducesSql( | ||
"SELECT LEAST(2, 3) from DoctrineExtensions\Tests\Entities\Blank b", | ||
'SELECT MIN(2, 3) AS sclr_0 FROM Blank b0_' | ||
); | ||
} | ||
} |