MySQL Compatibility Layer for PHP SDK
MySQL Compatibility Layer for PHP SDK
Section titled “MySQL Compatibility Layer for PHP SDK”This document describes the PDO and MySQLi compatibility layers for the WorkerSQL PHP SDK, enabling drop-in replacement for WordPress, Laravel, and other PHP applications.
Overview
Section titled “Overview”The WorkerSQL PHP SDK now includes two MySQL compatibility layers:
- PDO Driver (
WorkerSQL\PDO\WorkerSQLPDO
) - PDO-compatible interface - MySQLi Driver (
WorkerSQL\MySQLi\WorkerSQLMySQLi
) - MySQLi-compatible interface
Both layers wrap the WorkerSQL HTTP client and provide familiar MySQL interfaces while operating over the edge network.
PDO Compatibility
Section titled “PDO Compatibility”Installation
Section titled “Installation”<?phpuse WorkerSQL\PDO\WorkerSQLPDO;
// Create connection
Usage with WordPress
Section titled “Usage with WordPress”Update wp-config.php
:
<?php// Before (standard MySQL)// define( 'DB_NAME', 'database_name_here' );// define( 'DB_USER', 'username_here' );// define( 'DB_PASSWORD', 'password_here' );// define( 'DB_HOST', 'localhost' );
// After (WorkerSQL)
// Create custom db.php drop-in// File: wp-content/db.phprequire_once ABSPATH . 'vendor/autoload.php';use WorkerSQL\PDO\WorkerSQLPDO;
class wpdb extends wpdb { public function db_connect( $allow_bail = true ) { $this->dbh = new WorkerSQLPDO(DB_DSN); return true; }}
PDO Methods Supported
Section titled “PDO Methods Supported”prepare(string $query): WorkerSQLPDOStatement
- Prepare a statementexec(string $query): int
- Execute and return affected rowsquery(string $query): WorkerSQLPDOStatement
- Execute and return statementbeginTransaction(): bool
- Start transactioncommit(): bool
- Commit transactionrollBack(): bool
- Rollback transactionlastInsertId(): string
- Get last insert IDgetAttribute(int $attribute): mixed
- Get attributesetAttribute(int $attribute, mixed $value): bool
- Set attributequote(string $string): string
- Quote string
PDOStatement Methods Supported
Section titled “PDOStatement Methods Supported”execute(?array $params = null): bool
- Execute prepared statementfetch(?int $mode = null): mixed
- Fetch next rowfetchAll(?int $mode = null): array
- Fetch all rowsfetchColumn(int $column = 0): mixed
- Fetch single columnfetchObject(?string $class = "stdClass"): object|false
- Fetch as objectrowCount(): int
- Get affected row countcolumnCount(): int
- Get column count
MySQLi Compatibility
Section titled “MySQLi Compatibility”Installation
Section titled “Installation”<?phpuse WorkerSQL\MySQLi\WorkerSQLMySQLi;
// Create connection
Usage with WordPress
Section titled “Usage with WordPress”WordPress can use MySQLi via the wpdb abstraction. Update the db.php drop-in:
<?phprequire_once ABSPATH . 'vendor/autoload.php';use WorkerSQL\MySQLi\WorkerSQLMySQLi;
class wpdb extends wpdb { public function db_connect( $allow_bail = true ) { $this->dbh = new WorkerSQLMySQLi(DB_DSN); $this->use_mysqli = true; return true; }}
MySQLi Methods Supported
Section titled “MySQLi Methods Supported”query(string $query): WorkerSQLMySQLiResult|bool
- Execute queryprepare(string $query): WorkerSQLMySQLiStmt|false
- Prepare statementreal_query(string $query): bool
- Execute query (no result)begin_transaction(): bool
- Start transactioncommit(): bool
- Commit transactionrollback(): bool
- Rollback transactionreal_escape_string(string $string): string
- Escape stringinsert_id(): int|string
- Get last insert IDaffected_rows(): int|string
- Get affected rowserrno(): int
- Get error numbererror(): string
- Get error messageclose(): bool
- Close connection
MySQLi Statement Methods
Section titled “MySQLi Statement Methods”bind_param(string $types, mixed ...$vars): bool
- Bind parametersexecute(): bool
- Execute prepared statementget_result(): WorkerSQLMySQLiResult|false
- Get result setaffected_rows(): int
- Get affected rowsinsert_id(): int
- Get last insert IDclose(): bool
- Close statement
MySQLi Result Methods
Section titled “MySQLi Result Methods”fetch_assoc(): ?array
- Fetch as associative arrayfetch_array(): ?array
- Fetch as both numeric and associativefetch_object(string $class = "stdClass"): ?object
- Fetch as objectfetch_all(int $mode = MYSQLI_ASSOC): array
- Fetch all rowsnum_rows(): int
- Get row countfree(): void
- Free result memory
Transaction Support
Section titled “Transaction Support”Both PDO and MySQLi support transactions via batch queries:
// PDO Example$pdo->beginTransaction();try { $stmt = $pdo->prepare("UPDATE accounts SET balance = balance - ? WHERE id = ?"); $stmt->execute([100, 1]);
$stmt = $pdo->prepare("UPDATE accounts SET balance = balance + ? WHERE id = ?"); $stmt->execute([100, 2]);
$pdo->commit();} catch (Exception $e) { $pdo->rollBack(); throw $e;}
// MySQLi Example$mysqli->begin_transaction();try { $stmt = $mysqli->prepare("UPDATE accounts SET balance = balance - ? WHERE id = ?"); $stmt->bind_param("ii", 100, 1); $stmt->execute();
$stmt = $mysqli->prepare("UPDATE accounts SET balance = balance + ? WHERE id = ?"); $stmt->bind_param("ii", 100, 2); $stmt->execute();
$mysqli->commit();} catch (Exception $e) { $mysqli->rollback(); throw $e;}
Laravel Integration
Section titled “Laravel Integration”Update config/database.php
:
'connections' => [ 'workersql' => [ 'driver' => 'workersql_pdo', 'dsn' => env('WORKERSQL_DSN'), 'prefix' => '', 'prefix_indexes' => true, ],],
// In AppServiceProvider.phpuse WorkerSQL\PDO\WorkerSQLPDO;
public function register(){ $this->app->bind('db.connector.workersql_pdo', function ($app, $config) { return new WorkerSQLPDO($config['dsn']); });}
Limitations
Section titled “Limitations”- No Native Protocol: Uses HTTP API instead of MySQL wire protocol
- Batch Transactions: Transactions are queued and executed as batch
- Limited Metadata: Some PDO/MySQLi metadata methods return limited info
- Stored Procedures: Not supported
- Multi-Query: Not supported in single call
Performance Considerations
Section titled “Performance Considerations”- Connection Pooling: Not yet implemented (planned)
- Prepared Statements: Cached on client side
- Transactions: Batched for single network round-trip
- Edge Caching: Queries benefit from edge cache when appropriate
Testing
Section titled “Testing”Run compatibility tests:
composer test:pdocomposer test:mysqli
Future Enhancements
Section titled “Future Enhancements”- Connection pooling support
- Async query execution
- Streaming result sets for large queries
- Enhanced transaction isolation levels
- Stored procedure support via HTTP endpoints
Support
Section titled “Support”For issues or questions:
- GitHub: https://github.com/healthfees-org/workersql
- Documentation: /docs/architecture/010-sdk-integration.md