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

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

-- Aggiunto un controllo della struttura del db in aggiornadb

File size: 23.1 KB
RevLine 
[296]1<?php
[324]2
[362]3#imposta il charset su utf8, qualsiasi altro valore per cambiarlo in latin1;
4$newcs='utf8';
[387]5/*
[385]6@require_once("../../config.php");
[296]7 try{
[362]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 }
[296]12 catch(PDOException $e)
13 {
14 die( $sql . "<br>" . $e->getMessage());
15 }
[387]16*/
[362]17
[296]18###############
[357]19function aggiorna($sql,$dbi,$sql2,$num){
[330]20 $ret=0;
[296]21 try{
22 $res = $dbi->prepare("$sql");
23 $res->execute();
[330]24 $ret= 1;
[363]25 }
[296]26 catch(PDOException $e)
27 {
[363]28 $ret=0;
29 echo "<br><span style=\"color: red;\">- Aggiornamento Fallito: $sql</span>";
30 return $ret;
[330]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;
[363]42 echo "<br><span style=\"color: red;\">- Aggiornamento Fallito: $sql2</span>";
[357]43 return $ret;
[330]44 }
[296]45 }
[363]46 echo "<br><span style=\"color: green;\">- Aggiornamento eseguito correttamente</span>";
[330]47 return $ret;
[324]48}
[383]49
[359]50function aggiorna_index($tab,$ind,$dbi,$sql2,$num){
[357]51 $ret=0;
[377]52# $conn->getAttribute( constant( "PDO::ATTR_$val" ) )
[359]53 $sqltest="SHOW INDEX FROM `$tab` WHERE KEY_NAME = '$ind'";
54 $res = $dbi->prepare("$sqltest");
55 $res->execute();
[377]56
[359]57 if($res->rowCount()) {
[377]58 if($ind=='PRIMARY')
59 $sql="ALTER TABLE `$tab` DROP PRIMARY KEY , $sql2 ";
60 else
61 $sql="ALTER TABLE `$tab` DROP INDEX `$ind`";
[359]62 try{
63 $res = $dbi->prepare("$sql");
64 $res->execute();
65 $ret= 1;
66 }
67 catch(PDOException $e)
68 {
[363]69 $ret=0;
70 echo "<br><span style=\"color: red;\">- Tabella: $tab - Indice: $ind - Aggiornamento Fallito: $sql</span>";
71 return $ret;
[359]72 }
[357]73 }
[377]74 if("$sql2"!="" and $ind!='PRIMARY'){
[357]75 try{
76 $res = $dbi->prepare("$sql2");
77 $res->execute();
78 $ret=2;
79 }
80 catch(PDOException $e)
81 {
82 $ret=0;
[363]83 echo "<br><span style=\"color: red;\">- Tabella: $tab - Indice: $ind - Aggiornamento Fallito: $sql2</span>";
[357]84 return $ret;
85 }
86 }
[363]87 echo "<br><span style=\"color: green;\">- Tabella: $tab - Indice: $ind - Index aggiornato</span>";
[357]88 return $ret;
89}
[324]90
[357]91
92
[360]93
[357]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;
[363]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";
[357]103 return 0;
104}
105
[360]106$num=0;
[357]107
[362]108if(controllo($prefix.'_authors','admincomune',++$num))
109{
[389]110 $sql="UPDATE `".$prefix."_authors` SET `adminop` = '1', `adminsuper` = '0',`admincomune` = '0' WHERE `".$prefix."_authors`.`aid` != 'admin' AND `".$prefix."_authors`.`adminsuper` != '1'";
[362]111 $ret=aggiorna($sql,$dbi,'',$num);
[389]112 $sql="UPDATE `".$prefix."_authors` SET `adminop` = '0', `adminsuper` = '0',`admincomune` = '1' WHERE `".$prefix."_authors`.`aid` = 'admin'";
[371]113 $ret=aggiorna($sql,$dbi,'',$num);
[389]114 $sql="UPDATE `".$prefix."_authors` SET `adminop` = '0', `adminsuper` = '1',`admincomune` = '0' WHERE `".$prefix."_authors`.`aid` = 'suser' or `".$prefix."_authors`.`adminsuper` = '1'";
[376]115 $ret=aggiorna($sql,$dbi,'',$num);
[363]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>";
[362]118
[357]119if(!controllo($prefix.'_config','versione',++$num))
[324]120{
[357]121 $sql="alter table `".$prefix."_config` change column `Versione` `versione` int(3)";
122 $ret=aggiorna($sql,$dbi,'',$num);
[363]123} else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_config non richiede questo aggiornamento</span><br>";
124if(controllo($prefix.'_config','secret',++$num))
[362]125{
126 $sql="alter table `".$prefix."_config` DROP `secret`";
127 $ret=aggiorna($sql,$dbi,'',$num);
[363]128} else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_config non richiede questo aggiornamento</span><br>";
[392]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>";
[363]164if(controllo($prefix.'_ele_voti_parziale','data',++$num))
[361]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);
[363]168 echo "<br>";
[361]169}
[371]170++$num;
[376]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 ' '";
[371]172$ret=aggiorna($sql,$dbi,'',$num);
173echo "<br>";
[375]174if(!$ret) echo "Il tuo sistema non necessita di questo aggiornamento, questo avviso di errore va ignorato<br>";
[371]175++$num;
[376]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 ' '";
[371]177$ret=aggiorna($sql,$dbi,'',$num);
178echo "<br>";
[375]179if(!$ret) echo "Il tuo sistema non necessita di questo aggiornamento, questo avviso di errore va ignorato<br>";
[371]180++$num;
[376]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 ' '";
[371]182$ret=aggiorna($sql,$dbi,'',$num);
183echo "<br>";
[375]184if(!$ret) echo "Il tuo sistema non necessita di questo aggiornamento, questo avviso di errore va ignorato<br>";
[361]185
[363]186if(controllo($prefix.'_ele_rilaff','data',++$num))
[361]187{
188 $sql="ALTER TABLE `".$prefix."_ele_rilaff` CHANGE `data` `data` DATE NOT NULL DEFAULT '1900-01-01'";
189 $ret=aggiorna($sql,$dbi,'',$num);
[363]190 echo "<br>";
[361]191}
192
[357]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);
[363]197} else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_gruppo non richiede questo aggiornamento</span><br>";
[357]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);
[363]203} else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_gruppo non richiede questo aggiornamento</span><br>";
[357]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`";
[389]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";
[383]209 $ret=aggiorna($sql,$dbi,$sql2,$num);
210}else{
[389]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";
[357]212 $ret=aggiorna($sql,$dbi,'',$num);
[383]213echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_lista non richiede questo aggiornamento</span><br>";
214}
[357]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);
[363]219}else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_lista non richiede questo aggiornamento</span><br>";
220
[358]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);
[363]225}else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_operatori non richiede questo aggiornamento</span><br>";
226
[359]227if(!controllo($prefix.'_ele_operatori','id_sez',++$num))
[358]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);
[363]231}else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_operatori non richiede questo aggiornamento</span><br>";
232
[357]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);
[363]237}else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_voti_lista non richiede questo aggiornamento</span><br>";
[357]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);
[363]243}else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_voti_ref non richiede questo aggiornamento</span><br>";
[357]244
[396]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>";
[357]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);
[363]254}else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_voti_candidati non richiede questo aggiornamento</span><br>";
[357]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);
[363]260}else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_candidati non richiede questo aggiornamento</span><br>";
[357]261
262if(!controllo($prefix.'_ele_sezioni','colore',++$num))
263{
[363]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{
[364]267 $sql="ALTER TABLE `".$prefix."_ele_sezioni` CHANGE `colore` `colore` VARCHAR(50) DEFAULT '#FAFAD2'";
[363]268 $ret=aggiorna($sql,$dbi,'',$num);
269} echo "<br>";
270
[357]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);
[363]278}else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_controlli non richiede questo aggiornamento</span><br>";
[357]279
[363]280echo "<br>".++$num.") Eliminazione della vecchia tabella dei controlli: ";
281if(controllo($prefix.'_ele_controllisez','',$num))
[357]282{
283$sql="DROP TABLE if exists `".$prefix."_ele_controllisez`";
284$ret=aggiorna($sql,$dbi,'',$num);
[363]285} else echo "<br><span style=\"color: green;\">- Tabella non presente</span><br>";
[357]286
[363]287echo "<br>".++$num.") Aggiornamento tabella ".$prefix."_ele_voti_gruppo";
[357]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;";
[363]289$ret=aggiorna($sql,$dbi,'',$num);
[357]290
[377]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
[363]295echo "<br><br>".++$num.") Aggiornamento tabella ".$prefix."_ele_voti_lista";
[357]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;";
[363]297$ret=aggiorna($sql,$dbi,'',$num);
[357]298##############################################
299
[377]300echo "<br><br>".++$num.") Aggiornamento dei valori di default: ".$prefix."_ele_gruppo";
[357]301$sql="ALTER TABLE `".$prefix."_ele_gruppo` CHANGE `num_circ` `num_circ` INT(2) UNSIGNED NOT NULL DEFAULT '1';";
[363]302$ret=aggiorna($sql,$dbi,'',$num);
[357]303
[389]304$sql="ALTER TABLE `".$prefix."_authors` CHANGE `adminsuper` `adminsuper` TINYINT(2) NOT NULL DEFAULT '0';";
[363]305$ret=aggiorna($sql,$dbi,'',$num);
[359]306
[357]307$sql="ALTER TABLE `".$prefix."_ele_lista` CHANGE `num_gruppo` `num_gruppo` INT(2) UNSIGNED NOT NULL DEFAULT '0';";
[363]308$ret=aggiorna($sql,$dbi,'',$num);
[357]309
310$sql="ALTER TABLE `".$prefix."_ele_lista` CHANGE `num_circ` `num_circ` INT(2) UNSIGNED NOT NULL DEFAULT '1';";
[363]311$ret=aggiorna($sql,$dbi,'',$num);
[357]312
[384]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
[363]317echo "<br><br>".++$num.") Ricostruzione della tabella ".$prefix."_ele_fasce`";
[357]318$sql="DROP TABLE `".$prefix."_ele_fasce`";
[363]319$ret=aggiorna($sql,$dbi,'',$num);
[357]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
[363]326) ENGINE=MyISAM DEFAULT CHARSET 'utf8';";
[357]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);";
[363]397$ret=aggiorna($sql,$dbi,$sql2,$num);
[324]398#if(!$ret) echo "<br>".$num++.") Fallito: $sql"; else echo "<br>".$num++.") Aggiornato<br>";
[296]399
[357]400$sql="ALTER TABLE `".$prefix."_ele_fasce`
401 ADD KEY `id_fascia` (`id_fascia`);";
[363]402$ret=aggiorna($sql,$dbi,'',$num);
[357]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";
[359]413#$sql="ALTER TABLE `".$prefix."_ele_voti_lista` DROP INDEX if exists `id_cons`";
[363]414echo "<br><br>".++$num.") Ricostruzione e aggioramento indici";
[359]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";
[363]419$ret=aggiorna_index($tab, $ind,$dbi,$sql2,$num);
[359]420
[377]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
[359]426$tab=$prefix."_ele_voti_lista";
427$ind="id_cons";
[357]428$sql2="ALTER TABLE `".$prefix."_ele_voti_lista` ADD INDEX `id_cons` (`id_cons`, `id_sez`, `id_lista`) USING BTREE";
[363]429$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
[357]430
[359]431$tab=$prefix."_ele_voti_gruppo";
432$ind="id_cons";
433#$sql="ALTER TABLE `".$prefix."_ele_voti_gruppo` DROP INDEX if exists `id_cons`";
[357]434$sql2="ALTER TABLE `".$prefix."_ele_voti_gruppo` ADD INDEX `id_cons` (`id_cons`, `id_sez`, `id_gruppo`) USING BTREE";
[363]435$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
[357]436
[359]437$tab=$prefix."_ele_voti_candidati";
438$ind="id_cons";
439#$sql="ALTER TABLE `".$prefix."_ele_voti_candidati` DROP INDEX if exists `id_cons`";
[357]440$sql2="ALTER TABLE `".$prefix."_ele_voti_candidati` ADD INDEX `id_cons` (`id_cons`, `id_sez`, `id_cand`) USING BTREE";
[363]441$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
[357]442
[359]443$tab=$prefix."_ele_lista";
444$ind="id_cons";
445#$sql="ALTER TABLE `".$prefix."_ele_lista` DROP INDEX if exists `id_cons`";
[357]446$sql2="ALTER TABLE `".$prefix."_ele_lista` ADD INDEX `id_cons` (`id_cons`, `id_gruppo`) USING BTREE";
[363]447$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
[357]448
[359]449$tab=$prefix."_ele_lista";
450$ind="PRIMARY";
451#$sql="ALTER TABLE `".$prefix."_ele_lista` DROP INDEX if exists `PRIMARY`";
[377]452$sql2="ADD PRIMARY KEY (`id_lista`) USING BTREE";
[363]453$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
[357]454
[359]455$tab=$prefix."_ele_gruppo";
456$ind="id_cons";
457#$sql="ALTER TABLE `".$prefix."_ele_gruppo` DROP INDEX if exists `id_cons`";
[357]458$sql2="ALTER TABLE `".$prefix."_ele_gruppo` ADD INDEX `id_cons` (`id_cons`, `id_circ`) USING BTREE";
[363]459$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
[357]460
[359]461$tab=$prefix."_ele_gruppo";
462$ind="PRIMARY";
463#$sql="ALTER TABLE `".$prefix."_ele_gruppo` DROP INDEX if exists `PRIMARY`";
[377]464$sql2="ADD PRIMARY KEY (`id_gruppo`) USING BTREE";
[363]465$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
[357]466
[359]467$tab=$prefix."_ele_candidati";
468$ind="id_cons";
469#$sql="ALTER TABLE `".$prefix."_ele_candidati` DROP INDEX if exists `id_cons`";
[357]470$sql2="ALTER TABLE `".$prefix."_ele_candidati` ADD INDEX `id_cons` (`id_cons`, `id_lista`) USING BTREE";
[363]471$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
[357]472
[359]473$tab=$prefix."_ele_candidati";
474$ind="PRIMARY";
475#$sql="ALTER TABLE `".$prefix."_ele_candidati` DROP INDEX if exists `PRIMARY`";
[377]476$sql2="ADD PRIMARY KEY (`id_cand`) USING BTREE";
[363]477$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
[357]478
[359]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))";
[363]483$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
[357]484
[363]485echo "<br><br>".++$num.") Modifica Charset del database";
[362]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'";
[361]496#$res = $dbi->prepare("$sql");
497#$res->execute();
[362]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%')";
[361]500$res = $dbi->prepare("$sql");
501$res->execute();
502$tab='';
[363]503$agg=$res->rowCount();
[362]504
[361]505while(list($nometab,$campo,$def,$tipo,$nul)=$res->fetch(PDO::FETCH_NUM)) {
506 if($tab!=$nometab){
[362]507 $sql="alter table $nometab DEFAULT CHARSET=$cset COLLATE $ccollate";
[361]508 try{
509 $res2 = $dbi->prepare("$sql");
510 $res2->execute();
511 }
512 catch(PDOException $e)
513 {
514 die( $sql . "<br>" . $e->getMessage());
515 }
[363]516 $tab=$nometab; echo "<br><span style=\"color: green;\">- Tabella: $nometab</span>";
[361]517 }
518 if($def!='') $default="DEFAULT '$def'"; else $default='';
519 if($nul=='NO') $nullable='NOT NULL'; else $nullable='NULL';
[362]520 $sql="ALTER TABLE $nometab CHANGE $campo $campo $tipo CHARACTER SET '$cset' COLLATE '$ccollate' $nullable $default;";
[361]521 try{
[363]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>";
[361]539}
[362]540 $sql="SELECT table_name FROM INFORMATION_SCHEMA.tables WHERE TABLE_SCHEMA = '$dbname' and table_collation like '$preset%'";
[361]541 $res = $dbi->prepare("$sql");
542 $res->execute();
543$tab='';
[363]544if(!$agg) $agg=$res->rowCount();
[361]545while(list($nometab)=$res->fetch(PDO::FETCH_NUM)) {
[362]546 $sql="alter table $nometab DEFAULT CHARSET=$cset COLLATE $ccollate";
[361]547 try{
548 $res2 = $dbi->prepare("$sql");
549 $res2->execute();
550 }
551 catch(PDOException $e)
552 {
553 die( $sql . "<br>" . $e->getMessage());
554 }
[363]555 echo "<br><span style=\"color: green;\">- Tabella: $nometab</span>";
[361]556}
[363]557if(!$agg) echo "<br><span style=\"color: green;\">- Nessuna tabella da aggiornare</span>";
[361]558echo "<br><br>";
[296]559?>
Note: See TracBrowser for help on using the repository browser.