arel_extensions 1.3.1 → 1.3.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (117) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +1 -2
  3. data/.github/workflows/ruby.yml +4 -4
  4. data/.rubocop.yml +25 -1
  5. data/Gemfile +10 -10
  6. data/README.md +24 -4
  7. data/Rakefile +4 -4
  8. data/TODO +0 -1
  9. data/arel_extensions.gemspec +12 -12
  10. data/gemfiles/rails3.gemfile +10 -10
  11. data/gemfiles/rails4.gemfile +13 -13
  12. data/gemfiles/rails5_0.gemfile +12 -12
  13. data/gemfiles/rails5_1_4.gemfile +12 -12
  14. data/gemfiles/rails5_2.gemfile +13 -13
  15. data/gemfiles/rails6.gemfile +11 -11
  16. data/gemfiles/rails6_1.gemfile +11 -11
  17. data/gemfiles/rails7.gemfile +7 -7
  18. data/gemspecs/arel_extensions-v1.gemspec +12 -12
  19. data/gemspecs/arel_extensions-v2.gemspec +11 -11
  20. data/init/mssql.sql +0 -0
  21. data/init/mysql.sql +0 -0
  22. data/init/oracle.sql +0 -0
  23. data/init/postgresql.sql +0 -0
  24. data/init/sqlite.sql +0 -0
  25. data/lib/arel_extensions/attributes.rb +2 -2
  26. data/lib/arel_extensions/boolean_functions.rb +2 -4
  27. data/lib/arel_extensions/common_sql_functions.rb +10 -10
  28. data/lib/arel_extensions/comparators.rb +14 -14
  29. data/lib/arel_extensions/date_duration.rb +7 -7
  30. data/lib/arel_extensions/helpers.rb +16 -13
  31. data/lib/arel_extensions/insert_manager.rb +1 -1
  32. data/lib/arel_extensions/math.rb +16 -18
  33. data/lib/arel_extensions/math_functions.rb +18 -18
  34. data/lib/arel_extensions/nodes/abs.rb +0 -0
  35. data/lib/arel_extensions/nodes/aggregate_function.rb +0 -0
  36. data/lib/arel_extensions/nodes/blank.rb +1 -1
  37. data/lib/arel_extensions/nodes/case.rb +4 -6
  38. data/lib/arel_extensions/nodes/cast.rb +3 -3
  39. data/lib/arel_extensions/nodes/ceil.rb +0 -0
  40. data/lib/arel_extensions/nodes/change_case.rb +0 -0
  41. data/lib/arel_extensions/nodes/coalesce.rb +0 -0
  42. data/lib/arel_extensions/nodes/collate.rb +9 -9
  43. data/lib/arel_extensions/nodes/concat.rb +2 -2
  44. data/lib/arel_extensions/nodes/date_diff.rb +6 -6
  45. data/lib/arel_extensions/nodes/duration.rb +0 -0
  46. data/lib/arel_extensions/nodes/find_in_set.rb +0 -0
  47. data/lib/arel_extensions/nodes/floor.rb +0 -0
  48. data/lib/arel_extensions/nodes/format.rb +1 -1
  49. data/lib/arel_extensions/nodes/formatted_number.rb +2 -2
  50. data/lib/arel_extensions/nodes/function.rb +21 -21
  51. data/lib/arel_extensions/nodes/is_null.rb +0 -0
  52. data/lib/arel_extensions/nodes/json.rb +7 -7
  53. data/lib/arel_extensions/nodes/length.rb +0 -0
  54. data/lib/arel_extensions/nodes/levenshtein_distance.rb +1 -1
  55. data/lib/arel_extensions/nodes/locate.rb +0 -0
  56. data/lib/arel_extensions/nodes/log10.rb +0 -0
  57. data/lib/arel_extensions/nodes/matches.rb +1 -1
  58. data/lib/arel_extensions/nodes/md5.rb +0 -0
  59. data/lib/arel_extensions/nodes/power.rb +0 -0
  60. data/lib/arel_extensions/nodes/rand.rb +0 -0
  61. data/lib/arel_extensions/nodes/repeat.rb +0 -0
  62. data/lib/arel_extensions/nodes/replace.rb +2 -2
  63. data/lib/arel_extensions/nodes/round.rb +0 -0
  64. data/lib/arel_extensions/nodes/soundex.rb +2 -2
  65. data/lib/arel_extensions/nodes/std.rb +0 -0
  66. data/lib/arel_extensions/nodes/substring.rb +0 -0
  67. data/lib/arel_extensions/nodes/sum.rb +0 -0
  68. data/lib/arel_extensions/nodes/then.rb +0 -0
  69. data/lib/arel_extensions/nodes/trim.rb +0 -0
  70. data/lib/arel_extensions/nodes/union.rb +4 -4
  71. data/lib/arel_extensions/nodes/union_all.rb +3 -3
  72. data/lib/arel_extensions/nodes/wday.rb +0 -0
  73. data/lib/arel_extensions/nodes.rb +0 -0
  74. data/lib/arel_extensions/null_functions.rb +0 -0
  75. data/lib/arel_extensions/predications.rb +10 -10
  76. data/lib/arel_extensions/railtie.rb +1 -1
  77. data/lib/arel_extensions/set_functions.rb +3 -3
  78. data/lib/arel_extensions/string_functions.rb +8 -8
  79. data/lib/arel_extensions/tasks.rb +2 -2
  80. data/lib/arel_extensions/version.rb +1 -1
  81. data/lib/arel_extensions/visitors/convert_format.rb +0 -0
  82. data/lib/arel_extensions/visitors/ibm_db.rb +19 -19
  83. data/lib/arel_extensions/visitors/mssql.rb +139 -118
  84. data/lib/arel_extensions/visitors/mysql.rb +145 -144
  85. data/lib/arel_extensions/visitors/oracle.rb +136 -128
  86. data/lib/arel_extensions/visitors/oracle12.rb +16 -16
  87. data/lib/arel_extensions/visitors/postgresql.rb +147 -136
  88. data/lib/arel_extensions/visitors/sqlite.rb +83 -85
  89. data/lib/arel_extensions/visitors/to_sql.rb +143 -148
  90. data/lib/arel_extensions/visitors.rb +7 -7
  91. data/lib/arel_extensions.rb +46 -32
  92. data/test/arelx_test_helper.rb +14 -14
  93. data/test/database.yml +5 -5
  94. data/test/real_db_test.rb +78 -78
  95. data/test/support/fake_record.rb +1 -1
  96. data/test/test_comparators.rb +5 -5
  97. data/test/visitors/test_bulk_insert_oracle.rb +4 -4
  98. data/test/visitors/test_bulk_insert_sqlite.rb +4 -4
  99. data/test/visitors/test_bulk_insert_to_sql.rb +4 -4
  100. data/test/visitors/test_oracle.rb +14 -14
  101. data/test/visitors/test_to_sql.rb +82 -82
  102. data/test/with_ar/all_agnostic_test.rb +415 -313
  103. data/test/with_ar/insert_agnostic_test.rb +17 -14
  104. data/test/with_ar/test_bulk_sqlite.rb +4 -4
  105. data/test/with_ar/test_math_sqlite.rb +12 -12
  106. data/test/with_ar/test_string_mysql.rb +20 -20
  107. data/test/with_ar/test_string_sqlite.rb +20 -20
  108. data/version_v1.rb +1 -1
  109. data/version_v2.rb +1 -1
  110. metadata +2 -9
  111. data/.travis/oracle/download.js +0 -152
  112. data/.travis/oracle/download.sh +0 -30
  113. data/.travis/oracle/download_ojdbc.js +0 -116
  114. data/.travis/oracle/install.sh +0 -34
  115. data/.travis/setup_accounts.sh +0 -9
  116. data/.travis/sqlite3/extension-functions.sh +0 -6
  117. data/.travis.yml +0 -193
