Skip to content

Commit

Permalink
Fix: Handle mixed-type values in compileInsert (#53948)
Browse files Browse the repository at this point in the history
* Fix: Prevent incorrect handling of single-value inserts by checking if $values itself is a list using `array_is_list` instead of checking if the first element (`reset($values)`) is an array.

* Add tests for `compileInsert` with different value structures

* Refactor: Improve insert value handling for non-list arrays

* formatting

* formatting

* formatting

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
alipadron and taylorotwell authored Dec 18, 2024
1 parent 502b489 commit 7d0a3d3
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/Illuminate/Database/Query/Grammars/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -1168,8 +1168,11 @@ public function compileInsert(Builder $query, array $values)
return "insert into {$table} default values";
}

if (! is_array(reset($values))) {
$values = [$values];
if (! array_is_list($values)) {
$values = (new Collection(array_keys($values)))
->some(fn ($key) => ! is_numeric($key))
? [$values]
: array_values($values);
}

$columns = $this->columnize(array_keys(reset($values)));
Expand Down
77 changes: 77 additions & 0 deletions tests/Database/DatabaseQueryGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Illuminate\Tests\Database;

use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\Query\Builder;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Query\Grammars\Grammar;
use Illuminate\Database\Query\Processors\Processor;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
Expand Down Expand Up @@ -41,4 +43,79 @@ public function testWhereRawReturnsStringWhenStringPassed()

$this->assertSame('select * from "users"', $rawQuery);
}

public function testCompileInsertSingleValue()
{
$builder = $this->getBuilder();
$grammar = $builder->getGrammar();

$sql = $grammar->compileInsert($builder, ['name' => 'John Doe', 'email' => '[email protected]']);
$this->assertSame('insert into "users" ("name", "email") values (?, ?)', $sql);
}

public function testCompileInsertMultipleValues()
{
$builder = $this->getBuilder();
$grammar = $builder->getGrammar();
$values = [
['name' => 'John Doe', 'email' => '[email protected]'],
['name' => 'Alice Wong', 'email' => '[email protected]'],
];

$sql = $grammar->compileInsert($builder, $values);
$this->assertSame('insert into "users" ("name", "email") values (?, ?), (?, ?)', $sql);
}

public function testCompileInsertSingleValueWhereFirstKeyIsArray()
{
$builder = $this->getBuilder();
$grammar = $builder->getGrammar();
$value = [
'configuration' => [
'dark_mode' => false,
'language' => 'en',
],
'name' => 'John Doe',
'email' => '[email protected]',
];

$sql = $grammar->compileInsert($builder, $value);

$this->assertSame('insert into "users" ("configuration", "name", "email") values (?, ?, ?)', $sql);
}

public function testCompileInsertSingleValueWhereFirstKeyIsNotArray()
{
$builder = $this->getBuilder();
$grammar = $builder->getGrammar();

$value = [
'name' => 'John Doe',
'configuration' => [
'dark_mode' => false,
'language' => 'en',
],
'email' => '[email protected]',
];

$sql = $grammar->compileInsert($builder, $value);

$this->assertSame('insert into "users" ("name", "configuration", "email") values (?, ?, ?)', $sql);
}

protected function getConnection()
{
return m::mock(ConnectionInterface::class);
}

protected function getBuilder($tableName = 'users')
{
$grammar = new Grammar;
$processor = m::mock(Processor::class);

$builder = new Builder($this->getConnection(), $grammar, $processor);
$builder->from = $tableName;

return $builder;
}
}

0 comments on commit 7d0a3d3

Please sign in to comment.