Skip to content

Commit 1337287

Browse files
committed
formatting
2 parents 5ba19a2 + b91b3b5 commit 1337287

15 files changed

+515
-8
lines changed

src/Illuminate/Database/Eloquent/Builder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1011,7 +1011,7 @@ protected function addUpdatedAtColumn(array $values)
10111011

10121012
$qualifiedColumn = end($segments).'.'.$column;
10131013

1014-
$values[$qualifiedColumn] = $values[$column];
1014+
$values[$qualifiedColumn] = Arr::get($values, $qualifiedColumn, $values[$column]);
10151015

10161016
unset($values[$column]);
10171017

src/Illuminate/Database/Migrations/Migrator.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -387,11 +387,11 @@ protected function runMigration($migration, $method)
387387
$migration->getConnection()
388388
);
389389

390-
$callback = function () use ($migration, $method) {
390+
$callback = function () use ($connection, $migration, $method) {
391391
if (method_exists($migration, $method)) {
392392
$this->fireMigrationEvent(new MigrationStarted($migration, $method));
393393

394-
$migration->{$method}();
394+
$this->runMethod($connection, $migration, $method);
395395

396396
$this->fireMigrationEvent(new MigrationEnded($migration, $method));
397397
}
@@ -447,13 +447,34 @@ protected function getQueries($migration, $method)
447447
$migration->getConnection()
448448
);
449449

450-
return $db->pretend(function () use ($migration, $method) {
450+
return $db->pretend(function () use ($db, $migration, $method) {
451451
if (method_exists($migration, $method)) {
452-
$migration->{$method}();
452+
$this->runMethod($db, $migration, $method);
453453
}
454454
});
455455
}
456456

457+
/**
458+
* Run a migration method on the given connection.
459+
*
460+
* @param \Illuminate\Database\Connection $connection
461+
* @param object $migration
462+
* @param string $method
463+
* @return void
464+
*/
465+
protected function runMethod($connection, $migration, $method)
466+
{
467+
$previousConnection = $this->resolver->getDefaultConnection();
468+
469+
try {
470+
$this->resolver->setDefaultConnection($connection->getName());
471+
472+
$migration->{$method}();
473+
} finally {
474+
$this->resolver->setDefaultConnection($previousConnection);
475+
}
476+
}
477+
457478
/**
458479
* Resolve a migration instance from a file.
459480
*

src/Illuminate/Database/Query/Builder.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,15 @@ class Builder implements BuilderContract
205205
'not similar to', 'not ilike', '~~*', '!~~*',
206206
];
207207

208+
/**
209+
* All of the available bitwise operators.
210+
*
211+
* @var string[]
212+
*/
213+
public $bitwiseOperators = [
214+
'&', '|', '^', '<<', '>>', '&~',
215+
];
216+
208217
/**
209218
* Whether to use write pdo for the select.
210219
*
@@ -758,6 +767,10 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
758767
}
759768
}
760769

770+
if ($this->isBitwiseOperator($operator)) {
771+
$type = 'Bitwise';
772+
}
773+
761774
// Now that we are working with just a simple query we can put the elements
762775
// in our array and add the query binding to our array of bindings that
763776
// will be bound to each SQL statements when it is finally executed.
@@ -841,6 +854,18 @@ protected function invalidOperator($operator)
841854
! in_array(strtolower($operator), $this->grammar->getOperators(), true));
842855
}
843856

857+
/**
858+
* Determine if the operator is a bitwise operator.
859+
*
860+
* @param string $operator
861+
* @return bool
862+
*/
863+
protected function isBitwiseOperator($operator)
864+
{
865+
return in_array(strtolower($operator), $this->bitwiseOperators, true) ||
866+
in_array(strtolower($operator), $this->grammar->getBitwiseOperators(), true);
867+
}
868+
844869
/**
845870
* Add an "or where" clause to the query.
846871
*
@@ -1927,6 +1952,10 @@ public function having($column, $operator = null, $value = null, $boolean = 'and
19271952
[$value, $operator] = [$operator, '='];
19281953
}
19291954

1955+
if ($this->isBitwiseOperator($operator)) {
1956+
$type = 'Bitwise';
1957+
}
1958+
19301959
$this->havings[] = compact('type', 'column', 'operator', 'value', 'boolean');
19311960

19321961
if (! $value instanceof Expression) {

src/Illuminate/Database/Query/Grammars/Grammar.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ class Grammar extends BaseGrammar
1818
*/
1919
protected $operators = [];
2020

21+
/**
22+
* The grammar specific bitwise operators.
23+
*
24+
* @var array
25+
*/
26+
protected $bitwiseOperators = [];
27+
2128
/**
2229
* The components that make up a select clause.
2330
*
@@ -255,6 +262,18 @@ protected function whereBasic(Builder $query, $where)
255262
return $this->wrap($where['column']).' '.$operator.' '.$value;
256263
}
257264

265+
/**
266+
* Compile a bitwise operator where clause.
267+
*
268+
* @param \Illuminate\Database\Query\Builder $query
269+
* @param array $where
270+
* @return string
271+
*/
272+
protected function whereBitwise(Builder $query, $where)
273+
{
274+
return $this->whereBasic($query, $where);
275+
}
276+
258277
/**
259278
* Compile a "where in" clause.
260279
*
@@ -1331,4 +1350,14 @@ public function getOperators()
13311350
{
13321351
return $this->operators;
13331352
}
1353+
1354+
/**
1355+
* Get the grammar specific bitwise operators.
1356+
*
1357+
* @return array
1358+
*/
1359+
public function getBitwiseOperators()
1360+
{
1361+
return $this->bitwiseOperators;
1362+
}
13341363
}

src/Illuminate/Database/Query/Grammars/PostgresGrammar.php

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ class PostgresGrammar extends Grammar
2121
];
2222

