Class: Arel::Visitors::MySQL

Inherits:
Object
  • Object
show all
Defined in:
lib/arel_extensions/visitors/mysql.rb

Constant Summary collapse

DATE_MAPPING =
{
  'd' => 'DAY', 'm' => 'MONTH', 'w' => 'WEEK', 'y' => 'YEAR', 'wd' => 'WEEKDAY',
  'h' => 'HOUR', 'mn' => 'MINUTE', 's' => 'SECOND'
}.freeze
DATE_FORMAT_DIRECTIVES =

ISO C / POSIX

{ # ISO C / POSIX
  '%Y' => '%Y', '%C' =>   '', '%y' => '%y', '%m' => '%m', '%B' => '%M', '%b' => '%b', '%^b' => '%b',  # year, month
  '%V' => '%v', '%G' => '%x',                                                                         # ISO week number and year of week
  '%d' => '%d', '%e' => '%e', '%j' => '%j', '%w' => '%w', '%a' => '%a', '%A' => '%W',                 # day, weekday
  '%H' => '%H', '%k' => '%k', '%I' => '%I', '%l' => '%l', '%P' => '%p', '%p' => '%p',                 # hours
  '%M' => '%i', '%S' => '%S', '%L' =>   '', '%N' => '%f', '%z' => ''
}.freeze

Instance Method Summary collapse

Instance Method Details

#collect_nodes_for(nodes, collector, spacer, connector = ", ") ⇒ Object



19
20
21
22
23
24
# File 'lib/arel_extensions/visitors/mysql.rb', line 19

def collect_nodes_for(nodes, collector, spacer, connector = ", ")
  if nodes&.any?
    collector << spacer
    inject_join nodes, collector, connector
  end
end

#json_supported?Boolean

JSON if implemented only after 10.2.3 (aggregations after 10.5.0) in MariaDb and 5.7 (aggregations after 5.7.22) in MySql

Returns:

  • (Boolean)


535
536
537
# File 'lib/arel_extensions/visitors/mysql.rb', line 535

def json_supported?
  version_supported?('10.5.0', '5.7.22')
end

#regexp_replace_supported?Boolean

Returns:

  • (Boolean)


543
544
545
# File 'lib/arel_extensions/visitors/mysql.rb', line 543

def regexp_replace_supported?
  version_supported?('10.0.5', '8.0')
end

#version_supported?(mariadb_v = '10.2.3', mysql_v = '5.7.0') ⇒ Boolean

Returns:

  • (Boolean)


547
548
549
550
551
552
553
554
555
556
557
558
559
# File 'lib/arel_extensions/visitors/mysql.rb', line 547

def version_supported?(mariadb_v = '10.2.3', mysql_v = '5.7.0')
  conn = Arel::Table.engine.connection
  conn.send(:mariadb?) && \
    (conn.respond_to?(:get_database_version) && conn.send(:get_database_version) >= mariadb_v || \
    conn.respond_to?(:version) && conn.send(:version) >= mariadb_v || \
    conn.instance_variable_get(:"@version") && conn.instance_variable_get(:"@version") >= mariadb_v) || \
    !conn.send(:mariadb?) && \
      (conn.respond_to?(:get_database_version) && conn.send(:get_database_version) >= mysql_v || \
      conn.respond_to?(:version) && conn.send(:version) >= mysql_v || \
      conn.instance_variable_get(:"@version") && conn.instance_variable_get(:"@version") >= mysql_v)
  # ideally we should parse the instance_variable @full_version because @version contains only the supposedly
  # corresponding mysql version of the current mariadb version (which is not very helpful most of the time)
end

#visit_Aggregate_For_AggregateFunction(o, collector) ⇒ Object



495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
# File 'lib/arel_extensions/visitors/mysql.rb', line 495

