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

Changeset 7780

Show
Ignore:
Timestamp:
10/07/07 22:01:59 (2 years ago)
Author:
bitsweat
Message:

Use fast date/time parsing by default. Closes #9811.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/CHANGELOG

    r7778 r7780  
    33* Raise ProtectedAttributeAssignmentError in development and test environments when mass-assigning to an attr_protected attribute.  #9802 [Henrik N] 
    44 
    5 * MySQL: speedup date/time parsing.  [Jeremy Kemper
     5* Speedup database date/time parsing.  [Jeremy Kemper, Tarmo TÀnav
    66 
    77* Fix calling .clear on a has_many :dependent=>:delete_all association. [tarmo] 
  • trunk/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb

    r7771 r7780  
    77    # An abstract definition of a column in a table. 
    88    class Column 
     9      module Format 
     10        ISO_DATE = /\A(\d{4})-(\d\d)-(\d\d)\z/ 
     11        ISO_DATETIME = /\A(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)(\.\d+)?\z/ 
     12      end 
     13 
    914      attr_reader :name, :default, :type, :limit, :null, :sql_type, :precision, :scale 
    1015      attr_accessor :primary 
     
    110115        def string_to_date(string) 
    111116          return string unless string.is_a?(String) 
    112  
    113           new_date *ParseDate.parsedate(string)[0..2] 
     117          return nil if string.empty? 
     118 
     119          fast_string_to_date(string) || fallback_string_to_date(string) 
    114120        end 
    115121 
     
    118124          return nil if string.empty? 
    119125 
    120           time_hash = Date._parse(string) 
    121           time_hash[:sec_fraction] = microseconds(time_hash) 
    122  
    123           new_time *time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction) 
     126          fast_string_to_time(string) || fallback_string_to_time(string) 
    124127        end 
    125128 
     
    174177            # Append zero calendar reform start to account for dates skipped by calendar reform 
    175178            DateTime.new(year, mon, mday, hour, min, sec, zone_offset, 0) rescue nil 
     179          end 
     180 
     181          def fast_string_to_date(string) 
     182            if string =~ Format::ISO_DATE 
     183              new_date $1.to_i, $2.to_i, $3.to_i 
     184            end 
     185          end 
     186 
     187          # Doesn't handle time zones. 
     188          def fast_string_to_time(string) 
     189            if string =~ Format::ISO_DATETIME 
     190              microsec = ($7.to_f * 10e6).to_i 
     191              new_time $1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i, microsec 
     192            end 
     193          end 
     194 
     195          def fallback_string_to_date(string) 
     196            new_date *ParseDate.parsedate(string)[0..2] 
     197          end 
     198 
     199          def fallback_string_to_time(string) 
     200            time_hash = Date._parse(string) 
     201            time_hash[:sec_fraction] = microseconds(time_hash) 
     202 
     203            new_time *time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction) 
    176204          end 
    177205      end 
  • trunk/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb

    r7771 r7780  
    9292  module ConnectionAdapters 
    9393    class MysqlColumn < Column #:nodoc: 
    94       module Format 
    95         DATE = /\A(\d{4})-(\d\d)-(\d\d)\z/ 
    96         DATETIME = /\A(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)(?:\.(\d{6}))?\z/ 
    97       end 
    98  
    9994      def extract_default(default) 
    10095        if type == :binary || type == :text 
     
    108103        else 
    109104          super 
    110         end 
    111       end 
    112  
    113       class << self 
    114         def string_to_date(string) 
    115           return string unless string.is_a?(String) 
    116           return nil if string.empty? 
    117  
    118           if string =~ Format::DATE 
    119             new_date $1.to_i, $2.to_i, $3.to_i 
    120           else 
    121             super 
    122           end 
    123         end 
    124  
    125         def string_to_time(string) 
    126           return string unless string.is_a?(String) 
    127           return nil if string.empty? 
    128  
    129           if string =~ Format::DATETIME 
    130             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 
    133           end 
    134105        end 
    135106      end