Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRM_Utils_SQL_Select - Allow fluent query execution #10686

Merged
merged 3 commits into from
Jul 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions CRM/Core/DAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,22 @@ public function fetchAll() {
return $result;
}

/**
* Returns a singular value.
*
* @return mixed|NULL
*/
public function fetchValue() {
$result = $this->getDatabaseResult();
$row = $result->fetchRow();
$ret = NULL;
if ($row) {
$ret = $row[0];
}
$this->free();
return $ret;
}

/**
* Get all the result records as mapping between columns.
*
Expand Down
32 changes: 32 additions & 0 deletions CRM/Utils/SQL/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,38 @@ public function toSQL() {
return $sql;
}

/**
* Execute the query.
*
* To examine the results, use a function like `fetch()`, `fetchAll()`,
* `fetchValue()`, or `fetchMap()`.
*
* @param string|NULL $daoName
* The return object should be an instance of this class.
* Ex: 'CRM_Contact_BAO_Contact'.
* @param bool $i18nRewrite
* If the system has multilingual features, should the field/table
* names be rewritten?
* @return CRM_Core_DAO
* @see CRM_Core_DAO::executeQuery
* @see CRM_Core_I18n_Schema::rewriteQuery
*/
public function execute($daoName = NULL, $i18nRewrite = TRUE) {
// Don't pass through $params. toSQL() handles interpolation.
$params = array();

// Don't pass through $abort, $trapException. Just use straight-up exceptions.
$abort = TRUE;
$trapException = FALSE;
$errorScope = CRM_Core_TemporaryErrorScope::useException();

// Don't pass through freeDAO. You can do it yourself.
$freeDAO = FALSE;

return CRM_Core_DAO::executeQuery($this->toSQL(), $params, $abort, $daoName,
$freeDAO, $i18nRewrite, $trapException);
}

/**
* Has an offset been set.
*
Expand Down
40 changes: 40 additions & 0 deletions tests/phpunit/CRM/Utils/SQL/SelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,46 @@ public function testGetDefault() {
$this->assertLike('SELECT * FROM foo bar', $select->toSQL());
}

public function testExecute_OK_fetch() {
$select = CRM_Utils_SQL_Select::from('civicrm_contact')->select('count(*) as cnt');
$this->assertLike('SELECT count(*) as cnt FROM civicrm_contact', $select->toSQL());

$select = CRM_Utils_SQL_Select::from('civicrm_contact')
->select('count(*) as cnt');
$rows = 0;
$dao = $select->execute();
while ($dao->fetch()) {
$rows++;
$this->assertTrue(is_numeric($dao->cnt), "Expect query to execute");
}
$this->assertEquals(1, $rows);
}

public function testExecute_OK_fetchValue() {
$select = CRM_Utils_SQL_Select::from('civicrm_contact')->select('count(*) as cnt');
$this->assertLike('SELECT count(*) as cnt FROM civicrm_contact', $select->toSQL());
$this->assertTrue(is_numeric($select->execute()->fetchValue()));
}

public function testExecute_OK_fetchAll() {
$select = CRM_Utils_SQL_Select::from('civicrm_contact')->select('count(*) as cnt');
$this->assertLike('SELECT count(*) as cnt FROM civicrm_contact', $select->toSQL());
$records = $select->execute()->fetchAll();
$this->assertTrue(is_numeric($records[0]['cnt']));
}

public function testExecute_Error() {
$select = CRM_Utils_SQL_Select::from('civicrm_contact')->select('snarb;barg');

try {
$select->execute();
$this->fail('Expected an exception');
}
catch (PEAR_Exception $e) {
$this->assertTrue(TRUE, "Received expected exception");
}
}

public function testGetFields() {
$select = CRM_Utils_SQL_Select::from('foo')
->select('bar')
Expand Down