Skip to content

Commit

Permalink
Merge pull request #213 from envms/fix/literal-in-where
Browse files Browse the repository at this point in the history
Fix instances of FluentLiteral not taken literally in where
  • Loading branch information
cbornhoft committed Oct 12, 2017
2 parents ad319cd + 33c9f3f commit b172d58
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
6 changes: 4 additions & 2 deletions FluentPDO/BaseQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,13 @@ protected function buildParameters() {
if (is_array($value) && is_string(key($value)) && substr(key($value), 0, 1) == ':') {
// this is named params e.g. (':name' => 'Mark')
$parameters = array_merge($parameters, $value);
} else {
}
else {
$parameters[] = $value;
}
}
} else {
}
else {
if ($clauses) {
$parameters[] = $clauses;
}
Expand Down
10 changes: 9 additions & 1 deletion FluentPDO/CommonQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,15 @@ public function where($condition, $parameters = array()) {
return $this->addStatement('WHERE', "$condition IN $in");
}

$condition = "$condition = ?";
// don't parameterize the value if it's an instance of FluentLiteral
if ($parameters instanceof FluentLiteral) {
$condition = "{$condition} = {$parameters}";

return $this->addStatement('WHERE', $condition);
}
else {
$condition = "$condition = ?";
}
}

array_shift($args);
Expand Down
5 changes: 3 additions & 2 deletions FluentPDO/FluentUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ public static function toUpperWords($string) {
*/
public static function formatQuery($query) {
$query = preg_replace(
'/\b(WHERE|FROM|GROUP BY|HAVING|ORDER BY|LIMIT|OFFSET|UNION|ON DUPLICATE KEY UPDATE|VALUES|SET)/',
'/\b(WHERE|FROM|GROUP BY|HAVING|ORDER BY|LIMIT|OFFSET|UNION|ON DUPLICATE KEY UPDATE|VALUES|SET)\b/',
"\n$0", $query
);
$query = preg_replace(
'/\b(INNER|OUTER|LEFT|RIGHT|FULL|CASE|WHEN|END|ELSE|AND)/',
'/\b(INNER|OUTER|LEFT|RIGHT|FULL|CASE|WHEN|END|ELSE|AND)\b/',
"\n $0", $query
);
Expand Down

0 comments on commit b172d58

Please sign in to comment.