@@ -1,5 +1,3 @@
1
- require 'arel_extensions/helpers'
2
-
3
1
  module ArelExtensions
4
2
  module Visitors
5
3
  class Arel::Visitors::SQLite
@@ -16,7 +14,7 @@ module ArelExtensions
16
14
  }.freeze
17
15
 
18
16
  NUMBER_COMMA_MAPPING = {
19
- 'fr_FR' => {',' => ' ', '.' =>','}
17
+ 'fr_FR' => {',' => ' ', '.' => ','}
20
18
  }.freeze
21
19
 
22
20
  # String functions
@@ -33,27 +31,27 @@ module ArelExtensions
33
31
  end
34
32
 
35
33
  def visit_ArelExtensions_Nodes_AiMatches o, collector
36
- collector = visit o.left.ai_collate, collector
37
- collector << ' LIKE '
38
- collector = visit o.right.ai_collate, collector
39
- if o.escape
40
- collector << ' ESCAPE '
41
- visit o.escape, collector
42
- else
43
- collector
44
- end
34
+ collector = visit o.left.ai_collate, collector
35
+ collector << ' LIKE '
36
+ collector = visit o.right.ai_collate, collector
37
+ if o.escape
38
+ collector << ' ESCAPE '
39
+ visit o.escape, collector
40
+ else
41
+ collector
42
+ end
45
43
  end
