-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMydbOptions.php
More file actions
355 lines (305 loc) · 10.9 KB
/
MydbOptions.php
File metadata and controls
355 lines (305 loc) · 10.9 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<?php
/**
* This file is part of the sshilko/php-sql-mydb package.
*
* (c) Sergei Shilko <contact@sshilko.com>
*
* MIT License
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
* @license https://opensource.org/licenses/mit-license.php MIT
*/
declare(strict_types = 1);
namespace sql;
use sql\MydbException\OptionException;
use const E_ALL;
use const E_NOTICE;
use const E_WARNING;
/**
* @author Sergei Shilko <contact@sshilko.com>
* @license https://opensource.org/licenses/mit-license.php MIT
* @see https://github.com/sshilko/php-sql-mydb
*/
class MydbOptions implements MydbOptionsInterface
{
protected const NET_CMD_BUFFER_SIZE_MIN = 4096;
protected const NET_CMD_BUFFER_SIZE_MAX = 16384;
protected const NET_READ_BUFFER_MIN = 8192;
protected const NET_READ_BUFFER_MAX = 131072;
/**
* The execution timeout ONLY APPLIES TO "SELECT" statements, seconds
* X > 0, enabled
* X = 0, not enabled.
*
* @see https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_max_execution_time
*/
protected int $serverSideSelectTimeout = 89;
/**
* MySql client connection timeout, seconds
*/
protected int $connectTimeout = 5;
protected int $errorReporting = E_ALL & ~E_WARNING & ~E_NOTICE;
/**
* The timeout in seconds for each attempt to read from the server.
* @see https://dev.mysql.com/doc/c-api/8.0/en/mysql-options.html
* @see https://github.com/php/php-src/blob/12ab4cbd00e0dae52a5db98dda6da885acb408f6/
* ext/mysqli/mysqli.c#L654
* @see https://github.com/php/php-src/blob/a03c1ed7aa2325d91595dcf9371297ab45543517/
* ext/mysqli/tests/mysqli_constants.phpt#L24
*/
protected int $readTimeout = 90;
/**
* Internal network buffer of mysqlnd.net_cmd_buffer_size bytes for every connection
*
* Scope: connection.
*
* Number of network command buffer extensions while sending commands from PHP to MySQL.
*
* mysqlnd allocates an internal command/network buffer of mysqlnd.net_cmd_buffer_size (php.ini) bytes
* for every connection.
* If a MySQL Client Server protocol command, for example, COM_QUERY ("normal" query),
* does not fit into the buffer, mysqlnd will grow the buffer to what is needed for sending the command.
* Whenever the buffer gets extended for one connection command_buffer_too_small will be incremented by one.
*
* If mysqlnd has to grow the buffer beyond its initial size of mysqlnd.net_cmd_buffer_size (php.ini) bytes
* for almost every connection, you should consider to increase the default size to avoid re-allocations.
*
* The default can set either through the php.ini setting mysqlnd.net_cmd_buffer_size
* or using mysqli_options(MYSQLI_OPT_NET_CMD_BUFFER_SIZE, int size).
*
* It is recommended to set the buffer size to no less than 4096 bytes because mysqlnd also uses
* it when reading certain communication packet from MySQL.
*
* As of PHP 5.3.2 mysqlnd does not allow setting buffers smaller than 4096 bytes.
*
* Default 4096
*
* More memory usage, in exchange for better performance
* @see mysqlnd.net_cmd_buffer_size
* @see http://php.net/manual/en/mysqlnd.config.php
*/
protected int $networkBufferSize = 6144;
/**
* More memory for better performance
*
* Maximum read chunk size in bytes when reading the body of a MySQL command packet
* The MySQL client server protocol encapsulates all its commands in packets.
* The packets consist of a small header and a body with the actual payload
*
* If a packet body is larger than mysqlnd.net_read_buffer_size bytes,
* mysqlnd has to call read() multiple times
*
* This buffer controls how many bytes mysqlnd fetches from the PHP streams with one call.
* If a result set has less than 32kB in size, mysqlnd will call the PHP streams network
* functions only once, if it is larger more calls are needed
*
* Default 32768
*
* @see mysqlnd.net_read_buffer_size
* @see http://php.net/manual/en/mysqlnd.config.php
* @see http://blog.ulf-wendel.de/2007/php-mysqlnd-saves-40-memory-finally-new-tuning-options/
*/
protected int $networkReadBuffer = 49152;
/**
* Sets mysqli error reporting mode
*
* >=8.1.0 The default value is now MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT.
* < 8.1.0 MYSQLI_REPORT_OFF.
*
* MYSQLI_REPORT_OFF Turns reporting off
* MYSQLI_REPORT_ERROR Report errors from mysqli function calls
* MYSQLI_REPORT_STRICT Throw mysqli_sql_exception for errors instead of warnings
* MYSQLI_REPORT_INDEX Report if no index or bad index was used in a query
* MYSQLI_REPORT_ALL Set all options (report all)
*
* @see https://www.php.net/manual/en/function.mysqli-report.php
*/
protected int $clientErrorLevel = MydbMysqli::MYSQLI_REPORT_ALL ^
MydbMysqli::MYSQLI_REPORT_STRICT ^
MydbMysqli::MYSQLI_REPORT_INDEX;
/**
* Transaction isolation is one of the foundations of database processing.
* Isolation is the I in the acronym ACID; the isolation level is the setting that fine-tunes
* the balance between performance and reliability, consistency, and reproducibility of results
* when multiple transactions are making changes and performing queries at the same time.
*
* @see https://dev.mysql.com/doc/refman/8.0/en/innodb-transaction-isolation-levels.html
*/
protected ?string $transactionIsolationLevel = null;
/**
* Set session time zone
*
* SET time_zone = timezone;
*
* - As the value 'SYSTEM', indicating that the server time zone is the same as the system time zone.
* - As a string, an offset from UTC of the form [H]H:MM, prefixed with a + or -, such as '+10:00', '-6:00'
* Prior to MySQL 8.0.19, this value had to be in the range '-12:59' to '+13:00'
* - As a named time zone, such as 'Europe/Helsinki', 'US/Eastern', 'MET', or 'UTC'.
*
* @see https://dev.mysql.com/doc/refman/8.0/en/time-zone-support.html
*/
protected string $timeZone = 'UTC';
/**
* The number of seconds the server waits for activity
* on a non-interactive TCP/IP or UNIX File connection before closing it
*/
protected int $nonInteractiveTimeout = 7200;
/**
* Recommended defaults:
* false for rw connection
* true for ro connection
* true for async connection
*/
protected bool $autocommit = false;
protected string $charset = 'utf8mb4';
/**
* Transaction block will also carry over to the next script
* which uses that connection if script execution ends before the transaction block does
*
* @see http://php.net/manual/en/features.persistent-connections.php
*/
protected bool $persistent = false;
/**
* Readonly connection
*/
protected bool $readonly = false;
public function getNonInteractiveTimeout(): int
{
return $this->nonInteractiveTimeout;
}
public function setNonInteractiveTimeout(int $nonInteractiveTimeout): void
{
$this->nonInteractiveTimeout = $nonInteractiveTimeout;
}
public function getServerSideSelectTimeout(): int
{
return $this->serverSideSelectTimeout;
}
public function setServerSideSelectTimeout(int $seconds): void
{
$this->serverSideSelectTimeout = $seconds;
}
public function getConnectTimeout(): int
{
return $this->connectTimeout;
}
public function setConnectTimeout(int $seconds): void
{
$this->connectTimeout = $seconds;
}
public function getErrorReporting(): int
{
return $this->errorReporting;
}
public function setErrorReporting(int $errorReporting): void
{
$this->errorReporting = $errorReporting;
}
public function getReadTimeout(): int
{
return $this->readTimeout;
}
public function setReadTimeout(int $seconds): void
{
$this->readTimeout = $seconds;
}
public function getNetworkBufferSize(): int
{
return $this->networkBufferSize;
}
/**
* @param int $bytes bytes
* @throws \sql\MydbException\OptionException
*/
public function setNetworkBufferSize(int $bytes): void
{
if ($bytes < self::NET_CMD_BUFFER_SIZE_MIN || $bytes > self::NET_CMD_BUFFER_SIZE_MAX) {
throw new OptionException();
}
$this->networkBufferSize = $bytes;
}
public function getNetworkReadBuffer(): int
{
return $this->networkReadBuffer;
}
/**
* @throws \sql\MydbException\OptionException
*/
public function setNetworkReadBuffer(int $bytes): void
{
if ($bytes < self::NET_READ_BUFFER_MIN || $bytes > self::NET_READ_BUFFER_MAX) {
throw new OptionException();
}
$this->networkReadBuffer = $bytes;
}
public function getClientErrorLevel(): int
{
return $this->clientErrorLevel;
}
/**
* @throws \sql\MydbException\OptionException
*/
public function setClientErrorLevel(int $mysqliReport): void
{
if ($mysqliReport > 255 || $mysqliReport < 0) {
throw new OptionException();
}
$this->clientErrorLevel = $mysqliReport;
}
public function getTimeZone(): string
{
return $this->timeZone;
}
public function setTimeZone(string $timeZone): void
{
$this->timeZone = $timeZone;
}
public function isAutocommit(): bool
{
return $this->autocommit;
}
public function setAutocommit(bool $autocommit): void
{
$this->autocommit = $autocommit;
}
public function getCharset(): string
{
return $this->charset;
}
public function setCharset(string $charset): void
{
$this->charset = $charset;
}
public function isPersistent(): bool
{
return $this->persistent;
}
public function setPersistent(bool $persistent): void
{
$this->persistent = $persistent;
}
public function isReadonly(): bool
{
return $this->readonly;
}
public function setReadonly(bool $readonly): void
{
$this->readonly = $readonly;
}
public function getTransactionIsolationLevel(): ?string
{
$userLevel = $this->transactionIsolationLevel;
if (null === $userLevel && $this->isReadonly()) {
/**
* Optimal defaults for readonly connection
*/
return self::TRANSACTION_ISOLATION_LEVEL_READ_COMMITTED;
}
return $userLevel;
}
public function setTransactionIsolationLevel(string $isolationLevel): void
{
$this->transactionIsolationLevel = $isolationLevel;
}
}