Class: Hayfork::DeleteSql

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#bindingsObject (readonly)

Returns the value of attribute bindings.



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

def bindings
  @bindings
end

#haystackObject (readonly)

Returns the value of attribute haystack.



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

def haystack
  @haystack
end

#relationObject (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_sqlObject 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