Sha256: 018fa953544055694592110ae8b6728f76573cafd62601c3c20d82040a940af6

Contents?: true

Size: 1.44 KB

Versions: 4

Compression:

Stored size: 1.44 KB

Contents

module SearchCop
  module Visitors
    module Postgres
      class FulltextQuery < Visitor
        def visit_SearchCopGrammar_Nodes_MatchesFulltextNot(node)
          "!'#{node.right.gsub(/[\s&|!:'"]+/, " ")}'"
        end

        def visit_SearchCopGrammar_Nodes_MatchesFulltext(node)
          "'#{node.right.gsub(/[\s&|!:'"]+/, " ")}'"
        end

        def visit_SearchCopGrammar_Nodes_And_Fulltext(node)
          node.nodes.collect { |child_node| "(#{visit child_node})" }.join(" & ")
        end

        def visit_SearchCopGrammar_Nodes_Or_Fulltext(node)
          node.nodes.collect { |child_node| "(#{visit child_node})" }.join(" | ")
        end
      end

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

      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

      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
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
search_cop-1.1.0 lib/search_cop/visitors/postgres.rb
search_cop-1.0.9 lib/search_cop/visitors/postgres.rb
search_cop-1.0.8 lib/search_cop/visitors/postgres.rb
search_cop-1.0.7 lib/search_cop/visitors/postgres.rb