Merge pull request #10848 from annando/more-q

More replaced "q" calls
This commit is contained in:
Hypolite Petovan 2021-10-08 07:06:31 -04:00 committed by GitHub
commit 2488780bc2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 101 additions and 135 deletions

View file

@ -3727,15 +3727,8 @@ function api_direct_messages_destroy($type)
// add parent-uri to sql command if specified by calling app // add parent-uri to sql command if specified by calling app
$sql_extra = ($parenturi != "" ? " AND `parent-uri` = '" . DBA::escape($parenturi) . "'" : ""); $sql_extra = ($parenturi != "" ? " AND `parent-uri` = '" . DBA::escape($parenturi) . "'" : "");
// get data of the specified message id
$r = q(
"SELECT `id` FROM `mail` WHERE `uid` = %d AND `id` = %d" . $sql_extra,
intval($uid),
intval($id)
);
// error message if specified id is not in database // error message if specified id is not in database
if (!DBA::isResult($r)) { if (!DBA::exists('mail', ["`uid` = ? AND `id` = ? " . $sql_extra, $uid, $id])) {
if ($verbose == "true") { if ($verbose == "true") {
$answer = ['result' => 'error', 'message' => 'message id not in database']; $answer = ['result' => 'error', 'message' => 'message id not in database'];
return api_format_data("direct_messages_delete", $type, ['$result' => $answer]); return api_format_data("direct_messages_delete", $type, ['$result' => $answer]);
@ -3745,11 +3738,7 @@ function api_direct_messages_destroy($type)
} }
// delete message // delete message
$result = q( $result = DBA::delete('mail', ["`uid` = ? AND `id` = ? " . $sql_extra, $uid, $id]);
"DELETE FROM `mail` WHERE `uid` = %d AND `id` = %d" . $sql_extra,
intval($uid),
intval($id)
);
if ($verbose == "true") { if ($verbose == "true") {
if ($result) { if ($result) {
@ -3915,13 +3904,13 @@ function api_direct_messages_box($type, $box, $verbose)
$sql_extra .= " AND `contact`.`nick` = '" . DBA::escape($screen_name). "'"; $sql_extra .= " AND `contact`.`nick` = '" . DBA::escape($screen_name). "'";
} }
$r = q( $r = DBA::toArray(DBA::p(
"SELECT `mail`.*, `contact`.`nurl` AS `contact-url` FROM `mail`,`contact` WHERE `mail`.`contact-id` = `contact`.`id` AND `mail`.`uid`=%d AND $sql_extra AND `mail`.`id` > %d ORDER BY `mail`.`id` DESC LIMIT %d,%d", "SELECT `mail`.*, `contact`.`nurl` AS `contact-url` FROM `mail`,`contact` WHERE `mail`.`contact-id` = `contact`.`id` AND `mail`.`uid` = ? AND $sql_extra AND `mail`.`id` > ? ORDER BY `mail`.`id` DESC LIMIT ?,?",
intval(api_user()), api_user(),
intval($since_id), $since_id,
intval($start), $start,
intval($count) $count
); ));
if ($verbose == "true" && !DBA::isResult($r)) { if ($verbose == "true" && !DBA::isResult($r)) {
$answer = ['result' => 'error', 'message' => 'no mails available']; $answer = ['result' => 'error', 'message' => 'no mails available'];
return api_format_data("direct_messages_all", $type, ['$result' => $answer]); return api_format_data("direct_messages_all", $type, ['$result' => $answer]);
@ -4123,12 +4112,12 @@ function api_fr_photos_list($type)
if (api_user() === false) { if (api_user() === false) {
throw new ForbiddenException(); throw new ForbiddenException();
} }
$r = q( $r = DBA::toArray(DBA::p(
"SELECT `resource-id`, MAX(scale) AS `scale`, `album`, `filename`, `type`, MAX(`created`) AS `created`, "SELECT `resource-id`, MAX(scale) AS `scale`, `album`, `filename`, `type`, MAX(`created`) AS `created`,
MAX(`edited`) AS `edited`, MAX(`desc`) AS `desc` FROM `photo` MAX(`edited`) AS `edited`, MAX(`desc`) AS `desc` FROM `photo`
WHERE `uid` = %d AND `album` != 'Contact Photos' GROUP BY `resource-id`, `album`, `filename`, `type`", WHERE `uid` = ? AND NOT `album` IN (?, ?) GROUP BY `resource-id`, `album`, `filename`, `type`",
intval(local_user()) local_user(), Photo::CONTACT_PHOTOS, DI::l10n()->t(Photo::CONTACT_PHOTOS)
); ));
$typetoext = [ $typetoext = [
'image/jpeg' => 'jpg', 'image/jpeg' => 'jpg',
'image/png' => 'png', 'image/png' => 'png',

View file

@ -45,12 +45,12 @@ function fbrowser_content(App $a)
$sql_extra = ""; $sql_extra = "";
$sql_extra2 = " ORDER BY created DESC LIMIT 0, 10"; $sql_extra2 = " ORDER BY created DESC LIMIT 0, 10";
if (DI::args()->getArgc()==2) { if (DI::args()->getArgc() == 2) {
$photos = q("SELECT distinct(`album`) AS `album` FROM `photo` WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s' ", $photos = DBA::toArray(DBA::p("SELECT distinct(`album`) AS `album` FROM `photo` WHERE `uid` = ? AND NOT `album` IN (?, ?)",
intval(local_user()), local_user(),
DBA::escape(Photo::CONTACT_PHOTOS), Photo::CONTACT_PHOTOS,
DBA::escape(DI::l10n()->t(Photo::CONTACT_PHOTOS)) DI::l10n()->t(Photo::CONTACT_PHOTOS)
); ));
$albums = array_column($photos, 'album'); $albums = array_column($photos, 'album');
} }
@ -62,14 +62,14 @@ function fbrowser_content(App $a)
$path[$album] = $album; $path[$album] = $album;
} }
$r = q("SELECT `resource-id`, ANY_VALUE(`id`) AS `id`, ANY_VALUE(`filename`) AS `filename`, ANY_VALUE(`type`) AS `type`, $r = DBA::toArray(DBA::p("SELECT `resource-id`, ANY_VALUE(`id`) AS `id`, ANY_VALUE(`filename`) AS `filename`, ANY_VALUE(`type`) AS `type`,
min(`scale`) AS `hiq`, max(`scale`) AS `loq`, ANY_VALUE(`desc`) AS `desc`, ANY_VALUE(`created`) AS `created` min(`scale`) AS `hiq`, max(`scale`) AS `loq`, ANY_VALUE(`desc`) AS `desc`, ANY_VALUE(`created`) AS `created`
FROM `photo` WHERE `uid` = %d $sql_extra AND `album` != '%s' AND `album` != '%s' FROM `photo` WHERE `uid` = ? $sql_extra AND NOT `album` IN (?, ?)
GROUP BY `resource-id` $sql_extra2", GROUP BY `resource-id` $sql_extra2",
intval(local_user()), local_user(),
DBA::escape(Photo::CONTACT_PHOTOS), Photo::CONTACT_PHOTOS,
DBA::escape(DI::l10n()->t(Photo::CONTACT_PHOTOS)) DI::l10n()->t(Photo::CONTACT_PHOTOS)
); ));
function _map_files1($rr) function _map_files1($rr)
{ {

View file

@ -212,14 +212,7 @@ function message_content(App $a)
$o .= $header; $o .= $header;
$total = 0; $total = DBA::count('mail', ['uid' => local_user()], ['distinct' => true, 'expression' => 'parent-uri']);
$r = q("SELECT count(*) AS `total`, ANY_VALUE(`created`) AS `created` FROM `mail`
WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `created` DESC",
intval(local_user())
);
if (DBA::isResult($r)) {
$total = $r[0]['total'];
}
$pager = new Pager(DI::l10n(), DI::args()->getQueryString()); $pager = new Pager(DI::l10n(), DI::args()->getQueryString());

View file

@ -230,16 +230,16 @@ function photos_post(App $a)
// get the list of photos we are about to delete // get the list of photos we are about to delete
if ($visitor) { if ($visitor) {
$r = q("SELECT distinct(`resource-id`) as `rid` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d AND `album` = '%s'", $r = DBA::toArray(DBA::p("SELECT distinct(`resource-id`) as `rid` FROM `photo` WHERE `contact-id` = ? AND `uid` = ? AND `album` = ?",
intval($visitor), $visitor,
intval($page_owner_uid), $page_owner_uid,
DBA::escape($album) $album
); ));
} else { } else {
$r = q("SELECT distinct(`resource-id`) as `rid` FROM `photo` WHERE `uid` = %d AND `album` = '%s'", $r = DBA::toArray(DBA::p("SELECT distinct(`resource-id`) as `rid` FROM `photo` WHERE `uid` = ? AND `album` = ?",
intval(local_user()), local_user(),
DBA::escape($album) $album
); ));
} }
if (DBA::isResult($r)) { if (DBA::isResult($r)) {
@ -977,11 +977,11 @@ function photos_content(App $a)
$album = hex2bin($datum); $album = hex2bin($datum);
$total = 0; $total = 0;
$r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` = '%s' $r = DBA::toArray(DBA::p("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = ? AND `album` = ?
AND `scale` <= 4 $sql_extra GROUP BY `resource-id`", AND `scale` <= 4 $sql_extra GROUP BY `resource-id`",
intval($owner_uid), $owner_uid,
DBA::escape($album) $album
); ));
if (DBA::isResult($r)) { if (DBA::isResult($r)) {
$total = count($r); $total = count($r);
} }
@ -996,16 +996,16 @@ function photos_content(App $a)
$order = 'DESC'; $order = 'DESC';
} }
$r = q("SELECT `resource-id`, ANY_VALUE(`id`) AS `id`, ANY_VALUE(`filename`) AS `filename`, $r = DBA::toArray(DBA::p("SELECT `resource-id`, ANY_VALUE(`id`) AS `id`, ANY_VALUE(`filename`) AS `filename`,
ANY_VALUE(`type`) AS `type`, max(`scale`) AS `scale`, ANY_VALUE(`desc`) as `desc`, ANY_VALUE(`type`) AS `type`, max(`scale`) AS `scale`, ANY_VALUE(`desc`) as `desc`,
ANY_VALUE(`created`) as `created` ANY_VALUE(`created`) as `created`
FROM `photo` WHERE `uid` = %d AND `album` = '%s' FROM `photo` WHERE `uid` = ? AND `album` = ?
AND `scale` <= 4 $sql_extra GROUP BY `resource-id` ORDER BY `created` $order LIMIT %d , %d", AND `scale` <= 4 $sql_extra GROUP BY `resource-id` ORDER BY `created` $order LIMIT ? , ?",
intval($owner_uid), intval($owner_uid),
DBA::escape($album), DBA::escape($album),
$pager->getStart(), $pager->getStart(),
$pager->getItemsPerPage() $pager->getItemsPerPage()
); ));
if ($cmd === 'drop') { if ($cmd === 'drop') {
$drop_url = DI::args()->getQueryString(); $drop_url = DI::args()->getQueryString();
@ -1101,11 +1101,7 @@ function photos_content(App $a)
// Display one photo // Display one photo
if ($datatype === 'image') { if ($datatype === 'image') {
// fetch image, item containing image, then comments // fetch image, item containing image, then comments
$ph = q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' $ph = Photo::selectToArray([], ["`uid` = ? AND `resource-id` = ? " . $sql_extra, $owner_uid, $datum], ['order' => ['scale' => true]]);
$sql_extra ORDER BY `scale` ASC ",
intval($owner_uid),
DBA::escape($datum)
);
if (!DBA::isResult($ph)) { if (!DBA::isResult($ph)) {
if (DBA::exists('photo', ['resource-id' => $datum, 'uid' => $owner_uid])) { if (DBA::exists('photo', ['resource-id' => $datum, 'uid' => $owner_uid])) {
@ -1149,11 +1145,7 @@ function photos_content(App $a)
$order = 'DESC'; $order = 'DESC';
} }
$prvnxt = q("SELECT `resource-id` FROM `photo` WHERE `album` = '%s' AND `uid` = %d AND `scale` = 0 $prvnxt = Photo::selectToArray(['resource-id'], ["`album` = ? AND `uid` = ? AND `scale` = ?" . $sql_extra, $ph[0]['album'], $owner_uid, 0]);
$sql_extra ORDER BY `created` $order ",
DBA::escape($ph[0]['album']),
intval($owner_uid)
);
if (DBA::isResult($prvnxt)) { if (DBA::isResult($prvnxt)) {
$prv = null; $prv = null;
@ -1253,14 +1245,7 @@ function photos_content(App $a)
// The difference is that we won't be displaying the conversation head item // The difference is that we won't be displaying the conversation head item
// as a "post" but displaying instead the photo it is linked to // as a "post" but displaying instead the photo it is linked to
/// @todo Rewrite this query. To do so, $sql_extra must be changed $link_item = Post::selectFirst([], ["`resource-id` = ?" . $sql_extra, $datum]);
$linked_items = q("SELECT `id` FROM `post-user-view` WHERE `resource-id` = '%s' $sql_extra LIMIT 1",
DBA::escape($datum)
);
if (DBA::isResult($linked_items)) {
// This is a workaround to not being forced to rewrite the while $sql_extra handling
$link_item = Post::selectFirst([], ['id' => $linked_items[0]['id']]);
}
if (!empty($link_item['parent']) && !empty($link_item['uid'])) { if (!empty($link_item['parent']) && !empty($link_item['uid'])) {
$condition = ["`parent` = ? AND `gravity` = ?", $link_item['parent'], GRAVITY_COMMENT]; $condition = ["`parent` = ? AND `gravity` = ?", $link_item['parent'], GRAVITY_COMMENT];
@ -1561,29 +1546,29 @@ function photos_content(App $a)
// Default - show recent photos with upload link (if applicable) // Default - show recent photos with upload link (if applicable)
//$o = ''; //$o = '';
$total = 0; $total = 0;
$r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s' $r = DBA::toArray(DBA::p("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = ? AND NOT `album` IN (?, ?)
$sql_extra GROUP BY `resource-id`", $sql_extra GROUP BY `resource-id`",
intval($user['uid']), $user['uid'],
DBA::escape(Photo::CONTACT_PHOTOS), Photo::CONTACT_PHOTOS,
DBA::escape(DI::l10n()->t(Photo::CONTACT_PHOTOS)) DI::l10n()->t(Photo::CONTACT_PHOTOS)
); ));
if (DBA::isResult($r)) { if (DBA::isResult($r)) {
$total = count($r); $total = count($r);
} }
$pager = new Pager(DI::l10n(), DI::args()->getQueryString(), 20); $pager = new Pager(DI::l10n(), DI::args()->getQueryString(), 20);
$r = q("SELECT `resource-id`, ANY_VALUE(`id`) AS `id`, ANY_VALUE(`filename`) AS `filename`, $r = DBA::toArray(DBA::p("SELECT `resource-id`, ANY_VALUE(`id`) AS `id`, ANY_VALUE(`filename`) AS `filename`,
ANY_VALUE(`type`) AS `type`, ANY_VALUE(`album`) AS `album`, max(`scale`) AS `scale`, ANY_VALUE(`type`) AS `type`, ANY_VALUE(`album`) AS `album`, max(`scale`) AS `scale`,
ANY_VALUE(`created`) AS `created` FROM `photo` ANY_VALUE(`created`) AS `created` FROM `photo`
WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s' WHERE `uid` = ? AND NOT `album` IN (?, ?)
$sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d", $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT ? , ?",
intval($user['uid']), $user['uid'],
DBA::escape(Photo::CONTACT_PHOTOS), Photo::CONTACT_PHOTOS,
DBA::escape(DI::l10n()->t(Photo::CONTACT_PHOTOS)), DI::l10n()->t(Photo::CONTACT_PHOTOS),
$pager->getStart(), $pager->getStart(),
$pager->getItemsPerPage() $pager->getItemsPerPage()
); ));
$photos = []; $photos = [];
if (DBA::isResult($r)) { if (DBA::isResult($r)) {

View file

@ -175,20 +175,20 @@ function ping_init(App $a)
} }
} }
$intros1 = q( $intros1 = DBA::toArray(DBA::p(
"SELECT `intro`.`id`, `intro`.`datetime`, "SELECT `intro`.`id`, `intro`.`datetime`,
`fcontact`.`name`, `fcontact`.`url`, `fcontact`.`photo` `fcontact`.`name`, `fcontact`.`url`, `fcontact`.`photo`
FROM `intro` INNER JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id` FROM `intro` INNER JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
WHERE `intro`.`uid` = %d AND NOT `intro`.`blocked` AND NOT `intro`.`ignore` AND `intro`.`fid` != 0", WHERE `intro`.`uid` = ? AND NOT `intro`.`blocked` AND NOT `intro`.`ignore` AND `intro`.`fid` != 0",
intval(local_user()) local_user()
); ));
$intros2 = q( $intros2 = DBA::toArray(DBA::p(
"SELECT `intro`.`id`, `intro`.`datetime`, "SELECT `intro`.`id`, `intro`.`datetime`,
`contact`.`name`, `contact`.`url`, `contact`.`photo` `contact`.`name`, `contact`.`url`, `contact`.`photo`
FROM `intro` INNER JOIN `contact` ON `intro`.`contact-id` = `contact`.`id` FROM `intro` INNER JOIN `contact` ON `intro`.`contact-id` = `contact`.`id`
WHERE `intro`.`uid` = %d AND NOT `intro`.`blocked` AND NOT `intro`.`ignore` AND `intro`.`contact-id` != 0 AND (`intro`.`fid` = 0 OR `intro`.`fid` IS NULL)", WHERE `intro`.`uid` = ? AND NOT `intro`.`blocked` AND NOT `intro`.`ignore` AND `intro`.`contact-id` != 0 AND (`intro`.`fid` = 0 OR `intro`.`fid` IS NULL)",
intval(local_user()) local_user()
); ));
$intro_count = count($intros1) + count($intros2); $intro_count = count($intros1) + count($intros2);
$intros = $intros1 + $intros2; $intros = $intros1 + $intros2;
@ -397,17 +397,17 @@ function ping_get_notifications($uid)
$quit = false; $quit = false;
do { do {
$r = q( $r = DBA::toArray(DBA::p(
"SELECT `notify`.*, `post`.`visible`, `post`.`deleted` "SELECT `notify`.*, `post`.`visible`, `post`.`deleted`
FROM `notify` LEFT JOIN `post` ON `post`.`uri-id` = `notify`.`uri-id` FROM `notify` LEFT JOIN `post` ON `post`.`uri-id` = `notify`.`uri-id`
WHERE `notify`.`uid` = %d AND `notify`.`msg` != '' WHERE `notify`.`uid` = ? AND `notify`.`msg` != ''
AND NOT (`notify`.`type` IN (%d, %d)) AND NOT (`notify`.`type` IN (?, ?))
AND $seensql `notify`.`seen` ORDER BY `notify`.`date` $order LIMIT %d, 50", AND $seensql `notify`.`seen` ORDER BY `notify`.`date` $order LIMIT ?, 50",
intval($uid), $uid,
intval(Notification\Type::INTRO), Notification\Type::INTRO,
intval(Notification\Type::MAIL), Notification\Type::MAIL,
intval($offset) $offset
); ));
if (!$r && !$seen) { if (!$r && !$seen) {
$seen = true; $seen = true;

View file

@ -651,23 +651,23 @@ class Photo
if (!DI::config()->get("system", "no_count", false)) { if (!DI::config()->get("system", "no_count", false)) {
/// @todo This query needs to be renewed. It is really slow /// @todo This query needs to be renewed. It is really slow
// At this time we just store the data in the cache // At this time we just store the data in the cache
$albums = q("SELECT COUNT(DISTINCT `resource-id`) AS `total`, `album`, ANY_VALUE(`created`) AS `created` $albums = DBA::toArray(DBA::p("SELECT COUNT(DISTINCT `resource-id`) AS `total`, `album`, ANY_VALUE(`created`) AS `created`
FROM `photo` FROM `photo`
WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s' $sql_extra WHERE `uid` = ? AND NOT `album` IN (?, ?) $sql_extra
GROUP BY `album` ORDER BY `created` DESC", GROUP BY `album` ORDER BY `created` DESC",
intval($uid), $uid,
DBA::escape(self::CONTACT_PHOTOS), self::CONTACT_PHOTOS,
DBA::escape(DI::l10n()->t(self::CONTACT_PHOTOS)) DI::l10n()->t(self::CONTACT_PHOTOS)
); ));
} else { } else {
// This query doesn't do the count and is much faster // This query doesn't do the count and is much faster
$albums = q("SELECT DISTINCT(`album`), '' AS `total` $albums = DBA::toArray(DBA::p("SELECT DISTINCT(`album`), '' AS `total`
FROM `photo` USE INDEX (`uid_album_scale_created`) FROM `photo` USE INDEX (`uid_album_scale_created`)
WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s' $sql_extra", WHERE `uid` = ? AND NOT `album` IN (?, ?) $sql_extra",
intval($uid), $uid,
DBA::escape(self::CONTACT_PHOTOS), self::CONTACT_PHOTOS,
DBA::escape(DI::l10n()->t(self::CONTACT_PHOTOS)) DI::l10n()->t(self::CONTACT_PHOTOS)
); ));
} }
DI::cache()->set($key, $albums, Duration::DAY); DI::cache()->set($key, $albums, Duration::DAY);
} }

View file

@ -164,25 +164,24 @@ class UserExport extends BaseSettings
$table = $match[1]; $table = $match[1];
$result = []; $result = [];
$r = q($query); $rows = DBA::p($query);
if (DBA::isResult($r)) { while ($row = DBA::fetch($rows)) {
foreach ($r as $rr) { foreach ($row as $k => $v) {
foreach ($rr as $k => $v) { if (empty($dbStructure[$table]['fields'][$k])) {
if (empty($dbStructure[$table]['fields'][$k])) { continue;
continue; }
}
switch ($dbStructure[$table]['fields'][$k]['type']) { switch ($dbStructure[$table]['fields'][$k]['type']) {
case 'datetime': case 'datetime':
$result[$k] = $v ?? DBA::NULL_DATETIME; $result[$k] = $v ?? DBA::NULL_DATETIME;
break; break;
default: default:
$result[$k] = $v; $result[$k] = $v;
break; break;
}
} }
} }
} }
DBA::close($rows);
return $result; return $result;
} }