arel_extensions 1.2.5 → 1.2.15

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.
Files changed (76) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +7 -4
  3. data/.travis.yml +54 -86
  4. data/README.md +7 -2
  5. data/Rakefile +38 -27
  6. data/arel_extensions.gemspec +1 -1
  7. data/functions.html +2 -2
  8. data/gemfiles/rails4.gemfile +1 -1
  9. data/gemfiles/rails6.gemfile +30 -0
  10. data/gemspec_v2/arel_extensions-v2.gemspec +28 -0
  11. data/generate_gems.sh +14 -0
  12. data/lib/arel_extensions.rb +49 -21
  13. data/lib/arel_extensions/attributes.rb +0 -1
  14. data/lib/arel_extensions/boolean_functions.rb +38 -13
  15. data/lib/arel_extensions/common_sql_functions.rb +5 -4
  16. data/lib/arel_extensions/insert_manager.rb +26 -24
  17. data/lib/arel_extensions/math.rb +3 -3
  18. data/lib/arel_extensions/math_functions.rb +4 -4
  19. data/lib/arel_extensions/nodes/abs.rb +0 -0
  20. data/lib/arel_extensions/nodes/case.rb +8 -4
  21. data/lib/arel_extensions/nodes/ceil.rb +0 -0
  22. data/lib/arel_extensions/nodes/coalesce.rb +0 -0
  23. data/lib/arel_extensions/nodes/collate.rb +1 -1
  24. data/lib/arel_extensions/nodes/concat.rb +0 -0
  25. data/lib/arel_extensions/nodes/date_diff.rb +1 -3
  26. data/lib/arel_extensions/nodes/duration.rb +0 -2
  27. data/lib/arel_extensions/nodes/find_in_set.rb +0 -0
  28. data/lib/arel_extensions/nodes/floor.rb +0 -0
  29. data/lib/arel_extensions/nodes/formatted_number.rb +20 -20
  30. data/lib/arel_extensions/nodes/function.rb +0 -0
  31. data/lib/arel_extensions/nodes/is_null.rb +0 -0
  32. data/lib/arel_extensions/nodes/json.rb +43 -30
  33. data/lib/arel_extensions/nodes/length.rb +0 -0
  34. data/lib/arel_extensions/nodes/locate.rb +0 -0
  35. data/lib/arel_extensions/nodes/power.rb +5 -4
  36. data/lib/arel_extensions/nodes/rand.rb +0 -0
  37. data/lib/arel_extensions/nodes/replace.rb +23 -5
  38. data/lib/arel_extensions/nodes/round.rb +5 -5
  39. data/lib/arel_extensions/nodes/soundex.rb +14 -13
  40. data/lib/arel_extensions/nodes/substring.rb +8 -15
  41. data/lib/arel_extensions/nodes/trim.rb +1 -1
  42. data/lib/arel_extensions/nodes/union.rb +0 -1
  43. data/lib/arel_extensions/nodes/union_all.rb +0 -1
  44. data/lib/arel_extensions/nodes/wday.rb +0 -0
  45. data/lib/arel_extensions/predications.rb +35 -33
  46. data/lib/arel_extensions/set_functions.rb +2 -2
  47. data/lib/arel_extensions/string_functions.rb +25 -6
  48. data/lib/arel_extensions/tasks.rb +5 -5
  49. data/lib/arel_extensions/version.rb +1 -1
  50. data/lib/arel_extensions/visitors.rb +1 -1
  51. data/lib/arel_extensions/visitors/ibm_db.rb +1 -1
  52. data/lib/arel_extensions/visitors/mssql.rb +13 -12
  53. data/lib/arel_extensions/visitors/mysql.rb +67 -37
  54. data/lib/arel_extensions/visitors/oracle.rb +14 -14
  55. data/lib/arel_extensions/visitors/oracle12.rb +1 -1
  56. data/lib/arel_extensions/visitors/postgresql.rb +46 -28
  57. data/lib/arel_extensions/visitors/sqlite.rb +52 -44
  58. data/lib/arel_extensions/visitors/to_sql.rb +73 -59
  59. data/test/arelx_test_helper.rb +28 -0
  60. data/test/support/fake_record.rb +4 -0
  61. data/test/test_comparators.rb +8 -7
  62. data/test/visitors/test_bulk_insert_oracle.rb +8 -7
  63. data/test/visitors/test_bulk_insert_sqlite.rb +8 -7
  64. data/test/visitors/test_bulk_insert_to_sql.rb +3 -3
  65. data/test/visitors/test_oracle.rb +41 -41
  66. data/test/visitors/test_to_sql.rb +367 -199
  67. data/test/with_ar/all_agnostic_test.rb +63 -41
  68. data/test/with_ar/insert_agnostic_test.rb +1 -1
  69. data/test/with_ar/test_bulk_sqlite.rb +5 -4
  70. data/test/with_ar/test_math_sqlite.rb +2 -2
  71. data/test/with_ar/test_string_mysql.rb +2 -4
  72. data/test/with_ar/test_string_sqlite.rb +2 -6
  73. data/version_v1.rb +3 -0
  74. data/version_v2.rb +3 -0
  75. metadata +10 -5
  76. data/test/helper.rb +0 -18
