aboutsummaryrefslogblamecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb
blob: b28eb3f7580b322bf7f39f7129631ee4e4449601 (plain) (tree)
1
2
3
4
5
6
7
8

                                                         

                                     
                  

                                                                            
                                    


                                                               
                  

                                                    


                                                                                  
                                                                                         
                                                        
                                                                    


                                                          
 


                                                                                     
                                                                           


                                                      
            
                                         


           

                                                                        



                                                                







                                                               




                     
 


                      
 
                            





                                                                                                    
         

       
   
require 'active_support/core_ext/big_decimal/conversions'

module ActiveRecord
  module ConnectionAdapters # :nodoc:
    module Quoting
      # Quotes the column value to help prevent
      # {SQL injection attacks}[http://en.wikipedia.org/wiki/SQL_injection].
      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
          return "'#{quote_string(value)}'" unless column

          if column.type == :binary && column.class.respond_to?(:string_to_binary)
            "'#{quote_string(column.class.string_to_binary(value))}'" # ' (for ruby-mode)
          elsif [:integer, :float].include?(column.type)
            (column.type == :integer ? value.to_i : value.to_f).to_s
          else
            "'#{quote_string(value)}'" # ' (for ruby-mode)
          end

        when nil        then "NULL"
        when true       then (column && column.type == :integer ? '1' : quoted_true)
        when false      then (column && column.type == :integer ? '0' : quoted_false)
          # BigDecimals need to be put in a non-normalized form and quoted.
        when BigDecimal then value.to_s('F')
        when Numeric    then value.to_s
        when Date, Time then "'#{quoted_date(value)}'"
        else
          "'#{quote_string(value.to_s)}'"
        end
      end

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

      # 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)
      end

      def quoted_true
        "'t'"
      end

      def quoted_false
        "'f'"
      end

      def quoted_date(value)
        if value.acts_like?(:time)
          zone_conversion_method = ActiveRecord::Base.default_timezone == :utc ? :getutc : :getlocal
          value.respond_to?(zone_conversion_method) ? value.send(zone_conversion_method) : value
        else
          value
        end.to_s(:db)
      end
    end
  end
end