Class: Hayfork::InsertSql
- Inherits:
-
Object
- Object
- Hayfork::InsertSql
- Defined in:
- lib/hayfork/insert_sql.rb
Instance Attribute Summary collapse
-
#bindings ⇒ Object
readonly
Returns the value of attribute bindings.
-
#haystack ⇒ Object
readonly
Returns the value of attribute haystack.
-
#relation ⇒ Object
readonly
Returns the value of attribute relation.
Instance Method Summary collapse
-
#initialize(haystack, relation, bindings) ⇒ InsertSql
constructor
A new instance of InsertSql.
- #to_sql(from: true) ⇒ Object (also: #to_s)
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
#bindings ⇒ Object (readonly)
Returns the value of attribute bindings.
3 4 5 |
# File 'lib/hayfork/insert_sql.rb', line 3 def bindings @bindings end |
#haystack ⇒ Object (readonly)
Returns the value of attribute haystack.
3 4 5 |
# File 'lib/hayfork/insert_sql.rb', line 3 def haystack @haystack end |
#relation ⇒ Object (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 |