|
| 1 | +# PhobosFramework MySQL Driver |
| 2 | + |
| 3 | +This is the **PhobosFramework MySQL Driver** - a database driver implementation for the PhobosFramework Database Layer that provides MySQL and MariaDB connectivity. This is a **library package** meant to be used via Composer alongside `mongoose-studio/phobos-framework-database`. |
| 4 | + |
| 5 | +The driver extends `AbstractDriver` from the database layer and implements MySQL-specific functionality including: |
| 6 | +- DSN generation with support for TCP and Unix socket connections |
| 7 | +- MySQL-specific PDO configuration (charset, collation, strict mode) |
| 8 | +- Connection initialization with timezone and session variables |
| 9 | +- Database-specific operations (OPTIMIZE TABLE, ANALYZE TABLE) |
| 10 | +- Transaction isolation level configuration |
| 11 | +- MariaDB detection and version querying |
| 12 | + |
| 13 | +## Architecture |
| 14 | + |
| 15 | +### Driver Implementation |
| 16 | + |
| 17 | +**MySQLDriver** (`src/Drivers/MySQL/MySQLDriver.php`) |
| 18 | + |
| 19 | +This is the only class in this package. It implements `DriverInterface` from the database layer and provides: |
| 20 | + |
| 21 | +1. **Connection Configuration**: |
| 22 | + - `getDSN(array $config)`: Builds MySQL DSN string from configuration |
| 23 | + - `getPDOOptions(array $config)`: Returns MySQL-specific PDO options |
| 24 | + - `configure(PDO $pdo, array $config)`: Post-connection configuration |
| 25 | + |
| 26 | +2. **Driver Identification**: |
| 27 | + - `getName()`: Returns 'mysql' |
| 28 | + - `supportsSavepoints()`: Returns true (MySQL supports nested transactions) |
| 29 | + |
| 30 | +3. **SQL Generation**: |
| 31 | + - `quoteIdentifier(string $identifier)`: Wraps identifiers in backticks |
| 32 | + - `getSetIsolationLevelSQL(string $level)`: Generates SET SESSION TRANSACTION ISOLATION LEVEL SQL |
| 33 | + |
| 34 | +4. **Database Operations**: |
| 35 | + - `getServerVersion(PDO $pdo)`: Retrieves MySQL/MariaDB version |
| 36 | + - `isMariaDB(PDO $pdo)`: Detects if server is MariaDB |
| 37 | + - `optimizeTable(PDO $pdo, string $table)`: Runs OPTIMIZE TABLE |
| 38 | + - `analyzeTable(PDO $pdo, string $table)`: Runs ANALYZE TABLE |
| 39 | + |
| 40 | +### Configuration Structure |
| 41 | + |
| 42 | +The driver expects this configuration format (typically in `config/database.php`): |
| 43 | + |
| 44 | +```php |
| 45 | +[ |
| 46 | + 'driver' => 'mysql', |
| 47 | + 'host' => 'localhost', |
| 48 | + 'port' => 3306, // Optional, defaults to 3306 |
| 49 | + 'database' => 'mydb', |
| 50 | + 'username' => 'user', |
| 51 | + 'password' => 'pass', |
| 52 | + 'charset' => 'utf8mb4', // Optional, defaults to utf8mb4 |
| 53 | + 'collation' => 'utf8mb4_unicode_ci', // Optional |
| 54 | + 'strict' => true, // Optional, defaults to true (enables strict mode) |
| 55 | + 'timezone' => '+00:00', // Optional, sets session timezone |
| 56 | + 'unix_socket' => '/path/to/socket', // Optional, use Unix socket instead of TCP |
| 57 | + 'options' => [ // Optional, additional PDO attributes |
| 58 | + PDO::ATTR_TIMEOUT => 5, |
| 59 | + ], |
| 60 | + 'session_variables' => [ // Optional, MySQL session variables |
| 61 | + 'sql_mode' => 'TRADITIONAL', |
| 62 | + 'wait_timeout' => 28800, |
| 63 | + ], |
| 64 | +] |
| 65 | +``` |
| 66 | + |
| 67 | +### Integration with Database Layer |
| 68 | + |
| 69 | +This driver is registered in the database layer's configuration: |
| 70 | + |
| 71 | +```php |
| 72 | +// config/database.php |
| 73 | +[ |
| 74 | + 'drivers' => [ |
| 75 | + 'mysql' => \PhobosFramework\Database\Drivers\MySQL\MySQLDriver::class, |
| 76 | + ], |
| 77 | + 'connections' => [ |
| 78 | + 'mysql' => [ |
| 79 | + 'driver' => 'mysql', |
| 80 | + // ... configuration as shown above |
| 81 | + ], |
| 82 | + ], |
| 83 | +] |
| 84 | +``` |
| 85 | + |
| 86 | +## Common Development Commands |
| 87 | + |
| 88 | +### Composer Operations |
| 89 | + |
| 90 | +```bash |
| 91 | +# Install dependencies |
| 92 | +composer install |
| 93 | + |
| 94 | +# Update dependencies |
| 95 | +composer update |
| 96 | + |
| 97 | +# Validate composer.json |
| 98 | +composer validate |
| 99 | + |
| 100 | +# Dump autoloader (after adding new classes) |
| 101 | +composer dump-autoload |
| 102 | +``` |
| 103 | + |
| 104 | +### Code Quality |
| 105 | + |
| 106 | +```bash |
| 107 | +# PHP syntax check |
| 108 | +php -l src/Drivers/MySQL/MySQLDriver.php |
| 109 | + |
| 110 | +# Check PSR-4 autoloading |
| 111 | +composer dump-autoload --optimize |
| 112 | +``` |
| 113 | + |
| 114 | +## Important Implementation Details |
| 115 | + |
| 116 | +### Charset and Collation Handling |
| 117 | + |
| 118 | +The driver sets charset and collation via `MYSQL_ATTR_INIT_COMMAND` PDO attribute. If collation is provided, it generates: |
| 119 | +```sql |
| 120 | +SET NAMES 'utf8mb4' COLLATE 'utf8mb4_unicode_ci' |
| 121 | +``` |
| 122 | + |
| 123 | +### Strict Mode |
| 124 | + |
| 125 | +By default (`strict: true`), the driver enables strict SQL mode for production safety: |
| 126 | +```sql |
| 127 | +SET sql_mode='STRICT_ALL_TABLES,NO_ZERO_DATE,NO_ZERO_IN_DATE' |
| 128 | +``` |
| 129 | + |
| 130 | +This prevents: |
| 131 | +- Invalid data insertions (silently truncated to fit column) |
| 132 | +- Zero dates (0000-00-00) |
| 133 | +- Division by zero returning NULL instead of error |
| 134 | + |
| 135 | +### Unix Socket Support |
| 136 | + |
| 137 | +For local connections, you can use Unix sockets instead of TCP for better performance: |
| 138 | +```php |
| 139 | +[ |
| 140 | + 'unix_socket' => '/var/run/mysqld/mysqld.sock', |
| 141 | + 'database' => 'mydb', |
| 142 | +] |
| 143 | +``` |
| 144 | + |
| 145 | +When `unix_socket` is provided, the DSN ignores `host` and `port`. |
| 146 | + |
| 147 | +### Identifier Quoting |
| 148 | + |
| 149 | +MySQL uses backticks for identifiers. The driver properly escapes backticks within identifiers: |
| 150 | +```php |
| 151 | +`table_name` |
| 152 | +`column``with``backticks` // column`with`backticks |
| 153 | +``` |
| 154 | + |
| 155 | +### Transaction Isolation Levels |
| 156 | + |
| 157 | +MySQL requires `SET SESSION TRANSACTION ISOLATION LEVEL` before starting the transaction, not within it. The driver generates the correct SQL format. |
| 158 | + |
| 159 | +Supported levels: |
| 160 | +- `READ UNCOMMITTED` |
| 161 | +- `READ COMMITTED` |
| 162 | +- `REPEATABLE READ` (MySQL default) |
| 163 | +- `SERIALIZABLE` |
| 164 | + |
| 165 | +## Namespace Convention |
| 166 | + |
| 167 | +All code uses the namespace: `PhobosFramework\Database\Drivers\MySQL\` |
| 168 | + |
| 169 | +## Dependencies |
| 170 | + |
| 171 | +- PHP 8.3+ (uses typed properties, return types) |
| 172 | +- `ext-pdo`: Required for PDO connections |
| 173 | +- `mongoose-studio/phobos-framework`: ^3.0 (parent framework) |
| 174 | +- `mongoose-studio/phobos-framework-database`: ^3.0 (database layer providing AbstractDriver) |
| 175 | + |
| 176 | +## Related Packages |
| 177 | + |
| 178 | +This driver is part of the Phobos Framework ecosystem: |
| 179 | + |
| 180 | +- **phobos-framework**: Core framework with DI container, routing, HTTP layer |
| 181 | +- **phobos-framework-database**: Abstract database layer with query builder, entities, connection management |
| 182 | +- **phobos-framework-database-mysql**: This package - MySQL/MariaDB driver implementation |
| 183 | + |
| 184 | +## Code Style Notes |
| 185 | + |
| 186 | +- Follow PSR-4 autoloading |
| 187 | +- Use type hints for all parameters and return types |
| 188 | +- Document public methods with PHPDoc comments |
| 189 | +- Spanish comments are acceptable (matches parent framework conventions) |
| 190 | +- Code header includes MIT license notice and author attribution |
| 191 | + |
| 192 | +## MySQL vs MariaDB Detection |
| 193 | + |
| 194 | +The driver provides `isMariaDB()` method to detect MariaDB servers by checking if version string contains "mariadb". This can be useful for: |
| 195 | +- Feature detection (MariaDB-specific features) |
| 196 | +- Query optimization (different engines) |
| 197 | +- Logging/monitoring (tracking database type) |
| 198 | + |
| 199 | +## Database Maintenance Operations |
| 200 | + |
| 201 | +The driver provides utility methods for database maintenance: |
| 202 | + |
| 203 | +### OPTIMIZE TABLE |
| 204 | +```php |
| 205 | +$driver->optimizeTable($pdo, 'users'); |
| 206 | +``` |
| 207 | +Defragments tables, reclaims unused space, updates index statistics. Useful for tables with frequent DELETE/UPDATE operations. |
| 208 | + |
| 209 | +### ANALYZE TABLE |
| 210 | +```php |
| 211 | +$driver->analyzeTable($pdo, 'products'); |
| 212 | +``` |
| 213 | +Updates table statistics for query optimizer. Run after significant data changes to improve query performance. |
| 214 | + |
| 215 | +Both methods respect the driver's `quoteIdentifier()` to prevent SQL injection. |
0 commit comments