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;
$condition = ["`net-publish` AND MATCH(`pub_keywords`) AGAINST (?)", $search];
$total = DBA::count('owner-view', $condition);
$count_stmt = DBA::p(
"SELECT COUNT(*) AS `total`
FROM `profile`
@ -58,18 +60,7 @@ function msearch_post(App $a)
DBA::close($count_stmt);
$search_stmt = DBA::p(
"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
);
$search_stmt = DBA::select('owner-view', ['pub_keywords', 'name', 'nickname', 'uid'], $condition, ['limit' => [$startrec, $perpage]]);
while ($search_result = DBA::fetch($search_stmt)) {
$results[] = [
'name' => $search_result['name'],

View File

@ -106,7 +106,7 @@ HELP;
$this->errored = 0;
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)) {
$this->examined++;
$this->processRow($row);

View File

@ -712,13 +712,10 @@ class Worker
}
$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_stat += (microtime(true) - $stamp);
if ($job = DBA::fetch($jobs)) {
$jobs_per_minute[$interval] = number_format($job['jobs'] / $interval, 0);
}
DBA::close($jobs);
$jobs_per_minute[$interval] = number_format($jobs / $interval, 0);
}
$processlist = ' - jpm: '.implode('/', $jobs_per_minute);
}
@ -739,15 +736,12 @@ class Worker
self::$db_duration_stat += (microtime(true) - $stamp);
while ($entry = DBA::fetch($jobs)) {
$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_stat += (microtime(true) - $stamp);
if ($process = DBA::fetch($processes)) {
$idle_workers -= $process["running"];
$waiting_processes += $entry["entries"];
$listitem[$entry["priority"]] = $entry["priority"].":".$process["running"]."/".$entry["entries"];
}
DBA::close($processes);
$idle_workers -= $running;
$waiting_processes += $entry["entries"];
$listitem[$entry["priority"]] = $entry["priority"] . ":" . $running . "/" . $entry["entries"];
}
DBA::close($jobs);
} else {

View File

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

View File

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

View File

@ -166,21 +166,17 @@ class Transmitter
'pending' => false,
'blocked' => false,
];
$condition = DBA::buildCondition($parameters);
$sql = "SELECT COUNT(*) as `count`
FROM `contact`
JOIN `apcontact` ON `apcontact`.`url` = `contact`.`url`
" . $condition;
$condition = DBA::mergeConditions($parameters, ["`url` IN (SELECT `url` FROM `apcontact`)"]);
$contacts = DBA::fetchFirst($sql, ...$parameters);
$total = DBA::count('contact', $condition);
$modulePath = '/' . $module . '/';
$data = ['@context' => ActivityPub::CONTEXT];
$data['id'] = DI::baseUrl() . $modulePath . $owner['nickname'];
$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.
$profile = Profile::getByUID($owner['uid']);
@ -194,16 +190,7 @@ class Transmitter
$data['type'] = 'OrderedCollectionPage';
$list = [];
$sql = "SELECT `contact`.`url`
FROM `contact`
JOIN `apcontact` ON `apcontact`.`url` = `contact`.`url`
" . $condition . "
LIMIT ?, ?";
$parameters[] = ($page - 1) * 100;
$parameters[] = 100;
$contacts = DBA::p($sql, ...$parameters);
$contacts = DBA::select('contact', ['url'], $condition, ['limit' => [($page - 1) * 100, 100]]);
while ($contact = DBA::fetch($contacts)) {
$list[] = $contact['url'];
}

View File

@ -36,7 +36,6 @@ class ExpireConversations
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)) {
$dba->p('TRUNCATE TABLE `' . $tableName . '``');
$dba->e('TRUNCATE TABLE `' . $tableName . '``');
continue;
}