db

Форк
0
/
AlterTable.php 
297 строк · 7.4 Кб
1
<?php
2

3
namespace Upside\Db\Schema;
4

5
class AlterTable
6
{
7
    protected string $table;
8
    protected array $commands = [];
9

10
    public function __construct(string $table)
11
    {
12
        $this->table = $table;
13
    }
14

15
    protected function add_command(string $name, $data): static
16
    {
17
        $this->commands[] = [
18
            'type' => $name,
19
            'data' => $data,
20
        ];
21

22
        return $this;
23
    }
24

25
    protected function add_key(string $type, array|string $columns, ?string $name = null): static
26
    {
27
        static $map = [
28
            'add_primary' => 'pk',
29
            'add_unique' => 'uk',
30
            'add_foreign_key' => 'fk',
31
            'add_index' => 'ik',
32
        ];
33

34
        if (!\is_array($columns)) {
35
            $columns = [$columns];
36
        }
37

38
        if ($name === null) {
39
            $name = $this->table . '_' . $map[$type] . '_' . \implode('_', $columns);
40
        }
41

42
        return $this->add_command($type, [
43
            'name' => $name,
44
            'columns' => $columns,
45
        ]);
46
    }
47

48
    protected function add_column(string $name, string $type): AlterColumn
49
    {
50
        $column_object = new AlterColumn($this, $name, $type);
51
        $this->add_command('add_column', $column_object);
52

53
        return $column_object;
54
    }
55

56
    protected function modify_column(string $column, string $type): AlterColumn
57
    {
58
        $column_object = new AlterColumn($this, $column, $type);
59
        $column_object->set('handle_default', false);
60
        $this->add_command('modify_column', $column_object);
61

62
        return $column_object;
63
    }
64

65
    public function get_table_name(): string
66
    {
67
        return $this->table;
68
    }
69

70
    public function get_commands(): array
71
    {
72
        return $this->commands;
73
    }
74

75
    public function drop_index(string $name): static
76
    {
77
        return $this->add_command('drop_index', $name);
78
    }
79

80
    public function drop_unique(string $name): static
81
    {
82
        return $this->add_command('drop_unique_key', $name);
83
    }
84

85
    public function drop_primary(string $name): static
86
    {
87
        return $this->add_command('drop_primary_key', $name);
88
    }
89

90
    public function drop_foreign(string $name): static
91
    {
92
        return $this->add_command('drop_foreign_key', $name);
93
    }
94

95
    public function drop_column(string $name): static
96
    {
97
        return $this->add_command('drop_column', $name);
98
    }
99

100
    public function drop_default_value(string $column): static
101
    {
102
        return $this->add_command('drop_default_vValue', $column);
103
    }
104

105
    public function rename_column(string $from, string $to): static
106
    {
107
        return $this->add_command('rename_column', [
108
            'from' => $from,
109
            'column' => new AlterColumn($this, $to),
110
        ]);
111
    }
112

113
    public function primary(array|string $columns, ?string $name = null): static
114
    {
115
        return $this->add_key('add_primary', $columns, $name);
116
    }
117

118
    public function unique(array|string $columns, ?string $name = null): static
119
    {
120
        return $this->add_key('add_unique', $columns, $name);
121
    }
122

123
    public function index(array|string $columns, ?string $name = null): static
124
    {
125
        return $this->add_key('add_index', $columns, $name);
126
    }
127

128
    public function foreign(array|string $columns, ?string $name = null): ForeignKey
129
    {
130
        if (!\is_array($columns)) {
131
            $columns = [$columns];
132
        }
133

134
        if ($name === null) {
135
            $name = $this->table . '_fk_' . \implode('_', $columns);
136
        }
137

138
        $foreign = new ForeignKey($columns);
139

140
        $this->add_command('add_foreign', [
141
            'name' => $name,
142
            'foreign' => $foreign,
143
        ]);
144

145
        return $foreign;
146
    }
147

148
    public function set_default_value(string $column, $value): static
149
    {
150
        return $this->add_command('set_default_value', [
151
            'column' => $column,
152
            'value' => $value,
153
        ]);
154
    }
155

156
    public function integer(string $name): AlterColumn
157
    {
158
        return $this->add_column($name, 'integer');
159
    }
160

161
    public function float(string $name): AlterColumn
162
    {
163
        return $this->add_column($name, 'float');
164
    }
165

166
    public function double(string $name): AlterColumn
167
    {
168
        return $this->add_column($name, 'double');
169
    }
170

171
    public function decimal(string $name, ?int $length = null, ?int $precision = null): AlterColumn
172
    {
173
        return $this->add_column($name, 'decimal')
174
            ->set('length', $length)
175
            ->set('precision', $precision);
176
    }
177

178
    public function boolean(string $name): AlterColumn
179
    {
180
        return $this->add_column($name, 'boolean');
181
    }
182

183
    public function binary(string $name): AlterColumn
184
    {
185
        return $this->add_column($name, 'binary');
186
    }
187

188
    public function string(string $name, int $length = 255): AlterColumn
189
    {
190
        return $this
191
            ->add_column($name, 'string')
192
            ->set('length', $length);
193
    }
194

195
    public function fixed(string $name, int $length = 255): AlterColumn
196
    {
197
        return $this
198
            ->add_column($name, 'fixed')
199
            ->set('length', $length);
200
    }
201

202
    public function text(string $name): AlterColumn
203
    {
204
        return $this->add_column($name, 'text');
205
    }
206

207
    public function time(string $name): AlterColumn
208
    {
209
        return $this->add_column($name, 'time');
210
    }
211

212
    public function timestamp(string $name): AlterColumn
213
    {
214
        return $this->add_column($name, 'timestamp');
215
    }
216

217
    public function date(string $name): AlterColumn
218
    {
219
        return $this->add_column($name, 'date');
220
    }
221

222
    public function date_time(string $name): AlterColumn
223
    {
224
        return $this->add_column($name, 'datetime');
225
    }
226

227
    public function to_integer(string $name): AlterColumn
228
    {
229
        return $this->modify_column($name, 'integer');
230
    }
231

232
    public function to_float(string $name): AlterColumn
233
    {
234
        return $this->modify_column($name, 'float');
235
    }
236

237
    public function to_double(string $name): AlterColumn
238
    {
239
        return $this->modify_column($name, 'double');
240
    }
241

242
    public function to_decimal(string $name, ?int $length = null, ?int $precision = null): AlterColumn
243
    {
244
        return $this->modify_column($name, 'decimal')
245
            ->set('length', $length)
246
            ->set('precision', $precision);
247
    }
248

249
    public function to_boolean(string $name): AlterColumn
250
    {
251
        return $this->modify_column($name, 'boolean');
252
    }
253

254
    public function to_binary(string $name): AlterColumn
255
    {
256
        return $this->modify_column($name, 'binary');
257
    }
258

259
    public function to_string(string $name, int $length = 255): AlterColumn
260
    {
261
        return $this
262
            ->modify_column($name, 'string')
263
            ->set('length', $length);
264
    }
265

266
    public function to_fixed(string $name, int $length = 255): AlterColumn
267
    {
268
        return $this
269
            ->modify_column($name, 'fixed')
270
            ->set('length', $length);
271
    }
272

273
    public function to_text(string $name): AlterColumn
274
    {
275
        return $this->modify_column($name, 'text');
276
    }
277

278
    public function to_time(string $name): AlterColumn
279
    {
280
        return $this->modify_column($name, 'time');
281
    }
282

283
    public function to_timestamp(string $name): AlterColumn
284
    {
285
        return $this->modify_column($name, 'timestamp');
286
    }
287

288
    public function to_date(string $name): AlterColumn
289
    {
290
        return $this->modify_column($name, 'date');
291
    }
292

293
    public function to_date_time(string $name): AlterColumn
294
    {
295
        return $this->modify_column($name, 'datetime');
296
    }
297
}
298

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.