CRM_Utils_SQL_Select - Allow fluent query execution
authorTim Otten <totten@civicrm.org>
Mon, 17 Jul 2017 23:17:04 +0000 (16:17 -0700)
committerTim Otten <totten@civicrm.org>
Mon, 17 Jul 2017 23:20:40 +0000 (16:20 -0700)
commit77e74ae16f50b859ce94d66d4b7a890075adba54
treec6869d1a25b17c9d6a5248ec2face4c3a3749550
parent4a5b9e772e80988ddd5a4af025a35f415f874418
CRM_Utils_SQL_Select - Allow fluent query execution

When I first wrote `CRM_Utils_SQL_Select`, I was a bit dogmatic about
loose-coupling and wanted the class to be entirely independent of the SQL
runtime. But this is a bit annoying in usage and training.

Before
======

To build and execute query, you had to pass the rendered SQL to the execute
function, eg

```php
$select = CRM_Utils_SQL_Select::from('mytable')
  ->select('...')
$dao = CRM_Core_DAO::executeQuery($select->toSQL());
while ($dao->fetch()) { ... }
```

After
=====

You can use more fluent style:

```php
$dao = CRM_Utils_SQL_Select::from('mytable')
  ->select('...')
  ->execute();
while ($dao->fetch()) { ... }
```

And you can chain with other DAO functions like `fetchAll()` or
`fetchValue()`.

```php
$records = CRM_Utils_SQL_Select::from('mytable')
  ->select('...')
  ->execute()
  ->fetchAll();
```
CRM/Core/DAO.php
CRM/Utils/SQL/Select.php
tests/phpunit/CRM/Utils/SQL/SelectTest.php