From 5dc2c033f8e5c727c772dfbcb70d739ae9bcd54c Mon Sep 17 00:00:00 2001 From: Veselov Pavel Date: Sun, 7 Jul 2019 15:07:08 +0300 Subject: [PATCH 01/16] draft unique fluent: base --- src/.meta.php | 2 + src/Compilers/UniqueWhereCompiler.php | 19 ++++ src/Schema/Blueprint.php | 63 +++++++++- src/Schema/Definitions/ColumnDefinition.php | 13 +++ src/Schema/Definitions/UniqueDefinition.php | 66 +++++++++++ src/Schema/Definitions/WhereDefinition.php | 120 ++++++++++++++++++++ src/Schema/Grammars/PostgresGrammar.php | 16 ++- 7 files changed, 294 insertions(+), 5 deletions(-) create mode 100644 src/Compilers/UniqueWhereCompiler.php create mode 100644 src/Schema/Definitions/ColumnDefinition.php create mode 100644 src/Schema/Definitions/UniqueDefinition.php create mode 100644 src/Schema/Definitions/WhereDefinition.php diff --git a/src/.meta.php b/src/.meta.php index b309c78..43daa5d 100644 --- a/src/.meta.php +++ b/src/.meta.php @@ -5,12 +5,14 @@ use Illuminate\Support\Fluent; use Umbrellio\Postgres\Schema\Definitions\AttachPartitionDefinition; use Umbrellio\Postgres\Schema\Definitions\LikeDefinition; + use Umbrellio\Postgres\Schema\Definitions\ColumnDefinition; /** * @method AttachPartitionDefinition attachPartition(string $partition) * @method void detachPartition(string $partition) * @method LikeDefinition like(string $table) * @method Fluent ifNotExists() + * @method UniqueDefinition uniquePartial(string|array $columns, ?string $index = null, ?string $algorithm = null) */ class Blueprint { diff --git a/src/Compilers/UniqueWhereCompiler.php b/src/Compilers/UniqueWhereCompiler.php new file mode 100644 index 0000000..cbf23c8 --- /dev/null +++ b/src/Compilers/UniqueWhereCompiler.php @@ -0,0 +1,19 @@ +addCommand('attachPartition', compact('partition')); + return $this->addExtendedCommand( + AttachPartitionDefinition::class, + 'attachPartition', + compact('partition') + ); } public function detachPartition(string $partition): void @@ -25,15 +31,64 @@ public function detachPartition(string $partition): void } /** - * @return LikeDefinition|Fluent + * @return LikeDefinition */ public function like(string $table) { - return $this->addCommand('like', compact('table')); + return $this->addExtendedCommand(LikeDefinition::class, 'like', compact('table')); } public function ifNotExists(): Fluent { return $this->addCommand('ifNotExists'); } + + /** + * @param string|array $columns + * @param string|null $index + * @param string|null $algorithm + * @return UniqueDefinition + */ + public function uniquePartial($columns, $index = null, $algorithm = null) + { + $columns = (array) $columns; + + // If no name was specified for this index, we will create one using a basic + // convention of the table name, followed by the columns, followed by an + // index type, such as primary or index, which makes the index unique. + $index = $index ?: $this->createIndexName('unique', $columns); + + return $this->addExtendedCommand( + UniqueDefinition::class, + 'uniquePartial', + compact('columns', 'index', 'algorithm') + ); + } + + /** + * Add a new extented command to the blueprint. + * + * @param string $fluent + * @param string $name + * @param array $parameters + * @return \Illuminate\Support\Fluent + */ + protected function addExtendedCommand($fluent, $name, array $parameters = []) + { + $this->commands[] = $command = $this->createExtendedCommand($fluent, $name, $parameters); + return $command; + } + + /** + * Create a new Extended Fluent command. + * + * @param string $fluent + * @param string $name + * @param array $parameters + * @return \Illuminate\Support\Fluent + */ + protected function createExtendedCommand($fluent, $name, array $parameters = []) + { + return new $fluent(array_merge(compact('name'), $parameters)); + } } diff --git a/src/Schema/Definitions/ColumnDefinition.php b/src/Schema/Definitions/ColumnDefinition.php new file mode 100644 index 0000000..decd04e --- /dev/null +++ b/src/Schema/Definitions/ColumnDefinition.php @@ -0,0 +1,13 @@ +createCommand($boolean . 'WhereRaw', compact('sql', 'bindings')); + } + + public function where(string $column, $operator = null, $value = null, string $boolean = 'and'): WhereDefinition + { + return $this->createCommand($boolean . 'Where', compact('column', 'operator', 'value')); + } + + public function whereColumn(string $first, $operator = null, $second = null, string $boolean = 'and'): WhereDefinition + { + return $this->createCommand($boolean . 'WhereColumn', compact('first', 'operator', 'second')); + } + + public function whereIn(string $column, array $values, string $boolean = 'and', bool $not = false): WhereDefinition + { + $command = $boolean . 'Where' . ($not ? 'Not' : '') . 'In'; + return $this->createCommand($command, compact('column', 'values')); + } + + public function whereNotIn(string $column, array $values, string $boolean = 'and'): WhereDefinition + { + return $this->createCommand($boolean . 'WhereNotIn', compact('column', 'values')); + } + + public function whereNull(string $column, string $boolean = 'and', bool $not = false): WhereDefinition + { + $command = $boolean . 'Where' . ($not ? 'Not' : '') . 'Null'; + return $this->createCommand($command, compact('column')); + } + + public function whereBetween(string $column, array $values, string $boolean = 'and', bool $not = false): WhereDefinition + { + $command = $boolean . 'Where' . ($not ? 'Not' : '') . 'Between'; + return $this->createCommand($command, compact('column', 'values')); + } + + public function whereNotBetween(string $column, array $values, string $boolean = 'and'): WhereDefinition + { + return $this->createCommand($boolean . 'WhereNotBetween', compact('column', 'values')); + } + + public function whereNotNull(string $column, string $boolean = 'and'): WhereDefinition + { + return $this->createCommand($boolean . 'WhereNotNull', compact('column')); + } + + protected function createCommand($name, $parameters): WhereDefinition + { + $command = new WhereDefinition(); + $this->attributes['wheres'] = call_user_func_array([$command, $name], $parameters); + return $command; + } +} diff --git a/src/Schema/Definitions/WhereDefinition.php b/src/Schema/Definitions/WhereDefinition.php new file mode 100644 index 0000000..8062c53 --- /dev/null +++ b/src/Schema/Definitions/WhereDefinition.php @@ -0,0 +1,120 @@ +attributes['andWhereRaw'][] = compact('sql', 'bindings'); + return $this; + } + + public function orWhereRaw(string $sql, array $bindings = []) + { + $this->attributes['orWhereRaw'][] = compact('sql', 'bindings'); + return $this; + } + + public function andWhere(string $column, $operator = null, $value = null) + { + $this->attributes['andWhere'][] = compact('column', 'operator', 'value'); + return $this; + } + + public function orWhere(string $column, $operator = null, $value = null) + { + $this->attributes['orWhere'][] = compact('column', 'operator', 'value'); + return $this; + } + + public function andWhereColumn(string $first, $operator = null, $second = null) + { + $this->attributes['andWhereColumn'][] = compact('first', 'operator', 'second'); + return $this; + } + + public function orWhereColumn(string $first, $operator = null, $second = null) + { + $this->attributes['orWhereColumn'][] = compact('first', 'operator', 'second'); + return $this; + } + + public function andWhereIn(string $column, array $values) + { + $this->attributes['andWhereIn'][] = compact('column', 'values'); + return $this; + } + + public function orWhereIn(string $column, array $values) + { + $this->attributes['orWhereIn'][] = compact('column', 'values'); + return $this; + } + + public function andWhereNotIn(string $column, array $values) + { + $this->attributes['andWhereNotIn'][] = compact('column', 'values'); + return $this; + } + + public function orWhereNotIn(string $column, array $values) + { + $this->attributes['orWhereNotIn'][] = compact('column', 'values'); + return $this; + } + + public function andWhereNull(string $column) + { + $this->attributes['andWhereNull'][] = compact('column'); + return $this; + } + + public function orWhereNull(string $column) + { + $this->attributes['orWhereNull'][] = compact('column'); + return $this; + } + + public function andWhereBetween(string $column, array $values) + { + $this->attributes['andWhereBetween'][] = compact('column', 'values'); + return $this; + } + + public function orWhereBetween(string $column, array $values) + { + $this->attributes['orWhereBetween'][] = compact('column', 'values'); + return $this; + } + + public function andWhereNotBetween(string $column, array $values) + { + $this->attributes['andWhereNotBetween'][] = compact('column', 'values'); + return $this; + } + + public function orWhereNotBetween(string $column, array $values) + { + $this->attributes['orWhereNotBetween'][] = compact('column', 'values'); + return $this; + } + + public function andWhereNotNull(string $column) + { + $this->attributes['andWhereNotNull'][] = compact('column'); + return $this; + } + + public function orWhereNotNull(string $column) + { + $this->attributes['orWhereNotNull'][] = compact('column'); + return $this; + } +} diff --git a/src/Schema/Grammars/PostgresGrammar.php b/src/Schema/Grammars/PostgresGrammar.php index e283371..27508c0 100644 --- a/src/Schema/Grammars/PostgresGrammar.php +++ b/src/Schema/Grammars/PostgresGrammar.php @@ -6,9 +6,10 @@ use Illuminate\Database\Schema\Grammars\PostgresGrammar as BasePostgresGrammar; use Illuminate\Support\Fluent; -use Umbrellio\Postgres\Compilers\AttachPartitionCompiler; +use Umbrellio\Postgres\Compilers\UniqueWhereCompiler; use Umbrellio\Postgres\Compilers\CreateCompiler; use Umbrellio\Postgres\Schema\Blueprint; +use Umbrellio\Postgres\Schema\Definitions\UniqueDefinition; class PostgresGrammar extends BasePostgresGrammar { @@ -52,4 +53,17 @@ public function compileDetachPartition($blueprint, Fluent $command): string $command->get('partition') ); } + + /** + * @param Blueprint|\Illuminate\Database\Schema\Blueprint $blueprint + * @param UniqueDefinition $command + * @return string + */ + public function compileUniquePartial($blueprint, Fluent $command): string + { + if ($wheres = $command->get('wheres')) { + return UniqueWhereCompiler::compile($this, $blueprint, $command); + } + return $this->compileUnique($blueprint, $command); + } } From a6d95205c74ee5d09bc3b8efaa7703c1962fef5c Mon Sep 17 00:00:00 2001 From: Veselov Pavel Date: Sun, 7 Jul 2019 15:18:18 +0300 Subject: [PATCH 02/16] some fixes --- src/.meta.php | 1 - src/Schema/Definitions/ColumnDefinition.php | 13 -- src/Schema/Definitions/UniqueDefinition.php | 79 ++++++- .../Definitions/UniqueWhereDefinition.php | 210 ++++++++++++++++++ src/Schema/Definitions/WhereDefinition.php | 120 ---------- src/Schema/Grammars/PostgresGrammar.php | 3 +- 6 files changed, 280 insertions(+), 146 deletions(-) delete mode 100644 src/Schema/Definitions/ColumnDefinition.php create mode 100644 src/Schema/Definitions/UniqueWhereDefinition.php delete mode 100644 src/Schema/Definitions/WhereDefinition.php diff --git a/src/.meta.php b/src/.meta.php index 43daa5d..e18a7de 100644 --- a/src/.meta.php +++ b/src/.meta.php @@ -5,7 +5,6 @@ use Illuminate\Support\Fluent; use Umbrellio\Postgres\Schema\Definitions\AttachPartitionDefinition; use Umbrellio\Postgres\Schema\Definitions\LikeDefinition; - use Umbrellio\Postgres\Schema\Definitions\ColumnDefinition; /** * @method AttachPartitionDefinition attachPartition(string $partition) diff --git a/src/Schema/Definitions/ColumnDefinition.php b/src/Schema/Definitions/ColumnDefinition.php deleted file mode 100644 index decd04e..0000000 --- a/src/Schema/Definitions/ColumnDefinition.php +++ /dev/null @@ -1,13 +0,0 @@ -createCommand($boolean . 'WhereRaw', compact('sql', 'bindings')); } - public function where(string $column, $operator = null, $value = null, string $boolean = 'and'): WhereDefinition + /** + * @param string $column + * @param null $operator + * @param null $value + * @param string $boolean + * @return UniqueWhereDefinition + */ + public function where($column, $operator = null, $value = null, $boolean = 'and') { return $this->createCommand($boolean . 'Where', compact('column', 'operator', 'value')); } - public function whereColumn(string $first, $operator = null, $second = null, string $boolean = 'and'): WhereDefinition + /** + * @param $first + * @param mixed|string|null $operator + * @param mixed|string|null $second + * @param string $boolean + * @return UniqueWhereDefinition + */ + public function whereColumn($first, $operator = null, $second = null, $boolean = 'and') { return $this->createCommand($boolean . 'WhereColumn', compact('first', 'operator', 'second')); } - public function whereIn(string $column, array $values, string $boolean = 'and', bool $not = false): WhereDefinition + /** + * @param string $column + * @param array $values + * @param string $boolean + * @param bool $not + * @return UniqueWhereDefinition + */ + public function whereIn($column, $values, $boolean = 'and', $not = false) { $command = $boolean . 'Where' . ($not ? 'Not' : '') . 'In'; return $this->createCommand($command, compact('column', 'values')); } - public function whereNotIn(string $column, array $values, string $boolean = 'and'): WhereDefinition + /** + * @param string $column + * @param array $values + * @param string $boolean + * @return UniqueWhereDefinition + */ + public function whereNotIn($column, $values, $boolean = 'and') { return $this->createCommand($boolean . 'WhereNotIn', compact('column', 'values')); } - public function whereNull(string $column, string $boolean = 'and', bool $not = false): WhereDefinition + /** + * @param string $column + * @param string $boolean + * @param bool $not + * @return UniqueWhereDefinition + */ + public function whereNull($column, $boolean = 'and', $not = false) { $command = $boolean . 'Where' . ($not ? 'Not' : '') . 'Null'; return $this->createCommand($command, compact('column')); } - public function whereBetween(string $column, array $values, string $boolean = 'and', bool $not = false): WhereDefinition + /** + * @param string $column + * @param array $values + * @param string $boolean + * @param bool $not + * @return UniqueWhereDefinition + */ + public function whereBetween($column, $values, $boolean = 'and', $not = false) { $command = $boolean . 'Where' . ($not ? 'Not' : '') . 'Between'; return $this->createCommand($command, compact('column', 'values')); } - public function whereNotBetween(string $column, array $values, string $boolean = 'and'): WhereDefinition + /** + * @param string $column + * @param array $values + * @param string $boolean + * @return UniqueWhereDefinition + */ + public function whereNotBetween($column, $values, $boolean = 'and') { return $this->createCommand($boolean . 'WhereNotBetween', compact('column', 'values')); } - public function whereNotNull(string $column, string $boolean = 'and'): WhereDefinition + /** + * @param string $column + * @param string $boolean + * @return UniqueWhereDefinition + */ + public function whereNotNull($column, $boolean = 'and') { return $this->createCommand($boolean . 'WhereNotNull', compact('column')); } - protected function createCommand($name, $parameters): WhereDefinition + protected function createCommand(string $name, array $parameters = []): UniqueWhereDefinition { - $command = new WhereDefinition(); + $command = new UniqueWhereDefinition(); $this->attributes['wheres'] = call_user_func_array([$command, $name], $parameters); return $command; } diff --git a/src/Schema/Definitions/UniqueWhereDefinition.php b/src/Schema/Definitions/UniqueWhereDefinition.php new file mode 100644 index 0000000..6e011a6 --- /dev/null +++ b/src/Schema/Definitions/UniqueWhereDefinition.php @@ -0,0 +1,210 @@ +attributes['andWhereRaw'][] = compact('sql', 'bindings'); + return $this; + } + + /** + * @param string $sql + * @param array $bindings + * @return $this + */ + public function orWhereRaw($sql, $bindings = []) + { + $this->attributes['orWhereRaw'][] = compact('sql', 'bindings'); + return $this; + } + + /** + * @param string $column + * @param mixed|string|null $operator + * @param mixed|string|null $value + * @return $this + */ + public function andWhere($column, $operator = null, $value = null) + { + $this->attributes['andWhere'][] = compact('column', 'operator', 'value'); + return $this; + } + + /** + * @param string $column + * @param mixed|string|null $operator + * @param mixed|string|null $value + * @return $this + */ + public function orWhere($column, $operator = null, $value = null) + { + $this->attributes['orWhere'][] = compact('column', 'operator', 'value'); + return $this; + } + + /** + * @param string $first + * @param mixed|string|null $operator + * @param mixed|string|null $second + * @return $this + */ + public function andWhereColumn($first, $operator = null, $second = null) + { + $this->attributes['andWhereColumn'][] = compact('first', 'operator', 'second'); + return $this; + } + + /** + * @param string $first + * @param mixed|string|null $operator + * @param mixed|string|null $second + * @return $this + */ + public function orWhereColumn($first, $operator = null, $second = null) + { + $this->attributes['orWhereColumn'][] = compact('first', 'operator', 'second'); + return $this; + } + + /** + * @param string $column + * @param array $values + * @return $this + */ + public function andWhereIn($column, $values) + { + $this->attributes['andWhereIn'][] = compact('column', 'values'); + return $this; + } + + /** + * @param string $column + * @param array $values + * @return $this + */ + public function orWhereIn($column, $values) + { + $this->attributes['orWhereIn'][] = compact('column', 'values'); + return $this; + } + + /** + * @param string $column + * @param array $values + * @return $this + */ + public function andWhereNotIn($column, $values) + { + $this->attributes['andWhereNotIn'][] = compact('column', 'values'); + return $this; + } + + /** + * @param string $column + * @param array $values + * @return $this + */ + public function orWhereNotIn($column, $values) + { + $this->attributes['orWhereNotIn'][] = compact('column', 'values'); + return $this; + } + + /** + * @param string $column + * @return $this + */ + public function andWhereNull($column) + { + $this->attributes['andWhereNull'][] = compact('column'); + return $this; + } + + /** + * @param string $column + * @return $this + */ + public function orWhereNull($column) + { + $this->attributes['orWhereNull'][] = compact('column'); + return $this; + } + + /** + * @param string $column + * @param array $values + * @return $this + */ + public function andWhereBetween($column, $values) + { + $this->attributes['andWhereBetween'][] = compact('column', 'values'); + return $this; + } + + /** + * @param string $column + * @param array $values + * @return $this + */ + public function orWhereBetween($column, $values) + { + $this->attributes['orWhereBetween'][] = compact('column', 'values'); + return $this; + } + + /** + * @param string $column + * @param array $values + * @return $this + */ + public function andWhereNotBetween($column, $values) + { + $this->attributes['andWhereNotBetween'][] = compact('column', 'values'); + return $this; + } + + /** + * @param string $column + * @param array $values + * @return $this + */ + public function orWhereNotBetween($column, $values) + { + $this->attributes['orWhereNotBetween'][] = compact('column', 'values'); + return $this; + } + + /** + * @param string $column + * @return $this + */ + public function andWhereNotNull($column) + { + $this->attributes['andWhereNotNull'][] = compact('column'); + return $this; + } + + /** + * @param string $column + * @return $this + */ + public function orWhereNotNull($column) + { + $this->attributes['orWhereNotNull'][] = compact('column'); + return $this; + } +} diff --git a/src/Schema/Definitions/WhereDefinition.php b/src/Schema/Definitions/WhereDefinition.php deleted file mode 100644 index 8062c53..0000000 --- a/src/Schema/Definitions/WhereDefinition.php +++ /dev/null @@ -1,120 +0,0 @@ -attributes['andWhereRaw'][] = compact('sql', 'bindings'); - return $this; - } - - public function orWhereRaw(string $sql, array $bindings = []) - { - $this->attributes['orWhereRaw'][] = compact('sql', 'bindings'); - return $this; - } - - public function andWhere(string $column, $operator = null, $value = null) - { - $this->attributes['andWhere'][] = compact('column', 'operator', 'value'); - return $this; - } - - public function orWhere(string $column, $operator = null, $value = null) - { - $this->attributes['orWhere'][] = compact('column', 'operator', 'value'); - return $this; - } - - public function andWhereColumn(string $first, $operator = null, $second = null) - { - $this->attributes['andWhereColumn'][] = compact('first', 'operator', 'second'); - return $this; - } - - public function orWhereColumn(string $first, $operator = null, $second = null) - { - $this->attributes['orWhereColumn'][] = compact('first', 'operator', 'second'); - return $this; - } - - public function andWhereIn(string $column, array $values) - { - $this->attributes['andWhereIn'][] = compact('column', 'values'); - return $this; - } - - public function orWhereIn(string $column, array $values) - { - $this->attributes['orWhereIn'][] = compact('column', 'values'); - return $this; - } - - public function andWhereNotIn(string $column, array $values) - { - $this->attributes['andWhereNotIn'][] = compact('column', 'values'); - return $this; - } - - public function orWhereNotIn(string $column, array $values) - { - $this->attributes['orWhereNotIn'][] = compact('column', 'values'); - return $this; - } - - public function andWhereNull(string $column) - { - $this->attributes['andWhereNull'][] = compact('column'); - return $this; - } - - public function orWhereNull(string $column) - { - $this->attributes['orWhereNull'][] = compact('column'); - return $this; - } - - public function andWhereBetween(string $column, array $values) - { - $this->attributes['andWhereBetween'][] = compact('column', 'values'); - return $this; - } - - public function orWhereBetween(string $column, array $values) - { - $this->attributes['orWhereBetween'][] = compact('column', 'values'); - return $this; - } - - public function andWhereNotBetween(string $column, array $values) - { - $this->attributes['andWhereNotBetween'][] = compact('column', 'values'); - return $this; - } - - public function orWhereNotBetween(string $column, array $values) - { - $this->attributes['orWhereNotBetween'][] = compact('column', 'values'); - return $this; - } - - public function andWhereNotNull(string $column) - { - $this->attributes['andWhereNotNull'][] = compact('column'); - return $this; - } - - public function orWhereNotNull(string $column) - { - $this->attributes['orWhereNotNull'][] = compact('column'); - return $this; - } -} diff --git a/src/Schema/Grammars/PostgresGrammar.php b/src/Schema/Grammars/PostgresGrammar.php index 27508c0..4b94838 100644 --- a/src/Schema/Grammars/PostgresGrammar.php +++ b/src/Schema/Grammars/PostgresGrammar.php @@ -10,6 +10,7 @@ use Umbrellio\Postgres\Compilers\CreateCompiler; use Umbrellio\Postgres\Schema\Blueprint; use Umbrellio\Postgres\Schema\Definitions\UniqueDefinition; +use Umbrellio\Postgres\Schema\Definitions\UniqueWhereDefinition; class PostgresGrammar extends BasePostgresGrammar { @@ -61,7 +62,7 @@ public function compileDetachPartition($blueprint, Fluent $command): string */ public function compileUniquePartial($blueprint, Fluent $command): string { - if ($wheres = $command->get('wheres')) { + if ($command->get('wheres') instanceof UniqueWhereDefinition) { return UniqueWhereCompiler::compile($this, $blueprint, $command); } return $this->compileUnique($blueprint, $command); From 81f91b7ce0b7b88aec86ab13be867e124288ab2f Mon Sep 17 00:00:00 2001 From: Veselov Pavel Date: Mon, 8 Jul 2019 00:46:29 +0300 Subject: [PATCH 03/16] some fixes --- src/Compilers/UniqueWhereCompiler.php | 2 +- src/Schema/Definitions/UniqueDefinition.php | 26 ++--- .../Definitions/UniqueWhereDefinition.php | 102 ++++++++++++++---- src/Schema/Grammars/PostgresGrammar.php | 7 +- 4 files changed, 98 insertions(+), 39 deletions(-) diff --git a/src/Compilers/UniqueWhereCompiler.php b/src/Compilers/UniqueWhereCompiler.php index cbf23c8..c3ad70f 100644 --- a/src/Compilers/UniqueWhereCompiler.php +++ b/src/Compilers/UniqueWhereCompiler.php @@ -14,6 +14,6 @@ class UniqueWhereCompiler { public static function compile(Grammar $grammar, Blueprint $blueprint, Fluent $command): string { - dd($blueprint, $command); + dd($command); } } diff --git a/src/Schema/Definitions/UniqueDefinition.php b/src/Schema/Definitions/UniqueDefinition.php index f2ff519..d05d823 100644 --- a/src/Schema/Definitions/UniqueDefinition.php +++ b/src/Schema/Definitions/UniqueDefinition.php @@ -17,7 +17,7 @@ class UniqueDefinition extends Fluent */ public function whereRaw($sql, $bindings = [], $boolean = 'and') { - return $this->createCommand($boolean . 'WhereRaw', compact('sql', 'bindings')); + return $this->createCommand( "{$boolean}WhereRaw", compact('sql', 'bindings')); } /** @@ -29,7 +29,7 @@ public function whereRaw($sql, $bindings = [], $boolean = 'and') */ public function where($column, $operator = null, $value = null, $boolean = 'and') { - return $this->createCommand($boolean . 'Where', compact('column', 'operator', 'value')); + return $this->createCommand("{$boolean}Where", compact('column', 'operator', 'value')); } /** @@ -41,7 +41,7 @@ public function where($column, $operator = null, $value = null, $boolean = 'and' */ public function whereColumn($first, $operator = null, $second = null, $boolean = 'and') { - return $this->createCommand($boolean . 'WhereColumn', compact('first', 'operator', 'second')); + return $this->createCommand("{$boolean}WhereColumn", compact('first', 'operator', 'second')); } /** @@ -53,8 +53,8 @@ public function whereColumn($first, $operator = null, $second = null, $boolean = */ public function whereIn($column, $values, $boolean = 'and', $not = false) { - $command = $boolean . 'Where' . ($not ? 'Not' : '') . 'In'; - return $this->createCommand($command, compact('column', 'values')); + $type = $not ? 'NotIn' : 'In'; + return $this->createCommand("{$boolean}Where{$type}", compact('column', 'values', 'not')); } /** @@ -65,7 +65,8 @@ public function whereIn($column, $values, $boolean = 'and', $not = false) */ public function whereNotIn($column, $values, $boolean = 'and') { - return $this->createCommand($boolean . 'WhereNotIn', compact('column', 'values')); + return $this->whereIn($column, $values, $boolean, true); +// return $this->createCommand("{$boolean}WhereNotIn", compact('column', 'values')); } /** @@ -76,8 +77,8 @@ public function whereNotIn($column, $values, $boolean = 'and') */ public function whereNull($column, $boolean = 'and', $not = false) { - $command = $boolean . 'Where' . ($not ? 'Not' : '') . 'Null'; - return $this->createCommand($command, compact('column')); + $type = $not ? 'NotNull' : 'Null'; + return $this->createCommand("{$boolean}Where{$type}", compact('column', 'boolean')); } /** @@ -89,8 +90,7 @@ public function whereNull($column, $boolean = 'and', $not = false) */ public function whereBetween($column, $values, $boolean = 'and', $not = false) { - $command = $boolean . 'Where' . ($not ? 'Not' : '') . 'Between'; - return $this->createCommand($command, compact('column', 'values')); + return $this->createCommand("{$boolean}WhereBetween", compact('column', 'values', 'not')); } /** @@ -101,7 +101,7 @@ public function whereBetween($column, $values, $boolean = 'and', $not = false) */ public function whereNotBetween($column, $values, $boolean = 'and') { - return $this->createCommand($boolean . 'WhereNotBetween', compact('column', 'values')); + return $this->whereBetween($column, $values, $boolean, true); } /** @@ -111,13 +111,13 @@ public function whereNotBetween($column, $values, $boolean = 'and') */ public function whereNotNull($column, $boolean = 'and') { - return $this->createCommand($boolean . 'WhereNotNull', compact('column')); + return $this->whereNull($column, $boolean, true); } protected function createCommand(string $name, array $parameters = []): UniqueWhereDefinition { $command = new UniqueWhereDefinition(); - $this->attributes['wheres'] = call_user_func_array([$command, $name], $parameters); + $this->attributes['constraints'] = call_user_func_array([$command, $name], $parameters); return $command; } } diff --git a/src/Schema/Definitions/UniqueWhereDefinition.php b/src/Schema/Definitions/UniqueWhereDefinition.php index 6e011a6..2f96dab 100644 --- a/src/Schema/Definitions/UniqueWhereDefinition.php +++ b/src/Schema/Definitions/UniqueWhereDefinition.php @@ -17,7 +17,10 @@ class UniqueWhereDefinition extends Fluent */ public function andWhereRaw($sql, $bindings = []) { - $this->attributes['andWhereRaw'][] = compact('sql', 'bindings'); + $this->attributes['wheres'][] = array_merge( + ['type' => 'Raw', 'boolean' => 'and'], + compact('sql', 'bindings') + ); return $this; } @@ -28,7 +31,10 @@ public function andWhereRaw($sql, $bindings = []) */ public function orWhereRaw($sql, $bindings = []) { - $this->attributes['orWhereRaw'][] = compact('sql', 'bindings'); + $this->attributes['wheres'][] = array_merge( + ['type' => 'Raw', 'boolean' => 'or'], + compact('sql', 'bindings') + ); return $this; } @@ -40,7 +46,10 @@ public function orWhereRaw($sql, $bindings = []) */ public function andWhere($column, $operator = null, $value = null) { - $this->attributes['andWhere'][] = compact('column', 'operator', 'value'); + $this->attributes['wheres'][] = array_merge( + ['type' => 'Basic', 'boolean' => 'and'], + compact('column', 'operator', 'value') + ); return $this; } @@ -52,7 +61,10 @@ public function andWhere($column, $operator = null, $value = null) */ public function orWhere($column, $operator = null, $value = null) { - $this->attributes['orWhere'][] = compact('column', 'operator', 'value'); + $this->attributes['wheres'][] = array_merge( + ['type' => 'Basic', 'boolean' => 'or'], + compact('column', 'operator', 'value') + ); return $this; } @@ -64,7 +76,10 @@ public function orWhere($column, $operator = null, $value = null) */ public function andWhereColumn($first, $operator = null, $second = null) { - $this->attributes['andWhereColumn'][] = compact('first', 'operator', 'second'); + $this->attributes['wheres'][] = array_merge( + ['type' => 'Column', 'boolean' => 'and'], + compact('column', 'operator', 'second') + ); return $this; } @@ -76,7 +91,10 @@ public function andWhereColumn($first, $operator = null, $second = null) */ public function orWhereColumn($first, $operator = null, $second = null) { - $this->attributes['orWhereColumn'][] = compact('first', 'operator', 'second'); + $this->attributes['wheres'][] = array_merge( + ['type' => 'Column', 'boolean' => 'or'], + compact('column', 'operator', 'second') + ); return $this; } @@ -87,7 +105,10 @@ public function orWhereColumn($first, $operator = null, $second = null) */ public function andWhereIn($column, $values) { - $this->attributes['andWhereIn'][] = compact('column', 'values'); + $this->attributes['wheres'][] = array_merge( + ['type' => 'In', 'boolean' => 'and'], + compact('column', 'values') + ); return $this; } @@ -98,7 +119,10 @@ public function andWhereIn($column, $values) */ public function orWhereIn($column, $values) { - $this->attributes['orWhereIn'][] = compact('column', 'values'); + $this->attributes['wheres'][] = array_merge( + ['type' => 'In', 'boolean' => 'or'], + compact('column','values') + ); return $this; } @@ -109,7 +133,10 @@ public function orWhereIn($column, $values) */ public function andWhereNotIn($column, $values) { - $this->attributes['andWhereNotIn'][] = compact('column', 'values'); + $this->attributes['wheres'][] = array_merge( + ['type' => 'NotIn', 'boolean' => 'and'], + compact('column', 'values') + ); return $this; } @@ -120,7 +147,10 @@ public function andWhereNotIn($column, $values) */ public function orWhereNotIn($column, $values) { - $this->attributes['orWhereNotIn'][] = compact('column', 'values'); + $this->attributes['wheres'][] = array_merge( + ['type' => 'NotIn', 'boolean' => 'or'], + compact('column', 'values') + ); return $this; } @@ -130,7 +160,10 @@ public function orWhereNotIn($column, $values) */ public function andWhereNull($column) { - $this->attributes['andWhereNull'][] = compact('column'); + $this->attributes['wheres'][] = array_merge( + ['type' => 'Null', 'boolean' => 'and'], + compact('column') + ); return $this; } @@ -140,51 +173,70 @@ public function andWhereNull($column) */ public function orWhereNull($column) { - $this->attributes['orWhereNull'][] = compact('column'); + $this->attributes['wheres'][] = array_merge( + ['type' => 'Null', 'boolean' => 'or'], + compact('column') + ); return $this; } /** * @param string $column * @param array $values + * @param bool $not * @return $this */ - public function andWhereBetween($column, $values) + public function andWhereBetween($column, $values, $not = false) { - $this->attributes['andWhereBetween'][] = compact('column', 'values'); + $this->attributes['wheres'][] = array_merge( + ['type' => 'between', 'boolean' => 'and'], + compact('column', 'values', 'not') + ); return $this; } /** * @param string $column * @param array $values + * @param bool $not * @return $this */ - public function orWhereBetween($column, $values) + public function orWhereBetween($column, $values, $not = false) { - $this->attributes['orWhereBetween'][] = compact('column', 'values'); + $this->attributes['wheres'][] = array_merge( + ['type' => 'between', 'boolean' => 'or'], + compact('column', 'values', 'not') + ); return $this; } /** * @param string $column * @param array $values + * @param bool $not * @return $this */ - public function andWhereNotBetween($column, $values) + public function andWhereNotBetween($column, $values, $not = true) { - $this->attributes['andWhereNotBetween'][] = compact('column', 'values'); + $this->attributes['wheres'][] = array_merge( + ['type' => 'between', 'boolean' => 'and'], + compact('column', 'values', 'not') + ); return $this; } /** * @param string $column * @param array $values + * @param bool $not * @return $this */ - public function orWhereNotBetween($column, $values) + public function orWhereNotBetween($column, $values, $not = true) { - $this->attributes['orWhereNotBetween'][] = compact('column', 'values'); + $this->attributes['wheres'][] = array_merge( + ['type' => 'between', 'boolean' => 'or'], + compact('column', 'values', 'not') + ); return $this; } @@ -194,7 +246,10 @@ public function orWhereNotBetween($column, $values) */ public function andWhereNotNull($column) { - $this->attributes['andWhereNotNull'][] = compact('column'); + $this->attributes['wheres'][] = array_merge( + ['type' => 'NotNull', 'boolean' => 'and'], + compact('column') + ); return $this; } @@ -204,7 +259,10 @@ public function andWhereNotNull($column) */ public function orWhereNotNull($column) { - $this->attributes['orWhereNotNull'][] = compact('column'); + $this->attributes['wheres'][] = array_merge( + ['type' => 'NotNull', 'boolean' => 'or'], + compact('column') + ); return $this; } } diff --git a/src/Schema/Grammars/PostgresGrammar.php b/src/Schema/Grammars/PostgresGrammar.php index 4b94838..02947c2 100644 --- a/src/Schema/Grammars/PostgresGrammar.php +++ b/src/Schema/Grammars/PostgresGrammar.php @@ -62,9 +62,10 @@ public function compileDetachPartition($blueprint, Fluent $command): string */ public function compileUniquePartial($blueprint, Fluent $command): string { - if ($command->get('wheres') instanceof UniqueWhereDefinition) { - return UniqueWhereCompiler::compile($this, $blueprint, $command); + $sql = $this->compileUnique($blueprint, $command); + if ($command->get('constraints') instanceof UniqueWhereDefinition) { + $sql .= ' WHERE ' . UniqueWhereCompiler::compile($this, $blueprint, $command); } - return $this->compileUnique($blueprint, $command); + return $sql; } } From 6a7674126b85222a8cb33a8b1e25db3c2ee00dd3 Mon Sep 17 00:00:00 2001 From: Veselov Pavel Date: Mon, 8 Jul 2019 01:06:24 +0300 Subject: [PATCH 04/16] refactor --- src/Schema/Definitions/UniqueDefinition.php | 120 +-------- .../Definitions/UniqueWhereDefinition.php | 232 ++++-------------- 2 files changed, 58 insertions(+), 294 deletions(-) diff --git a/src/Schema/Definitions/UniqueDefinition.php b/src/Schema/Definitions/UniqueDefinition.php index d05d823..aaf4760 100644 --- a/src/Schema/Definitions/UniqueDefinition.php +++ b/src/Schema/Definitions/UniqueDefinition.php @@ -7,117 +7,23 @@ use phpDocumentor\Reflection\DocBlock; use ReflectionClass; +/** + * @method UniqueWhereDefinition where($column, $operator = null, $value = null, $boolean = 'and') + * @method UniqueWhereDefinition whereRaw($sql, $bindings = [], $boolean = 'and') + * @method UniqueWhereDefinition whereColumn($first, $operator = null, $second = null, $boolean = 'and') + * @method UniqueWhereDefinition whereIn($column, $values, $boolean = 'and', $not = false) + * @method UniqueWhereDefinition whereNotIn($column, $values, $boolean = 'and') + * @method UniqueWhereDefinition whereBetween($column, $values, $boolean = 'and', $not = false) + * @method UniqueWhereDefinition whereNotBetween($column, $values, $boolean = 'and') + * @method UniqueWhereDefinition whereNull($column, $boolean = 'and', $not = false) + * @method UniqueWhereDefinition whereNotNull($column, $boolean = 'and') + */ class UniqueDefinition extends Fluent { - /** - * @param string $sql - * @param array $bindings - * @param string $boolean - * @return UniqueWhereDefinition - */ - public function whereRaw($sql, $bindings = [], $boolean = 'and') - { - return $this->createCommand( "{$boolean}WhereRaw", compact('sql', 'bindings')); - } - - /** - * @param string $column - * @param null $operator - * @param null $value - * @param string $boolean - * @return UniqueWhereDefinition - */ - public function where($column, $operator = null, $value = null, $boolean = 'and') - { - return $this->createCommand("{$boolean}Where", compact('column', 'operator', 'value')); - } - - /** - * @param $first - * @param mixed|string|null $operator - * @param mixed|string|null $second - * @param string $boolean - * @return UniqueWhereDefinition - */ - public function whereColumn($first, $operator = null, $second = null, $boolean = 'and') - { - return $this->createCommand("{$boolean}WhereColumn", compact('first', 'operator', 'second')); - } - - /** - * @param string $column - * @param array $values - * @param string $boolean - * @param bool $not - * @return UniqueWhereDefinition - */ - public function whereIn($column, $values, $boolean = 'and', $not = false) - { - $type = $not ? 'NotIn' : 'In'; - return $this->createCommand("{$boolean}Where{$type}", compact('column', 'values', 'not')); - } - - /** - * @param string $column - * @param array $values - * @param string $boolean - * @return UniqueWhereDefinition - */ - public function whereNotIn($column, $values, $boolean = 'and') - { - return $this->whereIn($column, $values, $boolean, true); -// return $this->createCommand("{$boolean}WhereNotIn", compact('column', 'values')); - } - - /** - * @param string $column - * @param string $boolean - * @param bool $not - * @return UniqueWhereDefinition - */ - public function whereNull($column, $boolean = 'and', $not = false) - { - $type = $not ? 'NotNull' : 'Null'; - return $this->createCommand("{$boolean}Where{$type}", compact('column', 'boolean')); - } - - /** - * @param string $column - * @param array $values - * @param string $boolean - * @param bool $not - * @return UniqueWhereDefinition - */ - public function whereBetween($column, $values, $boolean = 'and', $not = false) - { - return $this->createCommand("{$boolean}WhereBetween", compact('column', 'values', 'not')); - } - - /** - * @param string $column - * @param array $values - * @param string $boolean - * @return UniqueWhereDefinition - */ - public function whereNotBetween($column, $values, $boolean = 'and') - { - return $this->whereBetween($column, $values, $boolean, true); - } - - /** - * @param string $column - * @param string $boolean - * @return UniqueWhereDefinition - */ - public function whereNotNull($column, $boolean = 'and') - { - return $this->whereNull($column, $boolean, true); - } - - protected function createCommand(string $name, array $parameters = []): UniqueWhereDefinition + public function __call($method, $parameters) { $command = new UniqueWhereDefinition(); - $this->attributes['constraints'] = call_user_func_array([$command, $name], $parameters); + $this->attributes['constraints'] = call_user_func_array([$command, $method], $parameters); return $command; } } diff --git a/src/Schema/Definitions/UniqueWhereDefinition.php b/src/Schema/Definitions/UniqueWhereDefinition.php index 2f96dab..1d881bd 100644 --- a/src/Schema/Definitions/UniqueWhereDefinition.php +++ b/src/Schema/Definitions/UniqueWhereDefinition.php @@ -13,256 +13,114 @@ class UniqueWhereDefinition extends Fluent /** * @param string $sql * @param array $bindings - * @return $this - */ - public function andWhereRaw($sql, $bindings = []) - { - $this->attributes['wheres'][] = array_merge( - ['type' => 'Raw', 'boolean' => 'and'], - compact('sql', 'bindings') - ); - return $this; - } - - /** - * @param string $sql - * @param array $bindings - * @return $this - */ - public function orWhereRaw($sql, $bindings = []) - { - $this->attributes['wheres'][] = array_merge( - ['type' => 'Raw', 'boolean' => 'or'], - compact('sql', 'bindings') - ); - return $this; - } - - /** - * @param string $column - * @param mixed|string|null $operator - * @param mixed|string|null $value - * @return $this + * @param string $boolean + * @return UniqueWhereDefinition */ - public function andWhere($column, $operator = null, $value = null) + public function whereRaw($sql, $bindings = [], $boolean = 'and') { - $this->attributes['wheres'][] = array_merge( - ['type' => 'Basic', 'boolean' => 'and'], - compact('column', 'operator', 'value') - ); - return $this; + return $this->compileWhere( "raw", $boolean, compact('sql', 'bindings')); } /** * @param string $column - * @param mixed|string|null $operator - * @param mixed|string|null $value - * @return $this + * @param null $operator + * @param null $value + * @param string $boolean + * @return UniqueWhereDefinition */ - public function orWhere($column, $operator = null, $value = null) + public function where($column, $operator = null, $value = null, $boolean = 'and') { - $this->attributes['wheres'][] = array_merge( - ['type' => 'Basic', 'boolean' => 'or'], - compact('column', 'operator', 'value') - ); - return $this; + return $this->compileWhere( "Basic", $boolean, compact('column', 'operator', 'value')); } /** - * @param string $first + * @param $first * @param mixed|string|null $operator * @param mixed|string|null $second - * @return $this - */ - public function andWhereColumn($first, $operator = null, $second = null) - { - $this->attributes['wheres'][] = array_merge( - ['type' => 'Column', 'boolean' => 'and'], - compact('column', 'operator', 'second') - ); - return $this; - } - - /** - * @param string $first - * @param mixed|string|null $operator - * @param mixed|string|null $second - * @return $this - */ - public function orWhereColumn($first, $operator = null, $second = null) - { - $this->attributes['wheres'][] = array_merge( - ['type' => 'Column', 'boolean' => 'or'], - compact('column', 'operator', 'second') - ); - return $this; - } - - /** - * @param string $column - * @param array $values - * @return $this - */ - public function andWhereIn($column, $values) - { - $this->attributes['wheres'][] = array_merge( - ['type' => 'In', 'boolean' => 'and'], - compact('column', 'values') - ); - return $this; - } - - /** - * @param string $column - * @param array $values - * @return $this - */ - public function orWhereIn($column, $values) - { - $this->attributes['wheres'][] = array_merge( - ['type' => 'In', 'boolean' => 'or'], - compact('column','values') - ); - return $this; - } - - /** - * @param string $column - * @param array $values - * @return $this + * @param string $boolean + * @return UniqueWhereDefinition */ - public function andWhereNotIn($column, $values) + public function whereColumn($first, $operator = null, $second = null, $boolean = 'and') { - $this->attributes['wheres'][] = array_merge( - ['type' => 'NotIn', 'boolean' => 'and'], - compact('column', 'values') - ); - return $this; + return $this->compileWhere( "Column", $boolean, compact('column', 'operator', 'second')); } /** * @param string $column * @param array $values - * @return $this - */ - public function orWhereNotIn($column, $values) - { - $this->attributes['wheres'][] = array_merge( - ['type' => 'NotIn', 'boolean' => 'or'], - compact('column', 'values') - ); - return $this; - } - - /** - * @param string $column - * @return $this - */ - public function andWhereNull($column) - { - $this->attributes['wheres'][] = array_merge( - ['type' => 'Null', 'boolean' => 'and'], - compact('column') - ); - return $this; - } - - /** - * @param string $column - * @return $this + * @param string $boolean + * @param bool $not + * @return UniqueWhereDefinition */ - public function orWhereNull($column) + public function whereIn($column, $values, $boolean = 'and', $not = false) { - $this->attributes['wheres'][] = array_merge( - ['type' => 'Null', 'boolean' => 'or'], - compact('column') - ); - return $this; + return $this->compileWhere( $not ? 'NotIn' : 'In', $boolean, compact('column', 'values')); } /** * @param string $column * @param array $values - * @param bool $not - * @return $this + * @param string $boolean + * @return UniqueWhereDefinition */ - public function andWhereBetween($column, $values, $not = false) + public function whereNotIn($column, $values, $boolean = 'and') { - $this->attributes['wheres'][] = array_merge( - ['type' => 'between', 'boolean' => 'and'], - compact('column', 'values', 'not') - ); - return $this; + return $this->whereIn($column, $values, $boolean, true); } /** * @param string $column - * @param array $values + * @param string $boolean * @param bool $not - * @return $this + * @return UniqueWhereDefinition */ - public function orWhereBetween($column, $values, $not = false) + public function whereNull($column, $boolean = 'and', $not = false) { - $this->attributes['wheres'][] = array_merge( - ['type' => 'between', 'boolean' => 'or'], - compact('column', 'values', 'not') - ); - return $this; + return $this->compileWhere($not ? 'NotNull' : 'Null', $boolean, compact('column')); } /** * @param string $column * @param array $values + * @param string $boolean * @param bool $not - * @return $this + * @return UniqueWhereDefinition */ - public function andWhereNotBetween($column, $values, $not = true) + public function whereBetween($column, $values, $boolean = 'and', $not = false) { - $this->attributes['wheres'][] = array_merge( - ['type' => 'between', 'boolean' => 'and'], - compact('column', 'values', 'not') - ); - return $this; + return $this->compileWhere('between', $boolean, compact('column', 'values', 'not')); } /** * @param string $column * @param array $values - * @param bool $not - * @return $this + * @param string $boolean + * @return UniqueWhereDefinition */ - public function orWhereNotBetween($column, $values, $not = true) + public function whereNotBetween($column, $values, $boolean = 'and') { - $this->attributes['wheres'][] = array_merge( - ['type' => 'between', 'boolean' => 'or'], - compact('column', 'values', 'not') - ); - return $this; + return $this->whereBetween($column, $values, $boolean, true); } /** * @param string $column - * @return $this + * @param string $boolean + * @return UniqueWhereDefinition */ - public function andWhereNotNull($column) + public function whereNotNull($column, $boolean = 'and') { - $this->attributes['wheres'][] = array_merge( - ['type' => 'NotNull', 'boolean' => 'and'], - compact('column') - ); - return $this; + return $this->whereNull($column, $boolean, true); } /** - * @param string $column + * @param string $type + * @param string $boolean + * @param array $parameters * @return $this */ - public function orWhereNotNull($column) + protected function compileWhere($type, $boolean, $parameters) { - $this->attributes['wheres'][] = array_merge( - ['type' => 'NotNull', 'boolean' => 'or'], - compact('column') - ); + $this->attributes['wheres'][] = array_merge(compact('type', 'boolean'), $parameters); return $this; } } From 71acc264de66908344b255e4a1e7f05203161472 Mon Sep 17 00:00:00 2001 From: Veselov Pavel Date: Mon, 8 Jul 2019 01:19:43 +0300 Subject: [PATCH 05/16] refactor --- src/Schema/Blueprint.php | 54 +++++-------------- src/Schema/Definitions/UniqueDefinition.php | 2 - .../Definitions/UniqueWhereDefinition.php | 10 ++-- src/Schema/Grammars/PostgresGrammar.php | 3 +- 4 files changed, 18 insertions(+), 51 deletions(-) diff --git a/src/Schema/Blueprint.php b/src/Schema/Blueprint.php index ea6d0fb..a1f465f 100644 --- a/src/Schema/Blueprint.php +++ b/src/Schema/Blueprint.php @@ -6,23 +6,17 @@ use Illuminate\Database\Schema\Blueprint as BaseBlueprint; use Illuminate\Support\Fluent; -use Illuminate\Validation\Rules\Unique; use Umbrellio\Postgres\Schema\Definitions\AttachPartitionDefinition; use Umbrellio\Postgres\Schema\Definitions\LikeDefinition; use Umbrellio\Postgres\Schema\Definitions\UniqueDefinition; class Blueprint extends BaseBlueprint { - /** - * @return AttachPartitionDefinition - */ - public function attachPartition(string $partition) + public function attachPartition(string $partition): AttachPartitionDefinition { - return $this->addExtendedCommand( - AttachPartitionDefinition::class, - 'attachPartition', - compact('partition') - ); + return $this->addExtendedCommand(AttachPartitionDefinition::class, 'attachPartition', compact( + 'partition' + )); } public function detachPartition(string $partition): void @@ -30,10 +24,7 @@ public function detachPartition(string $partition): void $this->addCommand('detachPartition', compact('partition')); } - /** - * @return LikeDefinition - */ - public function like(string $table) + public function like(string $table): LikeDefinition { return $this->addExtendedCommand(LikeDefinition::class, 'like', compact('table')); } @@ -43,13 +34,7 @@ public function ifNotExists(): Fluent return $this->addCommand('ifNotExists'); } - /** - * @param string|array $columns - * @param string|null $index - * @param string|null $algorithm - * @return UniqueDefinition - */ - public function uniquePartial($columns, $index = null, $algorithm = null) + public function uniquePartial($columns, $index = null, $algorithm = null): UniqueDefinition { $columns = (array) $columns; @@ -58,35 +43,22 @@ public function uniquePartial($columns, $index = null, $algorithm = null) // index type, such as primary or index, which makes the index unique. $index = $index ?: $this->createIndexName('unique', $columns); - return $this->addExtendedCommand( - UniqueDefinition::class, - 'uniquePartial', - compact('columns', 'index', 'algorithm') - ); + return $this->addExtendedCommand(UniqueDefinition::class, 'uniquePartial', compact( + 'columns', + 'index', + 'algorithm' + )); } /** - * Add a new extented command to the blueprint. - * - * @param string $fluent - * @param string $name - * @param array $parameters - * @return \Illuminate\Support\Fluent + * @return Fluent|LikeDefinition|AttachPartitionDefinition|UniqueDefinition */ - protected function addExtendedCommand($fluent, $name, array $parameters = []) + protected function addExtendedCommand(string $fluent, string $name, array $parameters = []) { $this->commands[] = $command = $this->createExtendedCommand($fluent, $name, $parameters); return $command; } - /** - * Create a new Extended Fluent command. - * - * @param string $fluent - * @param string $name - * @param array $parameters - * @return \Illuminate\Support\Fluent - */ protected function createExtendedCommand($fluent, $name, array $parameters = []) { return new $fluent(array_merge(compact('name'), $parameters)); diff --git a/src/Schema/Definitions/UniqueDefinition.php b/src/Schema/Definitions/UniqueDefinition.php index aaf4760..8341ba0 100644 --- a/src/Schema/Definitions/UniqueDefinition.php +++ b/src/Schema/Definitions/UniqueDefinition.php @@ -4,8 +4,6 @@ use Codeception\Util\Annotation; use Illuminate\Support\Fluent; -use phpDocumentor\Reflection\DocBlock; -use ReflectionClass; /** * @method UniqueWhereDefinition where($column, $operator = null, $value = null, $boolean = 'and') diff --git a/src/Schema/Definitions/UniqueWhereDefinition.php b/src/Schema/Definitions/UniqueWhereDefinition.php index 1d881bd..05d6ec6 100644 --- a/src/Schema/Definitions/UniqueWhereDefinition.php +++ b/src/Schema/Definitions/UniqueWhereDefinition.php @@ -2,11 +2,7 @@ namespace Umbrellio\Postgres\Schema\Definitions; -use DeepCopy\Reflection\ReflectionHelper; use Illuminate\Support\Fluent; -use InvalidArgumentException; -use ReflectionClass; -use Reflection; class UniqueWhereDefinition extends Fluent { @@ -18,7 +14,7 @@ class UniqueWhereDefinition extends Fluent */ public function whereRaw($sql, $bindings = [], $boolean = 'and') { - return $this->compileWhere( "raw", $boolean, compact('sql', 'bindings')); + return $this->compileWhere('raw', $boolean, compact('sql', 'bindings')); } /** @@ -30,7 +26,7 @@ public function whereRaw($sql, $bindings = [], $boolean = 'and') */ public function where($column, $operator = null, $value = null, $boolean = 'and') { - return $this->compileWhere( "Basic", $boolean, compact('column', 'operator', 'value')); + return $this->compileWhere('Basic', $boolean, compact('column', 'operator', 'value')); } /** @@ -42,7 +38,7 @@ public function where($column, $operator = null, $value = null, $boolean = 'and' */ public function whereColumn($first, $operator = null, $second = null, $boolean = 'and') { - return $this->compileWhere( "Column", $boolean, compact('column', 'operator', 'second')); + return $this->compileWhere('Column', $boolean, compact('first', 'operator', 'second')); } /** diff --git a/src/Schema/Grammars/PostgresGrammar.php b/src/Schema/Grammars/PostgresGrammar.php index 02947c2..2086601 100644 --- a/src/Schema/Grammars/PostgresGrammar.php +++ b/src/Schema/Grammars/PostgresGrammar.php @@ -7,6 +7,7 @@ use Illuminate\Database\Schema\Grammars\PostgresGrammar as BasePostgresGrammar; use Illuminate\Support\Fluent; use Umbrellio\Postgres\Compilers\UniqueWhereCompiler; +use Umbrellio\Postgres\Compilers\AttachPartitionCompiler; use Umbrellio\Postgres\Compilers\CreateCompiler; use Umbrellio\Postgres\Schema\Blueprint; use Umbrellio\Postgres\Schema\Definitions\UniqueDefinition; @@ -60,7 +61,7 @@ public function compileDetachPartition($blueprint, Fluent $command): string * @param UniqueDefinition $command * @return string */ - public function compileUniquePartial($blueprint, Fluent $command): string + public function compileUniquePartial($blueprint, UniqueDefinition $command): string { $sql = $this->compileUnique($blueprint, $command); if ($command->get('constraints') instanceof UniqueWhereDefinition) { From 3fa7d7c87251e28ceb50ee443d29b06f8293849e Mon Sep 17 00:00:00 2001 From: Veselov Pavel Date: Mon, 8 Jul 2019 02:47:57 +0300 Subject: [PATCH 06/16] complete compile wheres --- src/Compilers/UniqueWhereCompiler.php | 119 +++++++++++++++++++++++- src/Schema/Grammars/PostgresGrammar.php | 8 +- 2 files changed, 121 insertions(+), 6 deletions(-) diff --git a/src/Compilers/UniqueWhereCompiler.php b/src/Compilers/UniqueWhereCompiler.php index c3ad70f..02b2eed 100644 --- a/src/Compilers/UniqueWhereCompiler.php +++ b/src/Compilers/UniqueWhereCompiler.php @@ -5,15 +5,130 @@ namespace Umbrellio\Postgres\Compilers; use Carbon\Carbon; +use Illuminate\Database\Query\Builder; +use Illuminate\Database\Query\JoinClause; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Grammars\Grammar; use Illuminate\Support\Fluent; use InvalidArgumentException; +use Umbrellio\Postgres\Schema\Definitions\UniqueWhereDefinition; class UniqueWhereCompiler { - public static function compile(Grammar $grammar, Blueprint $blueprint, Fluent $command): string + public static function compile( + Grammar $grammar, + Blueprint $blueprint, + Fluent $fluent, + UniqueWhereDefinition $command + ): string { + $wheres = collect($command->get('wheres')) + ->map(function ($where) use ($grammar, $blueprint) { + return implode(' ', [ + $where['boolean'], + '(' . static::{"where{$where['type']}"}($grammar, $blueprint, $where) . ')' + ]); + }) + ->all(); + + return sprintf( + 'CREATE UNIQUE INDEX %s ON %s (%s) WHERE %s', + $fluent->get('index'), + $blueprint->getTable(), + implode(',', $fluent->get('columns')), + static::removeLeadingBoolean(implode(' ', $wheres)) + ); + } + + protected static function whereRaw(Grammar $grammar, Blueprint $blueprint, $where = []) + { + return call_user_func_array('sprintf', array_merge( + [str_replace('?', '%s', $where['sql'])], + static::wrapValues($where['bindings']) + )); + } + + protected static function whereBasic(Grammar $grammar, Blueprint $blueprint, $where) + { + return implode(' ', [ + $grammar->wrap($where['column']), + $where['operator'], + $grammar->parameter($where['value']) + ]); + } + + protected static function whereColumn(Grammar $grammar, Blueprint $blueprint, $where) + { + return implode(' ', [ + $grammar->wrap($where['first']), + $where['operator'], + $grammar->wrap($where['second']), + ]); + } + + protected static function whereIn(Grammar $grammar, Blueprint $blueprint, $where) + { + if (!empty($where['values'])) { + return implode(' ', [ + $grammar->wrap($where['column']), + 'in', + '(' . implode(',', static::wrapValues($where['values'])) . ')' + ]); + } + return '0 = 1'; + } + + protected static function whereNotIn(Grammar $grammar, Blueprint $blueprint, $where) + { + if (!empty($where['values'])) { + return implode(' ', [ + $grammar->wrap($where['column']), + 'not in', + '(' . implode(',', static::wrapValues($where['values'])) . ')' + ]); + } + return '1 = 1'; + } + + protected static function whereNull(Grammar $grammar, Blueprint $blueprint, $where) + { + return implode(' ', [$grammar->wrap($where['column']), 'is null']); + } + + protected static function whereNotNull(Grammar $grammar, Blueprint $blueprint, $where) + { + return implode(' ', [ + $grammar->wrap($where['column']), + 'is not null', + ]); + } + + protected static function whereBetween(Grammar $grammar, Blueprint $blueprint, $where) + { + return implode(' ', [ + $grammar->wrap($where['column']), + $where['not'] ? 'not between' : 'between', + reset($where['values']), + 'and', + end($where['values']), + ]); + } + + protected static function wrapValues($values = []): array + { + return collect($values)->map(function($value) { + if (is_string($value)) { + return "'{$value}'"; + } elseif ($value instanceof \DateTimeInterface) { + return $value->format($grammar->getDateFormat()); + } elseif (is_bool($value)) { + return (bool) $value; + } + return (int) $value; + })->toArray(); + } + + protected static function removeLeadingBoolean($value) { - dd($command); + return preg_replace('/and |or /i', '', $value, 1); } } diff --git a/src/Schema/Grammars/PostgresGrammar.php b/src/Schema/Grammars/PostgresGrammar.php index 2086601..858d56d 100644 --- a/src/Schema/Grammars/PostgresGrammar.php +++ b/src/Schema/Grammars/PostgresGrammar.php @@ -63,10 +63,10 @@ public function compileDetachPartition($blueprint, Fluent $command): string */ public function compileUniquePartial($blueprint, UniqueDefinition $command): string { - $sql = $this->compileUnique($blueprint, $command); - if ($command->get('constraints') instanceof UniqueWhereDefinition) { - $sql .= ' WHERE ' . UniqueWhereCompiler::compile($this, $blueprint, $command); + $constraints = $command->get('constraints'); + if ($constraints instanceof UniqueWhereDefinition) { + return UniqueWhereCompiler::compile($this, $blueprint, $command, $constraints); } - return $sql; + return $this->compileUnique($blueprint, $command); } } From 168d9d40b7e0b63ac99ea1e3a7e72583db1488c7 Mon Sep 17 00:00:00 2001 From: Veselov Pavel Date: Mon, 8 Jul 2019 03:20:06 +0300 Subject: [PATCH 07/16] linter fixes --- src/Compilers/UniqueWhereCompiler.php | 56 ++++++++++--------- src/Schema/Blueprint.php | 9 ++- src/Schema/Definitions/UniqueDefinition.php | 3 +- .../Definitions/UniqueWhereDefinition.php | 4 +- src/Schema/Grammars/PostgresGrammar.php | 2 +- 5 files changed, 39 insertions(+), 35 deletions(-) diff --git a/src/Compilers/UniqueWhereCompiler.php b/src/Compilers/UniqueWhereCompiler.php index 02b2eed..98b5069 100644 --- a/src/Compilers/UniqueWhereCompiler.php +++ b/src/Compilers/UniqueWhereCompiler.php @@ -4,14 +4,11 @@ namespace Umbrellio\Postgres\Compilers; -use Carbon\Carbon; -use Illuminate\Database\Query\Builder; -use Illuminate\Database\Query\JoinClause; -use Illuminate\Database\Schema\Blueprint; +use Umbrellio\Postgres\Schema\Blueprint; use Illuminate\Database\Schema\Grammars\Grammar; use Illuminate\Support\Fluent; -use InvalidArgumentException; use Umbrellio\Postgres\Schema\Definitions\UniqueWhereDefinition; +use DateTimeInterface; class UniqueWhereCompiler { @@ -25,7 +22,7 @@ public static function compile( ->map(function ($where) use ($grammar, $blueprint) { return implode(' ', [ $where['boolean'], - '(' . static::{"where{$where['type']}"}($grammar, $blueprint, $where) . ')' + '(' . static::{"where{$where['type']}"}($grammar, $blueprint, $where) . ')', ]); }) ->all(); @@ -38,21 +35,21 @@ public static function compile( static::removeLeadingBoolean(implode(' ', $wheres)) ); } - + protected static function whereRaw(Grammar $grammar, Blueprint $blueprint, $where = []) { return call_user_func_array('sprintf', array_merge( [str_replace('?', '%s', $where['sql'])], - static::wrapValues($where['bindings']) + static::wrapValues($grammar, $where['bindings']) )); } - + protected static function whereBasic(Grammar $grammar, Blueprint $blueprint, $where) { return implode(' ', [ $grammar->wrap($where['column']), $where['operator'], - $grammar->parameter($where['value']) + static::wrapValue($grammar, $where['value']), ]); } @@ -71,24 +68,24 @@ protected static function whereIn(Grammar $grammar, Blueprint $blueprint, $where return implode(' ', [ $grammar->wrap($where['column']), 'in', - '(' . implode(',', static::wrapValues($where['values'])) . ')' + '(' . implode(',', static::wrapValues($grammar, $where['values'])) . ')', ]); } return '0 = 1'; } - + protected static function whereNotIn(Grammar $grammar, Blueprint $blueprint, $where) { if (!empty($where['values'])) { return implode(' ', [ $grammar->wrap($where['column']), 'not in', - '(' . implode(',', static::wrapValues($where['values'])) . ')' + '(' . implode(',', static::wrapValues($grammar, $where['values'])) . ')', ]); } return '1 = 1'; } - + protected static function whereNull(Grammar $grammar, Blueprint $blueprint, $where) { return implode(' ', [$grammar->wrap($where['column']), 'is null']); @@ -107,26 +104,31 @@ protected static function whereBetween(Grammar $grammar, Blueprint $blueprint, $ return implode(' ', [ $grammar->wrap($where['column']), $where['not'] ? 'not between' : 'between', - reset($where['values']), + static::wrapValue($grammar, reset($where['values'])), 'and', - end($where['values']), + static::wrapValue($grammar, end($where['values'])), ]); } - protected static function wrapValues($values = []): array + protected static function wrapValues(Grammar $grammar, $values = []): array { - return collect($values)->map(function($value) { - if (is_string($value)) { - return "'{$value}'"; - } elseif ($value instanceof \DateTimeInterface) { - return $value->format($grammar->getDateFormat()); - } elseif (is_bool($value)) { - return (bool) $value; - } - return (int) $value; + return collect($values)->map(function ($value) use ($grammar) { + return static::wrapValue($grammar, $value); })->toArray(); } - + + protected static function wrapValue(Grammar $grammar, $value) + { + if (is_string($value)) { + return "'{$value}'"; + } elseif ($value instanceof DateTimeInterface) { + return $value->format($grammar->getDateFormat()); + } elseif (is_bool($value)) { + return (bool)$value; + } + return (int) $value; + } + protected static function removeLeadingBoolean($value) { return preg_replace('/and |or /i', '', $value, 1); diff --git a/src/Schema/Blueprint.php b/src/Schema/Blueprint.php index a1f465f..1228bd4 100644 --- a/src/Schema/Blueprint.php +++ b/src/Schema/Blueprint.php @@ -14,9 +14,7 @@ class Blueprint extends BaseBlueprint { public function attachPartition(string $partition): AttachPartitionDefinition { - return $this->addExtendedCommand(AttachPartitionDefinition::class, 'attachPartition', compact( - 'partition' - )); + return $this->addExtendedCommand(AttachPartitionDefinition::class, 'attachPartition', compact('partition')); } public function detachPartition(string $partition): void @@ -42,7 +40,7 @@ public function uniquePartial($columns, $index = null, $algorithm = null): Uniqu // convention of the table name, followed by the columns, followed by an // index type, such as primary or index, which makes the index unique. $index = $index ?: $this->createIndexName('unique', $columns); - + return $this->addExtendedCommand(UniqueDefinition::class, 'uniquePartial', compact( 'columns', 'index', @@ -55,7 +53,8 @@ public function uniquePartial($columns, $index = null, $algorithm = null): Uniqu */ protected function addExtendedCommand(string $fluent, string $name, array $parameters = []) { - $this->commands[] = $command = $this->createExtendedCommand($fluent, $name, $parameters); + $command = $this->createExtendedCommand($fluent, $name, $parameters); + $this->commands[] = $command; return $command; } diff --git a/src/Schema/Definitions/UniqueDefinition.php b/src/Schema/Definitions/UniqueDefinition.php index 8341ba0..118f630 100644 --- a/src/Schema/Definitions/UniqueDefinition.php +++ b/src/Schema/Definitions/UniqueDefinition.php @@ -1,8 +1,9 @@ compileWhere( $not ? 'NotIn' : 'In', $boolean, compact('column', 'values')); + return $this->compileWhere($not ? 'NotIn' : 'In', $boolean, compact('column', 'values')); } /** diff --git a/src/Schema/Grammars/PostgresGrammar.php b/src/Schema/Grammars/PostgresGrammar.php index 858d56d..f36b227 100644 --- a/src/Schema/Grammars/PostgresGrammar.php +++ b/src/Schema/Grammars/PostgresGrammar.php @@ -6,9 +6,9 @@ use Illuminate\Database\Schema\Grammars\PostgresGrammar as BasePostgresGrammar; use Illuminate\Support\Fluent; -use Umbrellio\Postgres\Compilers\UniqueWhereCompiler; use Umbrellio\Postgres\Compilers\AttachPartitionCompiler; use Umbrellio\Postgres\Compilers\CreateCompiler; +use Umbrellio\Postgres\Compilers\UniqueWhereCompiler; use Umbrellio\Postgres\Schema\Blueprint; use Umbrellio\Postgres\Schema\Definitions\UniqueDefinition; use Umbrellio\Postgres\Schema\Definitions\UniqueWhereDefinition; From 1a24dd5c91373da03c25ea6d50da96a58aabe87d Mon Sep 17 00:00:00 2001 From: Veselov Pavel Date: Mon, 8 Jul 2019 03:23:05 +0300 Subject: [PATCH 08/16] linter fixes --- src/Compilers/UniqueWhereCompiler.php | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/Compilers/UniqueWhereCompiler.php b/src/Compilers/UniqueWhereCompiler.php index 98b5069..7b840d1 100644 --- a/src/Compilers/UniqueWhereCompiler.php +++ b/src/Compilers/UniqueWhereCompiler.php @@ -4,11 +4,11 @@ namespace Umbrellio\Postgres\Compilers; -use Umbrellio\Postgres\Schema\Blueprint; +use DateTimeInterface; use Illuminate\Database\Schema\Grammars\Grammar; use Illuminate\Support\Fluent; +use Umbrellio\Postgres\Schema\Blueprint; use Umbrellio\Postgres\Schema\Definitions\UniqueWhereDefinition; -use DateTimeInterface; class UniqueWhereCompiler { @@ -93,10 +93,7 @@ protected static function whereNull(Grammar $grammar, Blueprint $blueprint, $whe protected static function whereNotNull(Grammar $grammar, Blueprint $blueprint, $where) { - return implode(' ', [ - $grammar->wrap($where['column']), - 'is not null', - ]); + return implode(' ', [$grammar->wrap($where['column']), 'is not null']); } protected static function whereBetween(Grammar $grammar, Blueprint $blueprint, $where) @@ -124,7 +121,7 @@ protected static function wrapValue(Grammar $grammar, $value) } elseif ($value instanceof DateTimeInterface) { return $value->format($grammar->getDateFormat()); } elseif (is_bool($value)) { - return (bool)$value; + return (bool) $value; } return (int) $value; } From 5bde5e8d5dabb6c6b5e989ffafc5dfd3ce0a86cc Mon Sep 17 00:00:00 2001 From: Veselov Pavel Date: Mon, 8 Jul 2019 09:51:20 +0300 Subject: [PATCH 09/16] add some tests --- tests/Functional/UniqueIndexTest.php | 92 ++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 tests/Functional/UniqueIndexTest.php diff --git a/tests/Functional/UniqueIndexTest.php b/tests/Functional/UniqueIndexTest.php new file mode 100644 index 0000000..5b95877 --- /dev/null +++ b/tests/Functional/UniqueIndexTest.php @@ -0,0 +1,92 @@ +increments('id'); + $table->string('name'); + $table->string('code'); + $table->integer('phone'); + $table->softDeletes(); + $callback($table); + }); + + $this->assertTrue(Schema::hasTable('test_table')); + + $indexes = $this->getIndexByName('test_table_name_unique'); + + $this->assertTrue(isset($indexes->indexdef)); + $this->assertSame($this->getDummyIndex() . $expected, $indexes->indexdef); + } + + public function provideIndexes(): Generator + { + yield [ + '', + function (Blueprint $table) { + $table->unique('name'); + } + ]; + yield [ + ' WHERE (deleted_at IS NULL)', + function (Blueprint $table) { + $table->uniquePartial('name')->whereNull('deleted_at'); + } + ]; + yield [ + ' WHERE (deleted_at IS NOT NULL)', + function (Blueprint $table) { + $table->uniquePartial('name')->whereNotNull('deleted_at'); + } + ]; + yield [ + " WHERE (phone = 1234)", + function (Blueprint $table) { + $table->uniquePartial('name')->where('phone', '=', 1234); + } + ]; + yield [ + " WHERE (phone = 1234)", + function (Blueprint $table) { + $table->uniquePartial('name')->where('phone', '=', 1234); + } + ]; + yield [ + " WHERE ((phone >= 1) AND (phone <= 2))", + function (Blueprint $table) { + $table->uniquePartial('name')->whereBetween('phone', [1, 2]); + } + ]; + yield [ + " WHERE ((phone < 1) OR (phone > 2))", + function (Blueprint $table) { + $table->uniquePartial('name')->whereNotBetween('phone', [1, 2]); + } + ]; + } + + protected function getDummyIndex() + { + return 'CREATE UNIQUE INDEX test_table_name_unique ON public.test_table USING btree (name)'; + } + + protected function getIndexByName($name) + { + return collect(DB::select("SELECT indexdef FROM pg_indexes WHERE indexname = '{$name}'"))->first(); + } +} From fc0269dfa20ef204293491fc533074ab58b3fc2d Mon Sep 17 00:00:00 2001 From: Veselov Pavel Date: Mon, 8 Jul 2019 09:57:28 +0300 Subject: [PATCH 10/16] linter fixes --- tests/Functional/UniqueIndexTest.php | 31 +++++++++++++--------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/tests/Functional/UniqueIndexTest.php b/tests/Functional/UniqueIndexTest.php index 5b95877..71416fd 100644 --- a/tests/Functional/UniqueIndexTest.php +++ b/tests/Functional/UniqueIndexTest.php @@ -4,10 +4,10 @@ namespace Umbrellio\Postgres\Tests\Functional; +use Generator; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Schema; use Umbrellio\Postgres\Schema\Blueprint; -use Generator; class UniqueIndexTest extends FunctionalTestCase { @@ -36,47 +36,44 @@ public function createPartialUniqueWithNull($expected, $callback): void public function provideIndexes(): Generator { - yield [ - '', - function (Blueprint $table) { - $table->unique('name'); - } - ]; + yield ['', function (Blueprint $table) { + $table->unique('name'); + }]; yield [ ' WHERE (deleted_at IS NULL)', function (Blueprint $table) { $table->uniquePartial('name')->whereNull('deleted_at'); - } + }, ]; yield [ ' WHERE (deleted_at IS NOT NULL)', function (Blueprint $table) { $table->uniquePartial('name')->whereNotNull('deleted_at'); - } + }, ]; yield [ - " WHERE (phone = 1234)", + ' WHERE (phone = 1234)', function (Blueprint $table) { $table->uniquePartial('name')->where('phone', '=', 1234); - } + }, ]; yield [ - " WHERE (phone = 1234)", + ' WHERE (phone = 1234)', function (Blueprint $table) { $table->uniquePartial('name')->where('phone', '=', 1234); - } + }, ]; yield [ - " WHERE ((phone >= 1) AND (phone <= 2))", + ' WHERE ((phone >= 1) AND (phone <= 2))', function (Blueprint $table) { $table->uniquePartial('name')->whereBetween('phone', [1, 2]); - } + }, ]; yield [ - " WHERE ((phone < 1) OR (phone > 2))", + ' WHERE ((phone < 1) OR (phone > 2))', function (Blueprint $table) { $table->uniquePartial('name')->whereNotBetween('phone', [1, 2]); - } + }, ]; } From 30b7096605074a05da1aa238637c11670a69951e Mon Sep 17 00:00:00 2001 From: Veselov Pavel Date: Mon, 8 Jul 2019 11:35:37 +0300 Subject: [PATCH 11/16] add some tests --- src/Compilers/UniqueWhereCompiler.php | 25 +++++++++------------ tests/Functional/FunctionalTestCase.php | 4 ++-- tests/Functional/UniqueIndexTest.php | 30 +++++++++++++++++++++++-- 3 files changed, 40 insertions(+), 19 deletions(-) diff --git a/src/Compilers/UniqueWhereCompiler.php b/src/Compilers/UniqueWhereCompiler.php index 7b840d1..1b64014 100644 --- a/src/Compilers/UniqueWhereCompiler.php +++ b/src/Compilers/UniqueWhereCompiler.php @@ -4,7 +4,6 @@ namespace Umbrellio\Postgres\Compilers; -use DateTimeInterface; use Illuminate\Database\Schema\Grammars\Grammar; use Illuminate\Support\Fluent; use Umbrellio\Postgres\Schema\Blueprint; @@ -40,7 +39,7 @@ protected static function whereRaw(Grammar $grammar, Blueprint $blueprint, $wher { return call_user_func_array('sprintf', array_merge( [str_replace('?', '%s', $where['sql'])], - static::wrapValues($grammar, $where['bindings']) + static::wrapValues($where['bindings']) )); } @@ -49,7 +48,7 @@ protected static function whereBasic(Grammar $grammar, Blueprint $blueprint, $wh return implode(' ', [ $grammar->wrap($where['column']), $where['operator'], - static::wrapValue($grammar, $where['value']), + static::wrapValue($where['value']), ]); } @@ -68,7 +67,7 @@ protected static function whereIn(Grammar $grammar, Blueprint $blueprint, $where return implode(' ', [ $grammar->wrap($where['column']), 'in', - '(' . implode(',', static::wrapValues($grammar, $where['values'])) . ')', + '(' . implode(',', static::wrapValues($where['values'])) . ')', ]); } return '0 = 1'; @@ -80,7 +79,7 @@ protected static function whereNotIn(Grammar $grammar, Blueprint $blueprint, $wh return implode(' ', [ $grammar->wrap($where['column']), 'not in', - '(' . implode(',', static::wrapValues($grammar, $where['values'])) . ')', + '(' . implode(',', static::wrapValues($where['values'])) . ')', ]); } return '1 = 1'; @@ -101,27 +100,23 @@ protected static function whereBetween(Grammar $grammar, Blueprint $blueprint, $ return implode(' ', [ $grammar->wrap($where['column']), $where['not'] ? 'not between' : 'between', - static::wrapValue($grammar, reset($where['values'])), + static::wrapValue(reset($where['values'])), 'and', - static::wrapValue($grammar, end($where['values'])), + static::wrapValue(end($where['values'])), ]); } - protected static function wrapValues(Grammar $grammar, $values = []): array + protected static function wrapValues($values = []): array { - return collect($values)->map(function ($value) use ($grammar) { - return static::wrapValue($grammar, $value); + return collect($values)->map(function ($value) { + return static::wrapValue($value); })->toArray(); } - protected static function wrapValue(Grammar $grammar, $value) + protected static function wrapValue($value) { if (is_string($value)) { return "'{$value}'"; - } elseif ($value instanceof DateTimeInterface) { - return $value->format($grammar->getDateFormat()); - } elseif (is_bool($value)) { - return (bool) $value; } return (int) $value; } diff --git a/tests/Functional/FunctionalTestCase.php b/tests/Functional/FunctionalTestCase.php index af522b6..848a275 100644 --- a/tests/Functional/FunctionalTestCase.php +++ b/tests/Functional/FunctionalTestCase.php @@ -21,8 +21,8 @@ protected function getEnvironmentSetUp($app) 'host' => env('TEST_DB_HOST', 'localhost'), 'port' => env('TEST_DB_PORT', 5432), 'database' => env('TEST_DB', 'testing'), - 'username' => env('TEST_DB_USER', 'postgres'), - 'password' => env('TEST_DB_PASSWORD', ''), + 'username' => env('TEST_DB_USER', 'user'), + 'password' => env('TEST_DB_PASSWORD', 'pass'), 'charset' => 'utf8', 'prefix' => '', 'schema' => 'public', diff --git a/tests/Functional/UniqueIndexTest.php b/tests/Functional/UniqueIndexTest.php index 71416fd..9a13dec 100644 --- a/tests/Functional/UniqueIndexTest.php +++ b/tests/Functional/UniqueIndexTest.php @@ -22,6 +22,8 @@ public function createPartialUniqueWithNull($expected, $callback): void $table->string('name'); $table->string('code'); $table->integer('phone'); + $table->boolean('enabled'); + $table->integer('icq'); $table->softDeletes(); $callback($table); }); @@ -58,9 +60,9 @@ function (Blueprint $table) { }, ]; yield [ - ' WHERE (phone = 1234)', + " WHERE ((code)::text = 'test'::text)", function (Blueprint $table) { - $table->uniquePartial('name')->where('phone', '=', 1234); + $table->uniquePartial('name')->where('code', '=', 'test'); }, ]; yield [ @@ -75,6 +77,30 @@ function (Blueprint $table) { $table->uniquePartial('name')->whereNotBetween('phone', [1, 2]); }, ]; + yield [ + ' WHERE (phone <> icq)', + function (Blueprint $table) { + $table->uniquePartial('name')->whereColumn('phone', '<>', 'icq'); + }, + ]; + yield [ + ' WHERE ((phone = 1) AND (icq < 2))', + function (Blueprint $table) { + $table->uniquePartial('name')->whereRaw('phone = ? and icq < ?', [1, 2]); + }, + ]; + yield [ + ' WHERE (phone = ANY (ARRAY[1, 2, 4]))', + function (Blueprint $table) { + $table->uniquePartial('name')->whereIn('phone', [1, 2, 4]); + }, + ]; + yield [ + ' WHERE (phone <> ALL (ARRAY[1, 2, 4]))', + function (Blueprint $table) { + $table->uniquePartial('name')->whereNotIn('phone', [1, 2, 4]); + }, + ]; } protected function getDummyIndex() From 72656221f51e38e87fa6b6726d4ae7d74cc4d041 Mon Sep 17 00:00:00 2001 From: Veselov Pavel Date: Mon, 8 Jul 2019 11:37:03 +0300 Subject: [PATCH 12/16] fix credentials for testing --- tests/Functional/FunctionalTestCase.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Functional/FunctionalTestCase.php b/tests/Functional/FunctionalTestCase.php index 848a275..af522b6 100644 --- a/tests/Functional/FunctionalTestCase.php +++ b/tests/Functional/FunctionalTestCase.php @@ -21,8 +21,8 @@ protected function getEnvironmentSetUp($app) 'host' => env('TEST_DB_HOST', 'localhost'), 'port' => env('TEST_DB_PORT', 5432), 'database' => env('TEST_DB', 'testing'), - 'username' => env('TEST_DB_USER', 'user'), - 'password' => env('TEST_DB_PASSWORD', 'pass'), + 'username' => env('TEST_DB_USER', 'postgres'), + 'password' => env('TEST_DB_PASSWORD', ''), 'charset' => 'utf8', 'prefix' => '', 'schema' => 'public', From 4a8aa9e605712e252e2ca7292879c05ad99af9e2 Mon Sep 17 00:00:00 2001 From: Veselov Pavel Date: Mon, 8 Jul 2019 11:47:16 +0300 Subject: [PATCH 13/16] fix coverage for smoke uniquePartial() --- tests/Functional/UniqueIndexTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Functional/UniqueIndexTest.php b/tests/Functional/UniqueIndexTest.php index 9a13dec..f05c6ce 100644 --- a/tests/Functional/UniqueIndexTest.php +++ b/tests/Functional/UniqueIndexTest.php @@ -39,7 +39,7 @@ public function createPartialUniqueWithNull($expected, $callback): void public function provideIndexes(): Generator { yield ['', function (Blueprint $table) { - $table->unique('name'); + $table->uniquePartial('name'); }]; yield [ ' WHERE (deleted_at IS NULL)', From 454c52f46d34c90a408912486895b22ca81f9fb2 Mon Sep 17 00:00:00 2001 From: Veselov Pavel Date: Mon, 8 Jul 2019 11:50:41 +0300 Subject: [PATCH 14/16] fix missing lines for coverage --- tests/Functional/UniqueIndexTest.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/Functional/UniqueIndexTest.php b/tests/Functional/UniqueIndexTest.php index f05c6ce..9a0dbd8 100644 --- a/tests/Functional/UniqueIndexTest.php +++ b/tests/Functional/UniqueIndexTest.php @@ -95,12 +95,24 @@ function (Blueprint $table) { $table->uniquePartial('name')->whereIn('phone', [1, 2, 4]); }, ]; + yield [ + ' WHERE (0 = 1)', + function (Blueprint $table) { + $table->uniquePartial('name')->whereIn('phone', []); + }, + ]; yield [ ' WHERE (phone <> ALL (ARRAY[1, 2, 4]))', function (Blueprint $table) { $table->uniquePartial('name')->whereNotIn('phone', [1, 2, 4]); }, ]; + yield [ + ' WHERE (1 = 1)', + function (Blueprint $table) { + $table->uniquePartial('name')->whereNotIn('phone', []); + }, + ]; } protected function getDummyIndex() From 39b7a6ea565ec3ae510a8997ed5d7ab4eac86485 Mon Sep 17 00:00:00 2001 From: Veselov Pavel Date: Mon, 8 Jul 2019 23:38:52 +0300 Subject: [PATCH 15/16] review fixes --- src/.meta.php | 3 +- src/Compilers/UniqueWhereCompiler.php | 30 ++--- src/Schema/Blueprint.php | 46 +++---- src/Schema/Builders/UniquePartialBuilder.php | 17 +++ src/Schema/Builders/UniqueWhereBuilder.php | 61 +++++++++ src/Schema/Definitions/UniqueDefinition.php | 24 ++-- .../Definitions/UniqueWhereDefinition.php | 124 ------------------ src/Schema/Grammars/PostgresGrammar.php | 36 ++--- tests/Functional/FunctionalTestCase.php | 4 +- 9 files changed, 137 insertions(+), 208 deletions(-) create mode 100644 src/Schema/Builders/UniquePartialBuilder.php create mode 100644 src/Schema/Builders/UniqueWhereBuilder.php delete mode 100644 src/Schema/Definitions/UniqueWhereDefinition.php diff --git a/src/.meta.php b/src/.meta.php index e18a7de..32053d7 100644 --- a/src/.meta.php +++ b/src/.meta.php @@ -5,13 +5,14 @@ use Illuminate\Support\Fluent; use Umbrellio\Postgres\Schema\Definitions\AttachPartitionDefinition; use Umbrellio\Postgres\Schema\Definitions\LikeDefinition; + use Umbrellio\Postgres\Schema\Definitions\UniqueDefinition; /** * @method AttachPartitionDefinition attachPartition(string $partition) * @method void detachPartition(string $partition) * @method LikeDefinition like(string $table) * @method Fluent ifNotExists() - * @method UniqueDefinition uniquePartial(string|array $columns, ?string $index = null, ?string $algorithm = null) + * @method UniqueDefinition uniquePartial($columns, ?string $index = null, ?string $algorithm = null) */ class Blueprint { diff --git a/src/Compilers/UniqueWhereCompiler.php b/src/Compilers/UniqueWhereCompiler.php index 1b64014..f1e1a61 100644 --- a/src/Compilers/UniqueWhereCompiler.php +++ b/src/Compilers/UniqueWhereCompiler.php @@ -4,18 +4,18 @@ namespace Umbrellio\Postgres\Compilers; +use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Grammars\Grammar; -use Illuminate\Support\Fluent; -use Umbrellio\Postgres\Schema\Blueprint; -use Umbrellio\Postgres\Schema\Definitions\UniqueWhereDefinition; +use Umbrellio\Postgres\Schema\Builders\UniquePartialBuilder; +use Umbrellio\Postgres\Schema\Builders\UniqueWhereBuilder; class UniqueWhereCompiler { public static function compile( Grammar $grammar, Blueprint $blueprint, - Fluent $fluent, - UniqueWhereDefinition $command + UniquePartialBuilder $fluent, + UniqueWhereBuilder $command ): string { $wheres = collect($command->get('wheres')) ->map(function ($where) use ($grammar, $blueprint) { @@ -35,7 +35,7 @@ public static function compile( ); } - protected static function whereRaw(Grammar $grammar, Blueprint $blueprint, $where = []) + protected static function whereRaw(Grammar $grammar, Blueprint $blueprint, array $where = []): string { return call_user_func_array('sprintf', array_merge( [str_replace('?', '%s', $where['sql'])], @@ -43,7 +43,7 @@ protected static function whereRaw(Grammar $grammar, Blueprint $blueprint, $wher )); } - protected static function whereBasic(Grammar $grammar, Blueprint $blueprint, $where) + protected static function whereBasic(Grammar $grammar, Blueprint $blueprint, array $where): string { return implode(' ', [ $grammar->wrap($where['column']), @@ -52,7 +52,7 @@ protected static function whereBasic(Grammar $grammar, Blueprint $blueprint, $wh ]); } - protected static function whereColumn(Grammar $grammar, Blueprint $blueprint, $where) + protected static function whereColumn(Grammar $grammar, Blueprint $blueprint, array $where): string { return implode(' ', [ $grammar->wrap($where['first']), @@ -61,7 +61,7 @@ protected static function whereColumn(Grammar $grammar, Blueprint $blueprint, $w ]); } - protected static function whereIn(Grammar $grammar, Blueprint $blueprint, $where) + protected static function whereIn(Grammar $grammar, Blueprint $blueprint, array $where = []): string { if (!empty($where['values'])) { return implode(' ', [ @@ -73,7 +73,7 @@ protected static function whereIn(Grammar $grammar, Blueprint $blueprint, $where return '0 = 1'; } - protected static function whereNotIn(Grammar $grammar, Blueprint $blueprint, $where) + protected static function whereNotIn(Grammar $grammar, Blueprint $blueprint, array $where = []): string { if (!empty($where['values'])) { return implode(' ', [ @@ -85,17 +85,17 @@ protected static function whereNotIn(Grammar $grammar, Blueprint $blueprint, $wh return '1 = 1'; } - protected static function whereNull(Grammar $grammar, Blueprint $blueprint, $where) + protected static function whereNull(Grammar $grammar, Blueprint $blueprint, array $where): string { return implode(' ', [$grammar->wrap($where['column']), 'is null']); } - protected static function whereNotNull(Grammar $grammar, Blueprint $blueprint, $where) + protected static function whereNotNull(Grammar $grammar, Blueprint $blueprint, array $where): string { return implode(' ', [$grammar->wrap($where['column']), 'is not null']); } - protected static function whereBetween(Grammar $grammar, Blueprint $blueprint, $where) + protected static function whereBetween(Grammar $grammar, Blueprint $blueprint, array $where): string { return implode(' ', [ $grammar->wrap($where['column']), @@ -106,7 +106,7 @@ protected static function whereBetween(Grammar $grammar, Blueprint $blueprint, $ ]); } - protected static function wrapValues($values = []): array + protected static function wrapValues(array $values = []): array { return collect($values)->map(function ($value) { return static::wrapValue($value); @@ -121,7 +121,7 @@ protected static function wrapValue($value) return (int) $value; } - protected static function removeLeadingBoolean($value) + protected static function removeLeadingBoolean(string $value): string { return preg_replace('/and |or /i', '', $value, 1); } diff --git a/src/Schema/Blueprint.php b/src/Schema/Blueprint.php index 1228bd4..eedde82 100644 --- a/src/Schema/Blueprint.php +++ b/src/Schema/Blueprint.php @@ -6,15 +6,19 @@ use Illuminate\Database\Schema\Blueprint as BaseBlueprint; use Illuminate\Support\Fluent; +use Umbrellio\Postgres\Schema\Builders\UniquePartialBuilder; use Umbrellio\Postgres\Schema\Definitions\AttachPartitionDefinition; use Umbrellio\Postgres\Schema\Definitions\LikeDefinition; use Umbrellio\Postgres\Schema\Definitions\UniqueDefinition; class Blueprint extends BaseBlueprint { - public function attachPartition(string $partition): AttachPartitionDefinition + /** + * @return AttachPartitionDefinition + */ + public function attachPartition(string $partition) { - return $this->addExtendedCommand(AttachPartitionDefinition::class, 'attachPartition', compact('partition')); + return $this->addCommand('attachPartition', compact('partition')); } public function detachPartition(string $partition): void @@ -22,9 +26,12 @@ public function detachPartition(string $partition): void $this->addCommand('detachPartition', compact('partition')); } - public function like(string $table): LikeDefinition + /** + * @return LikeDefinition + */ + public function like(string $table) { - return $this->addExtendedCommand(LikeDefinition::class, 'like', compact('table')); + return $this->addCommand('like', compact('table')); } public function ifNotExists(): Fluent @@ -32,34 +39,27 @@ public function ifNotExists(): Fluent return $this->addCommand('ifNotExists'); } - public function uniquePartial($columns, $index = null, $algorithm = null): UniqueDefinition + /** + * @param array|string $columns + * @return UniqueDefinition + */ + public function uniquePartial($columns, ?string $index = null, ?string $algorithm = null) { $columns = (array) $columns; - // If no name was specified for this index, we will create one using a basic - // convention of the table name, followed by the columns, followed by an - // index type, such as primary or index, which makes the index unique. $index = $index ?: $this->createIndexName('unique', $columns); - return $this->addExtendedCommand(UniqueDefinition::class, 'uniquePartial', compact( - 'columns', - 'index', - 'algorithm' - )); + return $this->addExtendedCommand( + UniquePartialBuilder::class, + 'uniquePartial', + compact('columns', 'index', 'algorithm') + ); } - /** - * @return Fluent|LikeDefinition|AttachPartitionDefinition|UniqueDefinition - */ - protected function addExtendedCommand(string $fluent, string $name, array $parameters = []) + private function addExtendedCommand(string $fluent, string $name, array $parameters = []) { - $command = $this->createExtendedCommand($fluent, $name, $parameters); + $command = new $fluent(array_merge(compact('name'), $parameters)); $this->commands[] = $command; return $command; } - - protected function createExtendedCommand($fluent, $name, array $parameters = []) - { - return new $fluent(array_merge(compact('name'), $parameters)); - } } diff --git a/src/Schema/Builders/UniquePartialBuilder.php b/src/Schema/Builders/UniquePartialBuilder.php new file mode 100644 index 0000000..fb19336 --- /dev/null +++ b/src/Schema/Builders/UniquePartialBuilder.php @@ -0,0 +1,17 @@ +attributes['constraints'] = call_user_func_array([$command, $method], $parameters); + return $command; + } +} diff --git a/src/Schema/Builders/UniqueWhereBuilder.php b/src/Schema/Builders/UniqueWhereBuilder.php new file mode 100644 index 0000000..35f11ad --- /dev/null +++ b/src/Schema/Builders/UniqueWhereBuilder.php @@ -0,0 +1,61 @@ +compileWhere('Raw', $boolean, compact('sql', 'bindings')); + } + + public function where(string $column, string $operator, string $value, string $boolean = 'and'): self + { + return $this->compileWhere('Basic', $boolean, compact('column', 'operator', 'value')); + } + + public function whereColumn(string $first, string $operator, string $second, string $boolean = 'and'): self + { + return $this->compileWhere('Column', $boolean, compact('first', 'operator', 'second')); + } + + public function whereIn(string $column, array $values, string $boolean = 'and', bool $not = false): self + { + return $this->compileWhere($not ? 'NotIn' : 'In', $boolean, compact('column', 'values')); + } + + public function whereNotIn(string $column, array $values = [], string $boolean = 'and'): self + { + return $this->whereIn($column, $values, $boolean, true); + } + + public function whereNull(string $column, string $boolean = 'and', bool $not = false): self + { + return $this->compileWhere($not ? 'NotNull' : 'Null', $boolean, compact('column')); + } + + public function whereBetween(string $column, array $values = [], string $boolean = 'and', bool $not = false): self + { + return $this->compileWhere('Between', $boolean, compact('column', 'values', 'not')); + } + + public function whereNotBetween(string $column, array $values = [], string $boolean = 'and'): self + { + return $this->whereBetween($column, $values, $boolean, true); + } + + public function whereNotNull(string $column, string $boolean = 'and'): self + { + return $this->whereNull($column, $boolean, true); + } + + protected function compileWhere(string $type, string $boolean, array $parameters = []): self + { + $this->attributes['wheres'][] = array_merge(compact('type', 'boolean'), $parameters); + return $this; + } +} diff --git a/src/Schema/Definitions/UniqueDefinition.php b/src/Schema/Definitions/UniqueDefinition.php index 118f630..a6bbed6 100644 --- a/src/Schema/Definitions/UniqueDefinition.php +++ b/src/Schema/Definitions/UniqueDefinition.php @@ -7,22 +7,16 @@ use Illuminate\Support\Fluent; /** - * @method UniqueWhereDefinition where($column, $operator = null, $value = null, $boolean = 'and') - * @method UniqueWhereDefinition whereRaw($sql, $bindings = [], $boolean = 'and') - * @method UniqueWhereDefinition whereColumn($first, $operator = null, $second = null, $boolean = 'and') - * @method UniqueWhereDefinition whereIn($column, $values, $boolean = 'and', $not = false) - * @method UniqueWhereDefinition whereNotIn($column, $values, $boolean = 'and') - * @method UniqueWhereDefinition whereBetween($column, $values, $boolean = 'and', $not = false) - * @method UniqueWhereDefinition whereNotBetween($column, $values, $boolean = 'and') - * @method UniqueWhereDefinition whereNull($column, $boolean = 'and', $not = false) - * @method UniqueWhereDefinition whereNotNull($column, $boolean = 'and') + * @method UniqueDefinition where($column, $operator, $value, $boolean = 'and') + * @method UniqueDefinition whereRaw($sql, $bindings = [], $boolean = 'and') + * @method UniqueDefinition whereColumn($first, $operator, $second, $boolean = 'and') + * @method UniqueDefinition whereIn($column, $values = [], $boolean = 'and', $not = false) + * @method UniqueDefinition whereNotIn($column, $values = [], $boolean = 'and') + * @method UniqueDefinition whereBetween($column, $values, $boolean = 'and', $not = false) + * @method UniqueDefinition whereNotBetween($column, $values, $boolean = 'and') + * @method UniqueDefinition whereNull($column, $boolean = 'and', $not = false) + * @method UniqueDefinition whereNotNull($column, $boolean = 'and') */ class UniqueDefinition extends Fluent { - public function __call($method, $parameters) - { - $command = new UniqueWhereDefinition(); - $this->attributes['constraints'] = call_user_func_array([$command, $method], $parameters); - return $command; - } } diff --git a/src/Schema/Definitions/UniqueWhereDefinition.php b/src/Schema/Definitions/UniqueWhereDefinition.php deleted file mode 100644 index d2341fc..0000000 --- a/src/Schema/Definitions/UniqueWhereDefinition.php +++ /dev/null @@ -1,124 +0,0 @@ -compileWhere('raw', $boolean, compact('sql', 'bindings')); - } - - /** - * @param string $column - * @param null $operator - * @param null $value - * @param string $boolean - * @return UniqueWhereDefinition - */ - public function where($column, $operator = null, $value = null, $boolean = 'and') - { - return $this->compileWhere('Basic', $boolean, compact('column', 'operator', 'value')); - } - - /** - * @param $first - * @param mixed|string|null $operator - * @param mixed|string|null $second - * @param string $boolean - * @return UniqueWhereDefinition - */ - public function whereColumn($first, $operator = null, $second = null, $boolean = 'and') - { - return $this->compileWhere('Column', $boolean, compact('first', 'operator', 'second')); - } - - /** - * @param string $column - * @param array $values - * @param string $boolean - * @param bool $not - * @return UniqueWhereDefinition - */ - public function whereIn($column, $values, $boolean = 'and', $not = false) - { - return $this->compileWhere($not ? 'NotIn' : 'In', $boolean, compact('column', 'values')); - } - - /** - * @param string $column - * @param array $values - * @param string $boolean - * @return UniqueWhereDefinition - */ - public function whereNotIn($column, $values, $boolean = 'and') - { - return $this->whereIn($column, $values, $boolean, true); - } - - /** - * @param string $column - * @param string $boolean - * @param bool $not - * @return UniqueWhereDefinition - */ - public function whereNull($column, $boolean = 'and', $not = false) - { - return $this->compileWhere($not ? 'NotNull' : 'Null', $boolean, compact('column')); - } - - /** - * @param string $column - * @param array $values - * @param string $boolean - * @param bool $not - * @return UniqueWhereDefinition - */ - public function whereBetween($column, $values, $boolean = 'and', $not = false) - { - return $this->compileWhere('between', $boolean, compact('column', 'values', 'not')); - } - - /** - * @param string $column - * @param array $values - * @param string $boolean - * @return UniqueWhereDefinition - */ - public function whereNotBetween($column, $values, $boolean = 'and') - { - return $this->whereBetween($column, $values, $boolean, true); - } - - /** - * @param string $column - * @param string $boolean - * @return UniqueWhereDefinition - */ - public function whereNotNull($column, $boolean = 'and') - { - return $this->whereNull($column, $boolean, true); - } - - /** - * @param string $type - * @param string $boolean - * @param array $parameters - * @return $this - */ - protected function compileWhere($type, $boolean, $parameters) - { - $this->attributes['wheres'][] = array_merge(compact('type', 'boolean'), $parameters); - return $this; - } -} diff --git a/src/Schema/Grammars/PostgresGrammar.php b/src/Schema/Grammars/PostgresGrammar.php index f36b227..473e4b8 100644 --- a/src/Schema/Grammars/PostgresGrammar.php +++ b/src/Schema/Grammars/PostgresGrammar.php @@ -4,23 +4,18 @@ namespace Umbrellio\Postgres\Schema\Grammars; +use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Grammars\PostgresGrammar as BasePostgresGrammar; use Illuminate\Support\Fluent; use Umbrellio\Postgres\Compilers\AttachPartitionCompiler; use Umbrellio\Postgres\Compilers\CreateCompiler; use Umbrellio\Postgres\Compilers\UniqueWhereCompiler; -use Umbrellio\Postgres\Schema\Blueprint; -use Umbrellio\Postgres\Schema\Definitions\UniqueDefinition; -use Umbrellio\Postgres\Schema\Definitions\UniqueWhereDefinition; +use Umbrellio\Postgres\Schema\Builders\UniquePartialBuilder; +use Umbrellio\Postgres\Schema\Builders\UniqueWhereBuilder; class PostgresGrammar extends BasePostgresGrammar { - /** - * @param Blueprint|\Illuminate\Database\Schema\Blueprint $blueprint - * @param Fluent $command - * @return string - */ - public function compileCreate($blueprint, Fluent $command): string + public function compileCreate(Blueprint $blueprint, Fluent $command): string { $like = $this->getCommandByName($blueprint, 'like'); $ifNotExists = $this->getCommandByName($blueprint, 'ifNotExists'); @@ -33,22 +28,12 @@ public function compileCreate($blueprint, Fluent $command): string ); } - /** - * @param Blueprint|\Illuminate\Database\Schema\Blueprint $blueprint - * @param Fluent $command - * @return string - */ - public function compileAttachPartition($blueprint, Fluent $command): string + public function compileAttachPartition(Blueprint $blueprint, Fluent $command): string { return AttachPartitionCompiler::compile($this, $blueprint, $command); } - /** - * @param Blueprint|\Illuminate\Database\Schema\Blueprint $blueprint - * @param Fluent $command - * @return string - */ - public function compileDetachPartition($blueprint, Fluent $command): string + public function compileDetachPartition(Blueprint $blueprint, Fluent $command): string { return sprintf('alter table %s detach partition %s', $this->wrapTable($blueprint), @@ -56,15 +41,10 @@ public function compileDetachPartition($blueprint, Fluent $command): string ); } - /** - * @param Blueprint|\Illuminate\Database\Schema\Blueprint $blueprint - * @param UniqueDefinition $command - * @return string - */ - public function compileUniquePartial($blueprint, UniqueDefinition $command): string + public function compileUniquePartial(Blueprint $blueprint, UniquePartialBuilder $command): string { $constraints = $command->get('constraints'); - if ($constraints instanceof UniqueWhereDefinition) { + if ($constraints instanceof UniqueWhereBuilder) { return UniqueWhereCompiler::compile($this, $blueprint, $command, $constraints); } return $this->compileUnique($blueprint, $command); diff --git a/tests/Functional/FunctionalTestCase.php b/tests/Functional/FunctionalTestCase.php index af522b6..848a275 100644 --- a/tests/Functional/FunctionalTestCase.php +++ b/tests/Functional/FunctionalTestCase.php @@ -21,8 +21,8 @@ protected function getEnvironmentSetUp($app) 'host' => env('TEST_DB_HOST', 'localhost'), 'port' => env('TEST_DB_PORT', 5432), 'database' => env('TEST_DB', 'testing'), - 'username' => env('TEST_DB_USER', 'postgres'), - 'password' => env('TEST_DB_PASSWORD', ''), + 'username' => env('TEST_DB_USER', 'user'), + 'password' => env('TEST_DB_PASSWORD', 'pass'), 'charset' => 'utf8', 'prefix' => '', 'schema' => 'public', From c57163ee6509868f992068e7def222df7b50cd94 Mon Sep 17 00:00:00 2001 From: Veselov Pavel Date: Mon, 8 Jul 2019 23:41:37 +0300 Subject: [PATCH 16/16] fix local credentials for travis --- tests/Functional/FunctionalTestCase.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Functional/FunctionalTestCase.php b/tests/Functional/FunctionalTestCase.php index 848a275..af522b6 100644 --- a/tests/Functional/FunctionalTestCase.php +++ b/tests/Functional/FunctionalTestCase.php @@ -21,8 +21,8 @@ protected function getEnvironmentSetUp($app) 'host' => env('TEST_DB_HOST', 'localhost'), 'port' => env('TEST_DB_PORT', 5432), 'database' => env('TEST_DB', 'testing'), - 'username' => env('TEST_DB_USER', 'user'), - 'password' => env('TEST_DB_PASSWORD', 'pass'), + 'username' => env('TEST_DB_USER', 'postgres'), + 'password' => env('TEST_DB_PASSWORD', ''), 'charset' => 'utf8', 'prefix' => '', 'schema' => 'public',