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

Last change on this file since 396 was 396, checked in by roby, 11 months ago
  • ADMIN

-- Aggiunto un controllo della struttura del db in aggiornadb

File size: 23.1 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>";
124if(controllo($prefix.'_config','secret',++$num))
125{
126 $sql="alter table `".$prefix."_config` DROP `secret`";
127 $ret=aggiorna($sql,$dbi,'',$num);
128} else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_config non richiede questo aggiornamento</span><br>";
129if(controllo($prefix.'_config','aggiornamento',++$num))
130{
131 $sql="alter table `".$prefix."_config` DROP `aggiornamento`";
132 $ret=aggiorna($sql,$dbi,'',$num);
133} else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_config non richiede questo aggiornamento</span><br>";
134if(!controllo($prefix.'_ele_conf','votolista',++$num))
135{
136 $sql="ALTER TABLE `".$prefix."_ele_conf` ADD `votolista` enum('0', '1') NOT NULL DEFAULT '0' AFTER `supdisgiunto`";
137 $ret=aggiorna($sql,$dbi,'',$num);
138} else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_conf non richiede questo aggiornamento</span><br>";
139if(!controllo($prefix.'_ele_conf','inffisso',++$num))
140{
141 $sql="ALTER TABLE `".$prefix."_ele_conf` ADD `inffisso` enum('0', '1') NOT NULL DEFAULT '0' AFTER `votolista`";
142 $ret=aggiorna($sql,$dbi,'',$num);
143} else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_conf non richiede questo aggiornamento</span><br>";
144if(!controllo($prefix.'_ele_conf','supfisso',++$num))
145{
146 $sql="ALTER TABLE `".$prefix."_ele_conf` ADD `supfisso` enum('0', '1') NOT NULL DEFAULT '0' AFTER `inffisso`";
147 $ret=aggiorna($sql,$dbi,'',$num);
148} else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_conf non richiede questo aggiornamento</span><br>";
149if(!controllo($prefix.'_ele_conf','fascia_capoluogo',++$num))
150{
151 $sql="ALTER TABLE `".$prefix."_ele_conf` ADD `fascia_capoluogo` int(2) NOT NULL DEFAULT '0' AFTER `supfisso`";
152 $ret=aggiorna($sql,$dbi,'',$num);
153} else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_conf non richiede questo aggiornamento</span><br>";
154if(controllo($prefix.'_ele_sezioni','bianchi_lista',++$num))
155{
156 $sql="alter table `".$prefix."_ele_sezioni` DROP `bianchi_lista`";
157 $ret=aggiorna($sql,$dbi,'',$num);
158} else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_sezioni non richiede questo aggiornamento</span><br>";
159if(controllo($prefix.'_ele_sezioni','nulli_lista',++$num))
160{
161 $sql="alter table `".$prefix."_ele_sezioni` DROP `nulli_lista`";
162 $ret=aggiorna($sql,$dbi,'',$num);
163} else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_sezioni non richiede questo aggiornamento</span><br>";
164if(controllo($prefix.'_ele_voti_parziale','data',++$num))
165{
166 $sql="ALTER TABLE `".$prefix."_ele_voti_parziale` CHANGE `data` `data` DATE NOT NULL DEFAULT '1900-01-01'";
167 $ret=aggiorna($sql,$dbi,'',$num);
168 echo "<br>";
169}
170++$num;
171$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 ' '";
172$ret=aggiorna($sql,$dbi,'',$num);
173echo "<br>";
174if(!$ret) echo "Il tuo sistema non necessita di questo aggiornamento, questo avviso di errore va ignorato<br>";
175++$num;
176$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 ' '";
177$ret=aggiorna($sql,$dbi,'',$num);
178echo "<br>";
179if(!$ret) echo "Il tuo sistema non necessita di questo aggiornamento, questo avviso di errore va ignorato<br>";
180++$num;
181$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 ' '";
182$ret=aggiorna($sql,$dbi,'',$num);
183echo "<br>";
184if(!$ret) echo "Il tuo sistema non necessita di questo aggiornamento, questo avviso di errore va ignorato<br>";
185
186if(controllo($prefix.'_ele_rilaff','data',++$num))
187{
188 $sql="ALTER TABLE `".$prefix."_ele_rilaff` CHANGE `data` `data` DATE NOT NULL DEFAULT '1900-01-01'";
189 $ret=aggiorna($sql,$dbi,'',$num);
190 echo "<br>";
191}
192
193if(!controllo($prefix.'_ele_gruppo','num_circ',++$num))
194{
195 $sql="ALTER TABLE `".$prefix."_ele_gruppo` ADD `num_circ` INT(2) UNSIGNED NOT NULL AFTER `id_circ`";
196 $ret=aggiorna($sql,$dbi,'',$num);
197} else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_gruppo non richiede questo aggiornamento</span><br>";
198
199if(!controllo($prefix.'_ele_voti_gruppo','num_gruppo',++$num))
200{
201 $sql="ALTER TABLE `".$prefix."_ele_voti_gruppo` ADD `num_gruppo` INT(2) UNSIGNED NOT NULL AFTER `id_sez`";
202 $ret=aggiorna($sql,$dbi,'',$num);
203} else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_gruppo non richiede questo aggiornamento</span><br>";
204
205if(!controllo($prefix.'_ele_lista','num_gruppo',++$num))
206{
207 $sql="ALTER TABLE `".$prefix."_ele_lista` ADD `num_gruppo` INT(2) UNSIGNED NOT NULL AFTER `id_gruppo`";
208 $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";
209 $ret=aggiorna($sql,$dbi,$sql2,$num);
210}else{
211 $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";
212 $ret=aggiorna($sql,$dbi,'',$num);
213echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_lista non richiede questo aggiornamento</span><br>";
214}
215if(!controllo($prefix.'_ele_lista','num_circ',++$num))
216{
217 $sql="ALTER TABLE `".$prefix."_ele_lista` ADD `num_circ` INT(2) UNSIGNED NOT NULL AFTER `id_circ`";
218 $ret=aggiorna($sql,$dbi,'',$num);
219}else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_lista non richiede questo aggiornamento</span><br>";
220
221if(!controllo($prefix.'_ele_operatori','id_circ',++$num))
222{
223 $sql="ALTER TABLE `".$prefix."_ele_operatori` ADD `id_circ` INT(11) NOT NULL DEFAULT 0 AFTER `aid`";
224 $ret=aggiorna($sql,$dbi,'',$num);
225}else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_operatori non richiede questo aggiornamento</span><br>";
226
227if(!controllo($prefix.'_ele_operatori','id_sez',++$num))
228{
229 $sql="ALTER TABLE `".$prefix."_ele_operatori` ADD `id_sez` INT(11) NOT NULL DEFAULT 0 AFTER `id_circ`";
230 $ret=aggiorna($sql,$dbi,'',$num);
231}else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_operatori non richiede questo aggiornamento</span><br>";
232
233if(!controllo($prefix.'_ele_voti_lista','num_lista',++$num))
234{
235 $sql="ALTER TABLE `".$prefix."_ele_voti_lista` ADD `num_lista` INT(2) UNSIGNED NOT NULL AFTER `id_sez`";
236 $ret=aggiorna($sql,$dbi,'',$num);
237}else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_voti_lista non richiede questo aggiornamento</span><br>";
238
239if(!controllo($prefix.'_ele_voti_ref','num_gruppo',++$num))
240{
241 $sql="ALTER TABLE `".$prefix."_ele_voti_ref` ADD `num_gruppo` INT(2) UNSIGNED NOT NULL AFTER `id_sez`";
242 $ret=aggiorna($sql,$dbi,'',$num);
243}else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_voti_ref non richiede questo aggiornamento</span><br>";
244
245if(controllo($prefix.'_ele_candidati','Sesso',++$num))
246{
247 $sql="ALTER TABLE `".$prefix."_ele_candidati` DROP `Sesso`";
248 $ret=aggiorna($sql,$dbi,'',$num);
249} else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_candidati non richiede questo aggiornamento</span><br>";
250if(!controllo($prefix.'_ele_voti_candidati','num_cand',++$num))
251{
252 $sql="ALTER TABLE `".$prefix."_ele_voti_candidati` ADD `num_cand` INT(2) UNSIGNED NOT NULL AFTER `id_sez`";
253 $ret=aggiorna($sql,$dbi,'',$num);
254}else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_voti_candidati non richiede questo aggiornamento</span><br>";
255
256if(!controllo($prefix.'_ele_candidati','num_lista',++$num))
257{
258 $sql="ALTER TABLE `".$prefix."_ele_candidati` ADD `num_lista` INT(2) UNSIGNED NOT NULL AFTER `id_lista`";
259 $ret=aggiorna($sql,$dbi,'',$num);
260}else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_candidati non richiede questo aggiornamento</span><br>";
261
262if(!controllo($prefix.'_ele_sezioni','colore',++$num))
263{
264 $sql="ALTER TABLE `".$prefix."_ele_sezioni` ADD `colore` VARCHAR(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '#FAFAD2' AFTER `solo_lista`";
265 $ret=aggiorna($sql,$dbi,'',$num);
266}else{
267 $sql="ALTER TABLE `".$prefix."_ele_sezioni` CHANGE `colore` `colore` VARCHAR(50) DEFAULT '#FAFAD2'";
268 $ret=aggiorna($sql,$dbi,'',$num);
269} echo "<br>";
270
271echo "<br>Aggiornamento per nuovo sistema dei controlli di congruità";
272
273if(!controllo($prefix.'_ele_controlli','id_cons',++$num))
274{
275 $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";
276 echo "<br>".$num.") Creazione tabella dei controlli: ";
277 $ret=aggiorna($sql,$dbi,'',$num);
278}else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_controlli non richiede questo aggiornamento</span><br>";
279
280echo "<br>".++$num.") Eliminazione della vecchia tabella dei controlli: ";
281if(controllo($prefix.'_ele_controllisez','',$num))
282{
283$sql="DROP TABLE if exists `".$prefix."_ele_controllisez`";
284$ret=aggiorna($sql,$dbi,'',$num);
285} else echo "<br><span style=\"color: green;\">- Tabella non presente</span><br>";
286
287echo "<br>".++$num.") Aggiornamento tabella ".$prefix."_ele_voti_gruppo";
288$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;";
289$ret=aggiorna($sql,$dbi,'',$num);
290
291echo "<br><br>".++$num.") Aggiornamento dei valori di default: ".$prefix."_ele_voti_lista";
292$sql="ALTER TABLE `".$prefix."_ele_voti_lista` CHANGE `num_lista` `num_lista` INT(2) UNSIGNED NULL DEFAULT '0';";
293$ret=aggiorna($sql,$dbi,'',$num);
294
295echo "<br><br>".++$num.") Aggiornamento tabella ".$prefix."_ele_voti_lista";
296$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;";
297$ret=aggiorna($sql,$dbi,'',$num);
298##############################################
299
300echo "<br><br>".++$num.") Aggiornamento dei valori di default: ".$prefix."_ele_gruppo";
301$sql="ALTER TABLE `".$prefix."_ele_gruppo` CHANGE `num_circ` `num_circ` INT(2) UNSIGNED NOT NULL DEFAULT '1';";
302$ret=aggiorna($sql,$dbi,'',$num);
303
304$sql="ALTER TABLE `".$prefix."_authors` CHANGE `adminsuper` `adminsuper` TINYINT(2) NOT NULL DEFAULT '0';";
305$ret=aggiorna($sql,$dbi,'',$num);
306
307$sql="ALTER TABLE `".$prefix."_ele_lista` CHANGE `num_gruppo` `num_gruppo` INT(2) UNSIGNED NOT NULL DEFAULT '0';";
308$ret=aggiorna($sql,$dbi,'',$num);
309
310$sql="ALTER TABLE `".$prefix."_ele_lista` CHANGE `num_circ` `num_circ` INT(2) UNSIGNED NOT NULL DEFAULT '1';";
311$ret=aggiorna($sql,$dbi,'',$num);
312
313echo "<br><br>".++$num.") Aggiornamento della tabella _ele_conf per la nuova gestione della L.R. Sicilia";
314$sql="UPDATE `".$prefix."_ele_conf` SET `inffisso` = '1' WHERE `id_conf` = 4;";
315$ret=aggiorna($sql,$dbi,'',$num);
316
317echo "<br><br>".++$num.") Ricostruzione della tabella ".$prefix."_ele_fasce`";
318$sql="DROP TABLE `".$prefix."_ele_fasce`";
319$ret=aggiorna($sql,$dbi,'',$num);
320
321$sql="CREATE TABLE `".$prefix."_ele_fasce` (
322 `id_fascia` int(2) NOT NULL,
323 `abitanti` int(11) NOT NULL,
324 `seggi` int(4) NOT NULL,
325 `id_conf` int(11) DEFAULT 1
326) ENGINE=MyISAM DEFAULT CHARSET 'utf8';";
327
328
329$sql2="INSERT INTO `".$prefix."_ele_fasce` (`id_fascia`, `abitanti`, `seggi`, `id_conf`) VALUES
330(1, 3000, 12, 1),
331(2, 10000, 16, 1),
332(3, 15000, 20, 1),
333(4, 30000, 20, 1),
334(5, 100000, 30, 1),
335(6, 250000, 40, 1),
336(7, 500000, 46, 1),
337(8, 1000000, 50, 1),
338(9, 100000000, 60, 1),
339(1, 3000, 12, 2),
340(2, 10000, 16, 2),
341(3, 15000, 20, 2),
342(4, 30000, 20, 2),
343(5, 100000, 30, 2),
344(6, 250000, 40, 2),
345(7, 500000, 46, 2),
346(8, 1000000, 50, 2),
347(9, 100000000, 60, 2),
348(1, 3000, 9, 3),
349(2, 5000, 9, 3),
350(4, 15000, 16, 3),
351(3, 10000, 12, 3),
352(5, 30000, 16, 3),
353(6, 100000, 24, 3),
354(7, 250000, 32, 3),
355(8, 500000, 36, 3),
356(9, 1000000, 40, 3),
357(10, 100000000, 48, 3),
358(1, 3000, 6, 4),
359(2, 5000, 7, 4),
360(3, 10000, 12, 4),
361(4, 15000, 16, 4),
362(5, 30000, 16, 4),
363(6, 100000, 24, 4),
364(7, 250000, 32, 4),
365(8, 500000, 36, 4),
366(9, 1000000, 40, 4),
367(10, 100000000, 48, 4),
368(1, 3000, 9, 5),
369(2, 5000, 9, 5),
370(3, 10000, 12, 5),
371(4, 15000, 16, 5),
372(5, 30000, 16, 5),
373(6, 100000, 24, 5),
374(7, 250000, 32, 5),
375(8, 500000, 36, 5),
376(9, 1000000, 40, 5),
377(10, 100000000, 48, 5),
378(1, 3000, 6, 6),
379(2, 5000, 7, 6),
380(3, 10000, 10, 6),
381(4, 15000, 16, 6),
382(5, 30000, 16, 6),
383(6, 100000, 24, 6),
384(7, 250000, 32, 6),
385(8, 500000, 36, 6),
386(9, 1000000, 40, 6),
387(10, 100000000, 48, 6),
388(1, 3000, 10, 7),
389(2, 10000, 12, 7),
390(3, 15000, 16, 7),
391(4, 30000, 16, 7),
392(5, 100000, 24, 7),
393(6, 250000, 32, 7),
394(7, 500000, 36, 7),
395(8, 1000000, 40, 7),
396(9, 100000000, 48, 7);";
397$ret=aggiorna($sql,$dbi,$sql2,$num);
398#if(!$ret) echo "<br>".$num++.") Fallito: $sql"; else echo "<br>".$num++.") Aggiornato<br>";
399
400$sql="ALTER TABLE `".$prefix."_ele_fasce`
401 ADD KEY `id_fascia` (`id_fascia`);";
402$ret=aggiorna($sql,$dbi,'',$num);
403#if(!$ret) echo "<br>".$num++.") Fallito: $sql"; else echo "<br>".$num++.") Aggiornato<br>";
404
405
406
407#if(!$ret)
408# echo "<br>".$num++.") Fallito: $sql";
409#elseif ($ret==1)
410# echo "<br>".$num++.") Index eliminato";
411#else
412# echo "<br>".$num++.") Index aggiunto";
413#$sql="ALTER TABLE `".$prefix."_ele_voti_lista` DROP INDEX if exists `id_cons`";
414echo "<br><br>".++$num.") Ricostruzione e aggioramento indici";
415$tab=$prefix."_ele_voti_ref";
416$ind="id_cons";
417# $sql="ALTER TABLE `".$prefix."_ele_voti_ref` DROP INDEX `id_cons`; ";
418$sql2="ALTER TABLE `".$prefix."_ele_voti_ref` ADD INDEX `id_cons` (`id_cons`, `id_gruppo`) USING BTREE";
419$ret=aggiorna_index($tab, $ind,$dbi,$sql2,$num);
420
421$tab=$prefix."_ele_sezioni";
422$ind="id_cons";
423$sql2="ALTER TABLE `".$prefix."_ele_sezioni` ADD UNIQUE `id_cons` (`id_cons`, `num_sez`) USING BTREE";
424$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
425
426$tab=$prefix."_ele_voti_lista";
427$ind="id_cons";
428$sql2="ALTER TABLE `".$prefix."_ele_voti_lista` ADD INDEX `id_cons` (`id_cons`, `id_sez`, `id_lista`) USING BTREE";
429$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
430
431$tab=$prefix."_ele_voti_gruppo";
432$ind="id_cons";
433#$sql="ALTER TABLE `".$prefix."_ele_voti_gruppo` DROP INDEX if exists `id_cons`";
434$sql2="ALTER TABLE `".$prefix."_ele_voti_gruppo` ADD INDEX `id_cons` (`id_cons`, `id_sez`, `id_gruppo`) USING BTREE";
435$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
436
437$tab=$prefix."_ele_voti_candidati";
438$ind="id_cons";
439#$sql="ALTER TABLE `".$prefix."_ele_voti_candidati` DROP INDEX if exists `id_cons`";
440$sql2="ALTER TABLE `".$prefix."_ele_voti_candidati` ADD INDEX `id_cons` (`id_cons`, `id_sez`, `id_cand`) USING BTREE";
441$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
442
443$tab=$prefix."_ele_lista";
444$ind="id_cons";
445#$sql="ALTER TABLE `".$prefix."_ele_lista` DROP INDEX if exists `id_cons`";
446$sql2="ALTER TABLE `".$prefix."_ele_lista` ADD INDEX `id_cons` (`id_cons`, `id_gruppo`) USING BTREE";
447$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
448
449$tab=$prefix."_ele_lista";
450$ind="PRIMARY";
451#$sql="ALTER TABLE `".$prefix."_ele_lista` DROP INDEX if exists `PRIMARY`";
452$sql2="ADD PRIMARY KEY (`id_lista`) USING BTREE";
453$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
454
455$tab=$prefix."_ele_gruppo";
456$ind="id_cons";
457#$sql="ALTER TABLE `".$prefix."_ele_gruppo` DROP INDEX if exists `id_cons`";
458$sql2="ALTER TABLE `".$prefix."_ele_gruppo` ADD INDEX `id_cons` (`id_cons`, `id_circ`) USING BTREE";
459$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
460
461$tab=$prefix."_ele_gruppo";
462$ind="PRIMARY";
463#$sql="ALTER TABLE `".$prefix."_ele_gruppo` DROP INDEX if exists `PRIMARY`";
464$sql2="ADD PRIMARY KEY (`id_gruppo`) USING BTREE";
465$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
466
467$tab=$prefix."_ele_candidati";
468$ind="id_cons";
469#$sql="ALTER TABLE `".$prefix."_ele_candidati` DROP INDEX if exists `id_cons`";
470$sql2="ALTER TABLE `".$prefix."_ele_candidati` ADD INDEX `id_cons` (`id_cons`, `id_lista`) USING BTREE";
471$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
472
473$tab=$prefix."_ele_candidati";
474$ind="PRIMARY";
475#$sql="ALTER TABLE `".$prefix."_ele_candidati` DROP INDEX if exists `PRIMARY`";
476$sql2="ADD PRIMARY KEY (`id_cand`) USING BTREE";
477$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
478
479$tab=$prefix."_ele_consultazione";
480$ind="descrizione";
481#$sql="ALTER TABLE `".$prefix."_ele_consultazione` DROP INDEX if exists `descrizione`";
482$sql2="ALTER TABLE `".$prefix."_ele_consultazione` ADD UNIQUE `descrizione` (`descrizione`(100))";
483$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
484
485echo "<br><br>".++$num.") Modifica Charset del database";
486if($newcs=='utf8') {
487 $cset='utf8';
488 $ccollate='utf8_general_ci';
489 $preset='latin1';
490}else{
491 $cset='latin1';
492 $ccollate='latin1_swedish_ci';
493 $preset='utf8';
494}
495#$sql="ALTER DATABASE $dbname CHARACTER SET '$cset' COLLATE '$ccollate'";
496#$res = $dbi->prepare("$sql");
497#$res->execute();
498
499$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%')";
500$res = $dbi->prepare("$sql");
501$res->execute();
502$tab='';
503$agg=$res->rowCount();
504
505while(list($nometab,$campo,$def,$tipo,$nul)=$res->fetch(PDO::FETCH_NUM)) {
506 if($tab!=$nometab){
507 $sql="alter table $nometab DEFAULT CHARSET=$cset COLLATE $ccollate";
508 try{
509 $res2 = $dbi->prepare("$sql");
510 $res2->execute();
511 }
512 catch(PDOException $e)
513 {
514 die( $sql . "<br>" . $e->getMessage());
515 }
516 $tab=$nometab; echo "<br><span style=\"color: green;\">- Tabella: $nometab</span>";
517 }
518 if($def!='') $default="DEFAULT '$def'"; else $default='';
519 if($nul=='NO') $nullable='NOT NULL'; else $nullable='NULL';
520 $sql="ALTER TABLE $nometab CHANGE $campo $campo $tipo CHARACTER SET '$cset' COLLATE '$ccollate' $nullable $default;";
521 try{
522 $res2 = $dbi->prepare("$sql");
523 $res2->execute();
524 }
525 catch(PDOException $e)
526 {
527 $default="DEFAULT $def";
528 $sql="ALTER TABLE $nometab CHANGE $campo $campo $tipo CHARACTER SET '$cset' COLLATE '$ccollate' $nullable $default;";
529 try{
530 $res2 = $dbi->prepare("$sql");
531 $res2->execute();
532 }
533 catch(PDOException $e)
534 {
535 die( $sql . "<br>" . $e->getMessage());
536 }
537 }
538 echo "<br><span style=\"color: green;\">-- $campo</span>";
539}
540 $sql="SELECT table_name FROM INFORMATION_SCHEMA.tables WHERE TABLE_SCHEMA = '$dbname' and table_collation like '$preset%'";
541 $res = $dbi->prepare("$sql");
542 $res->execute();
543$tab='';
544if(!$agg) $agg=$res->rowCount();
545while(list($nometab)=$res->fetch(PDO::FETCH_NUM)) {
546 $sql="alter table $nometab DEFAULT CHARSET=$cset COLLATE $ccollate";
547 try{
548 $res2 = $dbi->prepare("$sql");
549 $res2->execute();
550 }
551 catch(PDOException $e)
552 {
553 die( $sql . "<br>" . $e->getMessage());
554 }
555 echo "<br><span style=\"color: green;\">- Tabella: $nometab</span>";
556}
557if(!$agg) echo "<br><span style=\"color: green;\">- Nessuna tabella da aggiornare</span>";
558echo "<br><br>";
559?>
Note: See TracBrowser for help on using the repository browser.