-
-
Notifications
You must be signed in to change notification settings - Fork 196
Expand file tree
/
Copy pathviews.sql.ts
More file actions
69 lines (68 loc) · 2.28 KB
/
views.sql.ts
File metadata and controls
69 lines (68 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import type { SQLQueryPropsWithSchemaFilterAndIdsFilter } from './common.js'
export const VIEWS_SQL = (
props: SQLQueryPropsWithSchemaFilterAndIdsFilter & {
viewIdentifierFilter?: string
}
) => /* SQL */ `
SELECT
c.oid :: int8 AS id,
n.nspname AS schema,
c.relname AS name,
-- See definition of information_schema.views
(pg_relation_is_updatable(c.oid, false) & 20) = 20 AS is_updatable,
-- A view supports INSERT if it is auto-updatable OR has an INSTEAD OF INSERT trigger
(
(pg_relation_is_updatable(c.oid, false) & 8) = 8
OR EXISTS (
SELECT 1 FROM pg_trigger t
WHERE t.tgrelid = c.oid
AND t.tgtype & 64 > 0
AND t.tgtype & 4 > 0
AND NOT t.tgisinternal
)
) AS is_insert_enabled,
-- A view supports UPDATE if it is auto-updatable OR has an INSTEAD OF UPDATE trigger
(
(pg_relation_is_updatable(c.oid, false) & 4) = 4
OR EXISTS (
SELECT 1 FROM pg_trigger t
WHERE t.tgrelid = c.oid
AND t.tgtype & 64 > 0
AND t.tgtype & 16 > 0
AND NOT t.tgisinternal
)
) AS is_update_enabled,
-- A view supports INSERT if it is auto-updatable OR has an INSTEAD OF INSERT trigger
(
(pg_relation_is_updatable(c.oid, false) & 8) = 8
OR EXISTS (
SELECT 1 FROM pg_trigger t
WHERE t.tgrelid = c.oid
AND t.tgtype & (1 << 2) > 0 -- INSTEAD OF
AND t.tgtype & (1 << 3) > 0 -- INSERT event
AND NOT t.tgisinternal
)
) AS is_insert_enabled,
-- A view supports UPDATE if it is auto-updatable OR has an INSTEAD OF UPDATE trigger
(
(pg_relation_is_updatable(c.oid, false) & 4) = 4
OR EXISTS (
SELECT 1 FROM pg_trigger t
WHERE t.tgrelid = c.oid
AND t.tgtype & (1 << 2) > 0 -- INSTEAD OF
AND t.tgtype & (1 << 4) > 0 -- UPDATE event
AND NOT t.tgisinternal
)
) AS is_update_enabled,
obj_description(c.oid) AS comment
FROM
pg_class c
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE
${props.schemaFilter ? `n.nspname ${props.schemaFilter} AND` : ''}
${props.idsFilter ? `c.oid ${props.idsFilter} AND` : ''}
${props.viewIdentifierFilter ? `(n.nspname || '.' || c.relname) ${props.viewIdentifierFilter} AND` : ''}
c.relkind = 'v'
${props.limit ? `limit ${props.limit}` : ''}
${props.offset ? `offset ${props.offset}` : ''}
`