-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.sql
1384 lines (1256 loc) · 44.7 KB
/
schema.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
CREATE table "user" (
mid bigint,
name text not null,
sex char(1) not null,
birthday date,
level smallint not null,
coins int not null,
sign text,
identity char(1) not null,
password text not null,
qq text,
wechat text,
is_deleted boolean not null default false
);
create table video (
bv varchar(20),
title text not null,
owner bigint not null,
commit_time timestamp not null,
public_time timestamp,
duration numeric not null,
description text,
reviewer bigint,
review_time timestamp,
is_deleted boolean not null default false
);
create table danmu (
id bigserial,
bv varchar(20) not null,
mid bigint not null,
time numeric not null,
content text not null,
post_time timestamp not null,
is_deleted boolean not null default false
);
create table user_follow (
follower bigint not null,
followed bigint not null,
is_deleted boolean not null default false
);
create table user_like_video (
mid bigint not null,
bv varchar(20) not null,
is_deleted boolean not null default false
);
create table user_collect_video (
mid bigint not null,
bv varchar(20) not null,
is_deleted boolean not null default false
);
create table user_watch_video (
mid bigint not null,
bv varchar(20) not null,
view_time numeric not null,
is_deleted boolean not null default false
);
create table user_coin_video (
mid bigint not null,
bv varchar(20) not null,
is_deleted boolean not null default false
);
create table user_like_danmu (
mid bigint not null,
id bigint not null,
is_deleted boolean not null default false
);
alter table "user" add constraint pk_user primary key (mid);
alter table video add constraint pk_video primary key (bv);
alter table danmu add constraint pk_danmu primary key (id);
alter table user_follow add constraint pk_user_follow primary key (follower, followed);
alter table user_like_video add constraint pk_user_like_video primary key (mid, bv);
alter table user_collect_video add constraint pk_user_collect_video primary key (mid, bv);
alter table user_watch_video add constraint pk_user_watch_video primary key (mid, bv);
alter table user_coin_video add constraint pk_user_coin_video primary key (mid, bv);
alter table user_like_danmu add constraint pk_user_like_danmu primary key (mid, id);
create index idx_user_like_video_mid_bv on user_like_video(mid, bv) where is_deleted = false;
create index idx_user_like_video_bv on user_like_video(bv) where is_deleted = false;
create index idx_danmu_id on danmu(id) where is_deleted = false;
create index idx_danmu_bv_time on danmu(bv, time) where is_deleted = false;
create index idx_danmu_mid on danmu(mid) where is_deleted = false;
create index idx_user_like_danmu_mid_id on user_like_danmu(mid, id) where is_deleted = false;
create index idx_user_like_danmu_id on user_like_danmu(id) where is_deleted = false;
create index idx_video_bv on video(bv) where is_deleted = false;
create unique index uk_video_owner_title on video(owner, title) where is_deleted = false;
create index idx_user_mid on "user"(mid) where is_deleted = false;
create unique index uk_user_qq on "user"(qq) where is_deleted = false;
create unique index uk_user_wechat on "user"(wechat) where is_deleted = false;
create index idx_user_coin_video_mid_bv_idx on user_coin_video(mid, bv) where is_deleted = false;
create index idx_user_coin_video_bv_idx on user_coin_video(bv) where is_deleted = false;
create index idx_user_collect_video_mid_bv on user_collect_video(mid, bv) where is_deleted = false;
create index idx_user_collect_video_bv on user_collect_video(bv) where is_deleted = false;
create index idx_user_follow_follower_followed on user_follow(follower, followed) where is_deleted = false;
create index idx_user_follow_followed on user_follow(followed) where is_deleted = false;
create index idx_user_watch_video_mid_bv on user_watch_video(mid, bv) where is_deleted = false;
create index idx_user_watch_video_bv_idx on user_watch_video(bv) where is_deleted = false;
create view videos_visible as select * from video where is_deleted = false;
create view danmus_visible as select * from danmu where is_deleted = false;
create view users_visible as select * from "user" where is_deleted = false;
create view user_follow_visible as select * from user_follow where is_deleted = false;
create view user_like_video_visible as select * from user_like_video where is_deleted = false;
create view user_collect_video_visible as select * from user_collect_video where is_deleted = false;
create view user_watch_video_visible as select * from user_watch_video where is_deleted = false;
create view user_coin_video_visible as select * from user_coin_video where is_deleted = false;
create view user_like_danmu_visible as select * from user_like_danmu where is_deleted = false;
-- user service
CREATE OR REPLACE FUNCTION validateAuthInfo(authMid BIGINT, authPassword TEXT, authQq TEXT, authWechat TEXT)
RETURNS BIGINT
AS $$
DECLARE
hasMid BOOLEAN;
hasPassword BOOLEAN;
hasQQ BOOLEAN;
hasWechat BOOLEAN;
qqMid BIGINT;
wechatMid BIGINT;
passwordText TEXT;
qqText TEXT;
wechatText TEXT;
BEGIN
-- check if the auth info provides mid, password, qq, wechat
IF authMid <= 0 THEN
hasMid := FALSE;
ELSE
hasMid := TRUE;
END IF;
IF authPassword IS NULL OR length(authPassword) = 0 THEN
hasPassword := FALSE;
ELSE
hasPassword := TRUE;
END IF;
IF authQq IS NULL OR length(authQq) = 0 THEN
hasQQ := FALSE;
ELSE
hasQQ := TRUE;
END IF;
IF authWechat IS NULL OR length(authWechat) = 0 THEN
hasWechat := FALSE;
ELSE
hasWechat := TRUE;
END IF;
-- if the user provides mid, try to find the user's password, qq, wechat
IF hasMid THEN
SELECT
u.password, u.qq, u.wechat
INTO
passwordText, qqText, wechatText
FROM
users_visible u
WHERE
u.mid = authMid;
IF passwordText IS NULL THEN
RETURN -1; -- cannot find a user with the given mid
END IF;
-- validate by mid and password
IF hasPassword THEN
IF passwordText = authPassword THEN
RETURN authMid;
ELSE
RETURN -1; -- password not match
END IF;
END IF;
-- validate by mid and (qq or wechat)
IF hasQQ OR hasWechat THEN
IF hasQQ AND hasWechat THEN
IF qqText IS NOT NULL AND wechatText IS NOT NULL AND qqText = authQq AND wechatText = authWechat THEN
RETURN authMid;
ELSE
RETURN -1; -- qq or wechat not match
END IF;
ELSEIF hasQQ THEN
IF qqText IS NOT NULL AND qqText = authQq THEN
RETURN authMid;
ELSE
RETURN -1; -- qq not match
END IF;
ELSE
IF wechatText IS NOT NULL AND wechatText = authWechat THEN
RETURN authMid;
ELSE
RETURN -1; -- wechat not match
END IF;
END IF;
END IF;
RETURN -1; -- if the user has mid, but the password, qq, wechat are all not provided, it's invalid
END IF;
-- validate by qq or wechat without mid (if mid is not provided, can't validate by password)
IF hasQQ OR hasWechat THEN
-- if the user has qq or wechat, try to find the user by qq or wechat
IF hasQQ THEN
SELECT
u.mid
INTO
qqMid
FROM
users_visible u
WHERE
u.qq = authQq;
END IF;
IF hasWechat THEN
SELECT
u.mid
INTO
wechatMid
FROM
users_visible u
WHERE
u.wechat = authWechat;
END IF;
IF hasQQ AND hasWechat THEN
IF qqMid IS NULL OR wechatMid IS NULL THEN
RETURN -1; -- cannot find a user with the given qq or wechat
ELSEIF qqMid = wechatMid THEN
RETURN qqMid;
ELSE
RETURN -1; -- qq and wechat not match
END IF;
ELSEIF hasQQ THEN
IF qqMid IS NULL THEN
RETURN -1; -- cannot find a user with the given qq
ELSE
RETURN qqMid;
END IF;
ELSE
IF wechatMid IS NULL THEN
RETURN -1; -- cannot find a user with the given wechat
ELSE
RETURN wechatMid;
END IF;
END IF;
END IF;
-- if auth info don't have qq and wechat and mid, it's invalid
RETURN -1;
END
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION getUserInfo(authMid BIGINT)
RETURNS table(
coins INT,
followings BIGINT[],
followers BIGINT[],
watchedVideos VARCHAR(20)[],
likedVideos VARCHAR(20)[],
collectedVideos VARCHAR(20)[],
postedVideos VARCHAR(20)[]
) AS $$
BEGIN
IF NOT EXISTS(SELECT 1 FROM users_visible u WHERE u.mid = authMid) THEN
RETURN;
END IF;
RETURN QUERY
SELECT
(
SELECT
u.coins
FROM
users_visible u
WHERE
u.mid = authMid
),
array(
SELECT
uf.followed
FROM
user_follow_visible uf
WHERE
uf.follower = authMid
),
array(
SELECT
uf.follower
FROM
user_follow_visible uf
WHERE
uf.followed = authMid
),
array(
SELECT
uw.bv
FROM
user_watch_video_visible uw
WHERE
uw.mid = authMid
),
array(
SELECT
ul.bv
FROM
user_like_video_visible ul
WHERE
ul.mid = authMid
),
array(
SELECT
uc.bv
FROM
user_collect_video_visible uc
WHERE
uc.mid = authMid
),
array(
SELECT
v.bv
FROM
videos_visible v
WHERE
v.owner = authMid
);
END
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION follow(authMid BIGINT, authPassword TEXT, authQq TEXT, authWechat TEXT, followeeMid BIGINT)
RETURNS BOOLEAN
AS $$
DECLARE
authMidValid BIGINT;
currentStatus BOOLEAN;
BEGIN
authMidValid := validateAuthInfo(authMid, authPassword, authQq, authWechat);
IF authMidValid <= 0 THEN
RETURN FALSE;
END IF;
IF authMidValid = followeeMid THEN
RETURN FALSE;
END IF;
IF NOT EXISTS(SELECT 1 FROM users_visible u WHERE u.mid = followeeMid) THEN
RETURN FALSE;
END IF;
SELECT is_deleted INTO currentStatus FROM user_follow WHERE follower = authMidValid AND followed = followeeMid;
IF currentStatus IS NULL THEN
INSERT INTO user_follow(follower, followed) VALUES(authMidValid, followeeMid);
RETURN TRUE;
ELSE
UPDATE user_follow SET is_deleted = NOT currentStatus WHERE follower = authMidValid AND followed = followeeMid;
RETURN currentStatus;
END IF;
END
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION deleteAccount(authMid BIGINT, authPassword TEXT, authQq TEXT, authWechat TEXT, deleteMid BIGINT)
RETURNS BOOLEAN
AS $$
DECLARE
authMidValid BIGINT;
authIdentity CHAR(1);
deleteIdentity CHAR(1);
postedVideoBV VARCHAR(20)[];
BEGIN
authMidValid := validateAuthInfo(authMid, authPassword, authQq, authWechat);
IF authMidValid <= 0 THEN
RETURN FALSE;
END IF;
SELECT u.identity INTO authIdentity FROM users_visible u WHERE u.mid = authMidValid;
IF authIdentity IS NULL THEN
RETURN FALSE;
END IF;
IF authIdentity = 'U' THEN
IF authMidValid <> deleteMid THEN
RETURN FALSE;
END IF;
postedVideoBV := array(SELECT v.bv FROM videos_visible v WHERE v.owner = deleteMid);
UPDATE user_follow_visible SET is_deleted = true WHERE follower = deleteMid OR followed = deleteMid;
UPDATE user_watch_video_visible SET is_deleted = true WHERE mid = deleteMid;
UPDATE user_like_video_visible SET is_deleted = true WHERE mid = deleteMid;
UPDATE user_collect_video_visible SET is_deleted = true WHERE mid = deleteMid;
UPDATE user_coin_video_visible SET is_deleted = true WHERE mid = deleteMid;
IF postedVideoBV IS NOT NULL THEN
UPDATE user_watch_video_visible SET is_deleted = true WHERE bv = ANY(postedVideoBV);
UPDATE user_like_video_visible SET is_deleted = true WHERE bv = ANY(postedVideoBV);
UPDATE user_collect_video_visible SET is_deleted = true WHERE bv = ANY(postedVideoBV);
UPDATE user_coin_video_visible SET is_deleted = true WHERE bv = ANY(postedVideoBV);
UPDATE user_like_danmu_visible SET is_deleted = true WHERE id in (SELECT id FROM danmus_visible WHERE bv = ANY(postedVideoBV));
UPDATE danmus_visible SET is_deleted = true WHERE bv = ANY(postedVideoBV);
END IF;
UPDATE user_like_danmu_visible SET is_deleted = true WHERE id in (SELECT id FROM danmus_visible WHERE mid = deleteMid);
UPDATE user_like_danmu_visible SET is_deleted = true WHERE mid = deleteMid;
UPDATE danmus_visible SET is_deleted = true WHERE mid = deleteMid;
UPDATE videos_visible SET is_deleted = true WHERE owner = deleteMid;
UPDATE users_visible SET is_deleted = true WHERE mid = deleteMid;
RETURN TRUE;
END IF;
SELECT u.identity INTO deleteIdentity FROM users_visible u WHERE u.mid = deleteMid;
IF deleteIdentity = 'S' AND deleteMid <> authMidValid THEN
RETURN FALSE;
END IF;
postedVideoBV := array(SELECT v.bv FROM videos_visible v WHERE v.owner = deleteMid);
UPDATE user_follow_visible SET is_deleted = true WHERE follower = deleteMid OR followed = deleteMid;
UPDATE user_watch_video_visible SET is_deleted = true WHERE mid = deleteMid;
UPDATE user_like_video_visible SET is_deleted = true WHERE mid = deleteMid;
UPDATE user_collect_video_visible SET is_deleted = true WHERE mid = deleteMid;
UPDATE user_coin_video_visible SET is_deleted = true WHERE mid = deleteMid;
IF postedVideoBV IS NOT NULL THEN
UPDATE user_watch_video_visible SET is_deleted = true WHERE bv = ANY(postedVideoBV);
UPDATE user_like_video_visible SET is_deleted = true WHERE bv = ANY(postedVideoBV);
UPDATE user_collect_video_visible SET is_deleted = true WHERE bv = ANY(postedVideoBV);
UPDATE user_coin_video_visible SET is_deleted = true WHERE bv = ANY(postedVideoBV);
UPDATE user_like_danmu_visible SET is_deleted = true WHERE id in (SELECT id FROM danmus_visible WHERE bv = ANY(postedVideoBV));
UPDATE danmus_visible SET is_deleted = true WHERE bv = ANY(postedVideoBV);
END IF;
UPDATE user_like_danmu_visible SET is_deleted = true WHERE id in (SELECT id FROM danmus_visible WHERE mid = deleteMid);
UPDATE user_like_danmu_visible SET is_deleted = true WHERE mid = deleteMid;
UPDATE danmus_visible SET is_deleted = true WHERE mid = deleteMid;
UPDATE videos_visible SET is_deleted = true WHERE owner = deleteMid;
UPDATE users_visible SET is_deleted = true WHERE mid = deleteMid;
UPDATE videos_visible SET reviewer = null WHERE reviewer = deleteMid;
RETURN TRUE;
END
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION register(in_password text, in_qq text, in_wechat text, in_name text, in_sex char(1), in_birthday text, in_sign text)
RETURNS BIGINT
AS $$
DECLARE
currentMaxMid BIGINT;
BEGIN
SELECT max(mid) INTO currentMaxMid FROM "user";
INSERT INTO "user" (mid, name, sex, birthday, level, coins, sign, identity, password, qq, wechat)
VALUES (currentMaxMid + 1, in_name, in_sex, to_date(in_birthday, 'MM月DD日'), 1, 0, in_sign, 'U', in_password, CASE WHEN in_qq = '' THEN null ELSE in_qq END, CASE WHEN in_wechat = '' THEN null ELSE in_wechat END);
RETURN currentMaxMid + 1;
END
$$ LANGUAGE plpgsql;
-- danmu service
CREATE OR REPLACE FUNCTION likeDanmu(authMid BIGINT, authPassword TEXT, authQq TEXT, authWechat TEXT, danmuId BIGINT)
RETURNS BOOLEAN
AS $$
DECLARE
authMidValid BIGINT;
currentStatus BOOLEAN;
BEGIN
authMidValid := validateAuthInfo(authMid, authPassword, authQq, authWechat);
IF authMidValid <= 0 THEN
RETURN FALSE;
END IF;
IF NOT EXISTS(SELECT 1 FROM user_watch_video_visible uw WHERE uw.mid = authMidValid AND uw.bv = (SELECT bv FROM danmus_visible d WHERE d.id = danmuId)) THEN
RETURN FALSE;
END IF;
SELECT is_deleted INTO currentStatus FROM user_like_danmu WHERE mid = authMidValid AND id = danmuId;
IF currentStatus IS NULL THEN
INSERT INTO user_like_danmu(mid, id) VALUES (authMidValid, danmuId);
RETURN TRUE;
ELSE
UPDATE user_like_danmu SET is_deleted = NOT currentStatus WHERE mid = authMidValid AND id = danmuId;
RETURN currentStatus;
END IF;
END
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION displayDanmu(videoBV VARCHAR(20), startTime REAL, endTime REAL, filter BOOLEAN)
RETURNS BIGINT[]
AS $$
DECLARE
duration NUMERIC;
publicTime TIMESTAMP;
reviewTime TIMESTAMP;
BEGIN
SELECT v.duration, v.public_time, v.review_time INTO duration, publicTime, reviewTime FROM videos_visible v WHERE v.bv = videoBV;
IF duration IS NULL OR endTime > duration THEN
RETURN null;
END IF;
IF reviewTime IS NULL THEN
RETURN null;
end if;
IF publicTime > now() THEN
RETURN null;
END IF;
IF FILTER = true THEN
RETURN array(
SELECT
id
FROM (
SELECT
DISTINCT ON (content) id, time
FROM
danmus_visible
WHERE
bv = videoBV AND time >= startTime AND time <= endTime
ORDER BY
content, post_time
) AS t
ORDER BY
time
);
ELSE
RETURN array(
SELECT
id
FROM
danmus_visible
WHERE
bv = videoBV AND time >= startTime AND time <= endTime
ORDER BY
time
);
END IF;
END
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION sendDanmu(authMid BIGINT, authPassword TEXT, authQq TEXT, authWechat TEXT, BV_in VARCHAR(20), content_in TEXT, time_in REAL)
RETURNS BIGINT
AS $$
DECLARE
authMidValid BIGINT;
duration NUMERIC;
reviewTime TIMESTAMP;
publicTime TIMESTAMP;
danmuId BIGINT;
BEGIN
SELECT v.duration, v.public_time, v.review_time INTO duration, publicTime, reviewTime FROM videos_visible v WHERE v.bv = BV_in;
IF duration IS NULL OR time_in > duration THEN
RETURN -1;
END IF;
IF reviewTime IS NULL THEN
RETURN -1;
end if;
IF publicTime > now() THEN
RETURN -1;
END IF;
authMidValid := validateAuthInfo(authMid, authPassword, authQq, authWechat);
IF authMidValid <= 0 THEN
RETURN -1;
END IF;
IF NOT EXISTS(SELECT 1 FROM user_watch_video_visible uw WHERE uw.mid = authMidValid AND uw.bv = BV_in) THEN
RETURN -1;
END IF;
INSERT INTO danmu(bv, mid, time, content, post_time) VALUES(BV_in, authMidValid, time_in, content_in, now()) RETURNING id INTO danmuId;
RETURN danmuId;
END
$$ LANGUAGE plpgsql;
-- video service
CREATE OR REPLACE FUNCTION deleteVideo(authMid BIGINT, authPassword TEXT, authQq TEXT, authWechat TEXT, videoBV VARCHAR(20))
RETURNS BOOLEAN
AS $$
DECLARE
authMidValid BIGINT;
ownerMid BIGINT;
authIdentity CHAR(1);
BEGIN
authMidValid := validateAuthInfo(authMid, authPassword, authQq, authWechat);
IF authMidValid <= 0 THEN
RETURN FALSE;
END IF;
SELECT u.identity INTO authIdentity FROM users_visible u WHERE u.mid = authMidValid;
SELECT v.owner INTO ownerMid FROM videos_visible v WHERE v.bv = videoBV;
IF ownerMid IS NULL OR (authIdentity = 'U' AND ownerMid <> authMidValid) THEN
RETURN FALSE;
END IF;
UPDATE
user_like_danmu_visible
SET
is_deleted = true
WHERE
id IN (
SELECT
d.id
FROM
danmus_visible d
WHERE
d.bv = videoBV
);
UPDATE
danmus_visible
SET
is_deleted = true
WHERE
bv = videoBV;
UPDATE
user_watch_video_visible
SET
is_deleted = true
WHERE
bv = videoBV;
UPDATE
user_coin_video_visible
SET
is_deleted = true
WHERE
bv = videoBV;
UPDATE
user_like_video_visible
SET
is_deleted = true
WHERE
bv = videoBV;
UPDATE
user_collect_video_visible
SET
is_deleted = true
WHERE
bv = videoBV;
UPDATE
videos_visible
SET
is_deleted = true
WHERE
bv = videoBV;
RETURN TRUE;
END
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION reviewVideo(authMid BIGINT, authPassword TEXT, authQq TEXT, authWechat TEXT, videoBV VARCHAR(20))
RETURNS BOOLEAN
AS $$
DECLARE
authMidValid BIGINT;
authIdentity CHAR(1);
ownerMid BIGINT;
reviewTime TIMESTAMP;
BEGIN
SELECT v.owner, v.review_time INTO ownerMid, reviewTime FROM videos_visible v WHERE v.bv = videoBV;
IF ownerMid IS NULL OR reviewTime IS NOT NULL THEN
RETURN FALSE;
END IF;
authMidValid := validateAuthInfo(authMid, authPassword, authQq, authWechat);
IF authMidValid <= 0 OR authMidValid = ownerMid THEN
RETURN FALSE;
END IF;
SELECT u.identity INTO authIdentity FROM users_visible u WHERE u.mid = authMidValid;
IF authIdentity = 'U' THEN
RETURN FALSE;
END IF;
UPDATE video SET reviewer = authMidValid, review_time = now() WHERE bv = videoBV;
RETURN TRUE;
END
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION updateVideoInfo(authMid BIGINT, authPassword TEXT, authQq TEXT, authWechat TEXT, videoBV VARCHAR(20), title_in TEXT, duration_in REAL, description_in TEXT, publicTime_in TIMESTAMP)
RETURNS BOOLEAN
AS $$
DECLARE
authMidValid BIGINT;
ownerMid BIGINT;
title TEXT;
duration NUMERIC;
description TEXT;
publicTime TIMESTAMP;
reviewTime TIMESTAMP;
BEGIN
authMidValid := validateAuthInfo(authMid, authPassword, authQq, authWechat);
IF authMidValid <= 0 THEN
RETURN FALSE;
END IF;
SELECT v.owner, v.title, v.duration, v.description, v.public_time, v.review_time INTO ownerMid, title, duration, description, publicTime, reviewTime FROM videos_visible v WHERE v.bv = videoBV;
IF ownerMid IS NULL OR ownerMid <> authMidValid THEN
RETURN FALSE;
END IF;
IF duration <> duration_in THEN
RETURN FALSE;
END IF;
IF title = title_in AND ((description IS NULL AND description_in IS NULL) OR description = description_in) AND publicTime = publicTime_in THEN
RETURN FALSE;
END IF;
UPDATE video SET title = title_in, description = description_in, public_time = publicTime_in, review_time = null, reviewer = null WHERE bv = videoBV;
IF reviewTime IS NULL THEN
RETURN FALSE;
ELSE
RETURN TRUE;
END IF;
END
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION postVideo(authMid BIGINT, authPassword TEXT, authQq TEXT, authWechat TEXT, title_in TEXT, duration_in REAL, description_in TEXT, publicTime_in TIMESTAMP)
RETURNS VARCHAR(20)
AS $$
DECLARE
authMidValid BIGINT;
randomNum INT;
newBV VARCHAR(20);
BEGIN
authMidValid := validateAuthInfo(authMid, authPassword, authQq, authWechat);
IF authMidValid <= 0 THEN
RETURN null;
END IF;
WHILE true LOOP
randomNum := floor(random() * 1000000000);
newBV := 'BV' || substr(md5(randomNum::text), 1, 18);
IF NOT EXISTS(SELECT 1 FROM videos_visible v WHERE v.bv = newBV) THEN
EXIT;
END IF;
END LOOP;
INSERT INTO video (bv, title, owner, commit_time, public_time, duration, description, reviewer, review_time)
VALUES(newBV, title_in, authMidValid, now(), publicTime_in, duration_in, description_in, null, null);
RETURN newBV;
END
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION getAverageViewRate(in_bv varchar(20))
RETURNS NUMERIC
AS $$
DECLARE
vDuration NUMERIC;
vWatchCount BIGINT;
vWatchTimeCount NUMERIC;
BEGIN
SELECT duration INTO vDuration FROM videos_visible WHERE bv = in_bv;
IF vDuration IS NULL THEN
RETURN -1;
END IF;
SELECT COUNT(mid), SUM(view_time) INTO vWatchCount, vWatchTimeCount FROM user_watch_video_visible WHERE bv = in_bv;
IF vWatchCount = 0 THEN
RETURN -1;
END IF;
RETURN vWatchTimeCount / vDuration / vWatchCount;
END
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION getHotSpot(bv_in VARCHAR(20))
RETURNS INT[]
AS $$
BEGIN
IF NOT EXISTS(SELECT 1 FROM videos_visible WHERE bv = bv_in) THEN
RETURN null;
END IF;
RETURN array(
SELECT cast(new_time as INTEGER) AS hot_spots
FROM (SELECT floor(time / 10) AS new_time,
COUNT(id) AS cnt,
MAX(COUNT(id)) OVER () AS max_cnt
FROM danmus_visible
WHERE bv = bv_in
GROUP BY new_time
ORDER BY COUNT(id) desc) AS t
WHERE cnt = max_cnt
);
END
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION coinVideo(authMid BIGINT, authPassword TEXT, authQq TEXT, authWechat TEXT, videoBV VARCHAR(20))
RETURNS BOOLEAN
AS $$
DECLARE
authMidValid BIGINT;
authIdentity CHAR(1);
ownerMid BIGINT;
publicTime TIMESTAMP;
reviewTime TIMESTAMP;
authCoins INT;
BEGIN
SELECT v.owner, v.public_time, v.review_time INTO ownerMid, publicTime, reviewTime FROM videos_visible v WHERE v.bv = videoBV;
IF ownerMid IS NULL THEN
RETURN FALSE;
END IF;
authMidValid := validateAuthInfo(authMid, authPassword, authQq, authWechat);
IF authMidValid <= 0 OR ownerMid = authMidValid THEN
RETURN FALSE;
END IF;
SELECT u.identity, u.coins INTO authIdentity, authCoins FROM users_visible u WHERE u.mid = authMidValid;
IF authCoins = 0 THEN
RETURN FALSE;
END IF;
IF authIdentity = 'U' AND (reviewTime IS NULL OR publicTime > now()) THEN
RETURN FALSE;
END IF;
IF EXISTS(SELECT 1 FROM user_coin_video uc WHERE uc.mid = authMidValid AND uc.bv = videoBV) THEN
RETURN FALSE;
ELSE
UPDATE "user" SET coins = coins - 1 WHERE mid = authMidValid;
INSERT INTO user_coin_video(mid, bv) VALUES (authMidValid, videoBV);
RETURN TRUE;
END IF;
END
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION likeVideo(authMid BIGINT, authPassword TEXT, authQq TEXT, authWechat TEXT, videoBV VARCHAR(20))
RETURNS BOOLEAN
AS $$
DECLARE
authMidValid BIGINT;
authIdentity CHAR(1);
ownerMid BIGINT;
publicTime TIMESTAMP;
reviewTime TIMESTAMP;
currentStatus BOOLEAN;
BEGIN
SELECT v.owner, v.public_time, v.review_time INTO ownerMid, publicTime, reviewTime FROM videos_visible v WHERE v.bv = videoBV;
IF ownerMid IS NULL THEN
RETURN FALSE;
END IF;
authMidValid := validateAuthInfo(authMid, authPassword, authQq, authWechat);
IF authMidValid <= 0 OR ownerMid = authMidValid THEN
RETURN FALSE;
END IF;
SELECT u.identity INTO authIdentity FROM users_visible u WHERE u.mid = authMidValid;
IF authIdentity = 'U' AND (reviewTime IS NULL OR publicTime > now()) THEN
RETURN FALSE;
END IF;
SELECT is_deleted INTO currentStatus FROM user_like_video WHERE mid = authMidValid AND bv = videoBV;
IF currentStatus IS NULL THEN
INSERT INTO user_like_video(mid, bv) VALUES(authMidValid, videoBV);
RETURN TRUE;
ELSE
UPDATE user_like_video SET is_deleted = NOT currentStatus WHERE mid = authMidValid AND bv = videoBV;
RETURN currentStatus;
END IF;
END
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION collectVideo(authMid BIGINT, authPassword TEXT, authQq TEXT, authWechat TEXT, videoBV VARCHAR(20))
RETURNS BOOLEAN
AS $$
DECLARE
authMidValid BIGINT;
authIdentity CHAR(1);
ownerMid BIGINT;
publicTime TIMESTAMP;
reviewTime TIMESTAMP;
currentStatus BOOLEAN;
BEGIN
SELECT v.owner, v.public_time, v.review_time INTO ownerMid, publicTime, reviewTime FROM videos_visible v WHERE v.bv = videoBV;
IF ownerMid IS NULL THEN
RETURN FALSE;
END IF;
authMidValid := validateAuthInfo(authMid, authPassword, authQq, authWechat);
IF authMidValid <= 0 OR ownerMid = authMidValid THEN
RETURN FALSE;
END IF;
SELECT u.identity INTO authIdentity FROM users_visible u WHERE u.mid = authMidValid;
IF authIdentity = 'U' AND (reviewTime IS NULL OR publicTime > now()) THEN
RETURN FALSE;
END IF;
SELECT is_deleted INTO currentStatus FROM user_collect_video WHERE mid = authMidValid AND bv = videoBV;
IF currentStatus IS NULL THEN
INSERT INTO user_collect_video(mid, bv) VALUES(authMidValid, videoBV);
RETURN TRUE;
ELSE
UPDATE user_collect_video SET is_deleted = NOT currentStatus WHERE mid = authMidValid AND bv = videoBV;
RETURN currentStatus;
END IF;
END
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION searchVideo(authMid BIGINT, authPassword TEXT, authQq TEXT, authWechat TEXT, keywords TEXT[], pageSize INT, pageNum INT)
RETURNS VARCHAR(20)[]
AS $$
DECLARE
authMidValid BIGINT;
authIdentity CHAR(1);
dynamicSQL TEXT := '';
result VARCHAR(20)[];
BEGIN
authMidValid := validateAuthInfo(authMid, authPassword, authQq, authWechat);
IF authMidValid <= 0 THEN
RETURN null;
END IF;
SELECT u.identity INTO authIdentity FROM users_visible u WHERE u.mid = authMidValid;
FOR i IN 1..array_length(keywords, 1) LOOP
-- dynamicSQL := dynamicSQL || 'coalesce(regexp_count(v.title || '' '' || coalesce(v.description, '''') || '' '' || u.name, $1[' || i || '], 1, ''i''), 0) + ';
dynamicSQL := dynamicSQL || 'regexp_count(v.title, $1[' || i || '], 1, ''i'') + regexp_count(coalesce(v.description, ''''), $1[' || i || '], 1, ''i'') + regexp_count(u.name, $1[' || i || '], 1, ''i'') + ';
END LOOP;
dynamicSQL := substr(dynamicSQL, 1, length(dynamicSQL) - 2);
IF authIdentity = 'U' THEN
dynamicSQL := format(
'SELECT array_agg(bv) from
(WITH user_visible_video AS (
SELECT
v.bv, v.title, v.description, v.owner
FROM
videos_visible v
WHERE
v.review_time is not null and v.public_time < now() or v.owner = %s
)
SELECT
match_count.bv
FROM
(
SELECT
v.bv, ' || dynamicSQL || '
as full_match_count
FROM
user_visible_video v join users_visible u on v.owner = u.mid
) AS match_count
JOIN
(
SELECT
count(mid) as watch_count, bv
FROM user_watch_video_visible
GROUP BY bv
) AS w
ON
match_count.bv = w.bv
WHERE
full_match_count > 0
ORDER BY
full_match_count DESC, watch_count DESC
LIMIT
%s
OFFSET
%s) as t', authMidValid, pageSize, (pageNum - 1) * pageSize);
ELSE
dynamicSQL := format(
'SELECT array_agg(bv) from
(SELECT
match_count.bv
FROM
(
SELECT
v.bv, ' || dynamicSQL || '
as full_match_count
FROM
videos_visible v join users_visible u on v.owner = u.mid
) AS match_count
JOIN
(
SELECT
count(mid) as watch_count, bv
FROM user_watch_video_visible
GROUP BY bv
) AS w
ON
match_count.bv = w.bv
WHERE