46
44
 
47
45
  def visit_ArelExtensions_Nodes_AiIMatches o, collector
48
- collector = visit o.left.collate(true,true), collector
49
- collector << ' LIKE '
50
- collector = visit o.right.collate(true,true), collector
51
- if o.escape
52
- collector << ' ESCAPE '
53
- visit o.escape, collector
54
- else
55
- collector
56
- end
46
+ collector = visit o.left.collate(true, true), collector
47
+ collector << ' LIKE '
48
+ collector = visit o.right.collate(true, true), collector
49
+ if o.escape
50
+ collector << ' ESCAPE '
51
+ visit o.escape, collector
52
+ else
53
+ collector
54
+ end
57
55
  end
58
56
 
59
57
  def visit_ArelExtensions_Nodes_SMatches o, collector
@@ -96,11 +94,11 @@ module ArelExtensions
96
94
 
97
95
  # Date operations
98
96
  def visit_ArelExtensions_Nodes_DateAdd o, collector
99
- collector << "date("
97
+ collector << 'date('
100
98
  collector = visit o.expressions.first, collector
101
99
  collector << COMMA
102
100
  collector = visit o.sqlite_value, collector
103
- collector << ")"
101
+ collector << ')'
104
102
  collector
105
103
  end
106
104
 
@@ -112,28 +110,28 @@ module ArelExtensions
112
110
  collector << ") - strftime('%s', "
113
111
  collector = visit o.right, collector
114
112
  else
115
- collector << "julianday("
113
+ collector << 'julianday('
116
114
  collector = visit o.left, collector
117
- collector << ") - julianday("
115
+ collector << ') - julianday('
118
116
  collector = visit o.right, collector
119
117
  end
120
- collector << ")"
118
+ collector << ')'
121
119
  collector
122
120
  end
123
121
 
124
122
  def visit_ArelExtensions_Nodes_Duration o, collector
125
123
  collector << "strftime('#{DATE_MAPPING[o.left]}'#{COMMA}"
126
124
  collector = visit o.right, collector
127
- collector << ")"
125
+ collector << ')'
128
126
  collector
129
127
  end
130
128
 
131
129
  def visit_ArelExtensions_Nodes_Locate o, collector
132
- collector << "instr("
130
+ collector << 'instr('
133
131
  collector = visit o.expr, collector
134
132
  collector << COMMA
135
133
  collector = visit o.right, collector
136
- collector << ")"
134
+ collector << ')'
137
135
  collector
138
136
  end
139
137
 
@@ -143,17 +141,17 @@ module ArelExtensions
143
141
  collector = visit arg, collector
144
142
  collector << ' || ' unless i == o.expressions.length - 1
145
143
  }
146
- collector << ")"
144
+ collector << ')'
147
145
  collector
148
146
  end
149
147
 
150
148
  def visit_ArelExtensions_Nodes_Substring o, collector
151
- collector << "SUBSTR("
149
+ collector << 'SUBSTR('
152
150
  o.expressions.each_with_index { |arg, i|
153
151
  collector << COMMA if i != 0
154
152
  collector = visit arg, collector
155
153
  }
156
- collector << ")"
154
+ collector << ')'
157
155
  collector
158
156
  end
159
157
 
@@ -164,32 +162,32 @@ module ArelExtensions
164
162
  end
165
163
 
166
164
  def visit_ArelExtensions_Nodes_IsNotNull o, collector
167
- collector = visit o.expr, collector
165
+ collector = visit o.expr, collector
168
166
  collector << ' IS NOT NULL'
169
167
  collector
170
168
  end
171
169
 
172
170
  def visit_ArelExtensions_Nodes_Rand o, collector
173
- collector << "RANDOM("
171
+ collector << 'RANDOM('
174
172
  if o.left != nil && o.right != nil
175
173
  collector = visit o.left, collector
176
174
  collector << COMMA
177
175
  collector = visit o.right, collector
178
176
  end
179
- collector << ")"
177
+ collector << ')'
180
178
  collector
181
179
  end
182
180
 
183
181
  def visit_Arel_Nodes_Regexp o, collector
184
182
  collector = visit o.left, collector
185
- collector << " REGEXP"
183
+ collector << ' REGEXP'
186
184
  collector = visit o.right, collector
187
185
  collector
188
186
  end
189
187
 
190
188
  def visit_Arel_Nodes_NotRegexp o, collector
191
189
  collector = visit o.left, collector
192
- collector << " NOT REGEXP "
190
+ collector << ' NOT REGEXP '
193
191
  collector = visit o.right, collector
