diff options
author | Tobias Lütke <tobias.luetke@gmail.com> | 2006-04-04 18:53:40 +0000 |
---|---|---|
committer | Tobias Lütke <tobias.luetke@gmail.com> | 2006-04-04 18:53:40 +0000 |
commit | 5821096a52843244a11814d2c6e7158451227933 (patch) | |
tree | ca39a4884b5d63dc85414871d369783f7e842a09 /activerecord/lib/active_record | |
parent | 6b3bc1bc5216b7859552a131dbd8cdf843982119 (diff) | |
download | rails-5821096a52843244a11814d2c6e7158451227933.tar.gz rails-5821096a52843244a11814d2c6e7158451227933.tar.bz2 rails-5821096a52843244a11814d2c6e7158451227933.zip |
Multiple fixes and optimizations in PostgreSQL adapter, allowing ruby-postgres gem to work properly. Closes #4461
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4156 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb | 32 |
1 files changed, 25 insertions, 7 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 53f47b2ad6..ba26b05a17 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -24,6 +24,8 @@ module ActiveRecord PGconn.connect(host, port, "", "", database, username, password), logger, config ) + PGconn.translate_results = false if PGconn.respond_to? :translate_results= + pga.schema_search_path = config[:schema_search_path] || config[:schema_order] pga @@ -63,8 +65,9 @@ module ActiveRecord @connection.query 'SELECT 1' true end - rescue PGError - false + # postgres-pr raises a NoMethodError when querying if no conn is available + rescue PGError, NoMethodError + false end # Close then reopen the connection. @@ -339,6 +342,8 @@ module ActiveRecord private BYTEA_COLUMN_TYPE_OID = 17 + TIMESTAMPOID = 1114 + TIMESTAMPTZOID = 1184 def configure_connection if @config[:encoding] @@ -355,7 +360,7 @@ module ActiveRecord def select(sql, name = nil) res = execute(sql, name) - results = res.result + results = res.result rows = [] if results.length > 0 fields = res.fields @@ -363,9 +368,14 @@ module ActiveRecord hashed_row = {} row.each_index do |cel_index| column = row[cel_index] - if res.type(cel_index) == BYTEA_COLUMN_TYPE_OID - column = unescape_bytea(column) + + case res.type(cel_index) + when BYTEA_COLUMN_TYPE_OID + column = unescape_bytea(column) + when TIMESTAMPTZOID, TIMESTAMPOID + column = cast_to_time(column) end + hashed_row[fields[cel_index]] = column end rows << hashed_row @@ -472,8 +482,8 @@ module ActiveRecord return "t" if value =~ /true/i return "f" if value =~ /false/i - # Char/String type values - return $1 if value =~ /^'(.*)'::(bpchar|text|character varying)$/ + # Char/String/Bytea type values + return $1 if value =~ /^'(.*)'::(bpchar|text|character varying|bytea)$/ # Numeric values return value if value =~ /^-?[0-9]+(\.[0-9]*)?/ @@ -485,6 +495,14 @@ module ActiveRecord # and we can't know the value of that, so return nil. return nil end + + # Only needed for DateTime instances + def cast_to_time(value) + return value unless value.class == DateTime + v = value + time_array = [v.year, v.month, v.day, v.hour, v.min, v.sec] + Time.send(Base.default_timezone, *time_array) rescue nil + end end end end |