source: trunk/admin/modules/Elezioni/aggiornadb.php@ 392

Last change on this file since 392 was 392, checked in by roby, 15 months ago
  • ADMIN
  • modifica della funzione di aggiornamento
  • aggiunta di controlli di aggiornamento del db
  • adeguamento degli ultimi aggiormanenti a php < 5.7
  • CLIENT
  • modificata l'apertura delle stampe dei risultati in una nuova finestra
File size: 22.8 KB
Line 
1<?php
2
3#imposta il charset su utf8, qualsiasi altro valore per cambiarlo in latin1;
4$newcs='utf8';
5/*
6@require_once("../../config.php");
7 try{
8 $dbi = new PDO("mysql:host=$dbhost", $dbuname, $dbpass, array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION));
9 $sql = "use $dbname";
10 $dbi->exec($sql);
11 }
12 catch(PDOException $e)
13 {
14 die( $sql . "<br>" . $e->getMessage());
15 }
16*/
17
18###############
19function aggiorna($sql,$dbi,$sql2,$num){
20 $ret=0;
21 try{
22 $res = $dbi->prepare("$sql");
23 $res->execute();
24 $ret= 1;
25 }
26 catch(PDOException $e)
27 {
28 $ret=0;
29 echo "<br><span style=\"color: red;\">- Aggiornamento Fallito: $sql</span>";
30 return $ret;
31 }
32
33 if("$sql2"!=""){
34 try{
35 $res = $dbi->prepare("$sql2");
36 $res->execute();
37 $ret=2;
38 }
39 catch(PDOException $e)
40 {
41 $ret=0;
42 echo "<br><span style=\"color: red;\">- Aggiornamento Fallito: $sql2</span>";
43 return $ret;
44 }
45 }
46 echo "<br><span style=\"color: green;\">- Aggiornamento eseguito correttamente</span>";
47 return $ret;
48}
49
50function aggiorna_index($tab,$ind,$dbi,$sql2,$num){
51 $ret=0;
52# $conn->getAttribute( constant( "PDO::ATTR_$val" ) )
53 $sqltest="SHOW INDEX FROM `$tab` WHERE KEY_NAME = '$ind'";
54 $res = $dbi->prepare("$sqltest");
55 $res->execute();
56
57 if($res->rowCount()) {
58 if($ind=='PRIMARY')
59 $sql="ALTER TABLE `$tab` DROP PRIMARY KEY , $sql2 ";
60 else
61 $sql="ALTER TABLE `$tab` DROP INDEX `$ind`";
62 try{
63 $res = $dbi->prepare("$sql");
64 $res->execute();
65 $ret= 1;
66 }
67 catch(PDOException $e)
68 {
69 $ret=0;
70 echo "<br><span style=\"color: red;\">- Tabella: $tab - Indice: $ind - Aggiornamento Fallito: $sql</span>";
71 return $ret;
72 }
73 }
74 if("$sql2"!="" and $ind!='PRIMARY'){
75 try{
76 $res = $dbi->prepare("$sql2");
77 $res->execute();
78 $ret=2;
79 }
80 catch(PDOException $e)
81 {
82 $ret=0;
83 echo "<br><span style=\"color: red;\">- Tabella: $tab - Indice: $ind - Aggiornamento Fallito: $sql2</span>";
84 return $ret;
85 }
86 }
87 echo "<br><span style=\"color: green;\">- Tabella: $tab - Indice: $ind - Index aggiornato</span>";
88 return $ret;
89}
90
91
92
93
94function controllo($tabella,$campo,$num)
95{
96 global $dbi, $dbname;
97 $sql="SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '$dbname' AND TABLE_NAME = '$tabella'";
98 $res = $dbi->prepare("$sql");
99 $res->execute();
100 if($res->rowCount() and $campo=='') return 1;
101 while(list($nome)=$res->fetch(PDO::FETCH_NUM)) {if($nome==$campo) { echo "<br>".$num.") Il campo: $campo Ú presente nella tabella: $tabella"; return 1;}}
102 if($campo) echo "<br>$num) Il campo: $campo non Ú presente nella tabella: $tabella";
103 return 0;
104}
105
106$num=0;
107
108if(controllo($prefix.'_authors','admincomune',++$num))
109{
110 $sql="UPDATE `".$prefix."_authors` SET `adminop` = '1', `adminsuper` = '0',`admincomune` = '0' WHERE `".$prefix."_authors`.`aid` != 'admin' AND `".$prefix."_authors`.`adminsuper` != '1'";
111 $ret=aggiorna($sql,$dbi,'',$num);
112 $sql="UPDATE `".$prefix."_authors` SET `adminop` = '0', `adminsuper` = '0',`admincomune` = '1' WHERE `".$prefix."_authors`.`aid` = 'admin'";
113 $ret=aggiorna($sql,$dbi,'',$num);
114 $sql="UPDATE `".$prefix."_authors` SET `adminop` = '0', `adminsuper` = '1',`admincomune` = '0' WHERE `".$prefix."_authors`.`aid` = 'suser' or `".$prefix."_authors`.`adminsuper` = '1'";
115 $ret=aggiorna($sql,$dbi,'',$num);
116 echo "<br> La tabella dei permessi Ú stata aggiornata<br>";
117}else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_authors non richiede questo aggiornamento</span><br>";
118
119if(!controllo($prefix.'_config','versione',++$num))
120{
121 $sql="alter table `".$prefix."_config` change column `Versione` `versione` int(3)";
122 $ret=aggiorna($sql,$dbi,'',$num);
123} else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_config non richiede questo aggiornamento</span><br>";
124
125
126if(controllo($prefix.'_config','secret',++$num))
127{
128 $sql="alter table `".$prefix."_config` DROP `secret`";
129 $ret=aggiorna($sql,$dbi,'',$num);
130} else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_config non richiede questo aggiornamento</span><br>";
131if(controllo($prefix.'_config','aggiornamento',++$num))
132{
133 $sql="alter table `".$prefix."_config` DROP `aggiornamento`";
134 $ret=aggiorna($sql,$dbi,'',$num);
135} else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_config non richiede questo aggiornamento</span><br>";
136if(!controllo($prefix.'_ele_conf','votolista',++$num))
137{
138 $sql="ALTER TABLE `".$prefix."_ele_conf` ADD `votolista` enum('0', '1') NOT NULL DEFAULT '0' AFTER `supdisgiunto`";
139 $ret=aggiorna($sql,$dbi,'',$num);
140} else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_conf non richiede questo aggiornamento</span><br>";
141if(!controllo($prefix.'_ele_conf','inffisso',++$num))
142{
143 $sql="ALTER TABLE `".$prefix."_ele_conf` ADD `inffisso` enum('0', '1') NOT NULL DEFAULT '0' AFTER `votolista`";
144 $ret=aggiorna($sql,$dbi,'',$num);
145} else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_conf non richiede questo aggiornamento</span><br>";
146if(!controllo($prefix.'_ele_conf','supfisso',++$num))
147{
148 $sql="ALTER TABLE `".$prefix."_ele_conf` ADD `supfisso` enum('0', '1') NOT NULL DEFAULT '0' AFTER `inffisso`";
149 $ret=aggiorna($sql,$dbi,'',$num);
150} else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_conf non richiede questo aggiornamento</span><br>";
151if(!controllo($prefix.'_ele_conf','fascia_capoluogo',++$num))
152{
153 $sql="ALTER TABLE `".$prefix."_ele_conf` ADD `fascia_capoluogo` int(2) NOT NULL DEFAULT '0' AFTER `supfisso`";
154 $ret=aggiorna($sql,$dbi,'',$num);
155} else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_conf non richiede questo aggiornamento</span><br>";
156if(controllo($prefix.'_ele_sezioni','bianchi_lista',++$num))
157{
158 $sql="alter table `".$prefix."_ele_sezioni` DROP `bianchi_lista`";
159 $ret=aggiorna($sql,$dbi,'',$num);
160} else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_sezioni non richiede questo aggiornamento</span><br>";
161if(controllo($prefix.'_ele_sezioni','nulli_lista',++$num))
162{
163 $sql="alter table `".$prefix."_ele_sezioni` DROP `nulli_lista`";
164 $ret=aggiorna($sql,$dbi,'',$num);
165} else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_sezioni non richiede questo aggiornamento</span><br>";
166if(controllo($prefix.'_ele_voti_parziale','data',++$num))
167{
168 $sql="ALTER TABLE `".$prefix."_ele_voti_parziale` CHANGE `data` `data` DATE NOT NULL DEFAULT '1900-01-01'";
169 $ret=aggiorna($sql,$dbi,'',$num);
170 echo "<br>";
171}
172++$num;
173$sql="ALTER TABLE `".$prefix."_ele_come` CHANGE `title` `title` VARCHAR(150) NOT NULL DEFAULT ' ', CHANGE `preamble` `preamble` TEXT, CHANGE `content` `content` TEXT, CHANGE `editimage` `editimage` VARCHAR(100) NOT NULL DEFAULT ' '";
174$ret=aggiorna($sql,$dbi,'',$num);
175echo "<br>";
176if(!$ret) echo "Il tuo sistema non necessita di questo aggiornamento, questo avviso di errore va ignorato<br>";
177++$num;
178$sql="ALTER TABLE `".$prefix."_ele_link` CHANGE `title` `title` VARCHAR(150) NOT NULL DEFAULT ' ', CHANGE `preamble` `preamble` TEXT, CHANGE `content` `content` TEXT, CHANGE `editimage` `editimage` VARCHAR(100) NOT NULL DEFAULT ' '";
179$ret=aggiorna($sql,$dbi,'',$num);
180echo "<br>";
181if(!$ret) echo "Il tuo sistema non necessita di questo aggiornamento, questo avviso di errore va ignorato<br>";
182++$num;
183$sql="ALTER TABLE `".$prefix."_ele_servizi` CHANGE `title` `title` VARCHAR(150) NOT NULL DEFAULT ' ', CHANGE `preamble` `preamble` TEXT, CHANGE `content` `content` TEXT, CHANGE `editimage` `editimage` VARCHAR(100) NOT NULL DEFAULT ' '";
184$ret=aggiorna($sql,$dbi,'',$num);
185echo "<br>";
186if(!$ret) echo "Il tuo sistema non necessita di questo aggiornamento, questo avviso di errore va ignorato<br>";
187
188if(controllo($prefix.'_ele_rilaff','data',++$num))
189{
190 $sql="ALTER TABLE `".$prefix."_ele_rilaff` CHANGE `data` `data` DATE NOT NULL DEFAULT '1900-01-01'";
191 $ret=aggiorna($sql,$dbi,'',$num);
192 echo "<br>";
193}
194
195if(!controllo($prefix.'_ele_gruppo','num_circ',++$num))
196{
197 $sql="ALTER TABLE `".$prefix."_ele_gruppo` ADD `num_circ` INT(2) UNSIGNED NOT NULL AFTER `id_circ`";
198 $ret=aggiorna($sql,$dbi,'',$num);
199} else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_gruppo non richiede questo aggiornamento</span><br>";
200
201if(!controllo($prefix.'_ele_voti_gruppo','num_gruppo',++$num))
202{
203 $sql="ALTER TABLE `".$prefix."_ele_voti_gruppo` ADD `num_gruppo` INT(2) UNSIGNED NOT NULL AFTER `id_sez`";
204 $ret=aggiorna($sql,$dbi,'',$num);
205} else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_gruppo non richiede questo aggiornamento</span><br>";
206
207if(!controllo($prefix.'_ele_lista','num_gruppo',++$num))
208{
209 $sql="ALTER TABLE `".$prefix."_ele_lista` ADD `num_gruppo` INT(2) UNSIGNED NOT NULL AFTER `id_gruppo`";
210 $sql2="update `".$prefix."_ele_lista` as t1 set t1.num_gruppo=(select t2.num_gruppo from `".$prefix."_ele_gruppo` as t2 where t2.id_gruppo=t1.id_gruppo) where t1.num_gruppo=0 and (select t2.num_gruppo from `".$prefix."_ele_gruppo` as t2 where t2.id_gruppo=t1.id_gruppo) is not null";
211 $ret=aggiorna($sql,$dbi,$sql2,$num);
212}else{
213 $sql="update `".$prefix."_ele_lista` as t1 set t1.num_gruppo=(select t2.num_gruppo from `".$prefix."_ele_gruppo` as t2 where t2.id_gruppo=t1.id_gruppo) where t1.num_gruppo=0 and (select t2.num_gruppo from `".$prefix."_ele_gruppo` as t2 where t2.id_gruppo=t1.id_gruppo) is not null";
214 $ret=aggiorna($sql,$dbi,'',$num);
215echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_lista non richiede questo aggiornamento</span><br>";
216}
217if(!controllo($prefix.'_ele_lista','num_circ',++$num))
218{
219 $sql="ALTER TABLE `".$prefix."_ele_lista` ADD `num_circ` INT(2) UNSIGNED NOT NULL AFTER `id_circ`";
220 $ret=aggiorna($sql,$dbi,'',$num);
221}else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_lista non richiede questo aggiornamento</span><br>";
222
223if(!controllo($prefix.'_ele_operatori','id_circ',++$num))
224{
225 $sql="ALTER TABLE `".$prefix."_ele_operatori` ADD `id_circ` INT(11) NOT NULL DEFAULT 0 AFTER `aid`";
226 $ret=aggiorna($sql,$dbi,'',$num);
227}else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_operatori non richiede questo aggiornamento</span><br>";
228
229if(!controllo($prefix.'_ele_operatori','id_sez',++$num))
230{
231 $sql="ALTER TABLE `".$prefix."_ele_operatori` ADD `id_sez` INT(11) NOT NULL DEFAULT 0 AFTER `id_circ`";
232 $ret=aggiorna($sql,$dbi,'',$num);
233}else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_operatori non richiede questo aggiornamento</span><br>";
234
235if(!controllo($prefix.'_ele_voti_lista','num_lista',++$num))
236{
237 $sql="ALTER TABLE `".$prefix."_ele_voti_lista` ADD `num_lista` INT(2) UNSIGNED NOT NULL AFTER `id_sez`";
238 $ret=aggiorna($sql,$dbi,'',$num);
239}else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_voti_lista non richiede questo aggiornamento</span><br>";
240
241if(!controllo($prefix.'_ele_voti_ref','num_gruppo',++$num))
242{
243 $sql="ALTER TABLE `".$prefix."_ele_voti_ref` ADD `num_gruppo` INT(2) UNSIGNED NOT NULL AFTER `id_sez`";
244 $ret=aggiorna($sql,$dbi,'',$num);
245}else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_voti_ref non richiede questo aggiornamento</span><br>";
246
247if(!controllo($prefix.'_ele_voti_candidati','num_cand',++$num))
248{
249 $sql="ALTER TABLE `".$prefix."_ele_voti_candidati` ADD `num_cand` INT(2) UNSIGNED NOT NULL AFTER `id_sez`";
250 $ret=aggiorna($sql,$dbi,'',$num);
251}else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_voti_candidati non richiede questo aggiornamento</span><br>";
252
253if(!controllo($prefix.'_ele_candidati','num_lista',++$num))
254{
255 $sql="ALTER TABLE `".$prefix."_ele_candidati` ADD `num_lista` INT(2) UNSIGNED NOT NULL AFTER `id_lista`";
256 $ret=aggiorna($sql,$dbi,'',$num);
257}else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_candidati non richiede questo aggiornamento</span><br>";
258
259if(!controllo($prefix.'_ele_sezioni','colore',++$num))
260{
261 $sql="ALTER TABLE `".$prefix."_ele_sezioni` ADD `colore` VARCHAR(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '#FAFAD2' AFTER `solo_lista`";
262 $ret=aggiorna($sql,$dbi,'',$num);
263}else{
264 $sql="ALTER TABLE `".$prefix."_ele_sezioni` CHANGE `colore` `colore` VARCHAR(50) DEFAULT '#FAFAD2'";
265 $ret=aggiorna($sql,$dbi,'',$num);
266} echo "<br>";
267
268echo "<br>Aggiornamento per nuovo sistema dei controlli di congruità";
269
270if(!controllo($prefix.'_ele_controlli','id_cons',++$num))
271{
272 $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";
273 echo "<br>".$num.") Creazione tabella dei controlli: ";
274 $ret=aggiorna($sql,$dbi,'',$num);
275}else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_controlli non richiede questo aggiornamento</span><br>";
276
277echo "<br>".++$num.") Eliminazione della vecchia tabella dei controlli: ";
278if(controllo($prefix.'_ele_controllisez','',$num))
279{
280$sql="DROP TABLE if exists `".$prefix."_ele_controllisez`";
281$ret=aggiorna($sql,$dbi,'',$num);
282} else echo "<br><span style=\"color: green;\">- Tabella non presente</span><br>";
283
284echo "<br>".++$num.") Aggiornamento tabella ".$prefix."_ele_voti_gruppo";
285$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;";
286$ret=aggiorna($sql,$dbi,'',$num);
287
288echo "<br><br>".++$num.") Aggiornamento dei valori di default: ".$prefix."_ele_voti_lista";
289$sql="ALTER TABLE `".$prefix."_ele_voti_lista` CHANGE `num_lista` `num_lista` INT(2) UNSIGNED NULL DEFAULT '0';";
290$ret=aggiorna($sql,$dbi,'',$num);
291
292echo "<br><br>".++$num.") Aggiornamento tabella ".$prefix."_ele_voti_lista";
293$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;";
294$ret=aggiorna($sql,$dbi,'',$num);
295##############################################
296
297echo "<br><br>".++$num.") Aggiornamento dei valori di default: ".$prefix."_ele_gruppo";
298$sql="ALTER TABLE `".$prefix."_ele_gruppo` CHANGE `num_circ` `num_circ` INT(2) UNSIGNED NOT NULL DEFAULT '1';";
299$ret=aggiorna($sql,$dbi,'',$num);
300
301$sql="ALTER TABLE `".$prefix."_authors` CHANGE `adminsuper` `adminsuper` TINYINT(2) NOT NULL DEFAULT '0';";
302$ret=aggiorna($sql,$dbi,'',$num);
303
304$sql="ALTER TABLE `".$prefix."_ele_lista` CHANGE `num_gruppo` `num_gruppo` INT(2) UNSIGNED NOT NULL DEFAULT '0';";
305$ret=aggiorna($sql,$dbi,'',$num);
306
307$sql="ALTER TABLE `".$prefix."_ele_lista` CHANGE `num_circ` `num_circ` INT(2) UNSIGNED NOT NULL DEFAULT '1';";
308$ret=aggiorna($sql,$dbi,'',$num);
309
310echo "<br><br>".++$num.") Aggiornamento della tabella _ele_conf per la nuova gestione della L.R. Sicilia";
311$sql="UPDATE `".$prefix."_ele_conf` SET `inffisso` = '1' WHERE `id_conf` = 4;";
312$ret=aggiorna($sql,$dbi,'',$num);
313
314echo "<br><br>".++$num.") Ricostruzione della tabella ".$prefix."_ele_fasce`";
315$sql="DROP TABLE `".$prefix."_ele_fasce`";
316$ret=aggiorna($sql,$dbi,'',$num);
317
318$sql="CREATE TABLE `".$prefix."_ele_fasce` (
319 `id_fascia` int(2) NOT NULL,
320 `abitanti` int(11) NOT NULL,
321 `seggi` int(4) NOT NULL,
322 `id_conf` int(11) DEFAULT 1
323) ENGINE=MyISAM DEFAULT CHARSET 'utf8';";
324
325
326$sql2="INSERT INTO `".$prefix."_ele_fasce` (`id_fascia`, `abitanti`, `seggi`, `id_conf`) VALUES
327(1, 3000, 12, 1),
328(2, 10000, 16, 1),
329(3, 15000, 20, 1),
330(4, 30000, 20, 1),
331(5, 100000, 30, 1),
332(6, 250000, 40, 1),
333(7, 500000, 46, 1),
334(8, 1000000, 50, 1),
335(9, 100000000, 60, 1),
336(1, 3000, 12, 2),
337(2, 10000, 16, 2),
338(3, 15000, 20, 2),
339(4, 30000, 20, 2),
340(5, 100000, 30, 2),
341(6, 250000, 40, 2),
342(7, 500000, 46, 2),
343(8, 1000000, 50, 2),
344(9, 100000000, 60, 2),
345(1, 3000, 9, 3),
346(2, 5000, 9, 3),
347(4, 15000, 16, 3),
348(3, 10000, 12, 3),
349(5, 30000, 16, 3),
350(6, 100000, 24, 3),
351(7, 250000, 32, 3),
352(8, 500000, 36, 3),
353(9, 1000000, 40, 3),
354(10, 100000000, 48, 3),
355(1, 3000, 6, 4),
356(2, 5000, 7, 4),
357(3, 10000, 12, 4),
358(4, 15000, 16, 4),
359(5, 30000, 16, 4),
360(6, 100000, 24, 4),
361(7, 250000, 32, 4),
362(8, 500000, 36, 4),
363(9, 1000000, 40, 4),
364(10, 100000000, 48, 4),
365(1, 3000, 9, 5),
366(2, 5000, 9, 5),
367(3, 10000, 12, 5),
368(4, 15000, 16, 5),
369(5, 30000, 16, 5),
370(6, 100000, 24, 5),
371(7, 250000, 32, 5),
372(8, 500000, 36, 5),
373(9, 1000000, 40, 5),
374(10, 100000000, 48, 5),
375(1, 3000, 6, 6),
376(2, 5000, 7, 6),
377(3, 10000, 10, 6),
378(4, 15000, 16, 6),
379(5, 30000, 16, 6),
380(6, 100000, 24, 6),
381(7, 250000, 32, 6),
382(8, 500000, 36, 6),
383(9, 1000000, 40, 6),
384(10, 100000000, 48, 6),
385(1, 3000, 10, 7),
386(2, 10000, 12, 7),
387(3, 15000, 16, 7),
388(4, 30000, 16, 7),
389(5, 100000, 24, 7),
390(6, 250000, 32, 7),
391(7, 500000, 36, 7),
392(8, 1000000, 40, 7),
393(9, 100000000, 48, 7);";
394$ret=aggiorna($sql,$dbi,$sql2,$num);
395#if(!$ret) echo "<br>".$num++.") Fallito: $sql"; else echo "<br>".$num++.") Aggiornato<br>";
396
397$sql="ALTER TABLE `".$prefix."_ele_fasce`
398 ADD KEY `id_fascia` (`id_fascia`);";
399$ret=aggiorna($sql,$dbi,'',$num);
400#if(!$ret) echo "<br>".$num++.") Fallito: $sql"; else echo "<br>".$num++.") Aggiornato<br>";
401
402
403
404#if(!$ret)
405# echo "<br>".$num++.") Fallito: $sql";
406#elseif ($ret==1)
407# echo "<br>".$num++.") Index eliminato";
408#else
409# echo "<br>".$num++.") Index aggiunto";
410#$sql="ALTER TABLE `".$prefix."_ele_voti_lista` DROP INDEX if exists `id_cons`";
411echo "<br><br>".++$num.") Ricostruzione e aggioramento indici";
412$tab=$prefix."_ele_voti_ref";
413$ind="id_cons";
414# $sql="ALTER TABLE `".$prefix."_ele_voti_ref` DROP INDEX `id_cons`; ";
415$sql2="ALTER TABLE `".$prefix."_ele_voti_ref` ADD INDEX `id_cons` (`id_cons`, `id_gruppo`) USING BTREE";
416$ret=aggiorna_index($tab, $ind,$dbi,$sql2,$num);
417
418$tab=$prefix."_ele_sezioni";
419$ind="id_cons";
420$sql2="ALTER TABLE `".$prefix."_ele_sezioni` ADD UNIQUE `id_cons` (`id_cons`, `num_sez`) USING BTREE";
421$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
422
423$tab=$prefix."_ele_voti_lista";
424$ind="id_cons";
425$sql2="ALTER TABLE `".$prefix."_ele_voti_lista` ADD INDEX `id_cons` (`id_cons`, `id_sez`, `id_lista`) USING BTREE";
426$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
427
428$tab=$prefix."_ele_voti_gruppo";
429$ind="id_cons";
430#$sql="ALTER TABLE `".$prefix."_ele_voti_gruppo` DROP INDEX if exists `id_cons`";
431$sql2="ALTER TABLE `".$prefix."_ele_voti_gruppo` ADD INDEX `id_cons` (`id_cons`, `id_sez`, `id_gruppo`) USING BTREE";
432$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
433
434$tab=$prefix."_ele_voti_candidati";
435$ind="id_cons";
436#$sql="ALTER TABLE `".$prefix."_ele_voti_candidati` DROP INDEX if exists `id_cons`";
437$sql2="ALTER TABLE `".$prefix."_ele_voti_candidati` ADD INDEX `id_cons` (`id_cons`, `id_sez`, `id_cand`) USING BTREE";
438$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
439
440$tab=$prefix."_ele_lista";
441$ind="id_cons";
442#$sql="ALTER TABLE `".$prefix."_ele_lista` DROP INDEX if exists `id_cons`";
443$sql2="ALTER TABLE `".$prefix."_ele_lista` ADD INDEX `id_cons` (`id_cons`, `id_gruppo`) USING BTREE";
444$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
445
446$tab=$prefix."_ele_lista";
447$ind="PRIMARY";
448#$sql="ALTER TABLE `".$prefix."_ele_lista` DROP INDEX if exists `PRIMARY`";
449$sql2="ADD PRIMARY KEY (`id_lista`) USING BTREE";
450$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
451
452$tab=$prefix."_ele_gruppo";
453$ind="id_cons";
454#$sql="ALTER TABLE `".$prefix."_ele_gruppo` DROP INDEX if exists `id_cons`";
455$sql2="ALTER TABLE `".$prefix."_ele_gruppo` ADD INDEX `id_cons` (`id_cons`, `id_circ`) USING BTREE";
456$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
457
458$tab=$prefix."_ele_gruppo";
459$ind="PRIMARY";
460#$sql="ALTER TABLE `".$prefix."_ele_gruppo` DROP INDEX if exists `PRIMARY`";
461$sql2="ADD PRIMARY KEY (`id_gruppo`) USING BTREE";
462$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
463
464$tab=$prefix."_ele_candidati";
465$ind="id_cons";
466#$sql="ALTER TABLE `".$prefix."_ele_candidati` DROP INDEX if exists `id_cons`";
467$sql2="ALTER TABLE `".$prefix."_ele_candidati` ADD INDEX `id_cons` (`id_cons`, `id_lista`) USING BTREE";
468$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
469
470$tab=$prefix."_ele_candidati";
471$ind="PRIMARY";
472#$sql="ALTER TABLE `".$prefix."_ele_candidati` DROP INDEX if exists `PRIMARY`";
473$sql2="ADD PRIMARY KEY (`id_cand`) USING BTREE";
474$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
475
476$tab=$prefix."_ele_consultazione";
477$ind="descrizione";
478#$sql="ALTER TABLE `".$prefix."_ele_consultazione` DROP INDEX if exists `descrizione`";
479$sql2="ALTER TABLE `".$prefix."_ele_consultazione` ADD UNIQUE `descrizione` (`descrizione`(100))";
480$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
481
482echo "<br><br>".++$num.") Modifica Charset del database";
483if($newcs=='utf8') {
484 $cset='utf8';
485 $ccollate='utf8_general_ci';
486 $preset='latin1';
487}else{
488 $cset='latin1';
489 $ccollate='latin1_swedish_ci';
490 $preset='utf8';
491}
492#$sql="ALTER DATABASE $dbname CHARACTER SET '$cset' COLLATE '$ccollate'";
493#$res = $dbi->prepare("$sql");
494#$res->execute();
495
496$sql="SELECT table_name,column_name,column_default,column_type,is_nullable FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '$dbname' and (character_set_name='$preset' or collation_name like '$preset%')";
497$res = $dbi->prepare("$sql");
498$res->execute();
499$tab='';
500$agg=$res->rowCount();
501
502while(list($nometab,$campo,$def,$tipo,$nul)=$res->fetch(PDO::FETCH_NUM)) {
503 if($tab!=$nometab){
504 $sql="alter table $nometab DEFAULT CHARSET=$cset COLLATE $ccollate";
505 try{
506 $res2 = $dbi->prepare("$sql");
507 $res2->execute();
508 }
509 catch(PDOException $e)
510 {
511 die( $sql . "<br>" . $e->getMessage());
512 }
513 $tab=$nometab; echo "<br><span style=\"color: green;\">- Tabella: $nometab</span>";
514 }
515 if($def!='') $default="DEFAULT '$def'"; else $default='';
516 if($nul=='NO') $nullable='NOT NULL'; else $nullable='NULL';
517 $sql="ALTER TABLE $nometab CHANGE $campo $campo $tipo CHARACTER SET '$cset' COLLATE '$ccollate' $nullable $default;";
518 try{
519 $res2 = $dbi->prepare("$sql");
520 $res2->execute();
521 }
522 catch(PDOException $e)
523 {
524 $default="DEFAULT $def";
525 $sql="ALTER TABLE $nometab CHANGE $campo $campo $tipo CHARACTER SET '$cset' COLLATE '$ccollate' $nullable $default;";
526 try{
527 $res2 = $dbi->prepare("$sql");
528 $res2->execute();
529 }
530 catch(PDOException $e)
531 {
532 die( $sql . "<br>" . $e->getMessage());
533 }
534 }
535 echo "<br><span style=\"color: green;\">-- $campo</span>";
536}
537 $sql="SELECT table_name FROM INFORMATION_SCHEMA.tables WHERE TABLE_SCHEMA = '$dbname' and table_collation like '$preset%'";
538 $res = $dbi->prepare("$sql");
539 $res->execute();
540$tab='';
541if(!$agg) $agg=$res->rowCount();
542while(list($nometab)=$res->fetch(PDO::FETCH_NUM)) {
543 $sql="alter table $nometab DEFAULT CHARSET=$cset COLLATE $ccollate";
544 try{
545 $res2 = $dbi->prepare("$sql");
546 $res2->execute();
547 }
548 catch(PDOException $e)
549 {
550 die( $sql . "<br>" . $e->getMessage());
551 }
552 echo "<br><span style=\"color: green;\">- Tabella: $nometab</span>";
553}
554if(!$agg) echo "<br><span style=\"color: green;\">- Nessuna tabella da aggiornare</span>";
555echo "<br><br>";
556?>
Note: See TracBrowser for help on using the repository browser.