Class: Hayfork::DeleteSql
- Inherits:
-
Object
- Object
- Hayfork::DeleteSql
- Defined in:
- lib/hayfork/delete_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) ⇒ DeleteSql
constructor
A new instance of DeleteSql.
- #to_sql ⇒ Object (also: #to_s)
Constructor Details
#initialize(haystack, relation, bindings) ⇒ DeleteSql
Returns a new instance of DeleteSql.
5 6 7 8 9 |
# File 'lib/hayfork/delete_sql.rb', line 5 def initialize(haystack, relation, bindings) @haystack = haystack @relation = relation @bindings = bindings.reject { |binding| binding.key == Hayfork::SEARCH_VECTOR || binding.key == Hayfork::TEXT } end |
Instance Attribute Details
#bindings ⇒ Object (readonly)
Returns the value of attribute bindings.
3 4 5 |
# File 'lib/hayfork/delete_sql.rb', line 3 def bindings @bindings end |
#haystack ⇒ Object (readonly)
Returns the value of attribute haystack.
3 4 5 |
# File 'lib/hayfork/delete_sql.rb', line 3 def haystack @haystack end |
#relation ⇒ Object (readonly)
Returns the value of attribute relation.
3 4 5 |
# File 'lib/hayfork/delete_sql.rb', line 3 def relation @relation end |
Instance Method Details
#to_sql ⇒ Object Also known as: to_s
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/hayfork/delete_sql.rb', line 11 def to_sql select_statement = relation.select(bindings.map(&:to_s)) select_statement = select_statement.from("(SELECT OLD.*) \"#{relation.table_name}\"") constraints = bindings.map { |binding| "#{haystack.table_name}.#{binding.key}=x.#{binding.key}" }.join(" AND ") <<~SQL DELETE FROM #{haystack.table_name} USING (#{select_statement.to_sql}) "x" WHERE #{constraints}; SQL end |