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

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

-- Aggiornamento del db: aggiunto il controllo per il campo id della tabella _ele_temi (non presente nelle versioni precedenti alla 247)

File size: 23.4 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_temi','id',++$num))
234{
235 $sql="ALTER TABLE `".$prefix."_ele_temi` ADD `id` INT(11) NOT NULL AUTO_INCREMENT FIRST, ADD PRIMARY KEY (`id`)";
236 $ret=aggiorna($sql,$dbi,'',$num);
237}else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_operatori non richiede questo aggiornamento</span><br>";
238
239if(!controllo($prefix.'_ele_voti_lista','num_lista',++$num))
240{
241 $sql="ALTER TABLE `".$prefix."_ele_voti_lista` ADD `num_lista` 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_lista non richiede questo aggiornamento</span><br>";
244
245if(!controllo($prefix.'_ele_voti_ref','num_gruppo',++$num))
246{
247 $sql="ALTER TABLE `".$prefix."_ele_voti_ref` ADD `num_gruppo` INT(2) UNSIGNED NOT NULL AFTER `id_sez`";
248 $ret=aggiorna($sql,$dbi,'',$num);
249}else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_voti_ref non richiede questo aggiornamento</span><br>";
250
251if(controllo($prefix.'_ele_candidati','Sesso',++$num))
252{
253 $sql="ALTER TABLE `".$prefix."_ele_candidati` DROP `Sesso`";
254 $ret=aggiorna($sql,$dbi,'',$num);
255} else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_candidati non richiede questo aggiornamento</span><br>";
256if(!controllo($prefix.'_ele_voti_candidati','num_cand',++$num))
257{
258 $sql="ALTER TABLE `".$prefix."_ele_voti_candidati` ADD `num_cand` INT(2) UNSIGNED NOT NULL AFTER `id_sez`";
259 $ret=aggiorna($sql,$dbi,'',$num);
260}else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_voti_candidati non richiede questo aggiornamento</span><br>";
261
262if(!controllo($prefix.'_ele_candidati','num_lista',++$num))
263{
264 $sql="ALTER TABLE `".$prefix."_ele_candidati` ADD `num_lista` INT(2) UNSIGNED NOT NULL AFTER `id_lista`";
265 $ret=aggiorna($sql,$dbi,'',$num);
266}else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_candidati non richiede questo aggiornamento</span><br>";
267
268if(!controllo($prefix.'_ele_sezioni','colore',++$num))
269{
270 $sql="ALTER TABLE `".$prefix."_ele_sezioni` ADD `colore` VARCHAR(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '#FAFAD2' AFTER `solo_lista`";
271 $ret=aggiorna($sql,$dbi,'',$num);
272}else{
273 $sql="ALTER TABLE `".$prefix."_ele_sezioni` CHANGE `colore` `colore` VARCHAR(50) DEFAULT '#FAFAD2'";
274 $ret=aggiorna($sql,$dbi,'',$num);
275} echo "<br>";
276
277echo "<br>Aggiornamento per nuovo sistema dei controlli di congruità";
278
279if(!controllo($prefix.'_ele_controlli','id_cons',++$num))
280{
281 $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";
282 echo "<br>".$num.") Creazione tabella dei controlli: ";
283 $ret=aggiorna($sql,$dbi,'',$num);
284}else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_controlli non richiede questo aggiornamento</span><br>";
285
286echo "<br>".++$num.") Eliminazione della vecchia tabella dei controlli: ";
287if(controllo($prefix.'_ele_controllisez','',$num))
288{
289$sql="DROP TABLE if exists `".$prefix."_ele_controllisez`";
290$ret=aggiorna($sql,$dbi,'',$num);
291} else echo "<br><span style=\"color: green;\">- Tabella non presente</span><br>";
292
293echo "<br>".++$num.") Aggiornamento tabella ".$prefix."_ele_voti_gruppo";
294$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;";
295$ret=aggiorna($sql,$dbi,'',$num);
296
297echo "<br><br>".++$num.") Aggiornamento dei valori di default: ".$prefix."_ele_voti_lista";
298$sql="ALTER TABLE `".$prefix."_ele_voti_lista` CHANGE `num_lista` `num_lista` INT(2) UNSIGNED NULL DEFAULT '0';";
299$ret=aggiorna($sql,$dbi,'',$num);
300
301echo "<br><br>".++$num.") Aggiornamento tabella ".$prefix."_ele_voti_lista";
302$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;";
303$ret=aggiorna($sql,$dbi,'',$num);
304##############################################
305
306echo "<br><br>".++$num.") Aggiornamento dei valori di default: ".$prefix."_ele_gruppo";
307$sql="ALTER TABLE `".$prefix."_ele_gruppo` CHANGE `num_circ` `num_circ` INT(2) UNSIGNED NOT NULL DEFAULT '1';";
308$ret=aggiorna($sql,$dbi,'',$num);
309
310$sql="ALTER TABLE `".$prefix."_authors` CHANGE `adminsuper` `adminsuper` TINYINT(2) NOT NULL DEFAULT '0';";
311$ret=aggiorna($sql,$dbi,'',$num);
312
313$sql="ALTER TABLE `".$prefix."_ele_lista` CHANGE `num_gruppo` `num_gruppo` INT(2) UNSIGNED NOT NULL DEFAULT '0';";
314$ret=aggiorna($sql,$dbi,'',$num);
315
316$sql="ALTER TABLE `".$prefix."_ele_lista` CHANGE `num_circ` `num_circ` INT(2) UNSIGNED NOT NULL DEFAULT '1';";
317$ret=aggiorna($sql,$dbi,'',$num);
318
319echo "<br><br>".++$num.") Aggiornamento della tabella _ele_conf per la nuova gestione della L.R. Sicilia";
320$sql="UPDATE `".$prefix."_ele_conf` SET `inffisso` = '1' WHERE `id_conf` = 4;";
321$ret=aggiorna($sql,$dbi,'',$num);
322
323echo "<br><br>".++$num.") Ricostruzione della tabella ".$prefix."_ele_fasce`";
324$sql="DROP TABLE `".$prefix."_ele_fasce`";
325$ret=aggiorna($sql,$dbi,'',$num);
326
327$sql="CREATE TABLE `".$prefix."_ele_fasce` (
328 `id_fascia` int(2) NOT NULL,
329 `abitanti` int(11) NOT NULL,
330 `seggi` int(4) NOT NULL,
331 `id_conf` int(11) DEFAULT 1
332) ENGINE=MyISAM DEFAULT CHARSET 'utf8';";
333
334
335$sql2="INSERT INTO `".$prefix."_ele_fasce` (`id_fascia`, `abitanti`, `seggi`, `id_conf`) VALUES
336(1, 3000, 12, 1),
337(2, 10000, 16, 1),
338(3, 15000, 20, 1),
339(4, 30000, 20, 1),
340(5, 100000, 30, 1),
341(6, 250000, 40, 1),
342(7, 500000, 46, 1),
343(8, 1000000, 50, 1),
344(9, 100000000, 60, 1),
345(1, 3000, 12, 2),
346(2, 10000, 16, 2),
347(3, 15000, 20, 2),
348(4, 30000, 20, 2),
349(5, 100000, 30, 2),
350(6, 250000, 40, 2),
351(7, 500000, 46, 2),
352(8, 1000000, 50, 2),
353(9, 100000000, 60, 2),
354(1, 3000, 9, 3),
355(2, 5000, 9, 3),
356(4, 15000, 16, 3),
357(3, 10000, 12, 3),
358(5, 30000, 16, 3),
359(6, 100000, 24, 3),
360(7, 250000, 32, 3),
361(8, 500000, 36, 3),
362(9, 1000000, 40, 3),
363(10, 100000000, 48, 3),
364(1, 3000, 6, 4),
365(2, 5000, 7, 4),
366(3, 10000, 12, 4),
367(4, 15000, 16, 4),
368(5, 30000, 16, 4),
369(6, 100000, 24, 4),
370(7, 250000, 32, 4),
371(8, 500000, 36, 4),
372(9, 1000000, 40, 4),
373(10, 100000000, 48, 4),
374(1, 3000, 9, 5),
375(2, 5000, 9, 5),
376(3, 10000, 12, 5),
377(4, 15000, 16, 5),
378(5, 30000, 16, 5),
379(6, 100000, 24, 5),
380(7, 250000, 32, 5),
381(8, 500000, 36, 5),
382(9, 1000000, 40, 5),
383(10, 100000000, 48, 5),
384(1, 3000, 6, 6),
385(2, 5000, 7, 6),
386(3, 10000, 10, 6),
387(4, 15000, 16, 6),
388(5, 30000, 16, 6),
389(6, 100000, 24, 6),
390(7, 250000, 32, 6),
391(8, 500000, 36, 6),
392(9, 1000000, 40, 6),
393(10, 100000000, 48, 6),
394(1, 3000, 10, 7),
395(2, 10000, 12, 7),
396(3, 15000, 16, 7),
397(4, 30000, 16, 7),
398(5, 100000, 24, 7),
399(6, 250000, 32, 7),
400(7, 500000, 36, 7),
401(8, 1000000, 40, 7),
402(9, 100000000, 48, 7);";
403$ret=aggiorna($sql,$dbi,$sql2,$num);
404#if(!$ret) echo "<br>".$num++.") Fallito: $sql"; else echo "<br>".$num++.") Aggiornato<br>";
405
406$sql="ALTER TABLE `".$prefix."_ele_fasce`
407 ADD KEY `id_fascia` (`id_fascia`);";
408$ret=aggiorna($sql,$dbi,'',$num);
409#if(!$ret) echo "<br>".$num++.") Fallito: $sql"; else echo "<br>".$num++.") Aggiornato<br>";
410
411
412
413#if(!$ret)
414# echo "<br>".$num++.") Fallito: $sql";
415#elseif ($ret==1)
416# echo "<br>".$num++.") Index eliminato";
417#else
418# echo "<br>".$num++.") Index aggiunto";
419#$sql="ALTER TABLE `".$prefix."_ele_voti_lista` DROP INDEX if exists `id_cons`";
420echo "<br><br>".++$num.") Ricostruzione e aggioramento indici";
421$tab=$prefix."_ele_voti_ref";
422$ind="id_cons";
423# $sql="ALTER TABLE `".$prefix."_ele_voti_ref` DROP INDEX `id_cons`; ";
424$sql2="ALTER TABLE `".$prefix."_ele_voti_ref` ADD INDEX `id_cons` (`id_cons`, `id_gruppo`) USING BTREE";
425$ret=aggiorna_index($tab, $ind,$dbi,$sql2,$num);
426
427$tab=$prefix."_ele_sezioni";
428$ind="id_cons";
429$sql2="ALTER TABLE `".$prefix."_ele_sezioni` ADD UNIQUE `id_cons` (`id_cons`, `num_sez`) USING BTREE";
430$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
431
432$tab=$prefix."_ele_voti_lista";
433$ind="id_cons";
434$sql2="ALTER TABLE `".$prefix."_ele_voti_lista` ADD INDEX `id_cons` (`id_cons`, `id_sez`, `id_lista`) USING BTREE";
435$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
436
437$tab=$prefix."_ele_voti_gruppo";
438$ind="id_cons";
439#$sql="ALTER TABLE `".$prefix."_ele_voti_gruppo` DROP INDEX if exists `id_cons`";
440$sql2="ALTER TABLE `".$prefix."_ele_voti_gruppo` ADD INDEX `id_cons` (`id_cons`, `id_sez`, `id_gruppo`) USING BTREE";
441$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
442
443$tab=$prefix."_ele_voti_candidati";
444$ind="id_cons";
445#$sql="ALTER TABLE `".$prefix."_ele_voti_candidati` DROP INDEX if exists `id_cons`";
446$sql2="ALTER TABLE `".$prefix."_ele_voti_candidati` ADD INDEX `id_cons` (`id_cons`, `id_sez`, `id_cand`) USING BTREE";
447$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
448
449$tab=$prefix."_ele_lista";
450$ind="id_cons";
451#$sql="ALTER TABLE `".$prefix."_ele_lista` DROP INDEX if exists `id_cons`";
452$sql2="ALTER TABLE `".$prefix."_ele_lista` ADD INDEX `id_cons` (`id_cons`, `id_gruppo`) USING BTREE";
453$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
454
455$tab=$prefix."_ele_lista";
456$ind="PRIMARY";
457#$sql="ALTER TABLE `".$prefix."_ele_lista` DROP INDEX if exists `PRIMARY`";
458$sql2="ADD PRIMARY KEY (`id_lista`) USING BTREE";
459$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
460
461$tab=$prefix."_ele_gruppo";
462$ind="id_cons";
463#$sql="ALTER TABLE `".$prefix."_ele_gruppo` DROP INDEX if exists `id_cons`";
464$sql2="ALTER TABLE `".$prefix."_ele_gruppo` ADD INDEX `id_cons` (`id_cons`, `id_circ`) USING BTREE";
465$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
466
467$tab=$prefix."_ele_gruppo";
468$ind="PRIMARY";
469#$sql="ALTER TABLE `".$prefix."_ele_gruppo` DROP INDEX if exists `PRIMARY`";
470$sql2="ADD PRIMARY KEY (`id_gruppo`) USING BTREE";
471$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
472
473$tab=$prefix."_ele_candidati";
474$ind="id_cons";
475#$sql="ALTER TABLE `".$prefix."_ele_candidati` DROP INDEX if exists `id_cons`";
476$sql2="ALTER TABLE `".$prefix."_ele_candidati` ADD INDEX `id_cons` (`id_cons`, `id_lista`) USING BTREE";
477$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
478
479$tab=$prefix."_ele_candidati";
480$ind="PRIMARY";
481#$sql="ALTER TABLE `".$prefix."_ele_candidati` DROP INDEX if exists `PRIMARY`";
482$sql2="ADD PRIMARY KEY (`id_cand`) USING BTREE";
483$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
484
485$tab=$prefix."_ele_consultazione";
486$ind="descrizione";
487#$sql="ALTER TABLE `".$prefix."_ele_consultazione` DROP INDEX if exists `descrizione`";
488$sql2="ALTER TABLE `".$prefix."_ele_consultazione` ADD UNIQUE `descrizione` (`descrizione`(100))";
489$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
490
491echo "<br><br>".++$num.") Modifica Charset del database";
492if($newcs=='utf8') {
493 $cset='utf8';
494 $ccollate='utf8_general_ci';
495 $preset='latin1';
496}else{
497 $cset='latin1';
498 $ccollate='latin1_swedish_ci';
499 $preset='utf8';
500}
501#$sql="ALTER DATABASE $dbname CHARACTER SET '$cset' COLLATE '$ccollate'";
502#$res = $dbi->prepare("$sql");
503#$res->execute();
504
505$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%')";
506$res = $dbi->prepare("$sql");
507$res->execute();
508$tab='';
509$agg=$res->rowCount();
510
511while(list($nometab,$campo,$def,$tipo,$nul)=$res->fetch(PDO::FETCH_NUM)) {
512 if($tab!=$nometab){
513 $sql="alter table $nometab DEFAULT CHARSET=$cset COLLATE $ccollate";
514 try{
515 $res2 = $dbi->prepare("$sql");
516 $res2->execute();
517 }
518 catch(PDOException $e)
519 {
520 die( $sql . "<br>" . $e->getMessage());
521 }
522 $tab=$nometab; echo "<br><span style=\"color: green;\">- Tabella: $nometab</span>";
523 }
524 if($def!='') $default="DEFAULT '$def'"; else $default='';
525 if($nul=='NO') $nullable='NOT NULL'; else $nullable='NULL';
526 $sql="ALTER TABLE $nometab CHANGE $campo $campo $tipo CHARACTER SET '$cset' COLLATE '$ccollate' $nullable $default;";
527 try{
528 $res2 = $dbi->prepare("$sql");
529 $res2->execute();
530 }
531 catch(PDOException $e)
532 {
533 $default="DEFAULT $def";
534 $sql="ALTER TABLE $nometab CHANGE $campo $campo $tipo CHARACTER SET '$cset' COLLATE '$ccollate' $nullable $default;";
535 try{
536 $res2 = $dbi->prepare("$sql");
537 $res2->execute();
538 }
539 catch(PDOException $e)
540 {
541 die( $sql . "<br>" . $e->getMessage());
542 }
543 }
544 echo "<br><span style=\"color: green;\">-- $campo</span>";
545}
546 $sql="SELECT table_name FROM INFORMATION_SCHEMA.tables WHERE TABLE_SCHEMA = '$dbname' and table_collation like '$preset%'";
547 $res = $dbi->prepare("$sql");
548 $res->execute();
549$tab='';
550if(!$agg) $agg=$res->rowCount();
551while(list($nometab)=$res->fetch(PDO::FETCH_NUM)) {
552 $sql="alter table $nometab DEFAULT CHARSET=$cset COLLATE $ccollate";
553 try{
554 $res2 = $dbi->prepare("$sql");
555 $res2->execute();
556 }
557 catch(PDOException $e)
558 {
559 die( $sql . "<br>" . $e->getMessage());
560 }
561 echo "<br><span style=\"color: green;\">- Tabella: $nometab</span>";
562}
563if(!$agg) echo "<br><span style=\"color: green;\">- Nessuna tabella da aggiornare</span>";
564echo "<br><br>";
565?>
Note: See TracBrowser for help on using the repository browser.