lib/sequel/adapters/shared/mysql.rb in sequel-3.31.0 vs lib/sequel/adapters/shared/mysql.rb in sequel-3.32.0

- old
+ new

@@ -1,7 +1,8 @@ module Sequel Dataset::NON_SQL_OPTIONS << :insert_ignore + Dataset::NON_SQL_OPTIONS << :update_ignore Dataset::NON_SQL_OPTIONS << :on_duplicate_key_update module MySQL @convert_tinyint_to_bool = true @@ -309,17 +310,21 @@ end # MySQL has both datetime and timestamp classes, most people are going # to want datetime def type_literal_generic_datetime(column) - :datetime + if column[:default] == Sequel::CURRENT_TIMESTAMP + :timestamp + else + :datetime + end end # MySQL has both datetime and timestamp classes, most people are going # to want datetime def type_literal_generic_time(column) - column[:only_time] ? :time : :datetime + column[:only_time] ? :time : type_literal_generic_datetime(column) end # MySQL doesn't have a true boolean class, so it uses tinyint(1) def type_literal_generic_trueclass(column) :'tinyint(1)' @@ -334,11 +339,11 @@ FOR_SHARE = ' LOCK IN SHARE MODE'.freeze SQL_CALC_FOUND_ROWS = ' SQL_CALC_FOUND_ROWS'.freeze DELETE_CLAUSE_METHODS = Dataset.clause_methods(:delete, %w'delete from where order limit') INSERT_CLAUSE_METHODS = Dataset.clause_methods(:insert, %w'insert ignore into columns values on_duplicate_key_update') SELECT_CLAUSE_METHODS = Dataset.clause_methods(:select, %w'select distinct calc_found_rows columns from join where group having compounds order limit lock') - UPDATE_CLAUSE_METHODS = Dataset.clause_methods(:update, %w'update table set where order limit') + UPDATE_CLAUSE_METHODS = Dataset.clause_methods(:update, %w'update ignore table set where order limit') SPACE = Dataset::SPACE PAREN_OPEN = Dataset::PAREN_OPEN PAREN_CLOSE = Dataset::PAREN_CLOSE NOT_SPACE = Dataset::NOT_SPACE FROM = Dataset::FROM @@ -465,11 +470,11 @@ # ) # # INSERT IGNORE INTO tablename (name, value) VALUES (a, 1), (b, 2) def insert_ignore clone(:insert_ignore=>true) end - + # Sets up the insert methods to use ON DUPLICATE KEY UPDATE # If you pass no arguments, ALL fields will be # updated with the new values. If you pass the fields you # want then ONLY those field will be updated. # @@ -547,10 +552,20 @@ # they are ignored anyway, not using them is probably best. def supports_timestamp_usecs? false end + # Sets up the update methods to use UPDATE IGNORE. + # Useful if you have a unique key and want to just skip + # updating rows that violate the unique key restriction. + # + # dataset.update_ignore.update({:name => 'a', :value => 1}) + # # UPDATE IGNORE tablename SET name = 'a', value = 1 + def update_ignore + clone(:update_ignore=>true) + end + private # MySQL supports the ORDER BY and LIMIT clauses for DELETE statements def delete_clause_methods DELETE_CLAUSE_METHODS @@ -587,9 +602,14 @@ end # MySQL supports INSERT IGNORE INTO def insert_ignore_sql(sql) sql << IGNORE if opts[:insert_ignore] + end + + # MySQL supports UPDATE IGNORE + def update_ignore_sql(sql) + sql << IGNORE if opts[:update_ignore] end # If this is an replace instead of an insert, use replace instead def insert_insert_sql(sql) sql << (@opts[:replace] ? REPLACE : INSERT)