arel_extensions 1.2.14 → 1.2.19

Sign up to get free protection for your applications and to get access to all the features.
Files changed (99) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +102 -0
  3. data/.travis.yml +2 -0
  4. data/Gemfile +10 -10
  5. data/Rakefile +4 -4
  6. data/arel_extensions.gemspec +1 -1
  7. data/gemfiles/rails3.gemfile +9 -9
  8. data/gemfiles/rails4.gemfile +13 -13
  9. data/gemfiles/rails5_0.gemfile +13 -13
  10. data/gemfiles/rails5_1_4.gemfile +13 -13
  11. data/gemfiles/rails5_2.gemfile +13 -13
  12. data/gemfiles/rails6.gemfile +13 -13
  13. data/gemfiles/rails6_1.gemfile +30 -0
  14. data/gemspecs/arel_extensions-v1.gemspec +28 -0
  15. data/{gemspec_v2 → gemspecs}/arel_extensions-v2.gemspec +0 -0
  16. data/generate_gems.sh +4 -3
  17. data/lib/arel_extensions.rb +6 -4
  18. data/lib/arel_extensions/attributes.rb +0 -0
  19. data/lib/arel_extensions/boolean_functions.rb +21 -5
  20. data/lib/arel_extensions/common_sql_functions.rb +2 -4
  21. data/lib/arel_extensions/comparators.rb +11 -14
  22. data/lib/arel_extensions/date_duration.rb +4 -5
  23. data/lib/arel_extensions/insert_manager.rb +16 -17
  24. data/lib/arel_extensions/math.rb +8 -9
  25. data/lib/arel_extensions/math_functions.rb +18 -20
  26. data/lib/arel_extensions/nodes/abs.rb +0 -1
  27. data/lib/arel_extensions/nodes/aggregate_function.rb +0 -1
  28. data/lib/arel_extensions/nodes/blank.rb +0 -1
  29. data/lib/arel_extensions/nodes/case.rb +3 -4
  30. data/lib/arel_extensions/nodes/cast.rb +4 -2
  31. data/lib/arel_extensions/nodes/ceil.rb +1 -1
  32. data/lib/arel_extensions/nodes/change_case.rb +0 -0
  33. data/lib/arel_extensions/nodes/coalesce.rb +0 -1
  34. data/lib/arel_extensions/nodes/collate.rb +0 -1
  35. data/lib/arel_extensions/nodes/concat.rb +2 -4
  36. data/lib/arel_extensions/nodes/date_diff.rb +7 -8
  37. data/lib/arel_extensions/nodes/duration.rb +0 -1
  38. data/lib/arel_extensions/nodes/find_in_set.rb +0 -1
  39. data/lib/arel_extensions/nodes/floor.rb +1 -1
  40. data/lib/arel_extensions/nodes/format.rb +27 -1
  41. data/lib/arel_extensions/nodes/formatted_number.rb +0 -1
  42. data/lib/arel_extensions/nodes/function.rb +18 -15
  43. data/lib/arel_extensions/nodes/is_null.rb +0 -0
  44. data/lib/arel_extensions/nodes/json.rb +39 -30
  45. data/lib/arel_extensions/nodes/length.rb +0 -1
  46. data/lib/arel_extensions/nodes/levenshtein_distance.rb +0 -0
  47. data/lib/arel_extensions/nodes/locate.rb +0 -1
  48. data/lib/arel_extensions/nodes/log10.rb +1 -2
  49. data/lib/arel_extensions/nodes/matches.rb +0 -2
  50. data/lib/arel_extensions/nodes/md5.rb +0 -1
  51. data/lib/arel_extensions/nodes/power.rb +0 -1
  52. data/lib/arel_extensions/nodes/rand.rb +0 -1
  53. data/lib/arel_extensions/nodes/repeat.rb +0 -2
  54. data/lib/arel_extensions/nodes/replace.rb +0 -2
  55. data/lib/arel_extensions/nodes/round.rb +0 -1
  56. data/lib/arel_extensions/nodes/soundex.rb +0 -1
  57. data/lib/arel_extensions/nodes/std.rb +4 -5
  58. data/lib/arel_extensions/nodes/substring.rb +0 -1
  59. data/lib/arel_extensions/nodes/sum.rb +0 -0
  60. data/lib/arel_extensions/nodes/then.rb +0 -0
  61. data/lib/arel_extensions/nodes/trim.rb +0 -2
  62. data/lib/arel_extensions/nodes/union.rb +0 -2
  63. data/lib/arel_extensions/nodes/union_all.rb +0 -2
  64. data/lib/arel_extensions/nodes/wday.rb +0 -4
  65. data/lib/arel_extensions/null_functions.rb +3 -5
  66. data/lib/arel_extensions/predications.rb +5 -6
  67. data/lib/arel_extensions/railtie.rb +5 -5
  68. data/lib/arel_extensions/set_functions.rb +0 -2
  69. data/lib/arel_extensions/string_functions.rb +21 -22
  70. data/lib/arel_extensions/tasks.rb +1 -1
  71. data/lib/arel_extensions/version.rb +1 -1
  72. data/lib/arel_extensions/visitors.rb +68 -60
  73. data/lib/arel_extensions/visitors/convert_format.rb +37 -0
  74. data/lib/arel_extensions/visitors/ibm_db.rb +4 -11
  75. data/lib/arel_extensions/visitors/mssql.rb +49 -44
  76. data/lib/arel_extensions/visitors/mysql.rb +65 -67
  77. data/lib/arel_extensions/visitors/oracle.rb +58 -55
  78. data/lib/arel_extensions/visitors/oracle12.rb +2 -3
  79. data/lib/arel_extensions/visitors/postgresql.rb +41 -34
  80. data/lib/arel_extensions/visitors/sqlite.rb +23 -18
  81. data/lib/arel_extensions/visitors/to_sql.rb +78 -61
  82. data/test/arelx_test_helper.rb +0 -2
  83. data/test/database.yml +2 -0
  84. data/test/real_db_test.rb +27 -42
  85. data/test/support/fake_record.rb +1 -1
  86. data/test/test_comparators.rb +0 -4
  87. data/test/visitors/test_bulk_insert_oracle.rb +0 -1
  88. data/test/visitors/test_bulk_insert_sqlite.rb +0 -2
  89. data/test/visitors/test_oracle.rb +1 -2
  90. data/test/visitors/test_to_sql.rb +16 -25
  91. data/test/with_ar/all_agnostic_test.rb +135 -139
  92. data/test/with_ar/insert_agnostic_test.rb +0 -2
  93. data/test/with_ar/test_bulk_sqlite.rb +0 -4
  94. data/test/with_ar/test_math_sqlite.rb +4 -8
  95. data/test/with_ar/test_string_mysql.rb +1 -5
  96. data/test/with_ar/test_string_sqlite.rb +1 -5
  97. data/version_v1.rb +1 -1
  98. data/version_v2.rb +1 -1
  99. metadata +8 -4
