Total Complexity | 14 |
Total Lines | 78 |
Duplicated Lines | 0 % |
Coverage | 0% |
Changes | 0 |
1 | <?php |
||
5 | abstract class Schema extends ActiveRecord |
||
6 | { |
||
7 | abstract public function up(); |
||
8 | abstract public function down(); |
||
9 | |||
10 | public function __construct() |
||
11 | { |
||
12 | $this->table = new Table(); |
||
13 | } |
||
14 | |||
15 | public $sql = ''; |
||
16 | |||
17 | private $table; |
||
18 | |||
19 | public function create(string $name, $callback) |
||
20 | { |
||
21 | call_user_func_array($callback, [$this->table]); |
||
22 | |||
23 | $q = "CREATE TABLE $name ({$this->getColumns()}"; |
||
24 | $q .= $this->pk().$this->fk().$this->onUpdate().$this->onDelete(); |
||
25 | $q .= "\n) ENGINE=INNODB;"; |
||
26 | |||
27 | $this->sql = $q; |
||
28 | } |
||
29 | |||
30 | private function pk() |
||
31 | { |
||
32 | return (!empty($pk = $this->table->getPk())) |
||
|
|||
33 | ? $this->sql .= ",\n\tPRIMARY KEY ($pk)" |
||
34 | : ""; |
||
35 | } |
||
36 | |||
37 | private function fk() |
||
38 | { |
||
39 | return (!empty($fk = $this->table->getFk())) |
||
40 | ? ",\n\t$fk {$this->table->getReferences()}" |
||
41 | : ""; |
||
42 | } |
||
43 | |||
44 | private function onUpdate() |
||
45 | { |
||
46 | return (!empty($on = $this->table->getOnupdate())) |
||
47 | ? " $on" |
||
48 | : ""; |
||
49 | } |
||
50 | |||
51 | private function onDelete() |
||
56 | } |
||
57 | |||
58 | private function getColumns() |
||
77 | } |
||
78 | |||
79 | public function run() |
||
83 | } |
||
84 | } |
||
85 |