Permalink
Browse files

Allow string keys to be used when using Dataset#multi_insert

Add Dataset#identifier_list private method taking an array and
returning a string containing comma separated identifiers.
  • Loading branch information...
1 parent d313c97 commit ca7cc40a1e7fc6e369e9e80f3b32bb7c21395673 @jeremyevans committed Sep 19, 2008
View
@@ -1,5 +1,7 @@
=== HEAD
+* Allow string keys to be used when using Dataset#multi_insert (jeremyevans)
+
* Fix join_table when doing the first join for a dataset where the first source is a dataset when using unqualified columns (jeremyevans)
* Fix a few corner cases in eager_graph (jeremyevans)
@@ -186,9 +186,8 @@ def literal(v)
# MySQL specific syntax for inserting multiple values at once.
def multi_insert_sql(columns, values)
- columns = column_list(columns)
values = values.map {|r| literal(Array(r))}.join(COMMA_SEPARATOR)
- ["INSERT INTO #{source_list(@opts[:from])} (#{columns}) VALUES #{values}"]
+ ["INSERT INTO #{source_list(@opts[:from])} (#{identifier_list(columns)}) VALUES #{values}"]
end
# MySQL uses the nonstandard ` (backtick) for quoting identifiers.
@@ -466,9 +466,8 @@ def multi_insert_sql(columns, values)
return super if server_version < 80200
# postgresql 8.2 introduces support for multi-row insert
- columns = column_list(columns)
values = values.map {|r| literal(Array(r))}.join(COMMA_SEPARATOR)
- ["INSERT INTO #{source_list(@opts[:from])} (#{columns}) VALUES #{values}"]
+ ["INSERT INTO #{source_list(@opts[:from])} (#{identifier_list(columns)}) VALUES #{values}"]
end
# PostgreSQL assumes unquoted identifiers are lower case by default,
@@ -135,7 +135,7 @@ def multi_insert(*args)
elsif args[0].is_a?(Array) && args[1].is_a?(Dataset)
table = @opts[:from].first
columns, dataset = *args
- sql = "INSERT INTO #{quote_identifier(table)} #{literal(columns)} VALUES #{literal(dataset)}"
+ sql = "INSERT INTO #{quote_identifier(table)} (#{identifier_list(columns)}) VALUES #{literal(dataset)}"
return @db.transaction{execute_dui(sql)}
else
# we assume that an array of hashes is given
@@ -465,7 +465,7 @@ def literal(v)
when ::Sequel::SQL::Expression
v.to_s(self)
when Array
- v.all_two_pairs? ? literal(v.sql_expr) : (v.empty? ? '(NULL)' : "(#{v.collect{|i| literal(i)}.join(COMMA_SEPARATOR)})")
+ v.all_two_pairs? ? literal(v.sql_expr) : (v.empty? ? '(NULL)' : "(#{expression_list(v)})")
when Hash
literal(v.sql_expr)
when Time, DateTime
@@ -487,9 +487,9 @@ def literal(v)
# inserting multiple records in a single SQL statement.
def multi_insert_sql(columns, values)
table = quote_identifier(@opts[:from].first)
- columns = literal(columns)
+ columns = identifier_list(columns)
values.map do |r|
- "INSERT INTO #{table} #{columns} VALUES #{literal(r)}"
+ "INSERT INTO #{table} (#{columns}) VALUES #{literal(r)}"
end
end
@@ -725,9 +725,7 @@ def update_sql(values = {}, opts = nil)
# get values from hash
values = transform_save(values) if @transform
values.map do |k, v|
- # convert string key into symbol
- k = k.to_sym if String === k
- "#{literal(k)} = #{literal(v)}"
+ "#{k.is_one_of?(String, Symbol) ? quote_identifier(k) : literal(k)} = #{literal(v)}"
end.join(COMMA_SEPARATOR)
else
# copy values verbatim
@@ -812,6 +810,11 @@ def filter_expr(expr = nil, &block)
end
end
+ # SQL fragment specifying a list of identifiers
+ def identifier_list(columns)
+ columns.map{|i| quote_identifier(i)}.join(COMMA_SEPARATOR)
+ end
+
# SQL statement for formatting an insert statement with default values
def insert_default_values_sql
"INSERT INTO #{source_list(@opts[:from])} DEFAULT VALUES"
@@ -2319,6 +2319,17 @@ def transaction
]
end
+ specify "should accept string keys as column names" do
+ @ds.multi_insert([{'x'=>1, 'y'=>2}, {'x'=>3, 'y'=>4}])
+ @ds.multi_insert(['x', 'y'], [[1, 2], [3, 4]])
+ @db.sqls.should == [
+ 'BEGIN',
+ "INSERT INTO items (x, y) VALUES (1, 2)",
+ "INSERT INTO items (x, y) VALUES (3, 4)",
+ 'COMMIT'
+ ] * 2
+ end
+
specify "should accept a columns array and a values array" do
@ds.multi_insert([:x, :y], [[1, 2], [3, 4]])
@db.sqls.should == [

0 comments on commit ca7cc40

Please sign in to comment.