Skip to content

Commit

Permalink
add after method
Browse files Browse the repository at this point in the history
  • Loading branch information
BMTmohammedtaha committed Sep 28, 2023
1 parent 1b865a9 commit 2f05651
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 15 deletions.
33 changes: 23 additions & 10 deletions src/Build/ColumnQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public function constraints(): string
$result[] = $this->visible();
}


return join(' ', $result);
}

Expand All @@ -168,12 +168,11 @@ public function nullable()
*/
public function autoIncrement()
{
return (string) match($this->syntax->getDriver()){
Driver::MySQL => $this->syntax->getKey('auto_increment', 2) ,
Driver::PostgreSQL => $this->syntax->getKey('auto_increment', 2) ,
return (string) match ($this->syntax->getDriver()) {
Driver::MySQL => $this->syntax->getKey('auto_increment', 2),
Driver::PostgreSQL => $this->syntax->getKey('auto_increment', 2),
Driver::SQLite => $this->syntax->getKey('auto_increment', 2),
};

}

/**
Expand Down Expand Up @@ -259,13 +258,15 @@ public function visible(): string
*/
public function check(): string
{

if (!$this->hasAttribute('check')) {
return '';
}
$exprs = $this->getAttribute('check');
$check_sort = $this->getAttribute('check_sort') ?? [];

$result= $this->checkQuery($this->columnName(),$exprs,$check_sort);
$result = $this->checkQuery($this->columnName(), $exprs, $check_sort);

return $this->syntax->getCommand('check',2) . sprintf(
return $this->syntax->getCommand('check', 2) . sprintf(
'(%s)',
$result
);
Expand Down Expand Up @@ -315,6 +316,17 @@ public function getValueByKey($attribute, $key): mixed
return null;
}

public function afterColumn(): string
{
if ($this->syntax->getDriver() === Driver::SQLite) {
return '';
}
if (!$this->hasAttribute('after_column')) {
return '';
}
return $this->syntax->getCommand('after', 1) . $this->getAttribute('after_column');
}

/**
* Build the SQL column-related query.
*
Expand All @@ -323,14 +335,15 @@ public function getValueByKey($attribute, $key): mixed
public function build(): string
{
return sprintf(
'%s %s%s %s %s %s %s',
'%s %s%s %s %s %s %s %s',
$this->columnName(),
$this->set(),
$this->dataType(),
$this->size(),
$this->nullable(),
$this->constraints(),
$this->check()
$this->check(),
$this->afterColumn()
);
}

Expand Down
16 changes: 11 additions & 5 deletions src/Structure/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,22 +210,22 @@ public function defaultCurrentTime(): self
* @param mixed $expr The expression for the CHECK constraint.
* @return self
*/
public function check($expr,string $sort = 'and'): self
public function check($expr, string $sort = 'and'): self
{
$this->addToAttribute('check' , $expr);
$this->addToAttribute('check_sort' , $sort);
$this->addToAttribute('check', $expr);
$this->addToAttribute('check_sort', $sort);
return $this;
}

/**
/**
* Add a CHECK constraint with next CHECK constraint 'OR' to the column.
*
* @param mixed $expr The expression for the CHECK constraint.
* @return self
*/
public function checkOr($expr): self
{
return $this->check($expr,'or');
return $this->check($expr, 'or');
}

/**
Expand Down Expand Up @@ -380,6 +380,12 @@ public function storageMemory(): self
return $this->storage('memory');
}

public function afterColumn(string $column): self
{
$this->setAttribute('after_column', $column);
return $this;
}

/**
* Get the SQL query for the column modification.
*
Expand Down
1 change: 1 addition & 0 deletions src/Structure/TableTrait/TableModifyTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,5 @@ public function addColumn(string $column_name,callable $definition)
$this->setAttribute('add_column',$col->getAttributes());
return $this;
}

}
1 change: 1 addition & 0 deletions src/Syntax.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public function commands(): array
return [
"abs" => "ABS",
"add" => "ADD",
"after" => "AFTER",
"all" => "ALL",
"alter" => "ALTER",
"alterDatabase" => "ALTER DATABASE",
Expand Down
14 changes: 14 additions & 0 deletions src/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,20 @@ public function charset($charset): self
return $this;
}

/**
* Set the column after this column.
*
* @param mixed $column_name you want your column be after.
* @return self
*/
public function after(string $column_name): self
{
$col = new ColumnDestruct($this->getLastColumn());
$col->afterColumn($column_name);
$this->modifyColumn($col);
return $this;
}

/**
* Get the SQL query for the table modification.
*
Expand Down

0 comments on commit 2f05651

Please sign in to comment.