GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - atk4/dsql: Object-Oriented SQL Query Builder

This repository was archived by the owner on May 11, 2021. It is now read-only.
BranchesTags
Open more actions menu

Latest commit

 

History

653 Commits
653 Commits

Folders and files

NameName
Last commit message
Last commit date
.github
.github
 
 
docs
docs
 
 
src
src
 
 
tests
tests
 
 
.gitignore
.gitignore
 
 
.php-cs-fixer.dist.php
.php-cs-fixer.dist.php
 
 
CONTRIBUTING.md
CONTRIBUTING.md
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
codecov.yml
codecov.yml
 
 
composer.json
composer.json
 
 
phpstan.neon.dist
phpstan.neon.dist
 
 
phpunit-mssql.xml.dist
phpunit-mssql.xml.dist
 
 
phpunit-mysql.xml.dist
phpunit-mysql.xml.dist
 
 
phpunit-oracle.xml.dist
phpunit-oracle.xml.dist
 
 
phpunit-pgsql.xml.dist
phpunit-pgsql.xml.dist
 
 
phpunit.xml.dist
phpunit.xml.dist
 
 

Repository files navigation

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

$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

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:

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

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:

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

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:

  • querying data from table() or sub-select with join(), where(), order(), group(), limit(), having() and option()
  • update/replace single or multiple records with set(), where() and option()
  • insert one or multiple records with set() or setAll() and option()
  • delete records with where()
  • iterate through result-set or getRows() all data
  • supporting sub-queries and expressions anywhere

Community and Support

Gitter Stack Overlfow Community Discord User forum

About

Object-Oriented SQL Query Builder

agiletoolkit.org/

Topics

agileatk4dsqlphpquerysql

Resources

Readme
MIT license

Contributing

Contributing
Activity
Custom properties

Stars

58 stars

Watchers

6 watching

Forks

23 forks
Report repository

Releases

Used by

Contributors

Languages

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.