Sha256: b32d3add2d47657ff05f7320d26efec8e244f264a8df559dbcc007f234a16833

Contents?: true

Size: 1.99 KB

Versions: 1

Compression:

Stored size: 1.99 KB

Contents

# 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
      
      # Copy the specified table.
      def copy_table(old_table_name, new_table_name)
        transaction do
          execute "CREATE TABLE #{new_table_name} LIKE #{old_table_name}"
          execute "INSERT INTO #{new_table_name} SELECT * FROM #{old_table_name}"
        end
      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:
      # * <tt>:ignore</tt> -- Ignore the specified number of lines from the source file
      # * <tt>:columns</tt> -- Array of column names defining the source file column order
      # * <tt>:fields</tt> -- Hash of options for fields:
      # * <tt>:delimited_by</tt> -- The field delimiter
      # * <tt>:enclosed_by</tt> -- The field enclosure
      def do_bulk_load(file, table_name, options={})
        return if File.size(file) == 0
        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
      
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mainej-adapter_extensions-0.4.0.1 lib/adapter_extensions/connection_adapters/mysql_adapter.rb