Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.8] Use default INSERT INTO syntax for multiple records with SQLite #25995

Merged
merged 1 commit into from
Oct 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 4 additions & 35 deletions src/Illuminate/Database/Query/Grammars/SQLiteGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,46 +159,15 @@ protected function compileJsonLength($column, $operator, $value)
}

/**
* Compile an insert statement into SQL.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $values
* @return string
* {@inheritdoc}
*/
public function compileInsert(Builder $query, array $values)
{
// Essentially we will force every insert to be treated as a batch insert which
// simply makes creating the SQL easier for us since we can utilize the same
// basic routine regardless of an amount of records given to us to insert.
$table = $this->wrapTable($query->from);

if (! is_array(reset($values))) {
$values = [$values];
}

// If there is only one record being inserted, we will just use the usual query
// grammar insert builder because no special syntax is needed for the single
// row inserts in SQLite. However, if there are multiples, we'll continue.
if (count($values) === 1) {
return empty(reset($values))
? "insert into $table default values"
: parent::compileInsert($query, reset($values));
}

$names = $this->columnize(array_keys(reset($values)));

$columns = [];

// SQLite requires us to build the multi-row insert as a listing of select with
// unions joining them together. So we'll build out this list of columns and
// then join them all together with select unions to complete the queries.
foreach (array_keys(reset($values)) as $column) {
$columns[] = '? as '.$this->wrap($column);
}

$columns = array_fill(0, count($values), implode(', ', $columns));

return "insert into $table ($names) select ".implode(' union all select ', $columns);
return empty($values)
? "insert into {$table} DEFAULT VALUES"
: parent::compileInsert($query, $values);
}

/**
Expand Down
8 changes: 0 additions & 8 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1675,14 +1675,6 @@ public function testInsertMethod()
$this->assertTrue($result);
}

public function testSQLiteMultipleInserts()
{
$builder = $this->getSQLiteBuilder();
$builder->getConnection()->shouldReceive('insert')->once()->with('insert into "users" ("email", "name") select ? as "email", ? as "name" union all select ? as "email", ? as "name"', ['foo', 'taylor', 'bar', 'dayle'])->andReturn(true);
$result = $builder->from('users')->insert([['email' => 'foo', 'name' => 'taylor'], ['email' => 'bar', 'name' => 'dayle']]);
$this->assertTrue($result);
}

public function testInsertGetIdMethod()
{
$builder = $this->getBuilder();
Expand Down