Skip to content

Commit

Permalink
Use default INSERT INTO syntax for multiple records with SQLite
Browse files Browse the repository at this point in the history
This bumps the version of SQLite dependency to 3.7.11, released
March 2012.

Using a default syntax for the INSERT INTO clause fixes an issue
described in the bug #25262, but only when used with SQLite 3.8.8
(released January 2015) or newer.

Closes #25262
  • Loading branch information
dos1 committed Oct 7, 2018
1 parent 943bfb0 commit c3eee3c
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 51 deletions.
43 changes: 0 additions & 43 deletions src/Illuminate/Database/Query/Grammars/SQLiteGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,49 +158,6 @@ protected function compileJsonLength($column, $operator, $value)
return 'json_array_length('.$field.$path.') '.$operator.' '.$value;
}

/**
* Compile an insert statement into SQL.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $values
* @return string
*/
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);
}

/**
* Compile an update statement into SQL.
*
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

0 comments on commit c3eee3c

Please sign in to comment.