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 2 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
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to flesh out this docblock by:

  • providing a short description
  • documenting the parameters with types and descriptions (e.g. what happens when $i18nRewrite is TRUE vs FALSE?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, good point. I've updated docblock in c4dcc9c

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 😁

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
33 changes: 33 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,39 @@ public function testGetDefault() {
$this->assertLike('SELECT * FROM foo bar', $select->toSQL());
}

public function testExecute_OK() {
// We need some SQL query.
$select = CRM_Utils_SQL_Select::from('civicrm_contact')
->select('count(*) as cnt');
$this->assertLike('SELECT count(*) as cnt FROM civicrm_contact', $select->toSQL());

// Try with typical fetch().
$rows = 0;
$dao = $select->execute();
while ($dao->fetch()) {
$rows++;
$this->assertTrue(is_numeric($dao->cnt), "Expect query to execute");
}
$this->assertEquals(1, $rows);

// Try with fetchValue().
$this->assertTrue(is_numeric($select->execute()->fetchValue()));

// Try with fetchAll()
$records = $select->execute()->fetchAll();
$this->assertTrue(is_numeric($records[0]['cnt']));
}

public function testExecute_Error() {
try {
CRM_Utils_SQL_Select::from('civicrm_contact')->select('snarb;barg')->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