1 | <?php
|
---|
2 |
|
---|
3 | #implementare controllo con: SHOW INDEX FROM tua_tabella WHERE tua_tabella.tua_colonna='nome_colonna';
|
---|
4 | @require_once("config.php");
|
---|
5 | try{
|
---|
6 | $dbi = new PDO("mysql:host=$dbhost;charset=latin1", $dbuname, $dbpass, array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION));
|
---|
7 | $sql = "use $dbname";
|
---|
8 | $dbi->exec($sql);
|
---|
9 | }
|
---|
10 | catch(PDOException $e)
|
---|
11 | {
|
---|
12 | die( $sql . "<br>" . $e->getMessage());
|
---|
13 | }
|
---|
14 | ###############
|
---|
15 | function aggiorna($sql,$dbi,$sql2,$num){
|
---|
16 | $ret=0;
|
---|
17 | try{
|
---|
18 | $res = $dbi->prepare("$sql");
|
---|
19 | $res->execute();
|
---|
20 | $ret= 1;
|
---|
21 | }
|
---|
22 | catch(PDOException $e)
|
---|
23 | {
|
---|
24 | $ret=0;
|
---|
25 | echo "<br>".$num.") Fallito: $sql";
|
---|
26 | return $ret;
|
---|
27 | }
|
---|
28 |
|
---|
29 | if("$sql2"!=""){
|
---|
30 | try{
|
---|
31 | $res = $dbi->prepare("$sql2");
|
---|
32 | $res->execute();
|
---|
33 | $ret=2;
|
---|
34 | }
|
---|
35 | catch(PDOException $e)
|
---|
36 | {
|
---|
37 | $ret=0;
|
---|
38 | echo "<br>".$num.") Fallito: $sql2";
|
---|
39 | return $ret;
|
---|
40 | }
|
---|
41 | }
|
---|
42 | echo "<br>".$num.") Aggiornato<br>";
|
---|
43 | return $ret;
|
---|
44 | }
|
---|
45 | function aggiorna_index($sql,$dbi,$sql2,$num){
|
---|
46 | $ret=0;
|
---|
47 | try{
|
---|
48 | $res = $dbi->prepare("$sql");
|
---|
49 | $res->execute();
|
---|
50 | $ret= 1;
|
---|
51 | }
|
---|
52 | catch(PDOException $e)
|
---|
53 | {
|
---|
54 | $ret=0;
|
---|
55 | echo "<br>".$num.") Fallito: $sql";
|
---|
56 | return $ret;
|
---|
57 | }
|
---|
58 |
|
---|
59 | if("$sql2"!=""){
|
---|
60 | try{
|
---|
61 | $res = $dbi->prepare("$sql2");
|
---|
62 | $res->execute();
|
---|
63 | $ret=2;
|
---|
64 | }
|
---|
65 | catch(PDOException $e)
|
---|
66 | {
|
---|
67 | $ret=0;
|
---|
68 | echo "<br>".$num.") Fallito: $sql2";
|
---|
69 | return $ret;
|
---|
70 | }
|
---|
71 | }
|
---|
72 | echo "<br>".$num.") Index aggiornato<br>";
|
---|
73 | return $ret;
|
---|
74 | }
|
---|
75 |
|
---|
76 |
|
---|
77 | $num=0;
|
---|
78 |
|
---|
79 | function controllo($tabella,$campo,$num)
|
---|
80 | {
|
---|
81 | global $dbi, $dbname;
|
---|
82 | $sql="SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '$dbname' AND TABLE_NAME = '$tabella'";
|
---|
83 | $res = $dbi->prepare("$sql");
|
---|
84 | $res->execute();
|
---|
85 | if($res->rowCount() and $campo=='') return 1;
|
---|
86 | while(list($nome)=$res->fetch(PDO::FETCH_NUM)) {if($nome==$campo) { echo "<br>".$num.") Aggiornamento già effettuato<br>"; return 1;}}
|
---|
87 | return 0;
|
---|
88 | }
|
---|
89 |
|
---|
90 | $sql="SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = '$dbname' AND COLUMN_NAME = 'Versione'";
|
---|
91 | $res = $dbi->prepare("$sql");
|
---|
92 | $res->execute();
|
---|
93 |
|
---|
94 | if(!controllo($prefix.'_config','versione',++$num))
|
---|
95 | {
|
---|
96 | $sql="alter table `".$prefix."_config` change column `Versione` `versione` int(3)";
|
---|
97 | $ret=aggiorna($sql,$dbi,'',$num);
|
---|
98 | }
|
---|
99 |
|
---|
100 | if(!controllo($prefix.'_ele_gruppo','num_circ',++$num))
|
---|
101 | {
|
---|
102 | $sql="ALTER TABLE `".$prefix."_ele_gruppo` ADD `num_circ` INT(2) UNSIGNED NOT NULL AFTER `id_circ`";
|
---|
103 | $ret=aggiorna($sql,$dbi,'',$num);
|
---|
104 | # if(!$ret) echo "<br>".$num.") Fallito: $sql"; else echo "<br>".$num.") Aggiornato<br>";
|
---|
105 | }
|
---|
106 |
|
---|
107 | if(!controllo($prefix.'_ele_voti_gruppo','num_gruppo',++$num))
|
---|
108 | {
|
---|
109 | $sql="ALTER TABLE `".$prefix."_ele_voti_gruppo` ADD `num_gruppo` INT(2) UNSIGNED NOT NULL AFTER `id_sez`";
|
---|
110 | $ret=aggiorna($sql,$dbi,'',$num);
|
---|
111 | }
|
---|
112 |
|
---|
113 | if(!controllo($prefix.'_ele_lista','num_gruppo',++$num))
|
---|
114 | {
|
---|
115 | $sql="ALTER TABLE `".$prefix."_ele_lista` ADD `num_gruppo` INT(2) UNSIGNED NOT NULL AFTER `id_gruppo`";
|
---|
116 | $ret=aggiorna($sql,$dbi,'',$num);
|
---|
117 | }
|
---|
118 |
|
---|
119 | if(!controllo($prefix.'_ele_lista','num_circ',++$num))
|
---|
120 | {
|
---|
121 | $sql="ALTER TABLE `".$prefix."_ele_lista` ADD `num_circ` INT(2) UNSIGNED NOT NULL AFTER `id_circ`";
|
---|
122 | $ret=aggiorna($sql,$dbi,'',$num);
|
---|
123 | }
|
---|
124 | if(!controllo($prefix.'_ele_operatori','id_circ',++$num))
|
---|
125 | {
|
---|
126 | $sql="ALTER TABLE `".$prefix."_ele_operatori` ADD `id_circ` INT(11) NOT NULL DEFAULT 0 AFTER `aid`";
|
---|
127 | $ret=aggiorna($sql,$dbi,'',$num);
|
---|
128 | }
|
---|
129 | if(!controllo($prefix.'_ele_operatori','id_circ',++$num))
|
---|
130 | {
|
---|
131 | $sql="ALTER TABLE `".$prefix."_ele_operatori` ADD `id_sez` INT(11) NOT NULL DEFAULT 0 AFTER `id_circ`";
|
---|
132 | $ret=aggiorna($sql,$dbi,'',$num);
|
---|
133 | }
|
---|
134 | if(!controllo($prefix.'_ele_voti_lista','num_lista',++$num))
|
---|
135 | {
|
---|
136 | $sql="ALTER TABLE `".$prefix."_ele_voti_lista` ADD `num_lista` INT(2) UNSIGNED NOT NULL AFTER `id_sez`";
|
---|
137 | $ret=aggiorna($sql,$dbi,'',$num);
|
---|
138 | }
|
---|
139 |
|
---|
140 | if(!controllo($prefix.'_ele_voti_ref','num_gruppo',++$num))
|
---|
141 | {
|
---|
142 | $sql="ALTER TABLE `".$prefix."_ele_voti_ref` ADD `num_gruppo` INT(2) UNSIGNED NOT NULL AFTER `id_sez`";
|
---|
143 | $ret=aggiorna($sql,$dbi,'',$num);
|
---|
144 | }
|
---|
145 |
|
---|
146 | if(!controllo($prefix.'_ele_voti_candidati','num_cand',++$num))
|
---|
147 | {
|
---|
148 | $sql="ALTER TABLE `".$prefix."_ele_voti_candidati` ADD `num_cand` INT(2) UNSIGNED NOT NULL AFTER `id_sez`";
|
---|
149 | $ret=aggiorna($sql,$dbi,'',$num);
|
---|
150 | }
|
---|
151 |
|
---|
152 | if(!controllo($prefix.'_ele_candidati','num_lista',++$num))
|
---|
153 | {
|
---|
154 | $sql="ALTER TABLE `".$prefix."_ele_candidati` ADD `num_lista` INT(2) UNSIGNED NOT NULL AFTER `id_lista`";
|
---|
155 | $ret=aggiorna($sql,$dbi,'',$num);
|
---|
156 | }
|
---|
157 |
|
---|
158 | if(!controllo($prefix.'_ele_sezioni','colore',++$num))
|
---|
159 | {
|
---|
160 | $sql="ALTER TABLE `".$prefix."_ele_sezioni` ADD `colore` VARCHAR(50) NULL AFTER `solo_lista`";
|
---|
161 | $ret=aggiorna($sql,$dbi,$sql2,$num);
|
---|
162 | }
|
---|
163 | echo "<br>Aggiornamento per nuovo sistema dei controlli di congruità ";
|
---|
164 |
|
---|
165 | if(!controllo($prefix.'_ele_controlli','id_cons',++$num))
|
---|
166 | {
|
---|
167 | $sql="CREATE TABLE if not exists`".$prefix."_ele_controlli` ( `id_cons` INT(11) NOT NULL , `id_sez` INT(11) NOT NULL , `tipo` VARCHAR(10) NOT NULL , `id` INT(11) NOT NULL , INDEX `sezione` (`id_sez`)) ENGINE = MyISAM";
|
---|
168 | echo "<br>".$num.") Creazione tabella dei controlli: ";
|
---|
169 | $ret=aggiorna($sql,$dbi,'',$num);
|
---|
170 | }
|
---|
171 |
|
---|
172 | echo "<br>Eliminazione della vecchia tabella dei controlli: ";
|
---|
173 | if(controllo($prefix.'_ele_controllisez','',++$num))
|
---|
174 | {
|
---|
175 | $sql="DROP TABLE if exists `".$prefix."_ele_controllisez`";
|
---|
176 | $ret=aggiorna($sql,$dbi,'',$num);
|
---|
177 | } else echo "<br>$num) Tabella non presente<br>";
|
---|
178 |
|
---|
179 | $sql="update `".$prefix."_ele_voti_gruppo` as t1 left join `".$prefix."_ele_gruppo` as t2 on t1.id_gruppo=t2.id_gruppo set t1.num_gruppo=t2.num_gruppo;";
|
---|
180 | $ret=aggiorna($sql,$dbi,'',++$num);
|
---|
181 |
|
---|
182 | $sql="update `".$prefix."_ele_voti_lista` as t1 left join `".$prefix."_ele_lista` as t2 on t1.id_lista=t2.id_lista set t1.num_lista=t2.num_lista;";
|
---|
183 | $ret=aggiorna($sql,$dbi,'',++$num);
|
---|
184 | ##############################################
|
---|
185 |
|
---|
186 | $sql="ALTER TABLE `".$prefix."_ele_gruppo` CHANGE `num_circ` `num_circ` INT(2) UNSIGNED NOT NULL DEFAULT '1';";
|
---|
187 | $ret=aggiorna($sql,$dbi,'',++$num);
|
---|
188 |
|
---|
189 | $sql="ALTER TABLE `".$prefix."_ele_lista` CHANGE `num_gruppo` `num_gruppo` INT(2) UNSIGNED NOT NULL DEFAULT '0';";
|
---|
190 | $ret=aggiorna($sql,$dbi,'',++$num);
|
---|
191 |
|
---|
192 | $sql="ALTER TABLE `".$prefix."_ele_lista` CHANGE `num_circ` `num_circ` INT(2) UNSIGNED NOT NULL DEFAULT '1';";
|
---|
193 | $ret=aggiorna($sql,$dbi,'',++$num);
|
---|
194 |
|
---|
195 | $sql="DROP TABLE `".$prefix."_ele_fasce`";
|
---|
196 | $ret=aggiorna($sql,$dbi,'',++$num);
|
---|
197 |
|
---|
198 | $sql="CREATE TABLE `".$prefix."_ele_fasce` (
|
---|
199 | `id_fascia` int(2) NOT NULL,
|
---|
200 | `abitanti` int(11) NOT NULL,
|
---|
201 | `seggi` int(4) NOT NULL,
|
---|
202 | `id_conf` int(11) DEFAULT 1
|
---|
203 | ) ENGINE=MyISAM DEFAULT CHARSET=latin1;";
|
---|
204 |
|
---|
205 |
|
---|
206 | $sql2="INSERT INTO `".$prefix."_ele_fasce` (`id_fascia`, `abitanti`, `seggi`, `id_conf`) VALUES
|
---|
207 | (1, 3000, 12, 1),
|
---|
208 | (2, 10000, 16, 1),
|
---|
209 | (3, 15000, 20, 1),
|
---|
210 | (4, 30000, 20, 1),
|
---|
211 | (5, 100000, 30, 1),
|
---|
212 | (6, 250000, 40, 1),
|
---|
213 | (7, 500000, 46, 1),
|
---|
214 | (8, 1000000, 50, 1),
|
---|
215 | (9, 100000000, 60, 1),
|
---|
216 | (1, 3000, 12, 2),
|
---|
217 | (2, 10000, 16, 2),
|
---|
218 | (3, 15000, 20, 2),
|
---|
219 | (4, 30000, 20, 2),
|
---|
220 | (5, 100000, 30, 2),
|
---|
221 | (6, 250000, 40, 2),
|
---|
222 | (7, 500000, 46, 2),
|
---|
223 | (8, 1000000, 50, 2),
|
---|
224 | (9, 100000000, 60, 2),
|
---|
225 | (1, 3000, 9, 3),
|
---|
226 | (2, 5000, 9, 3),
|
---|
227 | (4, 15000, 16, 3),
|
---|
228 | (3, 10000, 12, 3),
|
---|
229 | (5, 30000, 16, 3),
|
---|
230 | (6, 100000, 24, 3),
|
---|
231 | (7, 250000, 32, 3),
|
---|
232 | (8, 500000, 36, 3),
|
---|
233 | (9, 1000000, 40, 3),
|
---|
234 | (10, 100000000, 48, 3),
|
---|
235 | (1, 3000, 6, 4),
|
---|
236 | (2, 5000, 7, 4),
|
---|
237 | (3, 10000, 12, 4),
|
---|
238 | (4, 15000, 16, 4),
|
---|
239 | (5, 30000, 16, 4),
|
---|
240 | (6, 100000, 24, 4),
|
---|
241 | (7, 250000, 32, 4),
|
---|
242 | (8, 500000, 36, 4),
|
---|
243 | (9, 1000000, 40, 4),
|
---|
244 | (10, 100000000, 48, 4),
|
---|
245 | (1, 3000, 9, 5),
|
---|
246 | (2, 5000, 9, 5),
|
---|
247 | (3, 10000, 12, 5),
|
---|
248 | (4, 15000, 16, 5),
|
---|
249 | (5, 30000, 16, 5),
|
---|
250 | (6, 100000, 24, 5),
|
---|
251 | (7, 250000, 32, 5),
|
---|
252 | (8, 500000, 36, 5),
|
---|
253 | (9, 1000000, 40, 5),
|
---|
254 | (10, 100000000, 48, 5),
|
---|
255 | (1, 3000, 6, 6),
|
---|
256 | (2, 5000, 7, 6),
|
---|
257 | (3, 10000, 10, 6),
|
---|
258 | (4, 15000, 16, 6),
|
---|
259 | (5, 30000, 16, 6),
|
---|
260 | (6, 100000, 24, 6),
|
---|
261 | (7, 250000, 32, 6),
|
---|
262 | (8, 500000, 36, 6),
|
---|
263 | (9, 1000000, 40, 6),
|
---|
264 | (10, 100000000, 48, 6),
|
---|
265 | (1, 3000, 10, 7),
|
---|
266 | (2, 10000, 12, 7),
|
---|
267 | (3, 15000, 16, 7),
|
---|
268 | (4, 30000, 16, 7),
|
---|
269 | (5, 100000, 24, 7),
|
---|
270 | (6, 250000, 32, 7),
|
---|
271 | (7, 500000, 36, 7),
|
---|
272 | (8, 1000000, 40, 7),
|
---|
273 | (9, 100000000, 48, 7);";
|
---|
274 | $ret=aggiorna($sql,$dbi,$sql2,++$num);
|
---|
275 | #if(!$ret) echo "<br>".$num++.") Fallito: $sql"; else echo "<br>".$num++.") Aggiornato<br>";
|
---|
276 |
|
---|
277 | $sql="ALTER TABLE `".$prefix."_ele_fasce`
|
---|
278 | ADD KEY `id_fascia` (`id_fascia`);";
|
---|
279 | $ret=aggiorna($sql,$dbi,'',++$num);
|
---|
280 | #if(!$ret) echo "<br>".$num++.") Fallito: $sql"; else echo "<br>".$num++.") Aggiornato<br>";
|
---|
281 |
|
---|
282 |
|
---|
283 | $sql="ALTER TABLE `".$prefix."_ele_voti_ref` DROP INDEX `id_cons`; ";
|
---|
284 | $sql2="ALTER TABLE `".$prefix."_ele_voti_ref` ADD INDEX `id_cons` (`id_cons`, `id_gruppo`) USING BTREE";
|
---|
285 | $ret=aggiorna_index($sql,$dbi,$sql2,++$num);
|
---|
286 |
|
---|
287 | #if(!$ret)
|
---|
288 | # echo "<br>".$num++.") Fallito: $sql";
|
---|
289 | #elseif ($ret==1)
|
---|
290 | # echo "<br>".$num++.") Index eliminato";
|
---|
291 | #else
|
---|
292 | # echo "<br>".$num++.") Index aggiunto";
|
---|
293 | $sql="ALTER TABLE `".$prefix."_ele_voti_lista` DROP INDEX `id_cons`";
|
---|
294 | $sql2="ALTER TABLE `".$prefix."_ele_voti_lista` ADD INDEX `id_cons` (`id_cons`, `id_sez`, `id_lista`) USING BTREE";
|
---|
295 | $ret=aggiorna_index($sql,$dbi,$sql2,++$num);
|
---|
296 |
|
---|
297 | $sql="ALTER TABLE `".$prefix."_ele_voti_gruppo` DROP INDEX `id_cons`";
|
---|
298 | $sql2="ALTER TABLE `".$prefix."_ele_voti_gruppo` ADD INDEX `id_cons` (`id_cons`, `id_sez`, `id_gruppo`) USING BTREE";
|
---|
299 | $ret=aggiorna_index($sql,$dbi,$sql2,++$num);
|
---|
300 |
|
---|
301 | $sql="ALTER TABLE `".$prefix."_ele_voti_candidati` DROP INDEX `id_cons`";
|
---|
302 | $sql2="ALTER TABLE `".$prefix."_ele_voti_candidati` ADD INDEX `id_cons` (`id_cons`, `id_sez`, `id_cand`) USING BTREE";
|
---|
303 | $ret=aggiorna_index($sql,$dbi,$sql2,++$num);
|
---|
304 |
|
---|
305 | $sql="ALTER TABLE `".$prefix."_ele_lista` DROP INDEX `id_cons`";
|
---|
306 | $sql2="ALTER TABLE `".$prefix."_ele_lista` ADD INDEX `id_cons` (`id_cons`, `id_gruppo`) USING BTREE";
|
---|
307 | $ret=aggiorna_index($sql,$dbi,$sql2,++$num);
|
---|
308 |
|
---|
309 | $sql="ALTER TABLE `".$prefix."_ele_lista` DROP INDEX `PRIMARY`";
|
---|
310 | $sql2="ALTER TABLE `".$prefix."_ele_lista` ADD PRIMARY KEY (`id_lista`) USING BTREE";
|
---|
311 | $ret=aggiorna_index($sql,$dbi,$sql2,++$num);
|
---|
312 |
|
---|
313 | $sql="ALTER TABLE `".$prefix."_ele_gruppo` DROP INDEX `id_cons`";
|
---|
314 | $sql2="ALTER TABLE `".$prefix."_ele_gruppo` ADD INDEX `id_cons` (`id_cons`, `id_circ`) USING BTREE";
|
---|
315 | $ret=aggiorna_index($sql,$dbi,$sql2,++$num);
|
---|
316 |
|
---|
317 | $sql="ALTER TABLE `".$prefix."_ele_gruppo` DROP INDEX `PRIMARY`";
|
---|
318 | $sql2="ALTER TABLE `".$prefix."_ele_gruppo` ADD PRIMARY KEY (`id_gruppo`) USING BTREE";
|
---|
319 | $ret=aggiorna_index($sql,$dbi,$sql2,++$num);
|
---|
320 |
|
---|
321 | $sql="ALTER TABLE `".$prefix."_ele_candidati` DROP INDEX `id_cons`";
|
---|
322 | $sql2="ALTER TABLE `".$prefix."_ele_candidati` ADD INDEX `id_cons` (`id_cons`, `id_lista`) USING BTREE";
|
---|
323 | $ret=aggiorna_index($sql,$dbi,$sql2,++$num);
|
---|
324 |
|
---|
325 | $sql="ALTER TABLE `".$prefix."_ele_candidati` DROP INDEX `PRIMARY`";
|
---|
326 | $sql2="ALTER TABLE `".$prefix."_ele_candidati` ADD PRIMARY KEY (`id_cand`) USING BTREE";
|
---|
327 | $ret=aggiorna_index($sql,$dbi,$sql2,++$num);
|
---|
328 |
|
---|
329 | $sql="ALTER TABLE `".$prefix."_ele_consultazione` ADD UNIQUE `descrizione` (`descrizione`(100))";
|
---|
330 | $ret=aggiorna_index($sql,$dbi,$sql2,++$num);
|
---|
331 |
|
---|
332 | ?>
|
---|