2 -- base-schema.sql - The baseline database schema for Giyusit (revision 3)
4 PRAGMA encoding = 'UTF-8';
9 CREATE TABLE FileParams
11 Key VARCHAR UNIQUE NOT NULL,
15 INSERT INTO FileParams (Key, Value) VALUES ('SchemaRevision', '3');
16 INSERT INTO FileParams (Key, Value) VALUES ('DefaultCandidateStatus', '1');
17 INSERT INTO FileParams (Key, Value) VALUES ('DefaultAttendaceType', '2');
20 CREATE TABLE DataViews
22 Title VARCHAR NOT NULL,
23 CategoryID INTEGER NOT NULL,
24 Query VARCHAR NOT NULL,
25 Ruler VARCHAR NOT NULL,
26 UserInd BOOLEAN DEFAULT 'false'
29 CREATE TABLE DataViewCategories
31 ID INTEGER PRIMARY KEY,
35 INSERT INTO DataViewCategories (ID, Name) VALUES (1, 'מועמדים');
36 INSERT INTO DataViewCategories (ID, Name) VALUES (2, 'אירועים');
39 CREATE TABLE RulerLibrary
41 Name VARCHAR NOT NULL,
42 Ruler VARCHAR NOT NULL
46 CREATE TABLE BooleanValues
52 INSERT INTO BooleanValues (Key, Value) VALUES ('true', 'כן');
53 INSERT INTO BooleanValues (Key, Value) VALUES ('false', 'לא');
59 CREATE TABLE Candidates
61 ID INTEGER PRIMARY KEY,
63 FirstName VARCHAR NOT NULL ON CONFLICT IGNORE,
72 WrongDetailsInd BOOLEAN,
81 CanceledDahashInd BOOLEAN,
82 SignedDahashInd BOOLEAN
85 CREATE TRIGGER OnCandidateInsert AFTER INSERT ON Candidates
86 WHEN NEW.StatusID IS NULL
88 UPDATE Candidates SET StatusID =
89 (SELECT Value FROM FileParams WHERE Key = 'DefaultCandidateStatus')
92 INSERT INTO CandidateStatuses (CandidateID, PriStatusID)
93 VALUES (NEW.ID, (SELECT Value FROM FileParams WHERE Key = 'DefaultCandidateStatus'));
96 CREATE TRIGGER OnCandidateDelete AFTER DELETE ON Candidates
98 DELETE FROM CandidateStatuses WHERE CandidateID = OLD.ID;
99 DELETE FROM EventAttendance WHERE CandidateID = OLD.ID;
103 CREATE TABLE CandidateStatuses
105 ID INTEGER PRIMARY KEY,
106 CandidateID INTEGER NOT NULL,
107 PriStatusID INTEGER NOT NULL,
109 StartTime TIMESTAMP DEFAULT CURRENT_DATE,
111 UNIQUE(CandidateID, StartTime)
114 CREATE TRIGGER OnCStatusInsert AFTER INSERT ON CandidateStatuses
116 UPDATE Candidates SET StatusID =
117 (SELECT PriStatusID FROM CandidateStatuses
118 WHERE CandidateID = NEW.CandidateID ORDER BY StartTime DESC)
119 WHERE ID = NEW.CandidateID;
122 CREATE TRIGGER OnCStatusDelete AFTER DELETE ON CandidateStatuses
124 UPDATE Candidates SET StatusID =
125 (SELECT PriStatusID FROM CandidateStatuses
126 WHERE CandidateID = OLD.CandidateID ORDER BY StartTime DESC)
127 WHERE ID = OLD.CandidateID;
131 CREATE TABLE CandidateStatusValues
133 ID INTEGER PRIMARY KEY,
134 Name VARCHAR NOT NULL,
136 ActiveInd BOOLEAN DEFAULT 'true',
141 INSERT INTO CandidateStatusValues (ID, Name) VALUES (1, 'חדש');
149 ID INTEGER PRIMARY KEY,
150 TypeID INTEGER NOT NULL,
153 Name VARCHAR NOT NULL,
160 CREATE TRIGGER OnEventDelete AFTER DELETE ON Events
162 DELETE FROM EventAttendance WHERE EventID = OLD.ID;
166 CREATE TABLE EventTypes
168 ID INTEGER PRIMARY KEY,
174 INSERT INTO EventTypes (Name) VALUES ('שבת פתוחה');
175 INSERT INTO EventTypes (Name) VALUES ('אורחות');
176 INSERT INTO EventTypes (Name) VALUES ('כנס מחויבות');
177 INSERT INTO EventTypes (Name) VALUES ('טיול פסח');
178 INSERT INTO EventTypes (Name) VALUES ('שבת גיבוש');
179 INSERT INTO EventTypes (Name) VALUES ('אחר');
182 CREATE TABLE EventAttendance
184 ID INTEGER PRIMARY KEY,
185 CandidateID INTEGER NOT NULL,
186 EventID INTEGER NOT NULL,
190 UNIQUE (CandidateID, EventID) ON CONFLICT IGNORE
193 CREATE TRIGGER OnEAttendanceInsert AFTER INSERT ON EventAttendance
194 WHEN NEW.AttTypeID IS NULL
196 UPDATE EventAttendance SET AttTypeID =
197 (SELECT Value FROM FileParams WHERE Key = 'DefaultAttendaceType')
202 CREATE TABLE AttendanceTypes
204 ID INTEGER PRIMARY KEY,
205 Name VARCHAR NOT NULL,
206 ActiveInd BOOLEAN DEFAULT 'true'
209 INSERT INTO AttendanceTypes (ID, Name, ActiveInd) VALUES (1, 'פוטנציאל מחושב', 'true');
210 INSERT INTO AttendanceTypes (ID, Name, ActiveInd) VALUES (2, 'מעוניין להשתתף', 'true');
211 INSERT INTO AttendanceTypes (ID, Name, ActiveInd) VALUES (3, 'אישר השתתפות', 'true');
212 INSERT INTO AttendanceTypes (ID, Name, ActiveInd) VALUES (4, 'השתתף בפועל', 'true');
213 INSERT INTO AttendanceTypes (ID, Name, ActiveInd) VALUES (5, 'לא מעוניין להשתתף', 'false');
214 INSERT INTO AttendanceTypes (ID, Name, ActiveInd) VALUES (6, 'כנראה לא ישתתף', 'false');
215 INSERT INTO AttendanceTypes (ID, Name, ActiveInd) VALUES (7, 'לא השתתף בפועל', 'false');
216 INSERT INTO AttendanceTypes (ID, Name, ActiveInd) VALUES (8, 'ביטל השתתפות', 'false');
224 ID INTEGER PRIMARY KEY,
226 Name VARCHAR NOT NULL,
228 RealInd BOOLEAN DEFAULT 'true'
231 CREATE TRIGGER OnStaffDelete AFTER DELETE ON Staff
233 -- Set owner of owned candidates and events to null
234 UPDATE Candidates SET OwnerID = NULL WHERE OwnerID = OLD.ID;
235 UPDATE Candidates SET RecruiterID = NULL WHERE RecruiterID = OLD.ID;
237 UPDATE Events SET OwnerID = NULL WHERE OwnerID = OLD.ID;
239 -- Re-parent children
240 UPDATE Staff SET ParentID = OLD.ParentID WHERE ParentID = OLD.ID;
246 CREATE VIEW AllCandidates AS SELECT
249 CSV.ActiveInd AS ActiveInd,
251 (C.FirstName || coalesce(' ' || C.LastName, '')) AS FullName
253 LEFT OUTER JOIN CandidateStatusValues CSV ON C.StatusID = CSV.ID
254 LEFT OUTER JOIN Staff ON C.OwnerID = Staff.ID;
256 CREATE VIEW StatusesView AS SELECT
258 strftime('%d/%m/%Y', CS.StartTime) AS StartDate,
259 CSV.Name AS StatusName
260 FROM CandidateStatuses CS, CandidateStatusValues CSV
261 WHERE CS.PriStatusID = CSV.ID
262 ORDER BY CS.StartTime DESC;
264 CREATE VIEW EventAttendanceCount AS SELECT
266 count(*) AS Attendants
270 CREATE VIEW EventActiveAttendanceCount AS SELECT
272 count(*) AS Attendants
274 WHERE AttTypeID IN (SELECT ID FROM AttendanceTypes WHERE ActiveInd = 'true')
277 CREATE VIEW AllEvents AS SELECT
280 E.OwnerID AS OwnerID,
282 E.Location AS Location,
284 E.StartDate AS ISOStartDate,
285 E.EndDate AS ISOEndDate,
287 strftime('%d/%m/%Y', E.StartDate) AS StartDate,
288 strftime('%d/%m/%Y', E.EndDate) AS EndDate,
289 coalesce(EAAC.Attendants, 0) AS ActiveAttendants,
290 EventTypes.Name AS Type,
293 LEFT OUTER JOIN EventActiveAttendanceCount EAAC ON E.ID = EAAC.EventID
294 LEFT OUTER JOIN EventTypes ON E.TypeID = EventTypes.ID
295 LEFT OUTER JOIN Staff ON E.OwnerID = Staff.ID;
297 CREATE VIEW EventCandidatesView AS SELECT
298 EA.CandidateID AS ID,
311 FROM EventAttendance EA
312 LEFT OUTER JOIN Candidates C ON EA.CandidateID = C.ID
313 LEFT OUTER JOIN AttendanceTypes AT ON EA.AttTypeID = AT.ID
314 LEFT OUTER JOIN BooleanValues BV ON AT.ActiveInd = BV.Key;
316 CREATE VIEW CandidateEventsView AS SELECT
321 strftime('%d/%m/%Y', E.StartDate) AS StartDate,
322 strftime('%d/%m/%Y', E.EndDate) AS EndDate,
327 FROM EventAttendance EA
328 LEFT OUTER JOIN Events E ON EA.EventID = E.ID
329 LEFT OUTER JOIN AttendanceTypes AT ON EA.AttTypeID = AT.ID
330 LEFT OUTER JOIN EventTypes ET ON E.TypeID = ET.ID
331 LEFT OUTER JOIN BooleanValues BV ON AT.ActiveInd = BV.Key;