@@ -19,7 +19,6 @@ module ArelExtensions
19
19
 
20
20
  class Else < Arel::Nodes::Unary # :nodoc:
21
21
  end
22
-
23
22
  end
24
23
  else
25
24
  class Case < Arel::Nodes::Case
@@ -96,9 +95,9 @@ module ArelExtensions
96
95
 
97
96
  def eql? other
98
97
  self.class == other.class &&
99
- self.case == other.case &&
100
- self.conditions == other.conditions &&
101
- self.default == other.default
98
+ self.case == other.case &&
99
+ self.conditions == other.conditions &&
100
+ self.default == other.default
102
101
  end
103
102
  alias :== :eql?
104
103
 
@@ -14,7 +14,10 @@ module ArelExtensions
14
14
  @return_type = :decimal
15
15
  when :number
16
16
  @return_type = :number
17
- when 'char', 'varchar', 'text', 'nchar', 'nvarchar', 'ntext'
17
+ when 'char', 'varchar', 'nchar', 'nvarchar'
18
+ @return_type = :string
19
+ when 'text', :text, 'ntext', :ntext
20
+ @as_attr = expr[1].to_sym
18
21
  @return_type = :string
19
22
  when :datetime, 'datetime','smalldatetime'
20
23
  @return_type = :datetime
@@ -46,7 +49,6 @@ module ArelExtensions
46
49
  def return_type
47
50
  @return_type
48
51
  end
49
-
50
52
  end
51
53
  end
52
54
  end
@@ -1,7 +1,7 @@
1
1
  module ArelExtensions
2
2
  module Nodes
3
3
  class Ceil < Function
4
- RETURN_TYPE = :number
4
+ RETURN_TYPE = :number
5
5
  end
6
6
  end
7
7
  end
File without changes
@@ -29,7 +29,6 @@ module ArelExtensions
29
29
  end
30
30
  return super(tab)
31
31
  end
32
-
33
32
  end
34
33
  end
35
34
  end
