| 1 | /* |
| 2 | Copyright (C) 2009-2018 Brazil |
| 3 | Copyright (C) 2018-2026 Sutou Kouhei <[email protected]> |
| 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 as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | This library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with this library; if not, write to the Free Software |
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include "../grn_proc.h" |
| 21 | |
| 22 | #include "../grn_ctx.h" |
| 23 | #include "../grn_str.h" |
| 24 | #include "../grn_db.h" |
| 25 | |
| 26 | #include <groonga/plugin.h> |
| 27 | |
| 28 | static grn_table_flags |
| 29 | command_table_create_parse_flags(grn_ctx *ctx, |
| 30 | const char *nptr, |
| 31 | const char *end) |
| 32 | { |
| 33 | grn_table_flags flags = 0; |
| 34 | while (nptr < end) { |
| 35 | size_t name_size; |
| 36 | |
| 37 | if (*nptr == '|' || *nptr == ' ') { |
| 38 | nptr += 1; |
| 39 | continue; |
| 40 | } |
| 41 | |
| 42 | #define CHECK_FLAG(name) \ |
| 43 | name_size = strlen(#name); \ |
| 44 | if ((size_t)(end - nptr) >= name_size && \ |
| 45 | memcmp(nptr, #name, name_size) == 0) { \ |
| 46 | flags |= GRN_OBJ_ ## name; \ |
| 47 | nptr += name_size; \ |
| 48 | continue; \ |
| 49 | } |
| 50 | |
| 51 | CHECK_FLAG(TABLE_HASH_KEY); |
| 52 | CHECK_FLAG(TABLE_PAT_KEY); |
| 53 | CHECK_FLAG(TABLE_DAT_KEY); |
| 54 | CHECK_FLAG(TABLE_NO_KEY); |
| 55 | CHECK_FLAG(KEY_NORMALIZE); |
| 56 | CHECK_FLAG(KEY_WITH_SIS); |
| 57 | CHECK_FLAG(KEY_LARGE); |
| 58 | |
| 59 | #undef CHECK_FLAG |
| 60 | |
| 61 | GRN_PLUGIN_ERROR(ctx, |
| 62 | GRN_INVALID_ARGUMENT, |
| 63 | "[table][create][flags] unknown flag: <%.*s>", |
| 64 | (int)(end - nptr), nptr); |
| 65 | return 0; |
| 66 | } |
| 67 | return flags; |
| 68 | } |
| 69 | |
| 70 | bool |
| 71 | grn_proc_table_set_token_filters(grn_ctx *ctx, |
| 72 | grn_obj *table, |
| 73 | grn_raw_string *token_filters_raw) |
| 74 | { |
| 75 | grn_obj token_filters; |
| 76 | |
| 77 | if (token_filters_raw->length == 0) { |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | GRN_TEXT_INIT(&token_filters, GRN_OBJ_DO_SHALLOW_COPY); |
| 82 | GRN_TEXT_SET(ctx, |
| 83 | &token_filters, |
| 84 | token_filters_raw->value, |
| 85 | token_filters_raw->length); |
| 86 | grn_obj_set_info(ctx, table, GRN_INFO_TOKEN_FILTERS, &token_filters); |
| 87 | GRN_OBJ_FIN(ctx, &token_filters); |
| 88 | |
| 89 | return ctx->rc == GRN_SUCCESS; |
| 90 | } |
| 91 | |
| 92 | bool |
| 93 | grn_proc_table_set_extractors(grn_ctx *ctx, |
| 94 | grn_obj *table, |
| 95 | grn_raw_string *extractors_raw) |
| 96 | { |
| 97 | if (extractors_raw->length == 0) { |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | grn_obj extractors; |
| 102 | GRN_TEXT_INIT(&extractors, GRN_OBJ_DO_SHALLOW_COPY); |
| 103 | GRN_TEXT_SET(ctx, |
| 104 | &extractors, |
| 105 | extractors_raw->value, |
| 106 | extractors_raw->length); |
| 107 | grn_obj_set_info(ctx, table, GRN_INFO_EXTRACTORS, &extractors); |
| 108 | GRN_OBJ_FIN(ctx, &extractors); |
| 109 | |
| 110 | return ctx->rc == GRN_SUCCESS; |
| 111 | } |
| 112 | |
| 113 | static grn_obj * |
| 114 | command_table_create(grn_ctx *ctx, |
| 115 | int nargs, |
| 116 | grn_obj **args, |
| 117 | grn_user_data *user_data) |
| 118 | { |
| 119 | grn_raw_string name_raw; |
| 120 | grn_raw_string flags_raw; |
| 121 | grn_raw_string key_type_raw; |
| 122 | grn_raw_string value_type_raw; |
| 123 | grn_obj *default_tokenizer_raw; |
| 124 | grn_obj *normalizer_raw; |
| 125 | grn_raw_string token_filters_raw; |
| 126 | grn_raw_string extractors_raw; |
| 127 | grn_obj *path_raw; |
| 128 | grn_obj *normalizers_raw; |
| 129 | grn_obj *key_type = NULL; |
| 130 | grn_obj *value_type = NULL; |
| 131 | grn_obj *table = NULL; |
| 132 | const char *rest; |
| 133 | grn_table_flags flags; |
| 134 | |
| 135 | #define GET_VALUE(name) \ |
| 136 | name ## _raw.value = \ |
| 137 | grn_plugin_proc_get_var_string(ctx, \ |
| 138 | user_data, \ |
| 139 | #name, \ |
| 140 | strlen(#name), \ |
| 141 | &(name ## _raw.length)) |
| 142 | |
| 143 | GET_VALUE(name); |
| 144 | GET_VALUE(flags); |
| 145 | GET_VALUE(key_type); |
| 146 | GET_VALUE(value_type); |
| 147 | default_tokenizer_raw = grn_plugin_proc_get_var(ctx, user_data, |
| 148 | "default_tokenizer", -1); |
| 149 | normalizer_raw = grn_plugin_proc_get_var(ctx, user_data, "normalizer", -1); |
| 150 | GET_VALUE(token_filters); |
| 151 | GET_VALUE(extractors); |
| 152 | path_raw = grn_plugin_proc_get_var(ctx, user_data, "path", -1); |
| 153 | normalizers_raw = grn_plugin_proc_get_var(ctx, user_data, "normalizers", -1); |
| 154 | |
| 155 | #undef GET_VALUE |
| 156 | |
| 157 | flags = (grn_table_flags)grn_atoi(flags_raw.value, |
| 158 | flags_raw.value + flags_raw.length, |
| 159 | &rest); |
| 160 | if (flags_raw.value == rest) { |
| 161 | flags = command_table_create_parse_flags(ctx, |
| 162 | flags_raw.value, |
| 163 | flags_raw.value + flags_raw.length); |
| 164 | if (ctx->rc) { goto exit; } |
| 165 | } |
| 166 | |
| 167 | if (name_raw.length == 0) { |
| 168 | GRN_PLUGIN_ERROR(ctx, |
| 169 | GRN_INVALID_ARGUMENT, |
| 170 | "[table][create] should not create anonymous table"); |
| 171 | goto exit; |
| 172 | } |
| 173 | |
| 174 | { |
| 175 | if (key_type_raw.length > 0) { |
| 176 | key_type = grn_ctx_get(ctx, |
| 177 | key_type_raw.value, |
| 178 | (int)(key_type_raw.length)); |
| 179 | if (!key_type) { |
| 180 | GRN_PLUGIN_ERROR(ctx, |
| 181 | GRN_INVALID_ARGUMENT, |
| 182 | "[table][create] " |
| 183 | "key type doesn't exist: <%.*s> (%.*s)", |
| 184 | (int)name_raw.length, |
| 185 | name_raw.value, |
| 186 | (int)key_type_raw.length, |
| 187 | key_type_raw.value); |
| 188 | goto exit; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | if (value_type_raw.length > 0) { |
| 193 | value_type = grn_ctx_get(ctx, |
| 194 | value_type_raw.value, |
| 195 | (int)(value_type_raw.length)); |
| 196 | if (!value_type) { |
| 197 | GRN_PLUGIN_ERROR(ctx, |
| 198 | GRN_INVALID_ARGUMENT, |
| 199 | "[table][create] " |
| 200 | "value type doesn't exist: <%.*s> (%.*s)", |
| 201 | (int)name_raw.length, |
| 202 | name_raw.value, |
| 203 | (int)value_type_raw.length, |
| 204 | value_type_raw.value); |
| 205 | goto exit; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | const char *path = NULL; |
| 210 | if (GRN_TEXT_LEN(path_raw) > 0) { |
| 211 | GRN_TEXT_PUTC(ctx, path_raw, '\0'); |
| 212 | path = GRN_TEXT_VALUE(path_raw); |
| 213 | } |
| 214 | flags |= GRN_OBJ_PERSISTENT; |
| 215 | table = grn_table_create(ctx, |
| 216 | name_raw.value, |
| 217 | (unsigned int)(name_raw.length), |
| 218 | path, |
| 219 | flags, |
| 220 | key_type, |
| 221 | value_type); |
| 222 | if (!table) { |
| 223 | goto exit; |
| 224 | } |
| 225 | |
| 226 | if (GRN_TEXT_LEN(default_tokenizer_raw) > 0) { |
| 227 | grn_obj_set_info(ctx, table, |
| 228 | GRN_INFO_DEFAULT_TOKENIZER, |
| 229 | default_tokenizer_raw); |
| 230 | if (ctx->rc != GRN_SUCCESS) { |
| 231 | GRN_PLUGIN_ERROR(ctx, |
| 232 | GRN_INVALID_ARGUMENT, |
| 233 | "[table][create][%.*s] " |
| 234 | "failed to set default tokenizer: <%.*s>: %s", |
| 235 | (int)name_raw.length, |
| 236 | name_raw.value, |
| 237 | (int)GRN_TEXT_LEN(default_tokenizer_raw), |
| 238 | GRN_TEXT_VALUE(default_tokenizer_raw), |
| 239 | ctx->errbuf); |
| 240 | goto exit; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | if (GRN_TEXT_LEN(normalizers_raw) == 0 && |
| 245 | GRN_TEXT_LEN(normalizer_raw) > 0) { |
| 246 | normalizers_raw = normalizer_raw; |
| 247 | } |
| 248 | if (GRN_TEXT_LEN(normalizers_raw) > 0) { |
| 249 | grn_obj_set_info(ctx, table, GRN_INFO_NORMALIZERS, normalizers_raw); |
| 250 | if (ctx->rc != GRN_SUCCESS) { |
| 251 | GRN_PLUGIN_ERROR(ctx, |
| 252 | GRN_INVALID_ARGUMENT, |
| 253 | "[table][create][%.*s] " |
| 254 | "failed to set normalizers: <%.*s>: %s", |
| 255 | (int)name_raw.length, |
| 256 | name_raw.value, |
| 257 | (int)GRN_TEXT_LEN(normalizers_raw), |
| 258 | GRN_TEXT_VALUE(normalizers_raw), |
| 259 | ctx->errbuf); |
| 260 | goto exit; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | if (!grn_proc_table_set_token_filters(ctx, table, &token_filters_raw)) { |
| 265 | goto exit; |
| 266 | } |
| 267 | |
| 268 | if (!grn_proc_table_set_extractors(ctx, table, &extractors_raw)) { |
| 269 | goto exit; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | exit : |
| 274 | { |
| 275 | bool success = (ctx->rc == GRN_SUCCESS); |
| 276 | if (key_type) { |
| 277 | grn_obj_unref(ctx, key_type); |
| 278 | } |
| 279 | if (value_type) { |
| 280 | grn_obj_unref(ctx, value_type); |
| 281 | } |
| 282 | if (success) { |
| 283 | if (table) { |
| 284 | grn_obj_unlink(ctx, table); |
| 285 | } |
| 286 | } else { |
| 287 | if (table) { |
| 288 | grn_obj_remove(ctx, table); |
| 289 | } |
| 290 | } |
| 291 | grn_ctx_output_bool(ctx, success); |
| 292 | } |
| 293 | return NULL; |
| 294 | } |
| 295 | |
| 296 | void |
| 297 | grn_proc_init_table_create(grn_ctx *ctx) |
| 298 | { |
| 299 | grn_expr_var vars[10]; |
| 300 | |
| 301 | grn_plugin_expr_var_init(ctx, &(vars[0]), "name", -1); |
| 302 | grn_plugin_expr_var_init(ctx, &(vars[1]), "flags", -1); |
| 303 | grn_plugin_expr_var_init(ctx, &(vars[2]), "key_type", -1); |
| 304 | grn_plugin_expr_var_init(ctx, &(vars[3]), "value_type", -1); |
| 305 | grn_plugin_expr_var_init(ctx, &(vars[4]), "default_tokenizer", -1); |
| 306 | grn_plugin_expr_var_init(ctx, &(vars[5]), "normalizer", -1); |
| 307 | grn_plugin_expr_var_init(ctx, &(vars[6]), "token_filters", -1); |
| 308 | grn_plugin_expr_var_init(ctx, &(vars[7]), "path", -1); |
| 309 | grn_plugin_expr_var_init(ctx, &(vars[8]), "normalizers", -1); |
| 310 | grn_plugin_expr_var_init(ctx, &(vars[9]), "extractors", -1); |
| 311 | grn_plugin_command_create(ctx, |
| 312 | "table_create", -1, |
| 313 | command_table_create, |
| 314 | 10, |
| 315 | vars); |
| 316 | } |
| 317 | |
| 318 | static void |
| 319 | output_table_info(grn_ctx *ctx, grn_obj *table) |
| 320 | { |
| 321 | grn_obj buffer; |
| 322 | grn_id id; |
| 323 | const char *path; |
| 324 | grn_table_flags flags; |
| 325 | grn_obj *default_tokenizer; |
| 326 | grn_obj *normalizer; |
| 327 | grn_obj *token_filters; |
| 328 | |
| 329 | GRN_TEXT_INIT(&buffer, 0); |
| 330 | |
| 331 | grn_ctx_output_array_open(ctx, "TABLE", 8); |
| 332 | |
| 333 | id = grn_obj_id(ctx, table); |
| 334 | grn_ctx_output_int64(ctx, id); |
| 335 | grn_proc_output_object_id_name(ctx, id); |
| 336 | path = grn_obj_path(ctx, table); |
| 337 | grn_ctx_output_cstr(ctx, path); |
| 338 | |
| 339 | grn_table_get_info(ctx, table, |
| 340 | &flags, |
| 341 | NULL, |
| 342 | &default_tokenizer, |
| 343 | &normalizer, |
| 344 | &token_filters); |
| 345 | |
| 346 | grn_dump_table_create_flags(ctx, flags, &buffer); |
| 347 | grn_ctx_output_obj(ctx, &buffer, NULL); |
| 348 | |
| 349 | grn_proc_output_object_id_name(ctx, table->header.domain); |
| 350 | grn_proc_output_object_id_name(ctx, grn_obj_get_range(ctx, table)); |
| 351 | |
| 352 | if (default_tokenizer) { |
| 353 | GRN_BULK_REWIND(&buffer); |
| 354 | grn_table_get_default_tokenizer_string(ctx, table, &buffer); |
| 355 | grn_ctx_output_obj(ctx, &buffer, NULL); |
| 356 | } else { |
| 357 | grn_ctx_output_null(ctx); |
| 358 | } |
| 359 | |
| 360 | if (normalizer) { |
| 361 | GRN_BULK_REWIND(&buffer); |
| 362 | grn_table_get_normalizers_string(ctx, table, &buffer); |
| 363 | grn_ctx_output_obj(ctx, &buffer, NULL); |
| 364 | } else { |
| 365 | grn_ctx_output_null(ctx); |
| 366 | } |
| 367 | |
| 368 | grn_ctx_output_array_close(ctx); |
| 369 | |
| 370 | GRN_OBJ_FIN(ctx, &buffer); |
| 371 | } |
| 372 | |
| 373 | static grn_obj * |
| 374 | command_table_list(grn_ctx *ctx, int nargs, grn_obj **args, |
| 375 | grn_user_data *user_data) |
| 376 | { |
| 377 | grn_obj *db; |
| 378 | grn_obj tables; |
| 379 | int n_elements_for_header = 1; |
| 380 | |
| 381 | db = grn_ctx_db(ctx); |
| 382 | |
| 383 | { |
| 384 | grn_table_cursor *cursor; |
| 385 | grn_id id; |
| 386 | grn_obj *prefix; |
| 387 | const void *min = NULL; |
| 388 | unsigned int min_size = 0; |
| 389 | int flags = 0; |
| 390 | |
| 391 | prefix = grn_plugin_proc_get_var(ctx, user_data, "prefix", -1); |
| 392 | if (GRN_TEXT_LEN(prefix) > 0) { |
| 393 | min = GRN_TEXT_VALUE(prefix); |
| 394 | min_size = (unsigned int)GRN_TEXT_LEN(prefix); |
| 395 | flags |= GRN_CURSOR_PREFIX; |
| 396 | } |
| 397 | cursor = grn_table_cursor_open(ctx, db, |
| 398 | min, min_size, |
| 399 | NULL, 0, |
| 400 | 0, -1, flags); |
| 401 | if (!cursor) { |
| 402 | return NULL; |
| 403 | } |
| 404 | |
| 405 | GRN_PTR_INIT(&tables, GRN_OBJ_VECTOR, GRN_ID_NIL); |
| 406 | while ((id = grn_table_cursor_next(ctx, cursor)) != GRN_ID_NIL) { |
| 407 | grn_obj *object; |
| 408 | const char *name; |
| 409 | void *key; |
| 410 | int i, key_size; |
| 411 | bool have_period = false; |
| 412 | |
| 413 | key_size = grn_table_cursor_get_key(ctx, cursor, &key); |
| 414 | name = key; |
| 415 | for (i = 0; i < key_size; i++) { |
| 416 | if (name[i] == '.') { |
| 417 | have_period = true; |
| 418 | break; |
| 419 | } |
| 420 | } |
| 421 | if (have_period) { |
| 422 | continue; |
| 423 | } |
| 424 | |
| 425 | object = grn_ctx_at(ctx, id); |
| 426 | if (object) { |
| 427 | if (grn_obj_is_table(ctx, object)) { |
| 428 | GRN_PTR_PUT(ctx, &tables, object); |
| 429 | } else { |
| 430 | grn_obj_unlink(ctx, object); |
| 431 | } |
| 432 | } else { |
| 433 | if (ctx->rc != GRN_SUCCESS) { |
| 434 | ERRCLR(ctx); |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | grn_table_cursor_close(ctx, cursor); |
| 439 | } |
| 440 | size_t n_tables = GRN_BULK_VSIZE(&tables) / sizeof(grn_obj *); |
| 441 | int n_top_level_elements = n_elements_for_header + (int)n_tables; |
| 442 | grn_ctx_output_array_open(ctx, "TABLE_LIST", n_top_level_elements); |
| 443 | |
| 444 | grn_ctx_output_array_open(ctx, "HEADER", 8); |
| 445 | grn_ctx_output_array_open(ctx, "PROPERTY", 2); |
| 446 | grn_ctx_output_cstr(ctx, "id"); |
| 447 | grn_ctx_output_cstr(ctx, "UInt32"); |
| 448 | grn_ctx_output_array_close(ctx); |
| 449 | grn_ctx_output_array_open(ctx, "PROPERTY", 2); |
| 450 | grn_ctx_output_cstr(ctx, "name"); |
| 451 | grn_ctx_output_cstr(ctx, "ShortText"); |
| 452 | grn_ctx_output_array_close(ctx); |
| 453 | grn_ctx_output_array_open(ctx, "PROPERTY", 2); |
| 454 | grn_ctx_output_cstr(ctx, "path"); |
| 455 | grn_ctx_output_cstr(ctx, "ShortText"); |
| 456 | grn_ctx_output_array_close(ctx); |
| 457 | grn_ctx_output_array_open(ctx, "PROPERTY", 2); |
| 458 | grn_ctx_output_cstr(ctx, "flags"); |
| 459 | grn_ctx_output_cstr(ctx, "ShortText"); |
| 460 | grn_ctx_output_array_close(ctx); |
| 461 | grn_ctx_output_array_open(ctx, "PROPERTY", 2); |
| 462 | grn_ctx_output_cstr(ctx, "domain"); |
| 463 | grn_ctx_output_cstr(ctx, "ShortText"); |
| 464 | grn_ctx_output_array_close(ctx); |
| 465 | grn_ctx_output_array_open(ctx, "PROPERTY", 2); |
| 466 | grn_ctx_output_cstr(ctx, "range"); |
| 467 | grn_ctx_output_cstr(ctx, "ShortText"); |
| 468 | grn_ctx_output_array_close(ctx); |
| 469 | grn_ctx_output_array_open(ctx, "PROPERTY", 2); |
| 470 | grn_ctx_output_cstr(ctx, "default_tokenizer"); |
| 471 | grn_ctx_output_cstr(ctx, "ShortText"); |
| 472 | grn_ctx_output_array_close(ctx); |
| 473 | grn_ctx_output_array_open(ctx, "PROPERTY", 2); |
| 474 | grn_ctx_output_cstr(ctx, "normalizer"); |
| 475 | grn_ctx_output_cstr(ctx, "ShortText"); |
| 476 | grn_ctx_output_array_close(ctx); |
| 477 | grn_ctx_output_array_close(ctx); |
| 478 | |
| 479 | size_t i; |
| 480 | for (i = 0; i < n_tables; i++) { |
| 481 | grn_obj *table = GRN_PTR_VALUE_AT(&tables, i); |
| 482 | output_table_info(ctx, table); |
| 483 | grn_obj_unlink(ctx, table); |
| 484 | } |
| 485 | GRN_OBJ_FIN(ctx, &tables); |
| 486 | |
| 487 | grn_ctx_output_array_close(ctx); |
| 488 | |
| 489 | return NULL; |
| 490 | } |
| 491 | |
| 492 | void |
| 493 | grn_proc_init_table_list(grn_ctx *ctx) |
| 494 | { |
| 495 | grn_expr_var vars[1]; |
| 496 | |
| 497 | grn_plugin_expr_var_init(ctx, &(vars[0]), "prefix", -1); |
| 498 | grn_plugin_command_create(ctx, |
| 499 | "table_list", -1, |
| 500 | command_table_list, |
| 501 | 1, |
| 502 | vars); |
| 503 | } |
| 504 | |
| 505 | static grn_obj * |
| 506 | command_table_remove(grn_ctx *ctx, |
| 507 | int nargs, |
| 508 | grn_obj **args, |
| 509 | grn_user_data *user_data) |
| 510 | { |
| 511 | grn_obj *name; |
| 512 | grn_obj *table; |
| 513 | uint32_t flags = 0; |
| 514 | |
| 515 | name = grn_plugin_proc_get_var(ctx, user_data, "name", -1); |
| 516 | if (grn_plugin_proc_get_var_bool(ctx, user_data, "dependent", -1, false)) { |
| 517 | flags |= GRN_OBJ_REMOVE_DEPENDENT; |
| 518 | } |
| 519 | if (grn_plugin_proc_get_var_bool(ctx, user_data, "ensure", -1, false)) { |
| 520 | flags |= GRN_OBJ_REMOVE_ENSURE; |
| 521 | } |
| 522 | table = grn_ctx_get(ctx, |
| 523 | GRN_TEXT_VALUE(name), |
| 524 | (int)GRN_TEXT_LEN(name)); |
| 525 | if (!table) { |
| 526 | GRN_PLUGIN_ERROR(ctx, |
| 527 | GRN_INVALID_ARGUMENT, |
| 528 | "[table][remove] table isn't found: <%.*s>", |
| 529 | (int)GRN_TEXT_LEN(name), |
| 530 | GRN_TEXT_VALUE(name)); |
| 531 | grn_ctx_output_bool(ctx, false); |
| 532 | return NULL; |
| 533 | } |
| 534 | |
| 535 | if (!grn_obj_is_table(ctx, table)) { |
| 536 | const char *type_name; |
| 537 | type_name = grn_obj_type_to_string(table->header.type); |
| 538 | grn_obj_unlink(ctx, table); |
| 539 | GRN_PLUGIN_ERROR(ctx, |
| 540 | GRN_INVALID_ARGUMENT, |
| 541 | "[table][remove] not table: <%.*s>: <%s>", |
| 542 | (int)GRN_TEXT_LEN(name), |
| 543 | GRN_TEXT_VALUE(name), |
| 544 | type_name); |
| 545 | grn_ctx_output_bool(ctx, false); |
| 546 | return NULL; |
| 547 | } |
| 548 | |
| 549 | grn_obj_remove_flags(ctx, table, flags); |
| 550 | grn_ctx_output_bool(ctx, ctx->rc == GRN_SUCCESS); |
| 551 | return NULL; |
| 552 | } |
| 553 | |
| 554 | void |
| 555 | grn_proc_init_table_remove(grn_ctx *ctx) |
| 556 | { |
| 557 | #define N_VARS 3 |
| 558 | grn_expr_var vars[N_VARS]; |
| 559 | |
| 560 | grn_plugin_expr_var_init(ctx, &(vars[0]), "name", -1); |
| 561 | grn_plugin_expr_var_init(ctx, &(vars[1]), "dependent", -1); |
| 562 | grn_plugin_expr_var_init(ctx, &(vars[2]), "ensure", -1); |
| 563 | grn_plugin_command_create(ctx, |
| 564 | "table_remove", |
| 565 | -1, |
| 566 | command_table_remove, |
| 567 | N_VARS, |
| 568 | vars); |
| 569 | #undef N_VARS |
| 570 | } |
| 571 | |
| 572 | static grn_obj * |
| 573 | command_table_rename(grn_ctx *ctx, |
| 574 | int nargs, |
| 575 | grn_obj **args, |
| 576 | grn_user_data *user_data) |
| 577 | { |
| 578 | grn_rc rc = GRN_SUCCESS; |
| 579 | grn_obj *name; |
| 580 | grn_obj *new_name; |
| 581 | grn_obj *table = NULL; |
| 582 | |
| 583 | name = grn_plugin_proc_get_var(ctx, user_data, "name", -1); |
| 584 | new_name = grn_plugin_proc_get_var(ctx, user_data, "new_name", -1); |
| 585 | if (GRN_TEXT_LEN(name) == 0) { |
| 586 | rc = GRN_INVALID_ARGUMENT; |
| 587 | GRN_PLUGIN_ERROR(ctx, rc, "[table][rename] table name isn't specified"); |
| 588 | goto exit; |
| 589 | } |
| 590 | table = grn_ctx_get(ctx, GRN_TEXT_VALUE(name), (int)GRN_TEXT_LEN(name)); |
| 591 | if (!table) { |
| 592 | rc = GRN_INVALID_ARGUMENT; |
| 593 | GRN_PLUGIN_ERROR(ctx, |
| 594 | rc, |
| 595 | "[table][rename] table isn't found: <%.*s>", |
| 596 | (int)GRN_TEXT_LEN(name), |
| 597 | GRN_TEXT_VALUE(name)); |
| 598 | goto exit; |
| 599 | } |
| 600 | if (GRN_TEXT_LEN(new_name) == 0) { |
| 601 | rc = GRN_INVALID_ARGUMENT; |
| 602 | GRN_PLUGIN_ERROR(ctx, |
| 603 | rc, |
| 604 | "[table][rename] new table name isn't specified: <%.*s>", |
| 605 | (int)GRN_TEXT_LEN(name), |
| 606 | GRN_TEXT_VALUE(name)); |
| 607 | goto exit; |
| 608 | } |
| 609 | rc = grn_table_rename(ctx, table, |
| 610 | GRN_TEXT_VALUE(new_name), |
| 611 | (unsigned int)GRN_TEXT_LEN(new_name)); |
| 612 | if (rc != GRN_SUCCESS && ctx->rc == GRN_SUCCESS) { |
| 613 | GRN_PLUGIN_ERROR(ctx, |
| 614 | rc, |
| 615 | "[table][rename] failed to rename: <%.*s> -> <%.*s>", |
| 616 | (int)GRN_TEXT_LEN(name), |
| 617 | GRN_TEXT_VALUE(name), |
| 618 | (int)GRN_TEXT_LEN(new_name), |
| 619 | GRN_TEXT_VALUE(new_name)); |
| 620 | } |
| 621 | exit : |
| 622 | grn_ctx_output_bool(ctx, !rc); |
| 623 | if (table) { grn_obj_unlink(ctx, table); } |
| 624 | return NULL; |
| 625 | } |
| 626 | |
| 627 | void |
| 628 | grn_proc_init_table_rename(grn_ctx *ctx) |
| 629 | { |
| 630 | grn_expr_var vars[2]; |
| 631 | |
| 632 | grn_plugin_expr_var_init(ctx, &(vars[0]), "name", -1); |
| 633 | grn_plugin_expr_var_init(ctx, &(vars[1]), "new_name", -1); |
| 634 | grn_plugin_command_create(ctx, |
| 635 | "table_rename", -1, |
| 636 | command_table_rename, |
| 637 | 2, |
| 638 | vars); |
| 639 | } |
| 640 | |
| 641 | static grn_obj * |
| 642 | command_table_copy(grn_ctx *ctx, |
| 643 | int nargs, |
| 644 | grn_obj **args, |
| 645 | grn_user_data *user_data) |
| 646 | { |
| 647 | grn_obj *from_table = NULL; |
| 648 | grn_obj *to_table = NULL; |
| 649 | grn_obj *from_name; |
| 650 | grn_obj *to_name; |
| 651 | |
| 652 | from_name = grn_plugin_proc_get_var(ctx, user_data, "from_name", -1); |
| 653 | to_name = grn_plugin_proc_get_var(ctx, user_data, "to_name", -1); |
| 654 | |
| 655 | from_table = grn_proc_get_value_object(ctx, from_name, "[table][copy][from]"); |
| 656 | if (!from_table) { |
| 657 | goto exit; |
| 658 | } |
| 659 | to_table = grn_proc_get_value_object(ctx, to_name, "[table][copy][to]"); |
| 660 | if (!to_table) { |
| 661 | goto exit; |
| 662 | } |
| 663 | |
| 664 | grn_table_copy(ctx, from_table, to_table); |
| 665 | |
| 666 | exit : |
| 667 | grn_ctx_output_bool(ctx, ctx->rc == GRN_SUCCESS); |
| 668 | |
| 669 | if (to_table) { |
| 670 | grn_obj_unlink(ctx, to_table); |
| 671 | } |
| 672 | if (from_table) { |
| 673 | grn_obj_unlink(ctx, from_table); |
| 674 | } |
| 675 | |
| 676 | return NULL; |
| 677 | } |
| 678 | |
| 679 | void |
| 680 | grn_proc_init_table_copy(grn_ctx *ctx) |
| 681 | { |
| 682 | grn_expr_var vars[2]; |
| 683 | |
| 684 | grn_plugin_expr_var_init(ctx, &(vars[0]), "from_name", -1); |
| 685 | grn_plugin_expr_var_init(ctx, &(vars[1]), "to_name", -1); |
| 686 | grn_plugin_command_create(ctx, |
| 687 | "table_copy", -1, |
| 688 | command_table_copy, |
| 689 | 2, |
| 690 | vars); |
| 691 | } |
| 692 | |
| 693 | static grn_obj * |
| 694 | command_table_create_similar(grn_ctx *ctx, |
| 695 | int nargs, |
| 696 | grn_obj **args, |
| 697 | grn_user_data *user_data) |
| 698 | { |
| 699 | grn_obj *name; |
| 700 | grn_obj *base_table_name; |
| 701 | grn_obj *base_table = NULL; |
| 702 | grn_obj *table = NULL; |
| 703 | |
| 704 | name = grn_plugin_proc_get_var(ctx, user_data, "name", -1); |
| 705 | base_table_name = grn_plugin_proc_get_var(ctx, user_data, "base_table", -1); |
| 706 | |
| 707 | base_table = grn_proc_get_value_object(ctx, |
| 708 | base_table_name, |
| 709 | "[table][create][similar][base_table]"); |
| 710 | if (!base_table) { |
| 711 | goto exit; |
| 712 | } |
| 713 | |
| 714 | table = grn_table_create_similar(ctx, |
| 715 | GRN_TEXT_VALUE(name), |
| 716 | (uint32_t)GRN_TEXT_LEN(name), |
| 717 | NULL, |
| 718 | base_table); |
| 719 | |
| 720 | exit : |
| 721 | grn_ctx_output_bool(ctx, ctx->rc == GRN_SUCCESS); |
| 722 | |
| 723 | if (base_table) { |
| 724 | grn_obj_unref(ctx, base_table); |
| 725 | } |
| 726 | if (table) { |
| 727 | grn_obj_unref(ctx, table); |
| 728 | } |
| 729 | |
| 730 | return NULL; |
| 731 | } |
| 732 | |
| 733 | void |
| 734 | grn_proc_init_table_create_similar(grn_ctx *ctx) |
| 735 | { |
| 736 | grn_expr_var vars[2]; |
| 737 | |
| 738 | grn_plugin_expr_var_init(ctx, &(vars[0]), "name", -1); |
| 739 | grn_plugin_expr_var_init(ctx, &(vars[1]), "base_table", -1); |
| 740 | grn_plugin_command_create(ctx, |
| 741 | "table_create_similar", -1, |
| 742 | command_table_create_similar, |
| 743 | 2, |
| 744 | vars); |
| 745 | } |