From 4c9093a453bf7e1a5a792494e6020a126e5fee03 Mon Sep 17 00:00:00 2001
From: PavelBegunkov <asml.Silence@gmail.com>
Date: Tue, 9 Jun 2015 16:32:58 +0300
Subject: [PATCH] db refactoring pt3

---
 db/StoredFunctions.sql  | 49 +++++++++++++++++++++++------------------
 db/StoredProcedures.sql |  4 ++--
 2 files changed, 30 insertions(+), 23 deletions(-)

diff --git a/db/StoredFunctions.sql b/db/StoredFunctions.sql
index fb7379135..374e08fb0 100644
--- a/db/StoredFunctions.sql
+++ b/db/StoredFunctions.sql
@@ -36,6 +36,8 @@ DROP FUNCTION IF EXISTS GetMaxRateForDisc//
 
 DROP FUNCTION IF EXISTS BindTeacher//
 
+DROP FUNCTION IF EXISTS GetDisciplineSemesterID//
+
 
 
 # -------------------------------------------------------------------------------------------
@@ -250,22 +252,31 @@ END //
 # Label: semesters
 # -------------------------------------------------------------------------------------------
 
-DROP FUNCTION IF EXISTS GetDisciplineSemesterID//
-CREATE FUNCTION `GetDisciplineSemesterID` (
-    `pDisciplineID` INT
+DROP FUNCTION IF EXISTS GetDisciplineProperty//
+CREATE FUNCTION `GetDisciplineProperty` (
+    `pDisciplineID` INT,
+    `pType` enum('grade', 'subject', 'author', 'semester', 'milestone')
 ) RETURNS int(11)
     NO SQL
 BEGIN
-    DECLARE vSemesterID INT DEFAULT -1;
+    DECLARE vRes INT DEFAULT -1;
 
-    SELECT disciplines.SemesterID INTO vSemesterID
-    FROM `disciplines`
-    WHERE disciplines.ID = pDisciplineID
-    LIMIT 1;
+    SELECT CASE pType
+            WHEN 'grade' THEN disciplines.GradeID
+            WHEN 'subject' THEN disciplines.SubjectID
+            WHEN 'author' THEN disciplines.AuthorID
+            WHEN 'semester' THEN disciplines.SemesterID
+            WHEN 'milestone' THEN disciplines.Milestone
+        END INTO vRes
+        FROM `disciplines`
+        WHERE disciplines.ID = pDisciplineID
+        LIMIT 1;
 
-    RETURN vSemesterID;
+    RETURN vRes;
 END //
 
+
+
 # -------------------------------------------------------------------------------------------
 # Label: faculties
 # -------------------------------------------------------------------------------------------
@@ -835,13 +846,13 @@ CREATE FUNCTION `Discipline_Create` (
     `pTeacherID` INT,
     `pGradeID` INT,
     `pSubjectID` INT,
-    `pExamType` VARCHAR(30) CHARSET utf8,
+    `pExamType` enum('exam', 'credit', 'grading_credit'),
     `pLectureCount` INT,
     `pPracticeCount` INT,
     `pLabCount` INT,
     `pFacultyID` INT,
     `pSemesterID` INT,
-    `pSubtype` VARCHAR(30) CHARSET utf8 # scientific_coursework discipline_coursework
+    `pSubtype` enum('scientific_coursework', 'disciplinary_course')
 ) RETURNS int(11)
     NO SQL
 BEGIN
@@ -940,11 +951,7 @@ BEGIN
         RETURN -1;
     END IF;
 
-    # get current grade
-    SELECT disciplines.GradeID INTO vCurGradeID
-        FROM `disciplines`
-        WHERE disciplines.ID = pDisciplineID
-        LIMIT 1;
+    SET vCurGradeID = GetDisciplineProperty(pDisciplineID, 'grade');
     IF vCurGradeID = pGradeID THEN
         RETURN 0;
     END IF;
@@ -1100,7 +1107,7 @@ BEGIN
             disciplines_groups.ID = ( @isAttached := LAST_INSERT_ID(disciplines_groups.ID) );
 
     IF @isAttached < 0 THEN # group was attached
-        SET vSemesterID = GetDisciplineSemesterID(pDisciplineID);
+        SET vSemesterID = GetDisciplineProperty(pDisciplineID, 'semester');
 
         # 3. delete students of this group which were bound to discipline before
                 DELETE FROM `disciplines_students`
@@ -1135,7 +1142,7 @@ BEGIN
         RETURN -1;
     END IF;
 
-    SET vSemesterID = GetDisciplineSemesterID(pDisciplineID);
+    SET vSemesterID = GetDisciplineProperty(pDisciplineID, 'semester');
     SET vStudentGroupID = GetStudentGroup(pStudentID, vSemesterID);
 
     # 2. check if student's group is bound yet
@@ -1185,7 +1192,7 @@ BEGIN
                 disciplines_groups.GroupID = pGroupID
         LIMIT 1;
 
-    SET vSemesterID = GetDisciplineSemesterID(pDisciplineID);
+    SET vSemesterID = GetDisciplineProperty(pDisciplineID, 'semester');
 
     # delete attached, and detached (doesn't take disc in any case)
     DELETE FROM `disciplines_students`
@@ -1216,7 +1223,7 @@ BEGIN
         RETURN -1;
     END IF;
 
-    SET vSemesterID = GetDisciplineSemesterID(pDisciplineID);
+    SET vSemesterID = GetDisciplineProperty(pDisciplineID, 'semester');
     SET vStudentGroupID = GetStudentGroup(pStudentID, vSemesterID);
 
     # 2. check if student's group is bound yet
@@ -1420,7 +1427,7 @@ BEGIN
     UPDATE `disciplines`
         SET disciplines.MilestoneDate = CURDATE(),
             disciplines.Milestone = pMilestone
-        WHERE    disciplines.FacultyID= pFacultyID AND
+        WHERE   disciplines.FacultyID= pFacultyID AND
                 disciplines.SemesterID = pSemesterID AND
                 ( disciplines.ExamType = 'credit' OR disciplines.ExamType = 'grading_credit');
     RETURN 0;
diff --git a/db/StoredProcedures.sql b/db/StoredProcedures.sql
index 53f5cfa7a..fb35d4580 100644
--- a/db/StoredProcedures.sql
+++ b/db/StoredProcedures.sql
@@ -500,7 +500,7 @@ CREATE PROCEDURE `GetStudentsForDiscipline` (
     NO SQL
 BEGIN
     DECLARE vSemesterID INT DEFAULT -1;
-    SET vSemesterID = GetDisciplineSemesterID(pDisciplineID);
+    SET vSemesterID = GetDisciplineProperty(pDisciplineID, 'semester');
 
     SELECT  view_disciplines_students.StudentID AS 'ID',
             view_disciplines_students.LastName,
@@ -744,7 +744,7 @@ BEGIN
               disciplines_groups.GroupID = pGroupID
         LIMIT 1;
 
-    SET vSemesterID = GetDisciplineSemesterID(pDisciplineID);
+    SET vSemesterID = GetDisciplineProperty(pDisciplineID, 'semester');
 
     IF !vChecker THEN
         SELECT  students.ID,
-- 
GitLab