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 110 110 def string_to_date(string) 111 111 return string unless string.is_a?(String) 112 112 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 114 118 end 115 119 116 120 def string_to_time(string) 117 121 return string unless string.is_a?(String) 118 122 return nil if string.empty? 119 123 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) 122 129 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 124 132 end 125 133 126 134 def string_to_dummy_time(string) … … 174 182 # Append zero calendar reform start to account for dates skipped by calendar reform 175 183 DateTime.new(year, mon, mday, hour, min, sec, zone_offset, 0) rescue nil 176 184 end 185 186 def fast_string_to_time(string) 187 end 188 189 def fast_string_to_date(string) 190 end 177 191 end 178 192 179 193 private -
a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
old new 111 111 end 112 112 113 113 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) 118 116 if string =~ Format::DATE 119 117 new_date $1.to_i, $2.to_i, $3.to_i 120 else121 super122 118 end 123 119 end 124 120 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) 129 122 if string =~ Format::DATETIME 130 123 new_time $1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i, $7.to_i 131 else132 super133 124 end 134 125 end 135 126 end -
a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
old new 27 27 module ConnectionAdapters 28 28 # PostgreSQL-specific extensions to column definitions in a table. 29 29 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 30 34 # Instantiates a new PostgreSQL column definition in a table. 31 35 def initialize(name, default, sql_type = nil, null = true) 32 36 super(name, self.class.extract_value_from_default(default), sql_type, null) 33 37 end 34 38 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 35 54 private 36 55 # Extracts the scale from PostgreSQL-specific data types. 37 56 def extract_scale(sql_type) … … 45 64 # depending on the server specifics 46 65 super 47 66 end 48 67 49 68 # Escapes binary strings for bytea input to the database. 50 69 def self.string_to_binary(value) 51 70 if PGconn.respond_to?(:escape_bytea)