Sha256: f5c01420fbb6b04eaf0d707ffc235aebe980c989cece72ec272ad30d11051399

Contents?: true

Size: 1.54 KB

Versions: 4

Compression:

Stored size: 1.54 KB

Contents

module SearchCop
  module Visitors
    module Postgres
      # rubocop:disable Naming/MethodName

      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} ESCAPE #{visit "\\"})"
      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

      # rubocop:enable Naming/MethodName
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
search_cop-1.2.3 lib/search_cop/visitors/postgres.rb
search_cop-1.2.2 lib/search_cop/visitors/postgres.rb
search_cop-1.2.1 lib/search_cop/visitors/postgres.rb
search_cop-1.2.0 lib/search_cop/visitors/postgres.rb