pipe / sql
Blazingly fast php sql util
v0.2.0
2026-12-02 13:46 UTC
Requires
- php: >=8.2
Requires (Dev)
- pestphp/pest: ^3.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Simple, pure PHP PDO database helper
Installation
composer require pipe/sql
Requires PHP 8.2 or newer. No runtime dependencies.
Usage
use Pipe\SQL; $db = (new SQL())->configure('mysql:host=localhost;dbname=app;charset=utf8mb4', 'user', 'secret'); // No connection yet: it opens on the first query. $users = $db->query('SELECT * FROM users WHERE active = ?', [1])->all(); $user = $db->query('SELECT * FROM users WHERE id = :id', ['id' => $id])->first(); $count = $db->query('SELECT COUNT(*) FROM users')->one(); $db->transaction(function (SQL $db) use ($from, $to, $amount) { $db->query('UPDATE accounts SET balance = balance - ? WHERE id = ?', [$amount, $from]); $db->query('UPDATE accounts SET balance = balance + ? WHERE id = ?', [$amount, $to]); });
Always pass user input as parameters. Never put it into the SQL string.
API
| Method | Description |
|---|---|
configure(string $dsn, ?string $user = null, ?string $password = null, array $options = []): self |
Configure any PDO driver without connecting. $options are PDO attributes that override the defaults. |
connect(string $host, string $user, string $password, string $dbname, int $port = 3306, string $encoding = 'utf8mb4'): self |
Shortcut for MySQL/MariaDB that connects immediately. |
pdo(): PDO |
The connection, opened on first use. Throws LogicException when nothing is configured. |
connected(): bool |
Whether the connection is open. |
query(string $sql, array $args = []): self |
Prepare and execute, with positional (?) or named (:name) parameters. |
raw(string $sql): self |
Execute without parameters, for schema changes and maintenance. Never include user input. |
all(): array |
Every row, as objects. |
first(): mixed |
The next row, or null. |
one(): mixed |
The first column of the next row, or null. |
rowCount(): int |
Rows affected by the last INSERT, UPDATE or DELETE. |
statement(): PDOStatement |
The last statement. Throws LogicException before any query has run. |
lastInsertId(): string, lastResult(): bool |
Last insert ID / result of the last execute. |
begin(), commit(), rollBack(), inTransaction() |
Manual transactions. |
transaction(callable $callback): mixed |
Runs $callback($db): commits when it returns, rolls back and rethrows when it throws. Inside an open transaction it simply joins that transaction. |
Every query() replaces the previous statement, so fetch the results before running the next query.
Defaults and safety
PDO::ERRMODE_EXCEPTION, so errors throwPDOException, andPDO::FETCH_OBJ.- For MySQL,
PDO::ATTR_EMULATE_PREPARESis off, so prepared statements are real server-side statements.connect()usesutf8mb4. - The password parameters are marked
#[\SensitiveParameter], so they don't appear in exception stack traces, andvar_dump($db)doesn't show connection details. - Database error messages can still contain the user name, host or SQL. Don't show exception messages to visitors in production.
Testing
composer install
composer test
The SQLite tests always run. The MySQL tests run when a server is reachable and are skipped otherwise; configure them with DB_HOST, DB_USER, DB_PASS and DB_NAME (defaults: 127.0.0.1, root, pass, test).
Philosophy
- Small is Beautiful.
- Do one thing,
- Do one thing well.
- Get the PoC out ASAP.
- Consider your code dead two weeks after inception.
- n'joy!
sounds familiar? It should!