quoting.rb 4.4 KB
Newer Older
J
Jeremy Kemper 已提交
1 2
require 'active_support/core_ext/big_decimal/conversions'

3 4 5
module ActiveRecord
  module ConnectionAdapters # :nodoc:
    module Quoting
6 7
      # Quotes the column value to help prevent
      # {SQL injection attacks}[http://en.wikipedia.org/wiki/SQL_injection].
8
      def quote(value, column = nil)
9 10 11
        # records are quoted as their primary key
        return value.quoted_id if value.respond_to?(:quoted_id)

12 13 14 15 16 17 18 19 20
        # FIXME: The only case we get an object other than nil or a real column
        # is `SchemaStatements#add_column` with a PG array that has a non-empty default
        # value. Is this really the only case? Are we missing tests for other types?
        # We should have a real column object passed (or nil) here, and check for that
        # instead
        if column.respond_to?(:type_cast_for_database)
          value = column.type_cast_for_database(value)
        end

21
        case value
A
Aaron Patterson 已提交
22
        when String, ActiveSupport::Multibyte::Chars
23 24 25
          "'#{quote_string(value.to_s)}'"
        when true       then quoted_true
        when false      then quoted_false
A
Aaron Patterson 已提交
26
        when nil        then "NULL"
27
        # BigDecimals need to be put in a non-normalized form and quoted.
28 29
        when BigDecimal then value.to_s('F')
        when Numeric, ActiveSupport::Duration then value.to_s
30
        when Date, Time then "'#{quoted_date(value)}'"
P
Pratik Naik 已提交
31
        when Symbol     then "'#{quote_string(value.to_s)}'"
32
        when Class      then "'#{value.to_s}'"
A
Aaron Patterson 已提交
33
        else
34
          "'#{quote_string(YAML.dump(value))}'"
35 36 37
        end
      end

38 39 40 41
      # Cast a +value+ to a type that the database understands. For example,
      # SQLite does not understand dates, so this method will convert a Date
      # to a String.
      def type_cast(value, column)
42 43 44
        if value.respond_to?(:quoted_id) && value.respond_to?(:id)
          return value.id
        end
45

46 47 48 49 50 51 52 53 54
        # FIXME: The only case we get an object other than nil or a real column
        # is `SchemaStatements#add_column` with a PG array that has a non-empty default
        # value. Is this really the only case? Are we missing tests for other types?
        # We should have a real column object passed (or nil) here, and check for that
        # instead
        if column.respond_to?(:type_cast_for_database)
          value = column.type_cast_for_database(value)
        end

55
        case value
56
        when Symbol, ActiveSupport::Multibyte::Chars
57
          value.to_s
58 59
        when true       then unquoted_true
        when false      then unquoted_false
60
        # BigDecimals need to be put in a non-normalized form and quoted.
61
        when BigDecimal then value.to_s('F')
62
        when Date, Time then quoted_date(value)
63 64
        when *types_which_need_no_typecasting
          value
65
        else
66 67
          to_type = column ? " to #{column.type}" : ""
          raise TypeError, "can't cast #{value.class}#{to_type}"
68 69 70
        end
      end

71 72
      # Quotes a string, escaping any ' (single quote) and \ (backslash)
      # characters.
73 74 75 76
      def quote_string(s)
        s.gsub(/\\/, '\&\&').gsub(/'/, "''") # ' (for ruby-mode)
      end

77 78 79 80 81 82 83 84
      # Quotes the column name. Defaults to no quoting.
      def quote_column_name(column_name)
        column_name
      end

      # Quotes the table name. Defaults to column name quoting.
      def quote_table_name(table_name)
        quote_column_name(table_name)
85 86
      end

87 88 89 90 91 92
      # Override to return the quoted table name for assignment. Defaults to
      # table quoting.
      #
      # This works for mysql and mysql2 where table.column can be used to
      # resolve ambiguity.
      #
93
      # We override this in the sqlite and postgresql adapters to use only
94 95 96 97 98
      # the column name (as per syntax requirements).
      def quote_table_name_for_assignment(table, attr)
        quote_table_name("#{table}.#{attr}")
      end

99 100 101
      def quoted_true
        "'t'"
      end
102

103 104 105 106
      def unquoted_true
        't'
      end

107 108 109
      def quoted_false
        "'f'"
      end
110

111 112 113 114
      def unquoted_false
        'f'
      end

115
      def quoted_date(value)
116 117
        if value.acts_like?(:time)
          zone_conversion_method = ActiveRecord::Base.default_timezone == :utc ? :getutc : :getlocal
J
Jon Leighton 已提交
118 119 120 121 122 123 124

          if value.respond_to?(zone_conversion_method)
            value = value.send(zone_conversion_method)
          end
        end

        value.to_s(:db)
125
      end
126 127 128 129 130 131

      private

      def types_which_need_no_typecasting
        [nil, Numeric, String]
      end
132 133
    end
  end
134
end