def visit_Aggregate_For_AggregateFunction o, collector
  if !window_supported?
    warn('Warning: ArelExtensions: Window Functions are not available in the current version of the DBMS.')
    return collector
  end

  if !o.order.empty? || !o.group.empty?
    collector << ' OVER ('
    if !o.group.empty?
      collector << ' PARTITION BY ('
      visit o.group, collector
      collector << ')'
    end
    if !o.order.empty?
      collector << ' ORDER BY ('
      visit o.order, collector
      collector << ')'
    end
    collector << ')'
  end
  collector
end

#visit_Arel_Nodes_As(o, collector) ⇒ Object



425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
# File 'lib/arel_extensions/visitors/mysql.rb', line 425

def visit_Arel_Nodes_As o, collector
  if o.left.is_a?(Arel::Nodes::Binary)
    collector << '('
    collector = visit o.left, collector
    collector << ')'
  else
    collector = visit o.left, collector
  end
  collector << ' AS '

  # sometimes these values are already quoted, if they are, don't double quote it
  quote = o.right.is_a?(Arel::Nodes::SqlLiteral) && o.right[0] != '`' && o.right[-1] != '`'

  collector << '`' if quote
  collector = visit o.right, collector
  collector << '`' if quote

  collector
end

#visit_Arel_Nodes_RollUp(o, collector) ⇒ Object



187
188
189
190
# File 'lib/arel_extensions/visitors/mysql.rb', line 187

def visit_Arel_Nodes_RollUp(o, collector)
  visit o.expr, collector
  collector << " WITH ROLLUP"
end

#visit_Arel_Nodes_SelectCore(o, collector) ⇒ Object

The whole purpose of this override is to fix the behavior of RollUp. All other databases treat RollUp sanely, execpt MySQL which requires that it figures as the last element of a GROUP BY.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/arel_extensions/visitors/mysql.rb', line 30

def visit_Arel_Nodes_SelectCore(o, collector)
  collector << "SELECT"

  collector = collect_optimizer_hints(o, collector) if self.respond_to?(:collect_optimizer_hinsts)
  collector = maybe_visit o.set_quantifier, collector

  collect_nodes_for o.projections, collector, " "

  if o.source && !o.source.empty?
    collector << " FROM "
    collector = visit o.source, collector
  end

  # The actual work
  groups = o.groups
  rollup = groups.select { |g| g.expr.class == Arel::Nodes::RollUp }.map { |r| r.expr.value }
  if rollup && !rollup.empty?
    groups = o.groups.reject { |g| g.expr.class == Arel::Nodes::RollUp }
    groups << Arel::Nodes::RollUp.new(rollup)
  end
  # FIN

  collect_nodes_for o.wheres, collector, " WHERE ", " AND "
  collect_nodes_for groups, collector, " GROUP BY "            # Look ma, I'm viring a group
  collect_nodes_for o.havings, collector, " HAVING ", " AND "
  collect_nodes_for o.windows, collector, " WINDOW "

  if o.respond_to?(:comment)
    maybe_visit o.comment, collector 
  else
    collector
  end
end

#visit_Arel_Nodes_SelectStatement(o, collector) ⇒ Object



416
417
418
419
420
421
422
# File 'lib/arel_extensions/visitors/mysql.rb', line 416

def visit_Arel_Nodes_SelectStatement o, collector
  if !(collector.value.blank? || (collector.value.is_a?(Array) && collector.value[0].blank?)) && o.limit.blank? && o.offset.blank?
    o = o.dup
    o.orders = []
  end
  old_visit_Arel_Nodes_SelectStatement(o, collector)
end

#visit_ArelExtensions_Nodes_AiIMatches(o, collector) ⇒ Object



110
111
112
113
114
115
116
117
118
119
# File 'lib/arel_extensions/visitors/mysql.rb', line 110

def visit_ArelExtensions_Nodes_AiIMatches o, collector
  collector = visit o.left.ai_collate, collector
  collector << ' LIKE '
  collector = visit o.right.ai_collate, collector
  if o.escape
    collector << ' ESCAPE '
    collector = visit o.escape, collector
  end
  collector
end

#visit_ArelExtensions_Nodes_AiMatches(o, collector) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/arel_extensions/visitors/mysql.rb', line 99

