Module: ActiveRecord::ConnectionAdapters::Quoting

Included in:
AbstractAdapter
Defined in:
lib/active_record/connection_adapters/abstract/quoting.rb

Instance Method Summary collapse

Instance Method Details

#quote(value, column = nil) ⇒ Object

Quotes the column value to help prevent SQL injection attacks.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/active_record/connection_adapters/abstract/quoting.rb', line 6

def quote(value, column = nil)
  case value
    when String
      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.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
    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) ⇒ Object

Returns a quoted form of the column name. This is highly adapter specific.



34
35
36
# File 'lib/active_record/connection_adapters/abstract/quoting.rb', line 34

def quote_column_name(name)
  name
end

#quote_string(s) ⇒ Object

Quotes a string, escaping any ‘ (single quote) and \ (backslash) characters.



28
29
30
# File 'lib/active_record/connection_adapters/abstract/quoting.rb', line 28

def quote_string(s)
  s.gsub(/\\/, '\&\&').gsub(/'/, "''") # ' (for ruby-mode)
end

#quoted_date(value) ⇒ Object



46
47
48
# File 'lib/active_record/connection_adapters/abstract/quoting.rb', line 46

def quoted_date(value)
  value.strftime("%Y-%m-%d %H:%M:%S")
end

#quoted_falseObject



42
43
44
# File 'lib/active_record/connection_adapters/abstract/quoting.rb', line 42

def quoted_false
  "'f'"
end

#quoted_trueObject



38
39
40
# File 'lib/active_record/connection_adapters/abstract/quoting.rb', line 38

def quoted_true
  "'t'"
end