sql_query_executor 0.4.0 → 0.5.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6b71902eb1b5e7adc42d527acac3fdaf40e22fee
4
- data.tar.gz: 358c703da427d72bdfd3fdc0739311791124840b
3
+ metadata.gz: 2a88fb06f74901e2e2f0f1aa38bd63da77a0c062
4
+ data.tar.gz: 88c2390a87b8a152e7cf4101d2be0e2493a91060
5
5
  SHA512:
6
- metadata.gz: d497c41d0cfc5a55c2108fe28624d34006a5be0cde82f95d99161a328f79ea5db7f4a47dccb6015d6694bc78d3f935abefbbf61dd7578819ee66f9b69caeeec3
7
- data.tar.gz: 6a878452fa8648c31e45e8a4c340a362f5a8fa694ef5985895db6fb4e9cc1fa3d60d0afb6d0bf628349d8d1d33f937aba219ac5d03e239820244fbafd7b296dd
6
+ metadata.gz: de9a0a0c32c214245a021ffe62c561a975463048f1ad4c86e177e2e6704dc6541bcd490f1673f9fef00b0b4bd40cdddcaa3016715652034b621c6edcd84d6b52
7
+ data.tar.gz: db3638046c93c332ac3becfeff6695c6b99b7dab583efaeb4ee9206e71d521ffbc2eda22c51e43266077403120cdcab45b1b6c56ae444078ba72c78656cc4b00
@@ -12,44 +12,66 @@ module SqlQueryExecutor
12
12
  { @field => @value }
13
13
  end
14
14
 
15
+ def logic
16
+ initialize_attributes(true)
17
+ "object.#{@field} #{@operator} #{@value || 'nil'}"
18
+ end
19
+
15
20
  protected
16
- def initialize_attributes
21
+ def initialize_attributes(logic=false)
17
22
  return if @array
18
23
 
19
24
  @query = SqlQueryExecutor::Query::Normalizers::QueryNormalizer.execute(@query).gsub(SqlQueryExecutor::Base::QUERY_SPACE, ' ')
20
25
  @array = @query.split(' ')
21
- @operator = @query.split(' ')[1]
26
+ @operator = convert_operator
22
27
  @field = get_field
23
- @value = get_value
28
+ @value = get_value(logic)
24
29
  end
25
30
 
26
31
  def get_field
27
32
  @array.first
28
33
  end
29
34
 
30
- def get_value
35
+ def get_value(logic=false)
31
36
  value = @array.last.gsub(SqlQueryExecutor::Base::STRING_SPACE, ' ')
32
37
 
33
- convert_value(value)
38
+ convert_value(value, logic)
34
39
  end
35
40
 
