arel_extensions 1.3.7 → 2.0.0.rc3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (128) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +2 -1
  3. data/.gitignore +6 -7
  4. data/.rubocop.yml +3 -67
  5. data/.travis/oracle/download.js +152 -0
  6. data/.travis/oracle/download.sh +30 -0
  7. data/.travis/oracle/download_ojdbc.js +116 -0
  8. data/.travis/oracle/install.sh +34 -0
  9. data/.travis/setup_accounts.sh +9 -0
  10. data/.travis/sqlite3/extension-functions.sh +6 -0
  11. data/.travis.yml +223 -0
  12. data/Gemfile +21 -16
  13. data/README.md +90 -270
  14. data/Rakefile +30 -41
  15. data/TODO +1 -0
  16. data/appveyor.yml +22 -60
  17. data/arel_extensions.gemspec +14 -13
  18. data/functions.html +3 -3
  19. data/gemfiles/rails3.gemfile +10 -10
  20. data/gemfiles/rails4.gemfile +29 -0
  21. data/gemfiles/rails5_0.gemfile +29 -0
  22. data/gemfiles/rails5_1_4.gemfile +14 -14
  23. data/gemfiles/rails5_2.gemfile +14 -16
  24. data/init/mssql.sql +4 -4
  25. data/init/mysql.sql +38 -38
  26. data/init/oracle.sql +0 -0
  27. data/init/postgresql.sql +21 -21
  28. data/init/sqlite.sql +0 -0
  29. data/lib/arel_extensions/attributes.rb +3 -4
  30. data/lib/arel_extensions/boolean_functions.rb +14 -53
  31. data/lib/arel_extensions/common_sql_functions.rb +17 -16
  32. data/lib/arel_extensions/comparators.rb +28 -27
  33. data/lib/arel_extensions/date_duration.rb +13 -17
  34. data/lib/arel_extensions/insert_manager.rb +15 -18
  35. data/lib/arel_extensions/math.rb +53 -55
  36. data/lib/arel_extensions/math_functions.rb +39 -46
  37. data/lib/arel_extensions/nodes/abs.rb +1 -0
  38. data/lib/arel_extensions/nodes/blank.rb +2 -1
  39. data/lib/arel_extensions/nodes/case.rb +19 -20
  40. data/lib/arel_extensions/nodes/cast.rb +8 -10
  41. data/lib/arel_extensions/nodes/ceil.rb +1 -1
  42. data/lib/arel_extensions/nodes/coalesce.rb +4 -3
  43. data/lib/arel_extensions/nodes/collate.rb +10 -9
  44. data/lib/arel_extensions/nodes/concat.rb +18 -9
  45. data/lib/arel_extensions/nodes/date_diff.rb +26 -42
  46. data/lib/arel_extensions/nodes/duration.rb +3 -0
  47. data/lib/arel_extensions/nodes/find_in_set.rb +1 -0
  48. data/lib/arel_extensions/nodes/floor.rb +1 -1
  49. data/lib/arel_extensions/nodes/format.rb +8 -35
  50. data/lib/arel_extensions/nodes/formatted_number.rb +23 -22
  51. data/lib/arel_extensions/nodes/function.rb +37 -42
  52. data/lib/arel_extensions/nodes/is_null.rb +0 -0
  53. data/lib/arel_extensions/nodes/json.rb +39 -48
  54. data/lib/arel_extensions/nodes/length.rb +0 -5
  55. data/lib/arel_extensions/nodes/levenshtein_distance.rb +1 -1
  56. data/lib/arel_extensions/nodes/locate.rb +2 -1
  57. data/lib/arel_extensions/nodes/log10.rb +2 -1
  58. data/lib/arel_extensions/nodes/matches.rb +7 -5
  59. data/lib/arel_extensions/nodes/md5.rb +1 -0
  60. data/lib/arel_extensions/nodes/power.rb +5 -5
  61. data/lib/arel_extensions/nodes/rand.rb +1 -0
  62. data/lib/arel_extensions/nodes/repeat.rb +5 -3
  63. data/lib/arel_extensions/nodes/replace.rb +8 -16
  64. data/lib/arel_extensions/nodes/round.rb +6 -5
  65. data/lib/arel_extensions/nodes/soundex.rb +15 -15
  66. data/lib/arel_extensions/nodes/std.rb +21 -18
  67. data/lib/arel_extensions/nodes/substring.rb +16 -8
  68. data/lib/arel_extensions/nodes/then.rb +1 -1
  69. data/lib/arel_extensions/nodes/trim.rb +6 -4
  70. data/lib/arel_extensions/nodes/union.rb +8 -5
  71. data/lib/arel_extensions/nodes/union_all.rb +7 -4
  72. data/lib/arel_extensions/nodes/wday.rb +4 -0
  73. data/lib/arel_extensions/nodes.rb +1 -1
  74. data/lib/arel_extensions/null_functions.rb +5 -19
  75. data/lib/arel_extensions/predications.rb +43 -44
  76. data/lib/arel_extensions/railtie.rb +5 -5
  77. data/lib/arel_extensions/set_functions.rb +7 -5
  78. data/lib/arel_extensions/string_functions.rb +29 -59
  79. data/lib/arel_extensions/tasks.rb +6 -6
  80. data/lib/arel_extensions/version.rb +1 -1
  81. data/lib/arel_extensions/visitors/ibm_db.rb +31 -24
  82. data/lib/arel_extensions/visitors/mssql.rb +180 -375
  83. data/lib/arel_extensions/visitors/mysql.rb +212 -350
  84. data/lib/arel_extensions/visitors/oracle.rb +178 -220
  85. data/lib/arel_extensions/visitors/oracle12.rb +31 -18
  86. data/lib/arel_extensions/visitors/postgresql.rb +173 -256
  87. data/lib/arel_extensions/visitors/sqlite.rb +126 -140
  88. data/lib/arel_extensions/visitors/to_sql.rb +237 -292
  89. data/lib/arel_extensions/visitors.rb +58 -82
  90. data/lib/arel_extensions.rb +31 -175
  91. data/test/database.yml +7 -15
  92. data/test/helper.rb +18 -0
  93. data/test/real_db_test.rb +116 -105
  94. data/test/support/fake_record.rb +3 -3
  95. data/test/test_comparators.rb +17 -14
  96. data/test/visitors/test_bulk_insert_oracle.rb +11 -11
  97. data/test/visitors/test_bulk_insert_sqlite.rb +13 -12
  98. data/test/visitors/test_bulk_insert_to_sql.rb +13 -11
  99. data/test/visitors/test_oracle.rb +55 -55
  100. data/test/visitors/test_to_sql.rb +226 -419
  101. data/test/with_ar/all_agnostic_test.rb +365 -702
  102. data/test/with_ar/insert_agnostic_test.rb +21 -27
  103. data/test/with_ar/test_bulk_sqlite.rb +16 -17
  104. data/test/with_ar/test_math_sqlite.rb +26 -26
  105. data/test/with_ar/test_string_mysql.rb +33 -31
  106. data/test/with_ar/test_string_sqlite.rb +34 -30
  107. metadata +34 -31
  108. data/.github/workflows/ruby.yml +0 -344
  109. data/NEWS.md +0 -32
  110. data/gemfiles/rails4_2.gemfile +0 -38
  111. data/gemfiles/rails5.gemfile +0 -29
  112. data/gemfiles/rails6.gemfile +0 -30
  113. data/gemfiles/rails6_1.gemfile +0 -30
  114. data/gemfiles/rails7.gemfile +0 -23
  115. data/gemspecs/arel_extensions-v1.gemspec +0 -27
  116. data/gemspecs/arel_extensions-v2.gemspec +0 -27
  117. data/generate_gems.sh +0 -15
  118. data/lib/arel_extensions/aliases.rb +0 -14
  119. data/lib/arel_extensions/helpers.rb +0 -51
  120. data/lib/arel_extensions/nodes/aggregate_function.rb +0 -13
  121. data/lib/arel_extensions/nodes/formatted_date.rb +0 -42
  122. data/lib/arel_extensions/nodes/rollup.rb +0 -36
  123. data/lib/arel_extensions/nodes/select.rb +0 -10
  124. data/lib/arel_extensions/nodes/sum.rb +0 -7
  125. data/lib/arel_extensions/visitors/convert_format.rb +0 -37
  126. data/test/arelx_test_helper.rb +0 -71
  127. data/version_v1.rb +0 -3
  128. data/version_v2.rb +0 -3
