1/* -*- c-basic-offset: 2 -*- */
2/*
3 Copyright(C) 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.h"
20#include "grn_ctx.h"
21#include "grn_expr_executor.h"
22
23grn_rc
24grn_table_apply_expr(grn_ctx *ctx,
25 grn_obj *table,
26 grn_obj *output_column,
27 grn_obj *expr)
28{
29 grn_expr_executor *executor;
30
31 GRN_API_ENTER;
32
33 if (!grn_obj_is_data_column(ctx, output_column)) {
34 grn_obj inspected;
35 GRN_TEXT_INIT(&inspected, 0);
36 grn_inspect(ctx, &inspected, output_column);
37 ERR(GRN_INVALID_ARGUMENT,
38 "[table][apply-expr] output column isn't data column: %.*s",
39 (int)GRN_TEXT_LEN(&inspected),
40 GRN_TEXT_VALUE(&inspected));
41 GRN_OBJ_FIN(ctx, &inspected);
42 GRN_API_RETURN(ctx->rc);
43 }
44
45 if (!grn_obj_is_expr(ctx, expr)) {
46 grn_obj inspected;
47 GRN_TEXT_INIT(&inspected, 0);
48 grn_inspect(ctx, &inspected, expr);
49 ERR(GRN_INVALID_ARGUMENT,
50 "[table][apply-expr] expr is invalid: %.*s",
51 (int)GRN_TEXT_LEN(&inspected),
52 GRN_TEXT_VALUE(&inspected));
53 GRN_OBJ_FIN(ctx, &inspected);
54 GRN_API_RETURN(ctx->rc);
55 }
56
57 executor = grn_expr_executor_open(ctx, expr);
58 if (!executor) {
59 GRN_API_RETURN(ctx->rc);
60 }
61 GRN_TABLE_EACH_BEGIN_FLAGS(ctx, table, cursor, id, GRN_CURSOR_BY_ID) {
62 grn_obj *value;
63 value = grn_expr_executor_exec(ctx, executor, id);
64 if (ctx->rc != GRN_SUCCESS) {
65 break;
66 }
67 if (value) {
68 grn_obj_set_value(ctx, output_column, id, value, GRN_OBJ_SET);
69 }
70 } GRN_TABLE_EACH_END(ctx, cursor);
71 grn_expr_executor_close(ctx, executor);
72
73 GRN_API_RETURN(ctx->rc);
74}
75
76grn_id
77grn_table_find_reference_object(grn_ctx *ctx, grn_obj *table)
78{
79 grn_id table_id;
80 grn_id reference_object_id = GRN_ID_NIL;
81
82 GRN_API_ENTER;
83
84 if (!grn_obj_is_table(ctx, table)) {
85 GRN_API_RETURN(GRN_ID_NIL);
86 }
87
88 table_id = DB_OBJ(table)->id;
89
90 GRN_DB_SPEC_EACH_BEGIN(ctx, cursor, id, spec) {
91 if (id == table_id) {
92 continue;
93 }
94
95 switch (spec->header.type) {
96 case GRN_TABLE_HASH_KEY :
97 case GRN_TABLE_PAT_KEY :
98 case GRN_TABLE_DAT_KEY :
99 if (spec->header.domain == table_id) {
100 reference_object_id = id;
101 }
102 break;
103 case GRN_COLUMN_VAR_SIZE :
104 case GRN_COLUMN_FIX_SIZE :
105 if (spec->header.domain == table_id) {
106 break;
107 }
108 if (spec->range == table_id) {
109 reference_object_id = id;
110 }
111 break;
112 default :
113 break;
114 }
115
116 if (reference_object_id != GRN_ID_NIL) {
117 break;
118 }
119 } GRN_DB_SPEC_EACH_END(ctx, cursor);
120
121 GRN_API_RETURN(reference_object_id);
122}
123