Skip to content

Commit

Permalink
Merge branch '5.7'
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamCampbell committed Dec 10, 2018
2 parents ca13d0e + adb72b4 commit 4cf2e4f
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 7 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG-5.7.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Release Notes for 5.7.x

## Unreleased

### Added
- Added `Database\Query\Builder::insertUsing` method ([#26732](https://github.com/laravel/framework/pull/26732), [8216b46](https://github.com/laravel/framework/commit/8216b4607152f9b01f26efba6b045add5382c625))
- Added `Database\Query\Builder::havingBetween` method ([#26758](https://github.com/laravel/framework/pull/26758))
- Added `Packets out of order. Expected` string to `DetectsLostConnections` trait ([#26760](https://github.com/laravel/framework/pull/26760))

### TODO
- https://github.com/laravel/framework/pull/26775
- https://github.com/laravel/framework/commit/0f9886d0cb17c9d756068e09743f871003cf1b45


## [v5.7.16 (2018-12-05)](https://github.com/laravel/framework/compare/v5.7.15...v5.7.16)

### Added
Expand Down
8 changes: 8 additions & 0 deletions src/Illuminate/Console/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,14 @@ protected function parseVerbosity($level = null)
return $level;
}

/**
* {@inheritdoc}
*/
public function isHidden()
{
return $this->hidden;
}

/**
* Get the console command arguments.
*
Expand Down
43 changes: 40 additions & 3 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1717,6 +1717,26 @@ public function orHaving($column, $operator = null, $value = null)
return $this->having($column, $operator, $value, 'or');
}

/**
* Add a "having between " clause to the query.
*
* @param string $column
* @param array $values
* @param string $boolean
* @param bool $not
* @return \Illuminate\Database\Query\Builder|static
*/
public function havingBetween($column, array $values, $boolean = 'and', $not = false)
{
$type = 'between';

$this->havings[] = compact('type', 'column', 'values', 'boolean', 'not');

$this->addBinding($this->cleanBindings($values), 'having');

return $this;
}

/**
* Add a raw having clause to the query.
*
Expand Down Expand Up @@ -2575,7 +2595,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 @@ -2588,6 +2608,23 @@ public function insertGetId(array $values, $sequence = null)
return $this->processor->processInsertGetId($this, $sql, $values, $sequence);
}

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

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

/**
* Update a record in the database.
*
Expand Down Expand Up @@ -2624,7 +2661,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 @@ -2645,7 +2682,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
34 changes: 34 additions & 0 deletions src/Illuminate/Database/Query/Grammars/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,8 @@ protected function compileHaving(array $having)
// clause into SQL based on the components that make it up from builder.
if ($having['type'] === 'Raw') {
return $having['boolean'].' '.$having['sql'];
} elseif ($having['type'] === 'between') {
return $this->compileHavingBetween($having);
}

return $this->compileBasicHaving($having);
Expand All @@ -668,6 +670,25 @@ protected function compileBasicHaving($having)
return $having['boolean'].' '.$column.' '.$having['operator'].' '.$parameter;
}

/**
* Compile a "between" having clause.
*
* @param array $having
* @return string
*/
protected function compileHavingBetween($having)
{
$between = $having['not'] ? 'not between' : 'between';

$column = $this->wrap($having['column']);

$min = $this->parameter(head($having['values']));

$max = $this->parameter(last($having['values']));

return $having['boolean'].' '.$column.' '.$between.' '.$min.' and '.$max;
}

/**
* Compile the "order by" portions of the query.
*
Expand Down Expand Up @@ -848,6 +869,19 @@ public function compileInsertGetId(Builder $query, $values, $sequence)
return $this->compileInsert($query, $values);
}

/**
* 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 compileInsertUsing(Builder $query, array $columns, string $sql)
{
return "insert into {$this->wrapTable($query->from)} ({$this->columnize($columns)}) $sql";
}

/**
* Compile an update statement into SQL.
*
Expand Down
4 changes: 4 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ public function compileForeign(Blueprint $blueprint, Fluent $command)
$sql .= $command->initiallyImmediate ? ' initially immediate' : ' initially deferred';
}

if (! is_null($command->notValid)) {
$sql .= ' not valid';
}

return $sql;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Foundation/Console/ModelMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ protected function createFactory()

$this->call('make:factory', [
'name' => "{$factory}Factory",
'--model' => $this->argument('name'),
'--model' => $this->qualifyClass($this->getNameInput()),
]);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Mail/Mailable.php
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ public function replyTo($address, $name = null)
}

/**
* Determine if the given recipient is set on the mailable.
* Determine if the given replyTo is set on the mailable.
*
* @param object|array|string $address
* @param string|null $name
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ function data_fill(&$target, $key, $value)
* Get an item from an array or object using "dot" notation.
*
* @param mixed $target
* @param string|array $key
* @param string|array|int $key
* @param mixed $default
* @return mixed
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/Database/DatabaseEloquentCastsDatabaseStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ protected function connection()
/**
* Get a schema builder instance.
*
* @return Schema\Builder
* @return \Illuminate\Database\Schema\Builder
*/
protected function schema()
{
Expand Down
7 changes: 7 additions & 0 deletions tests/Database/DatabasePostgresSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,13 @@ public function testCompileForeign()

$this->assertCount(1, $statements);
$this->assertEquals('alter table "users" add constraint "users_parent_id_foreign" foreign key ("parent_id") references "parents" ("id") on delete cascade deferrable initially deferred', $statements[0]);

$blueprint = new Blueprint('users');
$blueprint->foreign('parent_id')->references('id')->on('parents')->onDelete('cascade')->deferrable()->notValid();
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertEquals('alter table "users" add constraint "users_parent_id_foreign" foreign key ("parent_id") references "parents" ("id") on delete cascade deferrable not valid', $statements[0]);
}

public function testAddingGeometry()
Expand Down
31 changes: 31 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,10 @@ public function testHavings()
$builder = $this->getBuilder();
$builder->select(['category', new Raw('count(*) as "total"')])->from('item')->where('department', '=', 'popular')->groupBy('category')->having('total', '>', 3);
$this->assertEquals('select "category", count(*) as "total" from "item" where "department" = ? group by "category" having "total" > ?', $builder->toSql());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->havingBetween('last_login_date', ['2018-11-16', '2018-12-16']);
$this->assertEquals('select * from "users" having "last_login_date" between ? and ?', $builder->toSql());
}

public function testHavingShortcut()
Expand Down Expand Up @@ -1051,6 +1055,10 @@ public function testRawHavings()
$builder = $this->getBuilder();
$builder->select('*')->from('users')->having('baz', '=', 1)->orHavingRaw('user_foo < user_bar');
$this->assertEquals('select * from "users" having "baz" = ? or user_foo < user_bar', $builder->toSql());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->havingBetween('last_login_date', ['2018-11-16', '2018-12-16'])->orHavingRaw('user_foo < user_bar');
$this->assertEquals('select * from "users" having "last_login_date" between ? and ? or user_foo < user_bar', $builder->toSql());
}

public function testLimitsAndOffsets()
Expand Down Expand Up @@ -1760,6 +1768,29 @@ public function testInsertMethod()
$this->assertTrue($result);
}

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')->insertUsing(
['foo'],
function (Builder $query) {
$query->select(['bar'])->from('table2')->where('foreign_id', '=', 5);
}
);

$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

5 comments on commit 4cf2e4f

@driesvints
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@driesvints
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@staudenmeir this seems related to the recent union changes I believe? I can't figure out immediately what's going on.

@staudenmeir
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@driesvints The testSQLiteMultipleInserts() test has been removed by #25995, but the merge accidentally brought it back.

@driesvints
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@staudenmeir thanks! Just pushed a commit which removes it again.

@driesvints
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes but I think you need to rebase with master. You've currently applied Graham's commit on top of your commit.

Please sign in to comment.