1
0
mirror of synced 2024-12-13 22:56:04 +03:00
This commit is contained in:
zYne 2007-07-01 12:11:44 +00:00
parent cc7b1367fa
commit 20e6b007f7
2 changed files with 70 additions and 6 deletions

View File

@ -34,13 +34,64 @@ class Doctrine_Expression
{
protected $_expression;
public function __construct($expression)
protected $_conn;
public function __construct($expr, $conn = null)
{
$this->_expression = $expression;
$this->setExpression($expr);
if ($conn !== null) {
$this->_conn = $conn;
}
}
public function getConnection()
{
if ( ! isset($this->_conn)) {
return Doctrine_Manager::connection();
}
return $this->_conn;
}
public function setExpression($clause)
{
$this->_expression = $this->parseClause($clause);
}
public function parseExpression($expr)
{
$pos = strpos($expr, '(');
if ($pos === false) {
return $expr;
}
// get the name of the function
$name = substr($expr, 0, $pos);
$argStr = substr($expr, ($pos + 1), -1);
// parse args
foreach (Doctrine_Tokenizer::bracketExplode($argStr, ',') as $arg) {
$args[] = $this->parseClause($arg);
}
return call_user_func_array(array($this->getConnection()->expression, $name), $args);
}
public function parseClause($clause)
{
$e = Doctrine_Tokenizer::bracketExplode($clause, ' ');
foreach ($e as $k => $expr) {
$e[$k] = $this->parseExpression($expr);
}
return implode(' ', $e);
}
public function getSql()
{
return $this->_expression;
}
}

View File

@ -55,6 +55,19 @@ class Doctrine_Expression_TestCase extends Doctrine_UnitTestCase
$this->assertEqual($e->name, 'someone');
}
public function testExpressionParserSupportsNumericalClauses()
{
$e = new Doctrine_Expression('1 + 2');
$this->assertEqual($e->getSql(), '1 + 2');
}
public function testExpressionParserSupportsFunctionComposition()
{
$e = new Doctrine_Expression("SUBSTRING(CONCAT('some', 'one'), 0, 3)");
$this->assertEqual($e->getSql(), "SUBSTR(CONCAT('some', 'one'), 0, 3)");
}
}
class ExpressionTest extends Doctrine_Record
{