From d5acd5f96a9f71afdfa9af91e5e3104cc18e5ec6 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Wed, 1 Jul 2020 22:54:14 -0400 Subject: [PATCH] Add expected support for sort strings in select() parameters - Fix unexpected behaviors with calls already using the sort strings --- src/Database/DBA.php | 20 +++++++++++++++++++- src/Database/Database.php | 28 +++++++++++++++++----------- 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/src/Database/DBA.php b/src/Database/DBA.php index 9825d06c68..f3edf52be5 100644 --- a/src/Database/DBA.php +++ b/src/Database/DBA.php @@ -648,6 +648,20 @@ class DBA /** * Returns the SQL parameter string built from the provided parameter array * + * Expected format for each key: + * + * group_by: + * - list of column names + * + * order: + * - numeric keyed column name => ASC + * - associative element with boolean value => DESC (true), ASC (false) + * - associative element with string value => 'ASC' or 'DESC' literally + * + * limit: + * - single numeric value => count + * - list with two numeric values => offset, count + * * @param array $params * @return string */ @@ -665,7 +679,11 @@ class DBA if ($order === 'RAND()') { $order_string .= "RAND(), "; } elseif (!is_int($fields)) { - $order_string .= self::quoteIdentifier($fields) . " " . ($order ? "DESC" : "ASC") . ", "; + if ($order !== 'DESC' && $order !== 'ASC') { + $order = $order ? 'DESC' : 'ASC'; + } + + $order_string .= self::quoteIdentifier($fields) . " " . $order . ", "; } else { $order_string .= self::quoteIdentifier($order) . ", "; } diff --git a/src/Database/Database.php b/src/Database/Database.php index 897845ce0c..eaf4900509 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -1467,24 +1467,30 @@ class Database /** * Select rows from a table * - * @param string|array $table Table name or array [schema => table] - * @param array $fields Array of selected fields, empty for all - * @param array $condition Array of fields for condition - * @param array $params Array of several parameters - * - * @return boolean|object * * Example: - * $table = "item"; - * $fields = array("id", "uri", "uid", "network"); + * $table = 'item'; + * or: + * $table = ['schema' => 'table']; + * @see DBA::buildTableString() * - * $condition = array("uid" => 1, "network" => 'dspr'); + * $fields = ['id', 'uri', 'uid', 'network']; + * + * $condition = ['uid' => 1, 'network' => 'dspr', 'blocked' => true]; * or: - * $condition = array("`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr'); + * $condition = ['`uid` = ? AND `network` IN (?, ?)', 1, 'dfrn', 'dspr']; + * @see DBA::buildCondition() * - * $params = array("order" => array("id", "received" => true), "limit" => 10); + * $params = ['order' => ['id', 'received' => true, 'created' => 'ASC'), 'limit' => 10]; + * @see DBA::buildParameter() * * $data = DBA::select($table, $fields, $condition, $params); + * + * @param string|array $table Table name or array [schema => table] + * @param array $fields Array of selected fields, empty for all + * @param array $condition Array of fields for condition + * @param array $params Array of several parameters + * @return boolean|object * @throws \Exception */ public function select($table, array $fields = [], array $condition = [], array $params = []) -- 2.39.5