Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Feb 22, 2022
2 parents 5ba19a2 + b91b3b5 commit 1337287
Show file tree
Hide file tree
Showing 15 changed files with 515 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,7 @@ protected function addUpdatedAtColumn(array $values)

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

$values[$qualifiedColumn] = $values[$column];
$values[$qualifiedColumn] = Arr::get($values, $qualifiedColumn, $values[$column]);

unset($values[$column]);

Expand Down
29 changes: 25 additions & 4 deletions src/Illuminate/Database/Migrations/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,11 +387,11 @@ protected function runMigration($migration, $method)
$migration->getConnection()
);

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

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

$this->fireMigrationEvent(new MigrationEnded($migration, $method));
}
Expand Down Expand Up @@ -447,13 +447,34 @@ protected function getQueries($migration, $method)
$migration->getConnection()
);

return $db->pretend(function () use ($migration, $method) {
return $db->pretend(function () use ($db, $migration, $method) {
if (method_exists($migration, $method)) {
$migration->{$method}();
$this->runMethod($db, $migration, $method);
}
});
}

/**
* Run a migration method on the given connection.
*
* @param \Illuminate\Database\Connection $connection
* @param object $migration
* @param string $method
* @return void
*/
protected function runMethod($connection, $migration, $method)
{
$previousConnection = $this->resolver->getDefaultConnection();

try {
$this->resolver->setDefaultConnection($connection->getName());

$migration->{$method}();
} finally {
$this->resolver->setDefaultConnection($previousConnection);
}
}

/**
* Resolve a migration instance from a file.
*
Expand Down
29 changes: 29 additions & 0 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,15 @@ class Builder implements BuilderContract
'not similar to', 'not ilike', '~~*', '!~~*',
];

/**
* All of the available bitwise operators.
*
* @var string[]
*/
public $bitwiseOperators = [
'&', '|', '^', '<<', '>>', '&~',
];

/**
* Whether to use write pdo for the select.
*
Expand Down Expand Up @@ -758,6 +767,10 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
}
}

if ($this->isBitwiseOperator($operator)) {
$type = 'Bitwise';
}

// Now that we are working with just a simple query we can put the elements
// in our array and add the query binding to our array of bindings that
// will be bound to each SQL statements when it is finally executed.
Expand Down Expand Up @@ -841,6 +854,18 @@ protected function invalidOperator($operator)
! in_array(strtolower($operator), $this->grammar->getOperators(), true));
}

/**
* Determine if the operator is a bitwise operator.
*
* @param string $operator
* @return bool
*/
protected function isBitwiseOperator($operator)
{
return in_array(strtolower($operator), $this->bitwiseOperators, true) ||
in_array(strtolower($operator), $this->grammar->getBitwiseOperators(), true);
}

/**
* Add an "or where" clause to the query.
*
Expand Down Expand Up @@ -1927,6 +1952,10 @@ public function having($column, $operator = null, $value = null, $boolean = 'and
[$value, $operator] = [$operator, '='];
}

if ($this->isBitwiseOperator($operator)) {
$type = 'Bitwise';
}

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

if (! $value instanceof Expression) {
Expand Down
29 changes: 29 additions & 0 deletions src/Illuminate/Database/Query/Grammars/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ class Grammar extends BaseGrammar
*/
protected $operators = [];

/**
* The grammar specific bitwise operators.
*
* @var array
*/
protected $bitwiseOperators = [];

/**
* The components that make up a select clause.
*
Expand Down Expand Up @@ -255,6 +262,18 @@ protected function whereBasic(Builder $query, $where)
return $this->wrap($where['column']).' '.$operator.' '.$value;
}

/**
* Compile a bitwise operator where clause.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $where
* @return string
*/
protected function whereBitwise(Builder $query, $where)
{
return $this->whereBasic($query, $where);
}

/**
* Compile a "where in" clause.
*
Expand Down Expand Up @@ -1331,4 +1350,14 @@ public function getOperators()
{
return $this->operators;
}

/**
* Get the grammar specific bitwise operators.
*
* @return array
*/
public function getBitwiseOperators()
{
return $this->bitwiseOperators;
}
}
50 changes: 48 additions & 2 deletions src/Illuminate/Database/Query/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ class PostgresGrammar extends Grammar
];

