arel_extensions 1.4.0 → 2.0.0.rc3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (140) 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 +28 -2
  13. data/README.md +90 -239
  14. data/Rakefile +30 -48
  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 -15
  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 -52
  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 +191 -422
  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 -298
  89. data/lib/arel_extensions/visitors.rb +62 -83
  90. data/lib/arel_extensions.rb +31 -224
  91. data/test/database.yml +7 -15
  92. data/test/helper.rb +18 -0
  93. data/test/real_db_test.rb +117 -120
  94. data/test/support/fake_record.rb +3 -9
  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 +366 -709
  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 -43
  108. data/.github/workflows/publish.yml +0 -29
  109. data/.github/workflows/release.yml +0 -30
  110. data/.github/workflows/ruby.yml +0 -399
  111. data/CONTRIBUTING.md +0 -102
  112. data/NEWS.md +0 -78
  113. data/bin/build +0 -15
  114. data/bin/compose +0 -6
  115. data/bin/publish +0 -8
  116. data/dev/arelx.dockerfile +0 -44
  117. data/dev/compose.yaml +0 -71
  118. data/dev/postgres.dockerfile +0 -5
  119. data/dev/rbenv +0 -189
  120. data/gemfiles/rails4_2.gemfile +0 -38
  121. data/gemfiles/rails5.gemfile +0 -29
  122. data/gemfiles/rails6.gemfile +0 -33
  123. data/gemfiles/rails6_1.gemfile +0 -33
  124. data/gemfiles/rails7.gemfile +0 -33
  125. data/gemfiles/rails7_1.gemfile +0 -33
  126. data/gemfiles/rails7_2.gemfile +0 -33
  127. data/gemspecs/arel_extensions-v1.gemspec +0 -27
  128. data/gemspecs/arel_extensions-v2.gemspec +0 -27
  129. data/generate_gems.sh +0 -15
  130. data/lib/arel_extensions/aliases.rb +0 -14
  131. data/lib/arel_extensions/helpers.rb +0 -62
  132. data/lib/arel_extensions/nodes/aggregate_function.rb +0 -13
  133. data/lib/arel_extensions/nodes/formatted_date.rb +0 -42
  134. data/lib/arel_extensions/nodes/rollup.rb +0 -36
  135. data/lib/arel_extensions/nodes/select.rb +0 -10
  136. data/lib/arel_extensions/nodes/sum.rb +0 -7
  137. data/lib/arel_extensions/visitors/convert_format.rb +0 -37
  138. data/test/arelx_test_helper.rb +0 -74
  139. data/version_v1.rb +0 -3
  140. data/version_v2.rb +0 -3
@@ -1,23 +1,16 @@
1
1
  module ArelExtensions
2
2
  module Visitors
3
- class Arel::Visitors::SQLite
4
- DATE_MAPPING = {
5
- 'd' => '%d', 'm' => '%m', 'w' => '%W', 'y' => '%Y', 'wd' => '%w', 'M' => '%M',
6
- 'h' => '%H', 'mn' => '%M', 's' => '%S'
7
- }.freeze
8
-
9
- DATE_FORMAT_DIRECTIVES = { # ISO C / POSIX
3
+ Arel::Visitors::SQLite.class_eval do
4
+ Arel::Visitors::SQLite::DATE_MAPPING = {'d' => '%d', 'm' => '%m', 'w' => '%W', 'y' => '%Y', 'wd' => '%w', 'M' => '%M', 'h' => '%H', 'mn' => '%M', 's' => '%S'}
5
+ Arel::Visitors::SQLite::DATE_FORMAT_DIRECTIVES = { # ISO C / POSIX
10
6
  '%Y' => '%Y', '%C' => '', '%y' => '%y', '%m' => '%m', '%B' => '%M', '%b' => '%b', '%^b' => '%b', # year, month
11
7
  '%d' => '%d', '%e' => '%e', '%j' => '%j', '%w' => '%w', '%A' => '%W', # day, weekday
12
8
  '%H' => '%H', '%k' => '%k', '%I' => '%I', '%l' => '%l', '%P' => '%p', '%p' => '%p', # hours
13
9
  '%M' => '%M', '%S' => '%S', '%L' => '', '%N' => '%f', '%z' => '' # seconds, subseconds
14
- }.freeze
15
-
16
- NUMBER_COMMA_MAPPING = {
17
- 'fr_FR' => {',' => ' ', '.' => ','}
18
- }.freeze
10
+ }
11
+ Arel::Visitors::SQLite::NUMBER_COMMA_MAPPING = { 'fr_FR' => {',' => ' ','.' =>','} }
19
12
 
