项目作者: atk4

项目描述 :
Object-Oriented SQL Query Builder
高级语言: PHP
项目地址: git://github.com/atk4/dsql.git
创建时间: 2016-01-26T18:16:18Z
项目社区:https://github.com/atk4/dsql

开源协议:MIT License

下载


⚠️ 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.

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

Build
CodeCov
GitHub release
Code Climate

DSQL Is Simple and Powerful

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

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

  1. // Establish a query looking for a maximum salary
  2. $salary = new Atk4\Dsql\Query(['connection'=>$pdo]);
  3. // Create few expression objects
  4. $e_ms = $salary->expr('max(salary)');
  5. $e_df = $salary->expr('TimeStampDiff(month, from_date, to_date)');
  6. // Configure our basic query
  7. $salary
  8. ->table('salary')
  9. ->field(['emp_no', 'max_salary'=>$e_ms, 'months'=>$e_df])
  10. ->group('emp_no')
  11. ->order('-max_salary')
  12. // Define sub-query for employee "id" with certain birth-date
  13. $employees = $salary->dsql()
  14. ->table('employees')
  15. ->where('birth_date','1961-05-02')
  16. ->field('emp_no')
  17. ;
  18. // use sub-select to condition salaries
  19. $salary->where('emp_no', $employees);
  20. // Join with another table for more data
  21. $salary
  22. ->join('employees.emp_id','emp_id')
  23. ->field('employees.first_name');
  24. // finally, fetch result
  25. foreach ($salary as $row) {
  26. echo "Data: ".json_encode($row)."\n";
  27. }

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

  1. SELECT
  2. `emp_no`,
  3. max(salary) `max_salary`,
  4. TimeStampDiff(month, from_date, to_date) `months`
  5. FROM
  6. `salary`
  7. JOIN
  8. `employees` on `employees`.`emp_id` = `salary`.`emp_id`
  9. WHERE
  10. `salary`.`emp_no` in (select `id` from `employees` where `birth_date` = :a)
  11. GROUP BY `emp_no`
  12. ORDER BY max_salary desc
  13. :a = "1961-05-02"

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 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()”
to pre-populate DSQL object:

  1. $m = new Client($db);
  2. echo $m->addCondition('vip', true)
  3. ->ref('Order')
  4. ->ref('Line')
  5. ->action('fx', ['sum', 'total'])
  6. ->getDebugQuery();
  1. select sum(`price`*`qty`) from `order_line` `O_L` where `order_id` in (
  2. select `id` from `order` `O` where `client_id` in (
  3. select `id` from `client` where `vip` = :a
  4. )
  5. )

User Inerface

Agile UI is my other project that focuses on data visualization.

image

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:

  1. require 'vendor/autoload.php';
  2. $db = new \Atk4\Data\Persistence_SQL('mysql:dbname=atkui;host=localhost','root','root');
  3. $app = new \Atk4\Ui\App('My First App');
  4. $app->initLayout('Centered');
  5. $g = $layout->add(new \Atk4\Ui\Grid());
  6. $g->setModel(new Employee($db), false);

Limitations of DSQL

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

  • 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.

Documentation cheat-sheet

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

Community and Support

Gitter
Stack Overlfow Community
Discord User forum