|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Lagdo\DbAdmin\Driver\MySql\Db\MySqli; |
|
4
|
|
|
|
|
5
|
|
|
use Lagdo\DbAdmin\Driver\Db\Connection as AbstractConnection; |
|
6
|
|
|
|
|
7
|
|
|
use MySQLi; |
|
8
|
|
|
|
|
9
|
|
|
use function mysqli_report; |
|
10
|
|
|
use function explode; |
|
11
|
|
|
use function is_numeric; |
|
12
|
|
|
use function intval; |
|
13
|
|
|
use function ini_get; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* MySQL driver to be used with the mysqli PHP extension. |
|
17
|
|
|
*/ |
|
18
|
|
|
class Connection extends AbstractConnection |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @inheritDoc |
|
22
|
|
|
*/ |
|
23
|
|
|
public function open(string $database, string $schema = '') |
|
24
|
|
|
{ |
|
25
|
|
|
$server = $this->driver->options('server'); |
|
26
|
|
|
$options = $this->driver->options(); |
|
27
|
|
|
$username = $options['username']; |
|
28
|
|
|
$password = $options['password']; |
|
29
|
|
|
$socket = null; |
|
30
|
|
|
|
|
31
|
|
|
// Create the MySQLi client |
|
32
|
|
|
$this->client = new MySQLi(); |
|
33
|
|
|
|
|
34
|
|
|
mysqli_report(MYSQLI_REPORT_OFF); // stays between requests, not required since PHP 5.3.4 |
|
35
|
|
|
list($host, $port) = explode(":", $server, 2); // part after : is used for port or socket |
|
36
|
|
|
$ssl = $this->driver->options('ssl'); |
|
37
|
|
|
if ($ssl) { |
|
38
|
|
|
$this->client->ssl_set($ssl['key'], $ssl['cert'], $ssl['ca'], '', ''); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
if (!@$this->client->real_connect( |
|
42
|
|
|
($server != "" ? $host : ini_get("mysqli.default_host")), |
|
43
|
|
|
($server . $username != "" ? $username : ini_get("mysqli.default_user")), |
|
44
|
|
|
($server . $username . $password != "" ? $password : ini_get("mysqli.default_pw")), |
|
45
|
|
|
$database, |
|
46
|
|
|
(is_numeric($port) ? intval($port) : intval(ini_get("mysqli.default_port"))), |
|
47
|
|
|
(!is_numeric($port) ? $port : $socket), |
|
48
|
|
|
($ssl ? MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT : 0) // (not available before PHP 5.6.16) |
|
49
|
|
|
)) { |
|
50
|
|
|
return false; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$this->client->options(MYSQLI_OPT_LOCAL_INFILE, false); |
|
54
|
|
|
if (($database)) { |
|
55
|
|
|
$this->client->select_db($database); |
|
56
|
|
|
} |
|
57
|
|
|
// Available in MySQLi since PHP 5.0.5 |
|
58
|
|
|
$this->setCharset($this->driver->charset()); |
|
59
|
|
|
$this->query("SET sql_quote_show_create = 1, autocommit = 1"); |
|
60
|
|
|
return true; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @inheritDoc |
|
65
|
|
|
*/ |
|
66
|
|
|
public function serverInfo() |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->client->server_info; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @inheritDoc |
|
73
|
|
|
*/ |
|
74
|
|
|
public function setCharset(string $charset) |
|
75
|
|
|
{ |
|
76
|
|
|
if ($this->client->set_charset($charset)) { |
|
77
|
|
|
return; |
|
78
|
|
|
} |
|
79
|
|
|
// the client library may not support utf8mb4 |
|
80
|
|
|
$this->client->set_charset('utf8'); |
|
81
|
|
|
return $this->client->query("SET NAMES $charset"); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @inheritDoc |
|
86
|
|
|
*/ |
|
87
|
|
|
public function query(string $query, bool $unbuffered = false) |
|
88
|
|
|
{ |
|
89
|
|
|
$result = $this->client->query($query, $unbuffered); |
|
90
|
|
|
return ($result) ? new Statement($result) : false; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @inheritDoc |
|
95
|
|
|
*/ |
|
96
|
|
|
public function result(string $query, int $field = -1) |
|
97
|
|
|
{ |
|
98
|
|
|
if ($field < 0) { |
|
99
|
|
|
$field = $this->defaultField(); |
|
100
|
|
|
} |
|
101
|
|
|
$result = $this->client->query($query); |
|
102
|
|
|
if (!$result) { |
|
103
|
|
|
return null; |
|
104
|
|
|
} |
|
105
|
|
|
$row = $result->fetch_array(); |
|
106
|
|
|
return count($row) > $field ? $row[$field] : null; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @inheritDoc |
|
111
|
|
|
*/ |
|
112
|
|
|
public function quote(string $string) |
|
113
|
|
|
{ |
|
114
|
|
|
return "'" . $this->client->escape_string($string) . "'"; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @inheritDoc |
|
119
|
|
|
*/ |
|
120
|
|
|
public function multiQuery(string $query) |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->client->multi_query($query); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @inheritDoc |
|
127
|
|
|
*/ |
|
128
|
|
|
public function storedResult() |
|
129
|
|
|
{ |
|
130
|
|
|
$result = $this->client->store_result(); |
|
131
|
|
|
if (!$result) { // The resultset is empty |
|
132
|
|
|
// Error or no result |
|
133
|
|
|
$this->driver->setError($this->client->error); |
|
134
|
|
|
$this->setAffectedRows($this->client->affected_rows); |
|
135
|
|
|
return $this->client->error === ''; |
|
136
|
|
|
} |
|
137
|
|
|
return new Statement($result); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @inheritDoc |
|
142
|
|
|
*/ |
|
143
|
|
|
public function nextResult() |
|
144
|
|
|
{ |
|
145
|
|
|
$this->driver->setError(); |
|
146
|
|
|
$this->setAffectedRows(0); |
|
147
|
|
|
return $this->client->next_result(); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|