andrew-gos / query-builder
QueryBuilder
v1.2.0
2026-06-09 22:17 UTC
Requires
- php: ^8.5
- andrew-gos/helpers: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.87
- phpunit/phpunit: ^12
README
A lightweight, fluent SQL query builder with multi-dialect support (ANSI, MySQL, PostgreSQL) and an extensible expression system. Write complex SQL queries programmatically without string concatenation.
Quick Start
use AndrewGos\QueryBuilder\Grammar\DefaultGrammar; use AndrewGos\QueryBuilder\Query\Select\SelectQuery; $grammar = new DefaultGrammar(); $query = (new SelectQuery()) ->select(['id', 'name', 'email']) ->from(['users']) ->where(['active' => true]); $built = $grammar->buildSelectQuery($query); echo $built->sql; // SELECT "id", "name", "email" FROM "users" WHERE "active" IS TRUE print_r($built->params); // []
Features
| Feature | Status |
|---|---|
| SELECT, INSERT, UPDATE, DELETE, VALUES queries | ✅ |
| WHERE, HAVING, GROUP BY, ORDER BY, LIMIT / OFFSET | ✅ |
| JOIN (INNER, LEFT, RIGHT, FULL, CROSS, NATURAL variants) | ✅ |
| Window functions (OVER, PARTITION BY, frame specs) | ✅ |
| Common Table Expressions (WITH, WITH RECURSIVE) | ✅ |
| PostgreSQL: MATERIALIZED CTE, SEARCH / CYCLE | ✅ |
| Set operations: UNION, INTERSECT, EXCEPT (ALL / DISTINCT) | ✅ |
| Row-level locking (FOR UPDATE / FOR SHARE, NOWAIT / SKIP LOCKED) | ✅ |
| MySQL: HIGH_PRIORITY, STRAIGHT_JOIN, SQL_* hints, PARTITION | ✅ |
| PostgreSQL: DISTINCT ON, ONLY modifier, RETURNING, ON CONFLICT | ✅ |
| Named parameter binding with auto-generated IDs | ✅ |
| MERGE query | ✅ |
| PostgreSQL: MERGE RETURNING, DO NOTHING | ✅ |
| Additional SQL dialects | 🚧 In development |
Documentation
- Quick Start Guide — Installation, first query, architecture overview.
- Query Types — Full reference for SELECT, INSERT, UPDATE, DELETE, VALUES, MERGE.
- Expressions — Expression system, ValueBuilder, conditions, windows, CTEs.
- Dialects — Default/ANSI, MySQL, and PostgreSQL grammar specifics.
Requirements
- PHP 8.5+
andrew-gos/helpers^1.0
Installation
composer require andrew-gos/query-builder
Roadmap
- v1.2.0 — Additional SQL dialects (SQLite, MariaDB)
License
This library is open-source software licensed under the MIT License.