Created
August 19, 2009 08:35
-
-
Save agile/170252 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@another_query = connection.execute "SELECT * FROM table_of_love" | |
current_id = connection.select_value("SELECT MAX(id) FROM #{table_name}").to_i | |
pg_conn = connection.raw_connection | |
pg_conn.exec("COPY #{table_name} (#{column_names.join(',')}) FROM STDIN WITH CSV QUOTE AS '\"'") | |
pg_conn.put_copy_data ::FasterCSV.generate(csv_opts) { |csv| | |
@another_query.all_hashes.each do |hash| | |
csv << column_names.inject([]) { |row,col| row << (col == 'id' ? current_id+=1 : hash[col].kind_of?(String) ? Iconv.conv('UTF8//IGNORE','LATIN1',hash[col].to_s) : hash[col]); row } | |
end | |
} | |
pg_conn.put_copy_end | |
connection.execute "SELECT SETVAL('#{table_name}_id_seq', (SELECT MAX(id)+1 FROM #{table_name}))" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment