Skip to content

Commit

Permalink
Merge pull request beberlei#30 from andrewmackrodt/master
Browse files Browse the repository at this point in the history
Implemented MySQL CONCAT_WS and IFNULL functions
  • Loading branch information
beberlei committed Jun 5, 2011
2 parents 0d2adb2 + eb1d1f5 commit bdbcb5b
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
81 changes: 81 additions & 0 deletions lib/DoctrineExtensions/Query/Mysql/ConcatWs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?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: CONCAT_WS(SEPARATOR, STR1, STR2 ,...)
*
* CONCAT_WS() stands for Concatenate With Separator and is a special form of
* CONCAT(). The first argument is the separator for the rest of the arguments.
* The separator is added between the strings to be concatenated. The separator
* can be a string, as can the rest of the arguments. If the separator is NULL,
* the result is NULL.
*
* @author Andrew Mackrodt <[email protected]>
* @version 2011.06.05
*/
class ConcatWs extends FunctionNode
{
private $values = array();

public function parse(\Doctrine\ORM\Query\Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);

// Add the concat separator to the values array.
$this->values[] = $parser->StringPrimary();

// Add the rest of the strings to the values array. CONCAT_WS must
// be used with at least 2 strings not including the separator.

$lexer = $parser->getLexer();

while (count($this->values) < 3
|| $lexer->lookahead['type'] != Lexer::T_CLOSE_PARENTHESIS)
{
$parser->match(Lexer::T_COMMA);
$this->values[] = $parser->StringPrimary();
}

$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}

public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
{
// Create an array to hold the query elements.
$queryBuilder = array('CONCAT_WS(');

// Iterate over the captured expressions and add them to the query.
for ($i = 0; $i < count($this->values); $i++)
{
if ($i > 0)
{
$queryBuilder[] = ', ';
}

$queryBuilder[] = $sqlWalker->walkStringPrimary($this->values[$i]);
}

// Close the query.
$queryBuilder[] = ')';

// Return the joined query.
return implode('', $queryBuilder);
}
}
51 changes: 51 additions & 0 deletions lib/DoctrineExtensions/Query/Mysql/IfNull.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?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: IFNULL(expr1, expr2)
*
* If expr1 is not NULL, IFNULL() returns expr1; otherwise it returns expr2.
* IFNULL() returns a numeric or string value, depending on the context in
* which it is used.
*
* @author Andrew Mackrodt <[email protected]>
* @version 2011.06.05
*/
class IfNull extends FunctionNode
{
private $columnToCheck;
private $valueIfNull;

public function parse(\Doctrine\ORM\Query\Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$this->columnToCheck = $parser->StringPrimary();
$parser->match(Lexer::T_COMMA);
$this->valueIfNull = $parser->StringPrimary();
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}

public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
{
return 'IFNULL('
.$sqlWalker->walkStringPrimary($this->columnToCheck). ', '
.$sqlWalker->walkStringPrimary($this->valueIfNull).')';
}
}

0 comments on commit bdbcb5b

Please sign in to comment.