Skip to content

Fixing const detection in columns #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/SQLParser/Node/ConstNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
class ConstNode implements NodeInterface
{
private $value;
private $isString = true;

public function getValue()
{
Expand All @@ -63,6 +64,22 @@ public function setValue($value)
$this->value = $value;
}

/**
* @return mixed
*/
public function getIsString()
{
return $this->isString;
}

/**
* @param mixed $isString
*/
public function setIsString($isString)
{
$this->isString = $isString;
}

/**
* Returns a Mouf instance descriptor describing this object.
*
Expand Down Expand Up @@ -92,6 +109,8 @@ public function toSql(array $parameters = array(), Connection $dbConnection = nu
{
if ($this->value === null) {
return 'NULL';
} elseif (!$this->isString) {
return $this->value;
} elseif ($dbConnection != null) {
return $dbConnection->quote($this->value);
} else {
Expand Down
20 changes: 19 additions & 1 deletion src/SQLParser/Node/NodeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,35 @@ public static function toObject(array $desc)
$expr = $desc['base_expr'];
if (strpos($expr, "'") === 0) {
$expr = substr($expr, 1);
} else {
$const->setIsString(false);
}
if (strrpos($expr, "'") === strlen($expr) - 1) {
$expr = substr($expr, 0, strlen($expr) - 1);
$expr = substr($expr, 0, -1);
}
$expr = stripslashes($expr);

$const->setValue($expr);

// If the constant has an alias, it is declared in the columns section.
// If this is the case, let's wrap it in an "expression"
if (isset($desc['alias'])) {
$expression = new Expression();
$expression->setBaseExpression($desc['base_expr']);
$expression->setSubTree($const);
$expression->setAlias($desc['alias']['name']);
$expression->setBrackets(false);

$const = $expression;

unset($desc['alias']);
}

// Debug:
unset($desc['base_expr']);
unset($desc['expr_type']);
unset($desc['sub_tree']);

if (!empty($desc)) {
throw new \InvalidArgumentException('Unexpected parameters in exception: '.var_export($desc, true));
}
Expand Down
16 changes: 15 additions & 1 deletion tests/Mouf/Database/MagicQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function testStandardSelect()

// Triggers a const node
$sql = 'SELECT id+1 FROM users';
$this->assertEquals("SELECT id + '1' FROM users", self::simplifySql($magicQuery->build($sql)));
$this->assertEquals("SELECT id + 1 FROM users", self::simplifySql($magicQuery->build($sql)));

// Tests parameters with a ! (to force NULL values)
// Bonus: the = transforms into a IS
Expand All @@ -104,6 +104,20 @@ public function testStandardSelect()

$sql = 'SELECT * FROM users WHERE status IN :statuses!';
$this->assertEquals('SELECT * FROM users WHERE FALSE', self::simplifySql($magicQuery->build($sql, ['statuses' => []])));

// Test strings with "
$sql = 'SELECT * FROM users WHERE status = \'"\'';
$this->assertEquals('SELECT * FROM users WHERE status = \'\\"\'', self::simplifySql($magicQuery->build($sql)));

$sql = 'SELECT 1+2 as toto FROM users';
$this->assertEquals('SELECT 1 + 2 AS toto FROM users', self::simplifySql($magicQuery->build($sql)));

$sql = 'SELECT -1 as toto FROM users';
$this->assertEquals('SELECT -1 AS toto FROM users', self::simplifySql($magicQuery->build($sql)));

$sql = 'SELECT \'hello\' as toto FROM users';
$this->assertEquals('SELECT \'hello\' AS toto FROM users', self::simplifySql($magicQuery->build($sql)));

}

/**
Expand Down