|
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
|
|
|
|
|
14
|
|
|
public function __construct(Mapper $mapper) |
|
15
|
|
|
{ |
|
16
|
|
|
$this->mapper = $mapper; |
|
17
|
|
|
$this->reset(); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public function createSpace($space, $properties = null) |
|
21
|
|
|
{ |
|
22
|
|
|
$id = $this->mapper->getClient()->evaluate(" |
|
23
|
|
|
box.schema.space.create('$space') |
|
24
|
|
|
return box.space.$space.id |
|
25
|
|
|
")->getData()[0]; |
|
26
|
|
|
|
|
27
|
|
|
$this->names[$space] = $id; |
|
28
|
|
|
|
|
29
|
|
|
$this->spaces[$id] = new Space($this->mapper, $id, $space); |
|
30
|
|
|
|
|
31
|
|
|
if($properties) { |
|
32
|
|
|
$this->spaces[$id]->addProperties($properties); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
return $this->spaces[$id]; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function formatValue($type, $value) |
|
39
|
|
|
{ |
|
40
|
|
|
switch($type) { |
|
41
|
|
|
case 'STR': |
|
42
|
|
|
case 'str': |
|
43
|
|
|
return (string) $value; |
|
44
|
|
|
|
|
45
|
|
|
case 'unsigned': |
|
46
|
|
|
case 'UNSIGNED': |
|
47
|
|
|
case 'num': |
|
48
|
|
|
case 'NUM': |
|
49
|
|
|
return (int) $value; |
|
50
|
|
|
default: return $value; |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function getSpace($id) |
|
55
|
|
|
{ |
|
56
|
|
|
if(is_string($id)) { |
|
57
|
|
|
return $this->getSpace($this->getSpaceId($id)); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if(!$id) { |
|
61
|
|
|
throw new Exception("Space id or name not defined"); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if(!array_key_exists($id, $this->spaces)) { |
|
65
|
|
|
$this->spaces[$id] = new Space($this->mapper, $id, array_search($id, $this->names)); |
|
66
|
|
|
} |
|
67
|
|
|
return $this->spaces[$id]; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function getSpaceId($name) |
|
71
|
|
|
{ |
|
72
|
|
|
if(!$this->hasSpace($name)) { |
|
73
|
|
|
throw new Exception("No space $name"); |
|
74
|
|
|
} |
|
75
|
|
|
return $this->names[$name]; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function getSpaces() |
|
79
|
|
|
{ |
|
80
|
|
|
foreach($this->names as $id) { |
|
81
|
|
|
$this->getSpace($id); |
|
82
|
|
|
} |
|
83
|
|
|
return $this->spaces; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function hasSpace($name) |
|
87
|
|
|
{ |
|
88
|
|
|
return array_key_exists($name, $this->names); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function once($name, $callback) |
|
92
|
|
|
{ |
|
93
|
|
|
$key = 'once' . $name; |
|
94
|
|
|
|
|
95
|
|
|
$rows = $this->mapper->find('_schema', ['key' => $key]); |
|
96
|
|
|
if(!count($rows)) { |
|
97
|
|
|
$this->mapper->create('_schema', ['key' => $key]); |
|
98
|
|
|
return $callback($this->mapper); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function reset() |
|
103
|
|
|
{ |
|
104
|
|
|
$this->names = $this->mapper->getClient()->evaluate(" |
|
105
|
|
|
local spaces = {} |
|
106
|
|
|
local i, s |
|
107
|
|
|
for i, s in box.space._space:pairs() do |
|
108
|
|
|
spaces[s[3]] = s[1] |
|
109
|
|
|
end |
|
110
|
|
|
return spaces" |
|
111
|
|
|
)->getData()[0]; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
According to the PSR-2, the body of a default statement must start on the line immediately following the statement.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.