dynamic_query 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,8 +2,8 @@ class DynamicQuery
2
2
 
3
3
  module Version
4
4
  MAJOR = 0
5
- MINOR = 1
6
- PATCH = 4
5
+ MINOR = 2
6
+ PATCH = 0
7
7
 
8
8
  STRING = [MAJOR, MINOR, PATCH].compact.join('.')
9
9
  end
data/lib/dynamic_query.rb CHANGED
@@ -86,26 +86,43 @@ module DynamicQuery
86
86
  def statement(query)
87
87
  query ||= { 'or_1' => { 'and_1' => { :column => '', :operator => '', :value1 => '', :value2 => '' } } }
88
88
  or_stat = []
89
- query.select { |k, _| k =~ /^or_\d+$/ }.each do |or_key, or_val|
90
- and_stat = []
89
+ query.select { |k, v| k =~ /^or_\d+$/ && v.kind_of?(Hash) }.each do |or_key, or_val|
90
+ and_stat = [[]]
91
91
  or_val.each do |and_key, and_val|
92
- case and_val[:operator]
93
- when '=', '>', '>=', '<', '<=', '!=', 'LIKE', 'NOT LIKE'
94
- and_stat << "#{and_val[:column]} #{and_val[:operator]} '#{and_val[:value1]}'" unless and_val[:value1].blank?
95
- when 'IN', 'NOT IN'
96
- and_stat << "#{and_val[:column]} #{and_val[:operator]} (#{and_val[:value1].split(/,/).select { |word| !word.strip.empty? }.map { |word| "'" + word.strip + "'" }.join(',')})" unless and_val[:value1].delete(',').blank?
97
- when 'BETWEEN', 'NOT BETWEEN'
98
- and_stat << "#{and_val[:column]} #{and_val[:operator]} '#{and_val[:value1]}' AND '#{and_val[:value2]}'" unless and_val[:value1].blank? || and_val[:value2].blank?
99
- when 'IS NULL', 'IS NOT NULL'
100
- and_stat << "#{and_val[:column]} #{and_val[:operator]}"
92
+ if and_val.kind_of?(Hash) && @columns.include?(and_val[:column])
93
+ case and_val[:operator]
94
+ when '=', '>', '>=', '<', '<=', '!=', 'LIKE', 'NOT LIKE'
95
+ unless and_val[:value1].blank?
96
+ and_stat[0] << "#{and_val[:column]} #{and_val[:operator]} ?"
97
+ and_stat << and_val[:value1]
98
+ end
99
+ when 'IN', 'NOT IN'
100
+ unless and_val[:value1].delete(',').blank?
101
+ and_stat[0] << "#{and_val[:column]} #{and_val[:operator]} (?)"
102
+ and_stat << and_val[:value1].split(/,/).delete_if { |word| word.blank? }.map { |word| word.strip }
103
+ end
104
+ when 'BETWEEN', 'NOT BETWEEN'
105
+ unless and_val[:value1].blank? || and_val[:value2].blank?
106
+ and_stat[0] << "#{and_val[:column]} #{and_val[:operator]} ? AND ?"
107
+ and_stat << and_val[:value1] << and_val[:value2]
108
+ end
109
+ when 'IS NULL', 'IS NOT NULL'
110
+ and_stat[0] << "#{and_val[:column]} #{and_val[:operator]}"
111
+ end
101
112
  end
102
113
  end
114
+
115
+ and_stat[0] = and_stat[0].join(' AND ')
116
+ or_stat << and_stat unless and_stat[0].empty?
117
+ end
103
118
 
104
- and_stat_str = and_stat.join(' AND ')
105
- or_stat << "(#{and_stat_str})" unless and_stat_str.empty?
119
+ or_stat.each { |and_stat| and_stat[0] = "(#{and_stat[0]})" }
120
+ stat = []; params = []
121
+ or_stat.each do |and_stat|
122
+ stat << and_stat.shift
123
+ params = params + and_stat
106
124
  end
107
-
108
- or_stat = or_stat.join(' OR ')
125
+ params.unshift(stat.join(' OR '))
109
126
  end
110
127
 
111
128
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dynamic_query
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -143,7 +143,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
143
143
  version: '0'
144
144
  segments:
145
145
  - 0
146
- hash: 3050679975131115800
146
+ hash: 2346444755047590570
147
147
  required_rubygems_version: !ruby/object:Gem::Requirement
148
148
  none: false
149
149
  requirements:
@@ -155,5 +155,5 @@ rubyforge_project:
155
155
  rubygems_version: 1.8.24
156
156
  signing_key:
157
157
  specification_version: 3
158
- summary: dynamic_query-0.1.4
158
+ summary: dynamic_query-0.2.0
159
159
  test_files: []