replace "p" with higher level database functions

This commit is contained in:
Michael 2021-10-12 05:53:29 +00:00
parent 948450c24b
commit 27b9da3df5
8 changed files with 30 additions and 71 deletions

View File

@ -43,6 +43,8 @@ function msearch_post(App $a)
$total = 0; $total = 0;
$condition = ["`net-publish` AND MATCH(`pub_keywords`) AGAINST (?)", $search];
$total = DBA::count('owner-view', $condition);
$count_stmt = DBA::p( $count_stmt = DBA::p(
"SELECT COUNT(*) AS `total` "SELECT COUNT(*) AS `total`
FROM `profile` FROM `profile`
@ -58,18 +60,7 @@ function msearch_post(App $a)
DBA::close($count_stmt); DBA::close($count_stmt);
$search_stmt = DBA::p( $search_stmt = DBA::select('owner-view', ['pub_keywords', 'name', 'nickname', 'uid'], $condition, ['limit' => [$startrec, $perpage]]);
"SELECT `pub_keywords`, `username`, `nickname`, `user`.`uid`
FROM `user`
JOIN `profile` ON `user`.`uid` = `profile`.`uid`
WHERE `profile`.`net-publish`
AND MATCH(`pub_keywords`) AGAINST (?)
LIMIT ?, ?",
$search,
$startrec,
$perpage
);
while ($search_result = DBA::fetch($search_stmt)) { while ($search_result = DBA::fetch($search_stmt)) {
$results[] = [ $results[] = [
'name' => $search_result['name'], 'name' => $search_result['name'],

View File

@ -106,7 +106,7 @@ HELP;
$this->errored = 0; $this->errored = 0;
do { do {
$result = $this->dba->p('SELECT `id`, `parameter` FROM `workerqueue` WHERE `command` = "APDelivery" AND `parameter` LIKE "[\"%\",\"\",%" LIMIT ' . $this->examined . ', 100'); $result = $this->dba->select('workerqueue', ['id', 'parameter'], ["`command` = ? AND `parameter` LIKE ?", "APDelivery", "[\"%\",\"\",%"], ['limit' => [$this->examined, 100]]);
while ($row = $this->dba->fetch($result)) { while ($row = $this->dba->fetch($result)) {
$this->examined++; $this->examined++;
$this->processRow($row); $this->processRow($row);

View File

@ -712,13 +712,10 @@ class Worker
} }
$stamp = (float)microtime(true); $stamp = (float)microtime(true);
$jobs = DBA::p("SELECT COUNT(*) AS `jobs` FROM `workerqueue` WHERE `done` AND `executed` > UTC_TIMESTAMP() - INTERVAL ? MINUTE", $interval); $jobs = DBA::count('workerqueue', ["`done` AND `executed` > UTC_TIMESTAMP() - INTERVAL ? MINUTE", $interval]);
self::$db_duration += (microtime(true) - $stamp); self::$db_duration += (microtime(true) - $stamp);
self::$db_duration_stat += (microtime(true) - $stamp); self::$db_duration_stat += (microtime(true) - $stamp);
if ($job = DBA::fetch($jobs)) { $jobs_per_minute[$interval] = number_format($jobs / $interval, 0);
$jobs_per_minute[$interval] = number_format($job['jobs'] / $interval, 0);
}
DBA::close($jobs);
} }
$processlist = ' - jpm: '.implode('/', $jobs_per_minute); $processlist = ' - jpm: '.implode('/', $jobs_per_minute);
} }
@ -739,15 +736,12 @@ class Worker
self::$db_duration_stat += (microtime(true) - $stamp); self::$db_duration_stat += (microtime(true) - $stamp);
while ($entry = DBA::fetch($jobs)) { while ($entry = DBA::fetch($jobs)) {
$stamp = (float)microtime(true); $stamp = (float)microtime(true);
$processes = DBA::p("SELECT COUNT(*) AS `running` FROM `workerqueue-view` WHERE `priority` = ?", $entry["priority"]); $running = DBA::count('workerqueue-view', ['priority' => $entry["priority"]]);
self::$db_duration += (microtime(true) - $stamp); self::$db_duration += (microtime(true) - $stamp);
self::$db_duration_stat += (microtime(true) - $stamp); self::$db_duration_stat += (microtime(true) - $stamp);
if ($process = DBA::fetch($processes)) { $idle_workers -= $running;
$idle_workers -= $process["running"]; $waiting_processes += $entry["entries"];
$waiting_processes += $entry["entries"]; $listitem[$entry["priority"]] = $entry["priority"] . ":" . $running . "/" . $entry["entries"];
$listitem[$entry["priority"]] = $entry["priority"].":".$process["running"]."/".$entry["entries"];
}
DBA::close($processes);
} }
DBA::close($jobs); DBA::close($jobs);
} else { } else {

View File

@ -3013,37 +3013,29 @@ class Contact
} }
// check supported networks // check supported networks
$networks = [Protocol::DFRN, Protocol::ACTIVITYPUB];
if (DI::config()->get('system', 'diaspora_enabled')) { if (DI::config()->get('system', 'diaspora_enabled')) {
$diaspora = Protocol::DIASPORA; $networks[] = Protocol::DIASPORA;
} else {
$diaspora = Protocol::DFRN;
} }
if (!DI::config()->get('system', 'ostatus_disabled')) { if (!DI::config()->get('system', 'ostatus_disabled')) {
$ostatus = Protocol::OSTATUS; $networks[] = Protocol::OSTATUS;
} else {
$ostatus = Protocol::DFRN;
} }
$condition = ['network' => $networks, 'failed' => false, 'uid' => $uid];
// check if we search only communities or every contact // check if we search only communities or every contact
if ($mode === 'community') { if ($mode === 'community') {
$extra_sql = sprintf(' AND `contact-type` = %d', self::TYPE_COMMUNITY); $condition['contact-type'] = self::TYPE_COMMUNITY;
} else {
$extra_sql = '';
} }
$search .= '%'; $search .= '%';
$results = DBA::p("SELECT * FROM `contact` $condition = DBA::mergeConditions($condition,
WHERE (NOT `unsearchable` OR `nurl` IN (SELECT `nurl` FROM `owner-view` where `publish` OR `net-publish`)) ["(NOT `unsearchable` OR `nurl` IN (SELECT `nurl` FROM `owner-view` where `publish` OR `net-publish`))
AND `network` IN (?, ?, ?, ?) AND AND (`addr` LIKE ? OR `name` LIKE ? OR `nick` LIKE ?)", $search, $search, $search]);
NOT `failed` AND `uid` = ? AND
(`addr` LIKE ? OR `name` LIKE ? OR `nick` LIKE ?) $extra_sql
ORDER BY `nurl` DESC LIMIT 1000",
Protocol::DFRN, Protocol::ACTIVITYPUB, $ostatus, $diaspora, $uid, $search, $search, $search
);
$contacts = DBA::toArray($results); $contacts = self::selectToArray([], $condition);
return $contacts; return $contacts;
} }

View File

@ -1667,14 +1667,10 @@ class GServer
$last_update = date('c', time() - (60 * 60 * 24 * $requery_days)); $last_update = date('c', time() - (60 * 60 * 24 * $requery_days));
$gservers = DBA::p("SELECT `id`, `url`, `nurl`, `network`, `poco`, `directory-type` $gservers = DBA::select('gserver', ['id', 'url', 'nurl', 'network', 'poco', 'directory-type'],
FROM `gserver` ["NOT `failed` AND `directory-type` != ? AND `last_poco_query` < ?", GServer::DT_NONE, $last_update],
WHERE NOT `failed` ['order' => ['RAND()']]);
AND `directory-type` != ?
AND `last_poco_query` < ?
ORDER BY RAND()", self::DT_NONE, $last_update
);
while ($gserver = DBA::fetch($gservers)) { while ($gserver = DBA::fetch($gservers)) {
Logger::info('Update peer list', ['server' => $gserver['url'], 'id' => $gserver['id']]); Logger::info('Update peer list', ['server' => $gserver['url'], 'id' => $gserver['id']]);
Worker::add(PRIORITY_LOW, 'UpdateServerPeers', $gserver['url']); Worker::add(PRIORITY_LOW, 'UpdateServerPeers', $gserver['url']);

View File

@ -166,21 +166,17 @@ class Transmitter
'pending' => false, 'pending' => false,
'blocked' => false, 'blocked' => false,
]; ];
$condition = DBA::buildCondition($parameters);
$sql = "SELECT COUNT(*) as `count` $condition = DBA::mergeConditions($parameters, ["`url` IN (SELECT `url` FROM `apcontact`)"]);
FROM `contact`
JOIN `apcontact` ON `apcontact`.`url` = `contact`.`url`
" . $condition;
$contacts = DBA::fetchFirst($sql, ...$parameters); $total = DBA::count('contact', $condition);
$modulePath = '/' . $module . '/'; $modulePath = '/' . $module . '/';
$data = ['@context' => ActivityPub::CONTEXT]; $data = ['@context' => ActivityPub::CONTEXT];
$data['id'] = DI::baseUrl() . $modulePath . $owner['nickname']; $data['id'] = DI::baseUrl() . $modulePath . $owner['nickname'];
$data['type'] = 'OrderedCollection'; $data['type'] = 'OrderedCollection';
$data['totalItems'] = $contacts['count']; $data['totalItems'] = $total;
// When we hide our friends we will only show the pure number but don't allow more. // When we hide our friends we will only show the pure number but don't allow more.
$profile = Profile::getByUID($owner['uid']); $profile = Profile::getByUID($owner['uid']);
@ -194,16 +190,7 @@ class Transmitter
$data['type'] = 'OrderedCollectionPage'; $data['type'] = 'OrderedCollectionPage';
$list = []; $list = [];
$sql = "SELECT `contact`.`url` $contacts = DBA::select('contact', ['url'], $condition, ['limit' => [($page - 1) * 100, 100]]);
FROM `contact`
JOIN `apcontact` ON `apcontact`.`url` = `contact`.`url`
" . $condition . "
LIMIT ?, ?";
$parameters[] = ($page - 1) * 100;
$parameters[] = 100;
$contacts = DBA::p($sql, ...$parameters);
while ($contact = DBA::fetch($contacts)) { while ($contact = DBA::fetch($contacts)) {
$list[] = $contact['url']; $list[] = $contact['url'];
} }

View File

@ -36,7 +36,6 @@ class ExpireConversations
return; return;
} }
DBA::e("DELETE FROM `conversation` WHERE `received` < UTC_TIMESTAMP() - INTERVAL ? DAY", $days); DBA::delete('conversation', ["`received` < UTC_TIMESTAMP() - INTERVAL ? DAY", $days]);
} }
} }

View File

@ -68,7 +68,7 @@ trait DatabaseTestTrait
} }
if (!is_array($rows)) { if (!is_array($rows)) {
$dba->p('TRUNCATE TABLE `' . $tableName . '``'); $dba->e('TRUNCATE TABLE `' . $tableName . '``');
continue; continue;
} }