digital-fabric / extralite

Ruby on SQLite

Home Page:http://www.rubydoc.info/gems/extralite

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add ability to batch execute queries from an `Enumerable`

noteflakes opened this issue · comments

Right now, the Extralite API includes a #execute_multi method, that takes an array and executes the same query repeatedly, taking its parameters from each array entry. This can be generalized into a #batch_execute method that can take an array, an enumerable, or a block. Some examples:

q = db.prepare('insert into foo (?, ?, ?)')

# Batch execute from an array (existing functionality)
data = [[1, 2, 3], [4, 5, 6]]
q.execute_batch(data)

# Batch execute from an Enumerable (by calling #each)
data = (1..3).lazy.map { |i| [i * 10, i * 20, i * 30] }
q.execute_batch(data)

# Batch execute from block
q.execute_batch do
  STDOUT << "Enter a number: "
  number = gets
  if number.empty?
    nil
  else
    number = number.to_i
    [number * 10, number * 20, number * 30]
  end
end

The #execute_multi method can first be an alias to #batch_execute, then be deprecated in a future release.