def visit_ArelExtensions_Nodes_AiMatches o, collector
  collector = visit o.left.ai_collate, collector
  collector << ' LIKE '
  collector = visit o.right.ai_collate, collector
  if o.escape
    collector << ' ESCAPE '
    collector = visit o.escape, collector
  end
  collector
end

#visit_ArelExtensions_Nodes_Cast(o, collector) ⇒ Object



394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/arel_extensions/visitors/mysql.rb', line 394

def visit_ArelExtensions_Nodes_Cast o, collector
  as_attr =
    case o.as_attr
    when :binary           then 'binary'
    when :date             then 'date'
    when :datetime         then 'datetime'
    when :int              then 'signed'
    when :number, :decimal then 'decimal(20,6)'
    when :string           then 'char'
    when :time             then 'time'
    else                        o.as_attr.to_s
    end

  collector << 'CAST('
  collector = visit o.left, collector
  collector << ' AS '
  collector = visit Arel::Nodes::SqlLiteral.new(as_attr), collector
  collector << ')'
  collector
end

#visit_ArelExtensions_Nodes_Collate(o, collector) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/arel_extensions/visitors/mysql.rb', line 145

def visit_ArelExtensions_Nodes_Collate o, collector
  charset =
    case o.expressions.first
    when Arel::Attributes::Attribute
      case o.option
      when 'latin1', 'utf8'
        o.option
      else
        Arel::Table.engine.connection.charset || 'utf8'
      end
    else
      (o.option == 'latin1') ? 'latin1' : 'utf8'
    end
  collector = visit o.expressions.first, collector
  collector <<
    if o.ai
      " COLLATE #{charset == 'latin1' ? 'latin1_general_ci' : "#{charset}_unicode_ci"}"
    # doesn't work in latin1
    elsif o.ci
      " COLLATE #{charset == 'latin1' ? 'latin1_general_ci' : "#{charset}_unicode_ci"}"
    else
      " COLLATE #{charset}_bin"
    end
  collector
end

#visit_ArelExtensions_Nodes_Concat(o, collector) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/arel_extensions/visitors/mysql.rb', line 171

def visit_ArelExtensions_Nodes_Concat o, collector
  collector << 'CONCAT('
  o.expressions.each_with_index { |arg, i|
    collector << COMMA if i != 0
    if arg.is_a?(Numeric) || arg.is_a?(Arel::Attributes::Attribute)
      collector << 'CAST('
      collector = visit arg, collector
      collector << ' AS char)'
    else
      collector = visit arg, collector
    end
  }
  collector << ')'
  collector
end

#visit_ArelExtensions_Nodes_DateAdd(o, collector) ⇒ Object



338
339
340
341
342
343
344
345
# File 'lib/arel_extensions/visitors/mysql.rb', line 338

def visit_ArelExtensions_Nodes_DateAdd o, collector
  collector << 'DATE_ADD('
  collector = visit o.left, collector
  collector << COMMA
  collector = visit o.mysql_value(o.right), collector
  collector << ')'
  collector
end

#visit_ArelExtensions_Nodes_DateDiff(o, collector) ⇒ Object



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/arel_extensions/visitors/mysql.rb', line 308

def visit_ArelExtensions_Nodes_DateDiff o, collector
  case o.right_node_type
  when :ruby_date, :ruby_time, :date, :datetime, :time
    collector <<
      case o.left_node_type
      when :ruby_time, :datetime, :time then 'TIMESTAMPDIFF(SECOND, '
      else                                   'DATEDIFF('
      end
    collector = visit o.right, collector
    collector << COMMA
    collector = visit o.left, collector
    collector << ')'
  else
    collector << '('
    collector = visit o.left, collector
    collector << ' - '
    if o.right.is_a?(ArelExtensions::Nodes::Duration)
      o.right.with_interval = true
      collector = visit o.right, collector
    else
      collector << '('
      collector = visit o.right, collector
      collector << ')'
    end
    collector << ')'
    collector
  end
  collector
end

#visit_ArelExtensions_Nodes_Duration(o, collector) ⇒ Object



