module BigRecord::ConnectionAdapters::Quoting
Public Instance Methods
quote(value, column = nil)
click to toggle source
Quotes the column value to help prevent SQL injection attacks [en.wikipedia.org/wiki/SQL_injection].
# File lib/big_record/connection_adapters/abstract/quoting.rb, line 6 def quote(value, column = nil) # records are quoted as their primary key return value.quoted_id if value.respond_to?(:quoted_id) case value when String, ActiveSupport::Multibyte::Chars value = value.to_s if column && column.type == :binary && column.class.respond_to?(:string_to_binary) "'#{quote_string(column.class.string_to_binary(value))}'" # ' (for ruby-mode) elsif column && [:integer, :float].include?(column.type) value = column.type == :integer ? value.to_i : value.to_f value.to_s else "'#{quote_string(value)}'" # ' (for ruby-mode) end when NilClass then "NULL" when TrueClass then (column && column.type == :integer ? '1' : quoted_true) when FalseClass then (column && column.type == :integer ? '0' : quoted_false) when Float, Fixnum, Bignum then value.to_s # BigDecimals need to be output in a non-normalized form and quoted. when BigDecimal then value.to_s('F') when Date then "'#{value.to_s}'" when Time, DateTime then "'#{quoted_date(value)}'" else "'#{quote_string(value.to_yaml)}'" end end
quote_column_name(name)
click to toggle source
Returns a quoted form of the column name. This is highly adapter specific.
# File lib/big_record/connection_adapters/abstract/quoting.rb, line 41 def quote_column_name(name) name end
quote_string(s)
click to toggle source
Quotes a string, escaping any ' (single quote) and \ (backslash) characters.
# File lib/big_record/connection_adapters/abstract/quoting.rb, line 35 def quote_string(s) s.gsub(/\/, '\&\&').gsub(/'/, "''") # ' (for ruby-mode) end
quoted_date(value)
click to toggle source
# File lib/big_record/connection_adapters/abstract/quoting.rb, line 53 def quoted_date(value) value.strftime("%Y-%m-%d %H:%M:%S") end
quoted_false()
click to toggle source
# File lib/big_record/connection_adapters/abstract/quoting.rb, line 49 def quoted_false "'f'" end
quoted_true()
click to toggle source
# File lib/big_record/connection_adapters/abstract/quoting.rb, line 45 def quoted_true "'t'" end