Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Dec 7, 2018
1 parent a5d7745 commit 8216b46
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2616,18 +2616,18 @@ public function insertGetId(array $values, $sequence = null)
}

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

return $this->connection->insert(
$this->grammar->compileInsertSub($this, $columns, $sql),
$this->grammar->compileInsertUsing($this, $columns, $sql),
$this->cleanBindings($bindings)
);
}
Expand Down
10 changes: 3 additions & 7 deletions src/Illuminate/Database/Query/Grammars/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -870,20 +870,16 @@ public function compileInsertGetId(Builder $query, $values, $sequence)
}

/**
* Compile an insert statement with subquery into SQL.
* Compile an insert statement using a 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)
public function compileInsertUsing(Builder $query, array $columns, string $sql)
{
$table = $this->wrapTable($query->from);

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

return "insert into $table ($columns_string) $sql";
return "insert into {$this->wrapTable($query->from)} ({$this->columnize($columns)}) $sql";
}

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

public function testInsertSubMethod()
public function testInsertUsingMethod()
{
$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(

$result = $builder->from('table1')->insertUsing(
['foo'],
function (Builder $query) {
$query->select(['bar'])->from('table2')->where('foreign_id', '=', 5);
}
);

$this->assertTrue($result);
}

Expand Down

0 comments on commit 8216b46

Please sign in to comment.