Skip to content

Commit

Permalink
Do not convert int parameters in array to string
Browse files Browse the repository at this point in the history
  • Loading branch information
simPod committed Jun 27, 2018
1 parent 579b4cf commit 9de99be
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 23 deletions.
66 changes: 45 additions & 21 deletions src/Query/Degeneration/Bindings.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@
namespace ClickHouseDB\Query\Degeneration;

use DateTimeInterface;
use function array_map;
use function implode;
use function is_array;
use function is_float;
use function is_int;
use function is_string;
use function sprintf;
use function str_ireplace;

class Bindings implements \ClickHouseDB\Query\Degeneration
{
/**
* @var array
*/
protected $bindings = [];

/**
* @param array $bindings
*/
Expand All @@ -23,7 +32,7 @@ public function bindParams(array $bindings)

/**
* @param string $column
* @param mixed $value
* @param mixed $value
*/
public function bindParam($column, $value)
{
Expand Down Expand Up @@ -73,11 +82,10 @@ private function escapeArray($values)
$escapedValues[] = $this->escapeArray($value);
}
}

return $escapedValues;
}


/**
* Compile Bindings
*
Expand All @@ -92,43 +100,59 @@ public function process($sql)

arsort($this->bindings);


foreach ($this->bindings as $key => $value) {
$valueSet = null;
$formattedParameter = null;


$valueSet = null;
$valueSetText = null;

if (null === $value || $value === false) {
$valueSetText = "";
if ($value === null || $value === false) {
$formattedParameter = '';
}

if (is_array($value)) {
$escapedValue = $this->escapeArray($value);
$valueSetText = "'" . implode("','", $escapedValue) . "'";
$valueSet = implode(", ", $escapedValue);
$escapedValues = $this->escapeArray($value);

$escapedValues = array_map(
function ($escapedValue) {
if (is_string($escapedValue)) {
return $this->formatStringParameter($escapedValue);
}

return $escapedValue;
},
$escapedValues
);

$formattedParameter = implode(',', $escapedValues);
$valueSet = implode(', ', $escapedValues);
}

if (is_numeric($value)) {
$valueSetText = $value;
$valueSet = $value;
if (is_float($value) || is_int($value)) {
$formattedParameter = $value;
$valueSet = $value;
}

if (is_string($value)) {
$valueSet = $value;
$valueSetText = "'" . $this->escapeString($value) . "'";
$valueSet = $value;
$formattedParameter = $this->formatStringParameter($this->escapeString($value));
}

if ($valueSetText !== null) {
$sql = str_ireplace(':' . $key, $valueSetText, $sql);
if ($formattedParameter !== null) {
$sql = str_ireplace(':' . $key, $formattedParameter, $sql);
}

if ($valueSet !== null) {
$sql = str_ireplace('{' . $key . '}', $valueSet, $sql);
}
}

return ($sql);
return $sql;
}

/**
* @return string
*/
private function formatStringParameter(string $value)
{
return sprintf("'%s'", $value);
}
}
4 changes: 2 additions & 2 deletions tests/BindingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public function escapeDataProvider()
],
[
'select * from test. WHERE id IN (:id)',
['id' => ['1', 2]],
"select * from test. WHERE id IN ('1','2')",
['id' => [1, 2]],
'select * from test. WHERE id IN (1,2)',
],
[
'select * from test. WHERE id IN (:id)',
Expand Down

0 comments on commit 9de99be

Please sign in to comment.