Passed
Push — master ( 88d705...44ae42 )
by Andrea
12:16
created

Fifree2mysqltruncatetablesCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 18.75%

Importance

Changes 0
Metric Value
dl 0
loc 53
ccs 6
cts 32
cp 0.1875
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 6 1
B execute() 0 40 5
1
<?php
2
3
namespace Fi\CoreBundle\Command;
4
5
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
class Fifree2mysqltruncatetablesCommand extends ContainerAwareCommand
11
{
12
13 3
    protected function configure()
14
    {
15 3
        $this
16 3
                ->setName('fifree2:mysqltruncatetables')
17 3
                ->setDescription('Tronca tutte le tabelle mysql')
18 3
                ->addOption('tablesfifree2', null, InputOption::VALUE_OPTIONAL, 'Si devono trattare anche le tabelle di fifree2', false)
19
        //->setHelp('Modifica il motore mysql delle tabelle')
20
        ;
21 3
    }
22
23
    protected function execute(InputInterface $input, OutputInterface $output)
24
    {
25
        set_time_limit(0);
26
        ini_set('memory_limit', '-1');
27
28
        $inizio = microtime(true);
29
        /* @var $em \Doctrine\ORM\EntityManager */
30
        $em = $this->getContainer()->get('doctrine')->getManager();
31
        $driver = $em->getConnection()->getDriver()->getName();
32
33
        if ($driver != "pdo_mysql") {
34
            $output->writeln("Non previsto per driver: " . $driver);
35
            return 1;
36
        }
37
        $tablesfifree2 = $input->getOption('tablesfifree2');
38
39
        $dbname = $this->getContainer()->get('database_connection')->getDatabase();
40
41
        $sql = "SELECT Concat('TRUNCATE TABLE ',table_schema,'.',TABLE_NAME, ';') "
42
                . 'TRUNCTABLE, TABLE_NAME FROM INFORMATION_SCHEMA.TABLES '
43
                . "where  table_schema in ('" . $dbname . "')";
44
45
        $conn = $em->getConnection();
46
        $rows = $conn->fetchAll($sql);
47
        foreach ($rows as $row) {
48
            $tbl = $row['TRUNCTABLE'];
49
            if (substr($row['TABLE_NAME'], 0, 2) == '__' && !($tablesfifree2)) {
50
                continue;
51
            }
52
53
            $sqlalter = $tbl;
54
            $output->writeln($sqlalter);
55
            $conn->exec($sqlalter);
56
        }
57
58
        $fine = microtime(true);
59
        $tempo = gmdate('H:i:s', $fine - $inizio);
60
61
        $text = 'Fine in ' . $tempo . ' secondi';
62
        $output->writeln($text);
63
    }
64
}
65