@@ -13,12 +13,10 @@ module ArelExtensions
13
13
  super(tab, aliaz)
14
14
  end
15
15
 
16
-
17
16
  def left
18
17
  @expressions.first
19
18
  end
20
19
 
21
-
22
20
  def right
23
21
  @expressions[1]
24
22
  end
File without changes
File without changes
@@ -1,25 +1,25 @@
1
1
  module ArelExtensions
2
- module Nodes
3
- class FormattedNumber < Function
4
- RETURN_TYPE = :string
2
+ module Nodes
3
+ class FormattedNumber < Function
4
+ RETURN_TYPE = :string
5
5
 
6
- attr_accessor :locale, :prefix, :suffix, :flags, :scientific_notation, :width,:precision, :type, :original_string
6
+ attr_accessor :locale, :prefix, :suffix, :flags, :scientific_notation, :width,:precision, :type, :original_string
7
7
 
8
- def initialize expr
9
- # expr[1] = {:locale => 'fr_FR', :type => "e"/"f"/"d", :prefix => "$ ", :suffix => " %", :flags => " +-#0", :width => 5, :precision => 6}
10
- col = expr.first
11
- @locale = expr[1][:locale]
12
- @prefix = expr[1][:prefix]
13
- @suffix = expr[1][:suffix]
14
- @width = expr[1][:width]
15
- @precision = expr[1][:precision]
16
- @type = expr[1][:type]
17
- @flags = expr[1][:flags]
18
- @scientific_notation = /[eE]/.match(expr[1][:type]) || false
19
- @original_string = expr[1][:original_string]
20
- super [col]
21
- end
8
+ def initialize expr
9
+ # expr[1] = {:locale => 'fr_FR', :type => "e"/"f"/"d", :prefix => "$ ", :suffix => " %", :flags => " +-#0", :width => 5, :precision => 6}
10
+ col = expr.first
11
+ @locale = expr[1][:locale]
12
+ @prefix = expr[1][:prefix]
13
+ @suffix = expr[1][:suffix]
14
+ @width = expr[1][:width]
15
+ @precision = expr[1][:precision]
16
+ @type = expr[1][:type]
17
+ @flags = expr[1][:flags]
18
+ @scientific_notation = /[eE]/.match(expr[1][:type]) || false
19
+ @original_string = expr[1][:original_string]
20
+ super [col]
21
+ end
22
22
 
23
- end
24
- end
23
+ end
24
+ end
25
25
  end
File without changes
File without changes
@@ -18,7 +18,7 @@ module ArelExtensions
18
18
  JsonSet.new(self,key,value)
19
19
  end
20
20
 
21
- def group as_array = true, orders= nil
21
+ def group as_array = true, orders = nil
22
22
  JsonGroup.new(self,as_array, orders)
23
23
  end
24
24
 
@@ -31,34 +31,51 @@ module ArelExtensions
31
31
  class Json < JsonNode
32
32
 
33
33
  def initialize *expr
34
- if expr.length == 1
35
- case expr.first
36
- when JsonNode
37
- @dict = expr.first.dict
38
- when Array
39
- @dict = expr.first.map{|e|
40
- (e.is_a?(Array) || e.is_a?(Hash)) ? Json.new(e) : convert_to_node(e)
41
- }
42
- when Hash
43
- @dict = expr.first.inject({}){|acc,v|
44
- acc[convert_to_node(v[0])] = (v[1].is_a?(Array) || v[1].is_a?(Hash)) ? Json.new(v[1]) : convert_to_node(v[1])
45
- acc
46
- }
47
- when String, Numeric, TrueClass, FalseClass
48
- @dict = convert_to_node(expr.first)
49
- when NilClass
50
- @dict = Arel.sql('null')
34
+ @dict =
35
+ if expr.length == 1
36
+ convert_to_json_node(expr.first)
51
37
  else