347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/arel_extensions/visitors/mysql.rb', line 347

def visit_ArelExtensions_Nodes_Duration o, collector
  if o.left == 'wd'
    collector << '(WEEKDAY('
    collector = visit o.right, collector
    collector << ') + 1) % 7'
  else
    if o.with_interval
      interval =
        case o.left
        when 'd', 'm', 'y'
          'DAY'
        when 'h', 'mn', 's'
          'SECOND'
        when /i\z/
          DATE_MAPPING[o.left[0..-2]]
        end
    end
    collector << ' INTERVAL ' if o.with_interval && interval
    collector << "#{DATE_MAPPING[o.left]}("
    collector = visit o.right, collector
    collector << ')'
    collector << " #{interval} " if o.with_interval && interval
  end
  collector
end

#visit_ArelExtensions_Nodes_Format(o, collector) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/arel_extensions/visitors/mysql.rb', line 254

def visit_ArelExtensions_Nodes_Format o, collector
  # One use case we met is
  # `case…when…then(valid_date).else(Arel.null).format(…)`.
  #
  # In this case, `o.col_type` is `nil` but we have a legitimate type in
  # the expression to be formatted.  The following is a best effort to
  # infer the proper type.
  first = o.expressions[0]
  type =
    o.col_type.nil? \
      && (first.respond_to?(:return_type) && !first&.return_type.nil?) \
    ? first&.return_type \
    : o.col_type

  case type
  when :date, :datetime, :time
    visit_ArelExtensions_Nodes_FormattedDate o, collector
  when :integer, :float, :decimal
    collector << 'FORMAT('
    collector = visit o.left, collector
    collector << Arel::Visitors::ToSql::COMMA
    collector << '2'
    collector << Arel::Visitors::ToSql::COMMA
    collector = visit o.right, collector
    collector << ')'
  else
    collector = visit o.left, collector
  end
  collector
end

#visit_ArelExtensions_Nodes_FormattedDate(o, collector) ⇒ Object



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/arel_extensions/visitors/mysql.rb', line 285

def visit_ArelExtensions_Nodes_FormattedDate o, collector
  fmt = ArelExtensions::Visitors::strftime_to_format(o.iso_format, DATE_FORMAT_DIRECTIVES)
  collector << 'DATE_FORMAT('
  collector << 'CONVERT_TZ(' if o.time_zone
  collector = visit o.left, collector
  case o.time_zone
  when Hash
    src_tz, dst_tz = o.time_zone.first
    collector << COMMA
    collector = visit Arel.quoted(src_tz), collector
    collector << COMMA
    collector = visit Arel.quoted(dst_tz), collector
    collector << ')'
  when String
    collector << COMMA << "'UTC'" << COMMA
    collector = visit Arel.quoted(o.time_zone), collector
    collector << ')'
  end
  collector << COMMA
  collector = visit Arel.quoted(fmt), collector
  collector << ')'
end

#visit_ArelExtensions_Nodes_FormattedNumber(o, collector) ⇒ Object



445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
# File 'lib/arel_extensions/visitors/mysql.rb', line 445

def visit_ArelExtensions_Nodes_FormattedNumber o, collector
  col = o.left.coalesce(0)
  params = o.locale ? [o.precision, Arel.quoted(o.locale)] : [o.precision]
  sign = Arel.when(col < 0).
            then('-').
            else(o.flags.include?('+') ? '+' : (o.flags.include?(' ') ? ' ' : ''))
  sign_length = ArelExtensions::Nodes::Length.new([sign])

  number =
    if o.scientific_notation
      ArelExtensions::Nodes::Concat.new([
              Arel::Nodes::NamedFunction.new('FORMAT', [
                col.abs / Arel.quoted(10).pow(col.abs.log10.floor)
              ] + params),
              o.type,
              Arel::Nodes::NamedFunction.new('FORMAT', [
                col.abs.log10.floor,
                0
              ])
            ])
    else
      Arel::Nodes::NamedFunction.new('FORMAT', [col.abs] + params)
    end

  repeated_char =
    if o.width == 0
      Arel.quoted('')
    else
      Arel
        .when(Arel.quoted(o.width).abs - (number.length + sign_length) > 0)
        .then(Arel.quoted(
                o.flags.include?('-') ? ' ' : (o.flags.include?('0') ? '0' : ' ')
              ).repeat(Arel.quoted(o.width).abs - (number.length + sign_length))
             )
        .else('')
    end
  before = !o.flags.include?('0') && !o.flags.include?('-') ? repeated_char : ''
  middle = o.flags.include?('0') && !o.flags.include?('-')  ? repeated_char : ''
  after  = o.flags.include?('-') ? repeated_char : ''
  full_number = ArelExtensions::Nodes::Concat.new([
      before,
      sign,
      middle,
      number,
      after
    ])
  collector = visit ArelExtensions::Nodes::Concat.new([Arel.quoted(o.prefix), full_number, Arel.quoted(o.suffix)]), collector
  collector
