detect primary keys for views (fix #217)

This commit is contained in:
Ruslan Talpa
2015-09-24 23:30:47 +03:00
parent 6bd6108619
commit 8783615ebc
2 changed files with 113 additions and 10 deletions
+25 -10
View File
@@ -110,16 +110,31 @@ columns table = do
primaryKeyColumns :: QualifiedIdentifier -> H.Tx P.Postgres s [Text]
primaryKeyColumns table = do
r <- H.listEx $ [H.stmt|
select kc.column_name
from
information_schema.table_constraints tc,
information_schema.key_column_usage kc
where
tc.constraint_type = 'PRIMARY KEY'
and kc.table_name = tc.table_name and kc.table_schema = tc.table_schema
and kc.constraint_name = tc.constraint_name
and kc.table_schema = ?
and kc.table_name = ? |] (qiSchema table) (qiName table)
WITH table_pk AS
( SELECT kc.table_schema,
kc.table_name,
kc.column_name
FROM information_schema.table_constraints tc,
information_schema.key_column_usage kc
WHERE tc.constraint_type = 'PRIMARY KEY'
AND kc.table_name = tc.table_name
AND kc.table_schema = tc.table_schema
AND kc.constraint_name = tc.constraint_name
AND kc.table_schema = ?)
SELECT column_name
FROM table_pk
WHERE table_pk.table_name = ?
UNION
(
SELECT vcu.column_name
FROM information_schema.view_column_usage AS vcu
JOIN table_pk ON table_pk.table_schema = vcu.view_schema
AND table_pk.TABLE_NAME = vcu.TABLE_NAME
AND table_pk.column_name = vcu.column_name
WHERE vcu.view_schema = ?
AND vcu.view_name = ?
)
|] (qiSchema table) (qiName table) (qiSchema table) (qiName table)
return $ map runIdentity r
doesProcExist :: Text -> Text -> H.Tx P.Postgres s Bool