Module: BulkMethodsMixin
- Defined in:
- lib/bulk_data_methods/bulk_methods_mixin.rb
Overview
MixIn used to extend ActiveRecord::Base classes implementing bulk insert and update operations through #create_many and #update_many.
Defined Under Namespace
Classes: BulkUploadDataInconsistent
Instance Method Summary collapse
-
#create_many(rows, options = {}) ⇒ Array<Hash>
BULK creation of many rows.
-
#update_many(rows, options = {}) ⇒ Array<Hash>
BULK updates of many rows.
Instance Method Details
#create_many(rows, options = {}) ⇒ Array<Hash>
BULK creation of many rows
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/bulk_data_methods/bulk_methods_mixin.rb', line 50 def create_many(rows, = {}) return [] if rows.blank? [:slice_size] = 1000 unless .has_key?(:slice_size) [:check_consistency] = true unless .has_key?(:check_consistency) returning_clause = "" if [:returning] if [:returning].is_a? Array returning_list = [:returning].join(',') else returning_list = [:returning] end returning_clause = " returning #{returning_list}" end returning = [] created_at_value = Time.zone.now num_sequences_needed = rows.reject{|r| r[:id].present?}.length if num_sequences_needed > 0 row_ids = connection.next_sequence_values(sequence_name, num_sequences_needed) else row_ids = [] end rows.each do |row| # set the primary key if it needs to be set row[:id] ||= row_ids.shift end.each do |row| # set :created_at if need be row[:created_at] ||= created_at_value end.group_by do |row| respond_to?(:partition_table_name) ? partition_table_name(*partition_key_values(row)) : table_name end.each do |table_name, rows_for_table| column_names = rows_for_table[0].keys.sort{|a,b| a.to_s <=> b.to_s} sql_insert_string = "insert into #{table_name} (#{column_names.join(',')}) values " rows_for_table.map do |row| if [:check_consistency] row_column_names = row.keys.sort{|a,b| a.to_s <=> b.to_s} if column_names != row_column_names raise BulkUploadDataInconsistent.new(self, table_name, column_names, row_column_names, "while attempting to build insert statement") end end column_values = column_names.map do |column_name| quote_value(row[column_name], columns_hash[column_name.to_s]) end.join(',') "(#{column_values})" end.each_slice([:slice_size]) do |insert_slice| returning += find_by_sql(sql_insert_string + insert_slice.join(',') + returning_clause) end end return returning end |
#update_many(rows = [], options = {}) ⇒ Array<Hash> #update_many(rows = {}, options = {}) ⇒ Array<Hash>
Note:
Remember that you should probably set updated_at using “updated = datatable.updated_at” or “updated_at = now()” in the set_array if you want to follow the standard active record model for time columns (and you have an updated_at column)
BULK updates of many rows
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/bulk_data_methods/bulk_methods_mixin.rb', line 169 def update_many(rows, = {}) return [] if rows.blank? if rows.is_a?(Hash) [:where_datatable] = '"' + rows.keys[0].keys.map{|key| '#{table_name}.' + "#{key} = datatable.#{key}"}.join(' and ') + '"' [:set_array] = '"' + rows.values[0].keys.map{|key| "#{key} = datatable.#{key}"}.join(',') + '"' unless [:set_array] r = [] rows.each do |key,value| r << key.merge(value) end rows = r end unless [:set_array] column_names = rows[0].keys columns_to_remove = [:id] columns_to_remove += partition_keys.flatten.map{|k| k.to_sym} if respond_to?(:partition_keys) [:set_array] = '"' + (column_names - columns_to_remove).map{|cn| "#{cn} = datatable.#{cn}"}.join(',') + '"' end [:slice_size] = 1000 unless [:slice_size] [:check_consistency] = true unless .has_key?(:check_consistency) returning_clause = "" if [:returning] if [:returning].is_a?(Array) returning_list = [:returning].map{|r| '#{table_name}.' + r.to_s}.join(',') else returning_list = [:returning] end returning_clause = "\" returning #{returning_list}\"" end where_clause = [:where_datatable] || '"#{table_name}.id = datatable.id"' where_constraint = "" if [:where_constraint] where_constraint = '" AND #{eval(options[:where_constraint])}"' end returning = [] rows.group_by do |row| respond_to?(:partition_table_name) ? partition_table_name(*partition_key_values(row)) : table_name end.each do |table_name, rows_for_table| column_names = rows_for_table[0].keys.sort{|a,b| a.to_s <=> b.to_s} rows_for_table.each_slice([:slice_size]) do |update_slice| datatable_rows = [] update_slice.each_with_index do |row,i| if [:check_consistency] row_column_names = row.keys.sort{|a,b| a.to_s <=> b.to_s} if column_names != row_column_names raise BulkUploadDataInconsistent.new(self, table_name, column_names, row_column_names, "while attempting to build update statement") end end datatable_rows << row.map do |column_name,column_value| column_name = column_name.to_s columns_hash_value = columns_hash[column_name] if i == 0 "#{quote_value(column_value, columns_hash_value)}::#{columns_hash_value.sql_type} as #{column_name}" else quote_value(column_value, columns_hash_value) end end.join(',') end datatable = datatable_rows.join(' union select ') sql_update_string = <<-SQL update #{table_name} set #{eval([:set_array])} from (select #{datatable} ) as datatable where #{eval(where_clause)} #{eval(where_constraint)} #{eval(returning_clause)} SQL returning += find_by_sql(sql_update_string) end end return returning end |