class Hayfork::InsertSql

Attributes

bindings[R]
haystack[R]
relation[R]

Public Class Methods

new(haystack, relation, bindings) click to toggle source
# File lib/hayfork/insert_sql.rb, line 5
def initialize(haystack, relation, bindings)
  @haystack = haystack
  @relation = relation
  @bindings = bindings
end

Public Instance Methods

to_s(from: true)
Alias for: to_sql
to_sql(from: true) click to toggle source
# File lib/hayfork/insert_sql.rb, line 11
    def to_sql(from: true)
      select_statement = relation.select(bindings.map(&:to_s))
      select_statement = select_statement.from("(SELECT NEW.*) \"#{relation.table_name}\"") if from

      <<~SQL
        INSERT INTO #{haystack.table_name} (#{bindings.map(&:key).join(", ")}) SELECT * FROM (#{select_statement.to_sql}) "x" WHERE "x"."#{Hayfork::TEXT}" != '';
      SQL
    end
Also aliased as: to_s