Solving #50 - memoize MySQL's max_allowed_packet instead of querying it every time you run .import

This commit is contained in:
Seamus Abshere 2012-03-20 16:38:39 -05:00
parent 3dc99c357d
commit b36259af29

View file

@ -5,11 +5,13 @@ module ActiveRecord::Import::MysqlAdapter
# Returns the maximum number of bytes that the server will allow # Returns the maximum number of bytes that the server will allow
# in a single packet # in a single packet
def max_allowed_packet # :nodoc: def max_allowed_packet # :nodoc:
@max_allowed_packet ||= begin
result = execute( "SHOW VARIABLES like 'max_allowed_packet';" ) result = execute( "SHOW VARIABLES like 'max_allowed_packet';" )
# original Mysql gem responds to #fetch_row while Mysql2 responds to #first # 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 = result.respond_to?(:fetch_row) ? result.fetch_row[1] : result.first[1]
val.to_i val.to_i
end end
end
# Returns a generated ON DUPLICATE KEY UPDATE statement given the passed # Returns a generated ON DUPLICATE KEY UPDATE statement given the passed
# in +args+. # in +args+.