SpeciesFileGroup/logical_query_parser

Name: logical_query_parser

Owner: Species File Group

Description: A parser to generate a tree structure from a logical search query string using treetop.

Forked from: kanety/logical_query_parser

Created: 2017-08-17 21:12:59.0

Updated: 2017-08-17 21:13:01.0

Pushed: 2017-08-17 22:13:51.0

Homepage:

Size: 13

Language: Ruby

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

LogicalQueryParser

A parser to generate a tree structure from a logical search query string using treetop.

Requirements
Installation

Add this line to your application's Gemfile:

'logical_query_parser'

And then execute:

$ bundle

Or install it yourself as:

$ gem install logical_query_parser
Usage

You can parse a logical query string as follows:

er = LogicalQueryParser.new
er.parse('a AND b')

Return value is a syntax tree of treetop:

er = LogicalQueryParser.new
er.parse('a AND b')

yntaxNode+Exp0+ExpNode offset=0, "a AND b" (any):
ntaxNode+Cond0+CondNode offset=0, "a AND b" (lexp,logic,rexp):
SyntaxNode+Literal0+LiteralNode offset=0, "a" (word,negative):
  SyntaxNode offset=0, ""
  SyntaxNode+WordNode offset=0, "a":
    SyntaxNode+Atom0 offset=0, "a":
      SyntaxNode offset=0, ""
      SyntaxNode offset=0, "a"
SyntaxNode offset=1, " ":
  SyntaxNode offset=1, " "
SyntaxNode+AndNode offset=2, "AND"
SyntaxNode offset=5, " ":
  SyntaxNode offset=5, " "
SyntaxNode+Exp0+ExpNode offset=6, "b" (any):
  SyntaxNode+Literal0+LiteralNode offset=6, "b" (word,negative):
    SyntaxNode offset=6, ""
    SyntaxNode+WordNode offset=6, "b":
      SyntaxNode+Atom0 offset=6, "b":
        SyntaxNode offset=6, ""
        SyntaxNode offset=6, "b"

You can parse quoted strings:

er = LogicalQueryParser.new
er.parse('("a a" AND "b b") OR (c AND d)')

You can also parse negative conditions:

er = LogicalQueryParser.new
er.parse('("a a" AND -"b b") OR (c AND -d)')
Supported operators

You can parse a query string with following operators:

For more information, see grammar definition.

Compile into SQL

You can compile a syntax tree into a SQL statement by using activerecord model. For example, the code below

s Table < ActiveRecord::Base
f build_sql(str = "a AND b")
parser = LogicalQueryParser.new
parser.parse(str).to_sql(model: self, columns: %w(c1 c2))
d

builds a SQL statement as follows (this example shows a postgresql statement):

bles"."c1" ILIKE '%a%' OR "tables"."c2" ILIKE '%a%') AND ("tables"."c1" ILIKE '%b%' OR "tables"."c2" ILIKE '%b%')
Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/kanety/logical_query_parser. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.