-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactivities.sql
159 lines (136 loc) · 3.52 KB
/
activities.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
-- Renvoie 1 si le dernier post de l'utilisateur actuel date
-- d'au moins deux secondes, 0 sinon.
create or replace function is_post_cooldown_up return boolean as
max_date date := current_date;
delay number := 0.0;
post_count number := 0;
begin
-- S'il n'y a aucun post, il n'y a pas de cooldown.
select
count(idpost) into post_count
from Post;
if post_count = 0 then
return TRUE;
end if;
select
max(date_post) into max_date
from
Post
where
upper(pseudo) = user;
delay := current_date - max_date;
-- Conversion en secondes
delay := delay * 24 * 3600;
if delay > 2.0 then
return TRUE;
else
return FALSE;
end if;
end;
/
create or replace procedure add_hashtag(hashtag IN varchar, post IN number) as
occurences number := 0;
begin
select
count(content) into occurences
from Hashtag where content = hashtag;
if occurences = 0 then
insert into Hashtag values (hashtag);
end if;
insert into HasHashtag values (post, hashtag);
end;
/
-- Insère tous les mots précédés par un '#' dans la table
-- Hashtag.
create or replace procedure parse_hashtags(post IN number, msg IN varchar) as
parser_state number(1) := 0;
buffer varchar(140) := '';
c varchar(1) := '_';
begin
-- Parsing:
-- - Etat 0: Recherche du caractère '#'
-- - Etat 1: Récupération du hashtag dans buffer
if length(msg) > 1 then
for i in 1..length(msg) loop
c := substr(msg, i, 1);
if parser_state = 0 and c = '#' then
parser_state := 1;
buffer := '';
else
if parser_state = 1 and (c = ' ' or i = length(msg)-1) then
add_hashtag(buffer, post);
buffer := '';
parser_state := 0;
else
buffer := buffer || c;
end if;
end if;
end loop;
end if;
end;
/
create or replace function new_idpost return number AS
RESULT number(6);
begin
select NVL(max(idpost)+1, 0) into RESULT
from Post;
return RESULT;
end;
/
create or replace function new_idsurvey return number AS
RESULT number(6);
begin
select NVL(max(idsurvey)+1, 0) into RESULT
from Survey;
return RESULT;
end;
/
create or replace function new_idoption return number AS
RESULT number(6);
begin
select NVL(max(idoption)+1, 0) into RESULT
from Options;
return RESULT;
end;
/
-- Ajout de la location d'un post
create or replace procedure update_location(room in varchar, buiding in varchar) as
begin
update Post set room = room, building = building
where pseudo = lower(user);
end;
/
-- Seul et unique moyen d'ajouter des posts dans la base de donnée
-- pour l'utilisateur lambda.
create or replace procedure add_post(msg IN varchar, room IN varchar, building IN varchar) as
id varchar(32) := 'none';
begin
if is_post_cooldown_up = FALSE then
raise_application_error(-20589, 'Please wait at least 2 seconds before posting again.');
else
insert into Post values (new_idpost, msg, current_date, room, building, lower(user));
end if;
end;
/
-- Permet à l'utilisateur de modifier l'attribut validate de l'un de ses brouillons (draft)
create or replace procedure validate_draft(id in number, state in boolean) as
begin
update Draft set state = state where iddraft = id;
end;
/
-- Permet à l'utilisateur de signaler un post
create or replace procedure add_signal(TARGET_USER in varchar, ID_POST in number) as
begin
insert into Signal values(TARGET_USER, ID_POST);
end;
/
-- Permet d'ajouter un nouveau vote
create or replace procedure add_vote(ID_POST in number, IS_LIKE in boolean) as
begin
if IS_LIKE = TRUE then
insert into Vote values(lower(user),ID_POST,1);
else
insert into Vote values(lower(user),ID_POST,-1);
end if;
end;
/