learn-request-queue/db/95-procedures.sql

39 lines
1.1 KiB
MySQL
Raw Normal View History

2020-08-08 04:32:06 +00:00
CREATE OR REPLACE PROCEDURE update_scores()
LANGUAGE SQL
AS $$
UPDATE scores SET score = votescore + scoreModifier
FROM vote_score_vw
WHERE scores.url = vote_score_vw.url;
2020-08-08 04:32:06 +00:00
$$;
CREATE OR REPLACE PROCEDURE add_request(url varchar,requester int)
LANGUAGE SQL
AS $$
INSERT INTO requests (url,requester) VALUES (url,requester);
INSERT INTO requestMetadata (url) VALUES (url);
2020-08-08 04:32:06 +00:00
INSERT INTO scores (url) VALUES (url);
INSERT INTO votes (requesturl,userid) VALUES (url,requester);
CALL update_scores();
$$;
CREATE OR REPLACE PROCEDURE add_vote(url varchar,voteuser int)
LANGUAGE SQL
AS $$
INSERT INTO votes (requesturl,userid) VALUES (url,voteuser);
CALL update_scores();
$$;
CREATE OR REPLACE PROCEDURE delete_vote(url varchar,voteuser int)
LANGUAGE SQL
AS $$
DELETE FROM votes WHERE requesturl = url AND userid = voteuser;
CALL update_scores();
$$;
CREATE OR REPLACE PROCEDURE update_request_score_modifier(updateurl varchar, scoreDiff int)
LANGUAGE SQL
AS $$
UPDATE scores SET scoreModifier = scoreModifier + scoreDiff WHERE url = updateurl;
CALL update_scores();
$$;