2017 © Pedro Peláez
 

library dsql

Dynamic SQL Builder in PHP

image

atk4/dsql

Dynamic SQL Builder in PHP

  • Thursday, July 19, 2018
  • by romaninsh
  • Repository
  • 7 Watchers
  • 23 Stars
  • 15,102 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 6 Forks
  • 14 Open issues
  • 43 Versions
  • 10 % Grown

The README.md

⚠️ repo was integrated into atk4/data ⚠️

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., (*1)

Hold on! Why yet another query builder?

Obviously because existing ones are not good enough. DSQL tries to do things differently:, (*2)

  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 has been in production since 2006, initially included in AModules2 and later Agile Toolkit. We simply forked it and cleaned it up for you:, (*3)

Build CodeCov GitHub release Code Climate, (*4)

DSQL Is Simple and Powerful

``` php $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();, (*5)


If the basic query is not fun, how about more complex one? ``` php // 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:, (*6)

``` sql 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, (*7)

:a = "1961-05-02", (*8)


## DSQL is part of Agile Data Building SQL queries might be fun, but why not take it to the next level? ### 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: ``` php $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

Agile UI is my other project that focuses on data visualization., (*9)

image, (*10)

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:, (*11)

``` php require 'vendor/autoload.php';, (*12)

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

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

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

Limitations of DSQL

Our team intentionally keeps DSQL simple. The following features are deliberately excluded:, (*16)

  • no knowledge of your database schema (see https://github.com/atk4/schema).
  • 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., (*17)

Documentation cheat-sheet

DSQL has extensive documentation at http://dsql.readthedocs.org, but below we have linked some of the frequent topics:, (*18)

Community and Support

Gitter Stack Overlfow Community Discord User forum, (*19)

The Versions

19/07 2018

dev-develop

dev-develop http://agiletoolkit.org/

Dynamic SQL Builder in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

orm sql framework builder

19/07 2018

dev-feature/allow-custom-order

dev-feature/allow-custom-order http://agiletoolkit.org/

Dynamic SQL Builder in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

orm sql framework builder

14/05 2018

dev-release/1.2.4

dev-release/1.2.4 http://agiletoolkit.org/

Dynamic SQL Builder in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

orm sql framework builder

14/05 2018

1.2.4

1.2.4.0 http://agiletoolkit.org/

Dynamic SQL Builder in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

orm sql framework builder

19/04 2018

dev-feature/resource-support

dev-feature/resource-support http://agiletoolkit.org/

Dynamic SQL Builder in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

orm sql framework builder

03/04 2018

dev-release/1.2.3

dev-release/1.2.3 http://agiletoolkit.org/

Dynamic SQL Builder in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

orm sql framework builder

03/04 2018

1.2.3

1.2.3.0 http://agiletoolkit.org/

Dynamic SQL Builder in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

orm sql framework builder

03/04 2018

dev-feature/improve-debug-filters

dev-feature/improve-debug-filters http://agiletoolkit.org/

Dynamic SQL Builder in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

orm sql framework builder

19/03 2018

dev-release/1.2.2

dev-release/1.2.2 http://agiletoolkit.org/

Dynamic SQL Builder in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

orm sql framework builder

19/03 2018

1.2.2

1.2.2.0 http://agiletoolkit.org/

Dynamic SQL Builder in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

orm sql framework builder

02/03 2018

dev-release/1.2.1

dev-release/1.2.1 http://agiletoolkit.org/

Dynamic SQL Builder in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

orm sql framework builder

02/03 2018

1.2.1

1.2.1.0 http://agiletoolkit.org/

Dynamic SQL Builder in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

orm sql framework builder

01/03 2018

dev-feature/fix-bug-136

dev-feature/fix-bug-136 http://agiletoolkit.org/

Dynamic SQL Builder in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

orm sql framework builder

03/02 2018

dev-release/1.2.0

dev-release/1.2.0 http://agiletoolkit.org/

Dynamic SQL Builder in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

orm sql framework builder

03/02 2018

1.2.0

1.2.0.0 http://agiletoolkit.org/

Dynamic SQL Builder in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

orm sql framework builder

31/01 2018

dev-feature/oracle-support-fixes

dev-feature/oracle-support-fixes http://agiletoolkit.org/

Dynamic SQL Builder in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

orm sql framework builder

30/01 2018

dev-feature/connection-oracle

dev-feature/connection-oracle http://agiletoolkit.org/

Dynamic SQL Builder in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

orm sql framework builder

20/01 2018

dev-feature/oracle-lastid

dev-feature/oracle-lastid http://agiletoolkit.org/

Dynamic SQL Builder in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

orm sql framework builder

12/09 2017

dev-release/1.1.5

dev-release/1.1.5 http://agiletoolkit.org/

Dynamic SQL Builder in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

orm sql framework builder

12/09 2017

1.1.5

1.1.5.0 http://agiletoolkit.org/

Dynamic SQL Builder in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

orm sql framework builder

13/04 2017

dev-release/1.1.4

dev-release/1.1.4 http://agiletoolkit.org/

Dynamic SQL Builder in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

orm sql framework builder

13/04 2017

1.1.4

1.1.4.0 http://agiletoolkit.org/

Dynamic SQL Builder in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

orm sql framework builder

12/04 2017

dev-release/1.1.3

dev-release/1.1.3 http://agiletoolkit.org/dsql/

Dynamic SQL Builder in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

orm sql framework builder

12/04 2017

1.1.3

1.1.3.0 http://agiletoolkit.org/dsql/

Dynamic SQL Builder in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

orm sql framework builder

15/03 2017

dev-master

9999999-dev http://agiletoolkit.org/dsql/

Dynamic SQL Builder in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

orm sql framework builder

15/03 2017

1.1.2

1.1.2.0 http://agiletoolkit.org/dsql/

Dynamic SQL Builder in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

orm sql framework builder

08/03 2017

1.1.1

1.1.1.0 http://agiletoolkit.org/dsql/

Dynamic SQL Builder in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

orm sql framework builder

08/03 2017

1.1.0

1.1.0.0 http://agiletoolkit.org/dsql/

Dynamic SQL Builder in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

orm sql framework builder

22/09 2016

1.0.10

1.0.10.0 http://agiletoolkit.org/dsql/

Dynamic SQL Builder in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

orm sql framework builder

07/09 2016

1.0.9

1.0.9.0 http://agiletoolkit.org/dsql/

Dynamic SQL Builder in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

orm sql framework builder

26/07 2016

1.0.8

1.0.8.0 http://agiletoolkit.org/dsql/

Dynamic SQL Builder in PHP

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

orm sql framework builder

20/07 2016

1.0.7

1.0.7.0 http://agiletoolkit.org/dsql/

Dynamic SQL Builder in PHP

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

orm sql framework builder

19/07 2016

1.0.6

1.0.6.0 http://agiletoolkit.org/dsql/

Dynamic SQL Builder in PHP

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

orm sql framework builder

16/07 2016

1.0.5

1.0.5.0 http://agiletoolkit.org/dsql/

Dynamic SQL Builder in PHP

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

orm sql framework builder

05/07 2016

1.0.4

1.0.4.0 http://agiletoolkit.org/dsql/

Dynamic SQL Builder in PHP

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

orm sql framework builder

01/07 2016

1.0.3

1.0.3.0 http://agiletoolkit.org/dsql/

Dynamic SQL Builder in PHP

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

orm sql framework builder

28/06 2016

1.0.2

1.0.2.0 http://agiletoolkit.org/dsql/

Dynamic SQL Builder in PHP

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

orm sql framework builder

23/06 2016

1.0.1

1.0.1.0 http://agiletoolkit.org/dsql/

Dynamic SQL Builder in PHP

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

orm sql framework builder

14/04 2016

1.0.0-beta

1.0.0.0-beta http://agiletoolkit.org/dsql/

Dynamic SQL Builder in PHP

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

orm sql framework builder

08/04 2016

dev-feature/add-migrator

dev-feature/add-migrator http://agiletoolkit.org/dsql/

Dynamic SQL Builder in PHP

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

orm sql framework builder

27/03 2016

1.0.0-alpha2

1.0.0.0-alpha2 http://agiletoolkit.org/dsql/

Dynamic SQL Builder in PHP

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

orm sql framework builder

21/03 2016

1.0.0-alpha

1.0.0.0-alpha http://agiletoolkit.org/dsql/

Dynamic SQL Builder in PHP

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

orm sql framework builder

05/02 2016

0.1.1

0.1.1.0 http://agiletoolkit.org/dsql/

Dynamic SQL Builder in PHP

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

orm sql framework builder