-
Notifications
You must be signed in to change notification settings - Fork 0
/
catalog_queries.sql
146 lines (135 loc) · 4.42 KB
/
catalog_queries.sql
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
WITH T AS (
select c.relname as object_name,
ns.nspname AS "schema_name",
c.oid as object_id,
a.attname as "column_name",
a.attnum as column_number,
format_type(a.atttypid, a.atttypmod) AS data_type,
a.atttypmod as max_length,
NULL as "precision",
NULL as "scale",
a.attnotnull,
a.atthasdef,
a.attidentity,
col_description (c.oid, a.attnum) AS column_comment
FROM pg_class AS c
JOIN pg_namespace as ns ON (c.relnamespace = ns.oid)
JOIN pg_attribute as a ON (c.oid = a.attrelid)
WHERE ns.nspname NOT IN (''pg_toast'', ''pg_catalog'', ''information_schema'')
)
SELECT json_agg(T)::text FROM T;
--
WITH C(connection_string) AS (
select template_render(
template.odbc_template,
json_object(
'server',
server,
'port',
port,
'database',
database,
'username',
username,
'password',
password
),
json_object('expression', json_array('<<', '>>'))
)
FROM rule4_dataserver as ds
JOIN odbc_template as template ON (
ds.dialect = template.dialect
and template.name = 'connection'
)
WHERE ds.dialect = 'postgres'
)
SELECT connection_string,
OPENROWSET_JSON(
C.connection_string,
"SELECT boot_val,reset_val FROM pg_settings"
)
FROM C;
/*
*/
WITH C(connection_string) AS (
select template_render(
template.odbc_template,
json_object(
'server',
server,
'port',
port,
'database',
database,
'username',
username,
'password',
password
),
json_object('expression', json_array('<<', '>>'))
)
FROM rule4_dataserver as ds
JOIN odbc_template as template ON (
ds.dialect = template.dialect
and template.name = 'connection'
)
WHERE ds.dialect = 'postgres'
)
SELECT JE.value
FROM C,
json_each(
openrowset_clob(
C.connection_string,
'WITH T AS (SELECT * FROM food_des) SELECT json_agg(T)::text FROM T;'
)
) as JE;
CREATE VIEW rule4_dataserver_connection_string(dataserver_name, connection_string)
AS
create virtual table [rule4_dataserver_connection_string]
using define((
WITH T(dataserver_name, database_name) AS (
SELECT E.value->>'$.dataserver_name' as dataserver_name,
E.value->>'$.database_name' as database_name
FROM JSON_EACH(:j) AS E
)
select ds.dataserver_name,
template_render(
template.odbc_template,
json_object(
'server',
server,
'port',
port,
'database', T.database_name,
'username',
cred.username,
'password',
cred.password
),
json_object('expression', json_array('<<', '>>'))
) as connection_string
FROM T
JOIN rule4_dataserver as ds
ON (T.dataserver_name = ds.dataserver_name)
JOIN odbc_template as template ON (
ds.dialect = template.dialect
and template.name = 'connection_tvp'
)
LEFT OUTER JOIN rule4_credential as cred
ON (ds.dataserver_name = cred.dataserver_name)
));
CREATE TABLE rule4_dataserver (dataserver_name varchar PRIMARY KEY,
dialect varchar not null,
server varchar not null,
port varchar null
);
CREATE TABLE rule4_credential(
dataserver_name VARCHAR REFERENCES rule4_dataserver(dataserver_name),
username varchar NOT NULL,
password varchar null,
PRIMARY KEY(dataserver_name, [username])
);
INSERT INTO rule4_dataserver VALUES('TGRID_MSSQL', 'mssql','.\TGRID4ALL',NULL);
INSERT INTO rule4_dataserver VALUES('PG_mac', 'postgres','Pauls-Mac-mini.local','5432');
INSERT INTO rule4_credential(dataserver_name, username, password)
VALUES ('PG_mac', 'tgrid', 'tgrid');