1 | /* -*- c-basic-offset: 2 -*- */ |
2 | /* |
3 | Copyright(C) 2015 Brazil |
4 | |
5 | This library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License version 2.1 as published by the Free Software Foundation. |
8 | |
9 | This library is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | Lesser General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU Lesser General Public |
15 | License along with this library; if not, write to the Free Software |
16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
17 | */ |
18 | |
19 | #include "../grn_ctx_impl.h" |
20 | |
21 | #ifdef GRN_WITH_MRUBY |
22 | #include <mruby.h> |
23 | #include <mruby/class.h> |
24 | #include <mruby/data.h> |
25 | #include <mruby/hash.h> |
26 | #include <mruby/array.h> |
27 | #include <mruby/string.h> |
28 | |
29 | #include "mrb_ctx.h" |
30 | #include "mrb_converter.h" |
31 | #include "mrb_operator.h" |
32 | #include "mrb_table_group_result.h" |
33 | |
34 | static void |
35 | mrb_grn_table_group_result_free(mrb_state *mrb, void *data) |
36 | { |
37 | grn_ctx *ctx = (grn_ctx *)mrb->ud; |
38 | grn_table_group_result *result = data; |
39 | |
40 | if (!result) { |
41 | return; |
42 | } |
43 | |
44 | if (result->calc_target) { |
45 | grn_obj_unlink(ctx, result->calc_target); |
46 | } |
47 | if (result->table) { |
48 | grn_obj_unlink(ctx, result->table); |
49 | } |
50 | mrb_free(mrb, result); |
51 | } |
52 | |
53 | static struct mrb_data_type mrb_grn_table_group_result_type = { |
54 | "Groonga::TableGroupResult" , |
55 | mrb_grn_table_group_result_free |
56 | }; |
57 | |
58 | static mrb_value |
59 | mrb_grn_table_group_result_initialize(mrb_state *mrb, mrb_value self) |
60 | { |
61 | grn_table_group_result *result; |
62 | |
63 | DATA_TYPE(self) = &mrb_grn_table_group_result_type; |
64 | |
65 | result = mrb_calloc(mrb, 1, sizeof(grn_table_group_result)); |
66 | DATA_PTR(self) = result; |
67 | |
68 | return self; |
69 | } |
70 | |
71 | |
72 | static mrb_value |
73 | mrb_grn_table_group_result_close(mrb_state *mrb, mrb_value self) |
74 | { |
75 | grn_table_group_result *result; |
76 | |
77 | result = DATA_PTR(self); |
78 | if (result) { |
79 | mrb_grn_table_group_result_free(mrb, result); |
80 | DATA_PTR(self) = NULL; |
81 | } |
82 | |
83 | return mrb_nil_value(); |
84 | } |
85 | |
86 | static mrb_value |
87 | mrb_grn_table_group_result_get_table(mrb_state *mrb, mrb_value self) |
88 | { |
89 | grn_table_group_result *result; |
90 | |
91 | result = DATA_PTR(self); |
92 | |
93 | return grn_mrb_value_from_grn_obj(mrb, result->table); |
94 | } |
95 | |
96 | static mrb_value |
97 | mrb_grn_table_group_result_set_table(mrb_state *mrb, mrb_value self) |
98 | { |
99 | grn_table_group_result *result; |
100 | mrb_value mrb_table; |
101 | |
102 | result = DATA_PTR(self); |
103 | mrb_get_args(mrb, "o" , &mrb_table); |
104 | |
105 | if (mrb_nil_p(mrb_table)) { |
106 | result->table = NULL; |
107 | } else { |
108 | result->table = DATA_PTR(mrb_table); |
109 | } |
110 | |
111 | return mrb_nil_value(); |
112 | } |
113 | |
114 | static mrb_value |
115 | mrb_grn_table_group_result_set_key_begin(mrb_state *mrb, mrb_value self) |
116 | { |
117 | grn_table_group_result *result; |
118 | mrb_int key_begin; |
119 | |
120 | result = DATA_PTR(self); |
121 | mrb_get_args(mrb, "i" , &key_begin); |
122 | |
123 | result->key_begin = key_begin; |
124 | |
125 | return mrb_nil_value(); |
126 | } |
127 | |
128 | static mrb_value |
129 | mrb_grn_table_group_result_set_key_end(mrb_state *mrb, mrb_value self) |
130 | { |
131 | grn_table_group_result *result; |
132 | mrb_int key_end; |
133 | |
134 | result = DATA_PTR(self); |
135 | mrb_get_args(mrb, "i" , &key_end); |
136 | |
137 | result->key_end = key_end; |
138 | |
139 | return mrb_nil_value(); |
140 | } |
141 | |
142 | static mrb_value |
143 | mrb_grn_table_group_result_set_limit(mrb_state *mrb, mrb_value self) |
144 | { |
145 | grn_table_group_result *result; |
146 | mrb_int limit; |
147 | |
148 | result = DATA_PTR(self); |
149 | mrb_get_args(mrb, "i" , &limit); |
150 | |
151 | result->limit = limit; |
152 | |
153 | return mrb_nil_value(); |
154 | } |
155 | |
156 | static mrb_value |
157 | mrb_grn_table_group_result_set_flags(mrb_state *mrb, mrb_value self) |
158 | { |
159 | grn_table_group_result *result; |
160 | mrb_int flags; |
161 | |
162 | result = DATA_PTR(self); |
163 | mrb_get_args(mrb, "i" , &flags); |
164 | |
165 | result->flags = flags; |
166 | |
167 | return mrb_nil_value(); |
168 | } |
169 | |
170 | static mrb_value |
171 | mrb_grn_table_group_result_set_operator(mrb_state *mrb, mrb_value self) |
172 | { |
173 | grn_table_group_result *result; |
174 | mrb_value mrb_operator; |
175 | |
176 | result = DATA_PTR(self); |
177 | mrb_get_args(mrb, "o" , &mrb_operator); |
178 | |
179 | result->op = grn_mrb_value_to_operator(mrb, mrb_operator); |
180 | |
181 | return mrb_nil_value(); |
182 | } |
183 | |
184 | static mrb_value |
185 | mrb_grn_table_group_result_set_max_n_sub_records(mrb_state *mrb, mrb_value self) |
186 | { |
187 | grn_table_group_result *result; |
188 | mrb_int max_n_sub_records; |
189 | |
190 | result = DATA_PTR(self); |
191 | mrb_get_args(mrb, "i" , &max_n_sub_records); |
192 | |
193 | result->max_n_subrecs = max_n_sub_records; |
194 | |
195 | return mrb_nil_value(); |
196 | } |
197 | |
198 | static mrb_value |
199 | mrb_grn_table_group_result_set_calc_target(mrb_state *mrb, mrb_value self) |
200 | { |
201 | grn_table_group_result *result; |
202 | mrb_value mrb_calc_target; |
203 | |
204 | result = DATA_PTR(self); |
205 | mrb_get_args(mrb, "o" , &mrb_calc_target); |
206 | |
207 | if (mrb_nil_p(mrb_calc_target)) { |
208 | result->calc_target = NULL; |
209 | } else { |
210 | result->calc_target = DATA_PTR(mrb_calc_target); |
211 | } |
212 | |
213 | return mrb_nil_value(); |
214 | } |
215 | |
216 | void |
217 | grn_mrb_table_group_result_init(grn_ctx *ctx) |
218 | { |
219 | grn_mrb_data *data = &(ctx->impl->mrb); |
220 | mrb_state *mrb = data->state; |
221 | struct RClass *module = data->module; |
222 | struct RClass *klass; |
223 | |
224 | klass = mrb_define_class_under(mrb, module, "TableGroupResult" , |
225 | mrb->object_class); |
226 | MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA); |
227 | |
228 | mrb_define_method(mrb, klass, "initialize" , |
229 | mrb_grn_table_group_result_initialize, MRB_ARGS_NONE()); |
230 | |
231 | mrb_define_method(mrb, klass, "close" , |
232 | mrb_grn_table_group_result_close, MRB_ARGS_NONE()); |
233 | |
234 | mrb_define_method(mrb, klass, "table" , |
235 | mrb_grn_table_group_result_get_table, MRB_ARGS_NONE()); |
236 | mrb_define_method(mrb, klass, "table=" , |
237 | mrb_grn_table_group_result_set_table, MRB_ARGS_REQ(1)); |
238 | mrb_define_method(mrb, klass, "key_begin=" , |
239 | mrb_grn_table_group_result_set_key_begin, MRB_ARGS_REQ(1)); |
240 | mrb_define_method(mrb, klass, "key_end=" , |
241 | mrb_grn_table_group_result_set_key_end, MRB_ARGS_REQ(1)); |
242 | mrb_define_method(mrb, klass, "limit=" , |
243 | mrb_grn_table_group_result_set_limit, MRB_ARGS_REQ(1)); |
244 | mrb_define_method(mrb, klass, "flags=" , |
245 | mrb_grn_table_group_result_set_flags, MRB_ARGS_REQ(1)); |
246 | mrb_define_method(mrb, klass, "operator=" , |
247 | mrb_grn_table_group_result_set_operator, MRB_ARGS_REQ(1)); |
248 | mrb_define_method(mrb, klass, "max_n_sub_records=" , |
249 | mrb_grn_table_group_result_set_max_n_sub_records, |
250 | MRB_ARGS_REQ(1)); |
251 | mrb_define_method(mrb, klass, "calc_target=" , |
252 | mrb_grn_table_group_result_set_calc_target, MRB_ARGS_REQ(1)); |
253 | } |
254 | #endif |
255 | |