DataObjects::Quoting

Public Instance Methods

quote_array(value) click to toggle source

Quote an array as a list of quoted values

# File lib/data_objects/quoting.rb, line 78
def quote_array(value)
  "(#{value.map { |entry| quote_value(entry) }.join(', ')})"
end
quote_boolean(value) click to toggle source

Quote true, false as the strings TRUE, FALSE

# File lib/data_objects/quoting.rb, line 73
def quote_boolean(value)
  value.to_s.upcase
end
quote_byte_array(value) click to toggle source
# File lib/data_objects/quoting.rb, line 93
def quote_byte_array(value)
  quote_string(value)
end
quote_class(value) click to toggle source

Quote a class by quoting its name

# File lib/data_objects/quoting.rb, line 47
def quote_class(value)
  quote_string(value.name)
end
quote_date(value) click to toggle source

Convert a Date to standard YMD format

# File lib/data_objects/quoting.rb, line 68
def quote_date(value)
  "'#{value.strftime("%Y-%m-%d")}'"
end
quote_datetime(value) click to toggle source

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

# File lib/data_objects/quoting.rb, line 63
def quote_datetime(value)
  "'#{value.dup}'"
end
quote_numeric(value) click to toggle source

Convert the Numeric to a String and quote that

# File lib/data_objects/quoting.rb, line 37
def quote_numeric(value)
  value.to_s
end
quote_range(value) click to toggle source

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.

# 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) click to toggle source

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

# File lib/data_objects/quoting.rb, line 89
def quote_regexp(value)
  quote_string(value.source)
end
quote_string(value) click to toggle source

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

# File lib/data_objects/quoting.rb, line 42
def quote_string(value)
  "'#{value.gsub("'", "''")}'"
end
quote_symbol(value) click to toggle source

Convert the Symbol to a String and quote that

# File lib/data_objects/quoting.rb, line 32
def quote_symbol(value)
  quote_string(value.to_s)
end
quote_time(value) click to toggle source

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

# 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) click to toggle source

Quote a value of any of the recognised data types

# 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

[Validate]

Generated with the Darkfish Rdoc Generator 2.