line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# Copyright (C) 2021-2022 Horimoto Yasuhiro |
2
|
|
|
|
|
|
|
# |
3
|
|
|
|
|
|
|
# This program is free software: you can redistribute it and/or modify |
4
|
|
|
|
|
|
|
# it under the terms of the GNU General Public License as published by |
5
|
|
|
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or |
6
|
|
|
|
|
|
|
# (at your option) any later version. |
7
|
|
|
|
|
|
|
# |
8
|
|
|
|
|
|
|
# This program is distributed in the hope that it will be useful, |
9
|
|
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
10
|
|
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11
|
|
|
|
|
|
|
# GNU General Public License for more details. |
12
|
|
|
|
|
|
|
# |
13
|
|
|
|
|
|
|
# You should have received a copy of the GNU General Public License |
14
|
|
|
|
|
|
|
# along with this program. If not, see . |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
package Groonga::Commands::Select; |
17
|
|
|
|
|
|
|
|
18
|
1
|
|
|
1
|
|
5
|
use Carp 'croak'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
40
|
|
19
|
|
|
|
|
|
|
|
20
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
15
|
|
21
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
1042
|
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
my $groonga_http_client = undef; |
24
|
|
|
|
|
|
|
my $command_args = ""; |
25
|
|
|
|
|
|
|
my $n_hits = undef; |
26
|
|
|
|
|
|
|
my @records; |
27
|
|
|
|
|
|
|
my $use_drilldown = 0; |
28
|
|
|
|
|
|
|
my $n_drilldown_keys = 0; |
29
|
|
|
|
|
|
|
my @select_arguments = ( |
30
|
|
|
|
|
|
|
'table', |
31
|
|
|
|
|
|
|
'output_columns', |
32
|
|
|
|
|
|
|
'query', |
33
|
|
|
|
|
|
|
'filter', |
34
|
|
|
|
|
|
|
'columns', |
35
|
|
|
|
|
|
|
'sort_keys', |
36
|
|
|
|
|
|
|
'limit', |
37
|
|
|
|
|
|
|
'synonym', |
38
|
|
|
|
|
|
|
'drilldown', |
39
|
|
|
|
|
|
|
'drilldown_filter', |
40
|
|
|
|
|
|
|
'drilldown_output_columns', |
41
|
|
|
|
|
|
|
'drilldown_sort_keys', |
42
|
|
|
|
|
|
|
'dynamic_columns', |
43
|
|
|
|
|
|
|
'match_columns', |
44
|
|
|
|
|
|
|
'query_expander', |
45
|
|
|
|
|
|
|
'post_filter' |
46
|
|
|
|
|
|
|
); |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub new { |
49
|
0
|
|
|
0
|
0
|
|
my ($class, %args) = @_; |
50
|
0
|
|
|
|
|
|
my $self = {%args}; |
51
|
|
|
|
|
|
|
|
52
|
0
|
|
|
|
|
|
$groonga_http_client = $self->{client}; |
53
|
0
|
|
|
|
|
|
$command_args = _parse_arguments($self); |
54
|
|
|
|
|
|
|
|
55
|
0
|
|
|
|
|
|
return bless $self, $class; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub _is_valid_arguments { |
59
|
0
|
|
|
0
|
|
|
my $args = shift; |
60
|
|
|
|
|
|
|
|
61
|
0
|
|
|
|
|
|
while (my ($key, $value) = each %{$args}) { |
|
0
|
|
|
|
|
|
|
62
|
0
|
0
|
|
|
|
|
if ($key eq 'client') { |
63
|
0
|
|
|
|
|
|
next; |
64
|
|
|
|
|
|
|
} |
65
|
0
|
0
|
|
|
|
|
if (!(grep {$_ eq $key} @select_arguments)) { |
|
0
|
|
|
|
|
|
|
66
|
0
|
|
|
|
|
|
croak "Invalid arguments: ${key}"; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
0
|
|
|
|
|
|
return 1; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub _parse_arguments { |
74
|
0
|
|
|
0
|
|
|
my $args = shift; |
75
|
|
|
|
|
|
|
|
76
|
0
|
|
|
|
|
|
my %parsed_arguments = (); |
77
|
|
|
|
|
|
|
|
78
|
0
|
|
|
|
|
|
_is_valid_arguments($args); |
79
|
|
|
|
|
|
|
|
80
|
0
|
0
|
|
|
|
|
if (exists($args->{'table'})) { |
81
|
0
|
|
|
|
|
|
$parsed_arguments{'table'} = $args->{'table'}; |
82
|
|
|
|
|
|
|
} |
83
|
0
|
0
|
|
|
|
|
if (exists($args->{'output_columns'})) { |
84
|
0
|
|
|
|
|
|
my $parsed_output_columns = ""; |
85
|
0
|
|
|
|
|
|
my $output_columns = $args->{'output_columns'}; |
86
|
0
|
|
|
|
|
|
foreach my $output_column (@$output_columns) { |
87
|
0
|
|
|
|
|
|
$parsed_output_columns .= $output_column . ','; |
88
|
|
|
|
|
|
|
} |
89
|
0
|
|
|
|
|
|
chop($parsed_output_columns); |
90
|
0
|
|
|
|
|
|
$parsed_arguments{'output_columns'} = $parsed_output_columns; |
91
|
|
|
|
|
|
|
} |
92
|
0
|
0
|
|
|
|
|
if (exists($args->{'query'})) { |
93
|
0
|
|
|
|
|
|
$parsed_arguments{'query'} = $args->{'query'}; |
94
|
|
|
|
|
|
|
} |
95
|
0
|
0
|
|
|
|
|
if (exists($args->{'filter'})) { |
96
|
0
|
|
|
|
|
|
$parsed_arguments{'filter'} = $args->{'filter'}; |
97
|
|
|
|
|
|
|
} |
98
|
0
|
0
|
|
|
|
|
if (exists($args->{'columns'})) { |
99
|
0
|
|
|
|
|
|
$parsed_arguments{'match_columns'} = $args->{'columns'}; |
100
|
|
|
|
|
|
|
} |
101
|
0
|
0
|
|
|
|
|
if (exists($args->{'sort_keys'})) { |
102
|
0
|
|
|
|
|
|
$parsed_arguments{'sort_keys'} = $args->{'sort_keys'}; |
103
|
|
|
|
|
|
|
} |
104
|
0
|
0
|
|
|
|
|
if (exists($args->{'limit'})) { |
105
|
0
|
|
|
|
|
|
$parsed_arguments{'limit'} = $args->{'limit'}; |
106
|
|
|
|
|
|
|
} |
107
|
0
|
0
|
|
|
|
|
if (exists($args->{'synonym'})) { |
108
|
0
|
|
|
|
|
|
$parsed_arguments{'query_expander'} = $args->{'synonym'}; |
109
|
|
|
|
|
|
|
} |
110
|
0
|
0
|
|
|
|
|
if (exists($args->{'drilldown'})) { |
111
|
0
|
|
|
|
|
|
$use_drilldown = 1; |
112
|
0
|
|
|
|
|
|
$n_drilldown_keys = split(/,/, $args->{'drilldown'}); |
113
|
0
|
|
|
|
|
|
$parsed_arguments{'drilldown'} = $args->{'drilldown'}; |
114
|
|
|
|
|
|
|
} |
115
|
0
|
0
|
|
|
|
|
if (exists($args->{'drilldown_filter'})) { |
116
|
0
|
|
|
|
|
|
$use_drilldown = 1; |
117
|
0
|
|
|
|
|
|
$parsed_arguments{'drilldown_filter'} = $args->{'drilldown_filter'}; |
118
|
|
|
|
|
|
|
} |
119
|
0
|
0
|
|
|
|
|
if (exists($args->{'drilldown_output_columns'})) { |
120
|
0
|
|
|
|
|
|
$use_drilldown = 1; |
121
|
0
|
|
|
|
|
|
$parsed_arguments{'drilldown_output_columns'} = $args->{'drilldown_output_columns'}; |
122
|
|
|
|
|
|
|
} |
123
|
0
|
0
|
|
|
|
|
if (exists($args->{'drilldown_sort_keys'})) { |
124
|
0
|
|
|
|
|
|
$use_drilldown = 1; |
125
|
0
|
|
|
|
|
|
$parsed_arguments{'drilldown_sort_keys'} = $args->{'drilldown_sort_keys'}; |
126
|
|
|
|
|
|
|
} |
127
|
0
|
0
|
|
|
|
|
if (exists($args->{'dynamic_columns'})) { |
128
|
0
|
|
|
|
|
|
my $dynamic_columns = $args->{'dynamic_columns'}; |
129
|
|
|
|
|
|
|
|
130
|
0
|
|
|
|
|
|
for (my $i = 0; $i < scalar(@$dynamic_columns); $i++) { |
131
|
0
|
0
|
0
|
|
|
|
if (exists($dynamic_columns->[$i]->{'name'}) |
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
132
|
|
|
|
|
|
|
&& exists($dynamic_columns->[$i]->{'stage'}) |
133
|
|
|
|
|
|
|
&& exists($dynamic_columns->[$i]->{'type'}) |
134
|
|
|
|
|
|
|
&& exists($dynamic_columns->[$i]->{'value'}) |
135
|
|
|
|
|
|
|
) { |
136
|
|
|
|
|
|
|
; |
137
|
|
|
|
|
|
|
} else { |
138
|
0
|
|
|
|
|
|
croak "Missing required argument"; |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
0
|
|
|
|
|
|
my $name = $dynamic_columns->[$i]->{'name'}; |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
$parsed_arguments{'columns[' . $name . '].stage'} = |
144
|
0
|
|
|
|
|
|
$dynamic_columns->[$i]->{'stage'}; |
145
|
|
|
|
|
|
|
$parsed_arguments{'columns[' . $name . '].type'} = |
146
|
0
|
|
|
|
|
|
$dynamic_columns->[$i]->{'type'}; |
147
|
|
|
|
|
|
|
$parsed_arguments{'columns[' . $name . '].value'} = |
148
|
0
|
|
|
|
|
|
$dynamic_columns->[$i]->{'value'}; |
149
|
|
|
|
|
|
|
|
150
|
0
|
0
|
|
|
|
|
if (exists($dynamic_columns->[$i]->{'flags'})) { |
151
|
|
|
|
|
|
|
$parsed_arguments{'columns[' . $name . '].flags'} = |
152
|
0
|
|
|
|
|
|
$dynamic_columns->[$i]->{'flags'}; |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
} |
156
|
0
|
0
|
|
|
|
|
if (exists($args->{'match_columns'})) { |
157
|
0
|
|
|
|
|
|
$parsed_arguments{'match_columns'} = $args->{'match_columns'}; |
158
|
|
|
|
|
|
|
} |
159
|
0
|
0
|
|
|
|
|
if (exists($args->{'query_expander'})) { |
160
|
0
|
|
|
|
|
|
$parsed_arguments{'query_expander'} = $args->{'query_expander'}; |
161
|
|
|
|
|
|
|
} |
162
|
0
|
0
|
|
|
|
|
if (exists($args->{'post_filter'})) { |
163
|
0
|
|
|
|
|
|
$parsed_arguments{'post_filter'} = $args->{'post_filter'}; |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
|
166
|
0
|
|
|
|
|
|
return \%parsed_arguments; |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
sub _parse_result { |
170
|
0
|
|
|
0
|
|
|
my $result = shift; |
171
|
0
|
|
|
|
|
|
my %result_set = (); |
172
|
0
|
|
|
|
|
|
my @records = (); |
173
|
0
|
|
|
|
|
|
my @drilldown_result_records = (); |
174
|
|
|
|
|
|
|
|
175
|
0
|
0
|
|
|
|
|
if ($use_drilldown) { |
176
|
0
|
0
|
|
|
|
|
if ($n_drilldown_keys > 1) { |
177
|
0
|
|
|
|
|
|
my @n_hits_drilldown = (); |
178
|
0
|
|
|
|
|
|
for (my $n_keys = 0; $n_keys < $n_drilldown_keys; $n_keys++) { |
179
|
0
|
|
|
|
|
|
push(@n_hits_drilldown, $result->[$n_keys+1][0][0]); |
180
|
|
|
|
|
|
|
|
181
|
0
|
|
|
|
|
|
my @column_names_drilldown; |
182
|
0
|
|
|
|
|
|
for (my $i = 0; $result->[$n_keys+1][1][$i]; $i++) { |
183
|
0
|
|
|
|
|
|
push(@column_names_drilldown, $result->[$n_keys+1][1][$i][0]); |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
|
186
|
0
|
|
|
|
|
|
my @temporary = (); |
187
|
0
|
|
|
|
|
|
for (my $i = 0, my $j = 2; $i < $n_hits_drilldown[$n_keys]; $i++, $j++) { |
188
|
0
|
|
|
|
|
|
my %record = (); |
189
|
0
|
|
|
|
|
|
for (my $k=0; $k < @column_names_drilldown; $k++) { |
190
|
0
|
|
|
|
|
|
$record{"drilldown_" . $column_names_drilldown[$k]} = "$result->[$n_keys+1][$j][$k]"; |
191
|
|
|
|
|
|
|
} |
192
|
0
|
|
|
|
|
|
push(@temporary, \%record); |
193
|
|
|
|
|
|
|
} |
194
|
0
|
|
|
|
|
|
$drilldown_result_records[$n_keys] = \@temporary; |
195
|
|
|
|
|
|
|
} |
196
|
0
|
|
|
|
|
|
$result_set{'n_hits_drilldown'} = \@n_hits_drilldown; |
197
|
|
|
|
|
|
|
} else { |
198
|
0
|
|
|
|
|
|
$result_set{'n_hits_drilldown'} = ($result->[1][0][0]); |
199
|
|
|
|
|
|
|
|
200
|
0
|
|
|
|
|
|
my @column_names_drilldown; |
201
|
0
|
|
|
|
|
|
for (my $i = 0; $result->[1][1][$i]; $i++) { |
202
|
0
|
|
|
|
|
|
push(@column_names_drilldown, $result->[1][1][$i][0]); |
203
|
|
|
|
|
|
|
} |
204
|
|
|
|
|
|
|
|
205
|
0
|
|
|
|
|
|
for (my $i = 0, my $j = 2; $i < $result_set{'n_hits_drilldown'}; $i++, $j++) { |
206
|
0
|
|
|
|
|
|
my %record = (); |
207
|
0
|
|
|
|
|
|
for (my $k=0; $k < @column_names_drilldown; $k++) { |
208
|
0
|
|
|
|
|
|
$record{"drilldown_" . $column_names_drilldown[$k]} = "$result->[1][$j][$k]"; |
209
|
|
|
|
|
|
|
} |
210
|
0
|
|
|
|
|
|
push(@drilldown_result_records, \%record); |
211
|
|
|
|
|
|
|
} |
212
|
|
|
|
|
|
|
} |
213
|
0
|
|
|
|
|
|
$use_drilldown = 0; |
214
|
|
|
|
|
|
|
} |
215
|
|
|
|
|
|
|
|
216
|
0
|
|
|
|
|
|
$result_set{'n_hits'} = $result->[0][0][0]; |
217
|
|
|
|
|
|
|
|
218
|
0
|
|
|
|
|
|
my @column_names; |
219
|
0
|
|
|
|
|
|
for (my $i = 0; $result->[0][1][$i]; $i++) { |
220
|
0
|
|
|
|
|
|
push(@column_names, $result->[0][1][$i][0]); |
221
|
|
|
|
|
|
|
} |
222
|
0
|
|
|
|
|
|
for (my $i = 0, my $j = 2; $i < $result_set{'n_hits'}; $i++, $j++) { |
223
|
0
|
|
|
|
|
|
my %record = (); |
224
|
0
|
|
|
|
|
|
for (my $k=0; $k < @column_names; $k++) { |
225
|
0
|
|
|
|
|
|
$record{"$column_names[$k]"} = $result->[0][$j][$k]; |
226
|
|
|
|
|
|
|
} |
227
|
0
|
|
|
|
|
|
push(@records, \%record); |
228
|
|
|
|
|
|
|
} |
229
|
0
|
|
|
|
|
|
$result_set{'records'} = \@records; |
230
|
0
|
|
|
|
|
|
$result_set{'drilldown_result_records'} = \@drilldown_result_records; |
231
|
|
|
|
|
|
|
|
232
|
0
|
|
|
|
|
|
return \%result_set; |
233
|
|
|
|
|
|
|
} |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
sub execute { |
236
|
0
|
0
|
|
0
|
0
|
|
if (defined $groonga_http_client) { |
237
|
0
|
|
|
|
|
|
return _parse_result($groonga_http_client->send('select', $command_args)); |
238
|
|
|
|
|
|
|
} |
239
|
0
|
|
|
|
|
|
return; |
240
|
|
|
|
|
|
|
} |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
1; |