3
namespace Upside\Db\Schema;
7
protected string $table;
8
protected array $commands = [];
10
public function __construct(string $table)
12
$this->table = $table;
15
protected function add_command(string $name, $data): static
25
protected function add_key(string $type, array|string $columns, ?string $name = null): static
28
'add_primary' => 'pk',
30
'add_foreign_key' => 'fk',
34
if (!\is_array($columns)) {
35
$columns = [$columns];
39
$name = $this->table . '_' . $map[$type] . '_' . \implode('_', $columns);
42
return $this->add_command($type, [
44
'columns' => $columns,
48
protected function add_column(string $name, string $type): AlterColumn
50
$column_object = new AlterColumn($this, $name, $type);
51
$this->add_command('add_column', $column_object);
53
return $column_object;
56
protected function modify_column(string $column, string $type): AlterColumn
58
$column_object = new AlterColumn($this, $column, $type);
59
$column_object->set('handle_default', false);
60
$this->add_command('modify_column', $column_object);
62
return $column_object;
65
public function get_table_name(): string
70
public function get_commands(): array
72
return $this->commands;
75
public function drop_index(string $name): static
77
return $this->add_command('drop_index', $name);
80
public function drop_unique(string $name): static
82
return $this->add_command('drop_unique_key', $name);
85
public function drop_primary(string $name): static
87
return $this->add_command('drop_primary_key', $name);
90
public function drop_foreign(string $name): static
92
return $this->add_command('drop_foreign_key', $name);
95
public function drop_column(string $name): static
97
return $this->add_command('drop_column', $name);
100
public function drop_default_value(string $column): static
102
return $this->add_command('drop_default_vValue', $column);
105
public function rename_column(string $from, string $to): static
107
return $this->add_command('rename_column', [
109
'column' => new AlterColumn($this, $to),
113
public function primary(array|string $columns, ?string $name = null): static
115
return $this->add_key('add_primary', $columns, $name);
118
public function unique(array|string $columns, ?string $name = null): static
120
return $this->add_key('add_unique', $columns, $name);
123
public function index(array|string $columns, ?string $name = null): static
125
return $this->add_key('add_index', $columns, $name);
128
public function foreign(array|string $columns, ?string $name = null): ForeignKey
130
if (!\is_array($columns)) {
131
$columns = [$columns];
134
if ($name === null) {
135
$name = $this->table . '_fk_' . \implode('_', $columns);
138
$foreign = new ForeignKey($columns);
140
$this->add_command('add_foreign', [
142
'foreign' => $foreign,
148
public function set_default_value(string $column, $value): static
150
return $this->add_command('set_default_value', [
156
public function integer(string $name): AlterColumn
158
return $this->add_column($name, 'integer');
161
public function float(string $name): AlterColumn
163
return $this->add_column($name, 'float');
166
public function double(string $name): AlterColumn
168
return $this->add_column($name, 'double');
171
public function decimal(string $name, ?int $length = null, ?int $precision = null): AlterColumn
173
return $this->add_column($name, 'decimal')
174
->set('length', $length)
175
->set('precision', $precision);
178
public function boolean(string $name): AlterColumn
180
return $this->add_column($name, 'boolean');
183
public function binary(string $name): AlterColumn
185
return $this->add_column($name, 'binary');
188
public function string(string $name, int $length = 255): AlterColumn
191
->add_column($name, 'string')
192
->set('length', $length);
195
public function fixed(string $name, int $length = 255): AlterColumn
198
->add_column($name, 'fixed')
199
->set('length', $length);
202
public function text(string $name): AlterColumn
204
return $this->add_column($name, 'text');
207
public function time(string $name): AlterColumn
209
return $this->add_column($name, 'time');
212
public function timestamp(string $name): AlterColumn
214
return $this->add_column($name, 'timestamp');
217
public function date(string $name): AlterColumn
219
return $this->add_column($name, 'date');
222
public function date_time(string $name): AlterColumn
224
return $this->add_column($name, 'datetime');
227
public function to_integer(string $name): AlterColumn
229
return $this->modify_column($name, 'integer');
232
public function to_float(string $name): AlterColumn
234
return $this->modify_column($name, 'float');
237
public function to_double(string $name): AlterColumn
239
return $this->modify_column($name, 'double');
242
public function to_decimal(string $name, ?int $length = null, ?int $precision = null): AlterColumn
244
return $this->modify_column($name, 'decimal')
245
->set('length', $length)
246
->set('precision', $precision);
249
public function to_boolean(string $name): AlterColumn
251
return $this->modify_column($name, 'boolean');
254
public function to_binary(string $name): AlterColumn
256
return $this->modify_column($name, 'binary');
259
public function to_string(string $name, int $length = 255): AlterColumn
262
->modify_column($name, 'string')
263
->set('length', $length);
266
public function to_fixed(string $name, int $length = 255): AlterColumn
269
->modify_column($name, 'fixed')
270
->set('length', $length);
273
public function to_text(string $name): AlterColumn
275
return $this->modify_column($name, 'text');
278
public function to_time(string $name): AlterColumn
280
return $this->modify_column($name, 'time');
283
public function to_timestamp(string $name): AlterColumn
285
return $this->modify_column($name, 'timestamp');
288
public function to_date(string $name): AlterColumn
290
return $this->modify_column($name, 'date');
293
public function to_date_time(string $name): AlterColumn
295
return $this->modify_column($name, 'datetime');