diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2011-05-18 09:05:18 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2011-05-18 09:05:18 -0700 |
commit | 66afefdd79dbde92a64c3678f9ba748948080566 (patch) | |
tree | 9078aa8b63691c15c33990c8a5c40bbfbc32e180 | |
parent | 77b70c72a8878cb909088f412acf0739907ef145 (diff) | |
parent | 486a890505e268ddbc37edf7f1b32fbebd337fc1 (diff) | |
download | rails-66afefdd79dbde92a64c3678f9ba748948080566.tar.gz rails-66afefdd79dbde92a64c3678f9ba748948080566.tar.bz2 rails-66afefdd79dbde92a64c3678f9ba748948080566.zip |
Merge pull request #1122 from amatsuda/postgres_schema_queries
improvements on PostgreSQL schema queries
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb | 41 |
1 files changed, 15 insertions, 26 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index f4beeceb61..4c3fcfc70e 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -701,11 +701,11 @@ module ActiveRecord schemas = schema_search_path.split(/,/).map { |p| quote(p) }.join(',') result = query(<<-SQL, name) SELECT distinct i.relname, d.indisunique, d.indkey, t.oid - FROM pg_class t, pg_class i, pg_index d + FROM pg_class t + INNER JOIN pg_index d ON t.oid = d.indrelid + INNER JOIN pg_class i ON d.indexrelid = i.oid WHERE i.relkind = 'i' - AND d.indexrelid = i.oid AND d.indisprimary = 'f' - AND t.oid = d.indrelid AND t.relname = '#{table_name}' AND i.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname IN (#{schemas}) ) ORDER BY i.relname @@ -820,19 +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_namespace name, - 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] @@ -845,16 +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_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 + 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 |