1 | /* -*- c-basic-offset: 2 -*- */ |
2 | /* |
3 | Copyright(C) 2009-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 | #include "../grn_io.h" |
21 | |
22 | #include <groonga/plugin.h> |
23 | |
24 | #include <sys/types.h> |
25 | #include <sys/stat.h> |
26 | |
27 | static grn_obj * |
28 | command_object_exist(grn_ctx *ctx, |
29 | int nargs, |
30 | grn_obj **args, |
31 | grn_user_data *user_data) |
32 | { |
33 | grn_obj *db; |
34 | grn_obj *name; |
35 | grn_id id; |
36 | |
37 | db = grn_ctx_db(ctx); |
38 | name = grn_plugin_proc_get_var(ctx, user_data, "name" , -1); |
39 | if (GRN_TEXT_LEN(name) == 0) { |
40 | GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT, |
41 | "[object][exist] name is missing" ); |
42 | grn_ctx_output_bool(ctx, GRN_FALSE); |
43 | return NULL; |
44 | } |
45 | |
46 | id = grn_table_get(ctx, db, |
47 | GRN_TEXT_VALUE(name), |
48 | GRN_TEXT_LEN(name)); |
49 | grn_ctx_output_bool(ctx, id != GRN_ID_NIL); |
50 | return NULL; |
51 | } |
52 | |
53 | void |
54 | grn_proc_init_object_exist(grn_ctx *ctx) |
55 | { |
56 | grn_expr_var vars[1]; |
57 | |
58 | grn_plugin_expr_var_init(ctx, &(vars[0]), "name" , -1); |
59 | grn_plugin_command_create(ctx, |
60 | "object_exist" , -1, |
61 | command_object_exist, |
62 | 1, |
63 | vars); |
64 | } |
65 | |
66 | static grn_obj * |
67 | command_object_remove(grn_ctx *ctx, |
68 | int nargs, |
69 | grn_obj **args, |
70 | grn_user_data *user_data) |
71 | { |
72 | grn_obj *db; |
73 | grn_obj *name; |
74 | grn_bool force; |
75 | grn_obj *target; |
76 | grn_bool failed_to_open; |
77 | |
78 | db = grn_ctx_db(ctx); |
79 | name = grn_plugin_proc_get_var(ctx, user_data, "name" , -1); |
80 | force = grn_plugin_proc_get_var_bool(ctx, user_data, "force" , -1, GRN_FALSE); |
81 | |
82 | if (GRN_TEXT_LEN(name) == 0) { |
83 | GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT, |
84 | "[object][remove] name is missing" ); |
85 | grn_ctx_output_bool(ctx, GRN_FALSE); |
86 | return NULL; |
87 | } |
88 | |
89 | target = grn_ctx_get(ctx, |
90 | GRN_TEXT_VALUE(name), |
91 | GRN_TEXT_LEN(name)); |
92 | if (target) { |
93 | grn_obj_remove(ctx, target); |
94 | if (!force || ctx->rc == GRN_SUCCESS) { |
95 | grn_ctx_output_bool(ctx, ctx->rc == GRN_SUCCESS); |
96 | return NULL; |
97 | } |
98 | grn_obj_close(ctx, target); |
99 | failed_to_open = GRN_TRUE; |
100 | } else { |
101 | failed_to_open = (ctx->rc != GRN_SUCCESS); |
102 | } |
103 | |
104 | if (force) { |
105 | grn_obj_remove_force(ctx, GRN_TEXT_VALUE(name), GRN_TEXT_LEN(name)); |
106 | grn_ctx_output_bool(ctx, ctx->rc == GRN_SUCCESS); |
107 | } else { |
108 | if (failed_to_open) { |
109 | GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT, |
110 | "[object][remove] " |
111 | "failed to open the target object: <%.*s>" , |
112 | (int)GRN_TEXT_LEN(name), |
113 | GRN_TEXT_VALUE(name)); |
114 | } else { |
115 | GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT, |
116 | "[object][remove] target object doesn't exist: <%.*s>" , |
117 | (int)GRN_TEXT_LEN(name), |
118 | GRN_TEXT_VALUE(name)); |
119 | } |
120 | grn_ctx_output_bool(ctx, GRN_FALSE); |
121 | } |
122 | |
123 | return NULL; |
124 | } |
125 | |
126 | void |
127 | grn_proc_init_object_remove(grn_ctx *ctx) |
128 | { |
129 | grn_expr_var vars[2]; |
130 | |
131 | grn_plugin_expr_var_init(ctx, &(vars[0]), "name" , -1); |
132 | grn_plugin_expr_var_init(ctx, &(vars[1]), "force" , -1); |
133 | grn_plugin_command_create(ctx, |
134 | "object_remove" , -1, |
135 | command_object_remove, |
136 | 2, |
137 | vars); |
138 | } |
139 | |