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