Completed
Push — master ( d4d376...9739b0 )
by Sébastien
05:59 queued 01:19
created

AbstractMysqlDriver::disableInnoDbStats()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.1967

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 10
cts 13
cp 0.7692
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 8
nop 0
crap 4.1967
1
<?php
2
3
namespace Soluble\Schema\Source\Mysql;
4
5
use Soluble\DbWrapper\Adapter\AdapterInterface;
6
7
abstract class AbstractMysqlDriver implements MysqlDriverInterface
8
{
9
    /**
10
     * @var AdapterInterface
11
     */
12
    protected $adapter;
13
14
    /**
15
     * Schema name.
16
     *
17
     * @var string
18
     */
19
    protected $schema;
20
21
    /**
22
     * Used to restore innodb stats mysql global variable.
23
     *
24
     * @var string
25
     */
26
    protected $mysql_innodbstats_value;
27
28
    /**
29
     * @param AdapterInterface $adapter
30
     * @param string           $schema  database name
31
     */
32 23
    public function __construct(AdapterInterface $adapter, $schema)
33
    {
34 23
        $this->adapter = $adapter;
35 23
        $this->schema = $schema;
36 23
    }
37
38
    /**
39
     * Disable innodbstats will increase speed of metadata lookups.
40
     */
41 4
    protected function disableInnoDbStats()
42
    {
43 4
        $sql = "show global variables like 'innodb_stats_on_metadata'";
44
        try {
45 4
            $results = $this->adapter->query($sql);
46 4
            if (count($results) > 0) {
47 4
                $row = $results->offsetGet(0);
48
49 4
                $value = strtoupper($row['Value']);
50
                // if 'on' no need to do anything
51 4
                if ($value != 'OFF') {
52
                    $this->mysql_innodbstats_value = $value;
53
                    // disabling innodb_stats
54
                    $this->adapter->query("set global innodb_stats_on_metadata='OFF'");
55
                }
56 4
            }
57 4
        } catch (\Exception $e) {
58
            // do nothing, silently fallback
59
        }
60 4
    }
61
62
    /**
63
     * Restore old innodbstats variable.
64
     */
65 4
    protected function restoreInnoDbStats()
66
    {
67 4
        $value = $this->mysql_innodbstats_value;
68 4
        if ($value !== null) {
69
            // restoring old variable
70
            $this->adapter->query("set global innodb_stats_on_metadata='$value'");
71
        }
72 4
    }
73
}
74