end

#visit_ArelExtensions_Nodes_GroupConcat(o, collector) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/arel_extensions/visitors/mysql.rb', line 192

def visit_ArelExtensions_Nodes_GroupConcat o, collector
  collector << 'GROUP_CONCAT('
  collector = visit o.left, collector
  if !o.order.blank?
    collector << ' ORDER BY '
    o.order.each_with_index do |order, i|
      collector << Arel::Visitors::ToSql::COMMA if i != 0
      collector = visit order, collector
    end
  end
  if o.separator && o.separator != 'NULL'
    collector << ' SEPARATOR '
    collector = visit o.separator, collector
  end
  collector << ')'
  collector
end

#visit_ArelExtensions_Nodes_IDoesNotMatch(o, collector) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/arel_extensions/visitors/mysql.rb', line 132

def visit_ArelExtensions_Nodes_IDoesNotMatch o, collector
  collector << 'LOWER('
  collector = visit o.left, collector
  collector << ') NOT LIKE LOWER('
  collector = visit o.right, collector
  collector << ')'
  if o.escape
    collector << ' ESCAPE '
    collector = visit o.escape, collector
  end
  collector
end

#visit_ArelExtensions_Nodes_IMatches(o, collector) ⇒ Object

String functions



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/arel_extensions/visitors/mysql.rb', line 86

def visit_ArelExtensions_Nodes_IMatches o, collector # insensitive on ASCII
  collector << 'LOWER('
  collector = visit o.left, collector
  collector << ') LIKE LOWER('
  collector = visit o.right, collector
  collector << ')'
  if o.escape
    collector << ' ESCAPE '
    collector = visit o.escape, collector
  end
  collector
end

#visit_ArelExtensions_Nodes_IsNotNull(o, collector) ⇒ Object



380
381
382
383
384
385
# File 'lib/arel_extensions/visitors/mysql.rb', line 380

def visit_ArelExtensions_Nodes_IsNotNull o, collector
  collector << 'NOT ISNULL('
    collector = visit o.expr, collector
    collector << ')'
    collector
end

#visit_ArelExtensions_Nodes_IsNull(o, collector) ⇒ Object



373
374
375
376
377
378
# File 'lib/arel_extensions/visitors/mysql.rb', line 373

def visit_ArelExtensions_Nodes_IsNull o, collector
  collector << 'ISNULL('
  collector = visit o.expr, collector
  collector << ')'
  collector
end

#visit_ArelExtensions_Nodes_Json(o, collector) ⇒ Object



561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
# File 'lib/arel_extensions/visitors/mysql.rb', line 561

def visit_ArelExtensions_Nodes_Json o, collector
  return super if !json_supported?

  case o.dict
  when Array
    collector << 'JSON_ARRAY('
    o.dict.each.with_index do |v, i|
      if i != 0
        collector << COMMA
      end
      collector = visit v, collector
    end
    collector << ')'
  when Hash
    collector << 'JSON_OBJECT('
    o.dict.each.with_index do |(k, v), i|
      if i != 0
        collector << COMMA
      end
      collector = visit k, collector
      collector << COMMA
      collector = visit v, collector
    end
    collector << ')'
  else
    collector = visit o.dict, collector
  end
  collector
