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

AbstractMysqlDriver   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 78.26%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 67
ccs 18
cts 23
cp 0.7826
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A disableInnoDbStats() 0 20 4
A restoreInnoDbStats() 0 8 2
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