From 32d5d8b53df50c80d4abbaca4c17c922157edad1 Mon Sep 17 00:00:00 2001 From: Michael Tsang Date: Tue, 4 Dec 2018 14:28:45 +0800 Subject: [PATCH 1/7] add Builder::insertSub() --- src/Illuminate/Database/Query/Builder.php | 10 ++++++++++ tests/Database/DatabaseQueryBuilderTest.php | 13 +++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index bd66993decbb..687799896908 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -2558,6 +2558,16 @@ 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 + */ + public function insertSub(array $columns, $query) + { + } + /** * Update a record in the database. * diff --git a/tests/Database/DatabaseQueryBuilderTest.php b/tests/Database/DatabaseQueryBuilderTest.php index 952eafd20d4f..3b9c492ef64a 100755 --- a/tests/Database/DatabaseQueryBuilderTest.php +++ b/tests/Database/DatabaseQueryBuilderTest.php @@ -1675,6 +1675,19 @@ public function testInsertMethod() $this->assertTrue($result); } + public function testInsertSubMethod() + { + $builder = $this->getBuilder(); + $builder->getConnection()->shouldReceive('insert')->once()->with('insert into "users" ("email") select "address" from "emails" where "created_at" > ?', ['2018-01-01'])->andReturn(true); + $result = $builder->from('users')->insertSub( + ['email'], + function (Builder $query) { + $query->from('emails')->select(['address'])->where('created_at', '>', '2018-01-01'); + } + ); + $this->assertTrue($result); + } + public function testSQLiteMultipleInserts() { $builder = $this->getSQLiteBuilder(); From d50b93d7d1e18fafe7b99c03206d5c4c989f74bb Mon Sep 17 00:00:00 2001 From: Michael Tsang Date: Tue, 4 Dec 2018 14:40:25 +0800 Subject: [PATCH 2/7] add implementation of Builder::insertSub() --- src/Illuminate/Database/Query/Builder.php | 6 ++++++ src/Illuminate/Database/Query/Grammars/Grammar.php | 9 +++++++++ tests/Database/DatabaseQueryBuilderTest.php | 8 ++++---- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index 687799896908..3c3f5a713b0d 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -2566,6 +2566,12 @@ public function insertGetId(array $values, $sequence = null) */ public function insertSub(array $columns, $query) { + [$sql, $bindings] = $this->createSub($query); + + return $this->connection->insert( + $this->grammar->compileInsertSub($this, $columns, $sql), + $this->cleanBindings($bindings) + ); } /** diff --git a/src/Illuminate/Database/Query/Grammars/Grammar.php b/src/Illuminate/Database/Query/Grammars/Grammar.php index 29b08be0619f..1ab6baeb65aa 100755 --- a/src/Illuminate/Database/Query/Grammars/Grammar.php +++ b/src/Illuminate/Database/Query/Grammars/Grammar.php @@ -791,6 +791,15 @@ public function compileInsertGetId(Builder $query, $values, $sequence) return $this->compileInsert($query, $values); } + 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. * diff --git a/tests/Database/DatabaseQueryBuilderTest.php b/tests/Database/DatabaseQueryBuilderTest.php index 3b9c492ef64a..f3638820e566 100755 --- a/tests/Database/DatabaseQueryBuilderTest.php +++ b/tests/Database/DatabaseQueryBuilderTest.php @@ -1678,11 +1678,11 @@ public function testInsertMethod() public function testInsertSubMethod() { $builder = $this->getBuilder(); - $builder->getConnection()->shouldReceive('insert')->once()->with('insert into "users" ("email") select "address" from "emails" where "created_at" > ?', ['2018-01-01'])->andReturn(true); - $result = $builder->from('users')->insertSub( - ['email'], + $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->from('emails')->select(['address'])->where('created_at', '>', '2018-01-01'); + $query->select(['bar'])->from('table2')->where('foreign_id', '=', 5); } ); $this->assertTrue($result); From da09db73f3c3b4ecf788870896d84c73cd574107 Mon Sep 17 00:00:00 2001 From: Michael Tsang Date: Tue, 4 Dec 2018 14:49:28 +0800 Subject: [PATCH 3/7] add return type into phpDoc --- src/Illuminate/Database/Query/Builder.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index 3c3f5a713b0d..87a801976dc7 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -2563,6 +2563,7 @@ public function insertGetId(array $values, $sequence = null) * * @param array $columns * @param \Closure|\Illuminate\Database\Query\Builder|string $query + * @return bool */ public function insertSub(array $columns, $query) { From 95834e2ee92e83812a4c35920571d4e4ffe9b636 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Tue, 4 Dec 2018 17:19:21 +0000 Subject: [PATCH 4/7] Fixed phpdoc --- src/Illuminate/Database/Query/Builder.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index 87a801976dc7..b94b1d44a6df 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -2545,7 +2545,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 */ @@ -2561,8 +2561,8 @@ public function insertGetId(array $values, $sequence = null) /** * Insert new records from a subquery. * - * @param array $columns - * @param \Closure|\Illuminate\Database\Query\Builder|string $query + * @param array $columns + * @param \Closure|\Illuminate\Database\Query\Builder|string $query * @return bool */ public function insertSub(array $columns, $query) @@ -2611,7 +2611,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 = []) @@ -2632,7 +2632,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 = []) From 19494d2bb5d9c140777708eb6447d34be83a862e Mon Sep 17 00:00:00 2001 From: Michael Tsang Date: Thu, 6 Dec 2018 09:16:53 +0800 Subject: [PATCH 5/7] add phpdoc --- src/Illuminate/Database/Query/Grammars/Grammar.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Illuminate/Database/Query/Grammars/Grammar.php b/src/Illuminate/Database/Query/Grammars/Grammar.php index 1ab6baeb65aa..de4d4cc0fcde 100755 --- a/src/Illuminate/Database/Query/Grammars/Grammar.php +++ b/src/Illuminate/Database/Query/Grammars/Grammar.php @@ -791,6 +791,14 @@ public function compileInsertGetId(Builder $query, $values, $sequence) return $this->compileInsert($query, $values); } + /** + * Compile an insert statement with subquery into SQL. + * + * @param Builder $query + * @param array $columns + * @param string $sql + * @return string + */ public function compileInsertSub(Builder $query, array $columns, string $sql) { $table = $this->wrapTable($query->from); From 1f820bfffb1adabb6de6ce5b733411e2746e8d76 Mon Sep 17 00:00:00 2001 From: Michael Tsang Date: Fri, 7 Dec 2018 09:08:49 +0800 Subject: [PATCH 6/7] modify phpdoc --- src/Illuminate/Database/Query/Grammars/Grammar.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Database/Query/Grammars/Grammar.php b/src/Illuminate/Database/Query/Grammars/Grammar.php index de4d4cc0fcde..064583dadf4d 100755 --- a/src/Illuminate/Database/Query/Grammars/Grammar.php +++ b/src/Illuminate/Database/Query/Grammars/Grammar.php @@ -794,9 +794,9 @@ public function compileInsertGetId(Builder $query, $values, $sequence) /** * Compile an insert statement with subquery into SQL. * - * @param Builder $query - * @param array $columns - * @param string $sql + * @param \Illuminate\Database\Query\Builder $query + * @param array $columns + * @param string $sql * @return string */ public function compileInsertSub(Builder $query, array $columns, string $sql) From 49dad6f80092711af5e72a04c0f480feadd26a91 Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Fri, 7 Dec 2018 11:20:24 +0100 Subject: [PATCH 7/7] Use two spaces for DocBlock params --- src/Illuminate/Database/Query/Grammars/Grammar.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Query/Grammars/Grammar.php b/src/Illuminate/Database/Query/Grammars/Grammar.php index 064583dadf4d..7a1076991ff7 100755 --- a/src/Illuminate/Database/Query/Grammars/Grammar.php +++ b/src/Illuminate/Database/Query/Grammars/Grammar.php @@ -795,7 +795,7 @@ public function compileInsertGetId(Builder $query, $values, $sequence) * Compile an insert statement with subquery into SQL. * * @param \Illuminate\Database\Query\Builder $query - * @param array $columns + * @param array $columns * @param string $sql * @return string */