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



80
81
82
# File 'lib/data_objects/quoting.rb', line 80

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

#quote_boolean(value) ⇒ Object

Quote true, false as the strings TRUE, FALSE



75
76
77
# File 'lib/data_objects/quoting.rb', line 75

def quote_boolean(value)
  value.to_s.upcase
end

#quote_byte_array(value) ⇒ Object



95
96
97
# File 'lib/data_objects/quoting.rb', line 95

def quote_byte_array(value)
  quote_string(value)
end

#quote_class(value) ⇒ Object

Quote a class by quoting its name



44
45
46
# File 'lib/data_objects/quoting.rb', line 44

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

#quote_date(value) ⇒ Object

Convert a Date to standard YMD format



70
71
72
# File 'lib/data_objects/quoting.rb', line 70

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



65
66
67
# File 'lib/data_objects/quoting.rb', line 65

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

#quote_numeric(value) ⇒ Object

Convert the Numeric to a String and quote that



34
35
36
# File 'lib/data_objects/quoting.rb', line 34

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.



86
87
88
# File 'lib/data_objects/quoting.rb', line 86

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.



91
92
93
# File 'lib/data_objects/quoting.rb', line 91

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



39
40
41
# File 'lib/data_objects/quoting.rb', line 39

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

#quote_symbol(value) ⇒ Object

Convert the Symbol to a String and quote that



29
30
31
# File 'lib/data_objects/quoting.rb', line 29

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)



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

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

#quote_value(value) ⇒ Object

Quote a value of any of the recognised data types



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

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
    raise "Don't know how to quote #{value.class} objects (#{value.inspect})" unless value.respond_to?(:to_sql)

    value.to_sql

  end
end