apisub.sql 2.99 KB
Newer Older
Stephane Crozat's avatar
Stephane Crozat committed
1
DROP TABLE IF EXISTS subscribe CASCADE;
2
DROP TABLE IF EXISTS api CASCADE;
Stephane Crozat's avatar
Stephane Crozat committed
3
4
5
6

CREATE TABLE api (
  code INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
Stephane Crozat's avatar
Fix #2  
Stephane Crozat committed
7
  description TEXT,
Stephane Crozat's avatar
Stephane Crozat committed
8
9
  year INTEGER NOT NULL,
  semester CHAR(1) NOT NULL,
10
11
  dbegin DATE NOT NULL,
  dend DATE NOT NULL,
12
13
  size INTEGER,
  ects INTEGER NOT NULL,
14
  mail TEXT NOT NULL,
Stephane Crozat's avatar
Stephane Crozat committed
15
16
17
  CHECK (code>0),
  CHECK (year>2018 AND year<2100),
  CHECK (semester IN ('H','E')),
18
  CHECK (dbegin<=dend),
19
20
  CHECK (size>0),
  CHECK (ects>0),
Stephane Crozat's avatar
Stephane Crozat committed
21
22
23
24
25
26
27
28
29
30
  UNIQUE (name,year,semester)
);

CREATE TABLE subscribe (
  utclogin TEXT NOT NULL,
  api INTEGER REFERENCES api(code),
  subdate DATE NOT NULL,
  PRIMARY KEY (utclogin,api)
);

31
/* Table created to collect CAS user surname and firstname */
32
33
34
35
36
37
CREATE TABLE localuser (
  utclogin TEXT PRIMARY KEY,
  surname TEXT,
  firstname TEXT
);

38
39
40
41
42
43
44
45
46
47
48
49
/* Table created to retrieve CSV info from DFP */
CREATE TABLE utcstudent (
  surname TEXT NOT NULL,
  firstname TEXT NOT NULL,
  speciality TEXT NOT NULL,
  level TEXT NOT NULL
);

/*
\COPY utcstudent (firstname,surname,speciality,level) FROM '/home/stc/inscriptions_A18.csv' WITH CSV HEADER DELIMITER ';' QUOTE '"'
*/

50
51
52
53
54
55
56
57
58
59
60
61
62
63
CREATE OR REPLACE FUNCTION unaccent_string(TEXT)
RETURNS TEXT
IMMUTABLE
STRICT
LANGUAGE SQL
AS $$
SELECT translate(
    $1,
    'âãäåÁÂÃÄÅèééêëÈÉÉÊËìíîïìÌÍÎÏÌóôõöÒÓÔÕÖùúûüÙÚÛÜ',
    'aaaaAAAAAeeeeeEEEEEiiiiiIIIIIooooOOOOOuuuuUUUU'
);
$$;

CREATE OR REPLACE VIEW vlocaluser AS
64
SELECT l.utclogin, u.surname, u.firstname, MAX(u.speciality) AS speciality, MAX(u.level) AS level,
65
66
  CASE WHEN COUNT(*)>1 THEN 'duplication' END AS warning,
  l.utclogin || 'etu.utc.fr' AS utcmail
67
FROM localuser l
68
LEFT JOIN utcstudent u ON l.surname=u.surname AND l.firstname=unaccent_string(u.firstname)
69
70
GROUP BY l.utclogin, u.surname, u.firstname;

71
/** Test dataset
Stephane Crozat's avatar
Stephane Crozat committed
72
INSERT INTO api VALUES (
73
  1,'Poésie et ingénierie',2019,'H',TO_DATE('20190121','yyyymmdd'),TO_DATE('20190125','yyyymmdd'),24,2,'stc@utc.fr'
Stephane Crozat's avatar
Stephane Crozat committed
74
75
);
INSERT INTO api VALUES (
76
  2,'Cloud big data blockchain IA',2019,'H',TO_DATE('20190125','yyyymmdd'),TO_DATE('20190131','yyyymmdd'),12,3,'stc@utc.fr'
Stephane Crozat's avatar
Stephane Crozat committed
77
78
79
80
);
INSERT INTO subscribe VALUES (
  'crozatst',1,TO_DATE('20181018','YYYYMMDD')
);
81
**/
Stephane Crozat's avatar
Stephane Crozat committed
82

83
CREATE OR REPLACE VIEW vapi AS
84
85
SELECT
ap.code,
86
CASE WHEN ap.code<10 THEN '000'||ap.code WHEN ap.code>=10 THEN '00'||ap.code END AS normcode,
87
88
ap.name, ap.year, ap.semester,
TO_CHAR(ap.dbegin,'TMday FMDD TMmonth') AS dbegin, TO_CHAR(ap.dend,'TMday FMDD TMmonth') AS dend,
89
ap.mail, ap.ects, ap.size,
90
91
92
COUNT(su.utclogin) AS nbsub
FROM api ap LEFT JOIN subscribe su ON ap.code=su.api
GROUP BY ap.code, normcode, ap.name, ap.year, ap.semester, dbegin, dend, ap.size, ap.ects, ap.mail
93
94
ORDER BY ap.year, ap.semester, ap.dbegin, ap.dend, ap.code;

95
CREATE OR REPLACE VIEW vsubscription AS
96
SELECT ap.*, TO_CHAR(su.subdate,'DD/MM/YYYY') AS subdate, su.utclogin AS sublogin, lo.*
97
98
99
100
FROM subscribe su
JOIN vapi ap ON ap.code=su.api
LEFT JOIN vlocaluser lo ON su.utclogin=lo.utclogin
ORDER BY ap.year, ap.semester, ap.dbegin, ap.dend, ap.code, lo.firstname, lo.surname, su.utclogin;