quote(value, column = nil)
public
Quotes PostgreSQL-specific data types for SQL input.
Show source
def quote(value, column = nil)
return super unless column
sql_type = type_to_sql(column.type, column.limit, column.precision, column.scale)
case value
when Range
if /range$/ =~ sql_type
"'#{PostgreSQLColumn.range_to_string(value)}'::#{sql_type}"
else
super
end
when Array
case sql_type
when 'point' then super(PostgreSQLColumn.point_to_string(value))
when 'json' then super(PostgreSQLColumn.json_to_string(value))
else
if column.array
"'#{PostgreSQLColumn.array_to_string(value, column, self).gsub(/'/, "''")}'"
else
super
end
end
when Hash
case sql_type
when 'hstore' then super(PostgreSQLColumn.hstore_to_string(value), column)
when 'json' then super(PostgreSQLColumn.json_to_string(value), column)
else super
end
when IPAddr
case sql_type
when 'inet', 'cidr' then super(PostgreSQLColumn.cidr_to_string(value), column)
else super
end
when Float
if value.infinite? && column.type == :datetime
"'#{value.to_s.downcase}'"
elsif value.infinite? || value.nan?
"'#{value.to_s}'"
else
super
end
when Numeric
if sql_type == 'money' || [:string, :text].include?(column.type)
"'#{value}'"
else
super
end
when String
case sql_type
when 'bytea' then "'#{escape_bytea(value)}'"
when 'xml' then "xml '#{quote_string(value)}'"
when /^bit/
case value
when /^[01]*$/ then "B'#{value}'"
when /^[0-9A-F]*$/ then "X'#{value}'"
end
else
super
end
else
super
end
end