Skip to content

Commit 4d1c167

Browse files
committed
first commit
1 parent 877bfa5 commit 4d1c167

27 files changed

+2774
-1
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/runtime
2+
/.idea
3+
/.vscode
4+
/vendor
5+
*.log
6+
.env
7+
/tests/tmp
8+
/tests/.phpunit.result.cache

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
same "printed page" as the copyright notice for easier
187187
identification within third-party archives.
188188

189-
Copyright [yyyy] [name of copyright owner]
189+
Copyright 2025 workbunny [https://github.com/workbunny]
190190

191191
Licensed under the Apache License, Version 2.0 (the "License");
192192
you may not use this file except in compliance with the License.

composer.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "workbunny/mysql-protocol",
3+
"type": "library",
4+
"license": "Apache-2.0",
5+
"description": "The MySQL protocol implemented by PHP",
6+
"authors": [
7+
{
8+
"name": "chaz6chez",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"require": {
13+
"php": ">=8.1"
14+
},
15+
"autoload": {
16+
"psr-4": {
17+
"Workbunny\\MysqlProtocol\\": "src",
18+
"Protocols\\" : "protocols"
19+
}
20+
},
21+
"suggest": {
22+
"workerman/workerman": "workerman implementation supporting the MySQL protocol"
23+
}
24+
}

protocols/MySQL.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Protocols;
6+
7+
use nWorkbunny\MysqlProtocol\Utils\Binary;
8+
use nWorkbunny\MysqlProtocol\Utils\Packet;
9+
use Workerman\Connection\ConnectionInterface;
10+
use Workerman\Worker;
11+
12+
class MySQL
13+
{
14+
15+
/**
16+
* Check the integrity of the package.
17+
* Please return the length of package.
18+
* If length is unknown please return 0 that means waiting for more data.
19+
* If the package has something wrong please return -1 the connection will be closed.
20+
*
21+
* @param string $buffer
22+
* @param ConnectionInterface $connection
23+
* @return int
24+
*/
25+
public static function input(string $buffer, ConnectionInterface $connection): int
26+
{
27+
try {
28+
$data = Packet::parser(function (Binary $binary) {
29+
return [];
30+
}, $binary = new Binary($buffer));
31+
return 4 + $data['packet_length'];
32+
} catch (\Throwable $throwable) {
33+
Worker::safeEcho("Error: {$throwable->getMessage()}\n");
34+
$connection->close();
35+
return 0;
36+
}
37+
}
38+
39+
/**
40+
* Decode package and emit onMessage($message) callback, $message is the result that decode returned.
41+
*
42+
* @param string $buffer
43+
* @param ConnectionInterface $connection
44+
* @return Binary|null
45+
*/
46+
public static function decode(string $buffer, ConnectionInterface $connection): ?Binary
47+
{
48+
try {
49+
return new Binary($buffer);
50+
} catch (\Throwable $throwable) {
51+
Worker::safeEcho("Error: {$throwable->getMessage()}\n");
52+
$connection->close();
53+
return null;
54+
}
55+
}
56+
57+
/**
58+
* Encode package before sending to client.
59+
*
60+
* @param Binary $data
61+
* @param ConnectionInterface $connection
62+
* @return string
63+
*/
64+
public static function encode(Binary $data, ConnectionInterface $connection): string
65+
{
66+
return $data->pack();
67+
}
68+
}

src/Constants/Capabilities.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace nWorkbunny\MysqlProtocol\Constants;
6+
7+
enum Capabilities: int
8+
{
9+
// new more secure passwords
10+
case CLIENT_LONG_PASSWORD = 1;
11+
// Found instead of affected rows
12+
case CLIENT_FOUND_ROWS = 2;
13+
// Get all column flags
14+
case CLIENT_LONG_FLAG = 4;
15+
// One can specify db on connect
16+
case CLIENT_CONNECT_WITH_DB = 8;
17+
// Don't allow database.table.column
18+
case CLIENT_NO_SCHEMA = 16;
19+
// Can use compression protocol
20+
case CLIENT_COMPRESS = 32;
21+
// Odbc client
22+
case CLIENT_ODBC = 64;
23+
// Can use LOAD DATA LOCAL
24+
case CLIENT_LOCAL_FILES = 128;
25+
// Ignore spaces before '('
26+
case CLIENT_IGNORE_SPACE = 256;
27+
// New 4.1 protocol This is an interactive client
28+
case CLIENT_PROTOCOL_41 = 512;
29+
// This is an interactive client
30+
case CLIENT_INTERACTIVE = 1024;
31+
// Switch to SSL after handshake
32+
case CLIENT_SSL = 2048;
33+
// IGNORE sigpipes
34+
case CLIENT_IGNORE_SIGPIPE = 4096;
35+
// Client knows about transactions
36+
case CLIENT_TRANSACTIONS = 8192;
37+
// Old flag for 4.1 protocol
38+
case CLIENT_RESERVED = 16384;
39+
// New 4.1 authentication
40+
case CLIENT_SECURE_CONNECTION = 32768;
41+
// Enable/disable multi-stmt support
42+
case CLIENT_MULTI_STATEMENTS = 65536;
43+
// Enable/disable multi-results
44+
case CLIENT_MULTI_RESULTS = 131072;
45+
case CLIENT_PLUGIN_AUTH = 524288;
46+
case CLIENT_CONNECT_ATTRS = 1048576;
47+
case CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA = 2097152;
48+
}
49+

0 commit comments

Comments
 (0)