20
- # String functions
13
+ #String functions
21
14
  def visit_ArelExtensions_Nodes_IMatches o, collector # insensitive on ASCII
22
15
  collector = visit o.left.ci_collate, collector
23
16
  collector << ' LIKE '
@@ -31,27 +24,27 @@ module ArelExtensions
31
24
  end
32
25
 
33
26
  def visit_ArelExtensions_Nodes_AiMatches o, collector
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
27
+ collector = visit o.left.ai_collate, collector
28
+ collector << ' LIKE '
29
+ collector = visit o.right.ai_collate, collector
30
+ if o.escape
31
+ collector << ' ESCAPE '
32
+ visit o.escape, collector
33
+ else
34
+ collector
35
+ end
43
36
  end
44
37
 
45
38
  def visit_ArelExtensions_Nodes_AiIMatches o, collector
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
39
+ collector = visit o.left.collate(true,true), collector
40
+ collector << ' LIKE '
41
+ collector = visit o.right.collate(true,true), collector
42
+ if o.escape
43
+ collector << ' ESCAPE '
44
+ visit o.escape, collector
45
+ else
46
+ collector
47
+ end
55
48
  end
56
49
 
57
50
  def visit_ArelExtensions_Nodes_SMatches o, collector
@@ -80,6 +73,7 @@ module ArelExtensions
80
73
  collector
81
74
  end
82
75
 
76
+
83
77
  def visit_ArelExtensions_Nodes_IDoesNotMatch o, collector
84
78
  collector = visit o.left.lower, collector
85
79
  collector << ' NOT LIKE '
@@ -94,44 +88,44 @@ module ArelExtensions
94
88
 
95
89
  # Date operations
96
90
  def visit_ArelExtensions_Nodes_DateAdd o, collector
97
- collector << 'date('
91
+ collector << "date("
98
92
  collector = visit o.expressions.first, collector
99
- collector << COMMA
93
+ collector << Arel::Visitors::SQLite::COMMA
100
94
  collector = visit o.sqlite_value, collector
101
- collector << ')'
95
+ collector << ")"
102
96
  collector
103
97
  end
104
98
 
105
99
  def visit_ArelExtensions_Nodes_DateDiff o, collector
106
- case o.left_node_type
107
- when :ruby_time, :datetime, :time
100
+ if o.left_node_type == :ruby_time || o.left_node_type == :datetime || o.left_node_type == :time
108
101
  collector << "strftime('%s', "
109
102
  collector = visit o.left, collector
110
103
  collector << ") - strftime('%s', "
111
104
  collector = visit o.right, collector
112
105
  else
113
- collector << 'julianday('
106
+ collector << "julianday("
114
107
  collector = visit o.left, collector
115
- collector << ') - julianday('
108
+ collector << ") - julianday("
116
109
  collector = visit o.right, collector
117
110
  end
118
- collector << ')'
111
+ collector << ")"
119
112
  collector
120
113
  end
121
114
 
122
115
  def visit_ArelExtensions_Nodes_Duration o, collector
123
- collector << "strftime('#{DATE_MAPPING[o.left]}'#{COMMA}"
116
+ collector << "strftime('#{Arel::Visitors::SQLite::DATE_MAPPING[o.left]}'#{Arel::Visitors::SQLite::COMMA}"
124
117
  collector = visit o.right, collector
125
- collector << ')'
118
+ collector << ")"
126
119
  collector
127
120
  end
128
121
 
122
+
129
123
  def visit_ArelExtensions_Nodes_Locate o, collector
130
- collector << 'instr('
124
+ collector << "instr("
131
125
  collector = visit o.expr, collector
132
- collector << COMMA
126
+ collector << Arel::Visitors::SQLite::COMMA
133
127
  collector = visit o.right, collector
134
- collector << ')'
128
+ collector << ")"
135
129
  collector
136
130
  end
137
131
 
@@ -141,17 +135,17 @@ module ArelExtensions
141
135
  collector = visit arg, collector
142
136
  collector << ' || ' unless i == o.expressions.length - 1
143
137
  }
144
- collector << ')'
138
+ collector << ")"
145
139
  collector
146
140
  end
147
141
 
148
142
  def visit_ArelExtensions_Nodes_Substring o, collector
