Navigation Menu

Skip to content

Commit

Permalink
groonga: Extract validation logic for the table name
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Apr 28, 2014
1 parent 1e020dc commit d151dda
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 43 deletions.
8 changes: 1 addition & 7 deletions lib/droonga/plugins/groonga/column_create.rb
Expand Up @@ -27,13 +27,7 @@ def process_request(request)
command_class = ::Groonga::Command.find("column_create")
@command = command_class.new("column_create", request)

table_name = @command["table"]
if table_name.nil? or @context[table_name].nil?
message = "table doesn't exist: <#{table_name.to_s}>"
raise CommandError.new(:status => Status::INVALID_ARGUMENT,
:message => message,
:result => false)
end
table_name = valid_table_name("table")

if @command.column_index?
define_index(table_name)
Expand Down
7 changes: 1 addition & 6 deletions lib/droonga/plugins/groonga/column_list.rb
Expand Up @@ -38,12 +38,7 @@ def process_request(request)
command_class = ::Groonga::Command.find("column_list")
@command = command_class.new("column_list", request)

table_name = @command["table"]
if table_name.nil? or @context[table_name].nil?
message = "table doesn't exist: <#{table_name.to_s}>"
raise CommandError.new(:status => Status::INVALID_ARGUMENT,
:message => message)
end
table_name = valid_table_name("table")

columns = list_columns(table_name)
[HEADER, *columns]
Expand Down
8 changes: 1 addition & 7 deletions lib/droonga/plugins/groonga/column_remove.rb
Expand Up @@ -27,13 +27,7 @@ def process_request(request)
command_class = ::Groonga::Command.find("column_remove")
@command = command_class.new("column_remove", request)

table_name = @command["table"]
if table_name.nil? or @context[table_name].nil?
message = "table doesn't exist: <#{table_name.to_s}>"
raise CommandError.new(:status => Status::INVALID_ARGUMENT,
:message => message,
:result => false)
end
table_name = valid_table_name("table")

column_name = @command["name"]
if column_name.nil? or @context[table_name].column(column_name).nil?
Expand Down
8 changes: 1 addition & 7 deletions lib/droonga/plugins/groonga/column_rename.rb
Expand Up @@ -27,13 +27,7 @@ def process_request(request)
command_class = ::Groonga::Command.find("column_rename")
@command = command_class.new("column_rename", request)

table_name = @command["table"]
if table_name.nil? or @context[table_name].nil?
message = "table doesn't exist: <#{table_name.to_s}>"
raise CommandError.new(:status => Status::INVALID_ARGUMENT,
:message => message,
:result => false)
end
table_name = valid_table_name("table")

column_name = @command["name"]
if column_name.nil? or @context[table_name].column(column_name).nil?
Expand Down
14 changes: 4 additions & 10 deletions lib/droonga/plugins/groonga/delete.rb
Expand Up @@ -27,12 +27,13 @@ def process_request(request)
command_class = ::Groonga::Command.find("delete")
@command = command_class.new("delete", request)

table_name = @command["table"]
table_name = valid_table_name("table")

key = @command["key"]
id = @command["id"]
filter = @command["filter"]

validate_parameters(table_name, key, id, filter)
validate_parameters(key, id, filter)

table = @context[table_name]
if key
Expand All @@ -47,14 +48,7 @@ def process_request(request)
end

private
def validate_parameters(table_name, key, id, filter)
if table_name.nil? or @context[table_name].nil?
message = "table doesn't exist: <#{table_name}>"
raise CommandError.new(:status => Status::INVALID_ARGUMENT,
:message => message,
:result => false)
end

def validate_parameters(key, id, filter)
if key.nil? and id.nil? and filter.nil?
message = "you must specify \"key\", \"id\", or \"filter\""
raise CommandError.new(:status => Status::INVALID_ARGUMENT,
Expand Down
20 changes: 20 additions & 0 deletions lib/droonga/plugins/groonga/generic_command.rb
Expand Up @@ -58,6 +58,26 @@ def header(return_code, error_message="")
header.push(error_message) unless error_message.empty?
header
end

def valid_table_name(name)
table_name = @command[name]

if table_name.nil?
message = "you must specify table via \"#{name}\""
raise CommandError.new(:status => Status::INVALID_ARGUMENT,
:message => message,
:result => false)
end

if @context[table_name].nil?
message = "table not found: <#{table_name.to_s}>"
raise CommandError.new(:status => Status::INVALID_ARGUMENT,
:message => message,
:result => false)
end

table_name
end
end
end
end
Expand Down
7 changes: 1 addition & 6 deletions lib/droonga/plugins/groonga/table_remove.rb
Expand Up @@ -27,12 +27,7 @@ def process_request(request)
command_class = ::Groonga::Command.find("table_remove")
@command = command_class.new("table_remove", request)

name = @command["name"]
if name.nil? or @context[name].nil?
raise CommandError.new(:status => Status::INVALID_ARGUMENT,
:message => "table not found",
:result => false)
end
table_name = valid_table_name("name")

::Groonga::Schema.define(:context => @context) do |schema|
schema.remove_table(name)
Expand Down

0 comments on commit d151dda

Please sign in to comment.