Module: DataObjects::Quoting

Defined in:
lib/data_objects/quoting.rb

Instance Method Summary collapse

Instance Method Details

#quote_array(value) ⇒ Object

Quote an array as a list of quoted values



78
79
80
# File 'lib/data_objects/quoting.rb', line 78

def quote_array(value)
  "(#{value.map { |entry| quote_value(entry) }.join(', ')})"
end

#quote_boolean(value) ⇒ Object

Quote true, false as the strings TRUE, FALSE



73
74
75
# File 'lib/data_objects/quoting.rb', line 73

def quote_boolean(value)
  value.to_s.upcase
end

#quote_byte_array(value) ⇒ Object



93
94
95
# File 'lib/data_objects/quoting.rb', line 93

def quote_byte_array(value)
  quote_string(value)
end

#quote_class(value) ⇒ Object

Quote a class by quoting its name



47
48
49
# File 'lib/data_objects/quoting.rb', line 47

def quote_class(value)
  quote_string(value.name)
end

#quote_date(value) ⇒ Object

Convert a Date to standard YMD format



68
69
70
# File 'lib/data_objects/quoting.rb', line 68

def quote_date(value)
  "'#{value.strftime("%Y-%m-%d")}'"
end

#quote_datetime(value) ⇒ Object

Quote a DateTime by relying on it’s own to_s conversion



63
64
65
# File 'lib/data_objects/quoting.rb', line 63

def quote_datetime(value)
  "'#{value.dup}'"
end

#quote_numeric(value) ⇒ Object

Convert the Numeric to a String and quote that



37
38
39
# File 'lib/data_objects/quoting.rb', line 37

def quote_numeric(value)
  value.to_s
end

#quote_range(value) ⇒ Object

Quote a range by joining the quoted end-point values with AND. It’s not clear whether or when this is a useful or correct thing to do.



84
85
86
# File 'lib/data_objects/quoting.rb', line 84

def quote_range(value)
  "#{quote_value(value.first)} AND #{quote_value(value.last)}"
end

#quote_regexp(value) ⇒ Object

Quote a Regex using its string value. Note that there’s no attempt to make a valid SQL “LIKE” string.



89
90
91
# File 'lib/data_objects/quoting.rb', line 89

def quote_regexp(value)
  quote_string(value.source)
end

#quote_string(value) ⇒ Object

Quote a String for SQL by doubling any embedded single-quote characters



42
43
44
# File 'lib/data_objects/quoting.rb', line 42

def quote_string(value)
  "'#{value.gsub("'", "''")}'"
end

#quote_symbol(value) ⇒ Object

Convert the Symbol to a String and quote that



32
33
34
# File 'lib/data_objects/quoting.rb', line 32

def quote_symbol(value)
  quote_string(value.to_s)
end

#quote_time(value) ⇒ Object

Convert a Time to standard YMDHMS format (with microseconds if necessary)



52
53
54
55
56
57
58
59
60
# File 'lib/data_objects/quoting.rb', line 52

def quote_time(value)
  offset = value.utc_offset
  if offset >= 0
    offset_string = "+#{sprintf("%02d", offset / 3600)}:#{sprintf("%02d", (offset % 3600) / 60)}"
  elsif offset < 0
    offset_string = "-#{sprintf("%02d", -offset / 3600)}:#{sprintf("%02d", (-offset % 3600) / 60)}"
  end
  "'#{value.strftime('%Y-%m-%dT%H:%M:%S')}" << (value.usec > 0 ? ".#{value.usec.to_s.rjust(6, '0')}" : "") << offset_string << "'"
end

#quote_value(value) ⇒ Object

Quote a value of any of the recognised data types



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/data_objects/quoting.rb', line 6

def quote_value(value)
  return 'NULL' if value.nil?

  case value
    when Numeric then quote_numeric(value)
    when ::Extlib::ByteArray then quote_byte_array(value)
    when String then quote_string(value)
    when Time then quote_time(value)
    when DateTime then quote_datetime(value)
    when Date then quote_date(value)
    when TrueClass, FalseClass then quote_boolean(value)
    when Array then quote_array(value)
    when Range then quote_range(value)
    when Symbol then quote_symbol(value)
    when Regexp then quote_regexp(value)
    when Class then quote_class(value)
    else
      if value.respond_to?(:to_sql)
        value.to_sql
      else
        raise "Don't know how to quote #{value.class} objects (#{value.inspect})"
      end
  end
end