Replace "q" calls

This commit is contained in:
Philipp Holzer 2019-03-01 12:35:59 +01:00 committed by Hypolite Petovan
parent be6057b056
commit b2edb85588
5 changed files with 46 additions and 71 deletions

View File

@ -5957,7 +5957,7 @@ function api_friendica_notification($type)
} }
$nm = new NotificationsManager(); $nm = new NotificationsManager();
$notes = $nm->getAll([], "+seen -date", 50); $notes = $nm->getAll([], ['seen' => 'ASC', 'date' => 'DESC'], 50);
if ($type == "xml") { if ($type == "xml") {
$xmlnotes = []; $xmlnotes = [];

View File

@ -196,7 +196,7 @@ class ForumManager
*/ */
public static function countUnseenItems() public static function countUnseenItems()
{ {
$r = q( $r = 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 $r; return DBA::toArray($r);
} }
} }

View File

@ -55,47 +55,25 @@ class NotificationsManager extends BaseObject
* @brief Get all notifications for local_user() * @brief Get all notifications for local_user()
* *
* @param array $filter optional Array "column name"=>value: filter query by columns values * @param array $filter optional Array "column name"=>value: filter query by columns values
* @param string $order optional Space separated list of column to sort by. * @param array $order optional Array to order by
* Prepend name with "+" to sort ASC, "-" to sort DESC. Default to "-date"
* @param string $limit optional Query limits * @param string $limit optional Query limits
* *
* @return array of results or false on errors * @return array|bool of results or false on errors
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/ */
public function getAll($filter = [], $order = "-date", $limit = "") public function getAll($filter = [], $order = ['date' => 'DESC'], $limit = "")
{ {
$filter_str = []; $params = [];
$filter_sql = "";
foreach ($filter as $column => $value) { $params['order'] = $order;
$filter_str[] = sprintf("`%s` = '%s'", $column, DBA::escape($value));
} if (!empty($limit)) {
if (count($filter_str) > 0) { $params['limit'] = $limit;
$filter_sql = "AND " . implode(" AND ", $filter_str);
} }
$aOrder = explode(" ", $order); $dbFilter = array_merge($filter, ['uid' => local_user()]);
$asOrder = [];
foreach ($aOrder as $o) {
$dir = "asc";
if ($o[0] === "-") {
$dir = "desc";
$o = substr($o, 1);
}
if ($o[0] === "+") {
$dir = "asc";
$o = substr($o, 1);
}
$asOrder[] = "$o $dir";
}
$order_sql = implode(", ", $asOrder);
if ($limit != "") { $r = DBA::select('notify', [], $dbFilter, $order, $params);
$limit = " LIMIT " . $limit;
}
$r = q(
"SELECT * FROM `notify` WHERE `uid` = %d $filter_sql ORDER BY $order_sql $limit",
intval(local_user())
);
if (DBA::isResult($r)) { if (DBA::isResult($r)) {
return $this->_set_extra($r); return $this->_set_extra($r);
@ -113,11 +91,7 @@ class NotificationsManager extends BaseObject
*/ */
public function getByID($id) public function getByID($id)
{ {
$r = q( $r = DBA::selectFirst('notify', ['id' => $id, 'uid' => local_user()]);
"SELECT * FROM `notify` WHERE `id` = %d AND `uid` = %d LIMIT 1",
intval($id),
intval(local_user())
);
if (DBA::isResult($r)) { if (DBA::isResult($r)) {
return $this->_set_extra($r)[0]; return $this->_set_extra($r)[0];
} }
@ -134,14 +108,7 @@ class NotificationsManager extends BaseObject
*/ */
public function setSeen($note, $seen = true) public function setSeen($note, $seen = true)
{ {
return q( return DBA::update('notify', ['seen' => $seen], ['link' => $note['link'], 'parent' => $note['parent'], 'otype' => $note['otype'], 'uid' => local_user()]);
"UPDATE `notify` SET `seen` = %d WHERE (`link` = '%s' OR (`parent` != 0 AND `parent` = %d AND `otype` = '%s')) AND `uid` = %d",
intval($seen),
DBA::escape($note['link']),
intval($note['parent']),
DBA::escape($note['otype']),
intval(local_user())
);
} }
/** /**
@ -153,11 +120,7 @@ class NotificationsManager extends BaseObject
*/ */
public function setAllSeen($seen = true) public function setAllSeen($seen = true)
{ {
return q( return DBA::update('notify', ['seen' => $seen], ['uid' => local_user()]);
"UPDATE `notify` SET `seen` = %d WHERE `uid` = %d",
intval($seen),
intval(local_user())
);
} }
/** /**
@ -461,19 +424,21 @@ class NotificationsManager extends BaseObject
$sql_seen = ""; $sql_seen = "";
if ($seen === 0) { if ($seen === 0) {
$sql_seen = " AND NOT `seen` "; $filter = ['`uid` = ? AND NOT `seen`', local_user()];
} else {
$filter = ['uid' => local_user()];
} }
$r = q( $params = [];
"SELECT `id`, `url`, `photo`, `msg`, `date`, `seen`, `verb` FROM `notify` $params['limit'] = [$start, $limit];
WHERE `uid` = %d $sql_seen ORDER BY `date` DESC LIMIT %d, %d ",
intval(local_user()), $r = DBA::select('notify',
intval($start), ['id', 'url', 'photo', 'msg', 'date', 'seen', 'verb'],
intval($limit) $filter,
); $params);
if (DBA::isResult($r)) { if (DBA::isResult($r)) {
$notifs = $this->formatNotifs($r, $ident); $notifs = $this->formatNotifs(DBA::toArray($r), $ident);
} }
$arr = [ $arr = [

View File

@ -6,6 +6,7 @@ namespace Friendica\Core;
use Friendica\App; use Friendica\App;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\Database\DBStructure;
use Friendica\Model\Photo; use Friendica\Model\Photo;
use Friendica\Object\Image; use Friendica\Object\Image;
use Friendica\Util\Strings; use Friendica\Util\Strings;
@ -35,9 +36,8 @@ class UserImport
*/ */
private static function checkCols($table, &$arr) private static function checkCols($table, &$arr)
{ {
$query = sprintf("SHOW COLUMNS IN `%s`", DBA::escape($table)); $r = DBStructure::getColumns($table);
Logger::log("uimport: $query", Logger::DEBUG);
$r = q($query);
$tcols = []; $tcols = [];
// get a plain array of column names // get a plain array of column names
foreach ($r as $tcol) { foreach ($r as $tcol) {
@ -66,16 +66,12 @@ class UserImport
} }
self::checkCols($table, $arr); self::checkCols($table, $arr);
$cols = implode("`,`", array_map(['Friendica\Database\DBA', 'escape'], array_keys($arr)));
$vals = implode("','", array_map(['Friendica\Database\DBA', 'escape'], array_values($arr)));
$query = "INSERT INTO `$table` (`$cols`) VALUES ('$vals')";
Logger::log("uimport: $query", Logger::TRACE);
if (self::IMPORT_DEBUG) { if (self::IMPORT_DEBUG) {
return true; return true;
} }
return q($query); return DBA::insert($table, $arr);
} }
/** /**

View File

@ -844,4 +844,18 @@ class DBStructure
return $retval; return $retval;
} }
/**
* Returns the columns of a table
*
* @param string $table Table name
*
* @return array An array of the table columns
* @throws Exception
*/
public static function getColumns($table)
{
$r = DBA::p("SHOW COLUMNS FROM `" . $table . "`");
return DBA::toArray($r);
}
} }