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
|
|
|
|
|
39
|
|
19
|
|
|
|
|
|
|
|
20
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
16
|
|
21
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
890
|
|
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 @select_arguments = ( |
29
|
|
|
|
|
|
|
'table', |
30
|
|
|
|
|
|
|
'output_columns', |
31
|
|
|
|
|
|
|
'query', |
32
|
|
|
|
|
|
|
'filter', |
33
|
|
|
|
|
|
|
'columns', |
34
|
|
|
|
|
|
|
'sort_keys', |
35
|
|
|
|
|
|
|
'limit', |
36
|
|
|
|
|
|
|
'synonym', |
37
|
|
|
|
|
|
|
'drilldown', |
38
|
|
|
|
|
|
|
'drilldown_filter', |
39
|
|
|
|
|
|
|
'drilldown_output_columns', |
40
|
|
|
|
|
|
|
'drilldown_sort_keys', |
41
|
|
|
|
|
|
|
'dynamic_columns', |
42
|
|
|
|
|
|
|
'match_columns', |
43
|
|
|
|
|
|
|
'query_expander', |
44
|
|
|
|
|
|
|
'post_filter' |
45
|
|
|
|
|
|
|
); |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub new { |
48
|
0
|
|
|
0
|
0
|
|
my ($class, %args) = @_; |
49
|
0
|
|
|
|
|
|
my $self = {%args}; |
50
|
|
|
|
|
|
|
|
51
|
0
|
|
|
|
|
|
$groonga_http_client = $self->{client}; |
52
|
0
|
|
|
|
|
|
$command_args = _parse_arguments($self); |
53
|
|
|
|
|
|
|
|
54
|
0
|
|
|
|
|
|
return bless $self, $class; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub _is_valid_arguments { |
58
|
0
|
|
|
0
|
|
|
my $args = shift; |
59
|
|
|
|
|
|
|
|
60
|
0
|
|
|
|
|
|
while (my ($key, $value) = each %{$args}) { |
|
0
|
|
|
|
|
|
|
61
|
0
|
0
|
|
|
|
|
if ($key eq 'client') { |
62
|
0
|
|
|
|
|
|
next; |
63
|
|
|
|
|
|
|
} |
64
|
0
|
0
|
|
|
|
|
if (!(grep {$_ eq $key} @select_arguments)) { |
|
0
|
|
|
|
|
|
|
65
|
0
|
|
|
|
|
|
croak "Invalid arguments: ${key}"; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
0
|
|
|
|
|
|
return 1; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub _parse_arguments { |
73
|
0
|
|
|
0
|
|
|
my $args = shift; |
74
|
|
|
|
|
|
|
|
75
|
0
|
|
|
|
|
|
my %parsed_arguments = (); |
76
|
|
|
|
|
|
|
|
77
|
0
|
|
|
|
|
|
_is_valid_arguments($args); |
78
|
|
|
|
|
|
|
|
79
|
0
|
0
|
|
|
|
|
if (exists($args->{'table'})) { |
80
|
0
|
|
|
|
|
|
$parsed_arguments{'table'} = $args->{'table'}; |
81
|
|
|
|
|
|
|
} |
82
|
0
|
0
|
|
|
|
|
if (exists($args->{'output_columns'})) { |
83
|
0
|
|
|
|
|
|
my $parsed_output_columns = ""; |
84
|
0
|
|
|
|
|
|
my $output_columns = $args->{'output_columns'}; |
85
|
0
|
|
|
|
|
|
foreach my $output_column (@$output_columns) { |
86
|
0
|
|
|
|
|
|
$parsed_output_columns .= $output_column . ','; |
87
|
|
|
|
|
|
|
} |
88
|
0
|
|
|
|
|
|
chop($parsed_output_columns); |
89
|
0
|
|
|
|
|
|
$parsed_arguments{'output_columns'} = $parsed_output_columns; |
90
|
|
|
|
|
|
|
} |
91
|
0
|
0
|
|
|
|
|
if (exists($args->{'query'})) { |
92
|
0
|
|
|
|
|
|
$parsed_arguments{'query'} = $args->{'query'}; |
93
|
|
|
|
|
|
|
} |
94
|
0
|
0
|
|
|
|
|
if (exists($args->{'filter'})) { |
95
|
0
|
|
|
|
|
|
$parsed_arguments{'filter'} = $args->{'filter'}; |
96
|
|
|
|
|
|
|
} |
97
|
0
|
0
|
|
|
|
|
if (exists($args->{'columns'})) { |
98
|
0
|
|
|
|
|
|
$parsed_arguments{'match_columns'} = $args->{'columns'}; |
99
|
|
|
|
|
|
|
} |
100
|
0
|
0
|
|
|
|
|
if (exists($args->{'sort_keys'})) { |
101
|
0
|
|
|
|
|
|
$parsed_arguments{'sort_keys'} = $args->{'sort_keys'}; |
102
|
|
|
|
|
|
|
} |
103
|
0
|
0
|
|
|
|
|
if (exists($args->{'limit'})) { |
104
|
0
|
|
|
|
|
|
$parsed_arguments{'limit'} = $args->{'limit'}; |
105
|
|
|
|
|
|
|
} |
106
|
0
|
0
|
|
|
|
|
if (exists($args->{'synonym'})) { |
107
|
0
|
|
|
|
|
|
$parsed_arguments{'query_expander'} = $args->{'synonym'}; |
108
|
|
|
|
|
|
|
} |
109
|
0
|
0
|
|
|
|
|
if (exists($args->{'drilldown'})) { |
110
|
0
|
|
|
|
|
|
$use_drilldown = 1; |
111
|
0
|
|
|
|
|
|
$parsed_arguments{'drilldown'} = $args->{'drilldown'}; |
112
|
|
|
|
|
|
|
} |
113
|
0
|
0
|
|
|
|
|
if (exists($args->{'drilldown_filter'})) { |
114
|
0
|
|
|
|
|
|
$use_drilldown = 1; |
115
|
0
|
|
|
|
|
|
$parsed_arguments{'drilldown_filter'} = $args->{'drilldown_filter'}; |
116
|
|
|
|
|
|
|
} |
117
|
0
|
0
|
|
|
|
|
if (exists($args->{'drilldown_output_columns'})) { |
118
|
0
|
|
|
|
|
|
$use_drilldown = 1; |
119
|
0
|
|
|
|
|
|
$parsed_arguments{'drilldown_output_columns'} = $args->{'drilldown_output_columns'}; |
120
|
|
|
|
|
|
|
} |
121
|
0
|
0
|
|
|
|
|
if (exists($args->{'drilldown_sort_keys'})) { |
122
|
0
|
|
|
|
|
|
$use_drilldown = 1; |
123
|
0
|
|
|
|
|
|
$parsed_arguments{'drilldown_sort_keys'} = $args->{'drilldown_sort_keys'}; |
124
|
|
|
|
|
|
|
} |
125
|
0
|
0
|
|
|
|
|
if (exists($args->{'dynamic_columns'})) { |
126
|
0
|
|
|
|
|
|
my $dynamic_columns = $args->{'dynamic_columns'}; |
127
|
|
|
|
|
|
|
|
128
|
0
|
|
|
|
|
|
for (my $i = 0; $i < scalar(@$dynamic_columns); $i++) { |
129
|
0
|
0
|
0
|
|
|
|
if (exists($dynamic_columns->[$i]->{'name'}) |
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
130
|
|
|
|
|
|
|
&& exists($dynamic_columns->[$i]->{'stage'}) |
131
|
|
|
|
|
|
|
&& exists($dynamic_columns->[$i]->{'type'}) |
132
|
|
|
|
|
|
|
&& exists($dynamic_columns->[$i]->{'value'}) |
133
|
|
|
|
|
|
|
) { |
134
|
|
|
|
|
|
|
; |
135
|
|
|
|
|
|
|
} else { |
136
|
0
|
|
|
|
|
|
croak "Missing required argument"; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
0
|
|
|
|
|
|
my $name = $dynamic_columns->[$i]->{'name'}; |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
$parsed_arguments{'columns[' . $name . '].stage'} = |
142
|
0
|
|
|
|
|
|
$dynamic_columns->[$i]->{'stage'}; |
143
|
|
|
|
|
|
|
$parsed_arguments{'columns[' . $name . '].type'} = |
144
|
0
|
|
|
|
|
|
$dynamic_columns->[$i]->{'type'}; |
145
|
|
|
|
|
|
|
$parsed_arguments{'columns[' . $name . '].value'} = |
146
|
0
|
|
|
|
|
|
$dynamic_columns->[$i]->{'value'}; |
147
|
|
|
|
|
|
|
|
148
|
0
|
0
|
|
|
|
|
if (exists($dynamic_columns->[$i]->{'flags'})) { |
149
|
|
|
|
|
|
|
$parsed_arguments{'columns[' . $name . '].flags'} = |
150
|
0
|
|
|
|
|
|
$dynamic_columns->[$i]->{'flags'}; |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
} |
154
|
0
|
0
|
|
|
|
|
if (exists($args->{'match_columns'})) { |
155
|
0
|
|
|
|
|
|
$parsed_arguments{'match_columns'} = $args->{'match_columns'}; |
156
|
|
|
|
|
|
|
} |
157
|
0
|
0
|
|
|
|
|
if (exists($args->{'query_expander'})) { |
158
|
0
|
|
|
|
|
|
$parsed_arguments{'query_expander'} = $args->{'query_expander'}; |
159
|
|
|
|
|
|
|
} |
160
|
0
|
0
|
|
|
|
|
if (exists($args->{'post_filter'})) { |
161
|
0
|
|
|
|
|
|
$parsed_arguments{'post_filter'} = $args->{'post_filter'}; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
0
|
|
|
|
|
|
return \%parsed_arguments; |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
sub _parse_result { |
168
|
0
|
|
|
0
|
|
|
my $result = shift; |
169
|
0
|
|
|
|
|
|
my %result_set = (); |
170
|
0
|
|
|
|
|
|
my @records = (); |
171
|
0
|
|
|
|
|
|
my @drilldown_result_records = (); |
172
|
|
|
|
|
|
|
|
173
|
0
|
0
|
|
|
|
|
if ($use_drilldown) { |
174
|
0
|
|
|
|
|
|
$result_set{'n_hits_drilldown'} = $result->[1][0][0]; |
175
|
|
|
|
|
|
|
|
176
|
0
|
|
|
|
|
|
my @column_names_drilldown; |
177
|
0
|
|
|
|
|
|
for (my $i = 0; $result->[1][1][$i]; $i++) { |
178
|
0
|
|
|
|
|
|
push(@column_names_drilldown, $result->[1][1][$i][0]); |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
|
181
|
0
|
|
|
|
|
|
for (my $i = 0, my $j = 2; $i < $result_set{'n_hits_drilldown'}; $i++, $j++) { |
182
|
0
|
|
|
|
|
|
my %record = (); |
183
|
0
|
|
|
|
|
|
for (my $k=0; $k < @column_names_drilldown; $k++) { |
184
|
0
|
|
|
|
|
|
$record{"drilldown_" . $column_names_drilldown[$k]} = "$result->[1][$j][$k]"; |
185
|
|
|
|
|
|
|
} |
186
|
0
|
|
|
|
|
|
push(@drilldown_result_records, \%record); |
187
|
|
|
|
|
|
|
} |
188
|
0
|
|
|
|
|
|
$use_drilldown = 0; |
189
|
|
|
|
|
|
|
} |
190
|
|
|
|
|
|
|
|
191
|
0
|
|
|
|
|
|
$result_set{'n_hits'} = $result->[0][0][0]; |
192
|
|
|
|
|
|
|
|
193
|
0
|
|
|
|
|
|
my @column_names; |
194
|
0
|
|
|
|
|
|
for (my $i = 0; $result->[0][1][$i]; $i++) { |
195
|
0
|
|
|
|
|
|
push(@column_names, $result->[0][1][$i][0]); |
196
|
|
|
|
|
|
|
} |
197
|
0
|
|
|
|
|
|
for (my $i = 0, my $j = 2; $i < $result_set{'n_hits'}; $i++, $j++) { |
198
|
0
|
|
|
|
|
|
my %record = (); |
199
|
0
|
|
|
|
|
|
for (my $k=0; $k < @column_names; $k++) { |
200
|
0
|
|
|
|
|
|
$record{"$column_names[$k]"} = $result->[0][$j][$k]; |
201
|
|
|
|
|
|
|
} |
202
|
0
|
|
|
|
|
|
push(@records, \%record); |
203
|
|
|
|
|
|
|
} |
204
|
0
|
|
|
|
|
|
$result_set{'records'} = \@records; |
205
|
0
|
|
|
|
|
|
$result_set{'drilldown_result_records'} = \@drilldown_result_records; |
206
|
|
|
|
|
|
|
|
207
|
0
|
|
|
|
|
|
return \%result_set; |
208
|
|
|
|
|
|
|
} |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
sub execute { |
211
|
0
|
0
|
|
0
|
0
|
|
if (defined $groonga_http_client) { |
212
|
0
|
|
|
|
|
|
return _parse_result($groonga_http_client->send('select', $command_args)); |
213
|
|
|
|
|
|
|
} |
214
|
0
|
|
|
|
|
|
return; |
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
1; |