149
- collector << 'SUBSTR('
143
+ collector << "SUBSTR("
150
144
  o.expressions.each_with_index { |arg, i|
151
- collector << COMMA if i != 0
145
+ collector << Arel::Visitors::SQLite::COMMA unless i == 0
152
146
  collector = visit arg, collector
153
147
  }
154
- collector << ')'
148
+ collector << ")"
155
149
  collector
156
150
  end
157
151
 
@@ -162,32 +156,32 @@ module ArelExtensions
162
156
  end
163
157
 
164
158
  def visit_ArelExtensions_Nodes_IsNotNull o, collector
165
- collector = visit o.expr, collector
159
+ collector = visit o.expr, collector
166
160
  collector << ' IS NOT NULL'
167
161
  collector
168
162
  end
169
163
 
170
164
  def visit_ArelExtensions_Nodes_Rand o, collector
171
- collector << 'RANDOM('
165
+ collector << "RANDOM("
172
166
  if o.left != nil && o.right != nil
173
167
  collector = visit o.left, collector
174
- collector << COMMA
168
+ collector << Arel::Visitors::SQLite::COMMA
175
169
  collector = visit o.right, collector
176
170
  end
177
- collector << ')'
171
+ collector << ")"
178
172
  collector
179
173
  end
180
174
 
181
175
  def visit_Arel_Nodes_Regexp o, collector
182
176
  collector = visit o.left, collector
183
- collector << ' REGEXP'
177
+ collector << " REGEXP"
184
178
  collector = visit o.right, collector
185
179
  collector
186
180
  end
187
181
 
188
182
  def visit_Arel_Nodes_NotRegexp o, collector
189
183
  collector = visit o.left, collector
190
- collector << ' NOT REGEXP '
184
+ collector << " NOT REGEXP "
191
185
  collector = visit o.right, collector
192
186
  collector
193
187
  end
@@ -195,45 +189,35 @@ module ArelExtensions
195
189
  def visit_ArelExtensions_Nodes_Wday o, collector
196
190
  collector << "STRFTIME('%w',"
197
191
  collector = visit o.date, collector
198
- collector << ')'
192
+ collector << ")"
199
193
  collector
200
194
  end
201
195
 
202
- # CAST(
203
- # CASE
204
- # WHEN 3.42 >= 0 THEN CAST(3.42 AS INT)
205
- # WHEN CAST(3.42 AS INT) = 3.42 THEN CAST(3.42 AS INT)
206
- # ELSE CAST((3.42 - 1.0) AS INT)
207
- # END
208
- # AS FLOAT
209
- # )
196
+ # CASE WHEN ROUND(3.42,1) > round(3.42) THEN round(3.42) ELSE round(3.42)-1 END
197
+ # OR CAST(3.14 AS INTEGER)
210
198
  def visit_ArelExtensions_Nodes_Floor o, collector
211
- collector << 'CAST(CASE WHEN '
212
- collector = visit o.left, collector
213
- collector << ' >= 0 THEN CAST('
199
+ collector << "CASE WHEN ROUND("
214
200
  collector = visit o.left, collector
215
- collector << ' AS INT) WHEN CAST('
201
+ collector << ", 1) > ROUND("
216
202
  collector = visit o.left, collector
217
- collector << ' AS INT) = '
203
+ collector << ") THEN ROUND("
218
204
  collector = visit o.left, collector
219
- collector << ' THEN CAST('
205
+ collector << ") ELSE ROUND("
220
206
  collector = visit o.left, collector
221
- collector << ' AS INT) ELSE CAST(('
222
- collector = visit o.left, collector
223
- collector << ' - 1.0) AS INT) END AS FLOAT)'
207
+ collector << ") - 1 END"
224
208
  collector
225
209
  end
226
210
 
227
211
  def visit_ArelExtensions_Nodes_Ceil o, collector
228
- collector << 'CASE WHEN ROUND('
212
+ collector << "CASE WHEN ROUND("
229
213
  collector = visit o.left, collector
230
- collector << ', 1) > ROUND('
214
+ collector << ", 1) > ROUND("
231
215
  collector = visit o.left, collector
232
- collector << ') THEN ROUND('
216
+ collector << ") THEN ROUND("
233
217
  collector = visit o.left, collector
234
- collector << ') + 1 ELSE ROUND('
218
+ collector << ") + 1 ELSE ROUND("
235
219
  collector = visit o.left, collector
236
- collector << ') END'
220
+ collector << ") END"
237
221
  collector
238
222
  end
239
223
 
@@ -241,19 +225,20 @@ module ArelExtensions
241
225
  def visit_ArelExtensions_InsertManager_BulkValues o, collector
