Module: ActiveRecord::Extensions::ConnectionAdapters::MysqlAdapter

Includes:
Import::ImportSupport, Import::OnDuplicateKeyUpdateSupport
Defined in:
lib/ar-extensions/adapters/mysql.rb

Overview

:nodoc:

Instance Method Summary collapse

Methods included from Import::OnDuplicateKeyUpdateSupport

#supports_on_duplicate_key_update?

Methods included from Import::ImportSupport

#supports_import?

Instance Method Details

#duplicate_key_update_error?(exception) ⇒ Boolean

return true if the statement is a duplicate key record error



53
54
55
# File 'lib/ar-extensions/adapters/mysql.rb', line 53

def duplicate_key_update_error?(exception)# :nodoc:
  exception.is_a?(ActiveRecord::StatementInvalid) && exception.to_s.include?('Duplicate entry')
end

#max_allowed_packetObject

Returns the maximum number of bytes that the server will allow in a single packet



8
9
10
11
12
13
# File 'lib/ar-extensions/adapters/mysql.rb', line 8

def max_allowed_packet # :nodoc:
  result = execute( "SHOW VARIABLES like 'max_allowed_packet';" )
  # original Mysql gem responds to #fetch_row while Mysql2 responds to #first
  val = result.respond_to?(:fetch_row) ? result.fetch_row[1] : result.first[1]
  val.to_i
end

#rollup_sqlObject



15
# File 'lib/ar-extensions/adapters/mysql.rb', line 15

def rollup_sql; " WITH ROLLUP "; end

#sql_for_on_duplicate_key_update(table_name, *args) ⇒ Object

Returns a generated ON DUPLICATE KEY UPDATE statement given the passed in args.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ar-extensions/adapters/mysql.rb', line 19

def sql_for_on_duplicate_key_update( table_name, *args ) # :nodoc:
  sql = ' ON DUPLICATE KEY UPDATE '
  arg = args.first
  if arg.is_a?( Array )
    sql << sql_for_on_duplicate_key_update_as_array( table_name, arg )
  elsif arg.is_a?( Hash )
    sql << sql_for_on_duplicate_key_update_as_hash( table_name, arg )
  elsif arg.is_a?( String )
    sql << arg
  else
    raise ArgumentError.new( "Expected Array or Hash" )
  end
  sql
end

#sql_for_on_duplicate_key_update_as_array(table_name, arr) ⇒ Object

:nodoc:



34
35
36
37
38
39
40
# File 'lib/ar-extensions/adapters/mysql.rb', line 34

def sql_for_on_duplicate_key_update_as_array( table_name, arr )  # :nodoc:
  results = arr.map do |column|
    qc = quote_column_name( column )
    "#{table_name}.#{qc}=VALUES(#{qc})"        
  end
  results.join( ',' )
end

#sql_for_on_duplicate_key_update_as_hash(table_name, hsh) ⇒ Object

:nodoc:



42
43
44
45
46
47
48
49
50
# File 'lib/ar-extensions/adapters/mysql.rb', line 42

def sql_for_on_duplicate_key_update_as_hash( table_name, hsh ) # :nodoc:
  sql = ' ON DUPLICATE KEY UPDATE '
  results = hsh.map do |column1, column2|
    qc1 = quote_column_name( column1 )
    qc2 = quote_column_name( column2 )
    "#{table_name}.#{qc1}=VALUES( #{qc2} )"
  end
  results.join( ',')
end