Module: ArelExtensions::Visitors::MSSQL

Included in:
Arel::Visitors::MSSQL, Arel::Visitors::SQLServer
Defined in:
lib/arel_extensions/visitors/mssql.rb

Constant Summary collapse

MSSQL_CLASS_NAMES =
%i[MSSQL SQLServer].freeze
LOADED_VISITOR =
Arel::Visitors.const_get(mssql_class)

Instance Method Summary collapse

Instance Method Details

#grouping_array_or_grouping_element(o, collector) ⇒ Object

Utilized by GroupingSet, Cube & RollUp visitors to handle grouping aggregation semantics



774
775
776
777
778
779
780
781
782
# File 'lib/arel_extensions/visitors/mssql.rb', line 774

def grouping_array_or_grouping_element(o, collector)
  if o.expr.is_a? Array
    collector << "( "
    visit o.expr, collector
    collector << " )"
  else
    visit o.expr, collector
  end
end

#primary_Key_From_Table(t) ⇒ Object



149
150
151
152
153
154
155
# File 'lib/arel_extensions/visitors/mssql.rb', line 149

def primary_Key_From_Table t
  return unless t

  column_name = @connection.schema_cache.primary_keys(t.name) ||
                @connection.schema_cache.columns_hash(t.name).first.try(:second).try(:name)
  column_name ? t[column_name] : nil
end

#quote(value, column = nil) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/arel_extensions/visitors/mssql.rb', line 119

def quote value, column = nil
  case value
  when Arel::Nodes::SqlLiteral
    value
  when String, Symbol, ActiveSupport::Multibyte::Chars
    "'#{quote_string(value.to_s)}'"
  when true
    quoted_true
  when false
    quoted_false
  when nil
    'NULL'
  # BigDecimals need to be put in a non-normalized form and quoted.
  when BigDecimal
    value.to_s('F')
  when Numeric, ActiveSupport::Duration
    value.to_s
  when Arel::VERSION.to_i > 6 && ActiveRecord::Type::Time::Value
    "'#{quoted_time(value)}'"
  when Date, Time
    "'#{quoted_date(value)}'"
  when Class
    "'#{value}'"
  else
    raise TypeError, "can't quote #{value.class.name}"
  end
end

#quote_string(s) ⇒ Object



80
81
82
# File 'lib/arel_extensions/visitors/mssql.rb', line 80

def quote_string(s)
  s.gsub('\\', '\&\&').gsub("'", "''") # ' (for ruby-mode)
end

#quoted_binary(value) ⇒ Object

:nodoc:



84
85
86
# File 'lib/arel_extensions/visitors/mssql.rb', line 84

def quoted_binary(value) # :nodoc:
  "'#{quote_string(value.to_s)}'"
end

#quoted_date(value) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/arel_extensions/visitors/mssql.rb', line 88

def quoted_date(value)
  if value.acts_like?(:time)
    if (ActiveRecord.respond_to?(:default_timezone) && ActiveRecord.default_timezone == :utc) || ActiveRecord::Base.default_timezone == :utc
      value = value.getutc if value.respond_to?(:getutc) && !value.utc?
    else
      value = value.getlocal if value.respond_to?(:getlocal)
    end
  end
  # new versions of AR use `to_fs`, but we want max compatibility, and we're
  # not going to write it over and over, so it's fine like that.
  result = value.to_formatted_s(:db)
  if value.respond_to?(:usec) && value.usec > 0
    result << '.' << sprintf('%06d', value.usec)
  else
    result
  end
end

#quoted_falseObject



110
111
112
# File 'lib/arel_extensions/visitors/mssql.rb', line 110

def quoted_false
  'FALSE'
end

#quoted_time(value) ⇒ Object

:nodoc:



114
115
116
117
# File 'lib/arel_extensions/visitors/mssql.rb', line 114

def quoted_time(value) # :nodoc:
  value = value.change(year: 2000, month: 1, day: 1)
  quoted_date(value).sub(/\A\d{4}-\d{2}-\d{2} /, "")
end

#quoted_trueObject



106
107
108
# File 'lib/arel_extensions/visitors/mssql.rb', line 106

def quoted_true
  'TRUE'
end

#visit_Arel_Nodes_As(o, collector) ⇒ Object



