Skip to content

Commit

Permalink
Merge pull request #27008 from colemanw/booleanSearch
Browse files Browse the repository at this point in the history
Simplify queries of NOT NULL fields
  • Loading branch information
monishdeb authored Aug 13, 2023
2 parents b90125f + a134b35 commit c666dfb
Show file tree
Hide file tree
Showing 18 changed files with 31 additions and 31 deletions.
2 changes: 1 addition & 1 deletion CRM/ACL/Form/ACL.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public function buildQuickForm() {
$event = [
'-1' => ts('- select event -'),
'0' => ts('All Events'),
] + CRM_Event_PseudoConstant::event(NULL, FALSE, "( is_template IS NULL OR is_template != 1 )");
] + CRM_Event_PseudoConstant::event(NULL, FALSE, "is_template = 0");

$this->add('select', 'group_id', ts('Group'), $group);
$this->add('select', 'custom_group_id', ts('Custom Data'), $customGroup);
Expand Down
2 changes: 1 addition & 1 deletion CRM/Campaign/BAO/Campaign.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ public static function getCampaignSummary($params = [], $onlyCount = FALSE) {
if (array_key_exists('is_active', $params)) {
$active = "( campaign.is_active = 1 )";
if (!empty($params['is_active'])) {
$active = "( campaign.is_active = 0 OR campaign.is_active IS NULL )";
$active = "campaign.is_active = 0";
}
$where[] = $active;
}
Expand Down
2 changes: 1 addition & 1 deletion CRM/Campaign/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public static function whereClauseSingle(&$values, &$query) {

case 'campaign_search_voter_for':
if (in_array($value, ['release', 'interview'])) {
$query->_where[$grouping][] = '(civicrm_activity.is_deleted = 0 OR civicrm_activity.is_deleted IS NULL)';
$query->_where[$grouping][] = 'civicrm_activity.is_deleted = 0';
}
return;

Expand Down
4 changes: 2 additions & 2 deletions CRM/Campaign/BAO/Survey.php
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ public static function voterActivityDetails($surveyId, $voterIds, $interviewerId
INNER JOIN civicrm_activity_contact activityAssignment
ON ( activityAssignment.activity_id = activity.id AND activityAssignment.record_type_id = $assigneeID )
WHERE activity.source_record_id = %1
AND ( activity.is_deleted IS NULL OR activity.is_deleted = 0 ) ";
AND activity.is_deleted = 0 ";
if (!empty($interviewerId)) {
$query .= "AND activityAssignment.contact_id = %2 ";
$params[2] = [$interviewerId, 'Integer'];
Expand Down Expand Up @@ -598,7 +598,7 @@ public static function getSurveyActivities(
INNER JOIN civicrm_contact contact_a ON ( activityTarget.contact_id = contact_a.id )
WHERE activity.source_record_id = %1
AND activity.activity_type_id = %2
AND ( activity.is_deleted IS NULL OR activity.is_deleted = 0 )
AND activity.is_deleted = 0
$whereClause";

$params = [
Expand Down
4 changes: 2 additions & 2 deletions CRM/Case/BAO/Case.php
Original file line number Diff line number Diff line change
Expand Up @@ -1902,7 +1902,7 @@ public static function getRelatedCaseIds($caseId, $excludeDeleted = TRUE) {

$whereClause = "mainCase.id = %2";
if ($excludeDeleted) {
$whereClause .= " AND ( relAct.is_deleted = 0 OR relAct.is_deleted IS NULL )";
$whereClause .= " AND relAct.is_deleted = 0";
}

$query = "
Expand Down Expand Up @@ -1945,7 +1945,7 @@ public static function getRelatedCases($caseId, $excludeDeleted = TRUE) {

$whereClause = 'relCase.id IN ( ' . implode(',', $relatedCaseIds) . ' )';
if ($excludeDeleted) {
$whereClause .= " AND ( relCase.is_deleted = 0 OR relCase.is_deleted IS NULL )";
$whereClause .= " AND relCase.is_deleted = 0";
}

//filter for permissioned cases.
Expand Down
4 changes: 2 additions & 2 deletions CRM/Contact/BAO/GroupContactCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ protected static function groupRefreshedClause($groupIDClause = NULL, $includeHi
)";

if (!$includeHiddenGroups) {
$query .= "AND (g.is_hidden = 0 OR g.is_hidden IS NULL)";
$query .= "AND g.is_hidden = 0";
}

if (!empty($groupIDClause)) {
Expand Down Expand Up @@ -431,7 +431,7 @@ public static function contactGroup(int $contactID): array {
FROM civicrm_group_contact_cache gc
INNER JOIN civicrm_group g ON g.id = gc.group_id
WHERE gc.contact_id = $contactID
AND (g.is_hidden = 0 OR g.is_hidden IS NULL)
AND g.is_hidden = 0
ORDER BY gc.contact_id, g.children
";

Expand Down
2 changes: 1 addition & 1 deletion CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -1152,7 +1152,7 @@ public static function getTotalAmountAndCount($status = NULL, $startDate = NULL,
INNER JOIN civicrm_contact contact ON ( contact.id = c.contact_id )
$financialTypeACLJoin
WHERE $whereCond
AND ( is_test = 0 OR is_test IS NULL )
AND is_test = 0
AND contact.is_deleted = 0
GROUP BY currency
";
Expand Down
4 changes: 2 additions & 2 deletions CRM/Contribute/BAO/Contribution/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static function contributionChartMonthly($param) {
FROM civicrm_contribution AS contrib
INNER JOIN civicrm_contact AS contact ON ( contact.id = contrib.contact_id )
WHERE contrib.contact_id = contact.id
AND ( contrib.is_test = 0 OR contrib.is_test IS NULL )
AND contrib.is_test = 0
AND contrib.contribution_status_id = 1
AND date_format(contrib.receive_date,'%Y') = %1
AND contact.is_deleted = 0
Expand Down Expand Up @@ -83,7 +83,7 @@ public static function contributionChartYearly() {
{$yearClause}
FROM civicrm_contribution AS contrib
INNER JOIN civicrm_contact contact ON ( contact.id = contrib.contact_id )
WHERE ( contrib.is_test = 0 OR contrib.is_test IS NULL )
WHERE contrib.is_test = 0
AND contrib.contribution_status_id = 1
AND contact.is_deleted = 0
GROUP BY contribYear
Expand Down
2 changes: 1 addition & 1 deletion CRM/Event/ActionMapping/ByEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function getLabel(): string {
}

public function getValueLabels(): array {
return CRM_Event_PseudoConstant::event(NULL, FALSE, "( is_template IS NULL OR is_template != 1 )");
return CRM_Event_PseudoConstant::event(NULL, FALSE, "is_template = 0");
}

}
10 changes: 5 additions & 5 deletions CRM/Event/BAO/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public static function getEvents(
$query = "
SELECT `id`, `title`, `start_date`
FROM `civicrm_event`
WHERE ( civicrm_event.is_template IS NULL OR civicrm_event.is_template = 0 )";
WHERE ( civicrm_event.is_template = 0 )";

if (!empty($id)) {
$op = is_array($id) ? 'IN' : '=';
Expand Down Expand Up @@ -363,7 +363,7 @@ public static function getEventSummary() {
SELECT count(id) as total_events
FROM civicrm_event
WHERE civicrm_event.is_active = 1 AND
( civicrm_event.is_template IS NULL OR civicrm_event.is_template = 0) AND
civicrm_event.is_template = 0 AND
civicrm_event.start_date >= DATE_SUB( NOW(), INTERVAL 7 day )
$validEventIDs";

Expand Down Expand Up @@ -425,7 +425,7 @@ public static function getEventSummary() {
LEFT JOIN civicrm_pcp_block ON ( civicrm_pcp_block.entity_id = civicrm_event.id AND civicrm_pcp_block.entity_table = 'civicrm_event')
LEFT JOIN civicrm_recurring_entity ON ( civicrm_event.id = civicrm_recurring_entity.entity_id AND civicrm_recurring_entity.entity_table = 'civicrm_event' )
WHERE civicrm_event.is_active = 1 AND
( civicrm_event.is_template IS NULL OR civicrm_event.is_template = 0) AND
civicrm_event.is_template = 0 AND
civicrm_event.start_date >= DATE_SUB( NOW(), INTERVAL 7 day )
$validEventIDs
ORDER BY civicrm_event.start_date ASC
Expand Down Expand Up @@ -579,7 +579,7 @@ public static function getEventSummary() {
$countedRoles = CRM_Event_PseudoConstant::participantRole(NULL, 'filter = 1');
$nonCountedRoles = CRM_Event_PseudoConstant::participantRole(NULL, '( filter = 0 OR filter IS NULL )');
$countedStatus = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 1');
$nonCountedStatus = CRM_Event_PseudoConstant::participantStatus(NULL, '( is_counted = 0 OR is_counted IS NULL )');
$nonCountedStatus = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 0');

$countedStatusANDRoles = array_merge($countedStatus, $countedRoles);
$nonCountedStatusANDRoles = array_merge($nonCountedStatus, $nonCountedRoles);
Expand Down Expand Up @@ -827,7 +827,7 @@ public static function getCompleteInfo(
civicrm_event.event_type_id = civicrm_option_value.value AND
civicrm_option_value.option_group_id = %1 )
WHERE civicrm_event.is_active = 1
AND (is_template = 0 OR is_template IS NULL)
AND is_template = 0
{$publicCondition}
{$dateCondition}";

Expand Down
4 changes: 2 additions & 2 deletions CRM/Event/BAO/Participant.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ public static function eventFull(

$where = [' event.id = %1 '];
if (!$considerTestParticipant) {
$where[] = ' ( participant.is_test = 0 OR participant.is_test IS NULL ) ';
$where[] = ' participant.is_test = 0 ';
}

// Only count Participant Roles with the "Counted?" flag.
Expand Down Expand Up @@ -511,7 +511,7 @@ public static function priceSetOptionsCount(

$isTestClause = NULL;
if (!$considerTestParticipants) {
$isTestClause = ' AND ( participant.is_test IS NULL OR participant.is_test = 0 )';
$isTestClause = ' AND participant.is_test = 0 ';
}

$skipParticipantClause = NULL;
Expand Down
2 changes: 1 addition & 1 deletion CRM/Event/Page/ManageEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public function browse() {
$params = [];
$whereClause = $this->whereClause($params, TRUE, $this->_force);
// because is_template != 1 would be to simple
$whereClause .= ' AND (is_template = 0 OR is_template IS NULL)';
$whereClause .= ' AND is_template = 0';

$this->pager($whereClause, $params);

Expand Down
2 changes: 1 addition & 1 deletion CRM/Mailing/Selector/Browse.php
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ public function whereClause(&$params, $sortBy = TRUE) {
$clauses[] = "civicrm_mailing.is_archived = 1";
}
else {
$clauses[] = "(civicrm_mailing.is_archived IS NULL OR civicrm_mailing.is_archived = 0)";
$clauses[] = "civicrm_mailing.is_archived = 0";
}
}

Expand Down
2 changes: 1 addition & 1 deletion CRM/Member/ActionMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function createQuery($schedule, $phase, $defaultParams): CRM_Utils_SQL_Se
// scheduling another kind of reminder might not expect members to be
// excluded if they have status overrides. Ideally there would be some kind
// of setting per reminder.
$query->where("( e.is_override IS NULL OR e.is_override = 0 )");
$query->where("e.is_override = 0");

// FIXME: Similarly to overrides, excluding contacts who can't edit the
// primary member makes sense in the context of renewals (see CRM-11342) but
Expand Down
10 changes: 5 additions & 5 deletions CRM/Member/BAO/Membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ public static function getContactMembership($contactID, $memType, $isTest, $memb
$dao->is_test = $isTest;
}
else {
$dao->whereAdd('is_test IS NULL OR is_test = 0');
$dao->whereAdd('is_test = 0');
}

//avoid pending membership as current membership: CRM-3027
Expand Down Expand Up @@ -1027,7 +1027,7 @@ public static function getMembershipStarts($membershipTypeId, $startDate, $endDa

$testClause = 'membership.is_test = 1';
if (!$isTest) {
$testClause = '( membership.is_test IS NULL OR membership.is_test = 0 )';
$testClause = '( membership.is_test = 0 )';
}

if (!self::$_signupActType || !self::$_renewalActType) {
Expand Down Expand Up @@ -1686,7 +1686,7 @@ public static function isSubscriptionCancelled(int $membershipID): bool {
public static function getMembershipJoins($membershipTypeId, $startDate, $endDate, $isTest = 0) {
$testClause = 'membership.is_test = 1';
if (!$isTest) {
$testClause = '( membership.is_test IS NULL OR membership.is_test = 0 )';
$testClause = '( membership.is_test = 0 )';
}
if (!self::$_signupActType) {
self::_getActTypes();
Expand Down Expand Up @@ -1739,7 +1739,7 @@ public static function getMembershipJoins($membershipTypeId, $startDate, $endDat
public static function getMembershipRenewals($membershipTypeId, $startDate, $endDate, $isTest = 0) {
$testClause = 'membership.is_test = 1';
if (!$isTest) {
$testClause = '( membership.is_test IS NULL OR membership.is_test = 0 )';
$testClause = '( membership.is_test = 0 )';
}
if (!self::$_renewalActType) {
self::_getActTypes();
Expand Down Expand Up @@ -2282,7 +2282,7 @@ public static function updateAllMembershipStatus($params = []) {
self::processOverriddenUntilDateMembership($dao1);
}

$query = $baseQuery . " AND (civicrm_membership.is_override = 0 OR civicrm_membership.is_override IS NULL)
$query = $baseQuery . " AND civicrm_membership.is_override = 0
AND {$membershipStatusClause}
AND civicrm_membership.owner_membership_id IS NULL ";

Expand Down
2 changes: 1 addition & 1 deletion CRM/PCP/Form/PCP.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public function buildQuickForm() {
'event' => ts('Event'),
];
$contribPages = ['' => ts('- select -')] + CRM_Contribute_PseudoConstant::contributionPage();
$eventPages = ['' => ts('- select -')] + CRM_Event_PseudoConstant::event(NULL, FALSE, "( is_template IS NULL OR is_template != 1 )");
$eventPages = ['' => ts('- select -')] + CRM_Event_PseudoConstant::event(NULL, FALSE, "is_template = 0");

$this->addElement('select', 'status_id', ts('Status'), $status);
$this->addElement('select', 'page_type', ts('Source Type'), $types);
Expand Down
2 changes: 1 addition & 1 deletion CRM/PCP/Page/PCP.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public function browse($action = NULL) {
// although if target is contribution page this might not be correct. fixme? dgg
$query = "SELECT id, title, start_date, end_date, registration_start_date, registration_end_date
FROM civicrm_event
WHERE is_template IS NULL OR is_template != 1";
WHERE is_template = 0";
$epages = CRM_Core_DAO::executeQuery($query);
while ($epages->fetch()) {
$pages['event'][$epages->id]['id'] = $epages->id;
Expand Down
2 changes: 1 addition & 1 deletion CRM/Utils/Migrate/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ public function customGroups(&$xml, &$idMap) {

case 2:
// ParticipantEventName
$condition = "( is_template IS NULL OR is_template != 1 ) AND title IN( '{$optValues}' )";
$condition = "is_template = 0 AND title IN( '{$optValues}' )";
$optionIDs = CRM_Event_PseudoConstant::event(NULL, FALSE, $condition);
break;

Expand Down

0 comments on commit c666dfb

Please sign in to comment.