Skip to content

Commit 2049cf0

Browse files
committed
Add a CLI command to run the MySQL proxy
1 parent 5c837d5 commit 2049cf0

File tree

3 files changed

+48
-11
lines changed

3 files changed

+48
-11
lines changed

packages/wp-mysql-proxy/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# WP MySQL Proxy
2+
A MySQL proxy that bridges the MySQL wire protocol to a PDO-like interface.
3+
4+
This is a zero-dependency, pure PHP implementation of a MySQL proxy that acts as
5+
a MySQL server, accepts MySQL-native commands, and executes them using a configurable
6+
PDO-like driver. This allows MySQL-compatible clients to connect and run queries
7+
against alternative database backends over the MySQL wire protocol.
8+
9+
Combined with the **WP SQLite Driver**, this allows MySQL-based projects to run
10+
on SQLite.
11+
12+
## Usage
13+
14+
### CLI:
15+
16+
```bash
17+
$ php mysql-proxy.php [--database <path/to/db.sqlite>] [--port <port>]
18+
19+
Options:
20+
-h, --help Show this help message and exit.
21+
-d, --database=<path> The path to the SQLite database file. Default: :memory:
22+
-p, --port=<port> The port to listen on. Default: 3306
23+
```
24+
25+
### PHP:
26+
```php
27+
use WP_MySQL_Proxy\MySQL_Proxy;
28+
use WP_MySQL_Proxy\Adapter\SQLite_Adapter;
29+
30+
require_once __DIR__ . '/vendor/autoload.php';
31+
32+
$proxy = new MySQL_Proxy(
33+
new SQLite_Adapter( $db_path ),
34+
array( 'port' => $port )
35+
);
36+
$proxy->start();
37+
```

packages/wp-mysql-proxy/bin/mysql-proxy.php renamed to packages/wp-mysql-proxy/bin/wp-mysql-proxy.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
require_once __DIR__ . '/../vendor/autoload.php';
77

8-
9-
108
define( 'WP_SQLITE_AST_DRIVER', true );
119

1210
// Process CLI arguments:
@@ -15,29 +13,28 @@
1513
$opts = getopt( $shortopts, $longopts );
1614

1715
$help = <<<USAGE
18-
Usage: php mysql-proxy.php --database <path/to/db.sqlite> [--port <port>]
16+
Usage: php mysql-proxy.php [--database <path/to/db.sqlite>] [--port <port>]
1917
2018
Options:
2119
-h, --help Show this help message and exit.
22-
-d, --database=<path> The path to the SQLite database file.
23-
-p, --port=<port> The port to listen on.
20+
-d, --database=<path> The path to the SQLite database file. Default: :memory:
21+
-p, --port=<port> The port to listen on. Default: 3306
2422
2523
USAGE;
2624

25+
// Help.
2726
if ( isset( $opts['h'] ) || isset( $opts['help'] ) ) {
2827
fwrite( STDERR, $help );
2928
exit( 0 );
3029
}
3130

32-
$db_path = $opts['d'] ?? $opts['database'] ?? null;
33-
if ( null === $db_path || '' === $db_path ) {
34-
fwrite( STDERR, "Error: --database <path/to/db.sqlite> is required. Use --help for usage.\n" );
35-
exit( 1 );
36-
}
31+
// Database path.
32+
$db_path = $opts['d'] ?? $opts['database'] ?? ':memory:';
3733

34+
// Port.
3835
$port = (int) ( $opts['p'] ?? $opts['port'] ?? 3306 );
3936
if ( $port < 1 || $port > 65535 ) {
40-
fwrite( STDERR, "Error: --port must be an integer between 1 and 65535.\n" );
37+
fwrite( STDERR, "Error: --port must be an integer between 1 and 65535. Use --help for more information.\n" );
4138
exit( 1 );
4239
}
4340

packages/wp-mysql-proxy/composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{
22
"name": "wordpress/wp-mysql-proxy",
33
"type": "library",
4+
"bin": [
5+
"bin/wp-mysql-proxy.php"
6+
],
47
"scripts": {
58
"test": "phpunit"
69
},

0 commit comments

Comments
 (0)