null+****@clear*****
null+****@clear*****
2012年 1月 29日 (日) 14:27:26 JST
Kouhei Sutou 2012-01-29 14:27:26 +0900 (Sun, 29 Jan 2012)
New Revision: be1b93b32d3b66801b41a4b1b5887619c538a0cd
Log:
[command][table_rename] add. refs #1234
Added files:
test/unit/core/test-command-table-rename.c
Modified files:
lib/db.c
lib/proc.c
test/unit/core/Makefile.am
Modified: lib/db.c (+46 -25)
===================================================================
--- lib/db.c 2012-01-29 13:48:50 +0900 (ad5ffac)
+++ lib/db.c 2012-01-29 14:27:26 +0900 (f0a6ef9)
@@ -6423,35 +6423,56 @@ grn_rc
grn_table_rename(grn_ctx *ctx, grn_obj *table, const char *name, unsigned int name_size)
{
grn_rc rc = GRN_INVALID_ARGUMENT;
+ grn_hash *cols;
+
GRN_API_ENTER;
- if (GRN_OBJ_TABLEP(table) && DB_OBJ(table)->id &&
- !(DB_OBJ(table)->id & GRN_OBJ_TMP_OBJECT)) {
- grn_hash *cols;
- if ((cols = grn_hash_create(ctx, NULL, sizeof(grn_id), 0,
- GRN_OBJ_TABLE_HASH_KEY|GRN_HASH_TINY))) {
- grn_table_columns(ctx, table, "", 0, (grn_obj *)cols);
- if (!(rc = grn_obj_rename(ctx, table, name, name_size))) {
- grn_id *key;
- char fullname[GRN_TABLE_MAX_KEY_SIZE];
- memcpy(fullname, name, name_size);
- fullname[name_size] = GRN_DB_DELIMITER;
- GRN_HASH_EACH(ctx, cols, id, &key, NULL, NULL, {
- grn_obj *col = grn_ctx_at(ctx, *key);
- if (col) {
- int colname_len = grn_column_name(ctx, col, fullname + name_size + 1,
- GRN_TABLE_MAX_KEY_SIZE - name_size - 1);
- if (colname_len) {
- if ((rc = grn_obj_rename(ctx, col, fullname,
- name_size + 1 + colname_len))) {
- break;
- }
+
+ if (!GRN_OBJ_TABLEP(table)) {
+ char table_name[GRN_TABLE_MAX_KEY_SIZE];
+ int table_name_size;
+ table_name_size = grn_obj_name(ctx, table, table_name,
+ GRN_TABLE_MAX_KEY_SIZE);
+ rc = GRN_INVALID_ARGUMENT;
+ ERR(rc,
+ "[table][rename] isn't table: <%.*s> -> <%.*s>",
+ table_name_size, table_name,
+ name_size, name);
+ goto exit;
+ }
+ if (IS_TEMP(table)) {
+ rc = GRN_INVALID_ARGUMENT;
+ ERR(rc,
+ "[table][rename] temporary table doesn't have name: "
+ "(anonymous) -> <%.*s>",
+ name_size, name);
+ goto exit;
+ }
+
+ if ((cols = grn_hash_create(ctx, NULL, sizeof(grn_id), 0,
+ GRN_OBJ_TABLE_HASH_KEY|GRN_HASH_TINY))) {
+ grn_table_columns(ctx, table, "", 0, (grn_obj *)cols);
+ if (!(rc = grn_obj_rename(ctx, table, name, name_size))) {
+ grn_id *key;
+ char fullname[GRN_TABLE_MAX_KEY_SIZE];
+ memcpy(fullname, name, name_size);
+ fullname[name_size] = GRN_DB_DELIMITER;
+ GRN_HASH_EACH(ctx, cols, id, &key, NULL, NULL, {
+ grn_obj *col = grn_ctx_at(ctx, *key);
+ if (col) {
+ int colname_len = grn_column_name(ctx, col, fullname + name_size + 1,
+ GRN_TABLE_MAX_KEY_SIZE - name_size - 1);
+ if (colname_len) {
+ if ((rc = grn_obj_rename(ctx, col, fullname,
+ name_size + 1 + colname_len))) {
+ break;
}
}
- });
- }
- grn_hash_close(ctx, cols);
+ }
+ });
}
+ grn_hash_close(ctx, cols);
}
+exit:
GRN_API_RETURN(rc);
}
@@ -6467,7 +6488,7 @@ grn_column_rename(grn_ctx *ctx, grn_obj *column, const char *name, unsigned int
fullname, GRN_TABLE_MAX_KEY_SIZE);
if (name_size + 1 + len > GRN_TABLE_MAX_KEY_SIZE) {
ERR(GRN_INVALID_ARGUMENT,
- "[column][rename]: too long column name: required name_size(%d) < %d"
+ "[column][rename] too long column name: required name_size(%d) < %d"
": <%.*s>.<%.*s>",
name_size, GRN_TABLE_MAX_KEY_SIZE - 1 - len,
len, fullname, name_size, name);
Modified: lib/proc.c (+45 -0)
===================================================================
--- lib/proc.c 2012-01-29 13:48:50 +0900 (2fa1569)
+++ lib/proc.c 2012-01-29 14:27:26 +0900 (5067480)
@@ -892,6 +892,47 @@ proc_table_remove(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_d
}
static grn_obj *
+proc_table_rename(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
+{
+ grn_rc rc;
+ grn_obj *table = NULL;
+ if (GRN_TEXT_LEN(VAR(0)) == 0) {
+ ERR(GRN_INVALID_ARGUMENT, "[table][rename] table name isn't specified");
+ rc = ctx->rc;
+ goto exit;
+ }
+ table = grn_ctx_get(ctx, GRN_TEXT_VALUE(VAR(0)), GRN_TEXT_LEN(VAR(0)));
+ if (!table) {
+ ERR(GRN_INVALID_ARGUMENT,
+ "[table][rename] table isn't found: <%.*s>",
+ GRN_TEXT_LEN(VAR(0)), GRN_TEXT_VALUE(VAR(0)));
+ rc = ctx->rc;
+ goto exit;
+ }
+ if (GRN_TEXT_LEN(VAR(1)) == 0) {
+ ERR(GRN_INVALID_ARGUMENT,
+ "[table][rename] new table name isn't specified: <%.*s>",
+ GRN_TEXT_LEN(VAR(0)), GRN_TEXT_VALUE(VAR(0)));
+ rc = ctx->rc;
+ goto exit;
+ }
+ rc = grn_table_rename(ctx, table,
+ GRN_TEXT_VALUE(VAR(1)), GRN_TEXT_LEN(VAR(1)));
+ if (rc != GRN_SUCCESS && ctx->rc == GRN_SUCCESS) {
+ ERR(rc,
+ "[table][rename] failed to rename: <%.*s> -> <%.*s>",
+ GRN_TEXT_LEN(VAR(0)), GRN_TEXT_VALUE(VAR(0)),
+ GRN_TEXT_LEN(VAR(1)), GRN_TEXT_VALUE(VAR(1)));
+ }
+exit:
+ GRN_OUTPUT_BOOL(!rc);
+ if (table) {
+ grn_obj_unlink(ctx, table);
+ }
+ return NULL;
+}
+
+static grn_obj *
proc_column_create(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
{
grn_obj *column, *table = NULL, *type = NULL;
@@ -2690,6 +2731,10 @@ grn_db_init_builtin_query(grn_ctx *ctx)
DEF_VAR(vars[0], "name");
DEF_COMMAND("table_remove", proc_table_remove, 1, vars);
+ DEF_VAR(vars[0], "name");
+ DEF_VAR(vars[1], "new_name");
+ DEF_COMMAND("table_rename", proc_table_rename, 2, vars);
+
DEF_VAR(vars[0], "table");
DEF_VAR(vars[1], "name");
DEF_VAR(vars[2], "flags");
Modified: test/unit/core/Makefile.am (+2 -0)
===================================================================
--- test/unit/core/Makefile.am 2012-01-29 13:48:50 +0900 (4427e88)
+++ test/unit/core/Makefile.am 2012-01-29 14:27:26 +0900 (9b21246)
@@ -45,6 +45,7 @@ noinst_LTLIBRARIES = \
test-command-load.la \
test-command-table-create.la \
test-command-table-list.la \
+ test-command-table-rename.la \
test-command-column-create.la \
test-command-column-list.la \
test-command-select.la \
@@ -131,6 +132,7 @@ test_inspect_la_SOURCES = test-inspect.c
test_command_load_la_SOURCES = test-command-load.c
test_command_table_create_la_SOURCES = test-command-table-create.c
test_command_table_list_la_SOURCES = test-command-table-list.c
+test_command_table_rename_la_SOURCES = test-command-table-rename.c
test_command_column_create_la_SOURCES = test-command-column-create.c
test_command_column_list_la_SOURCES = test-command-column-list.c
test_command_select_la_SOURCES = test-command-select.c
Added: test/unit/core/test-command-table-rename.c (+156 -0) 100644
===================================================================
--- /dev/null
+++ test/unit/core/test-command-table-rename.c 2012-01-29 14:27:26 +0900 (81ad708)
@@ -0,0 +1,156 @@
+/* -*- c-basic-offset: 2; coding: utf-8 -*- */
+/*
+ Copyright (C) 2012 Kouhei Sutou <kou****@clear*****>
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License version 2.1 as published by the Free Software Foundation.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#include <gcutter.h>
+#include <glib/gstdio.h>
+
+#include "../lib/grn-assertions.h"
+
+#include <str.h>
+
+#define get(name) grn_ctx_get(context, name, strlen(name))
+
+void test_success(void);
+void test_error_no_argument(void);
+void test_error_nonexistent_table_name(void);
+void test_error_missing_new_table_name(void);
+
+static gchar *tmp_directory;
+static const gchar *database_path;
+
+static grn_ctx *context;
+static grn_obj *database;
+
+void
+cut_startup(void)
+{
+ tmp_directory = g_build_filename(grn_test_get_tmp_dir(),
+ "table-rename",
+ NULL);
+}
+
+void
+cut_shutdown(void)
+{
+ g_free(tmp_directory);
+}
+
+static void
+remove_tmp_directory(void)
+{
+ cut_remove_path(tmp_directory, NULL);
+}
+
+void
+cut_setup(void)
+{
+ remove_tmp_directory();
+ g_mkdir_with_parents(tmp_directory, 0700);
+
+ context = g_new0(grn_ctx, 1);
+ grn_ctx_init(context, 0);
+
+ database_path = cut_build_path(tmp_directory,
+ "command-table-rename",
+ NULL);
+ database = grn_db_create(context, database_path, NULL);
+}
+
+void
+cut_teardown(void)
+{
+ grn_obj_close(context, database);
+ grn_ctx_fin(context);
+ g_free(context);
+
+ remove_tmp_directory();
+}
+
+static void
+populate(void)
+{
+ assert_send_commands("table_create Users TABLE_HASH_KEY ShortText\n"
+ "column_create Users name COLUMN_SCALAR ShortText\n"
+ "load --table Users\n"
+ "[\n"
+ "[\"_key\",\"name\"],\n"
+ "[\"morita\",\"Daijiro MORI\"],\n"
+ "[\"yata\",\"Susumu Yata\"]\n"
+ "]");
+}
+
+void
+test_success(void)
+{
+ populate();
+ assert_send_command("table_rename Users People");
+ cut_assert_equal_string(
+ "table_create People TABLE_HASH_KEY ShortText\n"
+ "column_create People name COLUMN_SCALAR ShortText\n"
+ "load --table People\n"
+ "[\n"
+ "[\"_key\",\"name\"],\n"
+ "[\"morita\",\"Daijiro MORI\"],\n"
+ "[\"yata\",\"Susumu Yata\"]\n"
+ "]",
+ send_command("dump"));
+}
+
+void
+test_error_no_argument(void)
+{
+ populate();
+ grn_test_assert_send_command_error(
+ context,
+ GRN_INVALID_ARGUMENT,
+ "[table][rename] table name isn't specified",
+ "table_rename");
+}
+
+void
+test_error_nonexistent_table_name(void)
+{
+ populate();
+ grn_test_assert_send_command_error(
+ context,
+ GRN_INVALID_ARGUMENT,
+ "[table][rename] table isn't found: <nonexistent>",
+ "table_rename nonexistent");
+}
+
+void
+test_error_missing_new_table_name(void)
+{
+ populate();
+ grn_test_assert_send_command_error(
+ context,
+ GRN_INVALID_ARGUMENT,
+ "[table][rename] new table name isn't specified: <Users>",
+ "table_rename Users");
+}
+
+void
+test_error_not_table(void)
+{
+ populate();
+ grn_test_assert_send_command_error(
+ context,
+ GRN_INVALID_ARGUMENT,
+ "[table][rename] isn't table: <Users.name> -> <People>",
+ "table_rename Users.name People");
+}