forked from beberlei/DoctrineExtensions
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bdbcb5b
commit fc9253f
Showing
1 changed file
with
86 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
/** | ||
* DoctrineExtensions Mysql Function Pack | ||
* | ||
* LICENSE | ||
* | ||
* This source file is subject to the new BSD license that is bundled | ||
* with this package in the file LICENSE.txt. | ||
* If you did not receive a copy of the license and are unable to | ||
* obtain it through the world-wide-web, please send an email | ||
* to [email protected] so I can send you a copy immediately. | ||
*/ | ||
|
||
namespace DoctrineExtensions\Query\Mysql; | ||
|
||
use Doctrine\ORM\Query\AST\Functions\FunctionNode, | ||
Doctrine\ORM\Query\Lexer; | ||
|
||
/** | ||
* Usage: FIELD(str,str1,str2,str3,...) | ||
* | ||
* FIELD returns the index (position) of str in the str1, str2, str3, ... list. | ||
* Returns 0 if str is not found. If all arguments to FIELD() are strings, all | ||
* arguments are compared as strings. If all arguments are numbers, they are | ||
* compared as numbers. Otherwise, the arguments are compared as double. | ||
* If str is NULL, the return value is 0 because NULL fails equality comparison | ||
* with any value. FIELD() is the complement of ELT(). (Taken from MySQL | ||
* documentation.) | ||
* | ||
* @author Jeremy Hicks <[email protected]> | ||
* @version 2011.06.09 | ||
*/ | ||
namespace DoctrineExtensions\Query\Mysql; | ||
|
||
use Doctrine\ORM\Query\AST\Functions\FunctionNode; | ||
use Doctrine\ORM\Query\Lexer; | ||
|
||
class Field extends FunctionNode | ||
{ | ||
private $field = null; | ||
private $values = array(); | ||
|
||
public function parse(\Doctrine\ORM\Query\Parser $parser) | ||
{ | ||
$parser->match(Lexer::T_IDENTIFIER); | ||
$parser->match(Lexer::T_OPEN_PARENTHESIS); | ||
|
||
// Do the field. | ||
$this->field = $parser->ArithmeticPrimary(); | ||
|
||
// Add the strings to the values array. FIELD must | ||
// be used with at least 1 string not including the field. | ||
|
||
$lexer = $parser->getLexer(); | ||
|
||
while (count($this->values) < 1 || | ||
$lexer->lookahead['type'] != Lexer::T_CLOSE_PARENTHESIS) { | ||
$parser->match(Lexer::T_COMMA); | ||
$this->values[] = $parser->ArithmeticPrimary(); | ||
} | ||
|
||
$parser->match(Lexer::T_CLOSE_PARENTHESIS); | ||
} | ||
|
||
public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker) | ||
{ | ||
$query = 'FIELD('; | ||
|
||
$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; | ||
} | ||
} |