52
- if expr.first.is_a?(String) || (expr.first.is_a?(Arel::Attributes::Attribute) && type_of_attribute(expr.first) == :string) || (expr.first.return_type == :string)
53
- @dict = convert_to_node(expr.first)
54
- else
55
- @dict = [convert_to_node(expr.first)]
56
- end
38
+ expr.map{|e| convert_to_json_node(e) }
57
39
  end
40
+ super
41
+ end
42
+
43
+ def convert_to_json_node(n)
44
+ case n
45
+ when JsonNode
46
+ n.dict
47
+ when Array
48
+ n.map{|e|
49
+ (e.is_a?(Array) || e.is_a?(Hash)) ? Json.new(e) : convert_to_json_node(e)
50
+ }
51
+ when Hash
52
+ n.reduce({}){|acc,v|
53
+ acc[convert_to_json_node(v[0])] = (v[1].is_a?(Array) || v[1].is_a?(Hash)) ? Json.new(v[1]) : convert_to_json_node(v[1])
54
+ acc
55
+ }
56
+ when String, Numeric, TrueClass, FalseClass
57
+ convert_to_node(n)
58
+ when Date
59
+ convert_to_node(n.strftime("%Y-%m-%d"))
60
+ when DateTime, Time
61
+ convert_to_node(n.strftime("%Y-%m-%dT%H:%M:%S.%L%:z"))
62
+ when NilClass
63
+ Arel.sql('null')
58
64
  else
59
- @dict = expr.map{|e| (e.is_a?(Array) || e.is_a?(Hash)) ? Json.new(e) : convert_to_node(e) }
65
+ convert_to_node(n)
66
+ end
67
+ end
68
+
69
+ def type_of_node(v)
70
+ if v.is_a?(Arel::Attributes::Attribute)
71
+ self.type_of_attribute(v)
72
+ elsif v.respond_to?(:return_type)
73
+ v.return_type
74
+ elsif v.nil?
75
+ :nil
76
+ else
77
+ :string
60
78
  end
61
- super
62
79
  end
63
80
 
64
81
  end
@@ -73,11 +90,7 @@ module ArelExtensions
73
90
  @dict = as_array ? json : json.dict
74
91
  @as_array = as_array
75
92
  if orders
76
- if orders.is_a?(Array)
77
- @orders = orders
78
- else
79
- @orders = [orders]
80
- end
93
+ @orders = Array(orders)
81
94
  end
82
95
  end
83
96
  end
File without changes
File without changes
@@ -1,11 +1,12 @@
1
1
  module ArelExtensions
2
2
  module Nodes
3
3
  class Power < Function
4
- RETURN_TYPE = :number
4
+ RETURN_TYPE = :number
5
+
6
+ def initialize expr
7
+ super [convert_to_node(expr.first), convert_to_number(expr[1])]
8
+ end
5
9
 
6
- def initialize expr
7
- super [convert_to_node(expr.first), convert_to_number(expr[1])]
8
- end
9
10
  end
10
11
  end
11
12
  end
File without changes
@@ -2,12 +2,13 @@ module ArelExtensions
2
2
  module Nodes
3
3
  class Replace < Function
4
4
  RETURN_TYPE = :string
5
+ attr_accessor :left, :pattern, :substitute
5
6
 
6
- def initialize expr
7
- tab = expr.map { |arg|
8
- convert_to_node(arg)
9
- }
10
- return super(tab)
7
+ def initialize left, pattern, substitute
8
+ @left = convert_to_node(left)
9
+ @pattern = convert_to_node(pattern)
10
+ @substitute = convert_to_node(substitute)
11
+ super([@left,@pattern,@substitute])
11
12
  end
12
13
 
13
14
  def +(other)
@@ -15,5 +16,22 @@ module ArelExtensions
15
16
  end
16
17
 
17
18
  end
