From 1b865a9dda241465c0906e32c8f97730a7fdaef1 Mon Sep 17 00:00:00 2001 From: BMTmohammedtaha Date: Wed, 27 Sep 2023 12:18:20 +0200 Subject: [PATCH] add if/if not exists to create table --- src/Build/QueryBuilderTrait.php | 13 ++++++++++++ src/Build/TableQueryBuilder.php | 3 ++- src/Operations/CreateTable.php | 13 ------------ src/Operations/OperationsTrait.php | 33 +++++++++++++++++++++++++++++- 4 files changed, 47 insertions(+), 15 deletions(-) diff --git a/src/Build/QueryBuilderTrait.php b/src/Build/QueryBuilderTrait.php index 63ac90b..1dd2963 100644 --- a/src/Build/QueryBuilderTrait.php +++ b/src/Build/QueryBuilderTrait.php @@ -306,4 +306,17 @@ public function checkQuery(string $column, array $expressions, array $checkSort return $result; } + + /** + * build exists query + * + * @return string + */ + public function exists():string + { + if(!$this->hasAttribute('exists')){ + return ''; + } + return $this->getAttribute('exists') ? $this->syntax->getCommand('ifExists') : $this->syntax->getCommand('ifNotExists'); + } } diff --git a/src/Build/TableQueryBuilder.php b/src/Build/TableQueryBuilder.php index cbd5ed5..4dd8269 100644 --- a/src/Build/TableQueryBuilder.php +++ b/src/Build/TableQueryBuilder.php @@ -249,8 +249,9 @@ public function tableCharset(): string public function buildCreateTable(): string { $query = sprintf( - "%s %s ( %s %s ) %s %s", + "%s %s %s ( %s %s ) %s %s", $this->start(), + $this->exists(), $this->tableName(), $this->columnQueryBuilder(), $this->check(), diff --git a/src/Operations/CreateTable.php b/src/Operations/CreateTable.php index 4291662..d7da646 100644 --- a/src/Operations/CreateTable.php +++ b/src/Operations/CreateTable.php @@ -60,19 +60,6 @@ public function charset(string $charset): self return $this; } - /** - * Specify whether the table should only be created if it does not already exist. - * - * @param bool $act Whether to create the table only if it doesn't exist. - * - * @return self - */ - public function exists(bool $act = false): self - { - $this->setAttribute('exists', $act); - return $this; - } - /** * Get the table constructs based on the provided table definition callback or configuration. * diff --git a/src/Operations/OperationsTrait.php b/src/Operations/OperationsTrait.php index d4eb9fe..3932ca5 100644 --- a/src/Operations/OperationsTrait.php +++ b/src/Operations/OperationsTrait.php @@ -312,5 +312,36 @@ public function whereTable(string $table) return $this; } - + /** + * Specify whether the table should only be created if it does not already exist. + * + * @param bool $act Whether to create the table only if it doesn't exist. + * + * @return self + */ + public function exists(bool $act = false): self + { + $this->setAttribute('exists', $act); + return $this; + } + + /** + * Specify whether the table should only be created if exist. + * + * @return self + */ + public function ifExists(): self + { + return $this->exists(true); + } + + /** + * Specify whether the table should only be created if it does not already exist. + * + * @return self + */ + public function ifNotExists(): self + { + return $this->exists(false); + } }