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::last()->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::last()->status); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
# Constructor |
66
|
|
|
|
67
|
|
|
public function __construct() { |
68
|
|
|
|
69
|
|
|
$this->params = new Definition\Group\Params($this, static::$auto_increment); |
70
|
|
|
|
71
|
|
|
$this->indexes = new Definition\Group\Indexes($this); |
72
|
|
|
|
73
|
|
|
$this->foreigns = new Definition\Group\Foreigns($this); |
74
|
|
|
|
75
|
|
|
# Define specific params |
76
|
|
|
|
77
|
|
|
$this->define(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
# Return params |
81
|
|
|
|
82
|
|
|
public function params() { |
83
|
|
|
|
84
|
|
|
return $this->params->list(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
# Return secure params |
88
|
|
|
|
89
|
|
|
public function paramsSecure() { |
90
|
|
|
|
91
|
|
|
return $this->params->secure(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
# Return param by name |
95
|
|
|
|
96
|
|
|
public function param(string $name) { |
97
|
|
|
|
98
|
|
|
return $this->params->get($name); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
# Return indexes |
102
|
|
|
|
103
|
|
|
public function indexes() { |
104
|
|
|
|
105
|
|
|
return $this->indexes->list(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
# Return index by name |
109
|
|
|
|
110
|
|
|
public function index(string $name) { |
111
|
|
|
|
112
|
|
|
return $this->indexes->get($name); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
# Return foreigns |
116
|
|
|
|
117
|
|
|
public function foreigns() { |
118
|
|
|
|
119
|
|
|
return $this->foreigns->list(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
# Return foreign by name |
123
|
|
|
|
124
|
|
|
public function foreign(string $name) { |
125
|
|
|
|
126
|
|
|
return $this->foreigns->get($name); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
# Create table(s) |
130
|
|
|
|
131
|
|
|
public function createTable() { |
132
|
|
|
|
133
|
|
|
return ($this->createMainTable() && (!static::$nesting || $this->createRelationsTable())); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|