sql_query_executor 0.3.7 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/sql_query_executor/base.rb +38 -16
- data/lib/sql_query_executor/operators/base.rb +13 -7
- data/lib/sql_query_executor/operators/between.rb +4 -2
- data/lib/sql_query_executor/operators/default.rb +10 -7
- data/lib/sql_query_executor/operators/in.rb +4 -2
- data/lib/sql_query_executor/operators/is.rb +9 -6
- data/lib/sql_query_executor/query/normalizers/query_normalizer.rb +2 -1
- data/lib/sql_query_executor/query/sentence.rb +4 -5
- data/lib/sql_query_executor/query/sub_query.rb +17 -11
- data/lib/sql_query_executor/version.rb +1 -1
- data/spec/sql_query_executor/base_spec.rb +16 -17
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6b71902eb1b5e7adc42d527acac3fdaf40e22fee
|
4
|
+
data.tar.gz: 358c703da427d72bdfd3fdc0739311791124840b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d497c41d0cfc5a55c2108fe28624d34006a5be0cde82f95d99161a328f79ea5db7f4a47dccb6015d6694bc78d3f935abefbbf61dd7578819ee66f9b69caeeec3
|
7
|
+
data.tar.gz: 6a878452fa8648c31e45e8a4c340a362f5a8fa694ef5985895db6fb4e9cc1fa3d60d0afb6d0bf628349d8d1d33f937aba219ac5d03e239820244fbafd7b296dd
|
@@ -9,37 +9,59 @@ module SqlQueryExecutor #:nodoc:
|
|
9
9
|
QUERY_SPACE = "$QS$"
|
10
10
|
TEMP_SPACE = "$TS$"
|
11
11
|
|
12
|
-
def initialize(
|
13
|
-
|
12
|
+
def initialize(query)
|
13
|
+
if query.respond_to?(:size) && query.size == 1 && !query.is_a?(Hash)
|
14
|
+
@query = query.first
|
15
|
+
else
|
16
|
+
@query = query
|
17
|
+
end
|
14
18
|
|
15
|
-
message = check_query
|
19
|
+
message = check_query
|
16
20
|
raise ArgumentError.new(message) if message
|
17
|
-
|
18
|
-
get_collection(collection)
|
19
|
-
query = Query::Normalizers::QueryNormalizer.execute(query)
|
20
|
-
@query = SqlQueryExecutor::Query::SubQuery.new query, @collection
|
21
21
|
end
|
22
22
|
|
23
|
-
def execute!
|
24
|
-
|
23
|
+
def execute!(collection)
|
24
|
+
set_collection(collection)
|
25
|
+
|
26
|
+
convert_query.execute!(@collection)
|
25
27
|
end
|
26
28
|
|
27
29
|
def to_sql
|
28
|
-
@
|
30
|
+
return @to_sql if @to_sql
|
31
|
+
|
32
|
+
@to_sql = @query
|
33
|
+
|
34
|
+
return @query if @query.is_a?(String)
|
35
|
+
|
36
|
+
@to_sql = convert_query.to_sql
|
29
37
|
end
|
30
38
|
|
31
39
|
def selector
|
32
|
-
@
|
40
|
+
return @selector if @selector
|
41
|
+
|
42
|
+
@selector = @query
|
43
|
+
|
44
|
+
return @query if @query.is_a?(Hash)
|
45
|
+
|
46
|
+
@selector = convert_query.selector
|
33
47
|
end
|
34
48
|
|
35
49
|
# Recursive method that divides the query in sub queries, executes each part individually
|
36
50
|
# and finally relates its results as specified in the query.
|
37
51
|
def self.where(collection, *query)
|
38
|
-
self.new(
|
52
|
+
self.new(query).execute!(collection)
|
39
53
|
end
|
40
54
|
|
41
55
|
private
|
42
|
-
def
|
56
|
+
def convert_query
|
57
|
+
return @convert_query if @convert_query
|
58
|
+
|
59
|
+
query = Query::Normalizers::QueryNormalizer.execute(@query)
|
60
|
+
|
61
|
+
@convert_query = SqlQueryExecutor::Query::SubQuery.new(query)
|
62
|
+
end
|
63
|
+
|
64
|
+
def set_collection(collection)
|
43
65
|
if collection.any? && collection.first.is_a?(Hash)
|
44
66
|
convert_collection(collection)
|
45
67
|
else
|
@@ -47,7 +69,7 @@ module SqlQueryExecutor #:nodoc:
|
|
47
69
|
end
|
48
70
|
end
|
49
71
|
|
50
|
-
def convert_collection(collection)
|
72
|
+
def convert_collection(collection=[])
|
51
73
|
@collection = []
|
52
74
|
collection.each do |object|
|
53
75
|
attributes = object.is_a?(Hash) ? object : object.attributes
|
@@ -60,8 +82,8 @@ module SqlQueryExecutor #:nodoc:
|
|
60
82
|
collection.first.respond_to?(:attributes)
|
61
83
|
end
|
62
84
|
|
63
|
-
def check_query
|
64
|
-
if query.is_a?(Array) &&
|
85
|
+
def check_query
|
86
|
+
if @query.is_a?(Array) && !@query.first.is_a?(String)
|
65
87
|
"First element from array must be a String. eg: [\"name = ?\", \"John\"]"
|
66
88
|
end
|
67
89
|
end
|
@@ -3,20 +3,26 @@ require 'sql_query_executor/base'
|
|
3
3
|
module SqlQueryExecutor
|
4
4
|
module Operators
|
5
5
|
class Base
|
6
|
-
def initialize(query
|
7
|
-
@query
|
8
|
-
@collection = collection
|
9
|
-
@array = @query.split(' ')
|
10
|
-
@operator = @query.split(' ')[1]
|
11
|
-
@field = get_field
|
12
|
-
@value = get_value
|
6
|
+
def initialize(query)
|
7
|
+
@query = query
|
13
8
|
end
|
14
9
|
|
15
10
|
def selector
|
11
|
+
initialize_attributes
|
16
12
|
{ @field => @value }
|
17
13
|
end
|
18
14
|
|
19
15
|
protected
|
16
|
+
def initialize_attributes
|
17
|
+
return if @array
|
18
|
+
|
19
|
+
@query = SqlQueryExecutor::Query::Normalizers::QueryNormalizer.execute(@query).gsub(SqlQueryExecutor::Base::QUERY_SPACE, ' ')
|
20
|
+
@array = @query.split(' ')
|
21
|
+
@operator = @query.split(' ')[1]
|
22
|
+
@field = get_field
|
23
|
+
@value = get_value
|
24
|
+
end
|
25
|
+
|
20
26
|
def get_field
|
21
27
|
@array.first
|
22
28
|
end
|
@@ -3,8 +3,9 @@ require 'sql_query_executor/operators/base'
|
|
3
3
|
module SqlQueryExecutor
|
4
4
|
module Operators
|
5
5
|
class Between < SqlQueryExecutor::Operators::Base
|
6
|
-
def execute!
|
7
|
-
|
6
|
+
def execute!(collection)
|
7
|
+
initialize_attributes
|
8
|
+
collection.select do |record|
|
8
9
|
value = convert_value(record.send(@field).to_s)
|
9
10
|
|
10
11
|
if value.class != @value.first.class
|
@@ -19,6 +20,7 @@ module SqlQueryExecutor
|
|
19
20
|
end
|
20
21
|
|
21
22
|
def selector
|
23
|
+
initialize_attributes
|
22
24
|
{ @field => { "$gte" => @value.first, "$lte" => @value.last }}
|
23
25
|
end
|
24
26
|
|
@@ -12,13 +12,9 @@ module SqlQueryExecutor
|
|
12
12
|
"!=" => "$ne"
|
13
13
|
}
|
14
14
|
|
15
|
-
def
|
16
|
-
|
17
|
-
|
18
|
-
end
|
19
|
-
|
20
|
-
def execute!
|
21
|
-
@collection.select do |record|
|
15
|
+
def execute!(collection)
|
16
|
+
initialize_attributes
|
17
|
+
collection.select do |record|
|
22
18
|
value = record.send(@field.to_s)
|
23
19
|
|
24
20
|
value = convert_value(value) rescue value
|
@@ -28,12 +24,19 @@ module SqlQueryExecutor
|
|
28
24
|
end
|
29
25
|
|
30
26
|
def selector
|
27
|
+
initialize_attributes
|
28
|
+
|
31
29
|
operator = SELECTORS[@operator]
|
32
30
|
|
33
31
|
{ @field => operator ? {operator => @value} : @value}
|
34
32
|
end
|
35
33
|
|
36
34
|
private
|
35
|
+
def initialize_attributes
|
36
|
+
super
|
37
|
+
convert_operator
|
38
|
+
end
|
39
|
+
|
37
40
|
def convert_operator
|
38
41
|
@operator = @operator == "=" ? "==" : @operator
|
39
42
|
@operator = @operator == "<>" ? "!=" : @operator
|
@@ -3,8 +3,9 @@ require 'sql_query_executor/operators/base'
|
|
3
3
|
module SqlQueryExecutor
|
4
4
|
module Operators
|
5
5
|
class In < SqlQueryExecutor::Operators::Base
|
6
|
-
def execute!
|
7
|
-
|
6
|
+
def execute!(collection)
|
7
|
+
initialize_attributes
|
8
|
+
result = collection.select do |record|
|
8
9
|
value = record.send(@field)
|
9
10
|
|
10
11
|
@value.send('include?', value)
|
@@ -12,6 +13,7 @@ module SqlQueryExecutor
|
|
12
13
|
end
|
13
14
|
|
14
15
|
def selector
|
16
|
+
initialize_attributes
|
15
17
|
{ @field => { "$in" => @value }}
|
16
18
|
end
|
17
19
|
|
@@ -3,13 +3,10 @@ require 'sql_query_executor/operators/base'
|
|
3
3
|
module SqlQueryExecutor
|
4
4
|
module Operators
|
5
5
|
class Is < SqlQueryExecutor::Operators::Base
|
6
|
-
def
|
7
|
-
|
8
|
-
convert_operator
|
9
|
-
end
|
6
|
+
def execute!(collection)
|
7
|
+
initialize_attributes
|
10
8
|
|
11
|
-
|
12
|
-
@collection.select do |record|
|
9
|
+
collection.select do |record|
|
13
10
|
value = record.send(@field)
|
14
11
|
|
15
12
|
value.send(@operator, @value)
|
@@ -17,10 +14,16 @@ module SqlQueryExecutor
|
|
17
14
|
end
|
18
15
|
|
19
16
|
def selector
|
17
|
+
initialize_attributes
|
18
|
+
|
20
19
|
@operator == '==' ? { @field => @value } : { @field => {'$ne' => @value} }
|
21
20
|
end
|
22
21
|
|
23
22
|
private
|
23
|
+
def initialize_attributes
|
24
|
+
super
|
25
|
+
convert_operator
|
26
|
+
end
|
24
27
|
def get_value
|
25
28
|
@array.include?('null') ? nil : convert_value(@array.last)
|
26
29
|
end
|
@@ -12,6 +12,7 @@ module SqlQueryExecutor
|
|
12
12
|
query = clean_query_attribute(query)
|
13
13
|
method = CONVERT_METHODS[query.class.name]
|
14
14
|
|
15
|
+
|
15
16
|
query = sanitize(send(method, query))
|
16
17
|
end
|
17
18
|
|
@@ -21,7 +22,7 @@ module SqlQueryExecutor
|
|
21
22
|
|
22
23
|
def self.attributes_from_query(query)
|
23
24
|
return {} if query.empty?
|
24
|
-
selector = query.class == Hash ? query : Base.new(
|
25
|
+
selector = query.class == Hash ? query : Base.new(query).selector
|
25
26
|
super(selector)
|
26
27
|
end
|
27
28
|
|
@@ -23,19 +23,18 @@ module SqlQueryExecutor
|
|
23
23
|
"exists" => SqlQueryExecutor::Operators::Default,#Exists
|
24
24
|
}
|
25
25
|
|
26
|
-
def initialize(query
|
26
|
+
def initialize(query)
|
27
27
|
@query = query
|
28
|
-
@collection = collection
|
29
28
|
@array = query.split(' ')
|
30
29
|
|
31
30
|
set_operator
|
32
31
|
end
|
33
32
|
|
34
33
|
# The data parameter is only declared for SubQuery compatibility purposes
|
35
|
-
def execute!(data=[])
|
34
|
+
def execute!(collection, data=[])
|
36
35
|
return [] unless @operator
|
37
36
|
|
38
|
-
@operator.execute!
|
37
|
+
@operator.execute!(collection)
|
39
38
|
end
|
40
39
|
|
41
40
|
def selector
|
@@ -46,7 +45,7 @@ module SqlQueryExecutor
|
|
46
45
|
def set_operator
|
47
46
|
operator = OPERATORS[@query.split(' ')[1]]
|
48
47
|
|
49
|
-
@operator = operator ? operator.new(@query
|
48
|
+
@operator = operator ? operator.new(@query) : nil
|
50
49
|
end
|
51
50
|
end
|
52
51
|
end
|
@@ -6,22 +6,20 @@ module SqlQueryExecutor
|
|
6
6
|
BINDING_OPERATORS = { "or" => "+", "and" => "&" }
|
7
7
|
attr_reader :children, :kind, :binding_operator
|
8
8
|
ADD_CHILDREN_METHODS = { sentence: :add_sentence_children, sub_query: :add_sub_query_children }
|
9
|
-
|
10
|
-
|
11
|
-
@
|
12
|
-
@
|
13
|
-
|
14
|
-
set_kind
|
15
|
-
set_children
|
9
|
+
|
10
|
+
def initialize(query)
|
11
|
+
@children = []
|
12
|
+
@query = query
|
13
|
+
initialize_attributes
|
16
14
|
end
|
17
15
|
|
18
|
-
def execute!(data=[])
|
16
|
+
def execute!(collection, data=[])
|
19
17
|
return [] unless @children
|
20
18
|
|
21
19
|
result = []
|
22
20
|
|
23
21
|
@children.each do |child|
|
24
|
-
result = child.execute!(result)
|
22
|
+
result = child.execute!(collection, result)
|
25
23
|
end
|
26
24
|
|
27
25
|
result = (data || []).send(@binding_operator, result) if @binding_operator
|
@@ -49,6 +47,14 @@ module SqlQueryExecutor
|
|
49
47
|
end
|
50
48
|
|
51
49
|
private
|
50
|
+
def initialize_attributes
|
51
|
+
return if @kind
|
52
|
+
|
53
|
+
set_binding_operator
|
54
|
+
set_kind
|
55
|
+
set_children
|
56
|
+
end
|
57
|
+
|
52
58
|
def set_binding_operator
|
53
59
|
@binding_operator = nil
|
54
60
|
operator = @query.split(Base::QUERY_SPACE).first
|
@@ -68,7 +74,7 @@ module SqlQueryExecutor
|
|
68
74
|
end
|
69
75
|
|
70
76
|
def add_sentence_children
|
71
|
-
@children << SqlQueryExecutor::Query::Sentence.new(@query.gsub(Base::QUERY_SPACE, " ")
|
77
|
+
@children << SqlQueryExecutor::Query::Sentence.new(@query.gsub(Base::QUERY_SPACE, " "))
|
72
78
|
end
|
73
79
|
|
74
80
|
def add_sub_query_children
|
@@ -86,7 +92,7 @@ module SqlQueryExecutor
|
|
86
92
|
|
87
93
|
query = query.gsub("\$op1\$", "").gsub("\$cp1\$", "").gsub(/\$\Sp\d\$/, "")
|
88
94
|
|
89
|
-
@children << SqlQueryExecutor::Query::SubQuery.new(query
|
95
|
+
@children << SqlQueryExecutor::Query::SubQuery.new(query)
|
90
96
|
end
|
91
97
|
end
|
92
98
|
|
@@ -70,30 +70,29 @@ describe SqlQueryExecutor::Base do
|
|
70
70
|
end
|
71
71
|
end
|
72
72
|
end
|
73
|
+
end
|
73
74
|
|
74
|
-
|
75
|
-
|
76
|
-
|
75
|
+
context 'selector' do
|
76
|
+
let(:query) { 'id > 3' }
|
77
|
+
let(:selector) { {'id' => {'$gt' => 3}} }
|
77
78
|
|
78
|
-
|
79
|
-
|
80
|
-
end
|
79
|
+
it 'converts query' do
|
80
|
+
expect(described_class.new(query).selector).to eq selector
|
81
81
|
end
|
82
|
+
end
|
82
83
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
84
|
+
context 'to_sql' do
|
85
|
+
it 'converts selector' do
|
86
|
+
query = "name = 'Brazil'"
|
87
|
+
selector = {name: 'Brazil'}
|
87
88
|
|
88
|
-
|
89
|
-
|
89
|
+
expect(described_class.new(selector).to_sql).to eq query
|
90
|
+
end
|
90
91
|
|
91
|
-
|
92
|
-
|
93
|
-
right_query = 'id is not null'
|
92
|
+
it 'if is a string returns itself' do
|
93
|
+
wrong_query = "id is not 'null'"
|
94
94
|
|
95
|
-
|
96
|
-
end
|
95
|
+
expect(described_class.new(wrong_query).to_sql).to eq wrong_query
|
97
96
|
end
|
98
97
|
end
|
99
98
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sql_query_executor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Caio Torres
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-07-
|
11
|
+
date: 2014-07-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|