aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAkira Matsuda <ronnie@dio.jp>2011-05-18 18:31:34 +0900
committerAkira Matsuda <ronnie@dio.jp>2011-05-18 18:51:03 +0900
commit486a890505e268ddbc37edf7f1b32fbebd337fc1 (patch)
treeb69b524e52f50d1cc4c88a502cb12b9acd11772f
parentdf13f55d757dc8af30391b8d15af2c2d04f26aa4 (diff)
downloadrails-486a890505e268ddbc37edf7f1b32fbebd337fc1.tar.gz
rails-486a890505e268ddbc37edf7f1b32fbebd337fc1.tar.bz2
rails-486a890505e268ddbc37edf7f1b32fbebd337fc1.zip
why not use JOIN statement for joining tables? (vol. 2)
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb33
1 files changed, 12 insertions, 21 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
index 196e5630ae..4c3fcfc70e 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -820,18 +820,13 @@ module ActiveRecord
# given table's primary key.
result = exec_query(<<-end_sql, 'SCHEMA').rows.first
SELECT attr.attname, seq.relname
- FROM pg_class seq,
- pg_attribute attr,
- pg_depend dep,
- pg_constraint cons
- WHERE seq.oid = dep.objid
- AND seq.relkind = 'S'
- AND 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 = '#{quote_table_name(table)}'::regclass
+ FROM pg_class seq
+ INNER JOIN pg_depend dep on seq.oid = dep.objid
+ INNER JOIN pg_attribute attr ON attr.attrelid = dep.refobjid AND attr.attnum = dep.refobjsubid
+ INNER JOIN pg_constraint cons ON attr.attrelid = cons.conrelid AND attr.attnum = cons.conkey[1]
+ WHERE seq.relkind = 'S'
+ AND cons.contype = 'p'
+ AND dep.refobjid = '#{quote_table_name(table)}'::regclass
end_sql
# [primary_key, sequence]
@@ -844,15 +839,11 @@ module ActiveRecord
def primary_key(table)
row = exec_query(<<-end_sql, 'SCHEMA', [[nil, table]]).rows.first
SELECT DISTINCT(attr.attname)
- FROM pg_attribute attr,
- pg_depend dep,
- 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
+ FROM pg_attribute attr
+ INNER JOIN pg_depend dep ON attr.attrelid = dep.refobjid AND attr.attnum = dep.refobjsubid
+ INNER JOIN pg_constraint cons ON attr.attrelid = cons.conrelid AND attr.attnum = cons.conkey[1]
+ WHERE cons.contype = 'p'
+ AND dep.refobjid = $1::regclass
end_sql
row && row.first