sql_query_executor 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/sql_query_executor/base.rb +13 -0
- data/lib/sql_query_executor/query/base.rb +18 -17
- data/lib/sql_query_executor/query/sentence.rb +2 -18
- data/lib/sql_query_executor/query/sub_query.rb +2 -0
- data/lib/sql_query_executor/version.rb +1 -1
- data/spec/sql_query_executor/base_spec.rb +8 -0
- 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: 5ffa9b8c4f66f8c73e4accab049add69370287f2
|
4
|
+
data.tar.gz: e127e07ab476cf8e6286e20cda3c02605a2f5f18
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8fdb2800683d3cc07b58ca3f311ad47f4606179fb87a1b2f70d42e5a1d44c8e28c9f40024d3fb42481b376df671fedd8595166ca3b15b3cb1d1b2ed8319f2c5a
|
7
|
+
data.tar.gz: 58fbe23d4bebfd7dc6ddcf8ad3d01efc32df3aa3938b5a9edb41204ef74c269664da482d8effa6a703f656934c6183582f1c0503efe46788f6e247a0a2c7d55a
|
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'ostruct'
|
2
2
|
require 'sql_query_executor/query/base'
|
3
|
+
require 'sql_query_executor/query/sub_query'
|
3
4
|
|
4
5
|
# Simulates a SQL where clause to filter objects from the database
|
5
6
|
module SqlQueryExecutor #:nodoc:
|
@@ -17,6 +18,12 @@ module SqlQueryExecutor #:nodoc:
|
|
17
18
|
# Recursive method that divides the query in sub queries, executes each part individually
|
18
19
|
# and finally relates its results as specified in the query.
|
19
20
|
def where(*query)
|
21
|
+
raise ArgumentError.new("must pass at least one argument") if query.empty?
|
22
|
+
query = query.first if query.respond_to?(:size) && query.size == 1
|
23
|
+
|
24
|
+
message = check_query(query)
|
25
|
+
raise ArgumentError.new(message) if message
|
26
|
+
|
20
27
|
query = SqlQueryExecutor::Query::Base.new(query, @collection)
|
21
28
|
query.execute!
|
22
29
|
end
|
@@ -33,5 +40,11 @@ module SqlQueryExecutor #:nodoc:
|
|
33
40
|
def conforming_collection?(collection)
|
34
41
|
collection.first.respond_to?(:attributes)
|
35
42
|
end
|
43
|
+
|
44
|
+
def check_query(query)
|
45
|
+
if query.is_a?(Array) && !query.first.is_a?(String)
|
46
|
+
"First element from array must be a String. eg: [\"name = ?\", \"John\"]"
|
47
|
+
end
|
48
|
+
end
|
36
49
|
end
|
37
50
|
end
|
@@ -4,9 +4,9 @@ module SqlQueryExecutor
|
|
4
4
|
attr_reader :query
|
5
5
|
|
6
6
|
CONVERT_METHODS = {"String" => ["get_query", ""], "Array" => ["interpolate_query", "query.flatten"], "Hash" => ["concatenate_hash", "query"]}
|
7
|
-
STRING_SPACE
|
8
|
-
QUERY_SPACE
|
9
|
-
TEMP_SPACE
|
7
|
+
STRING_SPACE = "$SS$"
|
8
|
+
QUERY_SPACE = "$QS$"
|
9
|
+
TEMP_SPACE = "$TS$"
|
10
10
|
|
11
11
|
def initialize(query, collection)
|
12
12
|
query = clean_query_attribute(query)
|
@@ -50,33 +50,34 @@ module SqlQueryExecutor
|
|
50
50
|
|
51
51
|
# Removes all accents and other non default characters
|
52
52
|
def sanitize(query)
|
53
|
-
|
53
|
+
new_query = replace_on_query(query, /(["|'].*?["|'])/, " ", STRING_SPACE)
|
54
|
+
new_query = replace_on_query(new_query, /(\(.*?\))/, " ", QUERY_SPACE)
|
54
55
|
|
56
|
+
remove_spaces(prepare_query(new_query))
|
57
|
+
end
|
58
|
+
|
59
|
+
def replace_on_query(query, regexp, pattern, replacement)
|
55
60
|
new_query = query ? query.dup : query
|
56
|
-
|
61
|
+
|
62
|
+
params = new_query.scan(regexp).flatten.compact
|
57
63
|
|
58
64
|
params.each do |param|
|
59
65
|
new_param = param.dup
|
60
66
|
|
61
|
-
new_param = new_param.gsub(
|
67
|
+
new_param = new_param.gsub(pattern, replacement)
|
62
68
|
|
63
69
|
new_query = new_query.gsub(param, new_param)
|
64
70
|
end
|
65
71
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
params.each do |param|
|
70
|
-
new_param = param.dup
|
71
|
-
|
72
|
-
new_param = new_param.gsub(" ", QUERY_SPACE)
|
72
|
+
new_query
|
73
|
+
end
|
73
74
|
|
74
|
-
|
75
|
+
def prepare_query(query)
|
76
|
+
SubQuery::BINDING_OPERATORS.keys.each do |operator|
|
77
|
+
query.gsub!(" #{operator} ", "#{TEMP_SPACE}#{operator}#{QUERY_SPACE}")
|
75
78
|
end
|
76
79
|
|
77
|
-
query
|
78
|
-
|
79
|
-
remove_spaces(query)
|
80
|
+
query.gsub(" ", QUERY_SPACE).gsub(TEMP_SPACE, " ")
|
80
81
|
end
|
81
82
|
|
82
83
|
def remove_spaces(query)
|
@@ -6,7 +6,7 @@ require 'sql_query_executor/operators/in'
|
|
6
6
|
module SqlQueryExecutor
|
7
7
|
module Query
|
8
8
|
class Sentence
|
9
|
-
attr_reader :query, :
|
9
|
+
attr_reader :query, :operator
|
10
10
|
|
11
11
|
OPERATORS = {
|
12
12
|
"between" => SqlQueryExecutor::Operators::Between,
|
@@ -23,28 +23,18 @@ module SqlQueryExecutor
|
|
23
23
|
"exists" => SqlQueryExecutor::Operators::Default,#Exists
|
24
24
|
}
|
25
25
|
|
26
|
-
BINDING_OPERATORS = {
|
27
|
-
"and" => "&",
|
28
|
-
"or" => "+"
|
29
|
-
}
|
30
|
-
|
31
26
|
def initialize(query, collection)
|
32
27
|
@query = query
|
33
28
|
@collection = collection
|
34
29
|
@array = query.split(' ')
|
35
30
|
|
36
|
-
set_binding_operator
|
37
31
|
set_operator
|
38
32
|
end
|
39
33
|
|
40
34
|
def execute!(data)
|
41
35
|
return [] unless @operator
|
42
36
|
|
43
|
-
|
44
|
-
|
45
|
-
result = data.send(@binding_operator, result) if @binding_operator && (data && !data.empty?)
|
46
|
-
|
47
|
-
result
|
37
|
+
@operator.execute!(data)
|
48
38
|
end
|
49
39
|
|
50
40
|
private
|
@@ -54,12 +44,6 @@ module SqlQueryExecutor
|
|
54
44
|
@operator = operator ? operator.new(@query, @collection) : nil
|
55
45
|
end
|
56
46
|
|
57
|
-
def set_binding_operator
|
58
|
-
@binding_operator = BINDING_OPERATORS[@array.first]
|
59
|
-
|
60
|
-
fix_query if @binding_operator
|
61
|
-
end
|
62
|
-
|
63
47
|
def fix_query
|
64
48
|
@array.delete_at(0)
|
65
49
|
|
@@ -16,6 +16,14 @@ describe SqlQueryExecutor, "Base" do
|
|
16
16
|
subject { SqlQueryExecutor::Base.new(@data) }
|
17
17
|
|
18
18
|
describe ".where" do
|
19
|
+
context "when invalid query is passed" do
|
20
|
+
it "raises an ArgumentError" do
|
21
|
+
query = [{name: "John"}]
|
22
|
+
|
23
|
+
expect { subject.where(query) }.to raise_error(ArgumentError, "First element from array must be a String. eg: [\"name = ?\", \"John\"]")
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
19
27
|
describe "=" do
|
20
28
|
context "when attribute is string" do
|
21
29
|
it "matches a record" do
|
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.1.
|
4
|
+
version: 0.1.1
|
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-02-
|
11
|
+
date: 2014-02-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|