@@ -12,7 +12,6 @@ module ArelExtensions
12
12
  tab = [convert_to_node(left)]
13
13
  return super(tab)
14
14
  end
15
-
16
15
  end
17
16
  end
18
17
  end
@@ -11,7 +11,7 @@ module ArelExtensions::Nodes
11
11
  else
12
12
  node
13
13
  end
14
- }.flatten.reduce([]) { | res, b |
14
+ }.flatten.reduce([]) { |res, b|
15
15
  # concatenate successive literal strings.
16
16
  if b.is_a?(Arel::Nodes::Quoted) && b.expr == ""
17
17
  res
@@ -37,7 +37,6 @@ module ArelExtensions::Nodes
37
37
  def concat(other)
38
38
  Concat.new(self.expressions + [other])
39
39
  end
40
-
41
40
  end
42
41
 
43
42
  class GroupConcat < AggregateFunction
@@ -47,8 +46,7 @@ module ArelExtensions::Nodes
47
46
 
48
47
  def initialize node, separator = ', ', **opts
49
48
  @separator = convert_to_node(separator)
50
- super node, opts
49
+ super node, **opts
51
50
  end
52
-
53
51
  end
54
52
  end
@@ -2,7 +2,7 @@ require 'date'
2
2
 
3
3
  module ArelExtensions
4
4
  module Nodes
5
- class DateDiff < Function #difference entre colonne date et date string/date
5
+ class DateDiff < Function # difference entre colonne date et date string/date
6
6
  attr_accessor :left_node_type
7
7
  attr_accessor :right_node_type
8
8
 
@@ -97,7 +97,7 @@ module ArelExtensions
97
97
  v ||= self.expressions.last
98
98
  if defined?(ActiveSupport::Duration) && ActiveSupport::Duration === v
99
99
  if @date_type == :ruby_date
100
- Arel.sql("(INTERVAL '1' DAY) * %s" % v.inspect.to_i )
100
+ Arel.sql("(INTERVAL '1' DAY) * %s" % v.inspect.to_i)
101
101
  else
102
102
  Arel.sql("(INTERVAL '1' SECOND) * %s" % v.to_i)
103
103
  end
@@ -150,6 +150,7 @@ module ArelExtensions
150
150
  end
151
151
 
152
152
  private
153
+
153
154
  def convert(object)
154
155
  case object
155
156
  when Arel::Attributes::Attribute, Arel::Nodes::Node, ActiveSupport::Duration
@@ -157,16 +158,16 @@ module ArelExtensions
157
158
  when Integer
158
159
  object.days
159
160
  when DateTime, Time, Date
160
- raise(ArgumentError, "#{object.class} can not be converted to Integer")
161
+ raise(ArgumentError, "#{object.class} cannot be converted to Integer")
161
162
  when String
162
163
  Arel::Nodes.build_quoted(object)
163
164
  else
164
- raise(ArgumentError, "#{object.class} can not be converted to Integer")
165
+ raise(ArgumentError, "#{object.class} cannot be converted to Integer")
165
166
  end
166
167
  end
167
168
  end
168
169
 
169
- class DateSub < Function #difference entre colonne date et date string/date
170
+ class DateSub < Function # difference entre colonne date et date string/date
170
171
  RETURN_TYPE = :integer
171
172
 
172
173
  def initialize(expr)
@@ -183,12 +184,10 @@ module ArelExtensions
183
184
  if defined?(ActiveSupport::Duration) && ActiveSupport::Duration === object
184
185
  object.to_i
185
186
  else
186
- raise(ArgumentError, "#{object.class} can not be converted to Number")
187
+ raise(ArgumentError, "#{object.class} cannot be converted to Number")
187
188
  end
188
189
  end
189
190
  end
190
-
191
191
  end
192
-
193
192
  end
194
193
  end
@@ -20,7 +20,6 @@ module ArelExtensions
20
20
  def right
21
21
  @expressions[1]
22
22
  end
23
-
24
23
  end
25
24
  end
26
25
  end
@@ -2,7 +2,6 @@ module ArelExtensions
2
2
  module Nodes
3
3
  class FindInSet < Function
4
4
  RETURN_TYPE = :integer
5
-
6
5
  end
7
6
  end
8
7
  end
@@ -1,7 +1,7 @@
1
1
  module ArelExtensions
2
2
  module Nodes
3
3
  class Floor < Function
4
- RETURN_TYPE = :number
4
+ RETURN_TYPE = :number
5
5
  end
6
6
  end
7
7
  end
@@ -1,15 +1,41 @@
1
+ require 'strscan'
2
+
1
3
  module ArelExtensions
2
4
  module Nodes
3
5
  class Format < Function
4
6
  RETURN_TYPE = :string
5
7
 
6
8
  attr_accessor :col_type, :iso_format
9
+
7
10
  def initialize expr
8
11
  col = expr.first
9
- @iso_format = expr[1]
12
+ @iso_format = convert_format(expr[1])
10
13
  @col_type = type_of_attribute(col)
11
14
  super [col, convert_to_string_node(@iso_format)]
12
15
  end
16
+
17
+ private
18
+
19
+ # Address portability issues with some of the formats.
20
+ def convert_format(fmt)
21
+ s = StringScanner.new fmt
22
+ res = StringIO.new
23
+ while !s.eos?
24
+ res <<
25
+ case
26
+ when s.scan(/%D/) then '%m/%d/%y'
27
+ when s.scan(/%F/) then '%Y-%m-%d'
28
+ when s.scan(/%R/) then '%H:%M'
29
+ when s.scan(/%r/) then '%I:%M:%S %p'
30
+ when s.scan(/%T/) then '%H:%M:%S'
31
+ when s.scan(/%v/) then '%e-%b-%Y'
32
+
33
+ when s.scan(/[^%]+/) then s.matched
34
+ when s.scan(/./) then s.matched
35
+ end
36
+ end
37
+ res.string
38
+ end
13
39
  end
14
40
  end
15
41
  end
@@ -19,7 +19,6 @@ module ArelExtensions
19
19
  @original_string = expr[1][:original_string]
20
20
  super [col]
21
21
  end
22
-
23
22
  end
24
23
  end
25
24
  end
@@ -10,6 +10,10 @@ module ArelExtensions
10
10
 
11
11
  RETURN_TYPE = :string # by default...
12
12
 
13
+ # Support multibyte string if they are available.
14
+ MBSTRING =
15
+ defined?(ActiveSupport::Multibyte::Chars) ? ActiveSupport::Multibyte::Chars : String
16
+
13
17
  # overrides as to make new Node like AliasPredication
14
18
 
15
19
  def return_type
@@ -50,8 +54,8 @@ module ArelExtensions
50
54
  end
51
55
  when ArelExtensions::Nodes::Function
52
56
  att.return_type
53
- # else
54
- # nil
57
+ # else
58
+ # nil
55
59
  end
56
60
  end
57
61
 
@@ -63,7 +67,7 @@ module ArelExtensions
63
67
  Arel::Nodes.build_quoted(object, self)
64
68
  when Time
65
69
  Arel::Nodes.build_quoted(object.strftime('%H:%M:%S'), self)
66
- when String, Symbol
70
+ when MBSTRING, String, Symbol
67
71
  Arel::Nodes.build_quoted(object.to_s)
68
72
  when Date
69
73
  Arel::Nodes.build_quoted(object.to_s, self)
@@ -74,7 +78,7 @@ module ArelExtensions
74
78
  when Array
75
79
  Arel::Nodes::Grouping.new(object.map{|r| convert_to_node(e)})
76
80
  else
77
- raise(ArgumentError, "#{object.class} can not be converted to CONCAT arg")
81
+ raise(ArgumentError, "#{object.class} cannot be converted to CONCAT arg")
78
82
  end
79
83
  end
80
84
 
@@ -97,8 +101,8 @@ module ArelExtensions
97
101
  Arel::Nodes.build_quoted(object, self)
98
102
  when Time
99
103
  Arel::Nodes.build_quoted(object.strftime('%H:%M:%S'), self)
100
- when String
101
- Arel::Nodes.build_quoted(object)
104
+ when MBSTRING, String
105
+ Arel::Nodes.build_quoted(object.to_s)
102
106
  when Date
103
107
  Arel::Nodes.build_quoted(object, self)
104
108
  when NilClass
@@ -106,7 +110,7 @@ module ArelExtensions
106
110
  when ActiveSupport::Duration
107
111
  Arel::Nodes.build_quoted(object.to_i.to_s)
108
112
  else
109
- raise(ArgumentError, "#{object.class} can not be converted to CONCAT arg")
113
+ raise(ArgumentError, "#{object.class} cannot be converted to CONCAT arg")
110
114
  end
111
115
  end
112
116
 
@@ -116,12 +120,12 @@ module ArelExtensions
116
120
  object
117
121
  when DateTime, Time
118
122
  Arel::Nodes.build_quoted(Date.new(object.year, object.month, object.day), self)
119
- when String
120
- Arel::Nodes.build_quoted(Date.parse(object), self)
123
+ when MBSTRING, String
124
+ Arel::Nodes.build_quoted(Date.parse(object.to_s), self)
121
125
  when Date
122
126
  Arel::Nodes.build_quoted(object, self)
123
127
  else
124
- raise(ArgumentError, "#{object.class} can not be converted to Date")
128
+ raise(ArgumentError, "#{object.class} cannot be converted to Date")
125
129
  end
126
130
  end
127
131
 
@@ -131,12 +135,12 @@ module ArelExtensions
131
135
  object
132
136
  when DateTime, Time
133
137
  Arel::Nodes.build_quoted(object, self)
134
- when String
135
- Arel::Nodes.build_quoted(Time.parse(object), self)
138
+ when MBSTRING, String
139
+ Arel::Nodes.build_quoted(Time.parse(object.to_s), self)
136
140
  when Date
137
141
  Arel::Nodes.build_quoted(Time.utc(object.year, object.month, object.day, 0, 0, 0), self)
138
142
  else
139
- raise(ArgumentError, "#{object.class} can not be converted to Datetime")
143
+ raise(ArgumentError, "#{object.class} cannot be converted to Datetime")
140
144
  end
141
145
  end
142
146
 
@@ -154,10 +158,9 @@ module ArelExtensions
154
158
  when NilClass
155
159
  0
156
160
  else
157
- raise(ArgumentError, "#{object.class} can not be converted to NUMBER arg")
161
+ raise(ArgumentError, "#{object.class} cannot be converted to NUMBER arg")
158
162
  end
159
163
  end
160
-
161
164
  end
162
165
  end
163
166
  end
File without changes
@@ -26,43 +26,55 @@ module ArelExtensions
26
26
  [@dict].hash
27
27
  end
28
28
 
29
+ def convert_to_json_node(n)
30
+ case n
31
+ when JsonNode
32
+ n.dict
33
+ when Array
34
+ n.map{|e|
35
+ (e.is_a?(Array) || e.is_a?(Hash)) ? Json.new(e) : convert_to_json_node(e)
36
+ }
37
+ when Hash
38
+ n.reduce({}){|acc,v|
39
+ 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])
40
+ acc
41
+ }
42
+ when String, Numeric, TrueClass, FalseClass
43
+ convert_to_node(n)
44
+ when Date
45
+ convert_to_node(n.strftime("%Y-%m-%d"))
46
+ when DateTime, Time
47
+ convert_to_node(n.strftime("%Y-%m-%dT%H:%M:%S.%L%:z"))
48
+ when NilClass
49
+ Arel.sql('null')
50
+ else
51
+ convert_to_node(n)
52
+ end
53
+ end
54
+
55
+ def type_of_node(v)
56
+ if v.is_a?(Arel::Attributes::Attribute)
57
+ self.type_of_attribute(v)
58
+ elsif v.respond_to?(:return_type)
59
+ v.return_type
60
+ elsif v.nil?
61
+ :nil
62
+ else
63
+ :string
64
+ end
65
+ end
29
66
  end
