A powerful, fluent SQL Query Builder for PHP with intuitive syntax and comprehensive condition handling.
- π₯ Fluent Interface - Chain methods for readable query building
- π― Type Safety - Full PHP 8+ type declarations
- π§© Flexible Conditions - Support for complex WHERE clauses
- π§ Factory Pattern - Clean condition object creation
- π Well Tested - 41 test cases with 66 assertions
- β‘ Performance - Optimized for speed and memory usage
composer require vigihdev/sql-queryuse SqlQuery\Query;
$query = new Query();
// Simple SELECT query
$sql = $query
->from('users')
->where(['active', '=', 1])
->where(['age', '>', 18])
->orderBy(['name' => 'ASC'])
->limit(10)
->toSql();
echo $sql;
// Output: SELECT * FROM users WHERE active = ? AND age > ? ORDER BY name ASC LIMIT 10// SELECT with specific columns
$query->select('id', 'name', 'email')
->from('users');
// DISTINCT queries
$query->distinct()
->from('categories');
// LIMIT and OFFSET
$query->from('posts')
->limit(20)
->offset(40);// Simple conditions
$query->where(['name', '=', 'John']);
$query->where(['age', '>', 25]);
// Multiple conditions (AND)
$query->where(['status', '=', 'active'])
->where(['verified', '=', true]);
// OR conditions
$query->where(['role', '=', 'admin'])
->orWhere(['role', '=', 'moderator']);
// IN conditions
$query->where(['status', 'IN', ['active', 'pending']]);
// LIKE conditions
$query->where(['name', 'LIKE', '%john%']);
// NULL conditions
$query->where(['deleted_at', 'IS NULL']);// INNER JOIN
$query->from('users u')
->innerJoin('posts p', 'u.id', '=', 'p.user_id');
// LEFT JOIN
$query->from('users u')
->leftJoin('profiles pr', 'u.id', '=', 'pr.user_id');
// Multiple JOINs
$query->from('users u')
->innerJoin('posts p', 'u.id', '=', 'p.user_id')
->leftJoin('categories c', 'p.category_id', '=', 'c.id');// ORDER BY
$query->orderBy(['created_at' => 'DESC', 'name' => 'ASC']);
// GROUP BY
$query->from('orders')
->groupBy('customer_id');// Hash conditions (key-value pairs)
$query->where([
'status' => 'active',
'age' => 25,
'city' => 'Jakarta'
]);
// Composite conditions with AND/OR
$query->where(['AND',
['name', '=', 'John'],
['age', '>', 18]
]);- Query - Main query builder class
- Conditions - Various condition types (Simple, Composite, In, Like, etc.)
- Factory - Creates condition objects based on operators
- Parser - Parses different condition formats
- Utils - Helper utilities for type handling
| Class | Purpose | Example |
|---|---|---|
SimpleCondition |
Basic comparisons | name = 'John' |
CompositeCondition |
AND/OR logic | (age > 18 AND status = 'active') |
InCondition |
IN operations | id IN (1,2,3) |
LikeCondition |
Pattern matching | name LIKE '%john%' |
IsCondition |
NULL checks | deleted_at IS NULL |
Run the comprehensive test suite:
# Run all tests
composer test
# Run with coverage
./vendor/bin/phpunit --coverage-html coverage/Test Statistics:
- β 41 Test Cases
- β 66 Assertions
- β 100% Pass Rate
- π― Full Coverage of Core Features
src/
βββ Query.php # Main query builder
βββ AbstractQuery.php # Base query class
βββ Conditions/ # Condition classes
β βββ SimpleCondition.php
β βββ CompositeCondition.php
β βββ ...
βββ Factories/ # Factory classes
β βββ ConditionFactory.php
βββ Parsers/ # Parser classes
β βββ ConditionParser.php
βββ Utils/ # Utility classes
βββ ParseValueType.php
tests/
βββ QueryTest.php
βββ Conditions/ # Condition tests
βββ Factories/ # Factory tests
βββ Parsers/ # Parser tests
βββ Utils/ # Utility tests
βββ Integration/ # Integration tests
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Write tests for your changes
- Ensure all tests pass (
composer test) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
Vigih Dev
- Email: vigihdev@gmail.com
- GitHub: @vigihdev
- Built with β€οΈ for the PHP community
- Inspired by modern query builders
- Thanks to all contributors and testers
β Star this repo if you find it helpful!
Report Bug β’ Request Feature β’ Documentation