arel_search_builder 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +0 -0
- data/Rakefile +14 -0
- data/lib/arel_search/builder.rb +62 -0
- data/lib/arel_search/version.rb +3 -0
- data/lib/arel_search_builder.rb +1 -0
- metadata +105 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ea97800206732fd38de3ddc2db7391966f818548
|
4
|
+
data.tar.gz: 9c834081bdcde29a70adcd6c6f01738862e4b83e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 66aec1217964e4b1c8f53f649196daccb88b6f093cb9cd3a79e2f600f9dda757931e382b85e532ac4a2c65b00c117dc7746b2eeb38e8b5830b62d9f0d609ca9d
|
7
|
+
data.tar.gz: b15591a808ac80fc7d1275fa7f30ef0c5c2d10130267430c78ec2f46f2ebb266a9a579c4d28028494a2fe4a8d7534f11221f5e8c280782f8a446c1d2133d95f7
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2014 NIEK Antoine
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
File without changes
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require_relative 'lib/arel_search/version'
|
2
|
+
require 'rake/testtask'
|
3
|
+
|
4
|
+
task :package do
|
5
|
+
`gem build arel_search_builder.gemspec`
|
6
|
+
`gem install arel_search_builder-#{ArelSearch::VERSION}.gem`
|
7
|
+
end
|
8
|
+
|
9
|
+
Rake::TestTask.new do |t|
|
10
|
+
t.pattern = "test/**/*_test.rb"
|
11
|
+
end
|
12
|
+
|
13
|
+
task :default => [:test]
|
14
|
+
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'arel'
|
2
|
+
require 'sql_search_parser'
|
3
|
+
|
4
|
+
module ArelSearch
|
5
|
+
class Builder
|
6
|
+
def initialize table
|
7
|
+
@table = table
|
8
|
+
end
|
9
|
+
|
10
|
+
def build expression
|
11
|
+
ast = SQLSearch.parse(expression)
|
12
|
+
search_statement(ast)
|
13
|
+
end
|
14
|
+
|
15
|
+
def search_statement search
|
16
|
+
case search
|
17
|
+
when SQLSearch::Conditions::Base
|
18
|
+
condition_statement(search)
|
19
|
+
when SQLSearch::Comparisons::Base
|
20
|
+
comparison_statement(search)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def condition_statement condition
|
25
|
+
case condition
|
26
|
+
when SQLSearch::Conditions::Or
|
27
|
+
search_statement(condition.left).or(search_statement(condition.right))
|
28
|
+
when SQLSearch::Conditions::And
|
29
|
+
search_statement(condition.left).and(search_statement(condition.right))
|
30
|
+
when SQLSearch::Conditions::Not
|
31
|
+
search_statement(condition.value).not
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def comparison_statement comparison
|
36
|
+
|
37
|
+
unless comparison.left.is_a?(SQLSearch::Atoms::Column)
|
38
|
+
raise MalformedQueryError, 'left operand must always be a column'
|
39
|
+
end
|
40
|
+
|
41
|
+
case comparison.operator
|
42
|
+
when :IN
|
43
|
+
@table[comparison.left.name.to_sym].in(comparison.right.values.map(&:value))
|
44
|
+
when :">"
|
45
|
+
@table[comparison.left.name.to_sym].gt(comparison.right.value)
|
46
|
+
when :">="
|
47
|
+
@table[comparison.left.name.to_sym].gte(comparison.right.value)
|
48
|
+
when :"<"
|
49
|
+
@table[comparison.left.name.to_sym].lt(comparison.right.value)
|
50
|
+
when :"<="
|
51
|
+
@table[comparison.left.name.to_sym].lte(comparison.right.value)
|
52
|
+
when :"="
|
53
|
+
@table[comparison.left.name.to_sym].eq(comparison.right.value)
|
54
|
+
when :"<>"
|
55
|
+
@table[comparison.left.name.to_sym].neq(comparison.right.value)
|
56
|
+
when :LIKE
|
57
|
+
@table[comparison.left.name.to_sym].matches(comparison.right.value)
|
58
|
+
else raise
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require_relative 'arel_search/builder'
|
metadata
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: arel_search_builder
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Antoine Niek
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-03-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: sql_search_parser
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: arel
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Parses and Build Arel where clause search conditions from input string
|
70
|
+
email: antoineniek@gmail.com
|
71
|
+
executables: []
|
72
|
+
extensions: []
|
73
|
+
extra_rdoc_files: []
|
74
|
+
files:
|
75
|
+
- LICENSE
|
76
|
+
- README.md
|
77
|
+
- Rakefile
|
78
|
+
- lib/arel_search/builder.rb
|
79
|
+
- lib/arel_search/version.rb
|
80
|
+
- lib/arel_search_builder.rb
|
81
|
+
homepage: https://github.com/zapo/arel_search_builder
|
82
|
+
licenses:
|
83
|
+
- MIT
|
84
|
+
metadata: {}
|
85
|
+
post_install_message:
|
86
|
+
rdoc_options: []
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - '>='
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
requirements: []
|
100
|
+
rubyforge_project:
|
101
|
+
rubygems_version: 2.0.14
|
102
|
+
signing_key:
|
103
|
+
specification_version: 4
|
104
|
+
summary: Parses and Build Arel where clause search conditions from input string
|
105
|
+
test_files: []
|