diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2011-04-11 13:57:00 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2011-04-11 13:57:00 -0700 |
commit | a9e8554b46a29573094eec139b16778e29e5c37a (patch) | |
tree | 61549ae356a6892f9e5770feba07b3eca2c30146 | |
parent | 5df072d64bf24213c1eb99b3762b4fd597e28903 (diff) | |
download | rails-a9e8554b46a29573094eec139b16778e29e5c37a.tar.gz rails-a9e8554b46a29573094eec139b16778e29e5c37a.tar.bz2 rails-a9e8554b46a29573094eec139b16778e29e5c37a.zip |
use prepared statements for primary key queries
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index c179be828e..468f8d75d7 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -796,8 +796,21 @@ module ActiveRecord # Returns just a table's primary key def primary_key(table) - pk_and_sequence = pk_and_sequence_for(table) - pk_and_sequence && pk_and_sequence.first + row = exec_query(<<-end_sql, 'SCHEMA', [[nil, table]]).rows.first + SELECT DISTINCT(attr.attname) + FROM pg_attribute attr, + pg_depend dep, + pg_namespace name, + pg_constraint cons + WHERE attr.attrelid = dep.refobjid + AND attr.attnum = dep.refobjsubid + AND attr.attrelid = cons.conrelid + AND attr.attnum = cons.conkey[1] + AND cons.contype = 'p' + AND dep.refobjid = $1::regclass + end_sql + + row && row.first end # Renames a table. |