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

Last change on this file since 383 was 383, checked in by roby, 18 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
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 `soraldo_authors` SET `adminop` = '1', `adminsuper` = '0',`admincomune` = '0' WHERE `soraldo_authors`.`aid` != 'admin' AND `soraldo_authors`.`adminsuper` != '1'";
111 $ret=aggiorna($sql,$dbi,'',$num);
112 $sql="UPDATE `soraldo_authors` SET `adminop` = '0', `adminsuper` = '0',`admincomune` = '1' WHERE `soraldo_authors`.`aid` = 'admin'";
113 $ret=aggiorna($sql,$dbi,'',$num);
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);
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>";
124
125
126if(controllo($prefix.'_config','secret',++$num))
127{
128 $sql="alter table `".$prefix."_config` DROP `secret`";
129 $ret=aggiorna($sql,$dbi,'',$num);
130} else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_config non richiede questo aggiornamento</span><br>";
131
132if(controllo($prefix.'_ele_voti_parziale','data',++$num))
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);
136 echo "<br>";
137}
138++$num;
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 ' '";
140$ret=aggiorna($sql,$dbi,'',$num);
141echo "<br>";
142if(!$ret) echo "Il tuo sistema non necessita di questo aggiornamento, questo avviso di errore va ignorato<br>";
143++$num;
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 ' '";
145$ret=aggiorna($sql,$dbi,'',$num);
146echo "<br>";
147if(!$ret) echo "Il tuo sistema non necessita di questo aggiornamento, questo avviso di errore va ignorato<br>";
148++$num;
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 ' '";
150$ret=aggiorna($sql,$dbi,'',$num);
151echo "<br>";
152if(!$ret) echo "Il tuo sistema non necessita di questo aggiornamento, questo avviso di errore va ignorato<br>";
153
154if(controllo($prefix.'_ele_rilaff','data',++$num))
155{
156 $sql="ALTER TABLE `".$prefix."_ele_rilaff` CHANGE `data` `data` DATE NOT NULL DEFAULT '1900-01-01'";
157 $ret=aggiorna($sql,$dbi,'',$num);
158 echo "<br>";
159}
160
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);
165} else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_gruppo non richiede questo aggiornamento</span><br>";
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);
171} else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_gruppo non richiede questo aggiornamento</span><br>";
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`";
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";
180 $ret=aggiorna($sql,$dbi,'',$num);
181echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_lista non richiede questo aggiornamento</span><br>";
182}
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);
187}else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_lista non richiede questo aggiornamento</span><br>";
188
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);
193}else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_operatori non richiede questo aggiornamento</span><br>";
194
195if(!controllo($prefix.'_ele_operatori','id_sez',++$num))
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);
199}else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_operatori non richiede questo aggiornamento</span><br>";
200
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);
205}else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_voti_lista non richiede questo aggiornamento</span><br>";
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);
211}else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_voti_ref non richiede questo aggiornamento</span><br>";
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);
217}else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_voti_candidati non richiede questo aggiornamento</span><br>";
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);
223}else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_candidati non richiede questo aggiornamento</span><br>";
224
225if(!controllo($prefix.'_ele_sezioni','colore',++$num))
226{
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{
230 $sql="ALTER TABLE `".$prefix."_ele_sezioni` CHANGE `colore` `colore` VARCHAR(50) DEFAULT '#FAFAD2'";
231 $ret=aggiorna($sql,$dbi,'',$num);
232} echo "<br>";
233
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);
241}else echo "<br><span style=\"color: green;\">- La tabella ".$prefix."_ele_controlli non richiede questo aggiornamento</span><br>";
242
243echo "<br>".++$num.") Eliminazione della vecchia tabella dei controlli: ";
244if(controllo($prefix.'_ele_controllisez','',$num))
245{
246$sql="DROP TABLE if exists `".$prefix."_ele_controllisez`";
247$ret=aggiorna($sql,$dbi,'',$num);
248} else echo "<br><span style=\"color: green;\">- Tabella non presente</span><br>";
249
250echo "<br>".++$num.") Aggiornamento tabella ".$prefix."_ele_voti_gruppo";
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;";
252$ret=aggiorna($sql,$dbi,'',$num);
253
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
258echo "<br><br>".++$num.") Aggiornamento tabella ".$prefix."_ele_voti_lista";
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;";
260$ret=aggiorna($sql,$dbi,'',$num);
261##############################################
262
263echo "<br><br>".++$num.") Aggiornamento dei valori di default: ".$prefix."_ele_gruppo";
264$sql="ALTER TABLE `".$prefix."_ele_gruppo` CHANGE `num_circ` `num_circ` INT(2) UNSIGNED NOT NULL DEFAULT '1';";
265$ret=aggiorna($sql,$dbi,'',$num);
266
267$sql="ALTER TABLE `soraldo_authors` CHANGE `adminsuper` `adminsuper` TINYINT(2) NOT NULL DEFAULT '0';";
268$ret=aggiorna($sql,$dbi,'',$num);
269
270$sql="ALTER TABLE `".$prefix."_ele_lista` CHANGE `num_gruppo` `num_gruppo` INT(2) UNSIGNED NOT NULL DEFAULT '0';";
271$ret=aggiorna($sql,$dbi,'',$num);
272
273$sql="ALTER TABLE `".$prefix."_ele_lista` CHANGE `num_circ` `num_circ` INT(2) UNSIGNED NOT NULL DEFAULT '1';";
274$ret=aggiorna($sql,$dbi,'',$num);
275
276echo "<br><br>".++$num.") Ricostruzione della tabella ".$prefix."_ele_fasce`";
277$sql="DROP TABLE `".$prefix."_ele_fasce`";
278$ret=aggiorna($sql,$dbi,'',$num);
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
285) ENGINE=MyISAM DEFAULT CHARSET 'utf8';";
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);";
356$ret=aggiorna($sql,$dbi,$sql2,$num);
357#if(!$ret) echo "<br>".$num++.") Fallito: $sql"; else echo "<br>".$num++.") Aggiornato<br>";
358
359$sql="ALTER TABLE `".$prefix."_ele_fasce`
360 ADD KEY `id_fascia` (`id_fascia`);";
361$ret=aggiorna($sql,$dbi,'',$num);
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";
372#$sql="ALTER TABLE `".$prefix."_ele_voti_lista` DROP INDEX if exists `id_cons`";
373echo "<br><br>".++$num.") Ricostruzione e aggioramento indici";
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";
378$ret=aggiorna_index($tab, $ind,$dbi,$sql2,$num);
379
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
385$tab=$prefix."_ele_voti_lista";
386$ind="id_cons";
387$sql2="ALTER TABLE `".$prefix."_ele_voti_lista` ADD INDEX `id_cons` (`id_cons`, `id_sez`, `id_lista`) USING BTREE";
388$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
389
390$tab=$prefix."_ele_voti_gruppo";
391$ind="id_cons";
392#$sql="ALTER TABLE `".$prefix."_ele_voti_gruppo` DROP INDEX if exists `id_cons`";
393$sql2="ALTER TABLE `".$prefix."_ele_voti_gruppo` ADD INDEX `id_cons` (`id_cons`, `id_sez`, `id_gruppo`) USING BTREE";
394$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
395
396$tab=$prefix."_ele_voti_candidati";
397$ind="id_cons";
398#$sql="ALTER TABLE `".$prefix."_ele_voti_candidati` DROP INDEX if exists `id_cons`";
399$sql2="ALTER TABLE `".$prefix."_ele_voti_candidati` ADD INDEX `id_cons` (`id_cons`, `id_sez`, `id_cand`) USING BTREE";
400$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
401
402$tab=$prefix."_ele_lista";
403$ind="id_cons";
404#$sql="ALTER TABLE `".$prefix."_ele_lista` DROP INDEX if exists `id_cons`";
405$sql2="ALTER TABLE `".$prefix."_ele_lista` ADD INDEX `id_cons` (`id_cons`, `id_gruppo`) USING BTREE";
406$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
407
408$tab=$prefix."_ele_lista";
409$ind="PRIMARY";
410#$sql="ALTER TABLE `".$prefix."_ele_lista` DROP INDEX if exists `PRIMARY`";
411$sql2="ADD PRIMARY KEY (`id_lista`) USING BTREE";
412$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
413
414$tab=$prefix."_ele_gruppo";
415$ind="id_cons";
416#$sql="ALTER TABLE `".$prefix."_ele_gruppo` DROP INDEX if exists `id_cons`";
417$sql2="ALTER TABLE `".$prefix."_ele_gruppo` ADD INDEX `id_cons` (`id_cons`, `id_circ`) USING BTREE";
418$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
419
420$tab=$prefix."_ele_gruppo";
421$ind="PRIMARY";
422#$sql="ALTER TABLE `".$prefix."_ele_gruppo` DROP INDEX if exists `PRIMARY`";
423$sql2="ADD PRIMARY KEY (`id_gruppo`) USING BTREE";
424$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
425
426$tab=$prefix."_ele_candidati";
427$ind="id_cons";
428#$sql="ALTER TABLE `".$prefix."_ele_candidati` DROP INDEX if exists `id_cons`";
429$sql2="ALTER TABLE `".$prefix."_ele_candidati` ADD INDEX `id_cons` (`id_cons`, `id_lista`) USING BTREE";
430$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
431
432$tab=$prefix."_ele_candidati";
433$ind="PRIMARY";
434#$sql="ALTER TABLE `".$prefix."_ele_candidati` DROP INDEX if exists `PRIMARY`";
435$sql2="ADD PRIMARY KEY (`id_cand`) USING BTREE";
436$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
437
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))";
442$ret=aggiorna_index($tab,$ind,$dbi,$sql2,$num);
443
444echo "<br><br>".++$num.") Modifica Charset del database";
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'";
455#$res = $dbi->prepare("$sql");
456#$res->execute();
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%')";
459$res = $dbi->prepare("$sql");
460$res->execute();
461$tab='';
462$agg=$res->rowCount();
463
464while(list($nometab,$campo,$def,$tipo,$nul)=$res->fetch(PDO::FETCH_NUM)) {
465 if($tab!=$nometab){
466 $sql="alter table $nometab DEFAULT CHARSET=$cset COLLATE $ccollate";
467 try{
468 $res2 = $dbi->prepare("$sql");
469 $res2->execute();
470 }
471 catch(PDOException $e)
472 {
473 die( $sql . "<br>" . $e->getMessage());
474 }
475 $tab=$nometab; echo "<br><span style=\"color: green;\">- Tabella: $nometab</span>";
476 }
477 if($def!='') $default="DEFAULT '$def'"; else $default='';
478 if($nul=='NO') $nullable='NOT NULL'; else $nullable='NULL';
479 $sql="ALTER TABLE $nometab CHANGE $campo $campo $tipo CHARACTER SET '$cset' COLLATE '$ccollate' $nullable $default;";
480 try{
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>";
498}
499 $sql="SELECT table_name FROM INFORMATION_SCHEMA.tables WHERE TABLE_SCHEMA = '$dbname' and table_collation like '$preset%'";
500 $res = $dbi->prepare("$sql");
501 $res->execute();
502$tab='';
503if(!$agg) $agg=$res->rowCount();
504while(list($nometab)=$res->fetch(PDO::FETCH_NUM)) {
505 $sql="alter table $nometab DEFAULT CHARSET=$cset COLLATE $ccollate";
506 try{
507 $res2 = $dbi->prepare("$sql");
508 $res2->execute();
509 }
510 catch(PDOException $e)
511 {
512 die( $sql . "<br>" . $e->getMessage());
513 }
514 echo "<br><span style=\"color: green;\">- Tabella: $nometab</span>";
515}
516if(!$agg) echo "<br><span style=\"color: green;\">- Nessuna tabella da aggiornare</span>";
517echo "<br><br>";
518?>
Note: See TracBrowser for help on using the repository browser.