-
Notifications
You must be signed in to change notification settings - Fork 0
/
voteinsertion.sql
180 lines (161 loc) · 4.63 KB
/
voteinsertion.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
CREATE OR REPLACE FUNCTION generate_voters(
wkid INTEGER,
election INTEGER,
count INTEGER,
haveVoted INTEGER
)
RETURNS VOID AS
$BODY$
DECLARE
BirthDate DATE;
BEGIN
BirthDate := to_date('1900-01-01', 'YYYY-MM-DD');
IF count > haveVoted
THEN
INSERT INTO voter (FirstName, LastName, BirthDate, Address, Gender, Wahlkreis, FirstValidElection, LastVotedOn, LastValidElection)
(
(SELECT R.*
FROM
(VALUES ('FN', 'LN', BirthDate, 'AD', '?', wkid, election, election, NULL :: INTEGER)) AS R, generate_series(1,haveVoted))
UNION ALL
(SELECT R.*
FROM (VALUES ('FN', 'LN', BirthDate, 'AD', '?', wkid, election, NULL :: INTEGER, NULL :: INTEGER)) AS R,
generate_series(1,count - haveVoted))
);
ELSE
INSERT INTO voter (FirstName, LastName, BirthDate, Address, Gender, Wahlkreis, FirstValidElection, LastVotedOn, LastValidElection)
(SELECT R.*
FROM (VALUES ('FN', 'LN', BirthDate, 'AD', '?', wkid, election, NULL :: INTEGER, NULL :: INTEGER)) AS R,
generate_series(1,count));
UPDATE voter
SET LastVotedOn = election
WHERE id IN (
SELECT id
FROM (
SELECT id
FROM voter
WHERE LastVotedOn < election
AND wahlkreis = wkid
LIMIT haveVoted
) tmp
);
END IF;
END;$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION generate_voters( INTEGER, INTEGER, INTEGER, INTEGER )
OWNER TO postgres;
;
CREATE OR REPLACE FUNCTION kill_voters(
wkid INTEGER,
lastElectionAlive INTEGER,
count INTEGER
)
RETURNS VOID AS
$BODY$
DECLARE
BEGIN
UPDATE voter
SET LastValidElection = lastElectionAlive
WHERE id IN (
SELECT id
FROM (
SELECT id
FROM voter
WHERE LastVotedOn <= lastElectionAlive
AND FirstValidElection <= lastElectionAlive
AND wahlkreis = wkid
LIMIT count
) tmp
);
END;$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION kill_voters( INTEGER, INTEGER, INTEGER )
OWNER TO postgres;
;
CREATE OR REPLACE FUNCTION generate_erststimmen(
isinvalid BOOLEAN,
pname CHARACTER VARYING,
wkid INTEGER,
eid INTEGER,
count INTEGER)
RETURNS VOID AS
$BODY$
DECLARE
cID INTEGER;
BEGIN
SELECT d.Candidate,'0'
INTO cID
FROM directmandate d, party p
WHERE d.party = p.id
AND d.wahlkreis = wkID
AND d.election = eID
AND p.name = pName;
INSERT INTO erststimme (isInvalid, Candidate, Wahlkreis, Election)
(SELECT isinvalid, COALESCE(cID,'0'), wkID, eID
FROM (VALUES (isInvalid,cID, wkID, eID)) AS R, generate_series(1,count));
END;$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION generate_erststimmen( BOOLEAN, CHARACTER VARYING, INTEGER, INTEGER, INTEGER )
OWNER TO postgres;
CREATE OR REPLACE FUNCTION generate_zweitstimmen(
isinvalid BOOLEAN,
pname CHARACTER VARYING,
wkid INTEGER,
eid INTEGER,
count INTEGER)
RETURNS VOID AS
$BODY$
DECLARE
pID INTEGER;
BEGIN
SELECT p.id
INTO pID
FROM party p
WHERE p.Name = pName;
INSERT INTO zweitstimme (isInvalid, Party, Wahlkreis, Election)
(SELECT R.*
FROM (VALUES (isInvalid, COALESCE(pID,'0'), wkID, eID)) AS R, generate_series(1,count));
END;$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION generate_zweitstimmen( BOOLEAN, CHARACTER VARYING, INTEGER, INTEGER, INTEGER )
OWNER TO postgres;
-- Function: generate_erststimmen(boolean, character varying, integer, integer, integer)
-- DROP FUNCTION generate_erststimmen(boolean, character varying, integer, integer, integer);
CREATE OR REPLACE FUNCTION generate_uebrige(
wkid INTEGER,
eid INTEGER,
count INTEGER)
RETURNS VOID AS
$BODY$
DECLARE
cIDs INTEGER [];
cID INTEGER;
candidates INTEGER;
BEGIN
SELECT array_agg(d.Candidate)
INTO cIDs
FROM directmandate d
WHERE d.party IS NULL
AND d.wahlkreis = wkID
AND d.election = eID;
candidates = array_length(cIDs, 1);
IF candidates > 0
THEN
FOR i IN 0..candidates LOOP
INSERT INTO erststimme (isInvalid, Candidate, Wahlkreis, Election)
(SELECT R.*
FROM (VALUES (FALSE, COALESCE(cIDs [i],'0'), wkID, eID)) AS R, generate_series(1,count / candidates));
END LOOP;
INSERT INTO erststimme (isInvalid, Candidate, Wahlkreis, Election)
(SELECT R.*
FROM (VALUES (FALSE, cIDs [0], wkID, eID)) AS R, generate_series(1,count % candidates));
END IF;
END;$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION generate_erststimmen( BOOLEAN, CHARACTER VARYING, INTEGER, INTEGER, INTEGER )
OWNER TO postgres;