194
192
  collector
195
193
  end
@@ -197,7 +195,7 @@ module ArelExtensions
197
195
  def visit_ArelExtensions_Nodes_Wday o, collector
198
196
  collector << "STRFTIME('%w',"
199
197
  collector = visit o.date, collector
200
- collector << ")"
198
+ collector << ')'
201
199
  collector
202
200
  end
203
201
 
@@ -210,32 +208,32 @@ module ArelExtensions
210
208
  # AS FLOAT
211
209
  # )
212
210
  def visit_ArelExtensions_Nodes_Floor o, collector
213
- collector << "CAST(CASE WHEN "
211
+ collector << 'CAST(CASE WHEN '
214
212
  collector = visit o.left, collector
215
- collector << " >= 0 THEN CAST("
213
+ collector << ' >= 0 THEN CAST('
216
214
  collector = visit o.left, collector
217
- collector << " AS INT) WHEN CAST("
215
+ collector << ' AS INT) WHEN CAST('
218
216
  collector = visit o.left, collector
219
- collector << " AS INT) = "
217
+ collector << ' AS INT) = '
220
218
  collector = visit o.left, collector
221
- collector << " THEN CAST("
219
+ collector << ' THEN CAST('
222
220
  collector = visit o.left, collector
223
- collector << " AS INT) ELSE CAST(("
221
+ collector << ' AS INT) ELSE CAST(('
224
222
  collector = visit o.left, collector
225
- collector << " - 1.0) AS INT) END AS FLOAT)"
223
+ collector << ' - 1.0) AS INT) END AS FLOAT)'
226
224
  collector
227
225
  end
228
226
 
229
227
  def visit_ArelExtensions_Nodes_Ceil o, collector
230
- collector << "CASE WHEN ROUND("
228
+ collector << 'CASE WHEN ROUND('
231
229
  collector = visit o.left, collector
232
- collector << ", 1) > ROUND("
230
+ collector << ', 1) > ROUND('
233
231
  collector = visit o.left, collector
234
- collector << ") THEN ROUND("
232
+ collector << ') THEN ROUND('
235
233
  collector = visit o.left, collector
236
- collector << ") + 1 ELSE ROUND("
234
+ collector << ') + 1 ELSE ROUND('
237
235
  collector = visit o.left, collector
238
- collector << ") END"
236
+ collector << ') END'
239
237
  collector
240
238
  end
241
239
 
@@ -245,16 +243,16 @@ module ArelExtensions
245
243
  collector << 'SELECT '
246
244
  len = row.length - 1
247
245
  row.zip(o.cols).each_with_index { |(value, attr), i|
248
- case value
249
- when Arel::Nodes::SqlLiteral, Arel::Nodes::BindParam
250
- collector = visit value.as(attr.name), collector
251
- else
252
- collector << quote(value, attr && column_for(attr)).to_s
253
- if idx == 0
254
- collector << " AS "
255
- collector << quote(attr.name)
256
- end
246
+ case value
247
+ when Arel::Nodes::SqlLiteral, Arel::Nodes::BindParam
248
+ collector = visit value.as(attr.name), collector
249
+ else
250
+ collector << quote(value, attr && column_for(attr)).to_s
251
+ if idx == 0
252
+ collector << ' AS '
253
+ collector << quote(attr.name)
257
254
  end
255
+ end
258
256
  collector << COMMA unless i == len
259
257
  }
260
258
  collector << ' UNION ALL ' unless idx == o.left.length - 1
@@ -267,22 +265,22 @@ module ArelExtensions
267
265
  collector << 'SELECT '
268
266
  len = row.length - 1
269
267
  row.zip(o.cols).each_with_index { |(value, attr), i|
270
- case value
271
- when Arel::Nodes::SqlLiteral, Arel::Nodes::BindParam
272
- collector = visit value.as(attr.name), collector
273
- when Integer
274
- collector << value.to_s
275
- if idx == 0
276
- collector << " AS "
277
- collector << quote(attr.name)
278
- end
279
- else
280
- collector << (attr && attr.able_to_type_cast? ? quote(attr.type_cast_for_database(value)) : quote(value).to_s)
281
- if idx == 0
282
- collector << " AS "
283
- collector << quote(attr.name)
284
- end
268
+ case value
269
+ when Arel::Nodes::SqlLiteral, Arel::Nodes::BindParam
270
+ collector = visit value.as(attr.name), collector
271
+ when Integer
272
+ collector << value.to_s
273
+ if idx == 0
274
+ collector << ' AS '
275
+ collector << quote(attr.name)
276
+ end
277
+ else
278
+ collector << (attr && attr.able_to_type_cast? ? quote(attr.type_cast_for_database(value)) : quote(value).to_s)
279
+ if idx == 0
280
+ collector << ' AS '
281
+ collector << quote(attr.name)
285
282
  end
283
+ end
286
284
  collector << COMMA unless i == len
287
285
  }
