logical_query_parser 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +124 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/logical_query.rb +889 -0
- data/lib/logical_query.treetop +62 -0
- data/lib/logical_query_parser/nodes/active_record.rb +73 -0
- data/lib/logical_query_parser/nodes/base.rb +34 -0
- data/lib/logical_query_parser/version.rb +3 -0
- data/lib/logical_query_parser.rb +18 -0
- data/logical_query_parser.gemspec +30 -0
- metadata +160 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e8005a57b1dd4e044f6755066a55bc40f1435e6a
|
4
|
+
data.tar.gz: 063e0c5f78b1ca4440203cce18769cfe90f6cde4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c5c6e4b62fc124139ada278d98e6a6fcd5cfb630d01d55a5bae29c578929e0628a704db17982e88173d52e59d91db1d4e0eaf7aeaa6d3563fd75315aa0d0737d
|
7
|
+
data.tar.gz: 37d9daf9cbbc43eb052b3e1574bd120102618cb376b851271f4ff5929573c891352473f391697857a31c977f75fa3f4a59fca7a022db277a43d2e95401ae3733
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at kaneta@sitebridge.co.jp. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Yoshikazu Kaneta
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
# LogicalQueryParser
|
2
|
+
|
3
|
+
A parser to generate a tree structure from a logical search query string using treetop.
|
4
|
+
|
5
|
+
## Requirements
|
6
|
+
|
7
|
+
* ruby 2.3 or lator
|
8
|
+
* treetop 1.6 or lator
|
9
|
+
* activerecord 4.2 or lator (optional)
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'logical_query_parser'
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install logical_query_parser
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
You can parse a logical query string as follows:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
parser = LogicalQueryParser.new
|
33
|
+
parser.parse('a AND b')
|
34
|
+
```
|
35
|
+
|
36
|
+
Return value is a syntax tree of treetop:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
parser = LogicalQueryParser.new
|
40
|
+
parser.parse('a AND b')
|
41
|
+
|
42
|
+
=> SyntaxNode+Exp0+ExpNode offset=0, "a AND b" (any):
|
43
|
+
SyntaxNode+Cond0+CondNode offset=0, "a AND b" (lexp,logic,rexp):
|
44
|
+
SyntaxNode+Literal0+LiteralNode offset=0, "a" (word,negative):
|
45
|
+
SyntaxNode offset=0, ""
|
46
|
+
SyntaxNode+WordNode offset=0, "a":
|
47
|
+
SyntaxNode+Atom0 offset=0, "a":
|
48
|
+
SyntaxNode offset=0, ""
|
49
|
+
SyntaxNode offset=0, "a"
|
50
|
+
SyntaxNode offset=1, " ":
|
51
|
+
SyntaxNode offset=1, " "
|
52
|
+
SyntaxNode+AndNode offset=2, "AND"
|
53
|
+
SyntaxNode offset=5, " ":
|
54
|
+
SyntaxNode offset=5, " "
|
55
|
+
SyntaxNode+Exp0+ExpNode offset=6, "b" (any):
|
56
|
+
SyntaxNode+Literal0+LiteralNode offset=6, "b" (word,negative):
|
57
|
+
SyntaxNode offset=6, ""
|
58
|
+
SyntaxNode+WordNode offset=6, "b":
|
59
|
+
SyntaxNode+Atom0 offset=6, "b":
|
60
|
+
SyntaxNode offset=6, ""
|
61
|
+
SyntaxNode offset=6, "b"
|
62
|
+
```
|
63
|
+
|
64
|
+
You can parse quoted strings:
|
65
|
+
|
66
|
+
```
|
67
|
+
parser = LogicalQueryParser.new
|
68
|
+
parser.parse('("a a" AND "b b") OR (c AND d)')
|
69
|
+
```
|
70
|
+
|
71
|
+
You can also parse negative conditions:
|
72
|
+
|
73
|
+
```
|
74
|
+
parser = LogicalQueryParser.new
|
75
|
+
parser.parse('("a a" AND -"b b") OR (c AND -d)')
|
76
|
+
```
|
77
|
+
|
78
|
+
### Supported operators
|
79
|
+
|
80
|
+
You can parse a query string with following operators:
|
81
|
+
|
82
|
+
* AND / and: represents an AND logic.
|
83
|
+
* OR / or: represents an OR logic.
|
84
|
+
* \-: represents a NOT logic. This should precede to a word.
|
85
|
+
* (: represents beginning of a nested expression.
|
86
|
+
* ): represents end of a nested expression.
|
87
|
+
* ": represents beginning or end of a quoted word.
|
88
|
+
* Space: represents a boundary between two words.
|
89
|
+
|
90
|
+
For more information, see [grammar definition](lib/logical_query.treetop).
|
91
|
+
|
92
|
+
### Compile into SQL
|
93
|
+
|
94
|
+
You can compile a syntax tree into a SQL statement by using activerecord model. For example, the code below
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
class Table < ActiveRecord::Base
|
98
|
+
def build_sql(str = "a AND b")
|
99
|
+
parser = LogicalQueryParser.new
|
100
|
+
parser.parse(str).to_sql(model: self, columns: %w(c1 c2))
|
101
|
+
end
|
102
|
+
end
|
103
|
+
```
|
104
|
+
|
105
|
+
builds a SQL statement as follows (this example shows a postgresql statement):
|
106
|
+
|
107
|
+
```sql
|
108
|
+
("tables"."c1" ILIKE '%a%' OR "tables"."c2" ILIKE '%a%') AND ("tables"."c1" ILIKE '%b%' OR "tables"."c2" ILIKE '%b%')
|
109
|
+
```
|
110
|
+
|
111
|
+
## Development
|
112
|
+
|
113
|
+
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.
|
114
|
+
|
115
|
+
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](https://rubygems.org).
|
116
|
+
|
117
|
+
## Contributing
|
118
|
+
|
119
|
+
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](http://contributor-covenant.org) code of conduct.
|
120
|
+
|
121
|
+
## License
|
122
|
+
|
123
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
124
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "logical_query_parser"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|