Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #9811: refactored_string_to_time_date_with_psql.patch

File refactored_string_to_time_date_with_psql.patch, 4.5 kB (added by tarmo, 1 year ago)
  • a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb

    old new  
    110110        def string_to_date(string) 
    111111          return string unless string.is_a?(String) 
    112112 
    113           new_date *ParseDate.parsedate(string)[0..2] 
     113          if t = fast_string_to_date(string) 
     114            t 
     115          else 
     116            new_date(*ParseDate.parsedate(string)[0..2]) 
     117          end 
    114118        end 
    115119 
    116120        def string_to_time(string) 
    117121          return string unless string.is_a?(String) 
    118122          return nil if string.empty? 
    119123 
    120           time_hash = Date._parse(string) 
    121           time_hash[:sec_fraction] = microseconds(time_hash) 
     124          if t = fast_string_to_time(string) 
     125            t 
     126          else 
     127            time_hash = Date._parse(string) 
     128            time_hash[:sec_fraction] = microseconds(time_hash) 
    122129 
    123           new_time *time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction) 
     130            new_time *time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction) 
     131          end 
    124132        end 
    125133 
    126134        def string_to_dummy_time(string) 
     
    174182            # Append zero calendar reform start to account for dates skipped by calendar reform 
    175183            DateTime.new(year, mon, mday, hour, min, sec, zone_offset, 0) rescue nil 
    176184          end 
     185 
     186          def fast_string_to_time(string) 
     187          end 
     188 
     189          def fast_string_to_date(string) 
     190          end 
    177191      end 
    178192 
    179193      private 
  • a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb

    old new  
    111111      end 
    112112 
    113113      class << self 
    114         def string_to_date(string) 
    115           return string unless string.is_a?(String) 
    116           return nil if string.empty? 
    117  
     114        protected 
     115        def fast_string_to_date(string) 
    118116          if string =~ Format::DATE 
    119117            new_date $1.to_i, $2.to_i, $3.to_i 
    120           else 
    121             super 
    122118          end 
    123119        end 
    124120 
    125         def string_to_time(string) 
    126           return string unless string.is_a?(String) 
    127           return nil if string.empty? 
    128  
     121        def fast_string_to_time(string) 
    129122          if string =~ Format::DATETIME 
    130123            new_time $1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i, $7.to_i 
    131           else 
    132             super 
    133124          end 
    134125        end 
    135126      end 
  • a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb

    old new  
    2727  module ConnectionAdapters 
    2828    # PostgreSQL-specific extensions to column definitions in a table. 
    2929    class PostgreSQLColumn < Column #:nodoc: 
     30      module Format 
     31        DATETIME = /\A(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)(?:\+(\d{2}))?\z/ 
     32      end 
     33 
    3034      # Instantiates a new PostgreSQL column definition in a table. 
    3135      def initialize(name, default, sql_type = nil, null = true) 
    3236        super(name, self.class.extract_value_from_default(default), sql_type, null) 
    3337      end 
    3438 
     39      class << self 
     40        protected 
     41        def fast_string_to_date(string) 
     42          if string =~ Format::DATETIME 
     43            new_date $1.to_i, $2.to_i, $3.to_i 
     44          end 
     45        end 
     46 
     47        def fast_string_to_time(string) 
     48          if string =~ Format::DATETIME 
     49            new_time $1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i, $7.to_i 
     50          end 
     51        end 
     52      end 
     53 
    3554      private 
    3655        # Extracts the scale from PostgreSQL-specific data types. 
    3756        def extract_scale(sql_type) 
     
    4564          # depending on the server specifics 
    4665          super 
    4766        end 
    48    
     67 
    4968        # Escapes binary strings for bytea input to the database. 
    5069        def self.string_to_binary(value) 
    5170          if PGconn.respond_to?(:escape_bytea)