arel_extensions 1.2.2 → 1.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bc05c41fcdcdc0c411f00c62947f80e1004791ae
4
- data.tar.gz: 9d8ea2d6dfc47f8268e0eee6fd974fceb2a700e8
3
+ metadata.gz: 19306a80e5058901e7d9db6da1e05cea53e54f3c
4
+ data.tar.gz: e79f09a5b088c2d571079b796cb819ddec25ebd3
5
5
  SHA512:
6
- metadata.gz: ab845f8bd4ad51383c2858542183aedc38cbc1a56158249678fe6f89e5d0aae4b036710fc72d4f5c1b95c33bad430ffbc5844964b31730206c541061cffa1e5f
7
- data.tar.gz: c3bce90a322ffadcb309f2e183ef4c323bca7ee70c11d5ec75e12b6e1ad0d409757fda40826ee9f5645bb64eaa39063d74b61ac82f8ba097ac2a677ed25748ce
6
+ metadata.gz: 29df0447069762b05d5daaa2e53884a3cfa68fef1a67fe9df34ef09923c2ef152da138694a564e59b7302317895b150272f8ef9da5ef2eca20bcde54384068c1
7
+ data.tar.gz: b9102e07381a96d449c9257467e7437f5c4746c6a9f047172f72daf3d5cf0d1fa9c49d5bc6fcc61119032695ee58e84e4db0fdff068d5d820288a591631a9e1b
@@ -1,20 +1,23 @@
1
+ require 'arel_extensions/boolean_functions'
2
+
1
3
  module ArelExtensions
2
4
  module Nodes
3
- class Blank < Function
4
- RETURN_TYPE = :boolean
5
+ class Blank < Arel::Nodes::Unary
6
+ include ArelExtensions::BooleanFunctions
7
+ RETURN_TYPE = :boolean
5
8
 
6
- def initialize expr
7
- # super [expr.first.coalesce('').trim.trim("\t").trim("\n")]
8
- super expr
9
- end
9
+ def initialize expr
10
+ super expr.first
11
+ end
10
12
  end
11
13
 
12
- class NotBlank < Function
13
- RETURN_TYPE = :boolean
14
+ class NotBlank < Arel::Nodes::Unary
15
+ include ArelExtensions::BooleanFunctions
16
+ RETURN_TYPE = :boolean
14
17
 
15
- def initialize expr
16
- super expr
17
- end
18
+ def initialize expr
19
+ super expr.first
20
+ end
18
21
  end
19
22
 
20
23
  end
@@ -1,11 +1,11 @@
1
1
  module ArelExtensions
2
2
  module Nodes
3
3
  class Downcase < Function
4
- RETURN_TYPE = :string
4
+ RETURN_TYPE = :string
5
5
  end
6
6
 
7
7
  class Upcase < Function
8
- RETURN_TYPE = :string
8
+ RETURN_TYPE = :string
9
9
  end
10
10
  end
11
11
  end
@@ -1,18 +1,18 @@
1
1
  module ArelExtensions
2
2
  module Nodes
3
3
  class Collate < Function
4
- RETURN_TYPE = :string
5
-
6
- attr_accessor :ai, :ci, :option
7
-
8
- def initialize left, option=nil, ai=false, ci=false
9
- @ai = ai
10
- @ci = ci
11
- @option = option
12
- tab = [convert_to_node(left)]
13
- return super(tab)
14
- end
15
-
4
+ RETURN_TYPE = :string
5
+
6
+ attr_accessor :ai, :ci, :option
7
+
8
+ def initialize left, option=nil, ai=false, ci=false
9
+ @ai = ai
10
+ @ci = ci
11
+ @option = option
12
+ tab = [convert_to_node(left)]
13
+ return super(tab)
14
+ end
15
+
16
16
  end
17
17
  end
18
18
  end
@@ -32,6 +32,14 @@ module ArelExtensions
32
32
  @expressions[1]
33
33
  end
34
34
 
35
+ def ==(other)
36
+ Arel::Nodes::Equality.new self, Arel::Nodes.build_quoted(other, self)
37
+ end
38
+
39
+ def !=(other)
40
+ Arel::Nodes::NotEqual.new self, Arel::Nodes.build_quoted(other, self)
41
+ end
42
+
35
43
  def type_of_attribute(att)
36
44
  case att
37
45
  when Arel::Attributes::Attribute
@@ -1,13 +1,15 @@
1
+ require 'arel_extensions/boolean_functions'
2
+
1
3
  module ArelExtensions
2
4
  module Nodes