242
226
  o.left.each_with_index do |row, idx|
243
227
  collector << 'SELECT '
244
- len = row.length - 1
245
- row.zip(o.cols).each_with_index { |(value, attr), i|
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)
228
+ v = Arel::Nodes::Values.new(row, o.cols)
229
+ len = v.expressions.length - 1
230
+ v.expressions.zip(v.columns).each_with_index { |(value, attr), i|
231
+ case value
232
+ when Arel::Nodes::SqlLiteral, Arel::Nodes::BindParam
233
+ collector = visit value.as(attr.name), collector
234
+ else
235
+ collector << quote(value, attr && column_for(attr)).to_s
236
+ if idx == 0
237
+ collector << " AS "
238
+ collector << quote(attr.name)
239
+ end
254
240
  end
255
- end
256
- collector << COMMA unless i == len
241
+ collector << Arel::Visitors::SQLite::COMMA unless i == len
257
242
  }
258
243
  collector << ' UNION ALL ' unless idx == o.left.length - 1
259
244
  end
@@ -264,24 +249,19 @@ module ArelExtensions
264
249
  o.left.each_with_index do |row, idx|
265
250
  collector << 'SELECT '
266
251
  len = row.length - 1
267
- row.zip(o.cols).each_with_index { |(value, attr), i|
252
+ row.each_with_index { |value, i|
253
+ attr = o.cols[i]
268
254
  case value
269
255
  when Arel::Nodes::SqlLiteral, Arel::Nodes::BindParam
270
256
  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
257
  else
278
258
  collector << (attr && attr.able_to_type_cast? ? quote(attr.type_cast_for_database(value)) : quote(value).to_s)
279
259
  if idx == 0
280
- collector << ' AS '
260
+ collector << " AS "
281
261
  collector << quote(attr.name)
282
262
  end
283
263
  end
284
- collector << COMMA unless i == len
264
+ collector << Arel::Visitors::SQLite::COMMA unless i == len
285
265
  }
286
266
  collector << ' UNION ALL ' unless idx == o.left.length - 1
287
267
  end
@@ -290,58 +270,54 @@ module ArelExtensions
290
270
  end
291
271
 
292
272
  def visit_ArelExtensions_Nodes_Union o, collector
293
- collector =
294
- if o.left.is_a?(Arel::SelectManager)
295
- visit o.left.ast, collector
296
- else
297
- visit o.left, collector
298
- end
299
- collector << ' UNION '
300
- collector =
301
- if o.right.is_a?(Arel::SelectManager)
302
- visit o.right.ast, collector
303
- else
304
- visit o.right, collector
305
- end
273
+ if o.left.is_a?(Arel::SelectManager)
274
+ collector = visit o.left.ast, collector
275
+ else
276
+ collector = visit o.left, collector
277
+ end
278
+ collector << " UNION "
279
+ if o.right.is_a?(Arel::SelectManager)
280
+ collector = visit o.right.ast, collector
281
+ else
282
+ collector = visit o.right, collector
283
+ end
306
284
  collector
307
285
  end
308
286
 
309
287
  def visit_ArelExtensions_Nodes_UnionAll o, collector
310
- collector =
311
- if o.left.is_a?(Arel::SelectManager)
312
- visit o.left.ast, collector
313
- else
314
- visit o.left, collector
315
- end
316
- collector << ' UNION ALL '
317
- collector =
318
- if o.right.is_a?(Arel::SelectManager)
319
- visit o.right.ast, collector
320
- else
321
- visit o.right, collector
322
- end
288
+ if o.left.is_a?(Arel::SelectManager)
289
+ collector = visit o.left.ast, collector
290
+ else
291
+ collector = visit o.left, collector
292
+ end
293
+ collector << " UNION ALL "
294
+ if o.right.is_a?(Arel::SelectManager)
295
+ collector = visit o.right.ast, collector
296
+ else
297
+ collector = visit o.right, collector
298
+ end
323
299
  collector
324
300
  end
325
301
 
326
302
  def get_time_converted element
327
303
  if element.is_a?(Time)
328
- Arel::Nodes::NamedFunction.new('STRFTIME', [element, '%H:%M:%S'])
304
+ return Arel::Nodes::NamedFunction.new('STRFTIME',[element, '%H:%M:%S'])
329
305
  elsif element.is_a?(Arel::Attributes::Attribute)
330
- col = Arel.column_of(element.relation.table_name, element.name.to_s)
306
+ col = Arel::Table.engine.connection.schema_cache.columns_hash(element.relation.table_name)[element.name.to_s]
331
307
  if col && (col.type == :time)
332
- Arel::Nodes::NamedFunction.new('STRFTIME', [element, '%H:%M:%S'])
308
+ return Arel::Nodes::NamedFunction.new('STRFTIME',[element, '%H:%M:%S'])
333
309
  else
334
- element
310
+ return element
335
311
  end
336
312
  else
337
- element
313
+ return element
338
314
  end
339
315
  end
340
316
 
341
317
  remove_method(:visit_Arel_Nodes_GreaterThanOrEqual) rescue nil
342
318
  def visit_Arel_Nodes_GreaterThanOrEqual o, collector
343
319
  collector = visit get_time_converted(o.left), collector
344
- collector << ' >= '
320
+ collector << " >= "
345
321
  collector = visit get_time_converted(o.right), collector
346
322
  collector
347
323
  end
@@ -349,7 +325,7 @@ module ArelExtensions
349
325
  remove_method(:visit_Arel_Nodes_GreaterThan) rescue nil
350
326
  def visit_Arel_Nodes_GreaterThan o, collector
351
327
  collector = visit get_time_converted(o.left), collector
352
- collector << ' > '
328
+ collector << " > "
353
329
  collector = visit get_time_converted(o.right), collector
354
330
  collector
355
331
  end
@@ -357,7 +333,7 @@ module ArelExtensions
357
333
  remove_method(:visit_Arel_Nodes_LessThanOrEqual) rescue nil
358
334
  def visit_Arel_Nodes_LessThanOrEqual o, collector
359
335
  collector = visit get_time_converted(o.left), collector
360
- collector << ' <= '
336
+ collector << " <= "
361
337
  collector = visit get_time_converted(o.right), collector
362
338
  collector
363
339
  end
@@ -365,12 +341,22 @@ module ArelExtensions
365
341
  remove_method(:visit_Arel_Nodes_LessThan) rescue nil
366
342
  def visit_Arel_Nodes_LessThan o, collector
367
343
  collector = visit get_time_converted(o.left), collector
368
- collector << ' < '
344
+ collector << " < "
369
345
  collector = visit get_time_converted(o.right), collector
370
346
  collector
371
347
  end
372
348
 
373
- alias_method(:old_visit_Arel_Nodes_As, :visit_Arel_Nodes_As) rescue nil
349
+
350
+ alias_method :old_visit_Arel_Nodes_SelectStatement, :visit_Arel_Nodes_SelectStatement
351
+ def visit_Arel_Nodes_SelectStatement o, collector
352
+ if !collector.value.blank? && o.limit.blank?
353
+ o = o.dup
354
+ o.orders = []
355
+ end
356
+ old_visit_Arel_Nodes_SelectStatement(o,collector)
357
+ end
358
+
359
+ alias_method :old_visit_Arel_Nodes_As, :visit_Arel_Nodes_As
374
360
  def visit_Arel_Nodes_As o, collector
375
361
  if o.left.is_a?(Arel::Nodes::Binary)
376
362
  collector << '('
@@ -379,22 +365,22 @@ module ArelExtensions
379
365
  else
380
366
  collector = visit o.left, collector
381
367
  end
382
- sep = o.right.size > 1 && o.right[0] == '"' && o.right[-1] == '"' ? '' : '"'
383
- collector << " AS #{sep}"
368
+ collector << " AS \""
384
369
  collector = visit o.right, collector
385
- collector << "#{sep}"
370
+ collector << "\""
386
371
  collector
387
372
  end
388
373
 
389
374
  def visit_ArelExtensions_Nodes_FormattedNumber o, collector
390
- format = Arel::Nodes::NamedFunction.new('printf', [Arel.quoted(o.original_string), o.left])
391
- locale_map = NUMBER_COMMA_MAPPING[o.locale]
375
+ format = Arel::Nodes::NamedFunction.new('printf',[Arel::Nodes.build_quoted(o.original_string),o.left])
376
+ locale_map = Arel::Visitors::SQLite::NUMBER_COMMA_MAPPING[o.locale]
392
377
  if locale_map
393
- format = format.replace(',', locale_map[',']).replace('.', locale_map['.'])
378
+ format = format.replace(',',locale_map[',']).replace('.',locale_map['.'])
394
379
  end
395
380
  visit format, collector
396
381
  collector
397
382
  end
383
+
398
384
  end
399
385
  end
400
386
  end