-
Notifications
You must be signed in to change notification settings - Fork 0
/
pg.js
225 lines (204 loc) · 6.89 KB
/
pg.js
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
var async = require('async');
var { Client } = require('pg');
var client = new Client({
user: 'ubuntu',
host: 'localhost',
database: 'test',
password: 'cloud9isawesome',
port: 5432,
})
var
create_tables,
insert_data,
create_view_construcor,
create_graphtables_triger,
create_graph_triger,
create_graphpart_triger,
drop_all
;
create_tables = `
create table _ancientGraphTables (
id serial UNIQUE,
tableName text,
idField text,
sourceField text,
sourceFieldTable text DEFAULT '',
targetField text,
targetFieldTable text DEFAULT '',
primary key (idField, sourceField, targetField, tableName)
);
create table _ancientGraphs (
id serial UNIQUE,
defaultGraphTable integer references _ancientGraphTables(id) ON DELETE SET NULL
);
create table _ancientGraphParts (
id serial UNIQUE,
graphTableId integer references _ancientGraphTables(id) ON DELETE CASCADE,
graphId integer references _ancientGraphs(id) ON DELETE CASCADE,
primary key (graphId, graphTableId)
);
create table _ancientLinksId (
id serial UNIQUE,
graphTableId integer references _ancientGraphTables(id) ON DELETE CASCADE,
realId integer,
primary key (graphTableId, realId)
);
create table firstPart (
"number" serial,
"from" text,
"to" integer
);
create table secondPart (
id serial,
"source" text,
"target" text
);
create table someShitDocumets (
id serial
);
create table someShitRights (
username text,
linkId integer
);
`;
insert_data = `
insert into someShitDocumets (id) values (1),(2),(3);
insert into someShitRights (username,linkId) values ('ubuntu', 2);
insert into firstPart ("from", "to") values ('someShitDocumets/1',3);
insert into secondPart ("source", "target") values ('someShitDocumets/2','someShitDocumets/3');
insert into secondPart ("source", "target") values ('someShitDocumets/3','someShitDocumets/3');
insert into _ancientGraphTables (tableName, idField, sourceField, targetField, targetFieldTable) values
('firstPart', 'number', 'from', 'to', 'someShitDocumets/'),
('secondPart', 'id', 'source', 'target', '');
insert into _ancientGraphs (id, defaultGraphTable) values (1,1);
insert into _ancientGraphParts (graphId, graphTableId) values (1,1), (1,2);
`;
create_graphtables_trigers = `
CREATE OR REPLACE FUNCTION _ancientGraphTablesInsering() RETURNS TRIGGER AS $$
BEGIN
EXECUTE ('
insert into _ancientLinksId(realId, graphTableId)
select "'||NEW.idField||'" as realId, '||NEW.id::text||' as graphTableId from '||NEW.tableName||';
');
return NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER graphtables_inputaudit
AFTER INSERT ON _ancientGraphTables
FOR EACH ROW EXECUTE PROCEDURE _ancientGraphTablesInsering();
CREATE OR REPLACE FUNCTION _ancientGraphTablesUpdating() RETURNS TRIGGER AS $$
DECLARE
onePart record;
BEGIN
for onePart in
select graphId from _ancientGraphParts where graphTableId = OLD.id
LOOP
PERFORM _ancient_create_view(onePart.graphId);
END LOOP;
RETURN old;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER graphtables_updateaudit
AFTER delete ON _ancientGraphTables
FOR EACH ROW EXECUTE PROCEDURE _ancientGraphTablesUpdating();
`;
create_graphpart_triger = `
CREATE OR REPLACE FUNCTION createGraphView() RETURNS TRIGGER AS $$
DECLARE
graphId integer;
BEGIN
IF (TG_OP = 'DELETE') THEN
graphId := OLD.graphId;
ELSE
graphId := NEW.graphId;
END IF;
PERFORM _ancient_create_view(graphId);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER graph_audit
AFTER INSERT OR DELETE ON _ancientGraphParts
FOR EACH ROW EXECUTE PROCEDURE createGraphView();
`;
create_graph_triger = `
CREATE OR REPLACE FUNCTION _ancientGraphDeleting() RETURNS TRIGGER AS $$
BEGIN
EXECUTE ('DROP VIEW _ancientViewGraph'||cast(old.id as text)||';');
RETURN old;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER graph_deleting_audit
AFTER delete ON _ancientGraphs
FOR EACH ROW EXECUTE PROCEDURE _ancientGraphDeleting();
`;
create_view_construcor = `
CREATE OR REPLACE FUNCTION _ancientGraphViewInserting() RETURNS TRIGGER AS $$
BEGIN
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION _ancient_create_view(graph integer) RETURNS void AS $$
DECLARE
oneTable record;
viewString text := '';
FirstTime boolean := true;
BEGIN
for oneTable in
select gTable.*,gParts.graphTableId from _ancientGraphParts as gParts, _ancientGraphTables as gTable where
gParts.graphId = graph and
gTable.id = gParts.graphTableId
LOOP
viewString := viewString||' union all ';
if FirstTime THEN
viewString := '';
FirstTime := false;
END IF;
viewString := viewString||'select lId.id as "id", currentTable.'
||oneTable.idField||' as "graphPartTableIdField", '''
||oneTable.sourceFieldTable||'''|| currentTable."'||oneTable.sourceField||'" as "source", '''
||oneTable.targetFieldTable||'''|| currentTable."'||oneTable.targetField||'" as "target", text '''
||oneTable.id||''' as "graphTableId" from '||oneTable.tableName||' as currentTable, _ancientLinksId as lId, someShitRights as rights
where currentTable.'||oneTable.idField||' = lId.realId
and lId.graphTableId = '||oneTable.graphTableId||'
and rights.username = current_user
and lId.id = rights.linkId
';
END LOOP;
EXECUTE ('
CREATE OR REPLACE VIEW _ancientViewGraph'||graph||' as '|| viewString ||';
DROP TRIGGER IF EXISTS graphview_insertaudit ON _ancientviewgraph'||graph||';
CREATE TRIGGER graphview_insertaudit
instead of insert ON _ancientViewGraph'||graph||'
FOR EACH ROW EXECUTE PROCEDURE _ancientGraphViewInserting();
');
END;
$$ LANGUAGE plpgsql;
`;
drop_all = `
drop view IF EXISTS _ancientViewGraph1;
drop view IF EXISTS _ancientViewGraph2;
drop table IF EXISTS firstPart;
drop table IF EXISTS secondPart;
drop table IF EXISTS someShitDocumets;
drop table IF EXISTS someShitRights;
drop table IF EXISTS _ancientGraphParts;
drop table IF EXISTS _ancientGraphs;
drop table IF EXISTS _ancientLinksId;
drop table IF EXISTS _ancientGraphTables;
`;
check = `
select * from _ancientViewGraph1;
`;
client.connect()
async.series([
(next) => client.query(drop_all, next),
(next) => client.query(create_tables, next),
(next) => client.query(create_view_construcor, next),
(next) => client.query(create_graphtables_trigers, next),
(next) => client.query(create_graphpart_triger, next),
(next) => client.query(insert_data, next),
(next) => client.query(check, next),
(next) => client.query(drop_all, next),
], (error, results) => {
console.error(error);
console.log(results[6]);
});