Download the PHP package tusharkhan/db-error-explainer-core without Composer
On this page you can find all versions of the php package tusharkhan/db-error-explainer-core. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download tusharkhan/db-error-explainer-core
More information about tusharkhan/db-error-explainer-core
Files in tusharkhan/db-error-explainer-core
Package db-error-explainer-core
Short Description Explains PDO database errors in plain language with a suggested SQL fix, for MySQL, MariaDB, PostgreSQL and SQLite
License MIT
Homepage https://github.com/tusharkhan/db-error-explainer-core
Informations about the package db-error-explainer-core
DB Error Explainer Core
Framework-agnostic PHP library that explains database exceptions in plain language and suggests a fix. Works with any PDO connection to MySQL / MariaDB, PostgreSQL or SQLite.
For Laravel, use tusharkhan/db-error-explainer-laravel, which wires this up for you.
Requirements
- PHP 8.4+
ext-pdowith the driver for your database
Installation
Usage
explain() never throws. If analysis itself fails you get a diagnosis with errorType analysis_failed and the original message.
The database is detected from the PDO connection (PDO::ATTR_DRIVER_NAME). When there is no working connection, for example because connecting failed, pass null and name the driver:
The diagnosis
| Property | Example |
|---|---|
errorType |
fk_missing_parent, the rule id, shared across databases |
errorCode |
1452 on MySQL, the SQLSTATE (23503) on PostgreSQL, 19 on SQLite |
sqlState |
23000 |
explanation |
Plain-language explanation with your table / column names |
migrationSuggestion['migration_suggestion'] |
SQL fix, or '' |
migrationSuggestion['other_suggestions'] |
Tip (often Laravel-specific), or '' |
context |
Extracted names and, with introspection, table_exists, table_columns, foreign_keys, unique_constraints, column_type |
originalMessage |
The database's message |
metadata |
rule_id (none when no rule matched), driver, introspection_enabled, timestamp |
Output helpers: format() (readable text), toArray(), toJson().
Options
Introspection only runs read-only queries (information_schema, pg_catalog or SQLite PRAGMAs). Turn it off if you don't want the extra queries, e.g. 'allow_introspection' => getenv('APP_ENV') !== 'production'.
Custom rules
Rules are arrays, checked after the built-in ones for the active database:
{placeholders}inexplanation(and in a stringmigration) are filled from the context: whatextractreturns plustable,column,error_codeanderror_description.migrationcan be a string template or a callable returning a string or['migration_suggestion' => ..., 'other_suggestions' => ...].- Rules are validated when loaded. A typo in a key, a missing
id, nothing to match on or an invalid regex throws anInvalidArgumentExceptionnaming the rule.
Adding a database
Implement TusharKhan\DbErrorExplainer\Drivers\DriverInterface (rules, message parser, introspector, error descriptions) and pass an instance as the driver option. The built-in drivers in src/Drivers/ are the reference.
License
MIT. See LICENSE.
All versions of db-error-explainer-core with dependencies
ext-pdo Version *
psr/log Version ^3.0
tusharkhan/db-error-explainer-lib Version ^1.0