566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
# File 'lib/arel_extensions/visitors/mssql.rb', line 566

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
  lft, rgt =
    if o.right.is_a?(Arel::Nodes::SqlLiteral)
      if Arel::VERSION.to_i >= 6 && o.right[0] != '[' && o.right[-1] != ']'
        # This is a lie, it's not about arel version, but SQL Server's (>= 2000).
        ['[', ']']
      elsif o.right[0] != '"' && o.right[-1] != '"'
        ['"', '"']
      else
        []
      end
    end
  collector << lft if lft
  collector = visit o.right, collector
  collector << rgt if rgt
  collector
end

#visit_Arel_Nodes_NotRegexp(o, collector) ⇒ Object



600
601
602
603
604
# File 'lib/arel_extensions/visitors/mssql.rb', line 600

def visit_Arel_Nodes_NotRegexp o, collector
  collector = visit o.left, collector
  collector << "NOT LIKE '%#{o.right}%'"
  collector
end

#visit_Arel_Nodes_Regexp(o, collector) ⇒ Object

SQL Server does not know about REGEXP



594
595
596
597
598
# File 'lib/arel_extensions/visitors/mssql.rb', line 594

def visit_Arel_Nodes_Regexp o, collector
  collector = visit o.left, collector
  collector << "LIKE '%#{o.right}%'"
  collector
end

#visit_Arel_Nodes_RollUp(o, collector) ⇒ Object



606
607
608
609
# File 'lib/arel_extensions/visitors/mssql.rb', line 606

def visit_Arel_Nodes_RollUp(o, collector)
  collector << "ROLLUP"
  grouping_array_or_grouping_element o, collector
end

#visit_ArelExtensions_Nodes_AiIMatches(o, collector) ⇒ Object



524
525
526
527
528
529
530
531
532
533
534
# File 'lib/arel_extensions/visitors/mssql.rb', line 524

def visit_ArelExtensions_Nodes_AiIMatches o, collector
  collector = visit o.left.collate(true, true), collector
  collector << ' LIKE '
  collector = visit o.right.collate(true, true), collector
  if o.escape
    collector << ' ESCAPE '
    visit o.escape, collector
  else
    collector
  end
end

#visit_ArelExtensions_Nodes_AiMatches(o, collector) ⇒ Object



512
513
514
515
516
517
518
519
520
521
522
# File 'lib/arel_extensions/visitors/mssql.rb', line 512

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 '
    visit o.escape, collector
  else
    collector
  end
end

#visit_ArelExtensions_Nodes_Blank(o, collector) ⇒ Object



378
379
380
# File 'lib/arel_extensions/visitors/mssql.rb', line 378

def visit_ArelExtensions_Nodes_Blank o, collector
  visit o.expr.coalesce('').trim.length.eq(0), collector
end

#visit_ArelExtensions_Nodes_Cast(o, collector) ⇒ Object



642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
# File 'lib/arel_extensions/visitors/mssql.rb', line 642

def visit_ArelExtensions_Nodes_Cast o, collector
  as_attr =
    case o.as_attr
    when :string
      'varchar'
    when :time
      'time'
    when :date
      'date'
    when :datetime
      'datetime'
    when :number, :decimal, :float
      'decimal(10,6)'
    when :int
      collector << 'CAST(CAST('
      collector = visit o.left, collector
      collector << ' AS decimal(10,0)) AS int)'
      return collector
    when :binary
      'binary'
    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_Ceil(o, collector) ⇒ Object

Math Functions



158
159
160
161
162
163
# File 'lib/arel_extensions/visitors/mssql.rb', line 158

def visit_ArelExtensions_Nodes_Ceil o, collector
  collector << 'CEILING('
  collector = visit o.expr, collector
  collector << ')'
  collector
end

#visit_ArelExtensions_Nodes_Collate(o, collector) ⇒ Object



548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
# File 'lib/arel_extensions/visitors/mssql.rb', line 548

def visit_ArelExtensions_Nodes_Collate o, collector
  if o.ai && o.ci
    collector = visit o.expressions.first, collector
    collector << ' COLLATE Latin1_General_CI_AI'
  elsif o.ai
    collector = visit o.expressions.first, collector
    collector << ' COLLATE Latin1_General_CS_AI'
  elsif o.ci
    collector = visit o.expressions.first, collector
    collector << ' COLLATE Latin1_General_CI_AS'
  else
    collector = visit o.expressions.first, collector
    collector << ' COLLATE Latin1_General_CS_AS'
  end
  collector
end

#visit_ArelExtensions_Nodes_Concat(o, collector) ⇒ Object



199
200
201
202
203
204
205
206
207
# File 'lib/arel_extensions/visitors/mssql.rb', line 199

