Class: Hayfork::InsertSql

Inherits:
Object
  • Object
show all
Defined in:
lib/hayfork/insert_sql.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(haystack, relation, bindings) ⇒ InsertSql

Returns a new instance of InsertSql.



5
6
7
8
9
# File 'lib/hayfork/insert_sql.rb', line 5

def initialize(haystack, relation, bindings)
  @haystack = haystack
  @relation = relation
  @bindings = bindings
end

Instance Attribute Details

#bindingsObject (readonly)

Returns the value of attribute bindings.



3
4
5
# File 'lib/hayfork/insert_sql.rb', line 3

def bindings
  @bindings
end

#haystackObject (readonly)

Returns the value of attribute haystack.



3
4
5
# File 'lib/hayfork/insert_sql.rb', line 3

def haystack
  @haystack
end

#relationObject (readonly)

Returns the value of attribute relation.



3
4
5
# File 'lib/hayfork/insert_sql.rb', line 3

def relation
  @relation
end

Instance Method Details

#to_sql(from: true) ⇒ Object Also known as: to_s



11
12
13
14
15
16
17
18
# 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