quoting.rb 7.8 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
        _quote(value)
15 16
      end

17 18 19
      # 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.
20
      def type_cast(value, column = nil)
21 22
        value = id_value_for_database(value) if value.is_a?(Base)

23
        if column
24
          ActiveSupport::Deprecation.warn(<<~MSG)
25 26 27 28
            Passing a column to `type_cast` is deprecated and will be removed in Rails 6.2.
          MSG
          type = lookup_cast_type_from_column(column)
          value = type.serialize(value)
29 30
        end

31
        _type_cast(value)
32 33
      end

34 35 36
      # 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
37 38 39
      # 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
40 41 42 43 44 45 46
      # 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 lookup_cast_type_from_column(column) # :nodoc:
        lookup_cast_type(column.sql_type)
      end

47 48
      # Quotes a string, escaping any ' (single quote) and \ (backslash)
      # characters.
49
      def quote_string(s)
50
        s.gsub('\\', '\&\&').gsub("'", "''") # ' (for ruby-mode)
51 52
      end

53 54
      # Quotes the column name. Defaults to no quoting.
      def quote_column_name(column_name)
55
        column_name.to_s
56 57 58 59 60
      end

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

63 64 65
      # Override to return the quoted table name for assignment. Defaults to
      # table quoting.
      #
66
      # This works for mysql2 where table.column can be used to
67 68
      # resolve ambiguity.
      #
69
      # We override this in the sqlite3 and postgresql adapters to use only
70 71 72 73 74
      # the column name (as per syntax requirements).
      def quote_table_name_for_assignment(table, attr)
        quote_table_name("#{table}.#{attr}")
      end

75 76 77 78 79 80 81
      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
82 83
      end

84
      def quoted_true
85
        "TRUE"
86
      end
87

88
      def unquoted_true
89
        true
90 91
      end

92
      def quoted_false
93
        "FALSE"
94
      end
95

96
      def unquoted_false
97
        false
98 99
      end

100 101
      # Quote date/time values for use in SQL input. Includes microseconds
      # if the value is a Time responding to usec.
102
      def quoted_date(value)
103
        if value.acts_like?(:time)
104 105 106 107
          if ActiveRecord::Base.default_timezone == :utc
            value = value.getutc if value.respond_to?(:getutc) && !value.utc?
          else
            value = value.getlocal if value.respond_to?(:getlocal)
J
Jon Leighton 已提交
108 109 110
          end
        end

111 112
        result = value.to_s(:db)
        if value.respond_to?(:usec) && value.usec > 0
113
          result << "." << sprintf("%06d", value.usec)
114 115 116
        else
          result
        end
117
      end
118

119
      def quoted_time(value) # :nodoc:
120
        value = value.change(year: 2000, month: 1, day: 1)
121
        quoted_date(value).sub(/\A\d\d\d\d-\d\d-\d\d /, "")
122 123
      end

124 125 126 127
      def quoted_binary(value) # :nodoc:
        "'#{quote_string(value.to_s)}'"
      end

128 129 130 131
      def sanitize_as_sql_comment(value) # :nodoc:
        value.to_s.gsub(%r{ (/ (?: | \g<1>) \*) \+? \s* | \s* (\* (?: | \g<2>) /) }x, "")
      end

132 133 134 135 136 137 138 139 140 141 142 143 144
      def column_name_matcher # :nodoc:
        COLUMN_NAME
      end

      def column_name_with_order_matcher # :nodoc:
        COLUMN_NAME_WITH_ORDER
      end

      # Regexp for column names (with or without a table name prefix).
      # Matches the following:
      #
      #   "#{table_name}.#{column_name}"
      #   "#{column_name}"
145 146 147
      COLUMN_NAME = /
        \A
        (
148 149 150 151
          (?:
            # table_name.column_name | function(one or no argument)
            ((?:\w+\.)?\w+) | \w+\((?:|\g<2>)\)
          )
152
          (?:(?:\s+AS)?\s+\w+)?
153 154 155 156
        )
        (?:\s*,\s*\g<1>)*
        \z
      /ix
157 158 159 160 161 162 163 164 165 166 167 168 169 170

      # Regexp for column names with order (with or without a table name prefix,
      # with or without various order modifiers). Matches the following:
      #
      #   "#{table_name}.#{column_name}"
      #   "#{table_name}.#{column_name} #{direction}"
      #   "#{table_name}.#{column_name} #{direction} NULLS FIRST"
      #   "#{table_name}.#{column_name} NULLS LAST"
      #   "#{column_name}"
      #   "#{column_name} #{direction}"
      #   "#{column_name} #{direction} NULLS FIRST"
      #   "#{column_name} NULLS LAST"
      COLUMN_NAME_WITH_ORDER = /
        \A
171
        (
172 173 174 175
          (?:
            # table_name.column_name | function(one or no argument)
            ((?:\w+\.)?\w+) | \w+\((?:|\g<2>)\)
          )
176 177 178 179
          (?:\s+ASC|\s+DESC)?
          (?:\s+NULLS\s+(?:FIRST|LAST))?
        )
        (?:\s*,\s*\g<1>)*
180 181 182 183 184
        \z
      /ix

      private_constant :COLUMN_NAME, :COLUMN_NAME_WITH_ORDER

185 186
      private
        def type_casted_binds(binds)
187 188
          case binds.first
          when Array
189 190
            binds.map { |column, value| type_cast(value, column) }
          else
191 192 193 194 195 196 197
            binds.map do |value|
              if ActiveModel::Attribute === value
                type_cast(value.value_for_database)
              else
                type_cast(value)
              end
            end
198
          end
199 200
        end

201 202 203 204
        def lookup_cast_type(sql_type)
          type_map.lookup(sql_type)
        end

205 206 207 208 209 210
        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

211 212
        def _quote(value)
          case value
213
          when String, Symbol, ActiveSupport::Multibyte::Chars
214 215 216 217 218 219 220
            "'#{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
221
          when Type::Binary::Data then quoted_binary(value)
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
          when Type::Time::Value then "'#{quoted_time(value)}'"
          when Date, Time then "'#{quoted_date(value)}'"
          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")
237
          when nil, Numeric, String then value
238 239
          when Type::Time::Value then quoted_time(value)
          when Date, Time then quoted_date(value)
240
          else raise TypeError, "can't cast #{value.class.name}"
241
          end
242
        end
243 244
    end
  end
245
end