def visit_ArelExtensions_Nodes_Concat o, collector
  collector << 'CONCAT('
  o.expressions.each_with_index { |arg, i|
    collector << LOADED_VISITOR::COMMA if i != 0
    collector = visit arg, collector
  }
  collector << ')'
  collector
end

#visit_ArelExtensions_Nodes_DateAdd(o, collector) ⇒ Object



249
250
251
252
253
254
255
256
257
258
# File 'lib/arel_extensions/visitors/mssql.rb', line 249

def visit_ArelExtensions_Nodes_DateAdd o, collector
  collector << 'DATEADD('
  collector = visit o.mssql_datepart(o.right), collector
  collector << LOADED_VISITOR::COMMA
  collector = visit o.mssql_value(o.right), collector
  collector << LOADED_VISITOR::COMMA
  collector = visit o.left, collector
  collector << ')'
  collector
end

#visit_ArelExtensions_Nodes_DateDiff(o, collector) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/arel_extensions/visitors/mssql.rb', line 221

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 'DATEDIFF(second'
                else                                   'DATEDIFF(day'
                end
    collector << LOADED_VISITOR::COMMA
    collector = visit o.right, collector
    collector << LOADED_VISITOR::COMMA
    collector = visit o.left, collector
    collector << ')'
  else
    da = ArelExtensions::Nodes::DateAdd.new([])
    collector << 'DATEADD('
    collector = visit da.mssql_datepart(o.right), collector
    collector << LOADED_VISITOR::COMMA
    collector << '-('
    collector = visit da.mssql_value(o.right), collector
    collector << ')'
    collector << LOADED_VISITOR::COMMA
    collector = visit o.left, collector
    collector << ')'
    collector
  end
  collector
end

#visit_ArelExtensions_Nodes_Duration(o, collector) ⇒ Object



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/arel_extensions/visitors/mssql.rb', line 260

def visit_ArelExtensions_Nodes_Duration o, collector
  if o.with_interval && o.left.end_with?('i')
    collector = visit o.right, collector
  else
    left = o.left.end_with?('i') ? o.left[0..-2] : o.left
    conv = %w[h mn s].include?(o.left)
    collector << 'DATEPART('
    collector << LOADED_VISITOR::DATE_MAPPING[left]
    collector << LOADED_VISITOR::COMMA
    collector << 'CONVERT(datetime,' if conv
    collector = visit o.right, collector
    collector << ')' if conv
    collector << ')'
  end
  collector
end

#visit_ArelExtensions_Nodes_FindInSet(o, collector) ⇒ Object



476
477
478
479
480
481
482
483
484
# File 'lib/arel_extensions/visitors/mssql.rb', line 476

def visit_ArelExtensions_Nodes_FindInSet o, collector
  collector << 'dbo.FIND_IN_SET('
  o.expressions.each_with_index { |arg, i|
    collector << LOADED_VISITOR::COMMA if i != 0
    collector = visit arg, collector
  }
  collector << ')'
  collector
end

#visit_ArelExtensions_Nodes_Format(o, collector) ⇒ Object



386
387
388
# File 'lib/arel_extensions/visitors/mssql.rb', line 386

def visit_ArelExtensions_Nodes_Format o, collector
  visit_ArelExtensions_Nodes_FormattedDate o, collector
end

#visit_ArelExtensions_Nodes_FormattedDate(o, collector) ⇒ Object



390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# File 'lib/arel_extensions/visitors/mssql.rb', line 390

