-
Notifications
You must be signed in to change notification settings - Fork 0
/
project2.sql
725 lines (662 loc) · 25.8 KB
/
project2.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
create sequence "Departments_id_seq"
as integer
minvalue 0;
alter sequence "Departments_id_seq" owner to checker;
create table "Courses"
(
id serial,
"courseId" varchar not null
constraint courses_pk
primary key,
"courseName" varchar not null,
credit integer not null,
"classHour" integer not null,
grading varchar not null,
root_prerequisite integer not null
);
alter table "Courses"
owner to checker;
create table "Departments"
(
id integer default nextval('"Departments_id_seq"'::regclass) not null
constraint departments_pk
primary key,
name varchar not null
);
alter table "Departments"
owner to checker;
alter sequence "Departments_id_seq" owned by "Departments".id;
create table "Majors"
(
id serial
constraint majors_pk
primary key,
name varchar not null,
"departmentId" integer not null
constraint majors_departments_id_fk
references "Departments"
on delete cascade
);
alter table "Majors"
owner to checker;
create table prerequisite_list
(
id serial
constraint prerequisite_list_pk
primary key,
type varchar(15) not null
);
alter table prerequisite_list
owner to checker;
create table "AtomPrerequisites"
(
id serial
constraint atomprerequisites_pk
primary key,
"listId" integer not null
constraint atomprerequisites_prerequisite_list_id_fk
references prerequisite_list
on delete cascade,
"courseId" varchar not null
constraint atomprerequisites_courses__fk
references "Courses"
on delete cascade
);
alter table "AtomPrerequisites"
owner to checker;
create table "AndPrerequisites"
(
id serial
constraint andprerequisites_pk
primary key,
"listId" integer not null
constraint andprerequisites_prerequisite_list_id_fk
references prerequisite_list
on delete cascade,
terms integer[] not null
);
alter table "AndPrerequisites"
owner to checker;
create table "OrPrerequisites"
(
id serial
constraint orprerequisites_pk
primary key,
"listId" integer not null
constraint orprerequisites_fk
references prerequisite_list
on delete cascade,
terms integer[] not null
);
alter table "OrPrerequisites"
owner to checker;
create table "Semesters"
(
id serial
constraint semesters_pk
primary key,
name varchar not null,
begin_date date not null,
end_date date not null
);
alter table "Semesters"
owner to checker;
create table "courseSections"
(
id serial
constraint coursesections_pk
primary key,
"courseId" varchar not null
constraint coursesections_courses_c_fk
references "Courses"
on delete cascade,
"semesterId" integer not null
constraint coursesections_semesters_id_fk
references "Semesters"
on delete cascade,
"sectionName" varchar not null,
"totalCapacity" integer not null,
"leftCapacity" integer not null
);
alter table "courseSections"
owner to checker;
create table "Users"
(
"userId" integer not null
constraint users_pk
primary key,
"firstName" varchar not null,
"lastName" varchar not null
);
alter table "Users"
owner to checker;
create table "courseSectionClasses"
(
id serial
constraint coursesectionclasses_pk
primary key,
"sectionId" integer not null
constraint coursesectionclasses_course__fk
references "courseSections"
on delete cascade,
"instructorId" integer not null
constraint coursesectionclasses_users_userid_fk
references "Users",
"dayOfWeek" varchar not null,
"weekList" integer[] not null,
"classStart" smallint not null,
"classEnd" smallint not null,
location varchar not null
);
alter table "courseSectionClasses"
owner to checker;
create table "Students"
(
id serial,
"userId" integer not null
constraint students_pk
primary key
constraint students_users_userid_fk
references "Users"
on delete cascade,
"majorId" integer not null
constraint students_majors_id_fk
references "Majors"
on delete cascade,
"firstName" varchar not null,
"lastName" varchar not null,
"enrolledDate" date not null
);
alter table "Students"
owner to checker;
create table "Instructors"
(
id serial
constraint instructors_pk
primary key,
"userId" integer not null
constraint instructors_users_userid_fk
references "Users"
on delete cascade,
"firstName" varchar not null,
"lastName" varchar not null
);
alter table "Instructors"
owner to checker;
create table course_select
(
"studentId" integer not null
constraint course_select_students__fk
references "Students"
on delete cascade,
"sectionId" integer not null
constraint course_select_coursesections_id_fk
references "courseSections"
on delete cascade,
"gradeType" varchar,
grade smallint,
constraint course_select_pk
primary key ("studentId", "sectionId")
);
alter table course_select
owner to checker;
create table "majorCourses"
(
id serial
constraint majorcourses_pk
primary key,
"majorId" integer not null
constraint majorcourses_majors_id_fk
references "Majors"
on delete cascade,
"courseId" varchar not null
constraint majorcourses_courses__fk
references "Courses"
on delete cascade,
selection varchar not null
);
alter table "majorCourses"
owner to checker;
create function get_time_bad(sid integer, cid integer) returns character varying[]
language plpgsql
as
$$
declare
arr varchar[];
BEGIN
arr = array_agg(fullname)
from (select *
from (select ca."courseName" || '[' || sn || ']' fullname
from (select val.s s, val.sn sn
from (SELECT cs.id i, cs."semesterId" sm, cs."courseId" s, cs."sectionName" sn, *
from "courseSections" cs
where cs.id = cid) val
join (select "sectionId", ca."semesterId", "studentId"
from course_select cr
join "courseSections" ca on cr."sectionId" = ca.id and cr."studentId" = sid) sub
on sub."sectionId" = val.i and sub."semesterId" = val.sm) a
join "Courses" ca on ca."courseId" = a.s) p
union
(select *
from (select ful fullname
from (select cse."sectionId" l, *
from "courseSectionClasses" cse
join "courseSections" cc on cse."sectionId" = cc.id) csc
join (select last_time.cna || '[' || "sectionName" || ']' ful,
"dayOfWeek",
"weekList",
i,
last_time."classStart",
last_time."classEnd",
grading,
"sectionName",
last_time."semesterId"
from (select C."courseName" cna, *
from (select *
from (select c_s.id i, *
from course_select cs
join "courseSections" c_s
on c_s.id = cs."sectionId" and cs."studentId" = (sid)) sections
join "courseSectionClasses" csc on csc."sectionId" = i) body
join "Courses" C on body."courseId" = C."courseId") last_time
join "Semesters" s on last_time."semesterId" = s.id) t
on ((csc."classEnd" >= t."classStart" and csc."classEnd" <= t."classEnd") or
(csc."classStart" >= t."classEnd" and csc."classStart" <= t."classEnd")) and
csc."semesterId" = t."semesterId" and csc."dayOfWeek" = t."dayOfWeek" and
not check_week_fine(csc."weekList", t."weekList") and csc.l = (cid)) b
group by fullname
order by fullname)) c
group by fullname
order by fullname;
return arr;
end
$$;
alter function get_time_bad(integer, integer) owner to checker;
create function check_place_fine(places character varying[], place character varying) returns boolean
language plpgsql
as
$$
declare
pla varchar;
begin
foreach pla IN ARRAY places
loop
if place like '%' || pla || '%' then return true; end if;
end loop;
return false;
end;
$$;
alter function check_place_fine(character varying[], varchar) owner to checker;
create function check_week_fine(week_a integer[], week_b integer[]) returns boolean
language plpgsql
as
$$
DECLARE
wa integer; wb integer;
BEGIN
foreach wa IN ARRAY week_a
loop
foreach wb IN ARRAY week_b
loop
if wa = wb then return false; end if;
end loop;
end loop;
return true;
END;
$$;
alter function check_week_fine(integer[], integer[]) owner to checker;
create function check_course_full(csc_id integer) returns boolean
language plpgsql
as
$$
BEGIN
return (select "leftCapacity" from "courseSections" where id = (csc_id)) = 0;
END;
$$;
alter function check_course_full(integer) owner to checker;
create function check_prerequisite(pre_id integer, stu integer) returns boolean
language plpgsql
as
$$
DECLARE
pre_type varchar; pres int[]; pre int;
BEGIN
pre_type = (select type from prerequisite_list where prerequisite_list.id = pre_id);
if pre_type = 'Atom' then
return (select count(*)
from (select cs.id
from "AtomPrerequisites" bp
join "courseSections" cs
on bp."listId" = pre_id and bp."courseId" = cs."courseId") course_name
join course_select css
on css."sectionId" = course_name.id and css."studentId" = stu and css.grade >= 60) != 0;
elseif pre_type = 'And' then
pres = (select terms from "AndPrerequisites" where "listId" = pre_id);
foreach pre IN ARRAY pres
loop
if not check_prerequisite(pre, stu) then return false; end if;
end loop;
return true;
elseif pre_type = 'Or' then
pres = (select terms from "OrPrerequisites" where "listId" = pre_id);
foreach pre IN ARRAY pres
loop
if check_prerequisite(pre, stu) then return true; end if;
end loop;
return false;
else
RAISE EXCEPTION '====????====';
end if;
END;
$$;
alter function check_prerequisite(integer, integer) owner to checker;
create function check_prerequisite_by_csc_id(csc_id integer, stu integer) returns boolean
language plpgsql
as
$$
declare
pre_id integer;
begin
pre_id = (select root_prerequisite
from "Courses" c
join "courseSections" cs on c."courseId" = cs."courseId" and cs.id = csc_id);
if pre_id = 0 then return true; end if; return check_prerequisite(pre_id, stu);
end;
$$;
alter function check_prerequisite_by_csc_id(integer, integer) owner to checker;
create function check_course_passed(csc_id integer, stu integer) returns boolean
language plpgsql
as
$$
BEGIN
return
(select count(*)
from (SELECT ncs.id
from "courseSections" ncs
where id = csc_id) val
join course_select c
on c."sectionId" = val.id and c."studentId" = (stu) and coalesce(c.grade, -1) >= 60) != 0;
END;
$$;
alter function check_course_passed(integer, integer) owner to checker;
create function drop_course(sid integer, cid integer) returns boolean
language plpgsql
as
$$
declare
noGrade boolean;
begin
noGrade = (select grade from course_select where "studentId" = sid and "sectionId" = cid) IS NULL;
if noGrade then
delete from course_select where "studentId" = sid and "sectionId" = cid;
update "courseSections" set "leftCapacity" = "leftCapacity" + 1 where id = cid;
end if;
return noGrade;
end;
$$;
alter function drop_course(integer, integer) owner to checker;
create function get_all_course_and_grade(sid integer)
returns TABLE
(
a character varying,
b character varying,
c integer,
d integer,
e character varying,
f character varying,
g integer
)
language plpgsql
as
$$
begin
return query (select c."courseId",
c."courseName",
c.credit,
c."classHour",
c.grading,
coalesce("gradeType", 'null'),
coalesce(grade, -1)
from (select *
from course_select
where "studentId" = sid) c_s
join "courseSections" cS on cS.id = c_s."sectionId"
join "Courses" c on cS."courseId" = c."courseId");
end;
$$;
alter function get_all_course_and_grade(integer) owner to checker;
create function get_student_in_semester(courseid character varying, semester integer)
returns TABLE
(
a integer,
b character varying,
c character varying,
d date,
e integer,
f character varying,
g integer,
h character varying
)
language plpgsql
as
$$
begin
return query (select "studentId",
"firstName",
"lastName",
"enrolledDate",
m.id,
m.name,
d.id,
d.name
from ((select "studentId"
from (select id
from "courseSections"
where "courseId" = courseId
and "semesterId" = semester) cS
join course_select c_s on "sectionId" = cS.id) stus
join "Students" s on stus."studentId" = s."userId") sub
join "Majors" m on m.id = sub."majorId"
join "Departments" D on m."departmentId" = D.id);
end;
$$;
alter function get_student_in_semester(varchar, integer) owner to checker;
create function get_course_table(sid integer, date date)
returns TABLE
(
a character varying,
b character varying,
c integer,
d character varying,
e character varying,
f integer,
g integer,
h character varying,
i character varying,
j integer[],
k integer
)
language plpgsql
as
$$
declare
semester int; begin_d int; week int;
begin
semester = (select id from "Semesters" where begin_date <= date and end_date >= date);
begin_d = (select begin_date from "Semesters" where id = semester);
week = (date - begin_d) % 7 + 1;
return query (
select "courseName",
"sectionName",
"instructorId",
"firstName",
"lastName",
"classStart",
"classEnd",
location,
"dayOfWeek",
"weekList",
week
from (select "courseName",
"sectionName",
"instructorId",
"firstName",
"lastName",
"classStart",
"classEnd",
location,
"dayOfWeek",
"weekList"
from (select "sectionId" from course_select where "studentId" = sid) c_S
join "courseSections" cS on cS.id = c_S."sectionId"
and "semesterId" = semester
join "Courses" C2 on cS."courseId" = C2."courseId"
join "courseSectionClasses" csc on c_S."sectionId" = cS.id
join "Instructors" ins on ins."userId" = csc."instructorId") p);
end;
$$;
alter function get_course_table(integer, date) owner to checker;
create function add_course_with_grade(sid integer, cid integer, gtype character varying, score smallint) returns void
language plpgsql
as
$$
declare
type varchar;
begin
type = (select grading
from (select "courseId" from "courseSections" cS where cS.id = cid) t
join "Courses" c on t."courseId" = c."courseId");
if gType is null
then
insert into course_select values (sid, cid, gType, score);
else
if (gType = type)
then
insert into course_select values (sid, cid, gType, score);
end if;
end if;
end
$$;
alter function add_course_with_grade(integer, integer, varchar, smallint) owner to checker;
create function get_course_and_grade_in_semester(student integer, semester integer)
returns TABLE
(
a character varying,
b character varying,
c integer,
d integer,
e character varying,
f character varying,
g integer
)
language plpgsql
as
$$
begin
return query (select c."courseId",
c."courseName",
c.credit,
c."classHour",
c.grading,
coalesce("gradeType", 'null') gt,
coalesce(grade, -1) g
from (select *
from course_select
where "studentId" = student) c_s
join "courseSections" cS on cS.id = c_s."sectionId" and "semesterId" = semester
join "Courses" c on cS."courseId" = c."courseId");
end;
$$;
alter function get_course_and_grade_in_semester(integer, integer) owner to checker;
create function search_course(student_id integer, semester_id integer, search_cid character varying,
search_name character varying, search_instructor character varying,
search_dayofweek character varying, search_classtime smallint,
searchclasslocations character varying[], searchcoursetype character varying,
ignorefull boolean, ignoreconflict boolean, ignorepassed boolean,
ignoremissingprerequisites boolean, pagesize integer, pageindex integer)
returns TABLE
(
a character varying,
b character varying,
c integer,
d integer,
e character varying,
f integer,
g character varying,
h integer,
i integer,
j character varying[],
k integer,
l integer,
m character varying
)
language plpgsql
as
$$
begin
return query (select cname,
fir.nme,
fir.credit,
fir."classHour",
fir.grading,
csid,
fir."sectionName",
fir."totalCapacity",
fir."leftCapacity",
get_time_bad(student_id, csid) ct,
fir."semesterId",
fir.root_prerequisite,
selection
from (select mc."majorId" mid, *
from (select *
from (select cs.id csid, cs."courseId" cname, c."courseName" nme, *
from "courseSections" cs
join "Courses" c on c."courseId" = cs."courseId") fi
join "courseSectionClasses" csc on csc."sectionId" = fi.csid) f
left join "majorCourses" mc on mc."courseId" = f.cname) fir
join "Users" u on fir."instructorId" = u."userId" and "semesterId" = semester_id and
not (ignoreFull and check_course_full(fir.csid)) and
not (ignoreConflict and get_time_bad((student_id), csid) is not null) and
not (ignorePassed and check_course_passed(fir.csid, student_id)) and
not (ignoreMissingPrerequisites and
not check_prerequisite_by_csc_id(fir.csid, student_id)) and
(search_cid is null or fir.cname like '%' || search_cid || '%') and
(search_name is null or
fir.nme || '[' || fir."sectionName" || ']' like
'%' || search_name || '%') and
(search_classtime is null or
("classStart" <= search_classtime and "classEnd" >= search_classtime))
and
(searchCourseType is null or
((searchCourseType != 'MAJOR_COMPULSORY' or (selection = 'COMPULSORY')) and
(searchCourseType != 'MAJOR_ELECTIVE' or (selection = 'ELECTIVE')) and
(searchCourseType != 'PUBLIC' or (selection is null)) and
(searchCourseType != 'CROSS_MAJOR' or (selection is not null)) and
(searchCourseType = 'ALL' or searchCourseType = 'PUBLIC' or
((searchCourseType = 'CROSS_MAJOR' and (select count(*)
from "Students" ss
where ss."userId" = student_id
and ss."majorId"
= mid) =
0) or
(searchCourseType != 'CROSS_MAJOR' and (select count(*)
from "Students" ss
where ss."userId" = student_id
and ss."majorId" != mid) =
0)))))
and
(search_dayofweek is null or "dayOfWeek" = search_dayofweek) and
(search_instructor is null or
"firstName" like (search_instructor || '%') or
"lastName" like (search_instructor || '%') or
"firstName" || "lastName" like (search_instructor || '%')) and
(searchClassLocations is null or
check_place_fine(searchClassLocations, fir.location))
group by (csid, cname, fir.nme, fir."sectionName", fir."totalCapacity",
fir."leftCapacity", fir.credit, fir."classHour", fir.grading, fir."semesterId",
fir.root_prerequisite, selection)
order by cname, fir.nme || '[' || fir."sectionName" || ']'
offset pageSize * pageIndex limit pageSize);
end
$$;
alter function search_course(integer, integer, varchar, varchar, varchar, varchar, smallint, character varying[], varchar, boolean, boolean, boolean, boolean, integer, integer) owner to checker;