lib/sequel/adapters/shared/mysql.rb in sequel-3.5.0 vs lib/sequel/adapters/shared/mysql.rb in sequel-3.6.0
- old
+ new
@@ -1,6 +1,9 @@
module Sequel
+ Dataset::NON_SQL_OPTIONS << :insert_ignore
+ Dataset::NON_SQL_OPTIONS << :on_duplicate_key_update
+
module MySQL
class << self
# Set the default options used for CREATE TABLE
attr_accessor :default_charset, :default_collate, :default_engine
end
@@ -309,41 +312,11 @@
end
# MySQL specific syntax for REPLACE (aka UPSERT, or update if exists,
# insert if it doesn't).
def replace_sql(*values)
- from = source_list(@opts[:from])
- if values.empty?
- "REPLACE INTO #{from} DEFAULT VALUES"
- else
- values = values[0] if values.size == 1
-
- case values
- when Array
- if values.empty?
- "REPLACE INTO #{from} DEFAULT VALUES"
- else
- "REPLACE INTO #{from} VALUES #{literal(values)}"
- end
- when Hash
- if values.empty?
- "REPLACE INTO #{from} DEFAULT VALUES"
- else
- fl, vl = [], []
- values.each {|k, v| fl << literal(k.is_a?(String) ? k.to_sym : k); vl << literal(v)}
- "REPLACE INTO #{from} (#{fl.join(COMMA_SEPARATOR)}) VALUES (#{vl.join(COMMA_SEPARATOR)})"
- end
- when Dataset
- "REPLACE INTO #{from} #{literal(values)}"
- else
- if values.respond_to?(:values)
- replace_sql(values.values)
- else
- "REPLACE INTO #{from} VALUES (#{literal(values)})"
- end
- end
- end
+ clone(:replace=>true).insert_sql(*values)
end
# does not support DISTINCT ON
def supports_distinct_on?
false
@@ -358,10 +331,17 @@
# ignores them. Also, using them seems to cause problems on 1.9. Since
# they are ignored anyway, not using them is probably best.
def supports_timestamp_usecs?
false
end
+
+ protected
+
+ # If this is an replace instead of an insert, use replace instead
+ def _insert_sql
+ @opts[:replace] ? clause_sql(:replace) : super
+ end
private
# MySQL supports the ORDER BY and LIMIT clauses for DELETE statements
def delete_clause_methods
@@ -370,9 +350,10 @@
# MySQL supports the IGNORE and ON DUPLICATE KEY UPDATE clauses for INSERT statements
def insert_clause_methods
INSERT_CLAUSE_METHODS
end
+ alias replace_clause_methods insert_clause_methods
# MySQL doesn't use the SQL standard DEFAULT VALUES.
def insert_columns_sql(sql)
values = opts[:values]
if values.is_a?(Array) && values.empty?