|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Basis\Procedure; |
|
4
|
|
|
|
|
5
|
|
|
use Tarantool\Mapper\Procedure; |
|
6
|
|
|
|
|
7
|
|
|
class Select extends Procedure |
|
8
|
|
|
{ |
|
9
|
5 |
|
public function getParams() : array |
|
10
|
|
|
{ |
|
11
|
5 |
|
return ['space', 'index', 'values']; |
|
12
|
|
|
} |
|
13
|
|
|
|
|
14
|
5 |
|
public function getBody(): string |
|
15
|
|
|
{ |
|
16
|
|
|
return <<<LUA |
|
17
|
5 |
|
if box.space[space] == nil then |
|
18
|
|
|
return nil |
|
19
|
|
|
end |
|
20
|
|
|
if box.space[space].index[index] == nil then |
|
21
|
|
|
return nil |
|
22
|
|
|
end |
|
23
|
|
|
|
|
24
|
|
|
if #values == 0 then |
|
25
|
|
|
return {} |
|
26
|
|
|
end |
|
27
|
|
|
|
|
28
|
|
|
local pk = {} |
|
29
|
|
|
for i, t in pairs(box.space[space].index[0].parts) do |
|
30
|
|
|
table.insert(pk, t.fieldno) |
|
31
|
|
|
end |
|
32
|
|
|
|
|
33
|
|
|
local result = {} |
|
34
|
|
|
local keys = {} |
|
35
|
|
|
|
|
36
|
|
|
local parentField = nil |
|
37
|
|
|
|
|
38
|
|
|
for i, f in pairs(box.space[space]:format()) do |
|
39
|
|
|
if f.name == 'parent' and f.reference == space then |
|
40
|
|
|
parentField = i |
|
41
|
|
|
end |
|
42
|
|
|
end |
|
43
|
|
|
|
|
44
|
|
|
local childIndex = nil |
|
45
|
|
|
|
|
46
|
|
|
if parentField ~= nil then |
|
47
|
|
|
for i, candidate in pairs(box.space[space].index) do |
|
48
|
|
|
if #candidate.parts == 1 then |
|
49
|
|
|
if candidate.parts[1].fieldno == parentField then |
|
50
|
|
|
childIndex = candidate.name |
|
51
|
|
|
end |
|
52
|
|
|
end |
|
53
|
|
|
end |
|
54
|
|
|
end |
|
55
|
|
|
|
|
56
|
|
|
local function selector(index, value) |
|
57
|
|
|
for j, tuple in box.space[space].index[index]:pairs(value) do |
|
58
|
|
|
local key = "" |
|
59
|
|
|
for i, f in pairs(pk) do |
|
60
|
|
|
key = key .. tuple[f] .. '-' |
|
61
|
|
|
end |
|
62
|
|
|
if keys[key] == nil then |
|
63
|
|
|
keys[key] = true |
|
64
|
|
|
table.insert(result, tuple) |
|
65
|
|
|
if childIndex ~= nil then |
|
66
|
|
|
selector(childIndex, tuple[1]) |
|
67
|
|
|
end |
|
68
|
|
|
end |
|
69
|
|
|
end |
|
70
|
|
|
|
|
71
|
|
|
end |
|
72
|
|
|
|
|
73
|
|
|
for i, value in pairs(values) do |
|
74
|
|
|
selector(index, value) |
|
75
|
|
|
end |
|
76
|
|
|
|
|
77
|
|
|
return result |
|
78
|
|
|
LUA; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|