-
-
Notifications
You must be signed in to change notification settings - Fork 62
/
MysqlConfig.php
207 lines (172 loc) · 5.21 KB
/
MysqlConfig.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php declare(strict_types=1);
namespace Amp\Mysql;
use Amp\Socket\ConnectContext;
use Amp\Sql\SqlConfig;
final class MysqlConfig extends SqlConfig
{
public const DEFAULT_PORT = 3306;
public const BIN_CHARSET = 255; // utf8mb4_0900_ai_ci
public const KEY_MAP = [
...parent::KEY_MAP,
'compress' => 'compression',
'useCompression' => 'compression',
'cs' => 'charset',
'localInfile' => 'local-infile',
];
public const DEFAULT_CHARSET = "utf8mb4";
public const DEFAULT_COLLATE = "utf8mb4_0900_ai_ci";
private bool $useCompression;
private bool $useLocalInfile;
private ConnectContext $context;
private string $charset;
private string $collate;
/* @var string private key to use for sha256_password auth method */
private string $key;
private ?string $sqlMode;
public static function fromString(string $connectionString, ConnectContext $context = null): self
{
$parts = self::parseConnectionString($connectionString, self::KEY_MAP);
if (!isset($parts['host'])) {
throw new \Error('Host must be provided in connection string');
}
return new self(
host: $parts['host'],
port: (int) ($parts['port'] ?? self::DEFAULT_PORT),
user: $parts['user'] ?? null,
password: $parts['password'] ?? null,
database: $parts['db'] ?? null,
context: $context,
charset: $parts['charset'] ?? self::DEFAULT_CHARSET,
collate: $parts['collate'] ?? self::DEFAULT_COLLATE,
useCompression: ($parts['compression'] ?? '') === 'on',
useLocalInfile: ($parts['local-infile'] ?? '') === 'on'
);
}
public static function fromAuthority(
string $authority,
string $user,
string $password,
?string $database = null,
?ConnectContext $context = null
): self {
[$host, $port] = \explode(':', $authority, 2) + ['', (string) self::DEFAULT_PORT];
return new self(
host: $host,
port: (int) $port,
user: $user,
password: $password,
database: $database,
context: $context,
);
}
public function __construct(
string $host,
int $port = self::DEFAULT_PORT,
?string $user = null,
?string $password = null,
?string $database = null,
?ConnectContext $context = null,
string $charset = self::DEFAULT_CHARSET,
string $collate = self::DEFAULT_COLLATE,
?string $sqlMode = null,
bool $useCompression = false,
string $key = '',
bool $useLocalInfile = false
) {
parent::__construct($host, $port, $user, $password, $database);
$this->context = $context ?? (new ConnectContext);
$this->charset = $charset;
$this->collate = $collate;
$this->sqlMode = $sqlMode;
$this->useCompression = $useCompression;
$this->key = $key;
$this->useLocalInfile = $useLocalInfile;
}
public function getConnectionString(): string
{
return $this->getHost()[0] == "/"
? 'unix:https://' . $this->getHost()
: 'tcp:https://' . $this->getHost() . ':' . $this->getPort();
}
public function isCompressionEnabled(): bool
{
return $this->useCompression;
}
public function withCompression(): self
{
$new = clone $this;
$new->useCompression = true;
return $new;
}
public function withoutCompression(): self
{
$new = clone $this;
$new->useCompression = false;
return $new;
}
public function isLocalInfileEnabled(): bool
{
return $this->useLocalInfile;
}
public function withLocalInfile(): self
{
$new = clone $this;
$new->useLocalInfile = true;
return $new;
}
public function withoutLocalInfile(): self
{
$new = clone $this;
$new->useLocalInfile = false;
return $new;
}
public function getConnectContext(): ConnectContext
{
return $this->context;
}
public function withConnectContext(ConnectContext $context): self
{
$new = clone $this;
$new->context = $context;
return $new;
}
public function getCharset(): string
{
return $this->charset;
}
public function getCollation(): string
{
return $this->collate;
}
public function withCharset(string $charset, string $collate): self
{
$new = clone $this;
$new->charset = $charset;
$new->collate = $collate;
return $new;
}
public function getSqlMode(): ?string
{
return $this->sqlMode;
}
/**
* Set the Server SQL Modes at connection time.
* @see https://dev.mysql.com/doc/refman/8.0/en/sql-mode.html
*/
public function withSqlMode(?string $sqlMode): self
{
$new = clone $this;
$new->sqlMode = $sqlMode;
return $new;
}
public function getKey(): string
{
return $this->key;
}
public function withKey(string $key): self
{
$new = clone $this;
$new->key = $key;
return $new;
}
}