Download the PHP package toropyga/db without Composer
On this page you can find all versions of the php package toropyga/db. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package db
Short Description Libraries for working with databases from Toropyga
License MIT
Informations about the package db
DB
A lightweight PHP 8.1+ library that provides a unified interface for working with MySQL, PostgreSQL, Oracle, and PDO-supported databases. It includes prepared statements, SQL query helpers, metadata access, configurable error handling, logging, and backward compatibility with the legacy PDO_LIB adapter name.
The preferred PDO adapter name is
PDOLIB. A temporaryPDO_LIB extends PDOLIBcompatibility wrapper is still available for applications migrating from v2.x; new code should useToropyga\DB\PDOLIB.
Content
- General description
- Changelog
- Installation
- Requirements
- Configuration
- Configuration constants MySQL
- Configuration constants PostgreSQL
- Configuration constants ORACLE
- Configuration constants PDOLIB
- Work description
- Including a class file
- Classes initialisation
- Getting a list of tables
- Creating INSERT, DELETE and UPDATE queries from arrays
- Sending a request
- Unified DBAPI entry point
General description
The library includes 4 main adapters and the DBAPI factory:
- MySQL - class for working with MySQL database.
- PostgreSQL - class for working with PostgreSQL database.
- Oracle - class for working with Oracle database.
- PDOLIB - a generic class that uses the PDO library, including PostgreSQL and SQLite.
DBAPI provides a single entry point for selecting an adapter by connection
type and passing connection parameters as an array.
Functions are standardized in all libraries.
See CHANGELOG.md for release history and unreleased changes.
Changelog
Release history is maintained in CHANGELOG.md.
All adapters implement DatabaseAdapterInterface. Use getQuery() (or the
adapter's prepared execution methods) for user-controlled values. The
getInsertSQL(), getUpdateSQL(), and getDeleteSQL() methods only generate
SQL text for inspection or integration with external tooling.
Errors are written to the adapter log. When the *_ERROR_EXIT option or
setErrorExit(true) is enabled, a DatabaseException is thrown instead of
terminating the application or rendering HTML.
Installation
The recommended way to install the DB library is using Composer:
Requirements
- PHP 8.1 or newer.
ext-pdoforPDOLIB.ext-mysqliforMySQL.ext-pgsqlforPostgreSQL.ext-oci8forOracleand PDO Oracle connections.ext-pdo_pgsqlfor PostgreSQL connections throughPDOLIB.ext-pdo_sqlitefor SQLite connections throughPDOLIB.ext-pdo_sqlsrvfor Microsoft SQL Server connections throughPDOLIB.ext-pdo_dblibfor Sybase connections throughPDOLIB.ext-pdo_firebirdfor Firebird connections throughPDOLIB.ext-jsonwhen array values are encoded for SQL parameters.
Only install and enable the extensions required by the adapter you use.
Backwards-incompatible API changes
This release tightens parameter types on several public methods. Passing an
invalid value that previously returned false can now raise TypeError. The
affected methods are:
MySQL::getListFields(string $table)MySQL::setInsert(string $table, array $values)PDOLIB::prepare(string $sql, array $values = [...])PDOLIB::getListFields(string $table)Oracle::getProcedureQuery(string $package, string $procedure, ...)
Validate arguments before calling these methods and update integrations that relied on the old permissive behavior. This is an intentional backwards-incompatible API change; see CHANGELOG.md.
Support matrix
The PHP column describes the declared language compatibility from composer.json.
Database-driver combinations require both ext-pdo and the matching PDO driver.
| Adapter / driver | PHP 8.1+ | Required extensions | Status |
|---|---|---|---|
MySQL |
Yes | ext-mysqli |
Declared support |
PostgreSQL |
Yes | ext-pgsql |
Declared support |
Oracle |
Yes | ext-oci8 |
Declared support |
PDOLIB + mysql |
Yes | ext-pdo, ext-pdo_mysql |
Declared support |
PDOLIB + pgsql |
Yes | ext-pdo, ext-pdo_pgsql |
Declared support |
PDOLIB + oci |
Yes | ext-pdo, ext-pdo_oci |
Declared support |
PDOLIB + odbc |
Yes | ext-pdo, ext-pdo_odbc |
Declared support |
PDOLIB + sqlite |
Yes | ext-pdo, ext-pdo_sqlite |
Declared support |
PDOLIB + sqlsrv |
Yes | ext-pdo, ext-pdo_sqlsrv |
Declared support |
PDOLIB + dblib |
Yes | ext-pdo, ext-pdo_dblib |
Declared support |
PDOLIB + firebird |
Yes | ext-pdo, ext-pdo_firebird |
Declared support |
| JSON array values | Yes | ext-json |
Required only when arrays are encoded |
The matrix is a compatibility declaration, not a replacement for integration tests against each database server and driver version.
Configuration
Pre-setting of default parameters can be done directly in the class itself or using a named constant. Named constants are declared when the class is called, for example in a configuration file, and define default parameters.
Configuration constants PostgreSQL
Configuration constants MySQL
Configuration constants ORACLE
Configuration constants PDOLIB
Work description
Including a class file
Classes initialisation
The adapters can also be initialized with explicit constructor parameters:
Unified DBAPI entry point
DBAPI::connect() returns the concrete adapter selected by the connection
type. The instance form also proxies adapter methods:
Supported native types include mysql, postgresql, and oracle.
Supported PDO types include pdo_mysql, pdo_pgsql, pdo_oci, pdo_odbc,
pdo_sqlite, pdo_sqlsrv, pdo_dblib, and pdo_firebird.
Getting a list of tables
Creating INSERT, DELETE and UPDATE queries from arrays
Sending a request
For parameterized SELECT queries, use getQuery() instead of concatenating
values into SQL. The adapter uses real bound parameters:
getInsertSQL(), getUpdateSQL(), getDeleteSQL(), and getQuerySQL() build
SQL text for inspection or logging. For user-controlled values, prefer the
execution methods with bound parameters (getQuery(), prepare()/execute())
where the adapter supports them.
Where:
- $sql - SQL query to DB
- $one - type of return
$one can take values:
6/'dub_all' and 7/'explain' are supported by all three classes, with one exception:
Oraclehas no single-statementEXPLAIN.7/'explain'runsEXPLAIN PLAN FOR <sql>followed bySELECT ... FROM TABLE(DBMS_XPLAN.DISPLAY())under the hood and returns the formatted plan as a flat array of text lines (rather than the structured row shape the other numeric modes return).PDOLIBbehaves the same way for theocidriver type. Formysqlandpgsqlit simply runs the query with anEXPLAINprefix. For theodbcdriver type,'explain'is not supported - there is noEXPLAINsyntax that is portable across ODBC backends - and calling it logs a message and returns an empty array instead of sending unpredictable SQL to the database.
You can also execute a query without processing the result (UPDATE, INSERT, etc.):
For Oracle, ordinary SELECT statements are executed through the parsed
statement. Enable setCursor(true) only when the PL/SQL request uses an OUT
cursor such as :res; regular SELECT requests do not require cursor mode.
getListFields() returns a list of column names. Metadata lookup failures
return false and do not reuse a stale prepared statement or connection.
Use NULL or NOT NULL as a condition value to generate IS NULL or
IS NOT NULL, for example: ['deleted_at' => 'NULL'].