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
|
|
|
|