3
- class IsNull < Function
4
- RETURN_TYPE = :boolean
5
-
5
+ class IsNull < Arel::Nodes::Unary
6
+ include ArelExtensions::BooleanFunctions
7
+ RETURN_TYPE = :boolean
8
+ end
9
+
10
+ class IsNotNull < Arel::Nodes::Unary
11
+ include ArelExtensions::BooleanFunctions
12
+ RETURN_TYPE = :boolean
6
13
  end
7
-
8
- class IsNotNull < Function
9
- RETURN_TYPE = :boolean
10
-
11
- end
12
14
  end
13
15
  end
@@ -1,11 +1,11 @@
1
1
  module ArelExtensions
2
2
  module Nodes
3
3
  class LevenshteinDistance < Function
4
- RETURN_TYPE = :number
5
-
6
- def initialize expr
7
- super [convert_to_node(expr.first), Arel::Nodes.build_quoted(expr[1])]
8
- end
4
+ RETURN_TYPE = :number
5
+
6
+ def initialize expr
7
+ super [convert_to_node(expr.first), Arel::Nodes.build_quoted(expr[1])]
8
+ end
9
9
  end
10
10
  end
11
11
  end
@@ -1,14 +1,14 @@
1
1
  module ArelExtensions
2
2
  module Nodes
3
3
  class Locate < Function
4
- RETURN_TYPE = :integer
4
+ RETURN_TYPE = :integer
5
5
 
6
- def initialize expr
7
- tab = expr.map do |arg|
8
- convert_to_node(arg)
9
- end
10
- return super(tab)
11
- end
6
+ def initialize expr
7
+ tab = expr.map do |arg|
8
+ convert_to_node(arg)
9
+ end
10
+ return super(tab)
11
+ end
12
12
 
13
13
  end
14
14
  end
@@ -7,7 +7,7 @@ module ArelExtensions
7
7
  end
8
8
 
9
9
  def union_all(other)
10
- return ArelExtensions::Nodes::UnionAll.new(self,other)
10
+ return ArelExtensions::Nodes::UnionAll.new(self,other)
11
11
  end
12
12
 
13
13
  def as other
@@ -2,25 +2,25 @@ require 'arel_extensions/nodes/union'
2
2
  require 'arel_extensions/nodes/union_all'
3
3
 
4
4
  module ArelExtensions
5
- module SetFunctions
5
+ module SetFunctions
6
6
 
7
- def +(other)
8
- ArelExtensions::Nodes::Union.new(self,other)
9
- end
7
+ def +(other)
8
+ ArelExtensions::Nodes::Union.new(self,other)
9
+ end
10
10
 
11
- def union(other)
12
- ArelExtensions::Nodes::Union.new(self,other)
13
- end
11
+ def union(other)
12
+ ArelExtensions::Nodes::Union.new(self,other)
13
+ end
14
14
 
15
- def union_all(other)
16
- ArelExtensions::Nodes::UnionAll.new(self,other)
17
- end
15
+ def union_all(other)
16
+ ArelExtensions::Nodes::UnionAll.new(self,other)
17
+ end
18
18
 
19
- def uniq
20
- self
21
- end
19
+ def uniq
20
+ self
21
+ end
22
22
 
23
- end
23
+ end
24
24
  end
25
25
 
26
26
  Arel::Nodes::Union.class_eval do
@@ -1,3 +1,3 @@
1
1
  module ArelExtensions
2
- VERSION = "1.2.2".freeze
2
+ VERSION = "1.2.3".freeze
3
3
  end
@@ -51,14 +51,14 @@ module ArelExtensions
51
51
 
52
52
  def visit_ArelExtensions_Nodes_IsNull o, collector
53
53
  collector << "("
54
- collector = visit o.left, collector
54
+ collector = visit o.expr, collector
55
55
  collector << " IS NULL)"
56
56
  collector
57
57
  end
58
58
 
59
59
  def visit_ArelExtensions_Nodes_IsNotNull o, collector
60
60
  collector << "("
61
- collector = visit o.left, collector
61
+ collector = visit o.expr, collector
62
62
  collector << " IS NOT NULL)"
63
63
  collector
64
64
  end
@@ -234,11 +234,11 @@ module ArelExtensions
234
234
  end
235
235
 
236
236
  def visit_ArelExtensions_Nodes_Blank o, collector
237
- visit o.left.coalesce('').trim.length.eq(0), collector
237
+ visit o.expr.coalesce('').trim.length.eq(0), collector
238
238
  end
239
239
 
240
240
  def visit_ArelExtensions_Nodes_NotBlank o, collector
241
- visit o.left.coalesce('').trim.length.gt(0), collector
241
+ visit o.expr.coalesce('').trim.length.gt(0), collector
242
242
  end