36
- def convert_value(value)
41
+ def convert_value(value, logic=false)
37
42
  value.gsub!(/[\(\)\'\"]/, "")
38
43
  return value.to_i if is_a_number?(value)
44
+ return value.to_f if is_a_number?(value, true)
39
45
  return eval(value) if ['true', 'false'].include?(value)
40
46
 
41
47
  methods = {3 => "convert_date", 7 => "convert_time"}
42
48
 
43
49
  array = split(value)
44
50
 
45
- value = (send(methods[array.size], array) || value) if methods.keys.include?(array.size)
51
+ if methods.keys.include?(array.size)
52
+ value = (send(methods[array.size], array, logic) || (logic ? "'#{value}'" : value))
53
+ elsif logic
54
+ value = "'#{value}'" if value.is_a?(String)
55
+ end
46
56
 
47
57
  value
48
58
  end
49
59
 
50
60
  private
51
- def is_a_number?(value)
52
- value.to_s == value.to_i.to_s
61
+ def is_a_number?(value, float=false)
62
+ if float
63
+ Float(value)
64
+ else
65
+ Integer(value)
66
+ end rescue false
67
+ end
68
+
69
+ def convert_operator
70
+ operators_to_convert = {'<>' => '!=', '=' => '=='}
71
+
72
+ operator = @array[1]
73
+
74
+ operators_to_convert[operator] || operator
53
75
  end
54
76
 
55
77
  def split(value)
@@ -60,17 +82,26 @@ module SqlQueryExecutor
60
82
  array.flatten
61
83
  end
62
84
 
63
- def convert_date(args)
85
+ def convert_date(args, logic=false)
64
86
  return if args.first.to_i < 1000
65
87
 
66
- Date.new(args[0].to_i, args[1].to_i, args[2].to_i)
88
+ if logic
89
+ "Date.new(#{args[0].to_i}, #{args[1].to_i}, #{args[2].to_i})"
90
+ else
91
+ Date.new(args[0].to_i, args[1].to_i, args[2].to_i)
92
+ end
67
93
  end
68
94
 
69
- def convert_time(args)
95
+ def convert_time(args, logic=false)
70
96
  return if args.first.to_i < 1000
71
97
  args[6] = args[6].gsub('00', ':00').gsub("+:", "+")
72
98
 
73
- Time.new(*args)
99
+ if logic
100
+ timezone = args.delete(args.last)
101
+ "Time.new(#{args.join(',')},'#{timezone}')"
102
+ else
103
+ Time.new(*args)
104
+ end
74
105
  end
75
106
  end
76
107
  end
@@ -3,33 +3,23 @@ require 'sql_query_executor/operators/base'
3
3
  module SqlQueryExecutor
4
4
  module Operators
5
5
  class Between < SqlQueryExecutor::Operators::Base
6
- def execute!(collection)
7
- initialize_attributes
8
- collection.select do |record|
9
- value = convert_value(record.send(@field).to_s)
10
-
11
- if value.class != @value.first.class
12
- false
13
- else
14
- greather_than = value.send('>=', @value.first)
15
- smaller_than = value.send('<=', @value.last)
16
-
17
- greather_than && smaller_than
18
- end
19
- end
20
- end
21
-
22
6
  def selector
23
7
  initialize_attributes
24
8
  { @field => { "$gte" => @value.first, "$lte" => @value.last }}
25
9
  end
26
10
 
11
+ def logic
12
+ initialize_attributes(true)
13
+
14
+ "object.#{@field} >= #{@value[0]} && object.#{@field} <= #{@value[1]}"
15
+ end
16
+
27
17
  private
28
- def get_value
18
+ def get_value(logic=false)
29
19
  value = []
30
20
 
31
- value << convert_value(@array[2].gsub(SqlQueryExecutor::Base::STRING_SPACE, ' '))
32
- value << convert_value(@array[4].gsub(SqlQueryExecutor::Base::STRING_SPACE, ' '))
21
+ value << convert_value(@array[2].gsub(SqlQueryExecutor::Base::STRING_SPACE, ' '), logic)
22
+ value << convert_value(@array[4].gsub(SqlQueryExecutor::Base::STRING_SPACE, ' '), logic)
33
23
  end
34
24
  end
35
25
  end
@@ -12,17 +12,6 @@ module SqlQueryExecutor
12
12
  "!=" => "$ne"
13
13
  }
14
14
 
15
- def execute!(collection)
16
- initialize_attributes
17
- collection.select do |record|
18
- value = record.send(@field.to_s)
19
-
20
- value = convert_value(value) rescue value
21
-
22
- value.send(@operator, @value) rescue false
23
- end
24
- end
25
-
26
15
  def selector
27
16
  initialize_attributes
28
17
 
@@ -32,15 +21,6 @@ module SqlQueryExecutor
32
21
  end
33
22
 
34
23
  private
35
- def initialize_attributes
36
- super
37
- convert_operator
38
- end
39
-
40
- def convert_operator
41
- @operator = @operator == "=" ? "==" : @operator
42
- @operator = @operator == "<>" ? "!=" : @operator
43
- end
44
24
  end
45
25
  end
46
26
  end
@@ -3,25 +3,22 @@ require 'sql_query_executor/operators/base'
3
3
  module SqlQueryExecutor
4
4
  module Operators
5
5
  class In < SqlQueryExecutor::Operators::Base
6
- def execute!(collection)
7
- initialize_attributes
8
- result = collection.select do |record|
9
- value = record.send(@field)
10
-
11
- @value.send('include?', value)
12
- end
13
- end
14
-
15
6
  def selector
16
7
  initialize_attributes
17
8
  { @field => { "$in" => @value }}
18
9
  end
19
10
 
11
+ def logic
12
+ initialize_attributes(true)
13
+
14
+ "[#{@value.join(', ')}].include?(object.#{@field})"
15
+ end
16
+
20
17
  private
21
- def get_value
18
+ def get_value(logic=false)
22
19
  value = super
23
20
 
24
- value.gsub(SqlQueryExecutor::Base::STRING_SPACE, '').split(',').map{ |v| convert_value(v) }
21
+ value.gsub(SqlQueryExecutor::Base::STRING_SPACE, '').split(',').map{ |v| convert_value(v, logic) }
25
22
  end
26
23
  end
27
24
  end
@@ -3,16 +3,6 @@ require 'sql_query_executor/operators/base'
3
3
  module SqlQueryExecutor
4
4
  module Operators
5
5
  class Is < SqlQueryExecutor::Operators::Base
6
- def execute!(collection)
7
- initialize_attributes
8
-
9
- collection.select do |record|
10
- value = record.send(@field)
11
-
12
- value.send(@operator, @value)
13
- end
14
- end
15
-
16
6
  def selector
17
7
  initialize_attributes
18
8
 
@@ -20,12 +10,13 @@ module SqlQueryExecutor
20
10
  end
21
11
 
22
12
  private
23
- def initialize_attributes
13
+ def initialize_attributes(logic=false)
24
14
  super
25
15
  convert_operator
26
16
  end
27
- def get_value
28
- @array.include?('null') ? nil : convert_value(@array.last)
17
+
18
+ def get_value(logic=false)
19
+ @array.include?('null') ? nil : convert_value(@array.last, logic)
29
20
  end
30
21
 
31
22
  def convert_operator
@@ -30,17 +30,14 @@ module SqlQueryExecutor
30
30
  set_operator
31
31
  end
32
32
 
33
- # The data parameter is only declared for SubQuery compatibility purposes
34
- def execute!(collection, data=[])
35
- return [] unless @operator
36
-
37
- @operator.execute!(collection)
38
- end
39
-
40
33
  def selector
41
34
  @operator.selector
42
35
  end
43
36
 
37
+ def logic
38
+ @operator.logic
39
+ end
40
+
44
41
  private
45
42
  def set_operator
46
43
  operator = OPERATORS[@query.split(' ')[1]]
@@ -14,15 +14,7 @@ module SqlQueryExecutor
14
14
  end
15
15
 
16
16
  def execute!(collection, data=[])
17
- return [] unless @children
18
-
19
- result = []
20
-
21
- @children.each do |child|
22
- result = child.execute!(collection, result)
23
- end
24
-
25
- result = (data || []).send(@binding_operator, result) if @binding_operator
17
+ result = collection.select{ |object| eval(logic) }
26
18
 
27
19
  result.sort_by(&:id)
28
20
  end
@@ -46,6 +38,21 @@ module SqlQueryExecutor
46
38
  SqlQueryExecutor::Query::Normalizers::QueryNormalizer.clean_query(@query)
47
39
  end
48
40
 
41
+ def logic
42
+ string = ''
43
+
44
+ @children.each do |child|
45
+ if child.respond_to?(:binding_operator) && child.binding_operator
46
+ operator = BINDING_OPERATORS.invert[child.binding_operator]
47
+ string = "(#{string} #{operator} #{child.logic})"
48
+ else
49
+ string += child.logic
50
+ end
51
+ end
52
+
53
+ string
54
+ end
55
+
49
56
  private
50
57
  def initialize_attributes
51
58
  return if @kind
@@ -1,3 +1,3 @@
1
1
  module SqlQueryExecutor
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.2"
3
3
  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.0
4
+ version: 0.5.2
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-25 00:00:00.000000000 Z
11
+ date: 2014-08-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec