arel_extensions 1.2.2 → 1.2.13

Sign up to get free protection for your applications and to get access to all the features.
Files changed (94) hide show
  1. checksums.yaml +5 -5
  2. data/.rubocop.yml +7 -4
  3. data/.travis.yml +59 -91
  4. data/Gemfile +2 -2
  5. data/README.md +17 -12
  6. data/Rakefile +38 -27
  7. data/appveyor.yml +1 -1
  8. data/arel_extensions.gemspec +1 -1
  9. data/functions.html +3 -3
  10. data/gemfiles/rails4.gemfile +1 -1
  11. data/gemfiles/rails6.gemfile +30 -0
  12. data/gemspec_v2/arel_extensions-v2.gemspec +28 -0
  13. data/generate_gems.sh +14 -0
  14. data/init/mssql.sql +4 -4
  15. data/init/mysql.sql +38 -38
  16. data/init/postgresql.sql +21 -21
  17. data/lib/arel_extensions.rb +63 -19
  18. data/lib/arel_extensions/attributes.rb +0 -1
  19. data/lib/arel_extensions/boolean_functions.rb +38 -13
  20. data/lib/arel_extensions/common_sql_functions.rb +5 -4
  21. data/lib/arel_extensions/comparators.rb +4 -2
  22. data/lib/arel_extensions/insert_manager.rb +26 -24
  23. data/lib/arel_extensions/math.rb +3 -3
  24. data/lib/arel_extensions/math_functions.rb +10 -5
  25. data/lib/arel_extensions/nodes.rb +1 -1
  26. data/lib/arel_extensions/nodes/abs.rb +0 -0
  27. data/lib/arel_extensions/nodes/aggregate_function.rb +14 -0
  28. data/lib/arel_extensions/nodes/blank.rb +14 -11
  29. data/lib/arel_extensions/nodes/case.rb +8 -4
  30. data/lib/arel_extensions/nodes/ceil.rb +0 -0
  31. data/lib/arel_extensions/nodes/change_case.rb +2 -2
  32. data/lib/arel_extensions/nodes/coalesce.rb +2 -2
  33. data/lib/arel_extensions/nodes/collate.rb +12 -12
  34. data/lib/arel_extensions/nodes/concat.rb +6 -13
  35. data/lib/arel_extensions/nodes/date_diff.rb +3 -5
  36. data/lib/arel_extensions/nodes/duration.rb +0 -2
  37. data/lib/arel_extensions/nodes/find_in_set.rb +0 -0
  38. data/lib/arel_extensions/nodes/floor.rb +0 -0
  39. data/lib/arel_extensions/nodes/format.rb +8 -8
  40. data/lib/arel_extensions/nodes/formatted_number.rb +23 -23
  41. data/lib/arel_extensions/nodes/function.rb +10 -0
  42. data/lib/arel_extensions/nodes/is_null.rb +10 -8
  43. data/lib/arel_extensions/nodes/json.rb +28 -30
  44. data/lib/arel_extensions/nodes/length.rb +0 -0
  45. data/lib/arel_extensions/nodes/levenshtein_distance.rb +5 -5
  46. data/lib/arel_extensions/nodes/locate.rb +7 -7
  47. data/lib/arel_extensions/nodes/matches.rb +4 -4
  48. data/lib/arel_extensions/nodes/power.rb +6 -5
  49. data/lib/arel_extensions/nodes/rand.rb +0 -0
  50. data/lib/arel_extensions/nodes/repeat.rb +2 -2
  51. data/lib/arel_extensions/nodes/replace.rb +24 -6
  52. data/lib/arel_extensions/nodes/round.rb +5 -5
  53. data/lib/arel_extensions/nodes/soundex.rb +16 -15
  54. data/lib/arel_extensions/nodes/std.rb +19 -21
  55. data/lib/arel_extensions/nodes/substring.rb +8 -15
  56. data/lib/arel_extensions/nodes/sum.rb +7 -0
  57. data/lib/arel_extensions/nodes/trim.rb +3 -3
  58. data/lib/arel_extensions/nodes/union.rb +2 -3
  59. data/lib/arel_extensions/nodes/union_all.rb +1 -2
  60. data/lib/arel_extensions/nodes/wday.rb +0 -0
  61. data/lib/arel_extensions/null_functions.rb +2 -2
  62. data/lib/arel_extensions/predications.rb +35 -33
  63. data/lib/arel_extensions/set_functions.rb +16 -16
  64. data/lib/arel_extensions/string_functions.rb +34 -12
  65. data/lib/arel_extensions/tasks.rb +5 -5
  66. data/lib/arel_extensions/version.rb +1 -1
  67. data/lib/arel_extensions/visitors.rb +1 -1
  68. data/lib/arel_extensions/visitors/ibm_db.rb +1 -1
  69. data/lib/arel_extensions/visitors/mssql.rb +18 -17
  70. data/lib/arel_extensions/visitors/mysql.rb +92 -46
  71. data/lib/arel_extensions/visitors/oracle.rb +40 -28
  72. data/lib/arel_extensions/visitors/oracle12.rb +1 -1
  73. data/lib/arel_extensions/visitors/postgresql.rb +80 -34
  74. data/lib/arel_extensions/visitors/sqlite.rb +54 -46
  75. data/lib/arel_extensions/visitors/to_sql.rb +75 -62
  76. data/test/arelx_test_helper.rb +28 -0
  77. data/test/real_db_test.rb +1 -1
  78. data/test/support/fake_record.rb +4 -0
  79. data/test/test_comparators.rb +9 -8
  80. data/test/visitors/test_bulk_insert_oracle.rb +8 -7
  81. data/test/visitors/test_bulk_insert_sqlite.rb +9 -8
  82. data/test/visitors/test_bulk_insert_to_sql.rb +8 -10
  83. data/test/visitors/test_oracle.rb +41 -40
  84. data/test/visitors/test_to_sql.rb +367 -193
  85. data/test/with_ar/all_agnostic_test.rb +85 -39
  86. data/test/with_ar/insert_agnostic_test.rb +3 -2
  87. data/test/with_ar/test_bulk_sqlite.rb +6 -5
  88. data/test/with_ar/test_math_sqlite.rb +4 -4
  89. data/test/with_ar/test_string_mysql.rb +4 -6
  90. data/test/with_ar/test_string_sqlite.rb +3 -7
  91. data/version_v1.rb +3 -0
  92. data/version_v2.rb +3 -0
  93. metadata +13 -7
  94. 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,15 +1,15 @@
1
1
  module ArelExtensions
2
2
  module Nodes
3
- class Format < Function
3
+ class Format < Function
4
4
  RETURN_TYPE = :string
5
5
 
6
- attr_accessor :col_type, :iso_format
7
- def initialize expr
8
- col = expr.first
9
- @iso_format = expr[1]
10
- @col_type = type_of_attribute(col)
11
- super [col, convert_to_string_node(@iso_format)]
12
- end
6
+ attr_accessor :col_type, :iso_format
7
+ def initialize expr
8
+ col = expr.first
9
+ @iso_format = expr[1]
10
+ @col_type = type_of_attribute(col)
11
+ super [col, convert_to_string_node(@iso_format)]
12
+ end
13
13
  end
14
14
  end
15
15
  end
@@ -1,25 +1,25 @@
1
1
  module ArelExtensions
2
- module Nodes
3
- class FormattedNumber < Function
4
- RETURN_TYPE = :string
5
-
6
- attr_accessor :locale, :prefix, :suffix, :flags, :scientific_notation, :width,:precision, :type, :original_string
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
22
-
23
- end
24
- end
2
+ module Nodes
3
+ class FormattedNumber < Function
4
+ RETURN_TYPE = :string
5
+
6
+ attr_accessor :locale, :prefix, :suffix, :flags, :scientific_notation, :width,:precision, :type, :original_string
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
22
+
23
+ end
24
+ end
25
25
  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
@@ -63,6 +71,8 @@ module ArelExtensions
63
71
  Arel.sql('NULL')
64
72
  when ActiveSupport::Duration
65
73
  Arel.sql(object.to_i)
74
+ when Array
75
+ Arel::Nodes::Grouping.new(object.map{|r| convert_to_node(e)})
66
76
  else
67
77
  raise(ArgumentError, "#{object.class} can not be converted to CONCAT arg")
68
78
  end
@@ -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
@@ -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,33 +31,35 @@ 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')
51
- 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)
34
+ @dict =
35
+ if expr.length == 1
36
+ case exp = expr.first
37
+ when JsonNode
38
+ exp.dict
39
+ when Array
40
+ exp.map{|e|
41
+ (e.is_a?(Array) || e.is_a?(Hash)) ? Json.new(e) : convert_to_node(e)
42
+ }
43
+ when Hash
44
+ exp.reduce({}){|acc,v|
45
+ 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])
46
+ acc
47
+ }
48
+ when String, Numeric, TrueClass, FalseClass
49
+ convert_to_node(exp)
50
+ when NilClass
51
+ Arel.sql('null')
54
52
  else
55
- @dict = [convert_to_node(expr.first)]
53
+ if (exp.is_a?(Arel::Attributes::Attribute) && type_of_attribute(exp) == :string) \
54
+ || (exp.return_type == :string)
55
+ convert_to_node(exp)
56
+ else
57
+ [convert_to_node(exp)]
58
+ end
56
59
  end
60
+ else
61
+ expr.map{|e| (e.is_a?(Array) || e.is_a?(Hash)) ? Json.new(e) : convert_to_node(e) }
57
62
  end
58
- else
59
- @dict = expr.map{|e| (e.is_a?(Array) || e.is_a?(Hash)) ? Json.new(e) : convert_to_node(e) }
60
- end
61
63
  super
62
64
  end
63
65
 
@@ -73,11 +75,7 @@ module ArelExtensions
73
75
  @dict = as_array ? json : json.dict
74
76
  @as_array = as_array
75
77
  if orders
76
- if orders.is_a?(Array)
77
- @orders = orders
78
- else
79
- @orders = [orders]
80
- end
78
+ @orders = Array(orders)
81
79
  end
82
80
  end
83
81
  end
File without changes
@@ -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
@@ -3,7 +3,7 @@ module ArelExtensions
3
3
  class IMatches < Arel::Nodes::Matches
4
4
 
5
5
  attr_accessor :case_sensitive if Arel::VERSION.to_i < 7
6
-
6
+
7
7
  def initialize(left, right, escape = nil)
8
8
  r = Arel::Nodes.build_quoted(right)
9
9
  if Arel::VERSION.to_i < 7 # managed by default in version 7+ (rails 5), so useful for rails 3 & 4
@@ -17,13 +17,13 @@ module ArelExtensions
17
17
 
18
18
  class IDoesNotMatch < IMatches
19
19
  end
20
-
20
+
21
21
  class AiMatches < IMatches
22
22
  end
23
-
23
+
24
24
  class AiIMatches < IMatches
25
25
  end
26
-
26
+
27
27
  class SMatches < IMatches
28
28
  end
29
29
 
@@ -1,11 +1,12 @@
1
1
  module ArelExtensions
2
2
  module Nodes
3
3
  class Power < Function
4
- RETURN_TYPE = :number
5
-
6
- def initialize expr
7
- super [convert_to_node(expr.first), convert_to_number(expr[1])]
8
- end
4
+ RETURN_TYPE = :number
5
+
6
+ def initialize expr
7
+ super [convert_to_node(expr.first), convert_to_number(expr[1])]
8
+ end
9
+
9
10
  end
10
11
  end
11
12
  end
File without changes
@@ -2,7 +2,7 @@ module ArelExtensions
2
2
  module Nodes
3
3
  class Repeat < Function
4
4
  RETURN_TYPE = :string
5
-
5
+
6
6
  def initialize expr
7
7
  tab = expr.map { |arg|
8
8
  convert_to_node(arg)
@@ -11,7 +11,7 @@ module ArelExtensions
11
11
  end
12
12
 
13
13
  def +(other)
14
- return ArelExtensions::Nodes::Concat.new(self.expressions + [other])
14
+ return ArelExtensions::Nodes::Concat.new(self.expressions + [other])
15
15
  end
16
16
 
17
17
  end
@@ -2,18 +2,36 @@ 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)
14
- return ArelExtensions::Nodes::Concat.new(self.expressions + [other])
15
+ return ArelExtensions::Nodes::Concat.new(self.expressions + [other])
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
6
-
7
- RETURN_TYPE = :string
8
-
9
- def ==(other)
10
- Arel::Nodes::Equality.new self, Arel::Nodes.build_quoted(other, self)
11
- end
2
+ module Nodes
3
+ class Soundex < Function
4
+ include Arel::Expressions
5
+ include ArelExtensions::Comparators
12
6
 
13
- def !=(other)
14
- Arel::Nodes::NotEqual.new self, Arel::Nodes.build_quoted(other, self)
15
- end
16
- end
17
- end
7
+ RETURN_TYPE = :string
8
+
9
+ def ==(other)
10
+ Arel::Nodes::Equality.new self, Arel::Nodes.build_quoted(other, self)
11
+ end
12
+
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,26 +1,24 @@
1
1
  module ArelExtensions
2
2
  module Nodes
3
- class Std < Function
4
- RETURN_TYPE = :number
5
-
6
- attr_accessor :unbiased_estimator
7
- def initialize expr
8
- col = expr.first
9
- @unbiased_estimator = expr[1]
10
- super [col]
11
- end
3
+ class Std < AggregateFunction
4
+ RETURN_TYPE = :number
5
+ attr_accessor :unbiased_estimator
6
+
7
+ def initialize node, opts = {}
8
+ @unbiased_estimator = opts[:unbiased] ? true : false
9
+ super node, opts
10
+ end
12
11
  end
13
-
14
- class Variance < Function
15
- RETURN_TYPE = :number
16
-
17
- attr_accessor :unbiased_estimator
18
- def initialize expr
19
- col = expr.first
20
- @unbiased_estimator = expr[1]
21
- super [col]
22
- end
23
- end
24
-
12
+
13
+ class Variance < AggregateFunction
14
+ RETURN_TYPE = :number
15
+ attr_accessor :unbiased_estimator
16
+
17
+ def initialize node, opts = {}
18
+ @unbiased_estimator = opts[:unbiased] ? true : false
19
+ super node, opts
20
+ end
21
+ end
22
+
25
23
  end
26
24
  end