243
243
 
244
244
  def visit_ArelExtensions_Nodes_Format o, collector
@@ -284,22 +284,14 @@ module ArelExtensions
284
284
 
285
285
  def visit_ArelExtensions_Nodes_IsNull o, collector
286
286
  collector << "ISNULL("
287
- collector = visit o.left, collector
288
- if o.right
289
- collector << Arel::Visitors::MySQL::COMMA
290
- collector = visit o.right, collector
291
- end
287
+ collector = visit o.expr, collector
292
288
  collector << ")"
293
289
  collector
294
290
  end
295
291
 
296
292
  def visit_ArelExtensions_Nodes_IsNotNull o, collector
297
293
  collector << "NOT ISNULL("
298
- collector = visit o.left, collector
299
- if o.right
300
- collector << Arel::Visitors::MySQL::COMMA
301
- collector = visit o.right, collector
302
- end
294
+ collector = visit o.expr, collector
303
295
  collector << ")"
304
296
  collector
305
297
  end
@@ -154,8 +154,8 @@ module ArelExtensions
154
154
  o.expressions.each_with_index { |arg, i|
155
155
  collector << Arel::Visitors::Oracle::COMMA unless i == 0
156
156
  if i > 0 && o.left_node_type == :text
157
- if arg == ''
158
- collector << 'empty_clob()'
157
+ if arg == '' || (arg.is_a?(Arel::Nodes::Quoted) && (arg.expr == ''))
158
+ collector << "NULL"
159
159
  else
160
160
  collector << 'TO_CLOB('
161
161
  collector = visit arg, collector
@@ -257,9 +257,15 @@ module ArelExtensions
257
257
  collector << ")"
258
258
  return collector
259
259
  when :time
260
- collector << "TO_DATE(TO_CHAR("
261
- collector = visit o.left, collector
262
- collector << ",'HH24:MI:SS'),'HH24:MI:SS')"
260
+ if (o.left.respond_to?(:return_type) && o.left.return_type == :string) || o.left.is_a?(Arel::Nodes::Quoted)
261
+ collector << "TO_DATE("
262
+ collector = visit o.left, collector
263
+ collector << ",'HH24:MI:SS')"
264
+ else
265
+ collector << "TO_DATE(TO_CHAR("
266
+ collector = visit o.left, collector
267
+ collector << ",'HH24:MI:SS'),'HH24:MI:SS')"
268
+ end
263
269
  return collector
264
270
  when :number, :decimal
265
271
  collector << "TO_NUMBER("
@@ -269,9 +275,15 @@ module ArelExtensions
269
275
  when :datetime
270
276
  as_attr = Arel::Nodes::SqlLiteral.new('timestamp')
271
277
  when :date
272
- collector << "TO_DATE(TO_CHAR("
273
- collector = visit o.left, collector
274
- collector << ",'YYYY-MM-DD'),'YYYY-MM-DD')"
278
+ if (o.left.respond_to?(:return_type) && o.left.return_type == :string) || o.left.is_a?(Arel::Nodes::Quoted)
279
+ collector << "TO_DATE("
280
+ collector = visit o.left, collector
281
+ collector << ",'YYYY-MM-DD')"
282
+ else
283
+ collector << "TO_DATE(TO_CHAR("
284
+ collector = visit o.left, collector
285
+ collector << ",'YYYY-MM-DD'),'YYYY-MM-DD')"
286
+ end
275
287
  return collector
276
288
  when :binary
277
289
  as_attr = Arel::Nodes::SqlLiteral.new('binary')
@@ -294,13 +306,13 @@ module ArelExtensions
294
306
  end
295
307
 
296
308
  def visit_ArelExtensions_Nodes_IsNull o, collector
297
- collector = visit o.left, collector
309
+ collector = visit o.expr, collector
298
310
  collector << ' IS NULL'
299
311
  collector
300
312
  end
301
313
 
302
314
  def visit_ArelExtensions_Nodes_IsNotNull o, collector
303
- collector = visit o.left, collector
315
+ collector = visit o.expr, collector
304
316
  collector << ' IS NOT NULL'
305
317
  collector
306
318
  end
@@ -407,11 +419,11 @@ module ArelExtensions
407
419
  end
408
420
 
409
421
  def visit_ArelExtensions_Nodes_Blank o, collector
410
- visit o.left.trim.length.coalesce(0).eq(0), collector
422
+ visit o.expr.trim.length.coalesce(0).eq(0), collector
411
423
  end
412
424
 
413
425
  def visit_ArelExtensions_Nodes_NotBlank o, collector
414
- visit o.left.trim.length.coalesce(0).gt(0), collector
426
+ visit o.expr.trim.length.coalesce(0).gt(0), collector
415
427
  end
416
428
 
417
429
  def visit_ArelExtensions_Nodes_DateAdd o, collector
@@ -571,7 +583,7 @@ module ArelExtensions
571
583
  else
572
584
  collector = visit o.left, collector
573
585
  end
574
- quote = o.right.to_s =~ /[^a-zA-Z_]/ ? '"' : ''
586
+ quote = o.right.to_s =~ /(\A["].*["]\z)|\A[a-zA-Z_]*\z/ ? '' : '"'
575
587
  collector << " AS #{quote}"
576
588
  collector = visit o.right, collector
577
589
  collector << "#{quote}"
@@ -278,13 +278,13 @@ module ArelExtensions
278
278
  end
279
279
 
280
280
  def visit_ArelExtensions_Nodes_IsNull o, collector
281
- collector = visit o.left, collector
281
+ collector = visit o.expr, collector
282
282
  collector << ' IS NULL'
283
283
  collector
284
284
  end
285
285
 
286
286
  def visit_ArelExtensions_Nodes_IsNotNull o, collector
287
- collector = visit o.left, collector
287
+ collector = visit o.expr, collector
288
288
  collector << ' IS NOT NULL'
289
289
  collector
290
290
  end
@@ -150,13 +150,13 @@ module ArelExtensions
150
150
  end
151
151
 
152
152
  def visit_ArelExtensions_Nodes_IsNull o, collector
153
- collector = visit o.left, collector
153
+ collector = visit o.expr, collector
154
154
  collector << ' IS NULL'
155
155
  collector
156
156
  end
157
157
 
158
158
  def visit_ArelExtensions_Nodes_IsNotNull o, collector
159
- collector = visit o.left, collector
159
+ collector = visit o.expr, collector
160
160
  collector << ' IS NOT NULL'
161
161
  collector
162
162
  end
@@ -218,7 +218,7 @@ module ArelExtensions
218
218
  def visit_ArelExtensions_Nodes_Blank o, collector
219
219
  #visit o.left.coalesce('').trim.length.eq(0), collector
220
220
  collector << 'LENGTH(TRIM(COALESCE('
221
- collector = visit o.left, collector
221
+ collector = visit o.expr, collector
222
222
  collector << Arel::Visitors::ToSql::COMMA
223
223
  collector = visit Arel::Nodes.build_quoted(''), collector
224
224
  collector << "))) = 0"
@@ -229,7 +229,7 @@ module ArelExtensions
229
229
  def visit_ArelExtensions_Nodes_NotBlank o, collector
230
230
  #visit o.left.coalesce('').trim.length.gt(0), collector
231
231
  collector << 'LENGTH(TRIM(COALESCE('
232
- collector = visit o.left, collector
232
+ collector = visit o.expr, collector
233
233
  collector << Arel::Visitors::ToSql::COMMA
234
234
  collector = visit Arel::Nodes.build_quoted(''), collector
235
235
  collector << "))) > 0"
@@ -367,14 +367,14 @@ module ArelExtensions
367
367
 
368
368
  def visit_ArelExtensions_Nodes_IsNull o, collector
369
369
  collector << "ISNULL("
370
- collector = visit o.left, collector
370
+ collector = visit o.expr, collector
371
371
  collector << ")"
372
372
  collector
373
373
  end
374
374
 
375
375
  def visit_ArelExtensions_Nodes_IsNotNull o, collector
376
376
  collector << "NOT ISNULL("
377
- collector = visit o.left, collector
377
+ collector = visit o.expr, collector
378
378
  collector << ")"
379
379
  collector
380
380
  end
@@ -32,6 +32,7 @@ module ArelExtensions
32
32
  t.column :created_at, :date
33
33
  t.column :updated_at, :datetime
34
34
  t.column :duration, :time
35
+ t.column :other, :string
35
36
  t.column :score, :decimal, :precision => 20, :scale => 10
36
37
  end
37
38
  @cnx.drop_table(:product_tests) rescue nil
@@ -63,7 +64,7 @@ module ArelExtensions
63
64
  @myung = User.where(:id => u.id)
64
65
  u = User.create :age => 25, :name => "Laure", :created_at => d, :score => 20.16, :duration => Time.utc(2001, 1, 1, 12, 42, 21),:updated_at => Time.utc(2014, 3, 3, 12, 42, 0)
65
66
  @laure = User.where(:id => u.id)
66
- u = User.create :age => nil, :name => "Test", :created_at => d, :score => 1.62
67
+ u = User.create :age => nil, :name => "Test", :created_at => d, :score => 1.62, :other => 'toto'
67
68
  @test = User.where(:id => u.id)
68
69
  u = User.create :age => -42, :name => "Negatif", :comments => '1,22,3,42,2', :created_at => d, :updated_at => d.to_time, :score => 0.17
69
70
  @neg = User.where(:id => u.id)
@@ -77,6 +78,7 @@ module ArelExtensions
77
78
  @comments = User.arel_table[:comments]
78
79
  @duration = User.arel_table[:duration]
79
80
  @price = Product.arel_table[:price]
81
+ @other = User.arel_table[:other]
80
82
  @not_in_table = User.arel_table[:not_in_table]
81
83
 
82
84
  @ut = User.arel_table
@@ -347,12 +349,16 @@ module ArelExtensions
347
349
  def test_coalesce
348
350
  assert_equal 'Camille concat', t(@camille, @name.coalesce(nil, "default") + ' concat')
349
351
 
352
+ assert_equal 'toto', t(@test, @other.coalesce(""))
353
+
350
354
  assert_equal ' ', t(@myung, @comments.coalesce("Myung").coalesce('ignored'))
351
355
  assert_equal 'Laure', t(@laure, @comments.coalesce("Laure"))
352
356
  if @env_db == 'oracle'
353
357
  assert_nil t(@laure, @comments.coalesce(""))
358
+ assert_nil t(@camille, @other.coalesce(""))
354
359
  else
355
360
  assert_equal('', t(@laure, @comments.coalesce("")))
361
+ assert_equal '', t(@camille, @other.coalesce(""))
356
362
  end
357
363
  assert_equal 100, t(@test, @age.coalesce(100))
358
364
  assert_equal "Camille", t(@camille, @name.coalesce(nil, "default"))
@@ -470,20 +476,26 @@ module ArelExtensions
470
476
  assert_equal 21, t(@laure,@score.cast(:string).cast(:int)+1)
471
477
 
472
478
  assert_equal String, t(@lucas,@updated_at.cast(:string)).class
473
- assert_equal Date, t(@lucas,@updated_at.cast(:date)).class unless @env_db == 'oracle'
479
+ assert_equal Date, t(@lucas,@updated_at.cast(:date)).class unless @env_db == 'oracle' # DateTime
474
480
  assert_equal Time, t(@lucas,@updated_at.cast(:string).cast(:datetime)).class
475
481
  assert_equal Time, t(@lucas,@updated_at.cast(:time)).class
476
482
 
477
483
  assert_equal "2014-03-03 12:42:00", t(@lucas,@updated_at.cast(:string)) unless @env_db == 'mssql' #locale dependent
484
+ assert_equal Date.parse("2014-03-03"), t(@lucas,Arel::Nodes.build_quoted('2014-03-03').cast(:date))
485
+ assert_equal Date.parse("5014-03-03"), t(@lucas,(@age.cast(:string) + '014-03-03').cast(:date))
478
486
  assert_equal Time.parse("2014-03-03 12:42:00 UTC"), t(@lucas,@updated_at.cast(:string).cast(:datetime))
479
487
  assert_equal Date.parse("2014-03-03"), t(@lucas,@updated_at.cast(:date))
480
- assert_equal "12:42:00", t(@lucas,@updated_at.cast(:time).cast(:string)).split('.').first unless @env_db == 'oracle'
488
+ assert_equal "12:42:00", t(@lucas,@updated_at.cast(:time).cast(:string)).split('.').first unless @env_db == 'oracle' #DateTime
481
489
  end
482
490
  end
483
491
 
484
492
  def test_is_null
485
493
  #puts User.where(@age.is_null).select(@name).to_sql
486
- assert_equal "Test", User.where(@age.is_null).select(@name).first.name
494
+ #puts @age.is_null
495
+ #puts @age.is_null.inspect
496
+ #puts @age.is_null.to_sql
497
+ #puts @age=='34'
498
+ assert_equal "Test", User.select(@name).where(@age.is_null.to_sql).first.name
487
499
  end
488
500
 
489
501
  def test_math_plus
@@ -778,6 +790,7 @@ module ArelExtensions
778
790
  assert_equal 'Arthur', @arthur.select(@name.as('Na Me')).first.attributes["Na Me"]
779
791
  assert_equal 'ArthurArthur', @arthur.select((@name+@name).as('Na-Me')).first.attributes["Na-Me"]
780
792
  end
793
+
781
794
  end
782
795
  end
783
796
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arel_extensions
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 1.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yann Azoury
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2019-05-03 00:00:00.000000000 Z
13
+ date: 2019-07-24 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: arel