| 1 | /* |
| 2 | Copyright(C) 2019-2022 Sutou Kouhei <[email protected]> |
| 3 | |
| 4 | This library is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU Lesser General Public |
| 6 | License as published by the Free Software Foundation; either |
| 7 | version 2.1 of the License, or (at your option) any later version. |
| 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"cxx . . $ `<file>/lib/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 | |
| 26 | #include "mrb_converter.h" |
| 27 | #include "mrb_ctx.h" |
| 28 | #include "mrb_window_function_executor.h" |
| 29 | |
| 30 | static void |
| 31 | mrb_grn_window_function_executor_free(mrb_state *mrb, void *data) |
| 32 | { |
| 33 | grn_window_function_executor *executor = data; |
| 34 | |
| 35 | if (!executor) { |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | grn_ctx *ctx = (grn_ctx *)mrb->ud; |
| 40 | grn_window_function_executor_close(ctx, executor); |
| 41 | } |
| 42 | |
| 43 | static struct mrb_data_type mrb_grn_window_function_executor_type = { |
| 44 | "Groonga::WindowFunctionExecutor", |
| 45 | mrb_grn_window_function_executor_free |
| 46 | }; |
| 47 | |
| 48 | static mrb_value |
| 49 | mrb_grn_window_function_executor_initialize(mrb_state *mrb, mrb_value self) |
| 50 | { |
| 51 | DATA_TYPE(self) = &mrb_grn_window_function_executor_type; |
| 52 | |
| 53 | grn_ctx *ctx = (grn_ctx *)mrb->ud; |
| 54 | grn_window_function_executor *executor = |
| 55 | grn_window_function_executor_open(ctx); |
| 56 | grn_mrb_ctx_check(mrb); |
| 57 | DATA_PTR(self) = executor; |
| 58 | |
| 59 | return self; |
| 60 | } |
| 61 | |
| 62 | static mrb_value |
| 63 | mrb_grn_window_function_executor_close(mrb_state *mrb, mrb_value self) |
| 64 | { |
| 65 | grn_window_function_executor *executor = DATA_PTR(self); |
| 66 | if (executor) { |
| 67 | mrb_grn_window_function_executor_free(mrb, executor); |
| 68 | DATA_PTR(self) = NULL; |
| 69 | } |
| 70 | |
| 71 | return mrb_nil_value(); |
| 72 | } |
| 73 | |
| 74 | static mrb_value |
| 75 | mrb_grn_window_function_executor_set_source(mrb_state *mrb, mrb_value self) |
| 76 | { |
| 77 | char *source; |
| 78 | mrb_int source_size; |
| 79 | mrb_get_args(mrb, "s!", &source, &source_size); |
| 80 | |
| 81 | grn_ctx *ctx = (grn_ctx *)mrb->ud; |
| 82 | grn_window_function_executor *executor = DATA_PTR(self); |
| 83 | grn_window_function_executor_set_source(ctx, |
| 84 | executor, |
| 85 | source, |
| 86 | (int)source_size); |
| 87 | grn_mrb_ctx_check(mrb); |
| 88 | |
| 89 | return mrb_nil_value(); |
| 90 | } |
| 91 | |
| 92 | static mrb_value |
| 93 | mrb_grn_window_function_executor_add_table(mrb_state *mrb, mrb_value self) |
| 94 | { |
| 95 | mrb_value mrb_table; |
| 96 | mrb_get_args(mrb, "o", &mrb_table); |
| 97 | |
| 98 | grn_ctx *ctx = (grn_ctx *)mrb->ud; |
| 99 | grn_window_function_executor *executor = DATA_PTR(self); |
| 100 | grn_obj *table = GRN_MRB_DATA_PTR(mrb_table); |
| 101 | grn_window_function_executor_add_table(ctx, executor, table); |
| 102 | grn_mrb_ctx_check(mrb); |
| 103 | |
| 104 | return mrb_nil_value(); |
| 105 | } |
| 106 | |
| 107 | static mrb_value |
| 108 | mrb_grn_window_function_executor_add_context_table(mrb_state *mrb, mrb_value self) |
| 109 | { |
| 110 | mrb_value mrb_table; |
| 111 | mrb_get_args(mrb, "o", &mrb_table); |
| 112 | |
| 113 | grn_ctx *ctx = (grn_ctx *)mrb->ud; |
| 114 | grn_window_function_executor *executor = DATA_PTR(self); |
| 115 | grn_obj *table = GRN_MRB_DATA_PTR(mrb_table); |
| 116 | grn_window_function_executor_add_context_table(ctx, executor, table); |
| 117 | grn_mrb_ctx_check(mrb); |
| 118 | |
| 119 | return mrb_nil_value(); |
| 120 | } |
| 121 | |
| 122 | static mrb_value |
| 123 | mrb_grn_window_function_executor_set_sort_keys(mrb_state *mrb, mrb_value self) |
| 124 | { |
| 125 | char *keys; |
| 126 | mrb_int keys_size; |
| 127 | mrb_get_args(mrb, "s!", &keys, &keys_size); |
| 128 | |
| 129 | grn_ctx *ctx = (grn_ctx *)mrb->ud; |
| 130 | grn_window_function_executor *executor = DATA_PTR(self); |
| 131 | grn_window_function_executor_set_sort_keys(ctx, |
| 132 | executor, |
| 133 | keys, |
| 134 | (int)keys_size); |
| 135 | grn_mrb_ctx_check(mrb); |
| 136 | |
| 137 | return mrb_nil_value(); |
| 138 | } |
| 139 | |
| 140 | static mrb_value |
| 141 | mrb_grn_window_function_executor_set_group_keys(mrb_state *mrb, mrb_value self) |
| 142 | { |
| 143 | char *keys; |
| 144 | mrb_int keys_size; |
| 145 | mrb_get_args(mrb, "s!", &keys, &keys_size); |
| 146 | |
| 147 | grn_ctx *ctx = (grn_ctx *)mrb->ud; |
| 148 | grn_window_function_executor *executor = DATA_PTR(self); |
| 149 | grn_window_function_executor_set_group_keys(ctx, |
| 150 | executor, |
| 151 | keys, |
| 152 | (int)keys_size); |
| 153 | grn_mrb_ctx_check(mrb); |
| 154 | |
| 155 | return mrb_nil_value(); |
| 156 | } |
| 157 | |
| 158 | static mrb_value |
| 159 | mrb_grn_window_function_executor_set_output_column_name(mrb_state *mrb, |
| 160 | mrb_value self) |
| 161 | { |
| 162 | char *name; |
| 163 | mrb_int name_size; |
| 164 | mrb_get_args(mrb, "s!", &name, &name_size); |
| 165 | |
| 166 | grn_ctx *ctx = (grn_ctx *)mrb->ud; |
| 167 | grn_window_function_executor *executor = DATA_PTR(self); |
| 168 | grn_window_function_executor_set_output_column_name(ctx, |
| 169 | executor, |
| 170 | name, |
| 171 | (int)name_size); |
| 172 | grn_mrb_ctx_check(mrb); |
| 173 | |
| 174 | return mrb_nil_value(); |
| 175 | } |
| 176 | |
| 177 | static mrb_value |
| 178 | mrb_grn_window_function_executor_execute(mrb_state *mrb, mrb_value self) |
| 179 | { |
| 180 | grn_ctx *ctx = (grn_ctx *)mrb->ud; |
| 181 | grn_window_function_executor *executor = DATA_PTR(self); |
| 182 | grn_window_function_executor_execute(ctx, executor); |
| 183 | grn_mrb_ctx_check(mrb); |
| 184 | |
| 185 | return mrb_nil_value(); |
| 186 | } |
| 187 | |
| 188 | void |
| 189 | grn_mrb_window_function_executor_init(grn_ctx *ctx) |
| 190 | { |
| 191 | grn_mrb_data *data = &(ctx->impl->mrb); |
| 192 | mrb_state *mrb = data->state; |
| 193 | struct RClass *module = data->module; |
| 194 | struct RClass *klass; |
| 195 | |
| 196 | klass = mrb_define_class_under(mrb, module, "WindowFunctionExecutor", |
| 197 | mrb->object_class); |
| 198 | MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA); |
| 199 | |
| 200 | mrb_define_method(mrb, klass, "initialize", |
| 201 | mrb_grn_window_function_executor_initialize, |
| 202 | MRB_ARGS_NONE()); |
| 203 | |
| 204 | mrb_define_method(mrb, klass, "close", |
| 205 | mrb_grn_window_function_executor_close, MRB_ARGS_NONE()); |
| 206 | |
| 207 | mrb_define_method(mrb, klass, "add_table", |
| 208 | mrb_grn_window_function_executor_add_table, |
| 209 | MRB_ARGS_REQ(1)); |
| 210 | mrb_define_method(mrb, klass, "add_context_table", |
| 211 | mrb_grn_window_function_executor_add_context_table, |
| 212 | MRB_ARGS_REQ(1)); |
| 213 | mrb_define_method(mrb, klass, "source=", |
| 214 | mrb_grn_window_function_executor_set_source, |
| 215 | MRB_ARGS_REQ(1)); |
| 216 | mrb_define_method(mrb, klass, "sort_keys=", |
| 217 | mrb_grn_window_function_executor_set_sort_keys, |
| 218 | MRB_ARGS_REQ(1)); |
| 219 | mrb_define_method(mrb, klass, "group_keys=", |
| 220 | mrb_grn_window_function_executor_set_group_keys, |
| 221 | MRB_ARGS_REQ(1)); |
| 222 | mrb_define_method(mrb, klass, "output_column_name=", |
| 223 | mrb_grn_window_function_executor_set_output_column_name, |
| 224 | MRB_ARGS_REQ(1)); |
| 225 | |
| 226 | mrb_define_method(mrb, klass, "execute", |
| 227 | mrb_grn_window_function_executor_execute, |
| 228 | MRB_ARGS_NONE()); |
| 229 | } |
| 230 | #endif |