Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
test number of replacements on expression considers when same variabl…
Browse files Browse the repository at this point in the history
…e is used many times
  • Loading branch information
Jean Carlo Machado committed May 8, 2016
1 parent 465b3a0 commit d8d096c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Sql/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,13 @@ public function getExpressionData()

// assign locally, escaping % signs
$expression = str_replace(self::PLACEHOLDER, '%s', $expression, $count);
if ($count !== $parametersCount) {

//test number of replacements without considering same variable beign used many times first, which is
//faster, if the test fails then resort to regex wich are slow and used rarely
if ($count !== $parametersCount && $parametersCount == preg_match_all('/\:[a-zA-Z0-9_]*/', $expression)) {
throw new Exception\RuntimeException('The number of replacements in the expression does not match the number of parameters');
}

foreach ($parameters as $parameter) {
list($values[], $types[]) = $this->normalizeArgument($parameter, self::TYPE_VALUE);
}
Expand Down
6 changes: 6 additions & 0 deletions test/Sql/ExpressionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,10 @@ public function testGetExpressionPreservesPercentageSignInFromUnixtime()

$this->assertSame($expressionString, $expression->getExpression());
}

public function testNumberOfReplacemensConsidersWhenSameVariableIsUsedManyTimes()
{
$expression = new Expression('uf.user_id = :user_id OR uf.friend_id = :user_id', ['user_id' => 1]);
$expression->getExpressionData();
}
}

0 comments on commit d8d096c

Please sign in to comment.