Skip to content

Commit

Permalink
Merge pull request #6513 from totten/master-sql-fragment
Browse files Browse the repository at this point in the history
CRM-16036 - _civicrm_api3_get_using_utils_sql - Pass SQL fragments as object
  • Loading branch information
eileenmcnaughton committed Aug 17, 2015
2 parents 022394b + 01c8287 commit c1773a0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 31 deletions.
19 changes: 12 additions & 7 deletions api/v3/Activity.php
Expand Up @@ -242,7 +242,7 @@ function civicrm_api3_activity_get($params) {
}
}
else {
$extraSql = array();
$sql = CRM_Utils_SQL_Select::fragment();
$options = civicrm_api3('ActivityContact', 'getoptions', array('field' => 'record_type_id'));
$options = $options['values'];
$activityContactOptions = array(
Expand All @@ -252,15 +252,20 @@ function civicrm_api3_activity_get($params) {
);
foreach ($activityContactOptions as $activityContactName => $activityContactValue) {
if (!empty($params[$activityContactName])) {
$extraSql['join'][] = array(
'activity_' . $activityContactName => '
LEFT JOIN civicrm_activity_contact ac ON a.id = ac.activity_id AND ac.record_type_id = ' . (int) $activityContactValue,
// If the intent is to have multiple joins -- one for each relation -- then you would
// need different table aliases. Consider replacing 'ac' and passing in a '!alias' param,
// with a different value for each relation.
$sql->join(
'activity_' . $activityContactName,
'LEFT JOIN civicrm_activity_contact ac ON a.id = ac.activity_id AND ac.record_type_id = #typeId',
array('typeId' => $activityContactValue)
);
// Note that if we later need to change the int to an array we would need sql escaping.
$extraSql['where'] = array('activity_' . $activityContactName => 'ac.contact_id = ' . (int) $params[$activityContactName]);
$sql->where('ac.contact_id IN (#cid)', array(
'cid' => $params[$activityContactName],
));
}
}
$activities = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE, 'Activity', $extraSql);
$activities = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE, 'Activity', $sql);
}
$options = _civicrm_api3_get_options_from_params($params, FALSE, 'Activity', 'get');
if ($options['is_count']) {
Expand Down
6 changes: 3 additions & 3 deletions api/v3/Event.php
Expand Up @@ -125,12 +125,12 @@ function civicrm_api3_event_get($params) {
unset($params['return.max_results']);
}

$extraSql = array();
$sql = CRM_Utils_SQL_Select::fragment();
if (!empty($params['isCurrent'])) {
$extraSql['where'] = array('civicrm_event' => '(start_date >= CURDATE() || end_date >= CURDATE())');
$sql->where('(start_date >= CURDATE() || end_date >= CURDATE())');
}

$events = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE, 'Event', $extraSql, TRUE);
$events = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE, 'Event', $sql, TRUE);
$options = _civicrm_api3_get_options_from_params($params);
if ($options['is_count']) {
return civicrm_api3_create_success($events, $params, 'Event', 'get');
Expand Down
31 changes: 10 additions & 21 deletions api/v3/utils.php
Expand Up @@ -470,11 +470,11 @@ function _civicrm_api3_store_values(&$fields, &$params, &$values) {
* As passed into api get function.
* @param bool $isFillUniqueFields
* Do we need to ensure unique fields continue to be populated for this api? (backward compatibility).
* @param array $extraMysql
* @param CRM_Utils_SQL_Select|NULL $sqlFragment
*
* @return array
*/
function _civicrm_api3_get_using_utils_sql($dao_name, $params, $isFillUniqueFields, $extraMysql) {
function _civicrm_api3_get_using_utils_sql($dao_name, $params, $isFillUniqueFields, $sqlFragment) {

$dao = new $dao_name();
$entity = _civicrm_api_get_entity_name_from_dao($dao);
Expand Down Expand Up @@ -699,19 +699,8 @@ function _civicrm_api3_get_using_utils_sql($dao_name, $params, $isFillUniqueFiel
));
}
};
if (!empty($extraMysql)) {
foreach ($extraMysql as $type => $extraClauses) {
foreach ($extraClauses as $clauseKey => $clause) {
if ($type == 'join') {
foreach ($clause as $joinName => $join) {
$query->$type($joinName, $join);
}
}
else {
$query->$type($clause);
}
}
}
if (!empty($sqlFragment)) {
$query->merge($sqlFragment);
}

// order by
Expand Down Expand Up @@ -1637,21 +1626,21 @@ function _civicrm_api3_check_required_fields($params, $daoName, $return = FALSE)
* @param bool $returnAsSuccess
* Return in api success format.
* @param string $entity
* @param array $extraSql
* API specific queries eg for event isCurrent would be converted to
* $extraSql['where'] = array('civicrm_event' => array('(start_date >= CURDATE() || end_date >= CURDATE())'));
* @param CRM_Utils_SQL_Select|NULL $sql
* Extra SQL bits to add to the query. For filtering current events, this might be:
* CRM_Utils_SQL_Select::fragment()->where('(start_date >= CURDATE() || end_date >= CURDATE())');
* @param bool $uniqueFields
* Should unique field names be returned (for backward compatibility)
*
* @return array
*/
function _civicrm_api3_basic_get($bao_name, &$params, $returnAsSuccess = TRUE, $entity = "", $extraSql = array(), $uniqueFields = FALSE) {
function _civicrm_api3_basic_get($bao_name, &$params, $returnAsSuccess = TRUE, $entity = "", $sql = NULL, $uniqueFields = FALSE) {

if ($returnAsSuccess) {
return civicrm_api3_create_success(_civicrm_api3_get_using_utils_sql($bao_name, $params, $uniqueFields, $extraSql), $params, $entity, 'get');
return civicrm_api3_create_success(_civicrm_api3_get_using_utils_sql($bao_name, $params, $uniqueFields, $sql), $params, $entity, 'get');
}
else {
return _civicrm_api3_get_using_utils_sql($bao_name, $params, $uniqueFields, $extraSql);
return _civicrm_api3_get_using_utils_sql($bao_name, $params, $uniqueFields, $sql);
}
}

Expand Down

0 comments on commit c1773a0

Please sign in to comment.