19
+
20
+ class RegexpReplace < Function
21
+ RETURN_TYPE = :string
22
+ attr_accessor :left, :pattern, :substitute
23
+
24
+ def initialize left, pattern, substitute
25
+ @left = convert_to_node(left)
26
+ @pattern = (pattern.is_a?(Regexp) ? pattern : %r[#{pattern}])
27
+ @substitute = convert_to_node(substitute)
28
+ super([@left,@pattern,@substitute])
29
+ end
30
+
31
+ def +(other)
32
+ return ArelExtensions::Nodes::Concat.new(self.expressions + [other])
33
+ end
34
+ end
35
+
18
36
  end
19
37
  end
@@ -4,11 +4,11 @@ module ArelExtensions
4
4
  RETURN_TYPE = :number
5
5
 
6
6
  def initialize expr
7
- if expr && expr.length == 1
8
- super [convert_to_node(expr.first)]
9
- else
10
- super [convert_to_node(expr.first), convert_to_number(expr[1])]
11
- end
7
+ if expr && expr.length == 1
8
+ super [convert_to_node(expr.first)]
9
+ else
10
+ super [convert_to_node(expr.first), convert_to_number(expr[1])]
11
+ end
12
12
  end
13
13
 
14
14
  end
@@ -1,18 +1,19 @@
1
1
  module ArelExtensions
2
- module Nodes
3
- class Soundex < Function
4
- include Arel::Expressions
5
- include ArelExtensions::Comparators
2
+ module Nodes
3
+ class Soundex < Function
4
+ include Arel::Expressions
5
+ include ArelExtensions::Comparators
6
6
 
7
- RETURN_TYPE = :string
7
+ RETURN_TYPE = :string
8
8
 
9
- def ==(other)
10
- Arel::Nodes::Equality.new self, Arel::Nodes.build_quoted(other, self)
11
- end
9
+ def ==(other)
10
+ Arel::Nodes::Equality.new self, Arel::Nodes.build_quoted(other, self)
11
+ end
12
12
 
13
- def !=(other)
14
- Arel::Nodes::NotEqual.new self, Arel::Nodes.build_quoted(other, self)
15
- end
16
- end
17
- end
13
+ def !=(other)
14
+ Arel::Nodes::NotEqual.new self, Arel::Nodes.build_quoted(other, self)
15
+ end
16
+
17
+ end
18
+ end
18
19
  end
@@ -1,22 +1,15 @@
1
1
  module ArelExtensions
2
2
  module Nodes
3
3
  class Substring < Function
4
- RETURN_TYPE = :string
4
+ RETURN_TYPE = :string
5
5
 
6
- def initialize expr
7
- tab = [convert_to_node(expr[0]), convert_to_node(expr[1])]
8
- if expr[2]
9
- tab << convert_to_node(expr[2])
10
- # else
11
- # tab << expr[0].length
12
- end
13
- return super(tab)
14
- end
15
-
16
- # def +(other)
17
- # puts "[Substring] : #{other.inspect} (#{self.expressions.inspect})"
18
- # return ArelExtensions::Nodes::Concat.new(self.expressions + [other])
19
- # end
6
+ def initialize expr
7
+ tab = [convert_to_node(expr[0]), convert_to_node(expr[1])]
8
+ if expr[2]
9
+ tab << convert_to_node(expr[2])
10
+ end
11
+ return super(tab)
12
+ end
20
13
 
21
14
  end
22
15
  end
@@ -17,7 +17,7 @@ module ArelExtensions
17
17
  end
18
18
 
19
19
  class Ltrim < Trim
20
- RETURN_TYPE = :string
20
+ RETURN_TYPE = :string
21
21
  end
22
22
 
23
23
  class Rtrim < Trim
@@ -21,4 +21,3 @@ module ArelExtensions
21
21
 
22
22
  end
23
23
  end
24
-
@@ -17,4 +17,3 @@ module ArelExtensions
17
17
 
18
18
  end
19
19
  end
20
-
File without changes
@@ -1,10 +1,11 @@
1
1
  module ArelExtensions
2
2
  module Predications
3
- def when right, expression=nil
3
+
4
+ def when right, expression = nil
4
5
  ArelExtensions::Nodes::Case.new(self).when(right,expression)
5
6
  end
6
7
 
7
- def matches(other, escape=nil,case_sensitive= nil)
8
+ def matches(other, escape = nil,case_sensitive = nil)
8
9
  if Arel::VERSION.to_i < 7
9
10
  Arel::Nodes::Matches.new(self, Arel::Nodes.build_quoted(other), escape)
10
11
  else
@@ -12,7 +13,7 @@ module ArelExtensions
12
13
  end
13
14
  end
14
15
 
15
- def imatches(other, escape=nil)
16
+ def imatches(other, escape = nil)
16
17
  ArelExtensions::Nodes::IMatches.new(self, other, escape)
17
18
  end
18
19
 
@@ -20,51 +21,53 @@ module ArelExtensions
20
21
  ArelExtensions::Nodes::Cast.new([self,right])
21
22
  end
22
23
 
23
- def in(other) #In should handle nil element in the Array
24
+ def in(*other) #In should handle nil element in the Array
25
+ other = other.first if other.length <= 1
24
26
  case other
25
27
  when Range
26
28
  self.between(other)
29
+ when Arel::Nodes::Grouping
30
+ Arel::Nodes::In.new(self, quoted_node(other))
27
31
  when Enumerable
28
- if other.include?(nil)
29
- other.delete(nil)
30
- case other.length
31
- when 0
32
- self.is_null
33
- when 1
34
- self.is_null.or(self==other[0])
35
- else
36
- self.is_null.or(Arel::Nodes::In.new(self,quoted_array(other)))
37
- end
38
- else
39
- Arel::Nodes::In.new(self,quoted_array(other))
40
- end
32
+ nils, values = other.partition{ |v| v.nil? }
33
+ ranges, values = values.partition{ |v| v.is_a?(Range) || v.is_a?(Arel::SelectManager)}
34
+ # In order of (imagined) decreasing efficiency: nil, values, and then more complex.
35
+ clauses =
36
+ nils.uniq.map { |r| self.in(r) } \
37
+ + (case values.uniq.size
38
+ when 0 then []
39
+ when 1 then [values[0].is_a?(Arel::Nodes::Grouping) ? self.in(values[0]) : self.eq(values[0])]
40
+ else [Arel::Nodes::In.new(self, quoted_array(values))] end) \
41
+ + ranges.uniq.map { |r| self.in(r) }
42
+ clauses.empty? ? Arel.false : clauses.reduce(&:or)
41
43
  when nil
42
44
  self.is_null
43
45
  when Arel::SelectManager
44
46
  Arel::Nodes::In.new(self, other.ast)
45
47
  else
46
- Arel::Nodes::In.new(self,quoted_node(other))
48
+ Arel::Nodes::In.new(self, quoted_node(other))
47
49
  end
48
50
  end
49
51
 
50
- def not_in(other) #In should handle nil element in the Array
52
+ def not_in(*other) #In should handle nil element in the Array
53
+ other = other.first if other.length <= 1
51
54
  case other
52
55
  when Range
53
56
  Arel::Nodes::Not.new(self.between(other))
57
+ when Arel::Nodes::Grouping
58
+ Arel::Nodes::NotIn.new(self, quoted_node(other))
54
59
  when Enumerable
55
- if other.include?(nil)
56
- other.delete(nil)
57
- case other.length
58
- when 0
59
- self.is_not_null
60
- when 1
61
- self.is_not_null.and(self!=other[0])
62
- else
63
- self.is_not_null.and(Arel::Nodes::NotIn.new(self,quoted_array(other)))
64
- end
65
- else
66
- Arel::Nodes::NotIn.new(self,quoted_array(other))
67
- end
60
+ nils, values = other.partition{ |v| v.nil? }
61
+ ranges, values = values.partition{ |v| v.is_a?(Range) || v.is_a?(Arel::SelectManager)}
62
+ # In order of (imagined) decreasing efficiency: nil, values, and then more complex.
63
+ clauses =
64
+ nils.uniq.map { |r| self.not_in(r) } \
65
+ + (case values.uniq.size
66
+ when 0 then []
67
+ when 1 then [values[0].is_a?(Arel::Nodes::Grouping) ? self.not_in(values[0]) : self.not_eq(values[0])]
68
+ else [Arel::Nodes::NotIn.new(self, quoted_array(values))] end) \
69
+ + ranges.uniq.map { |r| self.not_in(r) }
70
+ Arel::Nodes::And.new clauses
68
71
  when nil
69
72
  self.is_not_null
70
73
  when Arel::SelectManager
@@ -94,6 +97,5 @@ module ArelExtensions
94
97
  raise(ArgumentError, "#{object.class} can not be converted to CONCAT arg")
95
98
  end
96
99
  end
97
-
98
100
  end
99
101
  end