onyxframework / sql

A delightful SQL ORM ☺️
https://api.onyxframework.com/sql
MIT License
91 stars 7 forks source link

Add BulkQuery #82

Closed vladfaust closed 5 years ago

vladfaust commented 5 years ago
users = [User.new(name: "John"), User.new(name: "Jake")]
users.insert == Onyx::SQL::BulkQuery(User).new.insert(users)
users.insert.build == {"INSERT INTO users (name) VALUES (?), (?)", ["John", "Jake"]}

users.delete.build == {"DELETE FROM users WHERE id IN (?, ?)", [1, 2]}

This would require patching Enumerable like this:

module Enumerable(T)
  def insert
    {% if T < Onyx::SQL::Model %}
      Onyx::SQL::BulkQuery(T).new.insert(self)
    {% else %}
      {% raise "Can only call argless Enumerable#insert on Enumerable(Onyx::SQL::Model)" %}
    {% end %}
  end
end

Which should work fine as Enumerable doesn't have #insert and #delete methods at all and other types like Array do not have these methods with zero arity.