quoting.rb 6.3 KB
Newer Older
1 2
# frozen_string_literal: true

3
require "active_support/core_ext/big_decimal/conversions"
4
require "active_support/multibyte/chars"
J
Jeremy Kemper 已提交
5

6 7 8
module ActiveRecord
  module ConnectionAdapters # :nodoc:
    module Quoting
9
      # Quotes the column value to help prevent
10
      # {SQL injection attacks}[https://en.wikipedia.org/wiki/SQL_injection].
11
      def quote(value)
12 13
        value = id_value_for_database(value) if value.is_a?(Base)

14 15 16 17
        if value.respond_to?(:value_for_database)
          value = value.value_for_database
        end

18
        _quote(value)
19 20
      end

21 22 23
      # 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.
24
      def type_cast(value, column = nil)
25 26
        value = id_value_for_database(value) if value.is_a?(Base)

27
        if column
28
          value = type_cast_from_column(column, value)
29 30
        end

31 32 33 34
        _type_cast(value)
      rescue TypeError
        to_type = column ? " to #{column.type}" : ""
        raise TypeError, "can't cast #{value.class}#{to_type}"
35 36
      end

37 38 39
      # If you are having to call this function, you are likely doing something
      # wrong. The column does not have sufficient type information if the user
      # provided a custom type on the class level either explicitly (via
40 41 42
      # Attributes::ClassMethods#attribute) or implicitly (via
      # AttributeMethods::Serialization::ClassMethods#serialize, +time_zone_aware_attributes+).
      # In almost all cases, the sql type should only be used to change quoting behavior, when the primitive to
43 44 45 46 47 48
      # represent the type doesn't sufficiently reflect the differences
      # (varchar vs binary) for example. The type used to get this primitive
      # should have been provided before reaching the connection adapter.
      def type_cast_from_column(column, value) # :nodoc:
        if column
          type = lookup_cast_type_from_column(column)
49
          type.serialize(value)
50 51 52 53 54
        else
          value
        end
      end

55
      # See docs for #type_cast_from_column
56 57 58 59
      def lookup_cast_type_from_column(column) # :nodoc:
        lookup_cast_type(column.sql_type)
      end

60 61
      # Quotes a string, escaping any ' (single quote) and \ (backslash)
      # characters.
62
      def quote_string(s)
63
        s.gsub('\\', '\&\&').gsub("'", "''") # ' (for ruby-mode)
64 65
      end

66 67
      # Quotes the column name. Defaults to no quoting.
      def quote_column_name(column_name)
68
        column_name.to_s
69 70 71 72 73
      end

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

76 77 78
      # Override to return the quoted table name for assignment. Defaults to
      # table quoting.
      #
79
      # This works for mysql2 where table.column can be used to
80 81
      # resolve ambiguity.
      #
82
      # We override this in the sqlite3 and postgresql adapters to use only
83 84 85 86 87
      # the column name (as per syntax requirements).
      def quote_table_name_for_assignment(table, attr)
        quote_table_name("#{table}.#{attr}")
      end

88 89 90 91 92 93 94
      def quote_default_expression(value, column) # :nodoc:
        if value.is_a?(Proc)
          value.call
        else
          value = lookup_cast_type(column.sql_type).serialize(value)
          quote(value)
        end
95 96
      end

97
      def quoted_true
98
        "TRUE"
99
      end
100

101
      def unquoted_true
102
        true
103 104
      end

105
      def quoted_false
106
        "FALSE"
107
      end
108

109
      def unquoted_false
110
        false
111 112
      end

113 114
      # Quote date/time values for use in SQL input. Includes microseconds
      # if the value is a Time responding to usec.
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

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

124 125 126 127 128 129
        result = value.to_s(:db)
        if value.respond_to?(:usec) && value.usec > 0
          "#{result}.#{sprintf("%06d", value.usec)}"
        else
          result
        end
130
      end
131

132
      def quoted_time(value) # :nodoc:
133
        value = value.change(year: 2000, month: 1, day: 1)
134
        quoted_date(value).sub(/\A\d\d\d\d-\d\d-\d\d /, "")
135 136
      end

137 138 139 140
      def quoted_binary(value) # :nodoc:
        "'#{quote_string(value.to_s)}'"
      end

141 142 143 144 145
      def type_casted_binds(binds) # :nodoc:
        if binds.first.is_a?(Array)
          binds.map { |column, value| type_cast(value, column) }
        else
          binds.map { |attr| type_cast(attr.value_for_database) }
146
        end
147
      end
148

149
      private
150 151 152 153
        def lookup_cast_type(sql_type)
          type_map.lookup(sql_type)
        end

154 155 156 157 158 159
        def id_value_for_database(value)
          if primary_key = value.class.primary_key
            value.instance_variable_get(:@attributes)[primary_key].value_for_database
          end
        end

160 161
        def _quote(value)
          case value
162
          when String, ActiveSupport::Multibyte::Chars
163 164 165 166 167 168 169
            "'#{quote_string(value.to_s)}'"
          when true       then quoted_true
          when false      then quoted_false
          when nil        then "NULL"
          # BigDecimals need to be put in a non-normalized form and quoted.
          when BigDecimal then value.to_s("F")
          when Numeric, ActiveSupport::Duration then value.to_s
170
          when Type::Binary::Data then quoted_binary(value)
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
          when Type::Time::Value then "'#{quoted_time(value)}'"
          when Date, Time then "'#{quoted_date(value)}'"
          when Symbol     then "'#{quote_string(value.to_s)}'"
          when Class      then "'#{value}'"
          else raise TypeError, "can't quote #{value.class.name}"
          end
        end

        def _type_cast(value)
          case value
          when Symbol, ActiveSupport::Multibyte::Chars, Type::Binary::Data
            value.to_s
          when true       then unquoted_true
          when false      then unquoted_false
          # BigDecimals need to be put in a non-normalized form and quoted.
          when BigDecimal then value.to_s("F")
187
          when nil, Numeric, String then value
188 189 190 191
          when Type::Time::Value then quoted_time(value)
          when Date, Time then quoted_date(value)
          else raise TypeError
          end
192
        end
193 194
    end
  end
195
end