|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tarantool\Mapper; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
|
|
7
|
|
|
class Schema |
|
8
|
|
|
{ |
|
9
|
|
|
private $mapper; |
|
10
|
|
|
|
|
11
|
|
|
private $names = []; |
|
12
|
|
|
private $spaces = []; |
|
13
|
|
|
private $params = []; |
|
14
|
|
|
|
|
15
|
|
|
public function __construct(Mapper $mapper, $meta = null) |
|
16
|
|
|
{ |
|
17
|
|
|
$this->mapper = $mapper; |
|
18
|
|
|
if ($meta) { |
|
19
|
|
|
$this->setMeta($meta); |
|
20
|
|
|
} else { |
|
21
|
|
|
$this->reset(); |
|
22
|
|
|
} |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function createSpace($space, $properties = null) |
|
26
|
|
|
{ |
|
27
|
|
|
$id = $this->mapper->getClient()->evaluate(" |
|
28
|
|
|
box.schema.space.create('$space') |
|
29
|
|
|
return box.space.$space.id |
|
30
|
|
|
")->getData()[0]; |
|
31
|
|
|
|
|
32
|
|
|
$this->names[$space] = $id; |
|
33
|
|
|
|
|
34
|
|
|
$this->spaces[$id] = new Space($this->mapper, $id, $space); |
|
35
|
|
|
|
|
36
|
|
|
if ($properties) { |
|
37
|
|
|
$this->spaces[$id]->addProperties($properties); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
return $this->spaces[$id]; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function getDefaultValue($type) |
|
44
|
|
|
{ |
|
45
|
|
|
switch ($type) { |
|
46
|
|
|
case 'STR': |
|
47
|
|
|
case 'STRING': |
|
48
|
|
|
case 'str': |
|
49
|
|
|
case 'string': |
|
50
|
|
|
return (string) null; |
|
51
|
|
|
|
|
52
|
|
|
case 'double': |
|
53
|
|
|
case 'float': |
|
54
|
|
|
case 'number': |
|
55
|
|
|
return (float) null; |
|
56
|
|
|
|
|
57
|
|
|
case 'unsigned': |
|
58
|
|
|
case 'UNSIGNED': |
|
59
|
|
|
case 'num': |
|
60
|
|
|
case 'NUM': |
|
61
|
|
|
return (int) null; |
|
62
|
|
|
|
|
63
|
|
|
default: |
|
64
|
|
|
return $value; |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $this->formatValue($type, null, true); |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function formatValue($type, $value) |
|
71
|
|
|
{ |
|
72
|
|
|
if (is_null($value)) { |
|
73
|
|
|
return null; |
|
74
|
|
|
} |
|
75
|
|
|
switch ($type) { |
|
76
|
|
|
case 'STR': |
|
77
|
|
|
case 'STRING': |
|
78
|
|
|
case 'str': |
|
79
|
|
|
case 'string': |
|
80
|
|
|
return (string) $value; |
|
81
|
|
|
|
|
82
|
|
|
case 'double': |
|
83
|
|
|
case 'float': |
|
84
|
|
|
case 'number': |
|
85
|
|
|
return (float) $value; |
|
86
|
|
|
|
|
87
|
|
|
case 'unsigned': |
|
88
|
|
|
case 'UNSIGNED': |
|
89
|
|
|
case 'num': |
|
90
|
|
|
case 'NUM': |
|
91
|
|
|
return (int) $value; |
|
92
|
|
|
|
|
93
|
|
|
default: |
|
94
|
|
|
return $value; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function getSpace($id) |
|
99
|
|
|
{ |
|
100
|
|
|
if (is_string($id)) { |
|
101
|
|
|
return $this->getSpace($this->getSpaceId($id)); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
if (!$id) { |
|
105
|
|
|
throw new Exception("Space id or name not defined"); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
if (!array_key_exists($id, $this->spaces)) { |
|
109
|
|
|
$name = array_search($id, $this->names); |
|
110
|
|
|
$meta = array_key_exists($id, $this->params) ? $this->params[$id] : null; |
|
111
|
|
|
$this->spaces[$id] = new Space($this->mapper, $id, $name, $meta); |
|
112
|
|
|
} |
|
113
|
|
|
return $this->spaces[$id]; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function getSpaceId($name) |
|
117
|
|
|
{ |
|
118
|
|
|
if (!$this->hasSpace($name)) { |
|
119
|
|
|
throw new Exception("No space $name"); |
|
120
|
|
|
} |
|
121
|
|
|
return $this->names[$name]; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function getSpaces() |
|
125
|
|
|
{ |
|
126
|
|
|
foreach ($this->names as $id) { |
|
127
|
|
|
$this->getSpace($id); |
|
128
|
|
|
} |
|
129
|
|
|
return $this->spaces; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
public function hasSpace($name) |
|
133
|
|
|
{ |
|
134
|
|
|
return array_key_exists($name, $this->names); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function once($name, $callback) |
|
138
|
|
|
{ |
|
139
|
|
|
$key = 'mapper-once' . $name; |
|
140
|
|
|
|
|
141
|
|
|
$rows = $this->mapper->find('_schema', ['key' => $key]); |
|
142
|
|
|
if (!count($rows)) { |
|
143
|
|
|
$this->mapper->create('_schema', ['key' => $key]); |
|
144
|
|
|
return $callback($this->mapper); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
public function reset() |
|
149
|
|
|
{ |
|
150
|
|
|
$this->names = $this->mapper->getClient()->evaluate(" |
|
151
|
|
|
local spaces = {} |
|
152
|
|
|
local i, s |
|
153
|
|
|
for i, s in box.space._vspace:pairs() do |
|
154
|
|
|
spaces[s[3]] = s[1] |
|
155
|
|
|
end |
|
156
|
|
|
return spaces |
|
157
|
|
|
")->getData()[0]; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
public function getMeta() |
|
161
|
|
|
{ |
|
162
|
|
|
$params = []; |
|
163
|
|
|
foreach ($this->getSpaces() as $space) { |
|
164
|
|
|
$params[$space->getId()] = $space->getMeta(); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
return [ |
|
168
|
|
|
'names' => $this->names, |
|
169
|
|
|
'params' => $params |
|
170
|
|
|
]; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
public function setMeta($meta) |
|
174
|
|
|
{ |
|
175
|
|
|
$this->names = $meta['names']; |
|
176
|
|
|
$this->params = $meta['params']; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
private $underscores = []; |
|
180
|
|
|
|
|
181
|
|
|
public function toUnderscore($input) |
|
182
|
|
|
{ |
|
183
|
|
|
if (!array_key_exists($input, $this->underscores)) { |
|
184
|
|
|
preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches); |
|
185
|
|
|
$ret = $matches[0]; |
|
186
|
|
|
foreach ($ret as &$match) { |
|
187
|
|
|
$match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match); |
|
188
|
|
|
} |
|
189
|
|
|
$this->underscores[$input] = implode('_', $ret); |
|
190
|
|
|
} |
|
191
|
|
|
return $this->underscores[$input]; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
private $camelcase = []; |
|
195
|
|
|
|
|
196
|
|
|
public function toCamelCase($input) |
|
197
|
|
|
{ |
|
198
|
|
|
if (!array_key_exists($input, $this->camelcase)) { |
|
199
|
|
|
$this->camelcase[$input] = lcfirst(implode('', array_map('ucfirst', explode('_', $input)))); |
|
200
|
|
|
} |
|
201
|
|
|
return $this->camelcase[$input]; |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.