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

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