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