end

#visit_ArelExtensions_Nodes_JsonGet(o, collector) ⇒ Object



603
604
605
606
607
608
609
610
611
612
613
614
# File 'lib/arel_extensions/visitors/mysql.rb', line 603

def visit_ArelExtensions_Nodes_JsonGet o, collector
  collector << 'JSON_EXTRACT('
  collector = visit o.dict, collector
  collector << COMMA
  if o.key.is_a?(Integer)
    collector << "\"$[#{o.key}]\""
  else
    collector = visit Arel.quoted('$.') + o.key, collector
  end
  collector << ')'
  collector
end

#visit_ArelExtensions_Nodes_JsonGroup(o, collector) ⇒ Object



631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
# File 'lib/arel_extensions/visitors/mysql.rb', line 631

def visit_ArelExtensions_Nodes_JsonGroup o, collector
  return super if !json_supported?

  if o.as_array
    collector << 'JSON_ARRAYAGG('
    collector = visit o.dict, collector
    collector << ')'
  else
    case o.dict
    when Hash
      collector << 'JSON_MERGE_PATCH(' if o.dict.length > 1
      o.dict.each.with_index do |(k, v), i|
        if i != 0
          collector << COMMA
        end
        collector << 'JSON_OBJECTAGG('
        collector = visit k, collector
        collector << COMMA
        collector = visit v, collector
        collector << ')'
      end
      collector << ')' if o.dict.length > 1
    else
      collector << 'JSON_OBJECTAGG('
      collector = visit o.dict, collector
      collector << ')'
    end
  end
  collector
end

#visit_ArelExtensions_Nodes_JsonMerge(o, collector) ⇒ Object



591
592
593
594
595
596
597
598
599
600
601
# File 'lib/arel_extensions/visitors/mysql.rb', line 591

def visit_ArelExtensions_Nodes_JsonMerge o, collector
  collector << 'JSON_MERGE_PATCH('
  o.expressions.each.with_index do |v, i|
    if i != 0
      collector << COMMA
    end
    collector = visit v, collector
  end
  collector << ')'
  collector
end

#visit_ArelExtensions_Nodes_JsonSet(o, collector) ⇒ Object



616
617
618
619
620
621
622
623
624
625
626
627
628
629
# File 'lib/arel_extensions/visitors/mysql.rb', line 616

def visit_ArelExtensions_Nodes_JsonSet o, collector
  collector << 'JSON_SET('
  collector = visit o.dict, collector
  collector << COMMA
  if o.key.is_a?(Integer)
    collector << "\"$[#{o.key}]\""
  else
    collector = visit Arel.quoted('$.') + o.key, collector
  end
  collector << COMMA
  collector = visit o.value, collector
  collector << ')'
  collector
end

#visit_ArelExtensions_Nodes_Log10(o, collector) ⇒ Object

Math functions



65
66
67
68
69
70
71
72
73
# File 'lib/arel_extensions/visitors/mysql.rb', line 65

def visit_ArelExtensions_Nodes_Log10 o, collector
  collector << 'LOG10('
  o.expressions.each_with_index { |arg, i|
    collector << Arel::Visitors::ToSql::COMMA if i != 0
    collector = visit arg, collector
  }
  collector << ')'
  collector
end

#visit_ArelExtensions_Nodes_Ltrim(o, collector) ⇒ Object



219
220
221
222
223
224
225
226
# File 'lib/arel_extensions/visitors/mysql.rb', line 219

def visit_ArelExtensions_Nodes_Ltrim o, collector
  collector << 'TRIM(LEADING '
  collector = visit o.right, collector
  collector << ' FROM '
  collector = visit o.left, collector
  collector << ')'
  collector
end

#visit_ArelExtensions_Nodes_Power(o, collector) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/arel_extensions/visitors/mysql.rb', line 75

def visit_ArelExtensions_Nodes_Power o, collector
  collector << 'POW('
  o.expressions.each_with_index { |arg, i|
    collector << Arel::Visitors::ToSql::COMMA if i != 0
    collector = visit arg, collector
  }
  collector << ')'
  collector
