PHPackages                             romainbessugesmeusy/php-sql-query - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. romainbessugesmeusy/php-sql-query

ActiveLibrary[Database &amp; ORM](/categories/database)

romainbessugesmeusy/php-sql-query
=================================

php-sql-query is an abstraction layer for SQL query construction

v0.3(12y ago)63.8k↓92.9%2[1 issues](https://github.com/romainbessugesmeusy/php-sql-query/issues)MITPHP

Since Feb 16Pushed 10y ago2 watchersCompare

[ Source](https://github.com/romainbessugesmeusy/php-sql-query)[ Packagist](https://packagist.org/packages/romainbessugesmeusy/php-sql-query)[ RSS](/packages/romainbessugesmeusy-php-sql-query/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (2)Versions (5)Used By (0)

SQL Query Abstraction Layer
===========================

[](#sql-query-abstraction-layer)

The **php-sql-query** package aims to provide a consistent abstraction layer for SQL query construction. It's divided in two parts:

1. abstract query components *(columns, tables, limit, groups)*
2. renderers *(MySQL, PgSql, Sql Server)*

Creating a select is really straightforward:

```
$select = new \RBM\SqlQuery\Select();
$select->setTable("project");
$select->setColumns(["project_id", "name"]);
$select->limit(0, 10);
```

To output the correct query according to the database system in use, you'll have to instanciate the renderer, and call its render method:

```
$renderer = new \RBM\SqlQuery\RendererAdapter\MySql();
echo $renderer->render($select);
```

This will print the following string:

```
SELECT
	`project`.`project_id`
	, `project`.`name`
FROM
	`project`
LIMIT 0, 10
```

Hopefully, you don't have to be *that* verbose to get the job done.

```
// do this once
\RBM\SqlQuery\Select::setDefaultRenderer("\RBM\SqlQuery\RendererAdapter");
// […]
$select = new \RBM\SqlQuery\Select("project", ["project_id", "name"]);
echo $select;
```

Filtering
---------

[](#filtering)

In this API there's no `->where()` method, instead, there's `->filter()` which returns a `\RBM\SqlQuery\Filter` object. This object provides some basic methods :

- `equals($column, $value)`
- `greaterThan($colum, $value)`
- `lowerThanEquals($colum, $value)`
- `isNull($colum)`
- …

There are also less basic methods such as :

- `in($column, $values)`
- `between($column, $a, $b)`

All of these are chainable for readability and effortless development sake.

```
	$select = new Select('project', ['project_id', 'name']);
	$select->filter()
		   		->equals('owner_id', 1)
		   		->isNull('date_deleted');
```

Of course, you can determine which operator you want, and nest clauses:

```
	$select->filter()->subFilter()
					->operator('OR')
					->equals('status', 'DRAFT')
					->equals('status', 'PUBLISHED');
```

Result:

```
	SELECT
		project.project_id
		, project.name
	FROM
		project
	WHERE
		( project.owner_id = 1 )
	 AND ( project.date_deleted IS NULL )
	 AND (
	 		( project.status = 'DRAFT' )
	 		OR ( project.status = 'PUBLISHED' )
	 	)
```

SELECT &amp; JOINS
------------------

[](#select--joins)

A **JOIN** is a **SELECT**. In fact, there is no `\RBM\SqlQuery\Join` class and there won't be.

```
$select = new Select('project', ['project_id', 'name']);
$owner = $select->join('user', 'owner_id', 'user_id');
print_r($owner);

```

Gives us

```
RBM\SqlQuery\Select Object
(
	[_table:protected] => RBM\SqlQuery\Table Object
    	(
        	[_name:protected] => user
…

```

The most effective advantage of considering J0IN as SELECT is reusability. Let say you have an entity that provides some selects.

```
class UserEntity
{

	public function getSelectForActiveUsers()
	{
		$select = new Select('user');
		$select->filter()->equals('active', 1);
		return $select;
	}

}

class ProjectEntity
{

	public function getSelectForProjectsOfActiveUsers()
	{
		$userEntity = new UserEntity();
		$select = new Select('project');
		$select->addJoin($userEntity->getSelectForActiveUsers(), 'owner_id', 'user_id');
	}
}

```

Overloading and inheritance
---------------------------

[](#overloading-and-inheritance)

One intersting feature of this package is that it can be extended to customize the query and filter layer. The finality of inheritance is to objectify the query construction, by making it fluent and business oriented.

\###Select

While extending selects, you'll discover that the first obvious usage is to define shortcuts for joins:

```
class ProjectSelect extends \RBM\SqlQuery\Select
{
	// overloading the table (simple way)
	protected $_table = 'project';

	public function owner()
	{
		return $this->join('user', 'owner_id', 'user_id');
	}
}

```

Usage:

```
$projects = new ProjectSelect();
$projects->owner()->filter()->equals('user_id', 1);

```

\###Filter

Creating a filter for our project table is straightforward:

```
class ProjectFilter extends \RBM\SqlQuery\Filter
{
	public function deleted($deleted)
	{
		return ($deleted) ? $this->isNull('date_deleted') : $this->isNotNull('date_deleted');
	}
}

```

To use this filter, you'll have to modified the `filterClass` property from the select:

```
$projects = new ProjectSelect();
$projects->filter()->deleted(true);

```

###  Health Score

30

—

LowBetter than 62% of packages

Maintenance17

Infrequent updates — may be unmaintained

Popularity25

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 98% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~108 days

Total

3

Last Release

4586d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/112633?v=4)[romainbessuges](/maintainers/romainbessuges)[@romainbessuges](https://github.com/romainbessuges)

---

Top Contributors

[![romainbessugesmeusy](https://avatars.githubusercontent.com/u/3127404?v=4)](https://github.com/romainbessugesmeusy "romainbessugesmeusy (48 commits)")[![rogeriopradoj](https://avatars.githubusercontent.com/u/443391?v=4)](https://github.com/rogeriopradoj "rogeriopradoj (1 commits)")

---

Tags

abstractionsqlqueryselectupdatedeleteinsertwhere

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/romainbessugesmeusy-php-sql-query/health.svg)

```
[![Health](https://phpackages.com/badges/romainbessugesmeusy-php-sql-query/health.svg)](https://phpackages.com/packages/romainbessugesmeusy-php-sql-query)
```

###  Alternatives

[aura/sqlquery

Object-oriented query builders for MySQL, Postgres, SQLite, and SQLServer; can be used with any database connection library.

4883.1M39](/packages/aura-sqlquery)[doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database schema introspection and management.

9.7k605.0M6.8k](/packages/doctrine-dbal)[atlas/query

Object-oriented query builders and performers for MySQL, Postgres, SQLite, and SQLServer.

41256.9k7](/packages/atlas-query)[bentools/where

PHP7.1 Fluent, immutable SQL query builder. Connectionless, framework-agnostic, no dependency.

125.2k2](/packages/bentools-where)[rennokki/laravel-eloquent-query-cache

Adding cache on your Laravel Eloquent queries' results is now a breeze.

1.1k4.4M14](/packages/rennokki-laravel-eloquent-query-cache)[nilportugues/sql-query-formatter

A very lightweight PHP class that reformats unreadable and computer-generated SQL query statements to human-friendly, readable text.

391.2M29](/packages/nilportugues-sql-query-formatter)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