@@ -1,45 +1,17 @@
1
1
  module ArelExtensions
2
2
  module Visitors
3
3
  module MSSQL
4
-
5
- mssql_class = Arel::Visitors.constants.select { |c|
6
- Arel::Visitors.const_get(c).is_a?(Class) && %i[MSSQL SQLServer].include?(c)
7
- }.first
8
-
9
- LOADED_VISITOR = Arel::Visitors.const_get(mssql_class) || Arel::Visitors.const_get('MSSQL')
10
-
11
- LOADED_VISITOR::DATE_MAPPING = {
12
- 'd' => 'day', 'm' => 'month', 'y' => 'year', 'wd' => 'weekday', 'w' => 'week', 'h' => 'hour', 'mn' => 'minute', 's' => 'second'
13
- }.freeze
14
-
15
- LOADED_VISITOR::DATE_FORMAT_DIRECTIVES = {
16
- '%Y' => 'YYYY', '%C' => '', '%y' => 'YY', '%m' => 'MM', '%B' => 'month', '%^B' => '', '%b' => '', '%^b' => '', # year, month
17
- '%V' => 'iso_week', '%G' => '', # ISO week number and year of week
18
- '%d' => 'DD', '%e' => '' , '%j' => '' , '%w' => 'dw', %'a' => '', '%A' => 'weekday', # day, weekday
19
- '%H' => 'hh', '%k' => '' , '%I' => '' , '%l' => '' , '%P' => '', '%p' => '', # hours
4
+ Arel::Visitors::MSSQL::DATE_MAPPING = {'d' => 'day', 'm' => 'month', 'y' => 'year', 'wd' => 'weekday', 'w' => 'week', 'h' => 'hour', 'mn' => 'minute', 's' => 'second'}
5
+ Arel::Visitors::MSSQL::DATE_FORMAT_DIRECTIVES = {
6
+ '%Y' => 'YYYY', '%C' => '', '%y' => 'YY', '%m' => 'MM', '%B' => '', '%b' => '', '%^b' => '', # year, month
7
+ '%d' => 'DD', '%e' => '', '%j' => '', '%w' => 'dw', '%A' => '', # day, weekday
8
+ '%H' => 'hh', '%k' => '', '%I' => '', '%l' => '', '%P' => '', '%p' => '', # hours
20
9
  '%M' => 'mi', '%S' => 'ss', '%L' => 'ms', '%N' => 'ns', '%z' => 'tz'
21
- }.freeze
22
-
23
- LOADED_VISITOR::DATE_FORMAT_FORMAT = {
24
- 'YY' => '0#', 'MM' => '0#', 'DD' => '0#', 'hh' => '0#', 'mi' => '0#', 'ss' => '0#', 'iso_week' => '0#'
25
10
  }
26
-
27
- LOADED_VISITOR::DATE_NAME = [
28
- '%B', '%A'
29
- ]
30
-
31
- LOADED_VISITOR::DATE_FORMAT_REGEX =
32
- Regexp.new(
33
- LOADED_VISITOR::DATE_FORMAT_DIRECTIVES
34
- .keys
35
- .map{|k| Regexp.escape(k)}
36
- .join('|')
37
- ).freeze
38
-
39
11
  # TODO; all others... http://www.sql-server-helper.com/tips/date-formats.aspx
40
- LOADED_VISITOR::DATE_CONVERT_FORMATS = {
12
+ Arel::Visitors::MSSQL::DATE_CONVERT_FORMATS = {
41
13
  'YYYY-MM-DD' => 120,
42
- 'YY-MM-DD' => 120,
14
+ 'YY-MM-DD' => 120,
43
15
  'MM/DD/YYYY' => 101,
44
16
  'MM-DD-YYYY' => 110,
45
17
  'YYYY/MM/DD' => 111,
@@ -47,184 +19,108 @@ module ArelExtensions
47
19
  'DD-MM-YY' => 5,
48
20
  'DD.MM.YYYY' => 104,
49
21
  'YYYY-MM-DDTHH:MM:SS:MMM' => 126
50
- }.freeze
51
-
52
- # Quoting in JRuby + AR < 5 requires special handling for MSSQL.
53
- #
54
- # It relied on @connection.quote, which in turn relied on column type for
55
- # quoting. We need only to rely on the value type.
56
- #
57
- # It didn't handle numbers correctly: `quote(1, nil)` translated into
58
- # `N'1'` which we don't want.
59
- #
60
- # The following is adapted from activerecord/lib/active_record/connection_adapters/abstract/quoting.rb
61
- #
62
- if RUBY_PLATFORM == 'java' && ActiveRecord::VERSION::MAJOR < 5
63
- def quote_string(s)
64
- s.gsub('\\', '\&\&').gsub("'", "''") # ' (for ruby-mode)
65
- end
66
-
67
- def quoted_binary(value) # :nodoc:
68
- "'#{quote_string(value.to_s)}'"
69
- end
70
-
71
- def quoted_date(value)
72
- if value.acts_like?(:time)
73
- if ActiveRecord::Base.default_timezone == :utc
74
- value = value.getutc if value.respond_to?(:getutc) && !value.utc?
75
- else
76
- value = value.getlocal if value.respond_to?(:getlocal)
77
- end
78
- end
79
-
80
- result = value.to_s(:db)
81
- if value.respond_to?(:usec) && value.usec > 0
82
- result << '.' << sprintf('%06d', value.usec)
83
- else
84
- result
85
- end
86
- end
87
-
88
- def quoted_true
89
- 'TRUE'
90
- end
91
-
92
- def quoted_false
93
- 'FALSE'
94
- end
95
-
96
- def quoted_time(value) # :nodoc:
97
- value = value.change(year: 2000, month: 1, day: 1)
98
- quoted_date(value).sub(/\A\d{4}-\d{2}-\d{2} /, "")
99
- end
100
-
101
- def quote value, column = nil
102
- case value
103
- when Arel::Nodes::SqlLiteral
104
- value
105
- when String, Symbol, ActiveSupport::Multibyte::Chars
106
- "'#{quote_string(value.to_s)}'"
107
- when true
108
- quoted_true
109
- when false
110
- quoted_false
111
- when nil
112
- 'NULL'
113
- # BigDecimals need to be put in a non-normalized form and quoted.
114
- when BigDecimal
115
- value.to_s('F')
116
- when Numeric, ActiveSupport::Duration
117
- value.to_s
118
- when Date, Time
119
- "'#{quoted_date(value)}'"
120
- when Class
121
- "'#{value}'"
122
- else
123
- raise TypeError, "can't quote #{value.class.name}"
124
- end
125
- end
126
- end
22
+ }
127
23
 
128
24
  # Math Functions
129
25
  def visit_ArelExtensions_Nodes_Ceil o, collector
130
- collector << 'CEILING('
26
+ collector << "CEILING("
131
27
  collector = visit o.expr, collector
132
- collector << ')'
28
+ collector << ")"
133
29
  collector
134
30
  end
135
31
 
136
32
  def visit_ArelExtensions_Nodes_Log10 o, collector
137
- collector << 'LOG10('
33
+ collector << "LOG10("
138
34
  o.expressions.each_with_index { |arg, i|
139
- collector << Arel::Visitors::ToSql::COMMA if i != 0
35
+ collector << Arel::Visitors::ToSql::COMMA unless i == 0
140
36
  collector = visit arg, collector
141
37
  }
142
- collector << ')'
38
+ collector << ")"
143
39
  collector
144
40
  end
145
41
 
146
42
  def visit_ArelExtensions_Nodes_Power o, collector
147
- collector << 'POWER('
43
+ collector << "POWER("
148
44
  o.expressions.each_with_index { |arg, i|
149
- collector << Arel::Visitors::ToSql::COMMA if i != 0
45
+ collector << Arel::Visitors::ToSql::COMMA unless i == 0
150
46
  collector = visit arg, collector
151
47
  }
152
- collector << ')'
48
+ collector << ")"
153
49
  collector
154
50
  end
155
51
 
156
52
  def visit_ArelExtensions_Nodes_IsNull o, collector
157
- collector << '('
53
+ collector << "("
158
54
  collector = visit o.expr, collector
159
- collector << ' IS NULL)'
55
+ collector << " IS NULL)"
160
56
  collector
161
57
  end
162
58
 
163
59
  def visit_ArelExtensions_Nodes_IsNotNull o, collector
164
- collector << '('
60
+ collector << "("
165
61
  collector = visit o.expr, collector
166
- collector << ' IS NOT NULL)'
62
+ collector << " IS NOT NULL)"
167
63
  collector
168
64
  end
169
65
 
170
66
  def visit_ArelExtensions_Nodes_Concat o, collector
171
- collector << 'CONCAT('
67
+ collector << "CONCAT("
172
68
  o.expressions.each_with_index { |arg, i|
173
- collector << LOADED_VISITOR::COMMA if i != 0
69
+ collector << Arel::Visitors::MSSQL::COMMA unless i == 0
174
70
  collector = visit arg, collector
175
71
  }
176
- collector << ')'
72
+ collector << ")"
177
73
  collector
178
74
  end
179
75
 
180
76
  def visit_ArelExtensions_Nodes_Repeat o, collector
181
- collector << 'REPLICATE('
77
+ collector << "REPLICATE("
182
78
  o.expressions.each_with_index { |arg, i|
183
- collector << Arel::Visitors::ToSql::COMMA if i != 0
79
+ collector << Arel::Visitors::ToSql::COMMA unless i == 0
184
80
  collector = visit arg, collector
185
81
  }
186
- collector << ')'
82
+ collector << ")"
187
83
  collector
188
84
  end
189
85
 
190
86
 
191
87
 
192
88
  def visit_ArelExtensions_Nodes_DateDiff o, collector
193
- case o.right_node_type
194
- when :ruby_date, :ruby_time, :date, :datetime, :time
195
- collector << case o.left_node_type
196
- when :ruby_time, :datetime, :time then 'DATEDIFF(second'
197
- else 'DATEDIFF(day'
89
+ if o.right_node_type == :ruby_date || o.right_node_type == :ruby_time || o.right_node_type == :date || o.right_node_type == :datetime || o.right_node_type == :time
90
+ collector << if o.left_node_type == :ruby_time || o.left_node_type == :datetime || o.left_node_type == :time
91
+ 'DATEDIFF(second'
92
+ else
93
+ 'DATEDIFF(day'
198
94
  end
199
- collector << LOADED_VISITOR::COMMA
95
+ collector << Arel::Visitors::MSSQL::COMMA
200
96
  collector = visit o.right, collector
201
- collector << LOADED_VISITOR::COMMA
97
+ collector << Arel::Visitors::MSSQL::COMMA
202
98
  collector = visit o.left, collector
203
99
  collector << ')'
204
100
  else
205
101
  da = ArelExtensions::Nodes::DateAdd.new([])
206
- collector << 'DATEADD('
102
+ collector << "DATEADD("
207
103
  collector = visit da.mssql_datepart(o.right), collector
208
- collector << LOADED_VISITOR::COMMA
209
- collector << '-('
104
+ collector << Arel::Visitors::MSSQL::COMMA
105
+ collector << "-("
210
106
  collector = visit da.mssql_value(o.right), collector
211
- collector << ')'
212
- collector << LOADED_VISITOR::COMMA
107
+ collector << ")"
108
+ collector << Arel::Visitors::MSSQL::COMMA
213
109
  collector = visit o.left, collector
214
- collector << ')'
110
+ collector << ")"
215
111
  collector
216
112
  end
217
113
  collector
218
114
  end
219
115
 
220
116
  def visit_ArelExtensions_Nodes_DateAdd o, collector
221
- collector << 'DATEADD('
117
+ collector << "DATEADD("
222
118
  collector = visit o.mssql_datepart(o.right), collector
223
- collector << LOADED_VISITOR::COMMA
119
+ collector << Arel::Visitors::MSSQL::COMMA
224
120
  collector = visit o.mssql_value(o.right), collector
225
- collector << LOADED_VISITOR::COMMA
121
+ collector << Arel::Visitors::MSSQL::COMMA
226
122
  collector = visit o.left, collector
227
- collector << ')'
123
+ collector << ")"
228
124
  collector
229
125
  end
230
126
 
@@ -233,77 +129,68 @@ module ArelExtensions
233
129
  collector = visit o.right, collector
234
130
  else
235
131
  left = o.left.end_with?('i') ? o.left[0..-2] : o.left
236
- conv = %w[h mn s].include?(o.left)
132
+ conv = ['h', 'mn', 's'].include?(o.left)
237
133
  collector << 'DATEPART('
238
- collector << LOADED_VISITOR::DATE_MAPPING[left]
239
- collector << LOADED_VISITOR::COMMA
134
+ collector << Arel::Visitors::MSSQL::DATE_MAPPING[left]
135
+ collector << Arel::Visitors::MSSQL::COMMA
240
136
  collector << 'CONVERT(datetime,' if conv
241
137
  collector = visit o.right, collector
242
138
  collector << ')' if conv
243
- collector << ')'
139
+ collector << ")"
244
140
  end
245
141
  collector
246
142
  end
247
143
 
248
144
  def visit_ArelExtensions_Nodes_Length o, collector
249
- if o.bytewise
250
- collector << '(DATALENGTH('
251
- collector = visit o.expr, collector
252
- collector << ') / ISNULL(NULLIF(DATALENGTH(LEFT(COALESCE('
253
- collector = visit o.expr, collector
254
- collector << ", '#' ), 1 )), 0), 1))"
255
- collector
256
- else
257
- collector << 'LEN('
258
- collector = visit o.expr, collector
259
- collector << ')'
260
- collector
261
- end
145
+ collector << "LEN("
146
+ collector = visit o.expr, collector
147
+ collector << ")"
148
+ collector
262
149
  end
263
150
 
264
151
  def visit_ArelExtensions_Nodes_Round o, collector
265
- collector << 'ROUND('
152
+ collector << "ROUND("
266
153
  o.expressions.each_with_index { |arg, i|
267
- collector << LOADED_VISITOR::COMMA if i != 0
154
+ collector << Arel::Visitors::MSSQL::COMMA unless i == 0
268
155
  collector = visit arg, collector
269
156
  }
270
157
  if o.expressions.length == 1
271
- collector << LOADED_VISITOR::COMMA
272
- collector << '0'
158
+ collector << Arel::Visitors::MSSQL::COMMA
159
+ collector << "0"
273
160
  end
274
- collector << ')'
161
+ collector << ")"
275
162
  collector
276
163
  end
277
164
 
278
165
  def visit_ArelExtensions_Nodes_Locate o, collector
279
- collector << 'CHARINDEX('
166
+ collector << "CHARINDEX("
280
167
  collector = visit o.right, collector
281
- collector << LOADED_VISITOR::COMMA
168
+ collector << Arel::Visitors::MSSQL::COMMA
282
169
  collector = visit o.left, collector
283
- collector << ')'
170
+ collector << ")"
284
171
  collector
285
172
  end
286
173
 
287
174
  def visit_ArelExtensions_Nodes_Substring o, collector
288
175
  collector << 'SUBSTRING('
289
176
  collector = visit o.expressions[0], collector
290
- collector << LOADED_VISITOR::COMMA
177
+ collector << Arel::Visitors::MSSQL::COMMA
291
178
  collector = visit o.expressions[1], collector
292
- collector << LOADED_VISITOR::COMMA
179
+ collector << Arel::Visitors::MSSQL::COMMA
293
180
  collector = o.expressions[2] ? visit(o.expressions[2], collector) : visit(o.expressions[0].length, collector)
294
181
  collector << ')'
295
182
  collector
296
183
  end
297
184
 
298
185
  def visit_ArelExtensions_Nodes_Trim o, collector
299
- # NOTE: in MSSQL's `blank`, o.right is the space char so we need to
300
- # account for it.
301
- if o.right && !/\A\s\Z/.match(o.right.expr)
302
- collector << 'dbo.TrimChar('
186
+ if o.right
187
+ collector << "REPLACE(REPLACE(LTRIM(RTRIM(REPLACE(REPLACE("
303
188
  collector = visit o.left, collector
304
- collector << Arel::Visitors::MSSQL::COMMA
189
+ collector << ", ' ', '~'), "
305
190
  collector = visit o.right, collector
306
- collector << ')'
191
+ collector << ", ' '))), ' ', "
192
+ collector = visit o.right, collector
193
+ collector << "), '~', ' ')"
307
194
  else
308
195
  collector << "LTRIM(RTRIM("
309
196
  collector = visit o.left, collector
@@ -314,7 +201,7 @@ module ArelExtensions
314
201
 
315
202
  def visit_ArelExtensions_Nodes_Ltrim o, collector
316
203
  if o.right
317
- collector << 'REPLACE(REPLACE(LTRIM(REPLACE(REPLACE('
204
+ collector << "REPLACE(REPLACE(LTRIM(REPLACE(REPLACE("
318
205
  collector = visit o.left, collector
319
206
  collector << ", ' ', '~'), "
320
207
  collector = visit o.right, collector
@@ -322,16 +209,16 @@ module ArelExtensions
322
209
  collector = visit o.right, collector
323
210
  collector << "), '~', ' ')"
324
211
  else
325
- collector << 'LTRIM('
212
+ collector << "LTRIM("
326
213
  collector = visit o.left, collector
327
- collector << ')'
214
+ collector << ")"
328
215
  end
329
216
  collector
330
217
  end
331
218
 
332
219
  def visit_ArelExtensions_Nodes_Rtrim o, collector
333
220
  if o.right
334
- collector << 'REPLACE(REPLACE(RTRIM(REPLACE(REPLACE('
221
+ collector << "REPLACE(REPLACE(RTRIM(REPLACE(REPLACE("
335
222
  collector = visit o.left, collector
336
223
  collector << ", ' ', '~'), "
337
224
  collector = visit o.right, collector
@@ -339,9 +226,9 @@ module ArelExtensions
339
226
  collector = visit o.right, collector
340
227
  collector << "), '~', ' ')"
341
228
  else
342
- collector << 'RTRIM('
229
+ collector << "RTRIM("
343
230
  collector = visit o.left, collector
344
- collector << ')'
231
+ collector << ")"
345
232
  end
346
233
  collector
347
234
  end
@@ -355,102 +242,64 @@ module ArelExtensions
355
242
  end
356
243
 
357
244
  def visit_ArelExtensions_Nodes_Format o, collector
358
- visit_ArelExtensions_Nodes_FormattedDate o, collector
359
- end
360
-
361
- def visit_ArelExtensions_Nodes_FormattedDate o, collector
362
- f = ArelExtensions::Visitors::strftime_to_format(o.iso_format, LOADED_VISITOR::DATE_FORMAT_DIRECTIVES)
363
- if fmt = LOADED_VISITOR::DATE_CONVERT_FORMATS[f]
245
+ f = o.iso_format.dup
246
+ Arel::Visitors::MSSQL::DATE_FORMAT_DIRECTIVES.each { |d, r| f.gsub!(d, r) }
247
+ if Arel::Visitors::MSSQL::DATE_CONVERT_FORMATS[f]
364
248
  collector << "CONVERT(VARCHAR(#{f.length})"
365
- collector << LOADED_VISITOR::COMMA
366
- if o.time_zone
367
- collector << 'CONVERT(datetime'
368
- collector << LOADED_VISITOR::COMMA
369
- collector << ' '
370
- end
249
+ collector << Arel::Visitors::MSSQL::COMMA
371
250
  collector = visit o.left, collector
372
- case o.time_zone
373
- when Hash
374
- src_tz, dst_tz = o.time_zone.first
375
- collector << ') AT TIME ZONE '
376
- collector = visit Arel.quoted(src_tz), collector
377
- collector << ' AT TIME ZONE '
378
- collector = visit Arel.quoted(dst_tz), collector
379
- when String
380
- collector << ') AT TIME ZONE '
381
- collector = visit Arel.quoted(o.time_zone), collector
382
- end
383
- collector << LOADED_VISITOR::COMMA
384
- collector << fmt.to_s
251
+ collector << Arel::Visitors::MSSQL::COMMA
252
+ collector << Arel::Visitors::MSSQL::DATE_CONVERT_FORMATS[f].to_s
385
253
  collector << ')'
386
254
  collector
387
255
  else
388
- s = StringScanner.new o.iso_format
389
- collector << '('
390
- sep = ''
391
- while !s.eos?
392
- collector << sep
393
- sep = ' + '
394
- case
395
- when s.scan(LOADED_VISITOR::DATE_FORMAT_REGEX)
396
- dir = LOADED_VISITOR::DATE_FORMAT_DIRECTIVES[s.matched]
397
- fmt = LOADED_VISITOR::DATE_FORMAT_FORMAT[dir]
398
- date_name = LOADED_VISITOR::DATE_NAME.include?(s.matched)
399
- collector << 'LTRIM(RTRIM('
400
- collector << 'FORMAT(' if fmt
401
- collector << 'STR(' if !fmt && !date_name
402
- collector << (date_name ? 'DATENAME(' : 'DATEPART(')
403
- collector << dir
404
- collector << LOADED_VISITOR::COMMA
405
- if o.time_zone
406
- collector << 'CONVERT(datetime'
407
- collector << LOADED_VISITOR::COMMA
408
- collector << ' '
256
+ collector << "("
257
+ t = o.iso_format.split('%')
258
+ t.each_with_index {|str, i|
259
+ if i == 0 && t[0] != '%'
260
+ collector = visit Arel::Nodes.build_quoted(str), collector
261
+ if str.length > 1
262
+ collector << Arel::Visitors::MSSQL::COMMA
263
+ collector = visit Arel::Nodes.build_quoted(str.sub(/\A./, '')), collector
409
264
  end
410
- collector = visit o.left, collector
411
- case o.time_zone
412
- when Hash
413
- src_tz, dst_tz = o.time_zone.first.first, o.time_zone.first.second
414
- collector << ') AT TIME ZONE '
415
- collector = visit Arel.quoted(src_tz), collector
416
- collector << ' AT TIME ZONE '
417
- collector = visit Arel.quoted(dst_tz), collector
418
- when String
419
- collector << ') AT TIME ZONE '
420
- collector = visit Arel.quoted(o.time_zone), collector
265
+ elsif str.length > 0
266
+ if !Arel::Visitors::MSSQL::DATE_FORMAT_DIRECTIVES['%' + str[0]].blank?
267
+ collector << 'LTRIM(STR(DATEPART('
268
+ collector << Arel::Visitors::MSSQL::DATE_FORMAT_DIRECTIVES['%' + str[0]]
269
+ collector << Arel::Visitors::MSSQL::COMMA
270
+ collector = visit o.left, collector
271
+ collector << ')))'
272
+ if str.length > 1
273
+ collector << ' + '
274
+ collector = visit Arel::Nodes.build_quoted(str.sub(/\A./, '')), collector
275
+ end
421
276
  end
422
- collector << ')'
423
- collector << ')' if !fmt && !date_name
424
- collector << LOADED_VISITOR::COMMA << "'#{fmt}')" if fmt
425
- collector << '))'
426
- when s.scan(/^%%/)
427
- collector = visit Arel.quoted('%'), collector
428
- when s.scan(/[^%]+|./)
429
- collector = visit Arel.quoted(s.matched), collector
430
277
  end
431
- end
278
+ collector << ' + ' if t[i + 1]
279
+ }
280
+
432
281
  collector << ')'
433
282
  collector
434
283
  end
435
284
  end
436
285
 
437
286
  def visit_ArelExtensions_Nodes_Replace o, collector
438
- collector << 'REPLACE('
287
+ collector << "REPLACE("
439
288
  o.expressions.each_with_index { |arg, i|
440
- collector << LOADED_VISITOR::COMMA if i != 0
289
+ collector << Arel::Visitors::MSSQL::COMMA unless i == 0
441
290
  collector = visit arg, collector
442
291
  }
443
- collector << ')'
292
+ collector << ")"
444
293
  collector
445
294
  end
446
295
 
447
296
  def visit_ArelExtensions_Nodes_FindInSet o, collector
448
- collector << 'dbo.FIND_IN_SET('
297
+ collector << "dbo.FIND_IN_SET("
449
298
  o.expressions.each_with_index { |arg, i|
450
- collector << LOADED_VISITOR::COMMA if i != 0
299
+ collector << Arel::Visitors::MSSQL::COMMA unless i == 0
451
300
  collector = visit arg, collector
452
301
  }
453
- collector << ')'
302
+ collector << ")"
454
303
  collector
455
304
  end
456
305
 
@@ -493,9 +342,9 @@ module ArelExtensions
493
342
  end
494
343
 
495
344
  def visit_ArelExtensions_Nodes_AiIMatches o, collector
496
- collector = visit o.left.collate(true, true), collector
345
+ collector = visit o.left.collate(true,true), collector
497
346
  collector << ' LIKE '
498
- collector = visit o.right.collate(true, true), collector
347
+ collector = visit o.right.collate(true,true), collector
499
348
  if o.escape
500
349
  collector << ' ESCAPE '
501
350
  visit o.escape, collector
@@ -533,27 +382,6 @@ module ArelExtensions
533
382
  collector
534
383
  end
535
384
 
536
- alias_method(:old_visit_Arel_Nodes_As, :visit_Arel_Nodes_As) rescue nil
537
- def visit_Arel_Nodes_As o, collector
538
- if o.left.is_a?(Arel::Nodes::Binary)
539
- collector << '('
540
- collector = visit o.left, collector
541
- collector << ')'
542
- else
543
- collector = visit o.left, collector
544
- end
545
- collector << ' AS '
546
-
547
- # sometimes these values are already quoted, if they are, don't double quote it
548
- quote = o.right.is_a?(Arel::Nodes::SqlLiteral) && o.right[0] != '"' && o.right[-1] != '"'
549
-
550
- collector << '"' if quote
551
- collector = visit o.right, collector
552
- collector << '"' if quote
553
-
554
- collector
555
- end
556
-
557
385
  # SQL Server does not know about REGEXP
558
386
  def visit_Arel_Nodes_Regexp o, collector
559
387
  collector = visit o.left, collector
@@ -567,121 +395,108 @@ module ArelExtensions
567
395
  collector
568
396
  end
569
397
 
570
- def visit_Arel_Nodes_RollUp(o, collector)
571
- collector << "ROLLUP"
572
- grouping_array_or_grouping_element o, collector
573
- end
574
-
575
398
  # TODO;
576
399
  def visit_ArelExtensions_Nodes_GroupConcat o, collector
577
- collector << '(STRING_AGG('
400
+ collector << "(STRING_AGG("
578
401
  collector = visit o.left, collector
579
402
  collector << Arel::Visitors::Oracle::COMMA
580
- collector =
581
- if o.separator && o.separator != 'NULL'
582
- visit o.separator, collector
583
- else
584
- visit Arel.quoted(','), collector
585
- end
586
- collector << ') WITHIN GROUP (ORDER BY '
587
- if o.order.present?
588
- o.order.each_with_index do |order, i|
589
- collector << Arel::Visitors::Oracle::COMMA if i != 0
403
+ if o.right && o.right != 'NULL'
404
+ collector = visit o.right, collector
405
+ else
406
+ collector = visit Arel::Nodes.build_quoted(','), collector
407
+ end
408
+ collector << ") WITHIN GROUP (ORDER BY "
409
+ if !o.orders.blank?
410
+ o.orders.each_with_index do |order,i|
411
+ collector << Arel::Visitors::Oracle::COMMA unless i == 0
590
412
  collector = visit order, collector
591
413
  end
592
414
  else
593
415
  collector = visit o.left, collector
594
416
  end
595
- collector << '))'
417
+ collector << "))"
596
418
  collector
597
419
  end
598
420
 
599
421
  def visit_ArelExtensions_Nodes_MD5 o, collector
600
422
  collector << "LOWER(CONVERT(NVARCHAR(32),HashBytes('MD5',CONVERT(VARCHAR,"
601
423
  collector = visit o.left, collector
602
- collector << ')),2))'
424
+ collector << ")),2))"
603
425
  collector
604
426
  end
605
427
 
606
428
  def visit_ArelExtensions_Nodes_Cast o, collector
607
- as_attr =
608
- case o.as_attr
609
- when :string
610
- 'varchar'
611
- when :time
612
- 'time'
613
- when :date
614
- 'date'
615
- when :datetime
616
- 'datetime'
617
- when :number, :decimal, :float
618
- 'decimal(10,6)'
619
- when :int
620
- collector << 'CAST(CAST('
621
- collector = visit o.left, collector
622
- collector << ' AS decimal(10,0)) AS int)'
623
- return collector
624
- when :binary
625
- 'binary'
626
- else
627
- o.as_attr.to_s
628
- end
629
- collector << 'CAST('
429
+ case o.as_attr
430
+ when :string
431
+ as_attr = Arel::Nodes::SqlLiteral.new('varchar')
432
+ when :time
433
+ as_attr = Arel::Nodes::SqlLiteral.new('time')
434
+ when :date
435
+ as_attr = Arel::Nodes::SqlLiteral.new('date')
436
+ when :datetime
437
+ as_attr = Arel::Nodes::SqlLiteral.new('datetime')
438
+ when :number,:decimal, :float
439
+ as_attr = Arel::Nodes::SqlLiteral.new('decimal(10,6)')
440
+ when :int
441
+ collector << "CAST(CAST("
442
+ collector = visit o.left, collector
443
+ collector << " AS decimal(10,0)) AS int)"
444
+ return collector
445
+ when :binary
446
+ as_attr = Arel::Nodes::SqlLiteral.new('binary')
447
+ else
448
+ as_attr = Arel::Nodes::SqlLiteral.new(o.as_attr.to_s)
449
+ end
450
+ collector << "CAST("
630
451
  collector = visit o.left, collector
631
- collector << ' AS '
632
- collector = visit Arel::Nodes::SqlLiteral.new(as_attr), collector
633
- collector << ')'
452
+ collector << " AS "
453
+ collector = visit as_attr, collector
454
+ collector << ")"
634
455
  collector
635
456
  end
636
457
 
637
458
  def visit_ArelExtensions_Nodes_FormattedNumber o, collector
638
459
  col = o.left.coalesce(0)
639
- locale = Arel.quoted(o.locale.tr('_', '-'))
640
- param = Arel.quoted("N#{o.precision}")
641
- sign = Arel.when(col < 0).
460
+ locale = Arel::Nodes.build_quoted(o.locale.tr('_','-'))
461
+ param = Arel::Nodes.build_quoted("N#{o.precision}")
462
+ sign = ArelExtensions::Nodes::Case.new.when(col<0).
642
463
  then('-').
643
464
  else(o.flags.include?('+') ? '+' : (o.flags.include?(' ') ? ' ' : ''))
644
465
  sign_length = o.flags.include?('+') || o.flags.include?(' ') ?
645
- Arel.quoted(1) :
646
- Arel.when(col < 0).then(1).else(0)
647
-
648
- number =
649
- if o.scientific_notation
650
- ArelExtensions::Nodes::Concat.new([
651
- Arel::Nodes::NamedFunction.new('FORMAT', [
652
- col.abs / Arel.quoted(10).pow(col.abs.log10.floor),
466
+ Arel::Nodes.build_quoted(1) :
467
+ ArelExtensions::Nodes::Case.new.when(col<0).then(1).else(0)
468
+
469
+ if o.scientific_notation
470
+ number = ArelExtensions::Nodes::Concat.new([
471
+ Arel::Nodes::NamedFunction.new('FORMAT',[
472
+ col.abs/Arel::Nodes.build_quoted(10).pow(col.abs.log10.floor),
653
473
  param,
654
474
  locale
655
475
  ]),
656
476
  o.type,
657
- Arel::Nodes::NamedFunction.new('FORMAT', [
477
+ Arel::Nodes::NamedFunction.new('FORMAT',[
658
478
  col.abs.log10.floor,
659
- Arel.quoted('N0'),
479
+ Arel::Nodes.build_quoted('N0'),
660
480
  locale
661
481
  ])
662
482
  ])
663
- else
664
- Arel::Nodes::NamedFunction.new('FORMAT', [
665
- Arel.quoted(col.abs),
483
+ else
484
+ number = Arel::Nodes::NamedFunction.new('FORMAT',[
485
+ Arel::Nodes.build_quoted(col.abs),
666
486
  param,
667
487
  locale
668
488
  ])
669
- end
489
+ end
670
490
 
671
- repeated_char =
672
- if o.width == 0
673
- Arel.quoted('')
674
- else
675
- Arel
676
- .when(Arel.quoted(o.width).abs - (number.length + sign_length) > 0)
677
- .then(Arel.quoted(
678
- o.flags.include?('-') ? ' ' : (o.flags.include?('0') ? '0' : ' ')
679
- ).repeat(Arel.quoted(o.width).abs - (number.length + sign_length))
680
- )
681
- .else('')
682
- end
683
- before = !o.flags.include?('0') && !o.flags.include?('-') ? repeated_char : ''
684
- middle = o.flags.include?('0') && !o.flags.include?('-') ? repeated_char : ''
491
+ repeated_char = (o.width == 0) ? Arel::Nodes.build_quoted('') : ArelExtensions::Nodes::Case.new().
492
+ when(Arel::Nodes.build_quoted(o.width).abs-(number.length+sign_length)>0).
493
+ then(Arel::Nodes.build_quoted(
494
+ o.flags.include?('-') ? ' ' : (o.flags.include?('0') ? '0' : ' ')
495
+ ).repeat(Arel::Nodes.build_quoted(o.width).abs-(number.length+sign_length))
496
+ ).
497
+ else('')
498
+ before = (!o.flags.include?('0'))&&(!o.flags.include?('-')) ? repeated_char : ''
499
+ middle = (o.flags.include?('0'))&&(!o.flags.include?('-')) ? repeated_char : ''
685
500
  after = o.flags.include?('-') ? repeated_char : ''
686
501
  full_number =
687
502
  ArelExtensions::Nodes::Concat.new([
@@ -691,27 +506,27 @@ module ArelExtensions
691
506
  number,
692
507
  after
693
508
  ])
694
- collector = visit ArelExtensions::Nodes::Concat.new([Arel.quoted(o.prefix), full_number, Arel.quoted(o.suffix)]), collector
509
+ collector = visit ArelExtensions::Nodes::Concat.new([Arel::Nodes.build_quoted(o.prefix),full_number,Arel::Nodes.build_quoted(o.suffix)]), collector
695
510
  collector
696
511
  end
697
512
 
698
513
  def visit_ArelExtensions_Nodes_Std o, collector
699
- collector << (o.unbiased_estimator ? 'STDEV(' : 'STDEVP(')
514
+ collector << (o.unbiased_estimator ? "STDEV(" : "STDEVP(")
700
515
  visit o.left, collector
701
- collector << ')'
516
+ collector << ")"
702
517
  collector
703
518
  end
704
519
 
705
520
  def visit_ArelExtensions_Nodes_Variance o, collector
706
- collector << (o.unbiased_estimator ? 'VAR(' : 'VARP(')
521
+ collector << (o.unbiased_estimator ? "VAR(" : "VARP(")
707
522
  visit o.left, collector
708
- collector << ')'
523
+ collector << ")"
709
524
  collector
710
525
  end
711
526
 
712
527
 
713
528
  def visit_ArelExtensions_Nodes_LevenshteinDistance o, collector
714
- collector << 'dbo.LEVENSHTEIN_DISTANCE('
529
+ collector << "dbo.LEVENSHTEIN_DISTANCE("
715
530
  collector = visit o.left, collector
716
531
  collector << Arel::Visitors::ToSql::COMMA
717
532
  collector = visit o.right, collector
@@ -720,30 +535,20 @@ module ArelExtensions
720
535
  end
721
536
 
722
537
 
723
- def visit_ArelExtensions_Nodes_JsonGet o, collector
538
+ def visit_ArelExtensions_Nodes_JsonGet o,collector
724
539
  collector << 'JSON_VALUE('
725
540
  collector = visit o.dict, collector
726
541
  collector << Arel::Visitors::MySQL::COMMA
727
542
  if o.key.is_a?(Integer)
728
543
  collector << "\"$[#{o.key}]\""
729
544
  else
730
- collector = visit Arel.quoted('$.') + o.key, collector
545
+ collector = visit Arel::Nodes.build_quoted('$.')+o.key, collector
731
546
  end
732
547
  collector << ')'
733
548
  collector
734
549
  end
735
550
 
736
- # Utilized by GroupingSet, Cube & RollUp visitors to
737
- # handle grouping aggregation semantics
738
- def grouping_array_or_grouping_element(o, collector)
739
- if o.expr.is_a? Array
740
- collector << "( "
741
- visit o.expr, collector
742
- collector << " )"
743
- else
744
- visit o.expr, collector
745
- end
746
- end
551
+
747
552
  end
748
553
  end
749
554
  end