| Class | ActiveRecord::ConnectionAdapters::MysqlAdapter |
| In: |
lib/adapter_extensions/connection_adapters/mysql_adapter.rb
|
| Parent: | AbstractAdapter |
Adds new functionality to ActiveRecord MysqlAdapter.
Inserts an INTO table_name clause to the sql_query.
# File lib/adapter_extensions/connection_adapters/mysql_adapter.rb, line 12
12: def add_select_into_table(new_table_name, sql_query)
13: "CREATE TABLE #{new_table_name} " + sql_query
14: end
Copy the specified table.
# File lib/adapter_extensions/connection_adapters/mysql_adapter.rb, line 17
17: def copy_table(old_table_name, new_table_name)
18: transaction do
19: execute "CREATE TABLE #{new_table_name} LIKE #{old_table_name}"
20: execute "INSERT INTO #{new_table_name} SELECT * FROM #{old_table_name}"
21: end
22: end
# File lib/adapter_extensions/connection_adapters/mysql_adapter.rb, line 7 7: def support_select_into_table? 8: true 9: end
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:
# File lib/adapter_extensions/connection_adapters/mysql_adapter.rb, line 36
36: def do_bulk_load(file, table_name, options={})
37: return if File.size(file) == 0
38:
39: # an unfortunate hack - setting the bulk load option after the connection has been
40: # established does not seem to have any effect, and since the connection is made when
41: # active-record is loaded, there's no chance for us to sneak it in earlier. So we
42: # disconnect, set the option, then reconnect - fortunately, this only needs to happen once.
43: unless @bulk_load_enabled
44: disconnect!
45: @connection.options(Mysql::OPT_LOCAL_INFILE, true)
46: connect
47: @bulk_load_enabled = true
48: end
49:
50: q = "LOAD DATA LOCAL INFILE '#{file}' INTO TABLE #{table_name}"
51: if options[:fields]
52: q << " FIELDS"
53: q << " TERMINATED BY '#{options[:fields][:delimited_by]}'" if options[:fields][:delimited_by]
54: q << " ENCLOSED BY '#{options[:fields][:enclosed_by]}'" if options[:fields][:enclosed_by]
55: end
56: q << " IGNORE #{options[:ignore]} LINES" if options[:ignore]
57: q << " (#{options[:columns].join(',')})" if options[:columns]
58: execute(q)
59: end