Skip to content

Commit

Permalink
Limit expected bindingx v2.
Browse files Browse the repository at this point in the history
  • Loading branch information
KaneCohen committed Jan 21, 2021
1 parent d08fd80 commit 9d3752c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,17 @@ public function fromRaw($expression, $bindings = [])
return $this;
}

/**
* Returns scalar type value from an unknown type of input.
*
* @param mixed $value
* @return mixed
*/
protected function scalarValue($value)
{
return is_array($value) ? head(Arr::flatten($value)) : $value;
}

/**
* Creates a subquery and parse it.
*
Expand Down Expand Up @@ -698,7 +709,7 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
);

if (! $value instanceof Expression) {
$this->addBinding(is_array($value) ? head($value) : $value, 'where');
$this->addBinding($this->scalarValue($value), 'where');
}

return $this;
Expand Down Expand Up @@ -1043,7 +1054,7 @@ public function whereBetween($column, array $values, $boolean = 'and', $not = fa

$this->wheres[] = compact('type', 'column', 'values', 'boolean', 'not');

$this->addBinding(array_slice($this->cleanBindings($values), 0, 2), 'where');
$this->addBinding(array_slice($this->cleanBindings(Arr::flatten($values)), 0, 2), 'where');

return $this;
}
Expand Down Expand Up @@ -1111,7 +1122,7 @@ public function whereDate($column, $operator, $value = null, $boolean = 'and')
$value, $operator, func_num_args() === 2
);

$value = is_array($value) ? head($value) : $value;
$value = $this->scalarValue($value);

if ($value instanceof DateTimeInterface) {
$value = $value->format('Y-m-d');
Expand Down Expand Up @@ -1152,7 +1163,7 @@ public function whereTime($column, $operator, $value = null, $boolean = 'and')
$value, $operator, func_num_args() === 2
);

$value = is_array($value) ? head($value) : $value;
$value = $this->scalarValue($value);

if ($value instanceof DateTimeInterface) {
$value = $value->format('H:i:s');
Expand Down Expand Up @@ -1238,7 +1249,7 @@ public function whereMonth($column, $operator, $value = null, $boolean = 'and')
$value, $operator, func_num_args() === 2
);

$value = is_array($value) ? head($value) : $value;
$value = $this->scalarValue($value);

if ($value instanceof DateTimeInterface) {
$value = $value->format('m');
Expand Down Expand Up @@ -1593,7 +1604,7 @@ public function whereJsonLength($column, $operator, $value = null, $boolean = 'a
$this->wheres[] = compact('type', 'column', 'operator', 'value', 'boolean');

if (! $value instanceof Expression) {
$this->addBinding((int) $value);
$this->addBinding((int) $this->scalarValue($value));
}

return $this;
Expand Down Expand Up @@ -1742,7 +1753,7 @@ public function having($column, $operator = null, $value = null, $boolean = 'and
$this->havings[] = compact('type', 'column', 'operator', 'value', 'boolean');

if (! $value instanceof Expression) {
$this->addBinding(is_array($value) ? head($value) : $value, 'having');
$this->addBinding($this->scalarValue($value), 'having');
}

return $this;
Expand Down
5 changes: 5 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,11 @@ public function testWheresWithArrayValue()
$builder->select('*')->from('users')->where('id', '<>', [12, 30]);
$this->assertSame('select * from "users" where "id" <> ?', $builder->toSql());
$this->assertEquals([0 => 12], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('id', '=', [[12, 30]]);
$this->assertSame('select * from "users" where "id" = ?', $builder->toSql());
$this->assertEquals([0 => 12], $builder->getBindings());
}

public function testMySqlWrappingProtectsQuotationMarks()
Expand Down

0 comments on commit 9d3752c

Please sign in to comment.