PHPackages                             atk4/dsql - 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. [Framework](/categories/framework)
4. /
5. atk4/dsql

AbandonedArchivedLibrary[Framework](/categories/framework)

atk4/dsql
=========

Dynamic SQL Builder in PHP

2.4.0(5y ago)58524.2k↑73.9%232MITPHPPHP &gt;=7.3.0

Since Feb 5Pushed 5y ago11 watchersCompare

[ Source](https://github.com/atk4/dsql)[ Packagist](https://packagist.org/packages/atk4/dsql)[ Docs](http://agiletoolkit.org/)[ RSS](/packages/atk4-dsql/feed)WikiDiscussions develop Synced 1mo ago

READMEChangelog (10)Dependencies (8)Versions (54)Used By (2)

⚠️ repo was integrated into [atk4/data](https://github.com/atk4/data) ⚠️
========================================================================

[](#️-repo-was-integrated-into-atk4data-️)

DSQL is a composable SQL query builder. You can write multi-vendor queries in PHP profiting from better security, clean syntax and avoid human errors.

Hold on! Why yet another query builder?
---------------------------------------

[](#hold-on-why-yet-another-query-builder)

Obviously because existing ones are not good enough. DSQL tries to do things differently:

1. Composability. Unlike other libraries, we render queries recursively allowing many levels of sub-selects.
2. Small footprint. We don't duplicate query code for all vendors, instead we use clever templating system.
3. Extensibility. We have 3 different ways to extend DSQL as well as 3rd party vendor driver support.
4. **Any Query** - any query with any complexity can be expressed through DSQL.
5. Almost no dependencies. Use DSQL in any PHP application or framework.
6. NoSQL support. In addition to supporting PDO, DSQL can be extended to deal with SQL-compatible NoSQL servers.

DSQL Is Stable!
---------------

[](#dsql-is-stable)

DSQL has been in production since 2006, initially included in [AModules2](https://sourceforge.net/projects/amodules3/) and later [Agile Toolkit](https://github.com/atk4/atk4/blob/release-4.0.1/lib/DBlite/dsql.php). We simply forked it and cleaned it up for you:

[![Build](https://github.com/atk4/dsql/workflows/Unit%20Testing/badge.svg)](https://github.com/atk4/dsql/workflows/Unit%20Testing/badge.svg)[![CodeCov](https://camo.githubusercontent.com/a7f1361a03211d6ce659727cb60f75ca353d36daf5cc002c1d839be4752aea80/68747470733a2f2f636f6465636f762e696f2f67682f61746b342f6473716c2f6272616e63682f646576656c6f702f67726170682f62616467652e737667)](https://codecov.io/gh/atk4/dsql)[![GitHub release](https://camo.githubusercontent.com/e238f87e2b71266de31d570b446c269d191b93b53b3784bf7e2058438a322b3f/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f61746b342f6473716c2e737667)](CHANGELOG.md)[![Code Climate](https://camo.githubusercontent.com/58edd91906ecdadb56fdce3052b7e39e9f82151c2031944466ab25e5598e8f1e/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f61746b342f6473716c2f6261646765732f6770612e737667)](https://codeclimate.com/github/atk4/dsql)

DSQL Is Simple and Powerful
---------------------------

[](#dsql-is-simple-and-powerful)

```
$query = new Atk4\Dsql\Query();
$query  ->table('employees')
        ->where('birth_date','1961-05-02')
        ->field('count(*)')
        ;
echo "Employees born on May 2, 1961: ".$query->getOne();
```

If the basic query is not fun, how about more complex one?

```
// Establish a query looking for a maximum salary
$salary = new Atk4\Dsql\Query(['connection'=>$pdo]);

// Create few expression objects
$e_ms = $salary->expr('max(salary)');
$e_df = $salary->expr('TimeStampDiff(month, from_date, to_date)');

// Configure our basic query
$salary
    ->table('salary')
    ->field(['emp_no', 'max_salary'=>$e_ms, 'months'=>$e_df])
    ->group('emp_no')
    ->order('-max_salary')

// Define sub-query for employee "id" with certain birth-date
$employees = $salary->dsql()
    ->table('employees')
    ->where('birth_date','1961-05-02')
    ->field('emp_no')
    ;

// use sub-select to condition salaries
$salary->where('emp_no', $employees);

// Join with another table for more data
$salary
    ->join('employees.emp_id','emp_id')
    ->field('employees.first_name');

// finally, fetch result
foreach ($salary as $row) {
    echo "Data: ".json_encode($row)."\n";
}
```

This builds and executes a single query that looks like this:

```
SELECT
    `emp_no`,
    max(salary) `max_salary`,
    TimeStampDiff(month, from_date, to_date) `months`
FROM
    `salary`
JOIN
    `employees` on `employees`.`emp_id` = `salary`.`emp_id`
WHERE
    `salary`.`emp_no` in (select `id` from `employees` where `birth_date` = :a)
GROUP BY `emp_no`
ORDER BY max_salary desc

:a = "1961-05-02"
```

DSQL is part of Agile Data
--------------------------

[](#dsql-is-part-of-agile-data)

Building SQL queries might be fun, but why not take it to the next level?

### Domain Model

[](#domain-model)

[Agile Data](https://github.com/atk4/data) is my other project, which implements Domain Model Persistence on top of DSQL. You still maintain control over your queries while also benefiting from database abstraction.

Next example uses Agile Data's method "[action](http://agile-data.readthedocs.io/en/develop/quickstart.html?highlight=action#actions)()" to pre-populate DSQL object:

```
$m = new Client($db);
echo $m->addCondition('vip', true)
  ->ref('Order')
  ->ref('Line')
  ->action('fx', ['sum', 'total'])
  ->getDebugQuery();
```

```
select sum(`price`*`qty`) from `order_line` `O_L` where `order_id` in (
  select `id` from `order` `O` where `client_id` in (
    select `id` from `client` where `vip` = :a
  )
)
```

### User Inerface

[](#user-inerface)

[Agile UI](https://github.com/atk4/ui) is my other project that focuses on data visualization.

[![image](https://github.com/atk4/ui/raw/develop/docs/images/grid.png)](https://github.com/atk4/ui/raw/develop/docs/images/grid.png)

If you wonder what's the most efficient way to display table like that on your page, with Agile UI, Agile Data and DSQL you can do it in **less than 10 lines**:

```
require 'vendor/autoload.php';

$db = new \Atk4\Data\Persistence_SQL('mysql:dbname=atkui;host=localhost','root','root');

$app = new \Atk4\Ui\App('My First App');
$app->initLayout('Centered');

$g = $layout->add(new \Atk4\Ui\Grid());
$g->setModel(new Employee($db), false);
```

Limitations of DSQL
-------------------

[](#limitations-of-dsql)

Our team intentionally keeps DSQL simple. The following features are deliberately excluded:

- no knowledge of your database schema (see ).
- no reliance on any usage pattern in your database or presence of specific tables.
- no decision making based on supplied data values.
- no active record or object relational mapping

If you need features above, I strongly advise you to look into [Agile Data](https://github.com/atk4/data).

Documentation cheat-sheet
-------------------------

[](#documentation-cheat-sheet)

DSQL has extensive documentation at , but below we have linked some of the frequent topics:

- querying data from [table()](http://dsql.readthedocs.org/en/latest/queries.html#modifying-your-query) or sub-select with [join()](http://dsql.readthedocs.org/en/develop/queries.html#joining-with-other-tables), [where()](http://dsql.readthedocs.io/en/develop/queries.html?highlight=delete#Query::where), [order()](http://dsql.readthedocs.io/en/develop/queries.html?highlight=order#ordering-result-set), [group()](http://dsql.readthedocs.org/en/develop/queries.html#grouping-results-by-field), [limit()](http://dsql.readthedocs.io/en/develop/queries.html?highlight=limit#limiting-result-set), [having()](http://dsql.readthedocs.io/en/develop/queries.html?highlight=having#Query::having) and [option()](http://dsql.readthedocs.io/en/develop/queries.html?highlight=option#Query::option)
- [update](http://dsql.readthedocs.io/en/develop/queries.html?highlight=update#Query::update)/[replace](http://dsql.readthedocs.io/en/develop/queries.html?highlight=replace#Query::replace) single or multiple records with [set()](http://dsql.readthedocs.io/en/develop/queries.html?highlight=set#set-value-to-a-field), [where()](http://dsql.readthedocs.io/en/develop/queries.html?highlight=delete#Query::where) and [option()](http://dsql.readthedocs.io/en/develop/queries.html?highlight=option#Query::option)
- [insert](http://dsql.readthedocs.io/en/develop/queries.html?highlight=insert#Query::insert) one or multiple records with [set()](http://dsql.readthedocs.io/en/develop/queries.html?highlight=set#set-value-to-a-field) or setAll() and [option()](http://dsql.readthedocs.io/en/develop/queries.html?highlight=option#Query::option)
- [delete](http://dsql.readthedocs.io/en/develop/queries.html?highlight=delete#Query::delete) records with [where()](http://dsql.readthedocs.io/en/develop/queries.html?highlight=delete#Query::where)
- [iterate](http://dsql.readthedocs.org/en/latest/quickstart.html#fetching-result) through [result-set](http://dsql.readthedocs.io/en/develop/results.html#results) or [getRows()](http://dsql.readthedocs.io/en/develop/expressions.html?highlight=getRows#Expression::getRows) all data
- supporting [sub-queries](http://dsql.readthedocs.org/en/latest/queries.html#using-query-as-expression) and [expressions](http://dsql.readthedocs.org/en/latest/expressions.html#expressions) anywhere

Community and Support
---------------------

[](#community-and-support)

[![Gitter](https://camo.githubusercontent.com/c1be95072ead23a5fe8eaa7b376163cb51b70abaaf8a8a783ce2dc4c9b5a65f7/68747470733a2f2f696d672e736869656c64732e696f2f6769747465722f726f6f6d2f61746b342f646174612e737667)](https://gitter.im/atk4/dataset?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)[![Stack Overlfow Community](https://camo.githubusercontent.com/385d1f827e1f6c65ed270301047791bb9c0933da3e8e1bdce44610c9948b89b1/68747470733a2f2f696d672e736869656c64732e696f2f737461636b65786368616e67652f737461636b6f766572666c6f772f742f61746b342e737667)](http://stackoverflow.com/questions/ask?tags=atk4)[![Discord User forum](https://camo.githubusercontent.com/453bb23813fc43b944b90e4ac2eb876b47ff6dc842f29fbbf97476d6b83dd7c3/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f646973636f72642d557365725f466f72756d2d677265656e2e737667)](https://forum.agiletoolkit.org/c/44)

###  Health Score

45

—

FairBetter than 93% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity49

Moderate usage in the ecosystem

Community26

Small or concentrated contributor base

Maturity72

Established project with proven stability

 Bus Factor1

Top contributor holds 51% 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 ~49 days

Total

39

Last Release

1858d ago

Major Versions

0.1.1 → 1.0.0-alpha2016-03-21

1.2.6 → 2.0.02019-12-02

PHP version history (4 changes)0.1.1PHP &gt;=5.5.0

1.1.4PHP &gt;=5.6.0

2.0.1PHP &gt;=7.2.0

2.2.0PHP &gt;=7.3.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/426ad318d07e7685454f7e449a9d0c9f005b83aef0777558d97d854ff9c28a5a?d=identicon)[romaninsh](/maintainers/romaninsh)

---

Top Contributors

[![romaninsh](https://avatars.githubusercontent.com/u/453929?v=4)](https://github.com/romaninsh "romaninsh (333 commits)")[![DarkSide666](https://avatars.githubusercontent.com/u/1969119?v=4)](https://github.com/DarkSide666 "DarkSide666 (208 commits)")[![mvorisek](https://avatars.githubusercontent.com/u/2228672?v=4)](https://github.com/mvorisek "mvorisek (77 commits)")[![gartner](https://avatars.githubusercontent.com/u/195601?v=4)](https://github.com/gartner "gartner (17 commits)")[![georgehristov](https://avatars.githubusercontent.com/u/8128250?v=4)](https://github.com/georgehristov "georgehristov (13 commits)")[![gowrav-vishwakarma](https://avatars.githubusercontent.com/u/1007955?v=4)](https://github.com/gowrav-vishwakarma "gowrav-vishwakarma (3 commits)")[![gitter-badger](https://avatars.githubusercontent.com/u/8518239?v=4)](https://github.com/gitter-badger "gitter-badger (1 commits)")[![NDFA](https://avatars.githubusercontent.com/u/60551901?v=4)](https://github.com/NDFA "NDFA (1 commits)")

---

Tags

agileatk4dsqlphpquerysqlframeworkormsqlbuilder

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/atk4-dsql/health.svg)

```
[![Health](https://phpackages.com/badges/atk4-dsql/health.svg)](https://phpackages.com/packages/atk4-dsql)
```

###  Alternatives

[atk4/data

Agile Data - Database access abstraction framework

2811.7M37](/packages/atk4-data)

PHPackages © 2026

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