/**
* The grammar specific bit operators.
* The grammar specific bitwise operators.
*
* @var array
*/
protected $bitOperators = [
protected $bitwiseOperators = [
'~', '&', '|', '#', '<<', '>>', '<<=', '>>=',
];

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

/**
* Compile a bitwise operator where clause.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $where
* @return string
*/
protected function whereBitwise(Builder $query, $where)
{
$value = $this->parameter($where['value']);

$operator = str_replace('?', '??', $where['operator']);

return '('.$this->wrap($where['column']).' '.$operator.' '.$value.')::bool';
}

/**
* Compile a "where date" clause.
*
Expand Down Expand Up @@ -214,6 +230,36 @@ protected function compileJsonLength($column, $operator, $value)
return 'jsonb_array_length(('.$column.')::jsonb) '.$operator.' '.$value;
}

/**
* Compile a single having clause.
*
* @param array $having
* @return string
*/
protected function compileHaving(array $having)
{
if ($having['type'] === 'Bitwise') {
return $this->compileHavingBitwise($having);
}

return parent::compileHaving($having);
}

/**
* Compile a having clause involving a bitwise operator.
*
* @param array $having
* @return string
*/
protected function compileHavingBitwise($having)
{
$column = $this->wrap($having['column']);

$parameter = $this->parameter($having['value']);

return '('.$column.' '.$having['operator'].' '.$parameter.')::bool';
}

/**
* Compile the lock into SQL.
*
Expand Down
46 changes: 46 additions & 0 deletions src/Illuminate/Database/Query/Grammars/SqlServerGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,22 @@ protected function compileFrom(Builder $query, $table)
return $from;
}

/**
* {@inheritdoc}
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $where
* @return string
*/
protected function whereBitwise(Builder $query, $where)
{
$value = $this->parameter($where['value']);

$operator = str_replace('?', '??', $where['operator']);

return '('.$this->wrap($where['column']).' '.$operator.' '.$value.') != 0';
}

/**
* Compile a "where date" clause.
*
Expand Down Expand Up @@ -164,6 +180,36 @@ protected function compileJsonLength($column, $operator, $value)
return '(select count(*) from openjson('.$field.$path.')) '.$operator.' '.$value;
}

/**
* Compile a single having clause.
*
* @param array $having
* @return string
*/
protected function compileHaving(array $having)
{
if ($having['type'] === 'Bitwise') {
return $this->compileHavingBitwise($having);
}

return parent::compileHaving($having);
}

/**
* Compile a having clause involving a bitwise operator.
*
* @param array $having
* @return string
*/
protected function compileHavingBitwise($having)
{
$column = $this->wrap($having['column']);

$parameter = $this->parameter($having['value']);

return '('.$column.' '.$having['operator'].' '.$parameter.') != 0';
}

/**
* Create a full ANSI offset clause for the query.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Application extends Container implements ApplicationContract, CachesConfig
*
* @var string
*/
const VERSION = '9.1.0';
const VERSION = '9.2.0';

/**
* The base path for the Laravel installation.
Expand Down
32 changes: 32 additions & 0 deletions tests/Database/DatabaseEloquentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1691,6 +1691,20 @@ public function testUpdateWithTimestampValue()
$this->assertEquals(1, $result);
}

public function testUpdateWithQualifiedTimestampValue()
{
$query = new BaseBuilder(m::mock(ConnectionInterface::class), new Grammar, m::mock(Processor::class));
$builder = new Builder($query);
$model = new EloquentBuilderTestStub;
$this->mockConnectionForModel($model, '');
$builder->setModel($model);
$builder->getConnection()->shouldReceive('update')->once()
->with('update "table" set "table"."foo" = ?, "table"."updated_at" = ?', ['bar', null])->andReturn(1);

$result = $builder->update(['table.foo' => 'bar', 'table.updated_at' => null]);
$this->assertEquals(1, $result);
}

public function testUpdateWithoutTimestamp()
{
$query = new BaseBuilder(m::mock(ConnectionInterface::class), new Grammar, m::mock(Processor::class));
Expand Down Expand Up @@ -1721,6 +1735,24 @@ public function testUpdateWithAlias()
$this->assertEquals(1, $result);
}

public function testUpdateWithAliasWithQualifiedTimestampValue()
{
Carbon::setTestNow($now = '2017-10-10 10:10:10');

$query = new BaseBuilder(m::mock(ConnectionInterface::class), new Grammar, m::mock(Processor::class));
$builder = new Builder($query);
$model = new EloquentBuilderTestStub;
$this->mockConnectionForModel($model, '');
$builder->setModel($model);
$builder->getConnection()->shouldReceive('update')->once()
->with('update "table" as "alias" set "foo" = ?, "alias"."updated_at" = ?', ['bar', null])->andReturn(1);

$result = $builder->from('table as alias')->update(['foo' => 'bar', 'alias.updated_at' => null]);
$this->assertEquals(1, $result);

Carbon::setTestNow(null);
}

public function testUpsert()
{
Carbon::setTestNow($now = '2017-10-10 10:10:10');
Expand Down
2 changes: 2 additions & 0 deletions tests/Database/DatabaseEloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2144,6 +2144,7 @@ protected function addMockConnection($model)
$model->setConnectionResolver($resolver = m::mock(ConnectionResolverInterface::class));
$resolver->shouldReceive('connection')->andReturn($connection = m::mock(Connection::class));
$connection->shouldReceive('getQueryGrammar')->andReturn($grammar = m::mock(Grammar::class));
$grammar->shouldReceive('getBitwiseOperators')->andReturn([]);
$connection->shouldReceive('getPostProcessor')->andReturn($processor = m::mock(Processor::class));
$connection->shouldReceive('query')->andReturnUsing(function () use ($connection, $grammar, $processor) {
return new BaseBuilder($connection, $grammar, $processor);
Expand Down Expand Up @@ -2431,6 +2432,7 @@ public function getConnection()
{
$mock = m::mock(Connection::class);
$mock->shouldReceive('getQueryGrammar')->andReturn($grammar = m::mock(Grammar::class));
$grammar->shouldReceive('getBitwiseOperators')->andReturn([]);
$mock->shouldReceive('getPostProcessor')->andReturn($processor = m::mock(Processor::class));
$mock->shouldReceive('getName')->andReturn('name');
$mock->shouldReceive('query')->andReturnUsing(function () use ($mock, $grammar, $processor) {
Expand Down
Loading

0 comments on commit 1337287

Please sign in to comment.