Skip to content

Commit

Permalink
add if/if not exists to create table
Browse files Browse the repository at this point in the history
  • Loading branch information
BMTmohammedtaha committed Sep 27, 2023
1 parent 21609ec commit 1b865a9
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 15 deletions.
13 changes: 13 additions & 0 deletions src/Build/QueryBuilderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
3 changes: 2 additions & 1 deletion src/Build/TableQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
13 changes: 0 additions & 13 deletions src/Operations/CreateTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
33 changes: 32 additions & 1 deletion src/Operations/OperationsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 1b865a9

Please sign in to comment.