null+****@clear*****
null+****@clear*****
2011年 12月 19日 (月) 11:53:32 JST
Kouhei Sutou 2011-12-19 02:53:32 +0000 (Mon, 19 Dec 2011) New Revision: 47db261c3cf7bc77949002e9ccc96413bbc94c27 Log: [shcema][table-create] validate flags. refs #915 refs #1210 Modified files: lib/db.c test/unit/http/test-http-schema.rb Modified: lib/db.c (+34 -0) =================================================================== --- lib/db.c 2011-12-19 00:54:29 +0000 (17cb09f) +++ lib/db.c 2011-12-19 02:53:32 +0000 (038c057) @@ -607,6 +607,36 @@ static grn_obj *grn_view_create(grn_ctx *ctx, const char *path, grn_obj_flags fl static grn_obj *grn_view_transcript(grn_ctx *ctx, const char *path, grn_obj *key_type, grn_obj *value_type, grn_obj_flags flags); +static grn_rc +grn_table_create_validate(grn_ctx *ctx, const char *name, unsigned name_size, + const char *path, grn_obj_flags flags, + grn_obj *key_type, grn_obj *value_type) +{ + switch (flags & GRN_OBJ_TABLE_TYPE_MASK) { + case GRN_OBJ_TABLE_HASH_KEY : + break; + case GRN_OBJ_TABLE_PAT_KEY : + break; + case GRN_OBJ_TABLE_DAT_KEY : + break; + case GRN_OBJ_TABLE_NO_KEY : + if (flags & GRN_OBJ_KEY_NORMALIZE) { + ERR(GRN_INVALID_ARGUMENT, + "key normalization isn't available for no key table: <%.*s>", + name_size, name); + } + break; + case GRN_OBJ_TABLE_VIEW : + if (flags & GRN_OBJ_KEY_NORMALIZE) { + ERR(GRN_INVALID_ARGUMENT, + "key normalization isn't available for view table: <%.*s>", + name_size, name); + } + break; + } + return ctx->rc; +} + grn_obj * grn_table_create(grn_ctx *ctx, const char *name, unsigned name_size, const char *path, grn_obj_flags flags, @@ -632,6 +662,10 @@ grn_table_create(grn_ctx *ctx, const char *name, unsigned name_size, ERR(GRN_INVALID_ARGUMENT, "invalid db assigned"); GRN_API_RETURN(NULL); } + if (grn_table_create_validate(ctx, name, name_size, path, flags, + key_type, value_type)) { + GRN_API_RETURN(NULL); + } if (key_type) { if ((flags & GRN_OBJ_TABLE_TYPE_MASK) == GRN_OBJ_TABLE_NO_KEY) { if (name_size > 0) { Modified: test/unit/http/test-http-schema.rb (+6 -4) =================================================================== --- test/unit/http/test-http-schema.rb 2011-12-19 00:54:29 +0000 (6ae6e7b) +++ test/unit/http/test-http-schema.rb 2011-12-19 02:53:32 +0000 (1efe999) @@ -531,8 +531,9 @@ class HTTPSchemaTest < Test::Unit::TestCase response = get(command_path(:table_create, :name => "users", :flags => Table::NO_KEY | Key::NORMALIZE)) - assert_error_response(Result::UNKNOWN_ERROR, - "key normalization isn't available", + assert_error_response(Result::INVALID_ARGUMENT, + "key normalization isn't available " + + "for no key table: <users>", response, :content_type => "application/json") @@ -621,8 +622,9 @@ class HTTPSchemaTest < Test::Unit::TestCase response = get(command_path(:table_create, :name => "users", :flags => Table::VIEW | Key::NORMALIZE)) - assert_error_response(Result::UNKNOWN_ERROR, - "key normalization isn't available", + assert_error_response(Result::INVALID_ARGUMENT, + "key normalization isn't available " + + "for view table: <users>", response, :content_type => "application/json")