30
67
 
31
68
  class Json < JsonNode
32
-
33
69
  def initialize *expr
34
70
  @dict =
35
71
  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')
52
- else
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
59
- end
72
+ convert_to_json_node(expr.first)
60
73
  else
61
- expr.map{|e| (e.is_a?(Array) || e.is_a?(Hash)) ? Json.new(e) : convert_to_node(e) }
74
+ expr.map{|e| convert_to_json_node(e) }
62
75
  end
63
76
  super
64
77
  end
65
-
66
78
  end
67
79
 
68
80
  class JsonMerge < JsonNode
@@ -87,7 +99,6 @@ module ArelExtensions
87
99
  @dict = json
88
100
  @key = convert_to_node(key)
89
101
  end
90
-
91
102
  end
92
103
 
93
104
  class JsonSet < JsonNode
@@ -98,8 +109,6 @@ module ArelExtensions
98
109
  @key = convert_to_node(key)
99
110
  @value = Json.new(value)
100
111
  end
101
-
102
112
  end
103
-
104
113
  end
105
114
  end
@@ -2,7 +2,6 @@ module ArelExtensions
2
2
  module Nodes
3
3
  class Length < Function
4
4
  RETURN_TYPE = :integer
5
-
6
5
  end
7
6
  end
8
7
  end