Skip to content
Browse files

Optimize postgresql selects.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2548 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent a91423e commit 140a5f8f7b7deea713b5c7cbccbc8b57fecb2c7d Marcel Molina committed Oct 13, 2005
Showing with 20 additions and 18 deletions.
  1. +2 −0 activerecord/CHANGELOG
  2. +18 −18 activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
View
2 activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Optimize postgresql selects. [skaes@web.de]
+
* Update DB2 adapter. #2206. [contact@maik-schmidt.de]
* Corrections to SQLServer native data types. #2267. [rails.20.clarry@spamgourmet.com]
View
36 activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -250,24 +250,24 @@ def last_insert_id(table, column = nil)
def select(sql, name = nil)
res = execute(sql, name)
results = res.result
- rows = []
- if results.length > 0
- fields = res.fields
- results.each do |row|
- 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)
- end
- hashed_row[fields[cel_index]] = column
- end
- rows << hashed_row
- end
- end
- return rows
- end
-
+ rows = []
+ if results.length > 0
+ fields = res.fields
+ hash_prototype = fields.inject({}){ |proto, field| proto[field] = nil; proto }
+ results.each do |row|
+ hashed_row = hash_prototype.clone
+ row.each_index do |col_num|
+ if res.type(col_num) == BYTEA_COLUMN_TYPE_OID
+ hashed_row[fields[col_num]] = unescape_bytea(row[col_num])
+ else
+ hashed_row[fields[col_num]] = row[col_num]
+ end
+ end
+ rows << hashed_row
+ end
+ end
+ rows
+ end
def escape_bytea(s)
if PGconn.respond_to? :escape_bytea
self.class.send(:define_method, :escape_bytea) do |s|

0 comments on commit 140a5f8

Please sign in to comment.
Something went wrong with that request. Please try again.