Module: ArPerfToolkit::InsertBulk::ClassMethods

Defined in:
lib/insert_bulk.rb

Instance Method Summary collapse

Instance Method Details

#insert_all(objs, options = {}) ⇒ Object

Insert a group of records insert_all, Insert a whole bunch of records in one transaction EmailAddress.insert_all([=> ‘[email protected]’, :name =>‘Blythe Dunham’,=> ‘[email protected]’, :name =>‘Blythe Dunham’], {:select => [:text, :name] }) options

<tt>:skip_validation</tt> => does not run valid? on each object
<tt>:duplicate</tt> 
<tt>:keywords</tt>
<tt>:post_sql</tt>
<tt>:pre_sql</tt>
<tt>:command</tt>
<tt>:select => </tt>choose the columns you wish to insert


56
57
58
# File 'lib/insert_bulk.rb', line 56

def insert_all(objs, options={})
  connection.insert(insert_all_sql(objs, options), "#{name} Insert All")
end

#insert_all_sql(objs, options = {}) ⇒ Object

the raw sql for bulk insert sql



34
35
36
37
38
39
40
41
42
# File 'lib/insert_bulk.rb', line 34

def insert_all_sql(objs, options={})
  value_sql = options.delete(:value_sql)
  sql = construct_sql({:command => 'INSERT'}.merge(options), [:skip_validation]) do |sql, options|
    sql << " INTO #{table_name} "
    sql << "(%s) " % select_columns(options[:select])
    sql << "VALUES "
    sql << (value_sql || insert_value_pairs(objs, options[:select], options[:skip_validation].kind_of?(TrueClass)))
  end
end

#insert_select(select_obj, select_options, insert_options = {}) ⇒ Object

Insert records with a select statement

add all the books written by the author into the cart

Cart.insert_select Book, {:select => [‘books.id, ?’, Time.now], :conditions => [‘books.author_id = ?’, 1 }, => [:book_id, :added-on]

generated sql

INSERT INTO carts (‘book_id`, `added_on`) SELECT books.id, ’2007-05-01 01:24:28’ FROM books where books.author_id = 1



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/insert_bulk.rb', line 16

def insert_select(select_obj, select_options, insert_options={})

  #santize and clean up the select columns. Cannot do * here because we need the columns in 
  #a particular order
  select_options[:select] = select_obj.send :select_columns, select_options[:select]
  
  #build the sql
  sql = construct_sql({:command => 'INSERT'}.merge(insert_options)) do |sql, options|
    sql << " INTO #{table_name} "
    sql << "(%s) " % select_columns(insert_options[:select])
    
    sql << select_obj.send(:finder_sql_to_string, select_options)
    
  end
  connection.insert(sql, "#{name} Insert Select #{select_obj}")
end

#update_all_ignore(updates, conditions = nil, options = {}) ⇒ Object



71
72
73
# File 'lib/insert_bulk.rb', line 71

def update_all_ignore updates, conditions = nil, options={}
  update_all_x updates, conditions, options.update(:keywords => 'IGNORE')
end

#update_all_x(updates, conditions = nil, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


60
61
62
63
64
65
66
67
68
69
# File 'lib/insert_bulk.rb', line 60

def update_all_x(updates, conditions = nil, options={})
  raise(ArgumentError, "Unknown key: duplicate") if options[:duplicate]
  
  sql = construct_sql({:command => 'UPDATE'}.merge(options)) do |sql, options|
    sql << "#{table_name} SET #{sanitize_sql(updates)} "
    add_conditions!(sql, conditions, scope(:find))
  end
  
  connection.update(sql, "#{name} Update #{options.inspect}")
end