-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllms.txt
More file actions
215 lines (160 loc) · 6.71 KB
/
Copy pathllms.txt
File metadata and controls
215 lines (160 loc) · 6.71 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
# PhobosFramework MySQL Driver
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`.
The driver extends `AbstractDriver` from the database layer and implements MySQL-specific functionality including:
- DSN generation with support for TCP and Unix socket connections
- MySQL-specific PDO configuration (charset, collation, strict mode)
- Connection initialization with timezone and session variables
- Database-specific operations (OPTIMIZE TABLE, ANALYZE TABLE)
- Transaction isolation level configuration
- MariaDB detection and version querying
## Architecture
### Driver Implementation
**MySQLDriver** (`src/Drivers/MySQL/MySQLDriver.php`)
This is the only class in this package. It implements `DriverInterface` from the database layer and provides:
1. **Connection Configuration**:
- `getDSN(array $config)`: Builds MySQL DSN string from configuration
- `getPDOOptions(array $config)`: Returns MySQL-specific PDO options
- `configure(PDO $pdo, array $config)`: Post-connection configuration
2. **Driver Identification**:
- `getName()`: Returns 'mysql'
- `supportsSavepoints()`: Returns true (MySQL supports nested transactions)
3. **SQL Generation**:
- `quoteIdentifier(string $identifier)`: Wraps identifiers in backticks
- `getSetIsolationLevelSQL(string $level)`: Generates SET SESSION TRANSACTION ISOLATION LEVEL SQL
4. **Database Operations**:
- `getServerVersion(PDO $pdo)`: Retrieves MySQL/MariaDB version
- `isMariaDB(PDO $pdo)`: Detects if server is MariaDB
- `optimizeTable(PDO $pdo, string $table)`: Runs OPTIMIZE TABLE
- `analyzeTable(PDO $pdo, string $table)`: Runs ANALYZE TABLE
### Configuration Structure
The driver expects this configuration format (typically in `config/database.php`):
```php
[
'driver' => 'mysql',
'host' => 'localhost',
'port' => 3306, // Optional, defaults to 3306
'database' => 'mydb',
'username' => 'user',
'password' => 'pass',
'charset' => 'utf8mb4', // Optional, defaults to utf8mb4
'collation' => 'utf8mb4_unicode_ci', // Optional
'strict' => true, // Optional, defaults to true (enables strict mode)
'timezone' => '+00:00', // Optional, sets session timezone
'unix_socket' => '/path/to/socket', // Optional, use Unix socket instead of TCP
'options' => [ // Optional, additional PDO attributes
PDO::ATTR_TIMEOUT => 5,
],
'session_variables' => [ // Optional, MySQL session variables
'sql_mode' => 'TRADITIONAL',
'wait_timeout' => 28800,
],
]
```
### Integration with Database Layer
This driver is registered in the database layer's configuration:
```php
// config/database.php
[
'drivers' => [
'mysql' => \PhobosFramework\Database\Drivers\MySQL\MySQLDriver::class,
],
'connections' => [
'mysql' => [
'driver' => 'mysql',
// ... configuration as shown above
],
],
]
```
## Common Development Commands
### Composer Operations
```bash
# Install dependencies
composer install
# Update dependencies
composer update
# Validate composer.json
composer validate
# Dump autoloader (after adding new classes)
composer dump-autoload
```
### Code Quality
```bash
# PHP syntax check
php -l src/Drivers/MySQL/MySQLDriver.php
# Check PSR-4 autoloading
composer dump-autoload --optimize
```
## Important Implementation Details
### Charset and Collation Handling
The driver sets charset and collation via `MYSQL_ATTR_INIT_COMMAND` PDO attribute. If collation is provided, it generates:
```sql
SET NAMES 'utf8mb4' COLLATE 'utf8mb4_unicode_ci'
```
### Strict Mode
By default (`strict: true`), the driver enables strict SQL mode for production safety:
```sql
SET sql_mode='STRICT_ALL_TABLES,NO_ZERO_DATE,NO_ZERO_IN_DATE'
```
This prevents:
- Invalid data insertions (silently truncated to fit column)
- Zero dates (0000-00-00)
- Division by zero returning NULL instead of error
### Unix Socket Support
For local connections, you can use Unix sockets instead of TCP for better performance:
```php
[
'unix_socket' => '/var/run/mysqld/mysqld.sock',
'database' => 'mydb',
]
```
When `unix_socket` is provided, the DSN ignores `host` and `port`.
### Identifier Quoting
MySQL uses backticks for identifiers. The driver properly escapes backticks within identifiers:
```php
`table_name`
`column``with``backticks` // column`with`backticks
```
### Transaction Isolation Levels
MySQL requires `SET SESSION TRANSACTION ISOLATION LEVEL` before starting the transaction, not within it. The driver generates the correct SQL format.
Supported levels:
- `READ UNCOMMITTED`
- `READ COMMITTED`
- `REPEATABLE READ` (MySQL default)
- `SERIALIZABLE`
## Namespace Convention
All code uses the namespace: `PhobosFramework\Database\Drivers\MySQL\`
## Dependencies
- PHP 8.3+ (uses typed properties, return types)
- `ext-pdo`: Required for PDO connections
- `mongoose-studio/phobos-framework`: ^3.0 (parent framework)
- `mongoose-studio/phobos-framework-database`: ^3.0 (database layer providing AbstractDriver)
## Related Packages
This driver is part of the Phobos Framework ecosystem:
- **phobos-framework**: Core framework with DI container, routing, HTTP layer
- **phobos-framework-database**: Abstract database layer with query builder, entities, connection management
- **phobos-framework-database-mysql**: This package - MySQL/MariaDB driver implementation
## Code Style Notes
- Follow PSR-4 autoloading
- Use type hints for all parameters and return types
- Document public methods with PHPDoc comments
- Spanish comments are acceptable (matches parent framework conventions)
- Code header includes MIT license notice and author attribution
## MySQL vs MariaDB Detection
The driver provides `isMariaDB()` method to detect MariaDB servers by checking if version string contains "mariadb". This can be useful for:
- Feature detection (MariaDB-specific features)
- Query optimization (different engines)
- Logging/monitoring (tracking database type)
## Database Maintenance Operations
The driver provides utility methods for database maintenance:
### OPTIMIZE TABLE
```php
$driver->optimizeTable($pdo, 'users');
```
Defragments tables, reclaims unused space, updates index statistics. Useful for tables with frequent DELETE/UPDATE operations.
### ANALYZE TABLE
```php
$driver->analyzeTable($pdo, 'products');
```
Updates table statistics for query optimizer. Run after significant data changes to improve query performance.
Both methods respect the driver's `quoteIdentifier()` to prevent SQL injection.