288
286
  collector << ' UNION ALL ' unless idx == o.left.length - 1
@@ -298,7 +296,7 @@ module ArelExtensions
298
296
  else
299
297
  visit o.left, collector
300
298
  end
301
- collector << " UNION "
299
+ collector << ' UNION '
302
300
  collector =
303
301
  if o.right.is_a?(Arel::SelectManager)
304
302
  visit o.right.ast, collector
@@ -315,7 +313,7 @@ module ArelExtensions
315
313
  else
316
314
  visit o.left, collector
317
315
  end
318
- collector << " UNION ALL "
316
+ collector << ' UNION ALL '
319
317
  collector =
320
318
  if o.right.is_a?(Arel::SelectManager)
321
319
  visit o.right.ast, collector
@@ -327,11 +325,11 @@ module ArelExtensions
327
325
 
328
326
  def get_time_converted element
329
327
  if element.is_a?(Time)
330
- return Arel::Nodes::NamedFunction.new('STRFTIME',[element, '%H:%M:%S'])
328
+ return Arel::Nodes::NamedFunction.new('STRFTIME', [element, '%H:%M:%S'])
331
329
  elsif element.is_a?(Arel::Attributes::Attribute)
332
- col = ArelExtensions::column_of(element.relation.table_name, element.name.to_s)
330
+ col = Arel.column_of(element.relation.table_name, element.name.to_s)
333
331
  if col && (col.type == :time)
334
- return Arel::Nodes::NamedFunction.new('STRFTIME',[element, '%H:%M:%S'])
332
+ return Arel::Nodes::NamedFunction.new('STRFTIME', [element, '%H:%M:%S'])
335
333
  else
336
334
  return element
337
335
  end
@@ -343,7 +341,7 @@ module ArelExtensions
343
341
  remove_method(:visit_Arel_Nodes_GreaterThanOrEqual) rescue nil
344
342
  def visit_Arel_Nodes_GreaterThanOrEqual o, collector
345
343
  collector = visit get_time_converted(o.left), collector
346
- collector << " >= "
344
+ collector << ' >= '
347
345
  collector = visit get_time_converted(o.right), collector
348
346
  collector
349
347
  end
@@ -351,7 +349,7 @@ module ArelExtensions
351
349
  remove_method(:visit_Arel_Nodes_GreaterThan) rescue nil
352
350
  def visit_Arel_Nodes_GreaterThan o, collector
353
351
  collector = visit get_time_converted(o.left), collector
354
- collector << " > "
352
+ collector << ' > '
355
353
  collector = visit get_time_converted(o.right), collector
356
354
  collector
357
355
  end
@@ -359,7 +357,7 @@ module ArelExtensions
359
357
  remove_method(:visit_Arel_Nodes_LessThanOrEqual) rescue nil
360
358
  def visit_Arel_Nodes_LessThanOrEqual o, collector
361
359
  collector = visit get_time_converted(o.left), collector
362
- collector << " <= "
360
+ collector << ' <= '
363
361
  collector = visit get_time_converted(o.right), collector
364
362
  collector
365
363
  end
@@ -367,7 +365,7 @@ module ArelExtensions
367
365
  remove_method(:visit_Arel_Nodes_LessThan) rescue nil
368
366
  def visit_Arel_Nodes_LessThan o, collector
369
367
  collector = visit get_time_converted(o.left), collector
370
- collector << " < "
368
+ collector << ' < '
371
369
  collector = visit get_time_converted(o.right), collector
372
370
  collector
373
371
  end
@@ -389,10 +387,10 @@ module ArelExtensions
389
387
  end
390
388
 
391
389
  def visit_ArelExtensions_Nodes_FormattedNumber o, collector
392
- format = Arel::Nodes::NamedFunction.new('printf',[Arel::Nodes.build_quoted(o.original_string),o.left])
390
+ format = Arel::Nodes::NamedFunction.new('printf', [Arel.quoted(o.original_string), o.left])
393
391
  locale_map = NUMBER_COMMA_MAPPING[o.locale]
394
392
  if locale_map
395
- format = format.replace(',',locale_map[',']).replace('.',locale_map['.'])
393
+ format = format.replace(',', locale_map[',']).replace('.', locale_map['.'])
396
394
  end
397
395
  visit format, collector
398
396
  collector