Connection::setCharset()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Lagdo\DbAdmin\Driver\MySql\Db\Pdo;
4
5
use Lagdo\DbAdmin\Driver\Db\Pdo\Connection as PdoConnection;
6
7
use PDO;
8
9
/**
10
 * MySQL driver to be used with the pdo_mysql PHP extension.
11
 */
12
class Connection extends PdoConnection
13
{
14
    /**
15
     * @inheritDoc
16
     */
17
    public function open(string $database, string $schema = '')
18
    {
19
        $server = $this->driver->options('server');
20
        $options = $this->driver->options();
21
        $username = $options['username'];
22
        $password = $options['password'];
23
        if (!$password) {
24
            $password = '';
25
        }
26
27
        $options = [PDO::MYSQL_ATTR_LOCAL_INFILE => false];
28
        $ssl = $this->driver->options('');
29
        if ($ssl) {
30
            if (!empty($ssl['key'])) {
31
                $options[PDO::MYSQL_ATTR_SSL_KEY] = $ssl['key'];
32
            }
33
            if (!empty($ssl['cert'])) {
34
                $options[PDO::MYSQL_ATTR_SSL_CERT] = $ssl['cert'];
35
            }
36
            if (!empty($ssl['ca'])) {
37
                $options[PDO::MYSQL_ATTR_SSL_CA] = $ssl['ca'];
38
            }
39
        }
40
        $this->dsn("mysql:charset=utf8;host=" . str_replace(":", ";unix_socket=",
41
            preg_replace('~:(\d)~', ';port=\1', $server)), $username, $password, $options);
42
43
        if (($database)) {
44
            $this->query("USE " . $this->driver->escapeId($database));
45
        }
46
        // Available in MySQLi since PHP 5.0.5
47
        $this->setCharset($this->driver->charset());
48
        $this->query("SET sql_quote_show_create = 1, autocommit = 1");
49
        return true;
50
    }
51
52
    /**
53
     * @inheritDoc
54
     */
55
    public function setCharset(string $charset)
56
    {
57
        $this->query("SET NAMES $charset"); // charset in DSN is ignored before PHP 5.3.6
58
    }
59
60
    /**
61
     * @inheritDoc
62
     */
63
    public function query(string $query, bool $unbuffered = false)
64
    {
65
        $this->client->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, !$unbuffered);
66
        return parent::query($query, $unbuffered);
67
    }
68
}
69