-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from DasunNethsara-04/main
Database Table Constraints implemented
- Loading branch information
Showing
3 changed files
with
170 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
namespace ZenithPHP\Core\Database; | ||
|
||
class Column | ||
{ | ||
protected string $name; | ||
protected string $type; | ||
protected array $modifiers = []; | ||
|
||
public function __construct(string $name, string $type) | ||
{ | ||
$this->name = $name; | ||
$this->type = $type; | ||
} | ||
|
||
public function unique(): self | ||
{ | ||
$this->modifiers[] = 'UNIQUE'; | ||
return $this; | ||
} | ||
|
||
public function default($value): self | ||
{ | ||
$this->modifiers[] = "DEFAULT $value"; | ||
return $this; | ||
} | ||
|
||
public function nullable(): self | ||
{ | ||
$this->modifiers[] = 'NULL'; | ||
return $this; | ||
} | ||
|
||
public function notNullable(): self | ||
{ | ||
$this->modifiers[] = 'NOT NULL'; | ||
return $this; | ||
} | ||
|
||
public function constrained(string $table): self | ||
{ | ||
$this->modifiers[] = "REFERENCES `$table` (`id`)"; | ||
return $this; | ||
} | ||
|
||
public function cascadeOnDelete(): self | ||
{ | ||
$this->modifiers[] = 'ON DELETE CASCADE'; | ||
return $this; | ||
} | ||
|
||
public function cascadeOnUpdate(): self | ||
{ | ||
$this->modifiers[] = 'ON UPDATE CASCADE'; | ||
return $this; | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
$modifiers = $this->modifiers; | ||
|
||
// Check if the column has a REFERENCES constraint before adding ON DELETE or ON UPDATE | ||
if (!in_array('REFERENCES', array_map(fn($mod) => strtok($mod, ' '), $modifiers))) { | ||
// Exclude cascade if it's not a foreign key constraint | ||
$modifiers = array_filter($modifiers, fn($mod) => !str_contains($mod, 'ON DELETE') && !str_contains($mod, 'ON UPDATE')); | ||
} | ||
return "`{$this->name}` {$this->type} " . implode(' ', $modifiers); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace ZenithPHP\Migrations; | ||
|
||
use ZenithPHP\Core\Database\Migration; | ||
use ZenithPHP\Core\Database\Schema; | ||
|
||
class _2024_10_28_093729_user extends Migration | ||
{ | ||
public function up() | ||
{ | ||
Schema::create('user', function (Schema $table) { | ||
$table->id(); | ||
$table->string('name', 50); | ||
$table->string('email', 100)->unique(); | ||
$table->string('password'); | ||
$table->boolean('is_active')->default(1); | ||
$table->text('bio')->nullable(); | ||
$table->timestamp('created_at')->default('CURRENT_TIMESTAMP'); | ||
$table->timestamp('updated_at')->default('CURRENT_TIMESTAMP')->cascadeOnUpdate(); | ||
}); | ||
} | ||
|
||
public function down() | ||
{ | ||
Schema::drop('user'); | ||
} | ||
} |