1/* -*- c-basic-offset: 2 -*- */
2/*
3 Copyright(C) 2013-2017 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#include "../grn_proc.h"
21
22#ifdef GRN_WITH_MRUBY
23#include <mruby.h>
24#include <mruby/class.h>
25#include <mruby/data.h>
26
27#include "mrb_ctx.h"
28#include "mrb_column.h"
29#include "mrb_bulk.h"
30#include "mrb_converter.h"
31
32static mrb_value
33mrb_grn_column_class_parse_flags(mrb_state *mrb, mrb_value self)
34{
35 grn_ctx *ctx = (grn_ctx *)mrb->ud;
36 char *error_message_tag;
37 char *flags_text;
38 mrb_int flags_text_size;
39 grn_column_flags flags;
40
41 mrb_get_args(mrb, "zs", &error_message_tag, &flags_text, &flags_text_size);
42
43 flags = grn_proc_column_parse_flags(ctx,
44 error_message_tag,
45 flags_text,
46 flags_text + flags_text_size);
47 return mrb_fixnum_value(flags);
48}
49
50static mrb_value
51mrb_grn_column_array_reference(mrb_state *mrb, mrb_value self)
52{
53 grn_ctx *ctx = (grn_ctx *)mrb->ud;
54 grn_obj *column;
55 mrb_int record_id;
56 grn_obj *column_value;
57
58 column = DATA_PTR(self);
59 mrb_get_args(mrb, "i", &record_id);
60
61 column_value = grn_obj_get_value(ctx, column, record_id, NULL);
62 return grn_mrb_value_from_grn_obj(mrb, column_value);
63}
64
65static mrb_value
66mrb_grn_column_is_scalar(mrb_state *mrb, mrb_value self)
67{
68 grn_obj *column;
69 grn_obj_flags column_type;
70
71 column = DATA_PTR(self);
72 column_type = (column->header.flags & GRN_OBJ_COLUMN_TYPE_MASK);
73
74 return mrb_bool_value(column_type == GRN_OBJ_COLUMN_SCALAR);
75}
76
77static mrb_value
78mrb_grn_column_is_vector(mrb_state *mrb, mrb_value self)
79{
80 grn_obj *column;
81 grn_obj_flags column_type;
82
83 column = DATA_PTR(self);
84 column_type = (column->header.flags & GRN_OBJ_COLUMN_TYPE_MASK);
85
86 return mrb_bool_value(column_type == GRN_OBJ_COLUMN_VECTOR);
87}
88
89static mrb_value
90mrb_grn_column_is_index(mrb_state *mrb, mrb_value self)
91{
92 grn_obj *column;
93 grn_obj_flags column_type;
94
95 column = DATA_PTR(self);
96 column_type = (column->header.flags & GRN_OBJ_COLUMN_TYPE_MASK);
97
98 return mrb_bool_value(column_type == GRN_OBJ_COLUMN_INDEX);
99}
100
101static mrb_value
102mrb_grn_column_is_locked(mrb_state *mrb, mrb_value self)
103{
104 grn_ctx *ctx = (grn_ctx *)mrb->ud;
105 unsigned int is_locked;
106
107 is_locked = grn_obj_is_locked(ctx, DATA_PTR(self));
108 grn_mrb_ctx_check(mrb);
109
110 return mrb_bool_value(is_locked != 0);
111}
112
113static mrb_value
114mrb_grn_column_get_table(mrb_state *mrb, mrb_value self)
115{
116 grn_ctx *ctx = (grn_ctx *)mrb->ud;
117 grn_obj *table;
118
119 table = grn_column_table(ctx, DATA_PTR(self));
120 if (!table) {
121 return mrb_nil_value();
122 }
123
124 return grn_mrb_value_from_grn_obj(mrb, table);
125}
126
127static mrb_value
128mrb_grn_column_truncate(mrb_state *mrb, mrb_value self)
129{
130 grn_ctx *ctx = (grn_ctx *)mrb->ud;
131 grn_obj *column;
132
133 column = DATA_PTR(self);
134 grn_column_truncate(ctx, column);
135 grn_mrb_ctx_check(mrb);
136 return mrb_nil_value();
137}
138
139void
140grn_mrb_column_init(grn_ctx *ctx)
141{
142 grn_mrb_data *data = &(ctx->impl->mrb);
143 mrb_state *mrb = data->state;
144 struct RClass *module = data->module;
145 struct RClass *object_class = data->object_class;
146 struct RClass *klass;
147
148 klass = mrb_define_class_under(mrb, module, "Column", object_class);
149 MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA);
150
151 mrb_define_class_method(mrb, klass, "parse_flags",
152 mrb_grn_column_class_parse_flags, MRB_ARGS_REQ(2));
153
154 mrb_define_method(mrb, klass, "[]",
155 mrb_grn_column_array_reference, MRB_ARGS_REQ(1));
156
157 mrb_define_method(mrb, klass, "scalar?",
158 mrb_grn_column_is_scalar, MRB_ARGS_NONE());
159 mrb_define_method(mrb, klass, "vector?",
160 mrb_grn_column_is_vector, MRB_ARGS_NONE());
161 mrb_define_method(mrb, klass, "index?",
162 mrb_grn_column_is_index, MRB_ARGS_NONE());
163
164 mrb_define_method(mrb, klass, "locked?",
165 mrb_grn_column_is_locked, MRB_ARGS_NONE());
166
167 mrb_define_method(mrb, klass, "table",
168 mrb_grn_column_get_table, MRB_ARGS_NONE());
169
170 mrb_define_method(mrb, klass, "truncate",
171 mrb_grn_column_truncate, MRB_ARGS_NONE());
172}
173#endif
174