Skip to content

Commit

Permalink
pkp#6264 DAO adaptations
Browse files Browse the repository at this point in the history
  • Loading branch information
asmecher committed Oct 2, 2020
1 parent 744d4ad commit 0e4531d
Show file tree
Hide file tree
Showing 14 changed files with 117 additions and 264 deletions.
27 changes: 9 additions & 18 deletions classes/context/CategoryDAO.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ class CategoryDAO extends DAO {
* @param $categoryId int
* @param $contextId int optional
* @param $parentId int optional
* @return Category
* @return Category?
*/
function getById($categoryId, $contextId = null, $parentId = null) {
$params = array((int) $categoryId);
$params = [(int) $categoryId];
if ($contextId) $params[] = (int) $contextId;
if ($parentId) $params[] = (int) $parentId;

Expand All @@ -38,33 +38,24 @@ function getById($categoryId, $contextId = null, $parentId = null) {
$params
);

$returner = null;
if ($result->RecordCount() != 0) {
$returner = $this->_fromRow($result->GetRowAssoc(false));
}
$result->Close();
return $returner;
$row = (array) $result->current();
return $row?$this->_fromRow($row):null;
}

/**
* Retrieve a category by path.
* @param $path string
* @param $contextId int
* @return Category
* @return Category?
*/
function getByPath($path, $contextId) {
$returner = null;
$result = $this->retrieve(
'SELECT * FROM categories WHERE path = ? AND context_id = ?',
array((string) $path, (int) $contextId)
[(string) $path, (int) $contextId]
);

if ($result->RecordCount() != 0) {
$returner = $this->_fromRow($result->GetRowAssoc(false));
}

$result->Close();
return $returner;
$row = (array) $result->current();
return $row?$this->_fromRow($row):null;
}

/**
Expand Down Expand Up @@ -112,7 +103,7 @@ public function getByPublicationId($publicationId) {
FROM categories c
INNER JOIN publication_categories pc ON (pc.category_id = c.category_id)
WHERE pc.publication_id = ?',
(int) $publicationId
[(int) $publicationId]
);

return new DAOResultFactory($result, $this, '_fromRow');
Expand Down
70 changes: 15 additions & 55 deletions classes/db/DAO.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function __construct($callHooks = true) {
* @param $params array parameters for the SQL statement
* @return ADORecordSet
*/
function retrieve($sql, $params = false, $callHooks = true) {
function retrieve($sql, $params = [], $callHooks = true) {
if ($callHooks === true) {
$trace = debug_backtrace();
// Call hooks based on the calling entity, assuming
Expand All @@ -62,20 +62,16 @@ function retrieve($sql, $params = false, $callHooks = true) {
}
}

yield from Capsule::cursor(Capsule::raw($sql), $this->_prepareParameters($params));
yield from Capsule::cursor(Capsule::raw($sql), $params);
}

private function _prepareParameters($params) {
if ($params === false) return [];
return $params;
}
/**
* Execute a cached SELECT SQL statement.
* @param $sql string the SQL statement
* @param $params array parameters for the SQL statement
* @return ADORecordSet
*/
function &retrieveCached($sql, $params = false, $secsToCache = 3600, $callHooks = true) {
function &retrieveCached($sql, $params = [], $secsToCache = 3600, $callHooks = true) {
throw new Exception('BROKEN');
if ($callHooks === true) {
$trace = debug_backtrace();
Expand All @@ -101,41 +97,13 @@ function &retrieveCached($sql, $params = false, $secsToCache = 3600, $callHooks
return $result;
}

/**
* Execute a SELECT SQL statement with LIMIT on the rows returned.
* @param $sql string the SQL statement
* @param $params array parameters for the SQL statement
* @param $numRows int maximum number of rows to return in the result set
* @param $offset int row offset in the result set
* @return ADORecordSet
*/
function retrieveLimit($sql, $params = false, $numRows = false, $offset = false, $callHooks = true) {
if ($callHooks === true) {
$trace = debug_backtrace();
// Call hooks based on the calling entity, assuming
// this method is only called by a subclass. Results
// in hook calls named e.g. "sessiondao::_getsession"
// (all lowercase).
$value = null;
if (HookRegistry::call(strtolower_codesafe($trace[1]['class'] . '::_' . $trace[1]['function']), array(&$sql, &$params, &$numRows, &$offset, &$value))) {
return $value;
}
}
$query = Capsule::raw($sql);
if ($offset !== false) $query->skip($offset);
if ($numRows !== false) $query->take($numRows);
yield from Capsule::cursor($query, $this->_prepareParameters($params));
}

/**
* Execute a SELECT SQL statment, returning rows in the range supplied.
* @param $sql string the SQL statement
* @param $params array parameters for the SQL statement
* @param $dbResultRange DBResultRange object describing the desired range
*/
function retrieveRange($sql, $params = false, $dbResultRange = null, $callHooks = true) {
if ($dbResultRange === null) return $this->retrieve($sql, $params, $callHooks);
throw new Exception('BROKEN');
function retrieveRange($sql, $params = [], $dbResultRange = null, $callHooks = true) {
if ($callHooks === true) {
$trace = debug_backtrace();
// Call hooks based on the calling entity, assuming
Expand All @@ -147,22 +115,14 @@ function retrieveRange($sql, $params = false, $dbResultRange = null, $callHooks
}
}

if (isset($dbResultRange) && $dbResultRange->isValid()) {
$start = Core::microtime();
$dataSource = $this->getDataSource();
if (is_null($dbResultRange->getOffset())) {
$result = $dataSource->PageExecute($sql, $dbResultRange->getCount(), $dbResultRange->getPage(), $params);
} else {
$result = $dataSource->SelectLimit($sql, $dbResultRange->getCount(), $dbResultRange->getOffset(), $params);
}
if ($dataSource->errorNo()) {
$this->handleError($dataSource, $sql);
}
if ($dbResultRange && $dbResultRange->isValid()) {
$sql .= ' LIMIT ' . (int) $dbResultRange->getCount();
$offset = (int) $dbResultRange->getOffset();
$offset += $dbResultRange->getPage() * Config::getVar('interface', 'items_per_page');
$sql .= ' OFFSET ' . $offset;
}
else {
$result = $this->retrieve($sql, $this->_prepareParameters($params));
}
return $result;

yield from Capsule::cursor(Capsule::raw($sql), $params);
}

/**
Expand All @@ -173,7 +133,7 @@ function retrieveRange($sql, $params = false, $dbResultRange = null, $callHooks
* @param $dieOnError boolean Whether or not to die if an error occurs
* @return int Affected row count
*/
function update($sql, $params = false, $callHooks = true, $dieOnError = true) {
function update($sql, $params = [], $callHooks = true, $dieOnError = true) {
if ($callHooks === true) {
$trace = debug_backtrace();
// Call hooks based on the calling entity, assuming
Expand All @@ -186,7 +146,7 @@ function update($sql, $params = false, $callHooks = true, $dieOnError = true) {
}
}

return Capsule::affectingStatement($sql, $this->_prepareParameters($params));
return Capsule::affectingStatement($sql, $params);
}

/**
Expand Down Expand Up @@ -523,10 +483,10 @@ function updateDataObjectSettings($tableName, $dataObject, $idArray) {
function getDataObjectSettings($tableName, $idFieldName, $idFieldValue, $dataObject) {
if ($idFieldName !== null) {
$sql = "SELECT * FROM $tableName WHERE $idFieldName = ?";
$params = array($idFieldValue);
$params = [$idFieldValue];
} else {
$sql = "SELECT * FROM $tableName";
$params = false;
$params = [];
}
$result = $this->retrieve($sql, $params);
foreach ($result as $row) {
Expand Down
12 changes: 4 additions & 8 deletions classes/db/SchemaDAO.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,16 @@ abstract public function newDataObject();
/**
* Retrieve an object by ID
* @param $objectId int
* @return DataObject
* @return DataObject?
*/
public function getById($objectId) {
$result = $this->retrieve(
'SELECT * FROM ' . $this->tableName . ' WHERE ' . $this->primaryKeyColumn . ' = ?',
(int) $objectId
[(int) $objectId]
);

$returner = null;
if ($result->RecordCount() != 0) {
$returner = $this->_fromRow($result->GetRowAssoc(false));
}
$result->Close();
return $returner;
$row = (array) $result->current();
return $row?$this->_fromRow($row):null;
}

/**
Expand Down
47 changes: 15 additions & 32 deletions classes/file/TemporaryFileDAO.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,16 @@ class TemporaryFileDAO extends DAO {
* Retrieve a temporary file by ID.
* @param $fileId int
* @param $userId int
* @return TemporaryFile
* @return TemporaryFile?
*/
function getTemporaryFile($fileId, $userId) {
$result = $this->retrieveLimit(
$result = $this->retrieve(
'SELECT t.* FROM temporary_files t WHERE t.file_id = ? and t.user_id = ?',
array((int) $fileId, (int) $userId),
1
[(int) $fileId, (int) $userId]
);

$returner = null;
if (isset($result) && $result->RecordCount() != 0) {
$returner = $this->_returnTemporaryFileFromRow($result->GetRowAssoc(false));
}

$result->Close();
return $returner;
$row = (array) $result->current();
return $row?$this->_returnTemporaryFileFromRow($row):null;
}

/**
Expand Down Expand Up @@ -129,39 +123,29 @@ function updateObject($temporaryFile) {
* @param $userId int
*/
function deleteTemporaryFileById($fileId, $userId) {
return $this->update(
'DELETE FROM temporary_files WHERE file_id = ? AND user_id = ?',
array((int) $fileId, (int) $userId)
);
return $this->update('DELETE FROM temporary_files WHERE file_id = ? AND user_id = ?', [(int) $fileId, (int) $userId]);
}

/**
* Delete temporary files by user ID.
* @param $userId int
*/
function deleteByUserId($userId) {
return $this->update(
'DELETE FROM temporary_files WHERE user_id = ?',
(int) $userId
);
return $this->update('DELETE FROM temporary_files WHERE user_id = ?', [(int) $userId]);
}

function &getExpiredFiles() {
/**
* Get all expired temorary files.
* @return array
*/
function getExpiredFiles() {
// Files older than one day can be cleaned up.
$expiryThresholdTimestamp = time() - (60 * 60 * 24);

$temporaryFiles = array();

$result = $this->retrieve(
'SELECT * FROM temporary_files WHERE date_uploaded < ' . $this->datetimeToDB($expiryThresholdTimestamp)
);

while (!$result->EOF) {
$temporaryFiles[] = $this->_returnTemporaryFileFromRow($result->GetRowAssoc(false));
$result->MoveNext();
$result = $this->retrieve('SELECT * FROM temporary_files WHERE date_uploaded < ' . $this->datetimeToDB($expiryThresholdTimestamp));
foreach ($result as $row) {
$temporaryFiles[] = $this->_returnTemporaryFileFromRow((array) $row);
}

$result->Close();
return $temporaryFiles;
}

Expand All @@ -174,4 +158,3 @@ function getInsertId() {
}
}


2 changes: 0 additions & 2 deletions classes/install/Installer.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
import('lib.pkp.classes.site.VersionDAO');
import('lib.pkp.classes.config.ConfigParser');

require_once './lib/pkp/lib/vendor/adodb/adodb-php/adodb-xmlschema.inc.php';

class Installer {

/** @var string descriptor path (relative to INSTALLER_DATA_DIR) */
Expand Down
19 changes: 8 additions & 11 deletions classes/mail/EmailTemplateDAO.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ public function _fromRow($primaryRow) {
[$emailTemplate->getData('key')]
);
$props = ['subject', 'body', 'description'];
while (!$result->EOF) {
$settingRow = $result->getRowAssoc(false);
foreach ($result as $settingRow) {
$settingRow = (array) $settingRow;
foreach ($props as $prop) {
// Don't allow default data to override custom template data
if ($emailTemplate->getData($prop, $settingRow['locale'])) {
Expand All @@ -118,9 +118,7 @@ public function _fromRow($primaryRow) {
$settingRow['locale']
);
}
$result->MoveNext();
}
$result->Close();

return $emailTemplate;
}
Expand All @@ -131,7 +129,7 @@ public function _fromRow($primaryRow) {
*/
function deleteEmailTemplatesByLocale($locale) {
$this->update(
'DELETE FROM email_templates_settings WHERE locale = ?', $locale
'DELETE FROM email_templates_settings WHERE locale = ?', [$locale]
);
}

Expand All @@ -141,7 +139,7 @@ function deleteEmailTemplatesByLocale($locale) {
*/
function deleteDefaultEmailTemplatesByLocale($locale) {
$this->update(
'DELETE FROM email_templates_default_data WHERE locale = ?', $locale
'DELETE FROM email_templates_default_data WHERE locale = ?', [$locale]
);
}

Expand All @@ -154,14 +152,13 @@ function deleteDefaultEmailTemplatesByLocale($locale) {
*/
function defaultTemplateIsInstalled($key) {
$result = $this->retrieve(
'SELECT COUNT(*)
'SELECT COUNT(*) AS row_count
FROM email_templates_default
WHERE email_key = ?',
$key
[$key]
);
$returner = isset($result->fields[0]) && $result->fields[0] != 0;
$result->Close();
return $returner;
$row = (array) $result->current();
return $row && $row['row_count'];
}

/**
Expand Down
12 changes: 4 additions & 8 deletions classes/security/RoleDAO.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,12 @@ function getUsersByRoleId($roleId = null, $contextId = null, $searchType = null,
function userHasRole($contextId, $userId, $roleId) {
$roleId = is_array($roleId) ? join(',', array_map('intval', $roleId)) : (int) $roleId;
$result = $this->retrieve(
'SELECT count(*) FROM user_groups ug JOIN user_user_groups uug ON ug.user_group_id = uug.user_group_id
'SELECT count(*) AS row_count FROM user_groups ug JOIN user_user_groups uug ON ug.user_group_id = uug.user_group_id
WHERE ug.context_id = ? AND uug.user_id = ? AND ug.role_id IN (' . $roleId . ')',
array((int) $contextId, (int) $userId)
[(int) $contextId, (int) $userId]
);

// > 0 because user could belong to more than one user group with this role
$returner = isset($result->fields[0]) && $result->fields[0] > 0 ? true : false;

$result->Close();
return $returner;
$row = (array) $result->current();
return $row && $row['row_count'];
}

/**
Expand Down
Loading

0 comments on commit 0e4531d

Please sign in to comment.