forked from sadeqn/voting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoting-with-data-old.sql
275 lines (227 loc) · 8.97 KB
/
voting-with-data-old.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
-- phpMyAdmin SQL Dump
-- version 4.0.4
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Aug 24, 2013 at 02:46 AM
-- Server version: 5.6.12-log
-- PHP Version: 5.4.16
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: `voting`
--
CREATE DATABASE IF NOT EXISTS `voting` DEFAULT CHARACTER SET utf8 COLLATE utf8_persian_ci;
USE `voting`;
DELIMITER $$
--
-- Functions
--
CREATE DEFINER=`root`@`localhost` FUNCTION `updatepass`(
userID int(10),
oldpass VARCHAR(45),
newpass VARCHAR(45)
) RETURNS tinyint(1)
READS SQL DATA
BEGIN
DECLARE no_more_rows BOOLEAN;
DECLARE oldvote VARCHAR(45);
DECLARE votes_cur CURSOR FOR
SELECT
`vote`
FROM `uservote`
WHERE `userID` = userID;
if @@error_count!=0 or @@warning_count!=0 then
return false;
end if;
set @check=(select `ID` from `user` where `ID`=userID and `password`=md5(concat(`username`,oldpass)));
if @@error_count!=0 or @@warning_count!=0 or @check <=0 then
return false;
end if;
insert into `tmppasschange` (select userID,`electionID`,`candidateID`,md5(concat(`electionID`,`candidateID`,oldpass)) from vote);
if @@error_count!=0 or @@warning_count!=0 then
return false;
end if;
set @votescount=(SELECT
count(`vote`)
FROM `uservote`
WHERE `userID` = userID);
if @votescount>0 then
the_loop: LOOP
FETCH votes_cur INTO oldvote;
IF no_more_rows THEN
CLOSE votes_cur;
LEAVE the_loop;
END IF;
set @newvote = (select md5(concat(`electionID`,`candidateID`,newpass)) from tmppasschange where `vote` = oldvote);
update `uservote` set `vote` = @newvote where `vote`=oldvote;
if @@error_count!=0 or @@warning_count!=0 then
return false;
end if;
END LOOP the_loop;
end if;
delete from `tmppasschange` where `userID` = userID;
if @@error_count!=0 or @@warning_count!=0 then
return false;
end if;
update user set `password` = md5(concat(`username`,newpass)) where `ID` =userID;
if @@error_count!=0 or @@warning_count!=0 then
return false;
end if;
return true;
END$$
DELIMITER ;
-- --------------------------------------------------------
--
-- Table structure for table `candidate`
--
CREATE TABLE IF NOT EXISTS `candidate` (
`ID` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(45) COLLATE utf8_persian_ci DEFAULT NULL,
`description` varchar(4000) COLLATE utf8_persian_ci DEFAULT NULL,
`deleted` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci AUTO_INCREMENT=13 ;
--
-- Dumping data for table `candidate`
--
INSERT INTO `candidate` (`ID`, `name`, `description`, `deleted`) VALUES
(1, 'محمد ابراهیمی', 'مدیرعامل انجمن سمپاد', NULL),
(2, 'محمود بافقی', 'کاندیدای انتخابات چهارم', NULL),
(3, 'علی بحری زاده', 'کاندیدای انتخابات چهارم', NULL),
(4, 'حامد بهادرزاده', 'کاندیدای انتخابات چهارم. فارغ التحصیل ۱۳۸۴', NULL),
(5, 'سمانه جعفری زاده', 'کاندیدای انتخابات چهارم', NULL),
(6, 'هادی خبیری', 'کاندیدای انتخابات چهارم', NULL),
(7, 'علی رفیعفر', 'کاندیدای انتخابات چهارم', NULL),
(8, 'محمد مهدی روشنی', 'کاندیدای انتخابات چهارم', NULL),
(9, 'سید محمدعلی مدرسی', 'کاندیدای انتخابات چهارم', NULL),
(10, 'منیره منتظری', 'کاندیدای انتخابات چهارم', NULL),
(11, 'محسن شریفی', 'کاندیدای انتخابات چهارم (بازرس)', NULL),
(12, 'امیرحسین عطائیان', 'کاندیدای انتخابات چهارم (بازرس)', NULL);
-- --------------------------------------------------------
--
-- Table structure for table `election`
--
CREATE TABLE IF NOT EXISTS `election` (
`ID` int(10) NOT NULL AUTO_INCREMENT,
`electingNumber` int(10) DEFAULT NULL,
`name` varchar(500) COLLATE utf8_persian_ci DEFAULT NULL,
`description` varchar(4000) COLLATE utf8_persian_ci DEFAULT NULL,
`startTime` timestamp NULL DEFAULT NULL,
`endTime` timestamp NULL DEFAULT NULL,
`deleted` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci AUTO_INCREMENT=3 ;
--
-- Dumping data for table `election`
--
INSERT INTO `election` (`ID`, `electingNumber`, `name`, `description`, `startTime`, `endTime`, `deleted`) VALUES
(1, 5, 'انتخابات هیئت مدیره انجمن', 'چهارمین دوره انتخابات هیات مدیره <br/> انجمن دانشآموختگان استعدادهای درخشان', '2013-08-27 07:00:00', '2013-08-27 11:00:00', NULL),
(2, 1, 'انتخابات بازرس هیئت مدیره انجمن', 'چهارمین دوره انتخابات بازرس هیات مدیره <br/> انجمن دانشآموختگان استعدادهای درخشان', '2013-08-27 07:00:00', '2013-08-27 11:00:00', NULL);
-- --------------------------------------------------------
--
-- Table structure for table `tmppasschange`
--
CREATE TABLE IF NOT EXISTS `tmppasschange` (
`userID` int(10) NOT NULL,
`electionID` int(10) NOT NULL,
`candidateID` int(10) NOT NULL,
`vote` varchar(45) COLLATE utf8_persian_ci DEFAULT NULL,
PRIMARY KEY (`userID`,`electionID`,`candidateID`)
) ENGINE=MEMORY DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci;
-- --------------------------------------------------------
--
-- Table structure for table `user`
--
CREATE TABLE IF NOT EXISTS `user` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) COLLATE utf8_persian_ci DEFAULT NULL,
`username` varchar(45) COLLATE utf8_persian_ci DEFAULT NULL,
`password` varchar(45) COLLATE utf8_persian_ci DEFAULT NULL,
`roll` enum('admin','user') COLLATE utf8_persian_ci DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci AUTO_INCREMENT=2 ;
--
-- Dumping data for table `user`
--
INSERT INTO `user` (`ID`, `name`, `username`, `password`, `roll`) VALUES
(1, 'admin', 'admin', '761f749e2a021829f7dfe2dede3eb283', 'admin');
-- --------------------------------------------------------
--
-- Table structure for table `userdata`
--
CREATE TABLE IF NOT EXISTS `userdata` (
`userID` int(11) NOT NULL,
`type` varchar(45) COLLATE utf8_persian_ci DEFAULT NULL,
`data` varchar(2000) COLLATE utf8_persian_ci DEFAULT NULL,
PRIMARY KEY (`userID`),
UNIQUE KEY `unique` (`userID`,`type`),
KEY `fk_userdata_1` (`userID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci;
-- --------------------------------------------------------
--
-- Table structure for table `uservote`
--
CREATE TABLE IF NOT EXISTS `uservote` (
`userID` int(11) NOT NULL,
`electionID` int(11) NOT NULL,
`vote` varchar(45) COLLATE utf8_persian_ci DEFAULT NULL,
PRIMARY KEY (`userID`,`electionID`),
KEY `fk_uservote_1` (`userID`),
KEY `fk_uservote_2` (`electionID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci;
-- --------------------------------------------------------
--
-- Table structure for table `vote`
--
CREATE TABLE IF NOT EXISTS `vote` (
`electionID` int(10) NOT NULL,
`candidateID` int(10) NOT NULL,
`vote` int(10) NOT NULL DEFAULT '0',
PRIMARY KEY (`electionID`,`candidateID`),
KEY `fk_vote_1` (`electionID`),
KEY `fk_vote_2` (`candidateID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci;
--
-- Dumping data for table `vote`
--
INSERT INTO `vote` (`electionID`, `candidateID`, `vote`) VALUES
(1, 1, 0),
(1, 2, 0),
(1, 3, 0),
(1, 4, 0),
(1, 5, 0),
(1, 6, 0),
(1, 7, 0),
(1, 8, 0),
(1, 9, 0),
(1, 10, 0),
(2, 11, 0),
(2, 12, 0);
--
-- Constraints for dumped tables
--
--
-- Constraints for table `userdata`
--
ALTER TABLE `userdata`
ADD CONSTRAINT `fk_userdata_1` FOREIGN KEY (`userID`) REFERENCES `user` (`ID`) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table `uservote`
--
ALTER TABLE `uservote`
ADD CONSTRAINT `fk_uservote_1` FOREIGN KEY (`userID`) REFERENCES `user` (`ID`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `fk_uservote_2` FOREIGN KEY (`electionID`) REFERENCES `election` (`ID`) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table `vote`
--
ALTER TABLE `vote`
ADD CONSTRAINT `fk_vote_1` FOREIGN KEY (`electionID`) REFERENCES `election` (`ID`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `fk_vote_2` FOREIGN KEY (`candidateID`) REFERENCES `candidate` (`ID`);
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;