Method: Sequel::Dataset#function_sql_append

Defined in:
lib/sequel/dataset/sql.rb

#function_sql_append(sql, f) ⇒ Object

Append literalization of function call to SQL string.


488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
# File 'lib/sequel/dataset/sql.rb', line 488

def function_sql_append(sql, f)
  name = f.name
  opts = f.opts

  if opts[:emulate]
    if emulate_function?(name)
      emulate_function_sql_append(sql, f)
      return
    end

    name = native_function_name(name) 
  end

  sql << 'LATERAL ' if opts[:lateral]

  case name
  when SQL::Identifier
    if supports_quoted_function_names? && opts[:quoted]
      literal_append(sql, name)
    else
      sql << name.value.to_s
    end
  when SQL::QualifiedIdentifier
    if supports_quoted_function_names? && opts[:quoted] != false
      literal_append(sql, name)
    else
      sql << split_qualifiers(name).join('.')
    end
  else
    if supports_quoted_function_names? && opts[:quoted]
      quote_identifier_append(sql, name)
    else
      sql << name.to_s
    end
  end

  sql << '('
  if filter = opts[:filter]
    filter = filter_expr(filter, &opts[:filter_block])
  end
  if opts[:*]
    if filter && !supports_filtered_aggregates?
      literal_append(sql, Sequel.case({filter=>1}, nil))
      filter = nil
    else
      sql <<  '*'
    end
  else
    sql << "DISTINCT " if opts[:distinct]
    if filter && !supports_filtered_aggregates?
      expression_list_append(sql, f.args.map{|arg| Sequel.case({filter=>arg}, nil)})
      filter = nil
    else
      expression_list_append(sql, f.args)
    end
    if order = opts[:order]
      sql << " ORDER BY "
      expression_list_append(sql, order)
    end
  end
  sql << ')'

  if group = opts[:within_group]
    sql << " WITHIN GROUP (ORDER BY "
    expression_list_append(sql, group)
    sql << ')'
  end

  if filter
    sql << " FILTER (WHERE "
    literal_append(sql, filter)
    sql << ')'
  end

  if window = opts[:over]
    sql << ' OVER '
    window_sql_append(sql, window.opts)
  end

  if opts[:with_ordinality]
    sql << " WITH ORDINALITY"
  end
end