1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tarantool\Mapper\Procedure; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Tarantool\Mapper\Plugin\Sequence; |
7
|
|
|
use Tarantool\Mapper\Procedure; |
8
|
|
|
|
9
|
|
|
class FindOrCreate extends Procedure |
10
|
|
|
{ |
11
|
|
|
public function execute($space, $params) |
12
|
|
|
{ |
13
|
|
|
$index = $space->castIndex($params); |
14
|
|
|
if (is_null($index)) { |
15
|
|
|
throw new Exception("No valid index for ".json_encode($params)); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
$values = $space->getIndexValues($index, $params); |
19
|
|
|
|
20
|
|
|
$tuple = []; |
21
|
|
|
$schema = $space->getMapper()->getSchema(); |
22
|
|
|
|
23
|
|
|
foreach ($space->getFormat() as $i => $info) { |
24
|
|
|
$name = $info['name']; |
25
|
|
|
if (!array_key_exists($name, $params)) { |
26
|
|
|
$params[$name] = null; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
$params[$name] = $schema->formatValue($info['type'], $params[$name]); |
30
|
|
|
if (is_null($params[$name])) { |
31
|
|
|
if (!$space->isPropertyNullable($name)) { |
32
|
|
|
$params[$name] = $schema->getDefaultValue($info['type']); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$tuple[$i] = $params[$name]; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$primary = $space->getPrimaryIndex(); |
40
|
|
|
$sequence = 0; |
41
|
|
|
if (count($primary['parts']) == 1) { |
42
|
|
|
$key = $space->getFormat()[$primary['parts'][0][0]]['name']; |
43
|
|
|
if (!array_key_exists($key, $params) || !$params[$key]) { |
44
|
|
|
$sequence = 1; |
45
|
|
|
$space->getMapper() |
46
|
|
|
->getPlugin(Sequence::class) |
47
|
|
|
->initializeSequence($space); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$tuple = $this($space->getName(), $index, $values, $tuple, $sequence); |
52
|
|
|
|
53
|
|
|
if (is_string($tuple)) { |
54
|
|
|
throw new Exception($tuple); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$key = []; |
58
|
|
|
$format = $space->getFormat(); |
59
|
|
|
foreach ($primary['parts'] as $part) { |
60
|
|
|
$key[$format[$part[0]]['name']] = $tuple[$part[0]]; |
61
|
|
|
} |
62
|
|
|
return $key; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getBody() : string |
66
|
|
|
{ |
67
|
|
|
return <<<LUA |
68
|
|
|
if box.space[space] == nil then |
69
|
|
|
return 'no space ' .. space |
70
|
|
|
end |
71
|
|
|
|
72
|
|
|
if box.space[space].index[index] == nil then |
73
|
|
|
return 'no space index ' .. index |
74
|
|
|
end |
75
|
|
|
|
76
|
|
|
local instances = box.space[space].index[index]:select(params) |
77
|
|
|
|
78
|
|
|
if #instances > 0 then |
79
|
|
|
return instances[1] |
80
|
|
|
end |
81
|
|
|
|
82
|
|
|
if sequence == 1 then |
83
|
|
|
if box.space._sequence == nil then |
84
|
|
|
return 'no _sequence space' |
85
|
|
|
end |
86
|
|
|
local row = box.space.sequence:update(box.space[space].id, {{'+', 2, 1}}); |
87
|
|
|
tuple[1] = row[2]; |
88
|
|
|
end |
89
|
|
|
|
90
|
|
|
return box.space[space]:insert(tuple) |
91
|
|
|
LUA; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function getParams() : array |
95
|
|
|
{ |
96
|
|
|
return ['space', 'index', 'params', 'tuple', 'sequence']; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function getName() : string |
100
|
|
|
{ |
101
|
|
|
return 'mapper_find_or_create'; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|