-
Notifications
You must be signed in to change notification settings - Fork 0
/
StoredProcedures.sql
116 lines (101 loc) · 3.3 KB
/
StoredProcedures.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
DELIMITER //
#1
DROP PROCEDURE IF EXISTS addAccount//
CREATE PROCEDURE addAccount(IN usid INT,IN handle VARCHAR(100),IN password VARCHAR(32),IN site VARCHAR(30))
BEGIN
DECLARE fname VARCHAR(100);
DECLARE lname VARCHAR(200);
DECLARE id INT;
SELECT firstName,lastName INTO fname,lname FROM users WHERE users.userId=usid;
INSERT INTO socialMedia(site,username,password) VALUES (site,handle,password);
SELECT accId INTO id FROM socialMedia WHERE socialMedia.username=handle and socialMedia.site=site;
UPDATE socialMedia SET firstName=fname,lastName=lname WHERE accId=id;
INSERT INTO userAcc VALUES (usid,id);
END//
#2
DROP PROCEDURE IF EXISTS deleteAccount//
CREATE PROCEDURE deleteAccount(IN id INT,IN site VARCHAR(30))
BEGIN
DELETE FROM socialMedia WHERE socialMedia.accId=fetchId(id,site);
END//
#3
DROP PROCEDURE IF EXISTS deleteUser//
CREATE PROCEDURE deleteUser(IN id INT)
BEGIN
DROP TABLE IF EXISTS accids;
CREATE TABLE accids(accId INT);
INSERT INTO accids(accId) SELECT accId FROM userAcc WHERE userId=id;
DELETE FROM users WHERE users.userId=id;
DROP TABLE accids;
END//
#4
DROP PROCEDURE IF EXISTS deletePost//
CREATE PROCEDURE deletePost(IN postId INT)
BEGIN
DELETE FROM accPost WHERE accPost.postId=postId;
END//
#5
DROP PROCEDURE IF EXISTS deleteMsg//
CREATE PROCEDURE deleteMsg(IN msgId INT)
BEGIN
DELETE FROM accMsg WHERE accMsg.msgId=msgId;
END//
#6
DROP PROCEDURE IF EXISTS interest//
CREATE PROCEDURE interest(IN id INT,IN inid INT)
BEGIN
DECLARE f INT DEFAULT 0;
DECLARE acid INT;
DECLARE fname VARCHAR(100);
DECLARE lname VARCHAR(200);
DECLARE type VARCHAR(10);
DECLARE myCursor CURSOR FOR
SELECT userAcc.accId,socialMedia.firstName,socialMedia.lastName,socialMedia.type FROM socialMedia,userAcc,users WHERE userAcc.userId=users.userId AND socialMedia.accId=userAcc.accId AND users.userId=inid;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET f=1;
OPEN myCursor;
label:LOOP
FETCH myCursor INTO acid,fname,lname,type;
IF f=1 THEN
LEAVE label;
END IF;
IF type="business" THEN
INSERT INTO interests VALUES (id,fname);
INSERT INTO interests VALUES (id,lname);
END IF;
END LOOP label;
CLOSE myCursor;
END//
#7
DROP PROCEDURE IF EXISTS location//
CREATE PROCEDURE location(IN id INT,IN tag VARCHAR(20),IN city VARCHAR(20),IN state VARCHAR(40),IN country VARCHAR(50))
BEGIN
IF tag="current" THEN
UPDATE location SET tag="lived" WHERE tag="current";
END IF;
INSERT INTO location VALUES(id,tag,city,state,country);
END//
#8
DROP PROCEDURE IF EXISTS folinterest//
CREATE PROCEDURE folinterest(IN id INT)
BEGIN
DECLARE f INT DEFAULT 0;
DECLARE acid INT;
DECLARE typ VARCHAR(10);
DECLARE myCursor CURSOR FOR
SELECT accFol.folId FROM userAcc,users,accFol WHERE userAcc.userId=users.userId AND accFol.accId=userAcc.accId AND users.userId=id;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET f=1;
OPEN myCursor;
label:LOOP
FETCH myCursor INTO acid;
IF f=1 THEN
LEAVE label;
END IF;
SELECT socialMedia.type INTO typ FROM socialMedia WHERE socialMedia.accId=acid;
IF typ="business" THEN
INSERT INTO interests(userId,interests) SELECT id,socialMedia.firstName FROM socialMedia WHERE socialMedia.accId=acid;
INSERT INTO interests(userId,interests) SELECT id,socialMedia.lastName FROM socialMedia WHERE socialMedia.accId=acid;
END IF;
END LOOP label;
CLOSE myCursor;
END//
DELIMITER ;