2323
/**
24-
* The grammar specific bit operators.
24+
* The grammar specific bitwise operators.
2525
*
2626
* @var array
2727
*/
28-
protected $bitOperators = [
28+
protected $bitwiseOperators = [
2929
'~', '&', '|', '#', '<<', '>>', '<<=', '>>=',
3030
];
3131

@@ -50,6 +50,22 @@ protected function whereBasic(Builder $query, $where)
5050
return parent::whereBasic($query, $where);
5151
}
5252

53+
/**
54+
* Compile a bitwise operator where clause.
55+
*
56+
* @param \Illuminate\Database\Query\Builder $query
57+
* @param array $where
58+
* @return string
59+
*/
60+
protected function whereBitwise(Builder $query, $where)
61+
{
62+
$value = $this->parameter($where['value']);
63+
64+
$operator = str_replace('?', '??', $where['operator']);
65+
66+
return '('.$this->wrap($where['column']).' '.$operator.' '.$value.')::bool';
67+
}
68+
5369
/**
5470
* Compile a "where date" clause.
5571
*
@@ -214,6 +230,36 @@ protected function compileJsonLength($column, $operator, $value)
214230
return 'jsonb_array_length(('.$column.')::jsonb) '.$operator.' '.$value;
215231
}
216232

233+
/**
234+
* Compile a single having clause.
235+
*
236+
* @param array $having
237+
* @return string
238+
*/
239+
protected function compileHaving(array $having)
240+
{
241+
if ($having['type'] === 'Bitwise') {
242+
return $this->compileHavingBitwise($having);
243+
}
244+
245+
return parent::compileHaving($having);
246+
}
247+
248+
/**
249+
* Compile a having clause involving a bitwise operator.
250+
*
251+
* @param array $having
252+
* @return string
253+
*/
254+
protected function compileHavingBitwise($having)
255+
{
256+
$column = $this->wrap($having['column']);
257+
258+
$parameter = $this->parameter($having['value']);
259+
260+
return '('.$column.' '.$having['operator'].' '.$parameter.')::bool';
261+
}
262+
217263
/**
218264
* Compile the lock into SQL.
219265
*

src/Illuminate/Database/Query/Grammars/SqlServerGrammar.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,22 @@ protected function compileFrom(Builder $query, $table)
9696
return $from;
9797
}
9898

99+
/**
100+
* {@inheritdoc}
101+
*
102+
* @param \Illuminate\Database\Query\Builder $query
103+
* @param array $where
104+
* @return string
105+
*/
106+
protected function whereBitwise(Builder $query, $where)
107+
{
108+
$value = $this->parameter($where['value']);
109+
110+
$operator = str_replace('?', '??', $where['operator']);
111+
112+
return '('.$this->wrap($where['column']).' '.$operator.' '.$value.') != 0';
113+
}
114+
99115
/**
100116
* Compile a "where date" clause.
101117
*
@@ -164,6 +180,36 @@ protected function compileJsonLength($column, $operator, $value)
164180
return '(select count(*) from openjson('.$field.$path.')) '.$operator.' '.$value;
165181
}
166182

183+
/**
184+
* Compile a single having clause.
185+
*
186+
* @param array $having
187+
* @return string
188+
*/
189+
protected function compileHaving(array $having)
190+
{
191+
if ($having['type'] === 'Bitwise') {
192+
return $this->compileHavingBitwise($having);
193+
}
194+
195+
return parent::compileHaving($having);
196+
}
197+
198+
/**
199+
* Compile a having clause involving a bitwise operator.
200+
*
201+
* @param array $having
202+
* @return string
203+
*/
204+
protected function compileHavingBitwise($having)
205+
{
206+
$column = $this->wrap($having['column']);
207+
208+
$parameter = $this->parameter($having['value']);
209+
210+
return '('.$column.' '.$having['operator'].' '.$parameter.') != 0';
211+
}
212+
167213
/**
168214
* Create a full ANSI offset clause for the query.
169215
*

src/Illuminate/Foundation/Application.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class Application extends Container implements ApplicationContract, CachesConfig
3535
*
3636
* @var string
3737
*/
38-
const VERSION = '9.1.0';
38+
const VERSION = '9.2.0';
3939