def visit_ArelExtensions_Nodes_FormattedDate o, collector
  f = ArelExtensions::Visitors::strftime_to_format(o.iso_format, LOADED_VISITOR::DATE_FORMAT_DIRECTIVES)
  if fmt = LOADED_VISITOR::DATE_CONVERT_FORMATS[f]
    collector << "CONVERT(VARCHAR(#{f.length})"
    collector << LOADED_VISITOR::COMMA
    if o.time_zone
      collector << 'CONVERT(datetime'
      collector << LOADED_VISITOR::COMMA
      collector << ' '
    end
    collector = visit o.left, collector
    case o.time_zone
    when Hash
      src_tz, dst_tz = o.time_zone.first
      collector << ') AT TIME ZONE '
      collector = visit Arel.quoted(src_tz), collector
      collector << ' AT TIME ZONE '
      collector = visit Arel.quoted(dst_tz), collector
    when String
      collector << ') AT TIME ZONE '
      collector = visit Arel.quoted(o.time_zone), collector
    end
    collector << LOADED_VISITOR::COMMA
    collector << fmt.to_s
    collector << ')'
    collector
  else
    s = StringScanner.new o.iso_format
    collector << '('
    sep = ''
    while !s.eos?
      collector << sep
      sep = ' + '
      case
      when s.scan(LOADED_VISITOR::DATE_FORMAT_REGEX)
        dir = LOADED_VISITOR::DATE_FORMAT_DIRECTIVES[s.matched]
        fmt = LOADED_VISITOR::DATE_FORMAT_FORMAT[dir]
        date_name = LOADED_VISITOR::DATE_NAME.include?(s.matched)
        collector << 'LTRIM(RTRIM('
        collector << 'FORMAT(' if fmt
        collector << 'STR('    if !fmt && !date_name
        collector << (date_name ? 'DATENAME(' : 'DATEPART(')
        collector << dir
        collector << LOADED_VISITOR::COMMA
        if o.time_zone
          collector << 'CONVERT(datetime'
          collector << LOADED_VISITOR::COMMA
          collector << ' '
        end
        collector = visit o.left, collector
        case o.time_zone
        when Hash
          src_tz, dst_tz = o.time_zone.first.first, o.time_zone.first.second
          collector << ') AT TIME ZONE '
          collector = visit Arel.quoted(src_tz), collector
          collector << ' AT TIME ZONE '
          collector = visit Arel.quoted(dst_tz), collector
        when String
          collector << ') AT TIME ZONE '
          collector = visit Arel.quoted(o.time_zone), collector
        end
        collector << ')'
        collector << ')'                                  if !fmt && !date_name
        collector << LOADED_VISITOR::COMMA << "'#{fmt}')" if fmt
        collector << '))'
      when s.scan(/^%%/)
        collector = visit Arel.quoted('%'), collector
      when s.scan(/[^%]+|./)
        collector = visit Arel.quoted(s.matched), collector
      end
    end
    collector << ')'
    collector
  end
end

#visit_ArelExtensions_Nodes_FormattedNumber(o, collector) ⇒ Object



673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
# File 'lib/arel_extensions/visitors/mssql.rb', line 673

def visit_ArelExtensions_Nodes_FormattedNumber o, collector
  col = o.left.coalesce(0)
  locale = Arel.quoted(o.locale.tr('_', '-'))
  param = Arel.quoted("N#{o.precision}")
  sign = Arel.when(col < 0).
            then('-').
            else(o.flags.include?('+') ? '+' : (o.flags.include?(' ') ? ' ' : ''))
  sign_length = o.flags.include?('+') || o.flags.include?(' ') ?
        Arel.quoted(1) :
        Arel.when(col < 0).then(1).else(0)

  number =
    if o.scientific_notation
      ArelExtensions::Nodes::Concat.new([
            Arel::Nodes::NamedFunction.new('FORMAT', [
              col.abs / Arel.quoted(10).pow(col.abs.log10.floor),
              param,
              locale
            ]),
            o.type,
            Arel::Nodes::NamedFunction.new('FORMAT', [
              col.abs.log10.floor,
              Arel.quoted('N0'),
              locale
            ])
          ])
    else
      Arel::Nodes::NamedFunction.new('FORMAT', [
          Arel.quoted(col.abs),
          param,
          locale
        ])
    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

TODO;



612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
# File 'lib/arel_extensions/visitors/mssql.rb', line 612

def visit_ArelExtensions_Nodes_GroupConcat o, collector
  collector << '(STRING_AGG('
  collector = visit o.left, collector
  collector << Arel::Visitors::Oracle::COMMA
  collector =
    if o.separator && o.separator != 'NULL'
      visit o.separator, collector
    else
      visit Arel.quoted(','), collector
    end
  collector << ') WITHIN GROUP (ORDER BY '
  if o.order.present?
    o.order.each_with_index do |order, i|
      collector << Arel::Visitors::Oracle::COMMA if i != 0
      collector = visit order, collector
    end
  else
    collector = visit o.left, collector
  end
  collector << '))'
  collector
end

#visit_ArelExtensions_Nodes_IDoesNotMatch(o, collector) ⇒ Object

TODO; manage case insensitivity



500
501
502
503
504
505
506
507
508
509
510
# File 'lib/arel_extensions/visitors/mssql.rb', line 500

def visit_ArelExtensions_Nodes_IDoesNotMatch o, collector
  collector = visit o.left.ci_collate, collector
  collector << ' NOT LIKE '
  collector = visit o.right.ci_collate, collector
  if o.escape
    collector << ' ESCAPE '
    visit o.escape, collector
  else
    collector
  end
