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

Last change on this file since 398 was 398, checked in by roby, 9 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
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
[398]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
[357]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);
[363]243}else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_voti_lista non richiede questo aggiornamento</span><br>";
[357]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);
[363]249}else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_voti_ref non richiede questo aggiornamento</span><br>";
[357]250
[396]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>";
[357]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);
[363]260}else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_voti_candidati non richiede questo aggiornamento</span><br>";
[357]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);
[363]266}else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_candidati non richiede questo aggiornamento</span><br>";
[357]267
268if(!controllo($prefix.'_ele_sezioni','colore',++$num))
269{
[363]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{
[364]273 $sql="ALTER TABLE `".$prefix."_ele_sezioni` CHANGE `colore` `colore` VARCHAR(50) DEFAULT '#FAFAD2'";
[363]274 $ret=aggiorna($sql,$dbi,'',$num);
275} echo "<br>";
276
[357]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);
[363]284}else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_controlli non richiede questo aggiornamento</span><br>";
[357]285
[363]286echo "<br>".++$num.") Eliminazione della vecchia tabella dei controlli: ";
287if(controllo($prefix.'_ele_controllisez','',$num))
[357]288{
289$sql="DROP TABLE if exists `".$prefix."_ele_controllisez`";
290$ret=aggiorna($sql,$dbi,'',$num);
[363]291} else echo "<br><span style=\"color: green;\">- Tabella non presente</span><br>";
[357]292
[363]293echo "<br>".++$num.") Aggiornamento tabella ".$prefix."_ele_voti_gruppo";
[357]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;";
[363]295$ret=aggiorna($sql,$dbi,'',$num);
[357]296
[377]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
[363]301echo "<br><br>".++$num.") Aggiornamento tabella ".$prefix."_ele_voti_lista";
[357]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;";
[363]303$ret=aggiorna($sql,$dbi,'',$num);
[357]304##############################################
305
[377]306echo "<br><br>".++$num.") Aggiornamento dei valori di default: ".$prefix."_ele_gruppo";
[357]307$sql="ALTER TABLE `".$prefix."_ele_gruppo` CHANGE `num_circ` `num_circ` INT(2) UNSIGNED NOT NULL DEFAULT '1';";
[363]308$ret=aggiorna($sql,$dbi,'',$num);
[357]309
[389]310$sql="ALTER TABLE `".$prefix."_authors` CHANGE `adminsuper` `adminsuper` TINYINT(2) NOT NULL DEFAULT '0';";
[363]311$ret=aggiorna($sql,$dbi,'',$num);
[359]312
[357]313$sql="ALTER TABLE `".$prefix."_ele_lista` CHANGE `num_gruppo` `num_gruppo` INT(2) UNSIGNED NOT NULL DEFAULT '0';";
[363]314$ret=aggiorna($sql,$dbi,'',$num);
[357]315
316$sql="ALTER TABLE `".$prefix."_ele_lista` CHANGE `num_circ` `num_circ` INT(2) UNSIGNED NOT NULL DEFAULT '1';";
[363]317$ret=aggiorna($sql,$dbi,'',$num);
[357]318
[384]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
[363]323echo "<br><br>".++$num.") Ricostruzione della tabella ".$prefix."_ele_fasce`";
[357]324$sql="DROP TABLE `".$prefix."_ele_fasce`";
[363]325$ret=aggiorna($sql,$dbi,'',$num);
[357]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
[363]332) ENGINE=MyISAM DEFAULT CHARSET 'utf8';";
[357]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);";
[363]403$ret=aggiorna($sql,$dbi,$sql2,$num);
[324]404#if(!$ret) echo "<br>".$num++.") Fallito: $sql"; else echo "<br>".$num++.") Aggiornato<br>";
[296]405
[357]406$sql="ALTER TABLE `".$prefix."_ele_fasce`
407 ADD KEY `id_fascia` (`id_fascia`);";
[363]408$ret=aggiorna($sql,$dbi,'',$num);
[357]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";
[359]419#$sql="ALTER TABLE `".$prefix."_ele_voti_lista` DROP INDEX if exists `id_cons`";
[363]420echo "<br><br>".++$num.") Ricostruzione e aggioramento indici";
[359]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";
[363]425$ret=aggiorna_index($tab, $ind,$dbi,$sql2,$num);
[359]426
[377]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
[359]432$tab=$prefix."_ele_voti_lista";
433$ind="id_cons";
[357]434$sql2="ALTER TABLE `".$prefix."_ele_voti_lista` ADD INDEX `id_cons` (`id_cons`, `id_sez`, `id_lista`) USING BTREE";
[363]435$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
[357]436
[359]437$tab=$prefix."_ele_voti_gruppo";
438$ind="id_cons";
439#$sql="ALTER TABLE `".$prefix."_ele_voti_gruppo` DROP INDEX if exists `id_cons`";
[357]440$sql2="ALTER TABLE `".$prefix."_ele_voti_gruppo` ADD INDEX `id_cons` (`id_cons`, `id_sez`, `id_gruppo`) USING BTREE";
[363]441$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
[357]442
[359]443$tab=$prefix."_ele_voti_candidati";
444$ind="id_cons";
445#$sql="ALTER TABLE `".$prefix."_ele_voti_candidati` DROP INDEX if exists `id_cons`";
[357]446$sql2="ALTER TABLE `".$prefix."_ele_voti_candidati` ADD INDEX `id_cons` (`id_cons`, `id_sez`, `id_cand`) USING BTREE";
[363]447$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
[357]448
[359]449$tab=$prefix."_ele_lista";
450$ind="id_cons";
451#$sql="ALTER TABLE `".$prefix."_ele_lista` DROP INDEX if exists `id_cons`";
[357]452$sql2="ALTER TABLE `".$prefix."_ele_lista` ADD INDEX `id_cons` (`id_cons`, `id_gruppo`) USING BTREE";
[363]453$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
[357]454
[359]455$tab=$prefix."_ele_lista";
456$ind="PRIMARY";
457#$sql="ALTER TABLE `".$prefix."_ele_lista` DROP INDEX if exists `PRIMARY`";
[377]458$sql2="ADD PRIMARY KEY (`id_lista`) USING BTREE";
[363]459$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
[357]460
[359]461$tab=$prefix."_ele_gruppo";
462$ind="id_cons";
463#$sql="ALTER TABLE `".$prefix."_ele_gruppo` DROP INDEX if exists `id_cons`";
[357]464$sql2="ALTER TABLE `".$prefix."_ele_gruppo` ADD INDEX `id_cons` (`id_cons`, `id_circ`) USING BTREE";
[363]465$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
[357]466
[359]467$tab=$prefix."_ele_gruppo";
468$ind="PRIMARY";
469#$sql="ALTER TABLE `".$prefix."_ele_gruppo` DROP INDEX if exists `PRIMARY`";
[377]470$sql2="ADD PRIMARY KEY (`id_gruppo`) USING BTREE";
[363]471$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
[357]472
[359]473$tab=$prefix."_ele_candidati";
474$ind="id_cons";
475#$sql="ALTER TABLE `".$prefix."_ele_candidati` DROP INDEX if exists `id_cons`";
[357]476$sql2="ALTER TABLE `".$prefix."_ele_candidati` ADD INDEX `id_cons` (`id_cons`, `id_lista`) USING BTREE";
[363]477$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
[357]478
[359]479$tab=$prefix."_ele_candidati";
480$ind="PRIMARY";
481#$sql="ALTER TABLE `".$prefix."_ele_candidati` DROP INDEX if exists `PRIMARY`";
[377]482$sql2="ADD PRIMARY KEY (`id_cand`) USING BTREE";
[363]483$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
[357]484
[359]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))";
[363]489$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
[357]490
[363]491echo "<br><br>".++$num.") Modifica Charset del database";
[362]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'";
[361]502#$res = $dbi->prepare("$sql");
503#$res->execute();
[362]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%')";
[361]506$res = $dbi->prepare("$sql");
507$res->execute();
508$tab='';
[363]509$agg=$res->rowCount();
[362]510
[361]511while(list($nometab,$campo,$def,$tipo,$nul)=$res->fetch(PDO::FETCH_NUM)) {
512 if($tab!=$nometab){
[362]513 $sql="alter table $nometab DEFAULT CHARSET=$cset COLLATE $ccollate";
[361]514 try{
515 $res2 = $dbi->prepare("$sql");
516 $res2->execute();
517 }
518 catch(PDOException $e)
519 {
520 die( $sql . "<br>" . $e->getMessage());
521 }
[363]522 $tab=$nometab; echo "<br><span style=\"color: green;\">- Tabella: $nometab</span>";
[361]523 }
524 if($def!='') $default="DEFAULT '$def'"; else $default='';
525 if($nul=='NO') $nullable='NOT NULL'; else $nullable='NULL';
[362]526 $sql="ALTER TABLE $nometab CHANGE $campo $campo $tipo CHARACTER SET '$cset' COLLATE '$ccollate' $nullable $default;";
[361]527 try{
[363]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>";
[361]545}
[362]546 $sql="SELECT table_name FROM INFORMATION_SCHEMA.tables WHERE TABLE_SCHEMA = '$dbname' and table_collation like '$preset%'";
[361]547 $res = $dbi->prepare("$sql");
548 $res->execute();
549$tab='';
[363]550if(!$agg) $agg=$res->rowCount();
[361]551while(list($nometab)=$res->fetch(PDO::FETCH_NUM)) {
[362]552 $sql="alter table $nometab DEFAULT CHARSET=$cset COLLATE $ccollate";
[361]553 try{
554 $res2 = $dbi->prepare("$sql");
555 $res2->execute();
556 }
557 catch(PDOException $e)
558 {
559 die( $sql . "<br>" . $e->getMessage());
560 }
[363]561 echo "<br><span style=\"color: green;\">- Tabella: $nometab</span>";
[361]562}
[363]563if(!$agg) echo "<br><span style=\"color: green;\">- Nessuna tabella da aggiornare</span>";
[361]564echo "<br><br>";
[296]565?>
Note: See TracBrowser for help on using the repository browser.