Skip to content

Commit

Permalink
Merge pull request #10686 from totten/master-sql
Browse files Browse the repository at this point in the history
CRM_Utils_SQL_Select - Allow fluent query execution
  • Loading branch information
monishdeb authored Jul 24, 2017
2 parents f45b8dc + ee7d22e commit f6f65fc
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
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

0 comments on commit f6f65fc

Please sign in to comment.