-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSchema.php
357 lines (313 loc) · 8.7 KB
/
Schema.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<?php
namespace PluginMaster\Schema;
use Closure;
use PluginMaster\Contracts\Schema\SchemaInterface;
class Schema implements SchemaInterface
{
private string $sql;
private string $table = '';
private string $column = '';
private array $columns = [];
private bool $nullable = false;
private bool $unsigned = false;
private bool $onUpdateTimeStamp = false;
private string $defaultValue = '';
private string $currentColumn = '';
private bool $primaryKey = false;
private bool $increment = false;
private string $foreignData = '';
private string $tablePrefix;
private bool $create = false;
public function __construct()
{
global $tablePrefix;
$this->tablePrefix = $tablePrefix;
}
/**
* @param string $table
* @param Closure $closure
* @return Schema
*/
public static function create(string $table, Closure $closure): self
{
$self = new self();
$self->sql = 'create table';
$self->table = $self->tablePrefix.$table;
$self->create = true;
if ($closure instanceof Closure) {
call_user_func($closure, $self);
}
$self->sql .= " `".$self->table."`( ".implode(', ', $self->columns).")";
return $self;
}
/**
* @param string $sql
* @return Schema
*/
public static function rawSql(string $sql): self
{
$self = new self();
$self->sql = $sql;
return $self;
}
/**
* @param string $column
* @param string|int $length
* @param string|int $places
* @return Schema
*/
public function decimal(string $column, int $length = 20, int $places = 2): self
{
$this->currentColumn = "`".$column."` decimal(".$length.",".$places.")";
$this->addColumn($this->currentColumn);
return $this;
}
/**
* @param $columnData
* @return Schema
*/
private function addColumn($columnData): self
{
$this->nullable = false;
$this->primaryKey = false;
$this->increment = false;
$this->unsigned = false;
$this->onUpdateTimeStamp = false;
$this->foreignData = '';
$this->column = $columnData.($this->defaultValue ? ' DEFAULT "'.$this->defaultValue.'"' : '').($this->nullable ? ' NULL' : ' NOT NULL');
$this->columns[] = $this->column;
return $this;
}
/**
* @param string $column
* @param array $values
* @return Schema
*/
public function enum(string $column, array $values): self
{
$enumValues = '';
foreach ($values as $k => $v) {
$enumValues .= '"'.$v.'",';
}
$this->currentColumn = "`".$column."` enum(".substr($enumValues, 0, -1).")";
$this->addColumn($this->currentColumn);
return $this;
}
/**
* @param string $column
* @return Schema
*/
public function intIncrements(string $column): self
{
$this->integer($column);
$this->increment();
$this->unsigned();
$this->primaryKey();
return $this;
}
/**
* @param string $column
* @param int $length
* @return Schema
*/
public function integer(string $column, int $length = 10): self
{
$this->currentColumn = "`".$column."` int(".$length.")";
$this->addColumn($this->currentColumn);
return $this;
}
/**
* @return Schema
*/
public function increment(): self
{
$this->increment = true;
$this->updateColumn($this->currentColumn);
return $this;
}
/**
* @param $columnData
* @return Schema
*/
public function updateColumn($columnData): self
{
$defaultValue = ($this->defaultValue ? ' DEFAULT '.(strtoupper(
$this->defaultValue
) === 'CURRENT_TIMESTAMP' ? strtoupper($this->defaultValue) : '"'.$this->defaultValue.'"') : '');
$unsigned = ($this->unsigned ? ' UNSIGNED ' : '');
$nullable = ($this->nullable ? ' NULL' : ' NOT NULL ');
$increment = ($this->increment ? ' auto_increment ' : '');
$primaryKey = ($this->primaryKey ? ' PRIMARY KEY' : '');
$onUpdateTimeStamp = ($this->onUpdateTimeStamp ? ' ON UPDATE CURRENT_TIMESTAMP' : '');
$foreignData = ($this->foreignData ? $this->foreignData : '');
$this->column = $columnData.$unsigned.$nullable.$defaultValue.$increment.$primaryKey.$onUpdateTimeStamp.$foreignData;
$lastIndex = count($this->columns) - 1;
$this->columns[$lastIndex] = $this->column;
return $this;
}
/**
* @return Schema
*/
public function unsigned(): self
{
$this->unsigned = true;
$this->updateColumn($this->currentColumn);
return $this;
}
/**
* @return Schema
*/
public function primaryKey(): self
{
$this->primaryKey = true;
$this->updateColumn($this->currentColumn);
return $this;
}
/**
* @param string $column
* @return Schema
*/
public function bigIntIncrements(string $column): self
{
$this->bigInt($column);
$this->increment();
$this->unsigned();
$this->primaryKey();
return $this;
}
/**
* @param string $column
* @param string|int $length
* @return Schema
*/
public function bigInt(string $column, int $length = 20): self
{
$this->currentColumn = "`".$column."` bigint(".$length.")";
$this->addColumn($this->currentColumn);
return $this;
}
/**
* @param string $column
* @param int $length
* @return Schema
*/
public function string(string $column, int $length = 255): self
{
$this->currentColumn = "`".$column."` varchar(".$length.")";
$this->addColumn($this->currentColumn);
return $this;
}
/**
* @param string $column
* @return Schema
*/
public function text(string $column): self
{
$this->currentColumn = "`".$column."` text";
$this->addColumn($this->currentColumn);
return $this;
}
/**
* @param string $column
* @return Schema
*/
public function date(string $column): self
{
$this->currentColumn = "`".$column."` date";
$this->addColumn($this->currentColumn);
return $this;
}
/**
* @param string $column
* @return Schema
*/
public function timestamp(string $column): self
{
$this->currentColumn = "`".$column."` timestamp";
$this->addColumn($this->currentColumn);
return $this;
}
/**
* @return Schema
*/
public function nullable(): self
{
$this->nullable = true;
$this->updateColumn($this->currentColumn);
return $this;
}
/**
* @param mixed $value
* @return Schema
*/
public function default($value): self
{
$this->defaultValue = $value;
$this->updateColumn($this->currentColumn);
return $this;
}
/**
* @return Schema
*/
public function onUpdateTimeStamp(): self
{
$this->onUpdateTimeStamp = true;
$this->updateColumn($this->currentColumn);
return $this;
}
/**
* @param string $column
* @return Schema
*/
public function foreign(string $column): self
{
$this->foreignData = ', CONSTRAINT '.$this->table.'_'.$column." FOREIGN KEY (`".$column."`) ";
return $this;
}
/**
* @param string $reference
* @return Schema
*/
public function on(string $reference): self
{
$data = explode('.', $reference);
$this->foreignData .= "REFERENCES `".$this->tablePrefix.$data[0]."` (`".$data[1]."`) ";
$this->updateColumn($this->currentColumn);
return $this;
}
/**
* process sql for execution
*/
public function execute(): void
{
global $wpdb;
$table = $this->table;
$charset = $wpdb->get_charset_collate();
$finalSql = '';
if ($this->create && $table && ($wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $table)) !== $table)) {
$finalSql = $this->getSql().$charset;
}
if (!$this->create) {
$finalSql = $this->getSql().$charset;
}
if ($finalSql) {
$this->executeSQL($finalSql);
}
}
/**
* @return mixed
*/
public function getSql(): string
{
return $this->sql;
}
/**
* finally execute sql query
* @param $sql
*/
private function executeSQL($sql): void
{
require_once(ABSPATH.'/wp-admin/includes/upgrade.php');
dbDelta($sql);
}
}