-
Notifications
You must be signed in to change notification settings - Fork 0
/
storedprocs.sql
505 lines (384 loc) · 9.64 KB
/
storedprocs.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
--Author Nick Marengo--
--Given a referred person's first and last names, lists some basic info about them--
CREATE OR REPLACE FUNCTION basicinfo(text, text, refcursor)
RETURNS refcursor AS '
DECLARE
Firstname VARCHAR :=$1;
Lastname VARCHAR :=$2;
Resultset refcursor :=$3;
BEGIN
open resultset FOR
SELECT Ref_F_Name, Ref_L_Name, Ref_Street, Ref_City, Ref_State, Ref_Zip, Ref_Cell_Phone, DOB, AGE
FROM Referrals
WHERE Firstname = Ref_F_Name
AND Lastname = Ref_L_Name;
RETURN resultset;
END;
'
LANGUAGE plpgsql
--In order to fetch the info, you need to write a query with the parameter(s) you do so like this:--
--SELECT basicinfo('John', 'Smith', 'results');
--FETCH ALL FROM results;
--Given a referred person's first and last names, lists their referred agency and their contact person
CREATE OR REPLACE FUNCTION agencyinfo(text, text, refcursor)
RETURNS refcursor AS '
DECLARE
Firstname VARCHAR :=$1;
Lastname VARCHAR :=$2;
Resultset refcursor :=$3;
BEGIN
open resultset FOR
SELECT Ref_F_Name, Ref_L_Name, Ref_Date, Referring_Agency
FROM Referrals
WHERE Firstname = Ref_F_Name
AND Lastname = Ref_L_Name;
RETURN resultset;
END;
'
LANGUAGE plpgsql
--info fetch test--
--SELECT agencyinfo('John', 'Smith', 'results');
--FETCH ALL FROM results;
--attempt at an attendance stored proc--
CREATE OR REPLACE FUNCTION attendanceinfo(text, text, refcursor)
RETURNS refcursor AS '
DECLARE
Firstname VARCHAR :=$1;
Lastname VARCHAR :=$2;
Resultset refcursor :=$3;
BEGIN
open resultset FOR
SELECT Ref_F_Name, Ref_L_Name, Curriculum_Name, Classes_Needed, Classes_Total
FROM Referrals r, Participants p, Curriculum c
WHERE Firstname = Ref_F_Name
AND Lastname = Ref_L_Name
AND r.P_Num = p.P_Num
AND p.CID = c.CID;
RETURN resultset;
END;
'
LANGUAGE plpgsql
--test query--
--SELECT attendanceinfo('John', 'Smith', 'results');
--FETCH ALL FROM results;
/*
* Dan Martino
* Returns all of the class ids, class names, and comments of the classes a specific
* participant has attended.
* PARAMETERS: A participant's PID
*/
CREATE OR REPLACE FUNCTION classesAttended(integer, refcursor)
RETURNS refcursor AS
'
DECLARE
PID INTEGER := $1;
Resultset refcursor := $2;
BEGIN
open resultset FOR
SELECT DISTINCT
Class_Subjects.C_Subject,
Class_Subjects.Class_Subject,
Class_Attendence.Participant_Comment
FROM
Referrals,
Participants,
Class_Attendence,
Classes_Scheduled,
Curriculum_Subjects,
Class_Subjects
WHERE
Referrals.P_Num = PID
AND Referrals.P_Num = Participants.P_Num
AND Participants.P_Num = Class_Attendence.P_Num
AND Class_Attendence.Class_ID = Classes_Scheduled.Class_ID
AND Classes_Scheduled.C_Subject = Curriculum_Subjects.C_Subject
AND Curriculum_Subjects.C_Subject = Class_Subjects.C_Subject
ORDER BY Class_Subjects.C_Subject;
RETURN resultset;
END;
'
LANGUAGE plpgsql
/*
* Dan Martino
* Returns all of the class ids and class names of the classes a specific
* participant has not attended
* PARAMETERS: A participant's PID
*/
CREATE OR REPLACE FUNCTION classesNotAttended(integer, refcursor)
RETURNS refcursor AS
'
DECLARE
PID INTEGER := $1;
Resultset refcursor := $2;
BEGIN
open resultset FOR
SELECT DISTINCT
Class_Subjects.C_Subject,
Class_Subjects.Class_Subject
FROM
Referrals,
Participants,
Curriculum,
Curriculum_Subjects,
Class_Subjects
WHERE
Referrals.P_Num = PID
AND Referrals.P_Num = Participants.P_Num
AND Participants.CID = Curriculum.CID
AND Curriculum.CID = Curriculum_Subjects.CID
AND Curriculum_Subjects.C_Subject = Class_Subjects.C_Subject
AND Curriculum_Subjects.C_Subject NOT IN (SELECT DISTINCT
Curriculum_Subjects.C_Subject
FROM
Referrals,
Participants,
Class_Attendence,
Classes_Scheduled,
Curriculum_Subjects,
Class_Subjects
WHERE
Referrals.P_Num = PID
AND Referrals.P_Num = Participants.P_Num
AND Participants.P_Num = Class_Attendence.P_Num
AND Class_Attendence.Class_ID = Classes_Scheduled.Class_ID
AND Classes_Scheduled.C_Subject = Curriculum_Subjects.C_Subject
AND Curriculum_Subjects.C_Subject = Class_Subjects.C_Subject)
ORDER BY Class_Subjects.C_Subject;
RETURN resultset;
END;
'
LANGUAGE plpgsql
/*
* Dan Martino
* Stored Procedure to add a new employee to the database
* PARAMETERS: First name, last name, email, home phone, cell phone, permission level, password
* TODO: What happens if a field is null? Example: An employee does not have a cellphone
*/
CREATE OR REPLACE FUNCTION insertEmployee(text, text, text, text, text, text, text, refcursor)
RETURNS refcursor AS
'
DECLARE
firstName VARCHAR := $1;
lastName VARCHAR := $2;
email VARCHAR := $3;
homephone VARCHAR := $4;
cellphone VARCHAR := $5;
permission VARCHAR := $6;
password VARCHAR := $7;
Resultset refcursor := $8;
BEGIN
open resultset FOR
INSERT INTO Employees(E_F_NAME, E_L_NAME, Email, Home_Phone, Cell_Phone, Permission_Lvl, Password)
VALUES(firstName, lastName, email, homephone, cellphone, permission, password);
RETURN resultset;
END;
'
LANGUAGE plpgsql
/*
* Dan Martino
* Stored Procedure to add a new employee to the database
* PARAMETERS: An employee's EID, first name, last name, email, home phone, cell phone, permission level, password
* TODO: How to only update certain fields
*/
CREATE OR REPLACE FUNCTION updateEmployee(text, text, text, text, text, text, text, refcursor)
RETURNS refcursor AS
'
DECLARE
firstName VARCHAR := $1;
lastName VARCHAR := $2;
email VARCHAR := $3;
homephone VARCHAR := $4;
cellphone VARCHAR := $5;
permission VARCHAR := $6;
password VARCHAR := $7;
Resultset refcursor := $8;
BEGIN
open resultset FOR
UPDATE
Employees
SET
E_F_NAME = firstName
E_L_NAME = lastName
Email = email
Home_Phone = homephone
Cell_Phone = cellphone
Permission_Lvl = permission
Password = password
WHERE
Employees.EID = EID;
RETURN resultset;
END;
'
LANGUAGE plpgsql
/*
* Dan Martino
* Stored Procedure to delete an employee from the database
* PARAMETERS: An employee's EID
*/
CREATE OR REPLACE FUNCTION deleteEmployee(integer, refcursor)
RETURNS refcursor AS
'
DECLARE
EID INTEGER := $1;
Resultset refcursor := $2;
BEGIN
open resultset FOR
DELETE FROM
Employees
WHERE
Employees.EID = EID;
RETURN resultset;
END;
'
LANGUAGE plpgsql
/*
* Dan Martino
* Stored Procedure to add a new class to the database
* PARAMETERS: A class' name
*/
CREATE OR REPLACE FUNCTION addClass(text, refcursor)
RETURNS refcursor AS
'
DECLARE
className VARCHAR := $1;
Resultset refcursor := $2;
BEGIN
open resultset FOR
INSERT INTO Class_Subjects (Class_Subject)
VALUES(className);
RETURN resultset;
END;
'
LANGUAGE plpgsql
/*
* Dan Martino
* Stored Procedure to add a class to a curriculum
* PARAMETERS: A class' id and a curriculum's id
*/
CREATE OR REPLACE FUNCTION addClassToCurriculum(integer, integer, refcursor)
RETURNS refcursor AS
'
DECLARE
classID INTEGER := $1;
curriculumID INTEGER := $2;
Resultset refcursor := $3;
BEGIN
open resultset FOR
INSERT INTO Curriculum_Subjects(C_Subject, CID)
VALUES(classID, curriculumID);
RETURN resultset;
END;
'
LANGUAGE plpgsql
/*
* Dan Martino
* Stored Procedure to schedule a class
* PARAMETERS: A curriculum's id, an employee's id, a location's id, a class' id, and a datetime
*/
CREATE OR REPLACE FUNCTION scheduleClass(integer, integer, integer, integer, datetime, refcursor)
RETURNS refcursor AS
'
DECLARE
curriculumID INTEGER := $1;
employeeID INTEGER := $2;
locationID INTEGER := $3;
classID INTEGER := $4;
datetime DATETIME := $5;
Resultset refcursor := $6;
BEGIN
open resultset FOR
INSERT INTO Classes_Scheduled(CID, EID, Location_ID, C_Subject, Date_Time_Schedules)
VALUES(curriculumID, employeeID, locationID, classID, datetime);
RETURN resultset;
END;
'
LANGUAGE plpgsql
/*
* Dan Martino
* Stored Procedure to add a new location into the database
* PARAMETERS: A location's name
*/
CREATE OR REPLACE FUNCTION addLocation(text, refcursor)
RETURNS refcursor AS
'
DECLARE
locationName VARCHAR := $1;
Resultset refcursor := $2;
BEGIN
open resultset FOR
INSERT INTO Locations(Location_Name)
VALUES(locationName);
RETURN resultset;
END;
'
LANGUAGE plpgsql
/*
* Dan Martino
* Stored Procedure to remove a location from the database
* PARAMETERS: A location's id
*/
CREATE OR REPLACE FUNCTION deleteLocation(integer, refcursor)
RETURNS refcursor AS
'
DECLARE
locationID INTEGER := $1;
Resultset refcursor := $2;
BEGIN
open resultset FOR
DELETE FROM
Locations
WHERE
Locations.Location_ID = locationID
RETURN resultset;
END;
'
LANGUAGE plpgsql
/*
* Dan Martino
* Stored Procedure to take attendence
* PARAMETERS: An employee's id, a class's id, a participant's id, and a comment on performance
*/
CREATE OR REPLACE FUNCTION takeAttendence(integer, integer, integer, text refcursor)
RETURNS refcursor AS
'
DECLARE
employeeID INTEGER := $1;
classID INTEGER := $2;
PID INTEGER := $3;
comment VARCHAR := $4
Resultset refcursor := $5;
BEGIN
open resultset FOR
INSERT INTO Class_Attendence(EID, Class_ID, P_Num, Participant_Comment)
VALUES(employeeID, classID, PID, comment);
RETURN resultset;
END;
'
LANGUAGE plpgsql
/*
* Dan Martino
* Returns all information about all the classes a specific employee is teaching
* PARAMETERS: An employee's EID
*/
CREATE OR REPLACE FUNCTION employeeClasses(integer, refcursor)
RETURNS refcursor AS
'
DECLARE
employeeID INTEGER := $1;
Resultset refcursor := $2;
BEGIN
open resultset FOR
SELECT DISTINCT
Class_Subjects.Class_Subject
FROM
Class_Subjects,
Curriculum_Subjects,
Classes_Scheduled
WHERE
Class_Subjects.C_Subject = Curriculum_Subjects.C_Subject
AND Curriculum_Subjects.C_Subject = Classes_Scheduled.C_Subject
AND Classes_Scheduled.EID = employeeID;
RETURN resultset;
END;
'
LANGUAGE plpgsql