end

#visit_ArelExtensions_Nodes_IMatches(o, collector) ⇒ Object

TODO; manage case insensitivity



487
488
489
490
491
492
493
494
495
496
497
# File 'lib/arel_extensions/visitors/mssql.rb', line 487

def visit_ArelExtensions_Nodes_IMatches o, collector
  collector = visit o.left.ci_collate, collector
  collector << ' LIKE '
  collector = visit o.right.ci_collate, collector
  if o.escape
    collector << ' ESCAPE '
    visit o.escape, collector
  else
    collector
  end
end

#visit_ArelExtensions_Nodes_IsNotNull(o, collector) ⇒ Object



192
193
194
195
196
197
# File 'lib/arel_extensions/visitors/mssql.rb', line 192

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

#visit_ArelExtensions_Nodes_IsNull(o, collector) ⇒ Object



185
186
187
188
189
190
# File 'lib/arel_extensions/visitors/mssql.rb', line 185

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

#visit_ArelExtensions_Nodes_JsonGet(o, collector) ⇒ Object



759
760
761
762
763
764
765
766
767
768
769
770
# File 'lib/arel_extensions/visitors/mssql.rb', line 759

def visit_ArelExtensions_Nodes_JsonGet o, collector
  collector << 'JSON_VALUE('
  collector = visit o.dict, collector
  collector << Arel::Visitors::MySQL::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_Length(o, collector) ⇒ Object



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/arel_extensions/visitors/mssql.rb', line 277

def visit_ArelExtensions_Nodes_Length o, collector
  if o.bytewise
    collector << '(DATALENGTH('
    collector = visit o.expr, collector
    collector << ') / ISNULL(NULLIF(DATALENGTH(LEFT(COALESCE('
    collector = visit o.expr, collector
    collector << ", '#' ), 1 )), 0), 1))"
    collector
  else
    collector << 'LEN('
    collector = visit o.expr, collector
    collector << ')'
    collector
  end
end

#visit_ArelExtensions_Nodes_LevenshteinDistance(o, collector) ⇒ Object



749
750
751
752
753
754
755
756
# File 'lib/arel_extensions/visitors/mssql.rb', line 749

def visit_ArelExtensions_Nodes_LevenshteinDistance o, collector
  collector << 'dbo.LEVENSHTEIN_DISTANCE('
  collector = visit o.left, collector
  collector << Arel::Visitors::ToSql::COMMA
  collector = visit o.right, collector
  collector << ')'
  collector
end

#visit_ArelExtensions_Nodes_Locate(o, collector) ⇒ Object



307
308
309
310
311
312
313
314
# File 'lib/arel_extensions/visitors/mssql.rb', line 307

def visit_ArelExtensions_Nodes_Locate o, collector
  collector << 'CHARINDEX('
  collector = visit o.right, collector
  collector << LOADED_VISITOR::COMMA
  collector = visit o.left, collector
  collector << ')'
  collector
end

#visit_ArelExtensions_Nodes_Log10(o, collector) ⇒ Object



165
166
167
168
169
170
171
172
173
# File 'lib/arel_extensions/visitors/mssql.rb', line 165

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



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/arel_extensions/visitors/mssql.rb', line 344

def visit_ArelExtensions_Nodes_Ltrim o, collector
  if o.right
    collector << 'REPLACE(REPLACE(LTRIM(REPLACE(REPLACE('
    collector = visit o.left, collector
    collector << ", ' ', '~'), "
    collector = visit o.right, collector
    collector << ", ' ')), ' ', "
    collector = visit o.right, collector
    collector << "), '~', ' ')"
  else
    collector << 'LTRIM('
    collector = visit o.left, collector
    collector << ')'
  end
  collector
end

#visit_ArelExtensions_Nodes_MD5(o, collector) ⇒ Object



635
636
637
638
639
640
# File 'lib/arel_extensions/visitors/mssql.rb', line 635

def visit_ArelExtensions_Nodes_MD5 o, collector
  collector << "LOWER(CONVERT(NVARCHAR(32),HashBytes('MD5',CONVERT(VARCHAR,"
  collector = visit o.left, collector
  collector << ')),2))'
  collector
end

#visit_ArelExtensions_Nodes_NotBlank(o, collector) ⇒ Object



382
383
384
# File 'lib/arel_extensions/visitors/mssql.rb', line 382

def visit_ArelExtensions_Nodes_NotBlank o, collector
  visit o.expr.coalesce('').trim.length.gt(0), collector
end

