Skip to content

Commit

Permalink
pkp/pkp#8700 Optimized DAOResultFactory instances
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasraoni committed May 23, 2024
1 parent ef34503 commit 9c77909
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 86 deletions.
53 changes: 28 additions & 25 deletions classes/galley/DAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,35 +294,38 @@ public function getExportable($contextId, $pubIdType = null, $title = null, $aut
$params[] = $pubIdSettingValue;
}

$baseSql = '
FROM publication_galleys g
LEFT JOIN publications p ON (p.publication_id = g.publication_id)
LEFT JOIN publication_settings ps ON (ps.publication_id = p.publication_id)
LEFT JOIN submissions s ON (s.submission_id = p.submission_id)
LEFT JOIN submission_files sf ON (g.submission_file_id = sf.submission_file_id)
' . ($pubIdType != null ? ' LEFT JOIN publication_galley_settings gs ON (g.galley_id = gs.galley_id)' : '')
. ($title != null ? ' LEFT JOIN publication_settings pst ON (p.publication_id = pst.publication_id)' : '')
. ($author != null ? ' LEFT JOIN authors au ON (p.publication_id = au.publication_id)
LEFT JOIN author_settings asgs ON (asgs.author_id = au.author_id AND asgs.setting_name = \'' . Identity::IDENTITY_SETTING_GIVENNAME . '\')
LEFT JOIN author_settings asfs ON (asfs.author_id = au.author_id AND asfs.setting_name = \'' . Identity::IDENTITY_SETTING_FAMILYNAME . '\')
' : '')
. ($pubIdSettingName != null ? ' LEFT JOIN publication_galley_settings gss ON (g.galley_id = gss.galley_id AND gss.setting_name = ?)' : '') . '
WHERE
s.status = ? AND s.context_id = ?
' . ($pubIdType != null ? ' AND gs.setting_name = ? AND gs.setting_value IS NOT NULL' : '')
. ($title != null ? ' AND (pst.setting_name = ? AND pst.setting_value LIKE ?)' : '')
. ($author != null ? ' AND (asgs.setting_value LIKE ? OR asfs.setting_value LIKE ?)' : '')
. ($issueId != null ? ' AND (ps.setting_name = \'issueId\' AND ps.setting_value = ? AND ps.locale = \'\'' : '')
. (($pubIdSettingName != null && $pubIdSettingValue != null && $pubIdSettingValue == EXPORT_STATUS_NOT_DEPOSITED) ? ' AND gss.setting_value IS NULL' : '')
. (($pubIdSettingName != null && $pubIdSettingValue != null && $pubIdSettingValue != EXPORT_STATUS_NOT_DEPOSITED) ? ' AND gss.setting_value = ?' : '')
. (($pubIdSettingName != null && is_null($pubIdSettingValue)) ? ' AND (gss.setting_value IS NULL OR gss.setting_value = \'\')' : '');

$result = $this->deprecatedDao->retrieveRange(
$sql = 'SELECT g.*
FROM publication_galleys g
LEFT JOIN publications p ON (p.publication_id = g.publication_id)
LEFT JOIN publication_settings ps ON (ps.publication_id = p.publication_id)
LEFT JOIN submissions s ON (s.submission_id = p.submission_id)
LEFT JOIN submission_files sf ON (g.submission_file_id = sf.submission_file_id)
' . ($pubIdType != null ? ' LEFT JOIN publication_galley_settings gs ON (g.galley_id = gs.galley_id)' : '')
. ($title != null ? ' LEFT JOIN publication_settings pst ON (p.publication_id = pst.publication_id)' : '')
. ($author != null ? ' LEFT JOIN authors au ON (p.publication_id = au.publication_id)
LEFT JOIN author_settings asgs ON (asgs.author_id = au.author_id AND asgs.setting_name = \'' . Identity::IDENTITY_SETTING_GIVENNAME . '\')
LEFT JOIN author_settings asfs ON (asfs.author_id = au.author_id AND asfs.setting_name = \'' . Identity::IDENTITY_SETTING_FAMILYNAME . '\')
' : '')
. ($pubIdSettingName != null ? ' LEFT JOIN publication_galley_settings gss ON (g.galley_id = gss.galley_id AND gss.setting_name = ?)' : '') . '
WHERE
s.status = ? AND s.context_id = ?
' . ($pubIdType != null ? ' AND gs.setting_name = ? AND gs.setting_value IS NOT NULL' : '')
. ($title != null ? ' AND (pst.setting_name = ? AND pst.setting_value LIKE ?)' : '')
. ($author != null ? ' AND (asgs.setting_value LIKE ? OR asfs.setting_value LIKE ?)' : '')
. ($issueId != null ? ' AND (ps.setting_name = \'issueId\' AND ps.setting_value = ? AND ps.locale = \'\'' : '')
. (($pubIdSettingName != null && $pubIdSettingValue != null && $pubIdSettingValue == EXPORT_STATUS_NOT_DEPOSITED) ? ' AND gss.setting_value IS NULL' : '')
. (($pubIdSettingName != null && $pubIdSettingValue != null && $pubIdSettingValue != EXPORT_STATUS_NOT_DEPOSITED) ? ' AND gss.setting_value = ?' : '')
. (($pubIdSettingName != null && is_null($pubIdSettingValue)) ? ' AND (gss.setting_value IS NULL OR gss.setting_value = \'\')' : '') . '
GROUP BY g.galley_id
ORDER BY p.date_published DESC, p.publication_id DESC, g.galley_id DESC',
"SELECT g.*
{$baseSql}
GROUP BY g.galley_id
ORDER BY p.date_published DESC, p.publication_id DESC, g.galley_id DESC",
$params,
$rangeInfo
);

return new DAOResultFactory($result, $this, '_fromRow', [], $sql, $params, $rangeInfo);
return new DAOResultFactory($result, $this, '_fromRow', [], "SELECT 0 {$baseSql}", $params, $rangeInfo);
}
}
19 changes: 10 additions & 9 deletions classes/log/EmailLogDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,20 @@ public function _getByEventType($assocType, $assocId, $eventType, $userId = null
$params[] = $userId;
}

$baseSql = '
FROM email_log e
' . ($userId ? ' LEFT JOIN email_log_users u ON e.log_id = u.email_log_id' : '') . '
WHERE e.assoc_type = ? AND
e.assoc_id = ? AND
e.event_type = ?
' . ($userId ? ' AND u.user_id = ?' : '');
$result = $this->retrieveRange(
$sql = 'SELECT e.*
FROM email_log e' .
($userId ? ' LEFT JOIN email_log_users u ON e.log_id = u.email_log_id' : '') .
' WHERE e.assoc_type = ? AND
e.assoc_id = ? AND
e.event_type = ?' .
($userId ? ' AND u.user_id = ?' : ''),
"SELECT e.* {$baseSql}",
$params,
$rangeInfo
);

return new DAOResultFactory($result, $this, 'build', [], $sql, $params, $rangeInfo); // Counted in submissionEmails.tpl
return new DAOResultFactory($result, $this, 'build', [], "SELECT 0 {$baseSql}", $params, $rangeInfo); // Counted in submissionEmails.tpl
}

/**
Expand Down Expand Up @@ -225,7 +226,7 @@ public function changeUser($oldUserId, $newUserId)
[(int) $newUserId, (int) $oldUserId]
),
$this->update(
'UPDATE email_log_users
'UPDATE email_log_users
SET user_id = ?
WHERE user_id = ? AND email_log_id NOT IN (SELECT t1.email_log_id
FROM (SELECT email_log_id FROM email_log_users WHERE user_id = ?) AS t1
Expand Down
14 changes: 7 additions & 7 deletions classes/reviewForm/ReviewFormElementDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,18 +233,18 @@ public function deleteSetting($reviewFormElementId, $name, $locale = null)
*/
public function getByReviewFormId($reviewFormId, $rangeInfo = null, $included = null)
{
$baseSql = '
FROM review_form_elements
WHERE review_form_id = ?
' . ($included === true ? ' AND included = 1' : '') . '
' . ($included === false ? ' AND included = 0' : '');
$result = $this->retrieveRange(
$sql = 'SELECT *
FROM review_form_elements
WHERE review_form_id = ?
' . ($included === true ? ' AND included = 1' : '') . '
' . ($included === false ? ' AND included = 0' : '') . '
ORDER BY seq',
"SELECT * {$baseSql} ORDER BY seq",
$params = [(int) $reviewFormId],
$rangeInfo
);

return new DAOResultFactory($result, $this, '_fromRow', [], $sql, $params);
return new DAOResultFactory($result, $this, '_fromRow', [], "SELECT 0 {$baseSql}", $params);
}

/**
Expand Down
29 changes: 11 additions & 18 deletions classes/submission/SubmissionCommentDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,24 +94,17 @@ public function getReviewerCommentsByReviewerId($submissionId, $reviewerId = nul
if ($reviewId) {
$params[] = (int) $reviewId;
}
return new DAOResultFactory(
$this->retrieve(
$sql = 'SELECT a.*
FROM submission_comments a
WHERE submission_id = ?
' . ($reviewerId ? ' AND author_id = ?' : '') . '
' . ($reviewId ? ' AND assoc_id = ?' : '') . '
' . ($viewable === true ? ' AND viewable = 1' : '') . '
' . ($viewable === false ? ' AND viewable = 0' : '') . '
ORDER BY date_posted DESC',
$params
),
$this,
'_fromRow',
[],
$sql,
$params // Counted in readReview.tpl and authorReadReview.tpl
);

$baseSql = '
FROM submission_comments a
WHERE submission_id = ?
' . ($reviewerId ? ' AND author_id = ?' : '') . '
' . ($reviewId ? ' AND assoc_id = ?' : '') . '
' . ($viewable === true ? ' AND viewable = 1' : '') . '
' . ($viewable === false ? ' AND viewable = 0' : '');

$result = $this->retrieve("SELECT a.* {$baseSql} ORDER BY date_posted DESC", $params);
return new DAOResultFactory($result, $this, '_fromRow', [], "SELECT 0 {$baseSql}", $params); // Counted in readReview.tpl and authorReadReview.tpl
}

/**
Expand Down
16 changes: 7 additions & 9 deletions classes/submission/reviewRound/ReviewRoundDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,13 @@ public function getBySubmissionId($submissionId, $stageId = null, $round = null)
$params[] = $round;
}

$result = $this->retrieve(
$sql = 'SELECT * FROM review_rounds WHERE submission_id = ?' .
($stageId ? ' AND stage_id = ?' : '') .
($round ? ' AND round = ?' : '') .
' ORDER BY stage_id ASC, round ASC',
$params
);

return new DAOResultFactory($result, $this, '_fromRow', [], $sql, $params);
$baseSql = '
FROM review_rounds
WHERE submission_id = ?
' . ($stageId ? ' AND stage_id = ?' : '') . '
' . ($round ? ' AND round = ?' : '');
$result = $this->retrieve("SELECT * {$baseSql} ORDER BY stage_id ASC, round ASC", $params);
return new DAOResultFactory($result, $this, '_fromRow', [], "SELECT 0 {$baseSql}", $params);
}

/**
Expand Down
33 changes: 15 additions & 18 deletions classes/user/UserStageAssignmentDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,25 +70,22 @@ public function filterUsersNotAssignedToStageInUserGroup($submissionId, $stageId
if ($name !== null) {
$params = array_merge($params, array_fill(0, 6, '%' . (string) $name . '%'));
}
$result = $this->retrieveRange(
$sql = 'SELECT u.*
FROM users u
LEFT JOIN user_user_groups uug ON (u.user_id = uug.user_id)
LEFT JOIN stage_assignments s ON (s.user_id = uug.user_id AND s.user_group_id = uug.user_group_id AND s.submission_id = ?)
JOIN user_group_stage ugs ON (uug.user_group_id = ugs.user_group_id AND ugs.stage_id = ?)
LEFT JOIN user_settings usgs_pl ON (usgs_pl.user_id = u.user_id AND usgs_pl.setting_name = ? AND usgs_pl.locale = ?)
LEFT JOIN user_settings usfs_pl ON (usfs_pl.user_id = u.user_id AND usfs_pl.setting_name = ? AND usfs_pl.locale = ?)
LEFT JOIN user_settings usgs_l ON (usgs_l.user_id = u.user_id AND usgs_l.setting_name = ? AND usgs_l.locale = ?)
LEFT JOIN user_settings usfs_l ON (usfs_l.user_id = u.user_id AND usfs_l.setting_name = ? AND usfs_l.locale = ?)

WHERE uug.user_group_id = ? AND
s.user_group_id IS NULL'
. ($name !== null ? ' AND (usgs_pl.setting_value LIKE ? OR usgs_l.setting_value LIKE ? OR usfs_pl.setting_value LIKE ? OR usfs_l.setting_value LIKE ? OR u.username LIKE ? OR u.email LIKE ?)' : '')
. ' ORDER BY COALESCE(usfs_l.setting_value, usfs_pl.setting_value)',
$params,
$rangeInfo
);
return new DAOResultFactory($result, $this, '_returnUserFromRowWithData', [], $sql, $params, $rangeInfo);
$baseSql = '
FROM users u
LEFT JOIN user_user_groups uug ON (u.user_id = uug.user_id)
LEFT JOIN stage_assignments s ON (s.user_id = uug.user_id AND s.user_group_id = uug.user_group_id AND s.submission_id = ?)
JOIN user_group_stage ugs ON (uug.user_group_id = ugs.user_group_id AND ugs.stage_id = ?)
LEFT JOIN user_settings usgs_pl ON (usgs_pl.user_id = u.user_id AND usgs_pl.setting_name = ? AND usgs_pl.locale = ?)
LEFT JOIN user_settings usfs_pl ON (usfs_pl.user_id = u.user_id AND usfs_pl.setting_name = ? AND usfs_pl.locale = ?)
LEFT JOIN user_settings usgs_l ON (usgs_l.user_id = u.user_id AND usgs_l.setting_name = ? AND usgs_l.locale = ?)
LEFT JOIN user_settings usfs_l ON (usfs_l.user_id = u.user_id AND usfs_l.setting_name = ? AND usfs_l.locale = ?)
WHERE uug.user_group_id = ? AND
s.user_group_id IS NULL'
. ($name !== null ? ' AND (usgs_pl.setting_value LIKE ? OR usgs_l.setting_value LIKE ? OR usfs_pl.setting_value LIKE ? OR usfs_l.setting_value LIKE ? OR u.username LIKE ? OR u.email LIKE ?)' : '');

$result = $this->retrieveRange("SELECT u.* {$baseSql} ORDER BY COALESCE(usfs_l.setting_value, usfs_pl.setting_value)", $params, $rangeInfo);
return new DAOResultFactory($result, $this, '_returnUserFromRowWithData', [], "SELECT 0 {$baseSql}", $params, $rangeInfo);
}
}

Expand Down

0 comments on commit 9c77909

Please sign in to comment.