Skip to content

Commit

Permalink
Add SchemaBuilder::add_column, add_date
Browse files Browse the repository at this point in the history
  • Loading branch information
olvlvl committed Mar 3, 2023
1 parent 57ff4a3 commit a02bdb0
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
42 changes: 42 additions & 0 deletions lib/ActiveRecord/SchemaBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,35 @@ public function build(): Schema
return $schema;
}

public function add_column(
string $col_name,
string $type,
string|int|null $size = null,
bool $unsigned = false,
bool $null = false,
mixed $default = null,
bool $auto_increment = false,
bool $unique = false,
bool $primary = false,
?string $comment = null,
?string $collate = null,
): self {
$this->columns[$col_name] = new SchemaColumn(
type: $type,
size: $size,
unsigned: $unsigned,
null: $null,
default: $default,
auto_increment: $auto_increment,
unique: $unique,
primary: $primary,
comment: $comment,
collate: $collate,
);

return $this;
}

public function add_boolean(
string $col_name,
bool $null = false,
Expand Down Expand Up @@ -104,6 +133,19 @@ public function add_foreign(
return $this;
}

public function add_date(
string $col_name,
bool $null = false,
?string $default = null,
): self {
$this->columns[$col_name] = SchemaColumn::date(
null: $null,
default: $default,
);

return $this;
}

public function add_datetime(
string $col_name,
bool $null = false,
Expand Down
14 changes: 14 additions & 0 deletions lib/ActiveRecord/SchemaColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public static function __set_state(array $an_array): self
public const TYPE_BLOB = 'BLOB';
public const TYPE_BOOLEAN = 'BOOLEAN';
public const TYPE_CHAR = 'CHAR';
public const TYPE_DATE = 'DATE';
public const TYPE_DATETIME = 'DATETIME';
public const TYPE_FLOAT = 'FLOAT';
public const TYPE_INT = 'INT';
Expand Down Expand Up @@ -165,6 +166,19 @@ public static function foreign(
* https://dev.mysql.com/doc/refman/8.0/en/datetime.html
*/

public static function date(
bool $null = false,
?string $default = null,
): self {
self::assert_datetime_default($default);

return new self(
type: self::TYPE_DATE,
null: $null,
default: $default,
);
}

public static function datetime(
bool $null = false,
?string $default = null,
Expand Down

0 comments on commit a02bdb0

Please sign in to comment.