Module: SearchCop::Visitors::Postgres

Defined in:
lib/search_cop/visitors/postgres.rb

Defined Under Namespace

Classes: FulltextQuery

Instance Method Summary collapse

Instance Method Details

#visit_SearchCopGrammar_Attributes_Collection(node) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/search_cop/visitors/postgres.rb', line 44

def visit_SearchCopGrammar_Attributes_Collection(node)
  res = node.attributes.collect do |attribute|
    if attribute.options[:coalesce]
      "COALESCE(#{visit attribute}, '')"
    else
      visit attribute
    end
  end

  res.join(" || ' ' || ")
end

#visit_SearchCopGrammar_Attributes_Hstore(attribute) ⇒ Object



18
19
20
# File 'lib/search_cop/visitors/postgres.rb', line 18

def visit_SearchCopGrammar_Attributes_Hstore(attribute)
  "#{quote_table_name attribute.table_alias}.#{quote_column_name attribute.column_name}->#{quote attribute.field_names.first}"
end

#visit_SearchCopGrammar_Attributes_Json(attribute) ⇒ Object

rubocop:disable Naming/MethodName



6
7
8
9
10
# File 'lib/search_cop/visitors/postgres.rb', line 6

def visit_SearchCopGrammar_Attributes_Json(attribute)
  elements = ["#{quote_table_name attribute.table_alias}.#{quote_column_name attribute.column_name}", *attribute.field_names.map { |field_name| quote(field_name) }]

  "#{elements[0...-1].join("->")}->>#{elements.last}"
end

#visit_SearchCopGrammar_Attributes_Jsonb(attribute) ⇒ Object



12
13
14
15
16
# File 'lib/search_cop/visitors/postgres.rb', line 12

def visit_SearchCopGrammar_Attributes_Jsonb(attribute)
  elements = ["#{quote_table_name attribute.table_alias}.#{quote_column_name attribute.column_name}", *attribute.field_names.map { |field_name| quote(field_name) }]

  "#{elements[0...-1].join("->")}->>#{elements.last}"
end

#visit_SearchCopGrammar_Nodes_FulltextExpression(node) ⇒ Object



56
57
58
59
60
# File 'lib/search_cop/visitors/postgres.rb', line 56

def visit_SearchCopGrammar_Nodes_FulltextExpression(node)
  dictionary = node.collection.options[:dictionary] || "simple"

  "to_tsvector(#{visit dictionary}, #{visit node.collection}) @@ to_tsquery(#{visit dictionary}, #{visit FulltextQuery.new(connection).visit(node.node)})"
end

#visit_SearchCopGrammar_Nodes_Matches(node) ⇒ Object



40
41
42
# File 'lib/search_cop/visitors/postgres.rb', line 40

def visit_SearchCopGrammar_Nodes_Matches(node)
  "(#{visit node.left} IS NOT NULL AND #{visit node.left} ILIKE #{visit node.right} ESCAPE #{visit "\\"})"
end