#visit_ArelExtensions_Nodes_Power(o, collector) ⇒ Object



175
176
177
178
179
180
181
182
183
# File 'lib/arel_extensions/visitors/mssql.rb', line 175

def visit_ArelExtensions_Nodes_Power o, collector
  collector << 'POWER('
  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_Repeat(o, collector) ⇒ Object



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

def visit_ArelExtensions_Nodes_Repeat o, collector
  collector << 'REPLICATE('
  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_Replace(o, collector) ⇒ Object



466
467
468
469
470
471
472
473
474
# File 'lib/arel_extensions/visitors/mssql.rb', line 466

def visit_ArelExtensions_Nodes_Replace o, collector
  collector << 'REPLACE('
  o.expressions.each_with_index { |arg, i|
    collector << LOADED_VISITOR::COMMA if i != 0
    collector = visit arg, collector
  }
  collector << ')'
  collector
end

#visit_ArelExtensions_Nodes_Round(o, collector) ⇒ Object



293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/arel_extensions/visitors/mssql.rb', line 293

def visit_ArelExtensions_Nodes_Round o, collector
  collector << 'ROUND('
  o.expressions.each_with_index { |arg, i|
    collector << LOADED_VISITOR::COMMA if i != 0
    collector = visit arg, collector
  }
  if o.expressions.length == 1
    collector << LOADED_VISITOR::COMMA
    collector << '0'
  end
  collector << ')'
  collector
end

#visit_ArelExtensions_Nodes_Rtrim(o, collector) ⇒ Object



361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# File 'lib/arel_extensions/visitors/mssql.rb', line 361

def visit_ArelExtensions_Nodes_Rtrim o, collector
  if o.right
    collector << 'REPLACE(REPLACE(RTRIM(REPLACE(REPLACE('
    collector = visit o.left, collector
    collector << ", ' ', '~'), "
    collector = visit o.right, collector
    collector << ", ' ')), ' ', "
    collector = visit o.right, collector
    collector << "), '~', ' ')"
  else
    collector << 'RTRIM('
    collector = visit o.left, collector
    collector << ')'
  end
  collector
end

#visit_ArelExtensions_Nodes_SMatches(o, collector) ⇒ Object



536
537
538
539
540
541
542
543
544
545
546
# File 'lib/arel_extensions/visitors/mssql.rb', line 536

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 '
    visit o.escape, collector
  else
    collector
  end
end

#visit_ArelExtensions_Nodes_Std(o, collector) ⇒ Object



734
735
736
737
738
739
# File 'lib/arel_extensions/visitors/mssql.rb', line 734

def visit_ArelExtensions_Nodes_Std o, collector
  collector << (o.unbiased_estimator ? 'STDEV(' : 'STDEVP(')
  visit o.left, collector
  collector << ')'
  collector
end

#visit_ArelExtensions_Nodes_Substring(o, collector) ⇒ Object



316
317
318
319
320
321
322
323
324
325
# File 'lib/arel_extensions/visitors/mssql.rb', line 316

def visit_ArelExtensions_Nodes_Substring o, collector
  collector << 'SUBSTRING('
  collector = visit o.expressions[0], collector
  collector << LOADED_VISITOR::COMMA
  collector = visit o.expressions[1], collector
  collector << LOADED_VISITOR::COMMA
  collector = o.expressions[2] ? visit(o.expressions[2], collector) : visit(o.expressions[0].length, collector)
  collector << ')'
  collector
end

#visit_ArelExtensions_Nodes_Trim(o, collector) ⇒ Object



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/arel_extensions/visitors/mssql.rb', line 327

def visit_ArelExtensions_Nodes_Trim o, collector
  # NOTE: in MSSQL's `blank`, o.right is the space char so we need to
  # account for it.
  if o.right && !/\A\s\Z/.match(o.right.expr)
    collector << 'dbo.TrimChar('
    collector = visit o.left, collector
    collector << Arel::Visitors::MSSQL::COMMA
    collector = visit o.right, collector
    collector << ')'
  else
    collector << "LTRIM(RTRIM("
    collector = visit o.left, collector
    collector << "))"
  end
  collector
end

#visit_ArelExtensions_Nodes_Variance(o, collector) ⇒ Object



741
742
743
744
745
746
# File 'lib/arel_extensions/visitors/mssql.rb', line 741

def visit_ArelExtensions_Nodes_Variance o, collector
  collector << (o.unbiased_estimator ? 'VAR(' : 'VARP(')
  visit o.left, collector
  collector << ')'
  collector
end