4040
/**
4141
* The base path for the Laravel installation.

tests/Database/DatabaseEloquentBuilderTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1691,6 +1691,20 @@ public function testUpdateWithTimestampValue()
16911691
$this->assertEquals(1, $result);
16921692
}
16931693

1694+
public function testUpdateWithQualifiedTimestampValue()
1695+
{
1696+
$query = new BaseBuilder(m::mock(ConnectionInterface::class), new Grammar, m::mock(Processor::class));
1697+
$builder = new Builder($query);
1698+
$model = new EloquentBuilderTestStub;
1699+
$this->mockConnectionForModel($model, '');
1700+
$builder->setModel($model);
1701+
$builder->getConnection()->shouldReceive('update')->once()
1702+
->with('update "table" set "table"."foo" = ?, "table"."updated_at" = ?', ['bar', null])->andReturn(1);
1703+
1704+
$result = $builder->update(['table.foo' => 'bar', 'table.updated_at' => null]);
1705+
$this->assertEquals(1, $result);
1706+
}
1707+
16941708
public function testUpdateWithoutTimestamp()
16951709
{
16961710
$query = new BaseBuilder(m::mock(ConnectionInterface::class), new Grammar, m::mock(Processor::class));
@@ -1721,6 +1735,24 @@ public function testUpdateWithAlias()
17211735
$this->assertEquals(1, $result);
17221736
}
17231737

1738+
public function testUpdateWithAliasWithQualifiedTimestampValue()
1739+
{
1740+
Carbon::setTestNow($now = '2017-10-10 10:10:10');
1741+
1742+
$query = new BaseBuilder(m::mock(ConnectionInterface::class), new Grammar, m::mock(Processor::class));
1743+
$builder = new Builder($query);
1744+
$model = new EloquentBuilderTestStub;
1745+
$this->mockConnectionForModel($model, '');
1746+
$builder->setModel($model);
1747+
$builder->getConnection()->shouldReceive('update')->once()
1748+
->with('update "table" as "alias" set "foo" = ?, "alias"."updated_at" = ?', ['bar', null])->andReturn(1);
1749+
1750+
$result = $builder->from('table as alias')->update(['foo' => 'bar', 'alias.updated_at' => null]);
1751+
$this->assertEquals(1, $result);
1752+
1753+
Carbon::setTestNow(null);
1754+
}
1755+
17241756
public function testUpsert()
17251757
{
17261758
Carbon::setTestNow($now = '2017-10-10 10:10:10');

tests/Database/DatabaseEloquentModelTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2144,6 +2144,7 @@ protected function addMockConnection($model)
21442144
$model->setConnectionResolver($resolver = m::mock(ConnectionResolverInterface::class));
21452145
$resolver->shouldReceive('connection')->andReturn($connection = m::mock(Connection::class));
21462146
$connection->shouldReceive('getQueryGrammar')->andReturn($grammar = m::mock(Grammar::class));
2147+
$grammar->shouldReceive('getBitwiseOperators')->andReturn([]);
21472148
$connection->shouldReceive('getPostProcessor')->andReturn($processor = m::mock(Processor::class));
21482149
$connection->shouldReceive('query')->andReturnUsing(function () use ($connection, $grammar, $processor) {
21492150
return new BaseBuilder($connection, $grammar, $processor);
@@ -2431,6 +2432,7 @@ public function getConnection()
24312432
{
24322433
$mock = m::mock(Connection::class);
24332434
$mock->shouldReceive('getQueryGrammar')->andReturn($grammar = m::mock(Grammar::class));
2435+
$grammar->shouldReceive('getBitwiseOperators')->andReturn([]);
24342436
$mock->shouldReceive('getPostProcessor')->andReturn($processor = m::mock(Processor::class));
24352437
$mock->shouldReceive('getName')->andReturn('name');
24362438
$mock->shouldReceive('query')->andReturnUsing(function () use ($mock, $grammar, $processor) {

0 commit comments

Comments
 (0)