# Source code for the MysqlAdapter extensions. module ActiveRecord #:nodoc: module ConnectionAdapters #:nodoc: # Adds new functionality to ActiveRecord MysqlAdapter. class MysqlAdapter < AbstractAdapter def support_select_into_table? true end # Inserts an INTO table_name clause to the sql_query. def add_select_into_table(new_table_name, sql_query) "CREATE TABLE #{new_table_name} " + sql_query end protected # Call +bulk_load+, as that method wraps this method. # # Bulk load the data in the specified file. This implementation always uses the LOCAL keyword # so the file must be found locally, not on the remote server, to be loaded. # # Options: # * :ignore -- Ignore the specified number of lines from the source file # * :columns -- Array of column names defining the source file column order # * :fields -- Hash of options for fields: # * :delimited_by -- The field delimiter # * :enclosed_by -- The field enclosure def do_bulk_load(file, table_name, options={}) q = "LOAD DATA LOCAL INFILE '#{file}' INTO TABLE #{table_name}" if options[:fields] q << " FIELDS" q << " TERMINATED BY '#{options[:fields][:delimited_by]}'" if options[:fields][:delimited_by] q << " ENCLOSED BY '#{options[:fields][:enclosed_by]}'" if options[:fields][:enclosed_by] end q << " IGNORE #{options[:ignore]} LINES" if options[:ignore] q << " (#{options[:columns].join(',')})" if options[:columns] execute(q) end private def connect encoding = @config[:encoding] if encoding @connection.options(Mysql::SET_CHARSET_NAME, encoding) rescue nil end @connection.options(Mysql::OPT_LOCAL_INFILE, true) @connection.ssl_set(@config[:sslkey], @config[:sslcert], @config[:sslca], @config[:sslcapath], @config[:sslcipher]) if @config[:sslkey] @connection.real_connect(*@connection_options) execute("SET NAMES '#{encoding}'") if encoding # By default, MySQL 'where id is null' selects the last inserted id. # Turn this off. http://dev.rubyonrails.org/ticket/6778 execute("SET SQL_AUTO_IS_NULL=0") end end end end