Driver_PDO_MYSQL   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 126
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 30 4
A limits() 0 6 1
A replace() 0 20 2
B info() 0 31 2
1
<?php
2
3
/**
4
 * Adds support for MySQL databases
5
 *
6
 * PHP Version 5
7
 *
8
 * @category  Core
9
 * @package   Database
10
 * @author    Hans-Joachim Piepereit <[email protected]>
11
 * @copyright 2013 cSphere Team
12
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
13
 * @link      http://www.csphere.eu
14
 **/
15
16
namespace csphere\core\database;
17
18
/**
19
 * Adds support for MySQL databases
20
 *
21
 * @category  Core
22
 * @package   Database
23
 * @author    Hans-Joachim Piepereit <[email protected]>
24
 * @copyright 2013 cSphere Team
25
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
26
 * @link      http://www.csphere.eu
27
 **/
28
29
class Driver_PDO_MYSQL extends Base_PDO
30
{
31
    /**
32
     * Creates the database handler object
33
     *
34
     * @param array $config Configuration details as an array
35
     *
36
     * @throws \Exception
37
     *
38
     * @return \csphere\core\database\Driver_PDO_MYSQL
39
     **/
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
40
41
    public function __construct(array $config)
42
    {
43
        parent::__construct($config);
44
45
        if (!extension_loaded('pdo_mysql')) {
46
47
            throw new \Exception('Extension "pdo_mysql" not found');
48
        }
49
50
        $dsn = empty($config['host']) ? '' :
51
               'host=' . $config['host'] . ';';
52
53
        $dsn .= 'dbname=' . $config['schema'];
54
55
        $options = [\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'];
56
57
        // Use try catch to hide connection details
58
        try {
59
60
            $this->con = new \PDO(
61
                'mysql:' . $dsn, $config['username'], $config['password'], $options
62
            );
63
64
            $this->con->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
65
        }
66
        catch(\PDOException $pdo_error) {
67
68
            $this->error('Connect', [], $pdo_error->getMessage(), false);
69
        }
70
    }
71
72
    /**
73
     * Builds the query string part for limit and offset
74
     *
75
     * @param integer $first Number of the first dataset to show
76
     * @param integer $max   Number of datasets to show from first on
77
     *
78
     * @return string
79
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
80
81
    protected function limits($first, $max)
82
    {
83
        $string = 'LIMIT ' . (int)$first . ',' . (int)$max;
84
85
        return $string;
86
    }
87
88
    /**
89
     * Replaces driver specific query placeholders
90
     *
91
     * @param string $replace The string to use for replaces
92
     *
93
     * @return string
94
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
95
96
    protected function replace($replace)
97
    {
98
        // Use InnoDB as default storage engine
99
        $engine = ' ENGINE=innodb DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci';
100
101
        $change_mysql = ['{engine}' => $engine,
102
                         '{integer}' => 'integer',
103
                         '{optimize}' => 'OPTIMIZE TABLE',
104
                         '{serial}' => 'integer NOT NULL auto_increment',
105
                         '{text}' => 'text',
106
                         '{longtext}' => 'longtext',
107
                         '{varchar}' => 'varchar'];
108
109
        foreach ($change_mysql AS $key => $mysql) {
110
111
            $replace = str_replace($key, $mysql, $replace);
112
        }
113
114
        return $replace;
115
    }
116
117
    /**
118
     * Returns a formatted array with statistics
119
     *
120
     * @return array
121
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
122
123
    public function info()
124
    {
125
        // Get general PDO information
126
        $info = parent::info();
127
128
        // Get encoding of database
129
        $query = 'SHOW VARIABLES WHERE Variable_name = \'character_set_server\'';
130
131
        $encoding = $this->query($query, [], 0, 0);
132
133
        $info['encoding'] = $encoding[0]['Value'];
134
135
        // Get size of database and amount of tables
136
        $query = 'SHOW TABLE STATUS LIKE \''
137
               . $this->config['prefix'] . '%\'';
138
139
        $tables = $this->query($query, [], 0, 0);
140
141
        $info['tables'] = count($tables);
142
        $info['size']   = 0;
143
144
        foreach ($tables AS $table) {
145
146
            $info['size'] += $table['Data_length'];
147
        }
148
149
        // Shorten mysqlnd client version
150
        $info['client'] = explode('$Id', $info['client'])[0];
151
152
        return $info;
153
    }
154
}
155