Method: Sequel::Dataset#placeholder_literal_string_sql_append

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

#placeholder_literal_string_sql_append(sql, pls) ⇒ Object

Append literalization of placeholder literal string to SQL string.

[View source]

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
661
662
663
664
665
666
667
668
669
670
671
672
# File 'lib/sequel/dataset/sql.rb', line 631

def placeholder_literal_string_sql_append(sql, pls)
  args = pls.args
  str = pls.str
  sql << '(' if pls.parens
  if args.is_a?(Hash)
    if args.empty?
      sql << str
    else
      re = /:(#{args.keys.map{|k| Regexp.escape(k.to_s)}.join('|')})\b/
      while true
        previous, q, str = str.partition(re)
        sql << previous
        literal_append(sql, args[($1||q[1..-1].to_s).to_sym]) unless q.empty?
        break if str.empty?
      end
    end
  elsif str.is_a?(Array)
    len = args.length
    str.each_with_index do |s, i|
      sql << s
      literal_append(sql, args[i]) unless i == len
    end
    unless str.length == args.length || str.length == args.length + 1
      raise Error, "Mismatched number of placeholders (#{str.length}) and placeholder arguments (#{args.length}) when using placeholder array"
    end
  else
    i = -1
    match_len = args.length - 1
    while true
      previous, q, str = str.partition('?')
      sql << previous
      literal_append(sql, args.at(i+=1)) unless q.empty?
      if str.empty?
        unless i == match_len
          raise Error, "Mismatched number of placeholders (#{i+1}) and placeholder arguments (#{args.length}) when using placeholder string"
        end
        break
      end
    end
  end
  sql << ')' if pls.parens
end