solophp/query-executor

A lightweight PDO wrapper for executing raw SQL queries with parameter binding and flexible result handling.

v1.0.0 2025-04-25 19:09 UTC

This package is auto-updated.

Last update: 2025-04-25 19:17:05 UTC


README

Version License

A lightweight PDO wrapper for executing raw SQL queries with parameter binding and flexible result handling.

Features

  • Simple, fluent API for database operations
  • Secure parameter binding for query execution
  • Transaction management with automatic rollback on exceptions
  • Configurable fetch modes
  • Type-safe return values with PHP 8's strict typing
  • Minimalist design with zero dependencies

Installation

composer require solophp/query-executor

Quick Start

use Solo\QueryExecutor;
use Solo\QueryExecutor\Config;
use Solo\QueryExecutor\Connection;

// Create configuration
$config = new Config(
    host: 'localhost',
    user: 'root',
    pass: 'password',
    db: 'my_database'
);

// Create query executor
$db = new QueryExecutor(new Connection($config));

// Query with parameters
$user = $db->query('SELECT * FROM users WHERE id = ?', [1])->fetch();

// Insert data and get ID
$db->query('INSERT INTO users (name, email) VALUES (?, ?)', ['John', 'john@example.com']);
$lastId = $db->lastInsertId();

// Transaction support
$db->withTransaction(function(QueryExecutor $executor) {
    $executor->query('UPDATE accounts SET balance = balance - ? WHERE id = ?', [100, 1]);
    $executor->query('UPDATE accounts SET balance = balance + ? WHERE id = ?', [100, 2]);
    // Automatic rollback on exception
});

API Reference

QueryExecutor Methods

  • query(string $sql, array $bindings = []): self - Execute a SQL query with parameter bindings
  • fetchAll(?int $mode = null): array - Fetch all rows from the result set
  • fetch(?int $mode = null): array|stdClass|null - Fetch a single row from the result set
  • fetchColumn(int $idx = 0): mixed - Fetch a single column from the result set
  • lastInsertId(): string|false - Get the last inserted ID
  • rowCount(): int - Get the number of affected rows
  • beginTransaction(): bool - Begin a transaction
  • commit(): bool - Commit a transaction
  • rollBack(): bool - Roll back a transaction
  • withTransaction(callable $callback): mixed - Execute a callback within a transaction

License

MIT