1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Modules\Entitizer\Utils { |
4
|
|
|
|
5
|
|
|
use DB; |
6
|
|
|
|
7
|
|
|
abstract class Definition { |
8
|
|
|
|
9
|
|
|
protected $params = null, $indexes = null, $foreigns = null; |
10
|
|
|
|
11
|
|
|
# Get list of statements |
12
|
|
|
|
13
|
|
|
private function getStatements() { |
14
|
|
|
|
15
|
|
|
$statements = []; |
16
|
|
|
|
17
|
|
|
foreach ([$this->params, $this->indexes, $this->foreigns] as $group) { |
18
|
|
|
|
19
|
|
|
foreach ($group->list() as $item) $statements[] = $item->statement(); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
# ------------------------ |
23
|
|
|
|
24
|
|
|
return $statements; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
# Create main table |
28
|
|
|
|
29
|
|
|
private function createMainTable() { |
30
|
|
|
|
31
|
|
|
$query = ("CREATE TABLE IF NOT EXISTS `" . static::$table . "` (") . |
32
|
|
|
|
33
|
|
|
implode(", ", $this->getStatements()) . |
34
|
|
|
|
35
|
|
|
(") ENGINE=InnoDB DEFAULT CHARSET=utf8"); |
36
|
|
|
|
37
|
|
|
# ------------------------ |
38
|
|
|
|
39
|
|
|
return (DB::send($query) && DB::getLast()->status); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
# Create relations table |
43
|
|
|
|
44
|
|
|
private function createRelationsTable() { |
45
|
|
|
|
46
|
|
|
$query = ("CREATE TABLE IF NOT EXISTS `" . static::$table_relations . "` (") . |
47
|
|
|
|
48
|
|
|
("`ancestor` int(10) UNSIGNED NOT NULL, `descendant` int(10) UNSIGNED NOT NULL, ") . |
49
|
|
|
|
50
|
|
|
("`depth` int(10) UNSIGNED NOT NULL, ") . |
51
|
|
|
|
52
|
|
|
("PRIMARY KEY (`ancestor`, `descendant`), KEY (`ancestor`), KEY (`descendant`), KEY (`depth`), ") . |
53
|
|
|
|
54
|
|
|
("FOREIGN KEY (`ancestor`) REFERENCES `" . static::$table . "` (`id`) ON DELETE CASCADE, ") . |
55
|
|
|
|
56
|
|
|
("FOREIGN KEY (`descendant`) REFERENCES `" . static::$table . "` (`id`) ON DELETE CASCADE ") . |
57
|
|
|
|
58
|
|
|
(") ENGINE=InnoDB DEFAULT CHARSET=utf8"); |
59
|
|
|
|
60
|
|
|
# ------------------------ |
61
|
|
|
|
62
|
|
|
return (DB::send($query) && DB::getLast()->status); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
# Drop main table |
66
|
|
|
|
67
|
|
|
private function removeMainTable() { |
68
|
|
|
|
69
|
|
|
$query = ("DROP TABLE IF EXISTS `" . static::$table . "`"); |
70
|
|
|
|
71
|
|
|
return (DB::send($query) && DB::getLast()->status); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
# Drop relations table |
75
|
|
|
|
76
|
|
|
private function removeRelationsTable() { |
77
|
|
|
|
78
|
|
|
$query = ("DROP TABLE IF EXISTS `" . static::$table_relations . "`"); |
79
|
|
|
|
80
|
|
|
return (DB::send($query) && DB::getLast()->status); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
# Constructor |
84
|
|
|
|
85
|
|
|
public function __construct() { |
86
|
|
|
|
87
|
|
|
$this->params = new Definition\Group\Params($this, static::$auto_increment); |
88
|
|
|
|
89
|
|
|
$this->indexes = new Definition\Group\Indexes($this); |
90
|
|
|
|
91
|
|
|
$this->foreigns = new Definition\Group\Foreigns($this); |
92
|
|
|
|
93
|
|
|
# Define specific params |
94
|
|
|
|
95
|
|
|
$this->define(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
# Return params |
99
|
|
|
|
100
|
|
|
public function params() { |
101
|
|
|
|
102
|
|
|
return $this->params->list(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
# Return secure params |
106
|
|
|
|
107
|
|
|
public function paramsSecure() { |
108
|
|
|
|
109
|
|
|
return $this->params->secure(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
# Return param by name |
113
|
|
|
|
114
|
|
|
public function param(string $name) { |
115
|
|
|
|
116
|
|
|
return $this->params->get($name); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
# Return indexes |
120
|
|
|
|
121
|
|
|
public function indexes() { |
122
|
|
|
|
123
|
|
|
return $this->indexes->list(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
# Return index by name |
127
|
|
|
|
128
|
|
|
public function index(string $name) { |
129
|
|
|
|
130
|
|
|
return $this->indexes->get($name); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
# Return foreigns |
134
|
|
|
|
135
|
|
|
public function foreigns() { |
136
|
|
|
|
137
|
|
|
return $this->foreigns->list(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
# Return foreign by name |
141
|
|
|
|
142
|
|
|
public function foreign(string $name) { |
143
|
|
|
|
144
|
|
|
return $this->foreigns->get($name); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
# Create tables |
148
|
|
|
|
149
|
|
|
public function createTables() { |
150
|
|
|
|
151
|
|
|
return ($this->createMainTable() && (!static::$nesting || $this->createRelationsTable())); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
# Remove tables |
155
|
|
|
|
156
|
|
|
public function removeTables() { |
157
|
|
|
|
158
|
|
|
return ((!static::$nesting || $this->removeRelationsTable()) && $this->removeMainTable()); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|