Skip to content

Commit

Permalink
Merge branch 'insert_sub' of https://github.com/miklcct/framework int…
Browse files Browse the repository at this point in the history
…o miklcct-insert_sub
  • Loading branch information
taylorotwell committed Dec 7, 2018
2 parents 1e2a5e1 + 49dad6f commit a5d7745
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2602,7 +2602,7 @@ public function insert(array $values)
/**
* Insert a new record and get the value of the primary key.
*
* @param array $values
* @param array $values
* @param string|null $sequence
* @return int
*/
Expand All @@ -2615,6 +2615,23 @@ public function insertGetId(array $values, $sequence = null)
return $this->processor->processInsertGetId($this, $sql, $values, $sequence);
}

/**
* Insert new records from a subquery.
*
* @param array $columns
* @param \Closure|\Illuminate\Database\Query\Builder|string $query
* @return bool
*/
public function insertSub(array $columns, $query)
{
[$sql, $bindings] = $this->createSub($query);

return $this->connection->insert(
$this->grammar->compileInsertSub($this, $columns, $sql),
$this->cleanBindings($bindings)
);
}

/**
* Update a record in the database.
*
Expand Down Expand Up @@ -2651,7 +2668,7 @@ public function updateOrInsert(array $attributes, array $values = [])
*
* @param string $column
* @param float|int $amount
* @param array $extra
* @param array $extra
* @return int
*/
public function increment($column, $amount = 1, array $extra = [])
Expand All @@ -2672,7 +2689,7 @@ public function increment($column, $amount = 1, array $extra = [])
*
* @param string $column
* @param float|int $amount
* @param array $extra
* @param array $extra
* @return int
*/
public function decrement($column, $amount = 1, array $extra = [])
Expand Down
17 changes: 17 additions & 0 deletions src/Illuminate/Database/Query/Grammars/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,23 @@ public function compileInsertGetId(Builder $query, $values, $sequence)
return $this->compileInsert($query, $values);
}

/**
* Compile an insert statement with subquery into SQL.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $columns
* @param string $sql
* @return string
*/
public function compileInsertSub(Builder $query, array $columns, string $sql)
{
$table = $this->wrapTable($query->from);

$columns_string = $this->columnize($columns);

return "insert into $table ($columns_string) $sql";
}

/**
* Compile an update statement into SQL.
*
Expand Down
13 changes: 13 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1762,6 +1762,19 @@ public function testInsertMethod()
$this->assertTrue($result);
}

public function testInsertSubMethod()
{
$builder = $this->getBuilder();
$builder->getConnection()->shouldReceive('insert')->once()->with('insert into "table1" ("foo") select "bar" from "table2" where "foreign_id" = ?', [5])->andReturn(true);
$result = $builder->from('table1')->insertSub(
['foo'],
function (Builder $query) {
$query->select(['bar'])->from('table2')->where('foreign_id', '=', 5);
}
);
$this->assertTrue($result);
}

public function testSQLiteMultipleInserts()
{
$builder = $this->getSQLiteBuilder();
Expand Down

0 comments on commit a5d7745

Please sign in to comment.