code lisibility & filter optimization

This commit is contained in:
Philipp Holzer 2019-03-01 17:15:34 +01:00 committed by Hypolite Petovan
parent 4496df79ab
commit ec2c84a4e9
4 changed files with 21 additions and 22 deletions

View File

@ -196,7 +196,7 @@ class ForumManager
*/ */
public static function countUnseenItems() public static function countUnseenItems()
{ {
$r = DBA::p( $stmtContacts = DBA::p(
"SELECT `contact`.`id`, `contact`.`name`, COUNT(*) AS `count` FROM `item` "SELECT `contact`.`id`, `contact`.`name`, COUNT(*) AS `count` FROM `item`
INNER JOIN `contact` ON `item`.`contact-id` = `contact`.`id` INNER JOIN `contact` ON `item`.`contact-id` = `contact`.`id`
WHERE `item`.`uid` = %d AND `item`.`visible` AND NOT `item`.`deleted` AND `item`.`unseen` WHERE `item`.`uid` = %d AND `item`.`visible` AND NOT `item`.`deleted` AND `item`.`unseen`
@ -208,6 +208,6 @@ class ForumManager
intval(local_user()) intval(local_user())
); );
return DBA::toArray($r); return DBA::toArray($stmtContacts);
} }
} }

View File

@ -36,7 +36,7 @@ class NotificationsManager extends BaseObject
* - msg_plain: message as plain text string * - msg_plain: message as plain text string
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/ */
private function _set_extra($notes) private function _set_extra(array $notes)
{ {
$rets = []; $rets = [];
foreach ($notes as $n) { foreach ($notes as $n) {
@ -73,10 +73,10 @@ class NotificationsManager extends BaseObject
$dbFilter = array_merge($filter, ['uid' => local_user()]); $dbFilter = array_merge($filter, ['uid' => local_user()]);
$r = DBA::select('notify', [], $dbFilter, $order, $params); $stmtNotifies = DBA::select('notify', [], $dbFilter, $order, $params);
if (DBA::isResult($r)) { if (DBA::isResult($stmtNotifies)) {
return $this->_set_extra($r); return $this->_set_extra(DBA::toArray($stmtNotifies));
} }
return false; return false;
@ -91,9 +91,9 @@ class NotificationsManager extends BaseObject
*/ */
public function getByID($id) public function getByID($id)
{ {
$r = DBA::selectFirst('notify', ['id' => $id, 'uid' => local_user()]); $stmtNotify = DBA::selectFirst('notify', ['id' => $id, 'uid' => local_user()]);
if (DBA::isResult($r)) { if (DBA::isResult($stmtNotify)) {
return $this->_set_extra($r)[0]; return $this->_set_extra([$stmtNotify])[0];
} }
return null; return null;
} }
@ -423,22 +423,21 @@ class NotificationsManager extends BaseObject
$notifs = []; $notifs = [];
$sql_seen = ""; $sql_seen = "";
$filter = ['uid' => local_user()];
if ($seen === 0) { if ($seen === 0) {
$filter = ['`uid` = ? AND NOT `seen`', local_user()]; $filter['seen'] = false;
} else {
$filter = ['uid' => local_user()];
} }
$params = []; $params = [];
$params['limit'] = [$start, $limit]; $params['limit'] = [$start, $limit];
$r = DBA::select('notify', $stmtNotifies = DBA::select('notify',
['id', 'url', 'photo', 'msg', 'date', 'seen', 'verb'], ['id', 'url', 'photo', 'msg', 'date', 'seen', 'verb'],
$filter, $filter,
$params); $params);
if (DBA::isResult($r)) { if (DBA::isResult($stmtNotifies)) {
$notifs = $this->formatNotifs(DBA::toArray($r), $ident); $notifs = $this->formatNotifs(DBA::toArray($stmtNotifies), $ident);
} }
$arr = [ $arr = [
@ -561,7 +560,7 @@ class NotificationsManager extends BaseObject
} }
/// @todo Fetch contact details by "Contact::getDetailsByUrl" instead of queries to contact, fcontact and gcontact /// @todo Fetch contact details by "Contact::getDetailsByUrl" instead of queries to contact, fcontact and gcontact
$r = DBA::p( $stmtNotifies = DBA::p(
"SELECT `intro`.`id` AS `intro_id`, `intro`.*, `contact`.*, "SELECT `intro`.`id` AS `intro_id`, `intro`.*, `contact`.*,
`fcontact`.`name` AS `fname`, `fcontact`.`url` AS `furl`, `fcontact`.`addr` AS `faddr`, `fcontact`.`name` AS `fname`, `fcontact`.`url` AS `furl`, `fcontact`.`addr` AS `faddr`,
`fcontact`.`photo` AS `fphoto`, `fcontact`.`request` AS `frequest`, `fcontact`.`photo` AS `fphoto`, `fcontact`.`request` AS `frequest`,
@ -578,8 +577,8 @@ class NotificationsManager extends BaseObject
intval($start), intval($start),
intval($limit) intval($limit)
); );
if (DBA::isResult($r)) { if (DBA::isResult($stmtNotifies)) {
$notifs = $this->formatIntros(DBA::toArray($r)); $notifs = $this->formatIntros(DBA::toArray($stmtNotifies));
} }
$arr = [ $arr = [

View File

@ -36,11 +36,11 @@ class UserImport
*/ */
private static function checkCols($table, &$arr) private static function checkCols($table, &$arr)
{ {
$r = DBStructure::getColumns($table); $tableColumns = DBStructure::getColumns($table);
$tcols = []; $tcols = [];
// get a plain array of column names // get a plain array of column names
foreach ($r as $tcol) { foreach ($tableColumns as $tcol) {
$tcols[] = $tcol['Field']; $tcols[] = $tcol['Field'];
} }
// remove inexistent columns // remove inexistent columns

View File

@ -855,7 +855,7 @@ class DBStructure
*/ */
public static function getColumns($table) public static function getColumns($table)
{ {
$r = DBA::p("SHOW COLUMNS FROM `" . $table . "`"); $stmtColumns = DBA::p("SHOW COLUMNS FROM `" . $table . "`");
return DBA::toArray($r); return DBA::toArray($stmtColumns);
} }
} }