smql 0.0.4.8 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.4.8
1
+ 0.0.5
@@ -222,10 +222,8 @@ class SmqlToAR
222
222
  SmqlToAR.logger = ::Rails.logger
223
223
  end
224
224
  end
225
- else
226
- require 'logger'
227
- @@logger = Logger.new $stdout
228
225
  end
226
+ @@logger = ::Rails.logger || begin require 'logger'; Logger.new( $stdout) end
229
227
 
230
228
  class <<self
231
229
  def logger=(logger) @@logger = logger end
@@ -294,6 +292,7 @@ class SmqlToAR
294
292
  def self.reload_library
295
293
  lib_dir = File.dirname __FILE__
296
294
  fj = lambda {|*a| File.join lib_dir, *a }
295
+ Object.send :remove_const, :SmqlToAR
297
296
  load fj.call( 'smql_to_ar.rb')
298
297
  load fj.call( 'smql_to_ar', 'condition_types.rb')
299
298
  load fj.call( 'smql_to_ar', 'query_builder.rb')
@@ -14,6 +14,9 @@
14
14
  # You should have received a copy of the GNU General Public License
15
15
  # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
16
 
17
+ # TODO:
18
+ # * Array als Typ ist zu allgemein. Allgemeine Lösung gesucht, um die Typen im Array angeben zu können.
19
+
17
20
  class SmqlToAR
18
21
  #############################################################################
19
22
  # Alle Subklassen (qualitativ: ConditionTypes::*), die als Superklasse Condition haben,
@@ -104,7 +107,7 @@ class SmqlToAR
104
107
  end
105
108
 
106
109
  def inspect
107
- "#{self.name}(:operator=>#{self::Operator.inspect}, :expected=>#{self::Expected.inspect}, :where=>#{self::Where.inspect})"
110
+ "#{self.name}( :operator=>#{self::Operator.inspect}, :expected=>#{self::Expected.inspect}, :where=>#{self::Where.inspect})"
108
111
  end
109
112
  end
110
113
 
@@ -175,11 +178,11 @@ class SmqlToAR
175
178
 
176
179
  class NotInRange < Condition
177
180
  Operator = '!..'
178
- Where = "%s NOT BETWEEN %s AND %s"
181
+ Where = '%s NOT BETWEEN %s AND %s'
179
182
  Expected = [Range, lambda {|val| Array === val && 2 == val.length } ]
180
183
 
181
184
  def initialize model, cols, val
182
- if Array === val && 2 == val.length
185
+ if Array === val
183
186
  f, l = val
184
187
  f, l = Time.parse(f), Time.parse(l) if f.kind_of? String
185
188
  val = f..l
@@ -196,7 +199,45 @@ class SmqlToAR
196
199
  self
197
200
  end
198
201
  end
199
- InRange = simple_condition NotInRange, '..', "%s BETWEEN %s AND %s"
202
+ InRange = simple_condition NotInRange, '..', '%s BETWEEN %s AND %s'
203
+
204
+ class NotOverlaps < Condition
205
+ Operator, Where = '<!>', 'NOT (%s, %s) OVERLAPS (%s, %s)'
206
+ Expected = [Range, lambda {|val|
207
+ Array === val && 2 == val.length &&
208
+ [Time, Date, String].any? {|v|v===val[0]} &&
209
+ [Numeric, String].any? {|v|v===val[1]}
210
+ }]
211
+
212
+ def initialize model, cols, val
213
+ if Array === val
214
+ f = Time.parse( val[0]).localtime
215
+ l = val[1]
216
+ l = case l
217
+ when String then Time.parse( l).localtime
218
+ when Numeric then f+l
219
+ else raise ArgumentError, "Unexpected type for end-value #{l.inspect}"
220
+ end
221
+ f += f.utc_offset
222
+ l += l.utc_offset
223
+ val = f.utc..l.utc
224
+ end
225
+ super model, cols, val
226
+ end
227
+
228
+ def build builder, table
229
+ builder.wobs (v1 = builder.vid).to_sym => @value.begin, (v2 = builder.vid).to_sym => @value.end
230
+ v1 = "TIMESTAMP #{v1}"
231
+ v2 = "TIMESTAMP #{v2}"
232
+ @cols.each do |col|
233
+ col.joins builder, table
234
+ end.each_slice 2 do |f,s|
235
+ builder.where self.class::Where % [
236
+ builder.column( table+f.path, f.col), builder.column( table+s.path, s.col), v1, v2]
237
+ end
238
+ end
239
+ end
240
+ Overlaps = simple_condition NotOverlaps, '<=>', '(%s, %s) OVERLAPS (%s, %s)'
200
241
 
201
242
  class NotIn < Condition
202
243
  Operator = '!|='
@@ -219,6 +260,13 @@ class SmqlToAR
219
260
  NotEqual2 = simple_condition Condition, '<>', "%s <> %s", [Array, String, Numeric]
220
261
  GreaterThanOrEqual = simple_condition Condition, '>=', "%s >= %s", [Array, Numeric]
221
262
  LesserThanOrEqual = simple_condition Condition, '<=', "%s <= %s", [Array, Numeric]
263
+ class StringTimeGreaterThanOrEqual < Condition
264
+ Operator, Where, Expected = '>=', '%s >= %s', [Time, Date, String]
265
+ def initialize model, cols, val
266
+ super model, cols, Time.parse( val.to_s)
267
+ end
268
+ end
269
+ StringTimeLesserThanOrEqual = simple_condition StringTimeGreaterThanOrEqual, '<=', "%s <= %s"
222
270
 
223
271
  # Examples:
224
272
  # { 'articles=>' => { id: 1 } }
@@ -303,10 +351,12 @@ class SmqlToAR
303
351
  end
304
352
  =end
305
353
 
306
- Equal = simple_condition Condition, '=', "%s = %s", [Array, String, Numeric]
307
- Equal2 = simple_condition Equal, '', "%s = %s", [String, Numeric]
354
+ Equal = simple_condition Condition, '=', "%s = %s", [Array, String, Numeric, Date, Time]
355
+ Equal2 = simple_condition Equal, '', "%s = %s", [String, Numeric, Date, Time]
308
356
  GreaterThan = simple_condition Condition, '>', "%s > %s", [Array, Numeric]
357
+ StringTimeGreaterThan = simple_condition StringTimeGreaterThanOrEqual, '>', "%s > %s"
309
358
  LesserThan = simple_condition Condition, '<', "%s < %s", [Array, Numeric]
359
+ StringTimeLesserThan = simple_condition StringTimeGreaterThanOrEqual, '<', "%s < %s"
310
360
  NotIlike = simple_condition Condition, '!~', "%s NOT ILIKE %s", [Array, String]
311
361
  Ilike = simple_condition Condition, '~', "%s ILIKE %s", [Array, String]
312
362
  Exists = simple_condition Condition, '', '%s IS NOT NULL', [TrueClass]
@@ -352,7 +402,7 @@ class SmqlToAR
352
402
  end
353
403
 
354
404
  def inspect
355
- "#{self.name}(:name=>#{self::Name}, :expected=>#{self::Expected})"
405
+ "#{self.name}( :name=>#{self::Name}, :expected=>#{self::Expected})"
356
406
  end
357
407
  end
358
408
 
metadata CHANGED
@@ -5,9 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 4
9
- - 8
10
- version: 0.0.4.8
8
+ - 5
9
+ version: 0.0.5
11
10
  platform: ruby
12
11
  authors:
13
12
  - Denis Knauf