-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlog.sql
240 lines (195 loc) · 5.19 KB
/
log.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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
-- Active: 1675688148512@@127.0.0.1@5432@postgres@web_project
--DROP TABLE user_log;
-- 建日志表
CREATE TABLE
user_log(
log_date TIMESTAMP not NULL,
"userId" character varying(100) NOT NULL,
--old_operation character varying(100),
operation character varying(100)
);
--answer
--add函数
CREATE OR REPLACE FUNCTION web_project.user_log_fun_answer_add() RETURNS
TRIGGER AS $answer$
BEGIN
INSERT INTO
web_project.user_log(
log_date,
"userId",
operation
)
VALUES (NOW(),new."userId",CONCAT('insert id=',new.id,' into answer'));
RETURN NEW;
END;
$answer$ LANGUAGE
PLPGSQL;
--add触发器
CREATE OR REPLACE TRIGGER answer_add_trigger AFTER INSERT ON answer FOR EACH ROW EXECUTE PROCEDURE user_log_fun_answer_add();
-- test
-- INSERT INTO
-- answer(
-- id,
-- "userId",
-- qid,
-- time,
-- content,
-- "like",
-- dislike,
-- like_id
-- )
-- VALUES (12345,'sj_xian',1,'2009-06-04 00:00:00','this test',12,2,NULL);
--del
CREATE OR REPLACE FUNCTION web_project.user_log_fun_answer_del() RETURNS
TRIGGER AS $answer$
BEGIN
INSERT INTO
web_project.user_log(
log_date,
"userId",
operation
)
VALUES (NOW(),old."userId",CONCAT('delete id=',old.id,' from answer'));
RETURN NEW;
END;
$answer$ LANGUAGE
PLPGSQL;
--del
CREATE OR REPLACE TRIGGER answer_del_trigger AFTER DELETE ON answer FOR EACH ROW EXECUTE PROCEDURE user_log_fun_answer_del();
--test
--DELETE FROM answer WHERE answer.id=3;
--update
CREATE OR REPLACE FUNCTION web_project.user_log_fun_answer_update() RETURNS
TRIGGER AS $answer$
BEGIN
INSERT INTO
web_project.user_log(
log_date,
"userId",
operation
)
VALUES (NOW(),new."userId",CONCAT('update id=',new.id,' from answer'));
RETURN NEW;
END;
$answer$ LANGUAGE
PLPGSQL;
CREATE OR REPLACE TRIGGER answer_update_trigger AFTER UPDATE ON answer FOR EACH ROW EXECUTE PROCEDURE user_log_fun_answer_update();
--test
-- UPDATE answer set content='this test after update' WHERE answer.id=12345;
--quiz
--add触发器
CREATE OR REPLACE FUNCTION web_project.user_log_fun_quiz_add() RETURNS
TRIGGER AS $quiz$
BEGIN
INSERT INTO
web_project.user_log(
log_date,
"userId",
operation
)
VALUES (NOW(),new."userId",CONCAT('insert qid=',new.qid,' into quiz'));
RETURN NEW;
END;
$quiz$ LANGUAGE
PLPGSQL;
CREATE OR REPLACE TRIGGER quiz_add_trigger BEFORE INSERT ON quiz FOR EACH ROW EXECUTE PROCEDURE user_log_fun_quiz_add();
--test
-- INSERT INTO
-- quiz(
-- qid,
-- "userId",
-- time,
-- title,
-- content,
-- "keyWords",
-- "like",
-- dislike,
-- max_like_reply_id,
-- ans_num,
-- like_id,
-- star_id
-- )
-- VALUES (12345,'sj_xian',NOW(),'this title','this content','{key,key2}',12,2,0,0,NULL,'{}');
--del
CREATE OR REPLACE FUNCTION web_project.user_log_fun_quiz_del() RETURNS
TRIGGER AS $quiz$
BEGIN
INSERT INTO
web_project.user_log(
log_date,
"userId",
operation
)
VALUES (NOW(),old."userId",CONCAT('delete qid=',old.qid,' from quiz'));
RETURN NEW;
END;
$quiz$ LANGUAGE
PLPGSQL;
--del
CREATE OR REPLACE TRIGGER quiz_del_trigger AFTER DELETE ON quiz FOR EACH ROW EXECUTE PROCEDURE user_log_fun_quiz_del();
--test
-- DELETE FROM quiz WHERE quiz.qid=12345;
-- user
--add函数
CREATE OR REPLACE FUNCTION web_project.user_log_fun_user_add() RETURNS
TRIGGER AS $user$
BEGIN
INSERT INTO
web_project.user_log(
log_date,
"userId",
operation
)
VALUES (NOW(),new."userId",CONCAT('insert userId=',new."userId",' into user'));
RETURN NEW;
END;
$user$ LANGUAGE
PLPGSQL;
-- add触发器
CREATE OR REPLACE TRIGGER user_add_trigger AFTER INSERT ON "user" FOR EACH ROW EXECUTE PROCEDURE user_log_fun_user_add();
--test
-- INSERT INTO
-- "user"(
-- "userId",
-- username,
-- password,
-- rights
-- )
-- VALUES ('123','123','123',0);
--del
CREATE OR REPLACE FUNCTION web_project.user_log_fun_user_del() RETURNS
TRIGGER AS $user$
BEGIN
INSERT INTO
web_project.user_log(
log_date,
"userId",
operation
)
VALUES (NOW(),old."userId",CONCAT('delete userId=',old."userId",' from user'));
RETURN NEW;
END;
$user$ LANGUAGE
PLPGSQL;
--del
CREATE OR REPLACE TRIGGER user_del_trigger AFTER DELETE ON "user" FOR EACH ROW EXECUTE PROCEDURE user_log_fun_user_del();
--test
-- DELETE FROM "user" WHERE "user"."userId"='123';
--update
CREATE OR REPLACE FUNCTION web_project.user_log_fun_user_update() RETURNS
TRIGGER AS $user$
BEGIN
INSERT INTO
web_project.user_log(
log_date,
"userId",
operation
)
VALUES (NOW(),new."userId",CONCAT('update userId=',new."userId",' from user'));
RETURN NEW;
END;
$user$ LANGUAGE
PLPGSQL;
CREATE OR REPLACE TRIGGER user_update_trigger AFTER UPDATE ON "user" FOR EACH ROW EXECUTE PROCEDURE user_log_fun_user_update();
--test
-- UPDATE "user" set password='1234' WHERE "user"."userId"='123';