Skip to content
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
31 changes: 30 additions & 1 deletion src/SQLParser/Node/NodeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ public static function toObject(array $desc)
unset($desc['alias']);
unset($desc['direction']);
unset($desc['delim']);
unset($desc['no_quotes']);
if (!empty($desc)) {
error_log('MagicQuery - NodeFactory: Unexpected parameters in simple function: '.var_export($desc, true));
}
Expand Down Expand Up @@ -349,7 +350,6 @@ public static function toObject(array $desc)
case ExpressionType::RECORD:

case ExpressionType::MATCH_ARGUMENTS:
case ExpressionType::MATCH_MODE:

case ExpressionType::ALIAS:
case ExpressionType::POSITION:
Expand Down Expand Up @@ -392,6 +392,23 @@ public static function toObject(array $desc)
error_log('MagicQuery - NodeFactory: Unexpected parameters in exception: '.var_export($desc, true));
}

return $expr;
case ExpressionType::MATCH_MODE:
$expr = new ConstNode();
$expr->setValue($desc['base_expr']);
$expr->setIsString(false);

// Debug:
unset($desc['base_expr']);
unset($desc['expr_type']);
unset($desc['sub_tree']);
unset($desc['alias']);
unset($desc['direction']);
unset($desc['delim']);
if (!empty($desc)) {
error_log('MagicQuery - NodeFactory: Unexpected parameters in exception: '.var_export($desc, true));
}

return $expr;
default:
throw new \Exception('Unknown expression type');
Expand Down Expand Up @@ -545,6 +562,18 @@ public static function simplify($nodes)
}
$nodes = $newNodes;

// Handle AGAINST function. Without this patch params will be placed after AGAINST() and not inside the brackets
$newNodes = array();
for ($i = 0; $i < count($nodes); ++$i) {
$node = $nodes[$i];
if ($node instanceof SimpleFunction && $node->getBaseExpression() === 'AGAINST' && isset($nodes[$i + 1])) {
$node->setSubTree($nodes[$i + 1]);
$i++;
}
$newNodes[] = $node;
}
$nodes = $newNodes;

// Let's find the highest level operator.
for ($i = count($nodes) - 1; $i >= 0; --$i) {
$node = $nodes[$i];
Expand Down
16 changes: 16 additions & 0 deletions tests/Mouf/Database/MagicQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,22 @@ public function testMagicJoin3()
$this->assertEquals($expectedSql, self::simplifySql($magicQuery->build($sql)));
}

public function testMatchAgainst()
{
$magicQuery = new MagicQuery();

$sql = "
SELECT MATCH(column) AGAINST(:searchTerm IN BOOLEAN MODE) AS rang FROM table
WHERE MATCH(column) AGAINST(:searchTerm IN BOOLEAN MODE)
ORDER BY MATCH(column) AGAINST(:searchTerm IN BOOLEAN MODE) DESC
";
$expectedSql = "SELECT MATCH(column) AGAINST('searchString' IN BOOLEAN MODE) AS rang FROM table WHERE MATCH(column) AGAINST('searchString' IN BOOLEAN MODE) ORDER BY MATCH(column) AGAINST('searchString' IN BOOLEAN MODE) DESC";

$params["searchTerm"] = "searchString";

$this->assertEquals($expectedSql, self::simplifySql($magicQuery->build($sql, $params)));
}

/**
* @expectedException \Mouf\Database\MagicQueryMissingConnectionException
*/
Expand Down