end

#visit_ArelExtensions_Nodes_RegexpReplace(o, collector) ⇒ Object



247
248
249
250
251
252
# File 'lib/arel_extensions/visitors/mysql.rb', line 247

def visit_ArelExtensions_Nodes_RegexpReplace o, collector
  if !regexp_replace_supported?
    warn('Warning: ArelExtensions: REGEXP_REPLACE does not seem to be available in the current version of the DBMS, it might crash')
  end
  super(o, collector)
end

#visit_ArelExtensions_Nodes_Repeat(o, collector) ⇒ Object



237
238
239
240
241
242
243
244
245
# File 'lib/arel_extensions/visitors/mysql.rb', line 237

def visit_ArelExtensions_Nodes_Repeat o, collector
  collector << 'REPEAT('
  o.expressions.each_with_index { |arg, i|
    collector << Arel::Visitors::ToSql::COMMA if i != 0
    collector = visit arg, collector
  }
  collector << ')'
  collector
end

#visit_ArelExtensions_Nodes_Rtrim(o, collector) ⇒ Object



228
229
230
231
232
233
234
235
# File 'lib/arel_extensions/visitors/mysql.rb', line 228

def visit_ArelExtensions_Nodes_Rtrim o, collector
  collector << 'TRIM(TRAILING '
  collector = visit o.right, collector
  collector << ' FROM '
  collector = visit o.left, collector
  collector << ')'
  collector
end

#visit_ArelExtensions_Nodes_SMatches(o, collector) ⇒ Object



121
122
123
124
125
126
127
128
129
130
# File 'lib/arel_extensions/visitors/mysql.rb', line 121

def visit_ArelExtensions_Nodes_SMatches o, collector
  collector = visit o.left.collate, collector
  collector << ' LIKE '
  collector = visit o.right.collate, collector
  if o.escape
    collector << ' ESCAPE '
    collector = visit o.escape, collector
  end
  collector
end

#visit_ArelExtensions_Nodes_Std(o, collector) ⇒ Object



518
519
520
521
522
523
524
# File 'lib/arel_extensions/visitors/mysql.rb', line 518

def visit_ArelExtensions_Nodes_Std o, collector
  collector << (o.unbiased_estimator ? 'STDDEV_SAMP(' : 'STDDEV_POP(')
  visit o.left, collector
  collector << ')'
  visit_Aggregate_For_AggregateFunction o, collector
  collector
end

#visit_ArelExtensions_Nodes_Trim(o, collector) ⇒ Object



210
211
212
213
214
215
216
217
# File 'lib/arel_extensions/visitors/mysql.rb', line 210

def visit_ArelExtensions_Nodes_Trim o, collector
  collector << 'TRIM(' # BOTH
  collector = visit o.right, collector
  collector << ' FROM '
  collector = visit o.left, collector
  collector << ')'
  collector
end

#visit_ArelExtensions_Nodes_Variance(o, collector) ⇒ Object



526
527
528
529
530
531
532
# File 'lib/arel_extensions/visitors/mysql.rb', line 526

def visit_ArelExtensions_Nodes_Variance o, collector
  collector << (o.unbiased_estimator ? 'VAR_SAMP(' : 'VAR_POP(')
  visit o.left, collector
  collector << ')'
  visit_Aggregate_For_AggregateFunction o, collector
  collector
end

#visit_ArelExtensions_Nodes_Wday(o, collector) ⇒ Object



387
388
389
390
391
392
# File 'lib/arel_extensions/visitors/mysql.rb', line 387

def visit_ArelExtensions_Nodes_Wday o, collector
  collector << '(WEEKDAY('
  collector = visit o.date, collector
  collector << ') + 1) % 7'
  collector
end

#window_supported?Boolean

Returns:

  • (Boolean)


539
540
541
# File 'lib/arel_extensions/visitors/mysql.rb', line 539

def window_supported?
  version_supported?('10.2.3', '8.0')
end