Class: HumanQL::PostgreSQLGenerator
- Inherits:
-
Object
- Object
- HumanQL::PostgreSQLGenerator
- Defined in:
- lib/human-ql/postgresql_generator.rb
Overview
Generate query strings suitable for passing to PostgreSQL’s to_tsquery function, from a HumanQL abstract syntax tree (AST).
In order to guarantee valid output for any human input, the AST should be created using PostgreSQLCustomParser and normalized via TreeNormalizer (with minimal defaults).
Any scope’s provided in the parser should have been handled and stripped out of the AST, as PostgreSQL is not expected to have a direct equivalent in tsquery syntax.
Constant Summary collapse
- AND =
– From www.postgresql.org/docs/9.6/static/datatype-textsearch.html > In the absence of parentheses, ‘!’ (NOT) binds most tightly, > and ‘&’ (AND) and ‘<->’ (FOLLOWED BY) both bind more tightly > than | (OR). ++
' & '.freeze
- OR =
' | '.freeze
- NOT =
'!'.freeze
- NEAR =
' <-> '.freeze
Instance Method Summary collapse
-
#generate(node) ⇒ Object
Given the root node of the AST, return a string in PostgreSQL tsquery syntax.
Instance Method Details
#generate(node) ⇒ Object
Given the root node of the AST, return a string in PostgreSQL tsquery syntax.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/human-ql/postgresql_generator.rb', line 45 def generate( node ) op,*args = node if ! node.is_a?( Array ) op elsif args.empty? nil else case op when :and terms_join( args, AND ) when :or pwrap( terms_join( args, OR ) ) when :not if args[0].is_a?( Array ) NOT + pwrap( generate( args[0] ) ) else NOT + args[0] end when :phrase terms_join( args, NEAR ) else raise "Unsupported op: #{node.inspect}" end end end |