1/* -*- c-basic-offset: 2 -*- */
2/*
3 Copyright(C) 2016 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_proc.h"
20
21#include <groonga/plugin.h>
22
23static grn_obj *
24command_query_expand(grn_ctx *ctx, int nargs, grn_obj **args,
25 grn_user_data *user_data)
26{
27 const char *expander;
28 size_t expander_size;
29 const char *query;
30 size_t query_size;
31 const char *flags_raw;
32 size_t flags_raw_size;
33 grn_expr_flags flags = GRN_EXPR_SYNTAX_QUERY;
34 const char *term_column;
35 size_t term_column_size;
36 const char *expanded_term_column;
37 size_t expanded_term_column_size;
38 grn_obj expanded_query;
39
40 expander = grn_plugin_proc_get_var_string(ctx,
41 user_data,
42 "expander",
43 -1,
44 &expander_size);
45 query = grn_plugin_proc_get_var_string(ctx,
46 user_data,
47 "query",
48 -1,
49 &query_size);
50 flags_raw = grn_plugin_proc_get_var_string(ctx,
51 user_data,
52 "flags",
53 -1,
54 &flags_raw_size);
55 term_column = grn_plugin_proc_get_var_string(ctx,
56 user_data,
57 "term_column",
58 -1,
59 &term_column_size);
60 expanded_term_column =
61 grn_plugin_proc_get_var_string(ctx,
62 user_data,
63 "expanded_term_column",
64 -1,
65 &expanded_term_column_size);
66
67 if (flags_raw_size > 0) {
68 flags |= grn_proc_expr_query_flags_parse(ctx,
69 flags_raw,
70 flags_raw_size,
71 "[query][expand]");
72 } else {
73 flags |= GRN_EXPR_ALLOW_PRAGMA | GRN_EXPR_ALLOW_COLUMN;
74 }
75
76 if (ctx->rc != GRN_SUCCESS) {
77 return NULL;
78 }
79
80 GRN_TEXT_INIT(&expanded_query, 0);
81 grn_proc_syntax_expand_query(ctx,
82 query,
83 query_size,
84 flags,
85 expander,
86 expander_size,
87 term_column,
88 term_column_size,
89 expanded_term_column,
90 expanded_term_column_size,
91 &expanded_query,
92 "[query][expand]");
93 if (ctx->rc == GRN_SUCCESS) {
94 grn_ctx_output_str(ctx,
95 GRN_TEXT_VALUE(&expanded_query),
96 GRN_TEXT_LEN(&expanded_query));
97 }
98 GRN_OBJ_FIN(ctx, &expanded_query);
99
100 return NULL;
101}
102
103void
104grn_proc_init_query_expand(grn_ctx *ctx)
105{
106 grn_expr_var vars[5];
107
108 grn_plugin_expr_var_init(ctx, &(vars[0]), "expander", -1);
109 grn_plugin_expr_var_init(ctx, &(vars[1]), "query", -1);
110 grn_plugin_expr_var_init(ctx, &(vars[2]), "flags", -1);
111 grn_plugin_expr_var_init(ctx, &(vars[3]), "term_column", -1);
112 grn_plugin_expr_var_init(ctx, &(vars[4]), "expanded_term_column", -1);
113 grn_plugin_command_create(ctx,
114 "query_expand", -1,
115 command_query_expand,
116 5,
117 vars);
118}
119