Module: ArPerfToolkit::InsertBulk::ClassMethods
- Defined in:
- lib/insert_bulk.rb
Instance Method Summary collapse
-
#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
:skip_validation
=> does not run valid? on each object:duplicate
:keywords
:post_sql
:pre_sql
:command
:select =>
choose the columns you wish to insert. -
#insert_all_sql(objs, options = {}) ⇒ Object
the raw sql for bulk insert sql.
-
#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.
- #update_all_ignore(updates, conditions = nil, options = {}) ⇒ Object
- #update_all_x(updates, conditions = nil, options = {}) ⇒ Object
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, ={}) connection.insert(insert_all_sql(objs, ), "#{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, ={}) value_sql = .delete(:value_sql) sql = construct_sql({:command => 'INSERT'}.merge(), [:skip_validation]) do |sql, | sql << " INTO #{table_name} " sql << "(%s) " % select_columns([:select]) sql << "VALUES " sql << (value_sql || insert_value_pairs(objs, [:select], [: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 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, , ={}) #santize and clean up the select columns. Cannot do * here because we need the columns in #a particular order [:select] = select_obj.send :select_columns, [:select] #build the sql sql = construct_sql({:command => 'INSERT'}.merge()) do |sql, | sql << " INTO #{table_name} " sql << "(%s) " % select_columns([:select]) sql << select_obj.send(:finder_sql_to_string, ) 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, ={} update_all_x updates, conditions, .update(:keywords => 'IGNORE') end |
#update_all_x(updates, conditions = nil, options = {}) ⇒ Object
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/insert_bulk.rb', line 60 def update_all_x(updates, conditions = nil, ={}) raise(ArgumentError, "Unknown key: duplicate") if [:duplicate] sql = construct_sql({:command => 'UPDATE'}.merge()) do |sql, | sql << "#{table_name} SET #{sanitize_sql(updates)} " add_conditions!(sql, conditions, scope(:find)) end connection.update(sql, "#{name} Update #{.inspect}") end |