]> git.mxchange.org Git - friendica.git/blobdiff - src/Database/Database.php
Removed temporary code
[friendica.git] / src / Database / Database.php
index 813d4e98538decbeb08e01c5d5cf9357229939cf..0b38c24ba336b9ab27308dcdbfddeb2f0cca567e 100644 (file)
@@ -1,9 +1,30 @@
 <?php
+/**
+ * @copyright Copyright (C) 2020, Friendica
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
 
 namespace Friendica\Database;
 
-use Friendica\Core\Config\Cache\ConfigCache;
+use Exception;
+use Friendica\Core\Config\Cache;
 use Friendica\Core\System;
+use Friendica\DI;
 use Friendica\Network\HTTPException\InternalServerErrorException;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Profiler;
@@ -16,8 +37,6 @@ use PDOStatement;
 use Psr\Log\LoggerInterface;
 
 /**
- * @class MySQL database class
- *
  * This class is for the low level database stuff that does driver specific things.
  */
 class Database
@@ -25,7 +44,7 @@ class Database
        protected $connected = false;
 
        /**
-        * @var ConfigCache
+        * @var Cache
         */
        protected $configCache;
        /**
@@ -40,14 +59,16 @@ class Database
        /** @var PDO|mysqli */
        protected $connection;
        protected $driver;
+       private $emulate_prepares = false;
        private $error          = false;
        private $errorno        = 0;
        private $affected_rows  = 0;
        protected $in_transaction = false;
        protected $in_retrial     = false;
+       protected $testmode       = false;
        private $relation       = [];
 
-       public function __construct(ConfigCache $configCache, Profiler $profiler, LoggerInterface $logger, array $server = [])
+       public function __construct(Cache $configCache, Profiler $profiler, LoggerInterface $logger, array $server = [])
        {
                // We are storing these values for being able to perform a reconnect
                $this->configCache   = $configCache;
@@ -67,7 +88,7 @@ class Database
        {
                // Use environment variables for mysql if they are set beforehand
                if (!empty($server['MYSQL_HOST'])
-                   && !empty($server['MYSQL_USERNAME'] || !empty($server['MYSQL_USER']))
+                   && (!empty($server['MYSQL_USERNAME'] || !empty($server['MYSQL_USER'])))
                    && $server['MYSQL_PASSWORD'] !== false
                    && !empty($server['MYSQL_DATABASE']))
                {
@@ -113,7 +134,10 @@ class Database
                        return false;
                }
 
-               if (class_exists('\PDO') && in_array('mysql', PDO::getAvailableDrivers())) {
+               $this->emulate_prepares = (bool)$this->configCache->get('database', 'emulate_prepares');
+               $this->pdo_emulate_prepares = (bool)$this->configCache->get('database', 'pdo_emulate_prepares');
+
+               if (!$this->configCache->get('database', 'disable_pdo') && class_exists('\PDO') && in_array('mysql', PDO::getAvailableDrivers())) {
                        $this->driver = 'pdo';
                        $connect      = "mysql:host=" . $server . ";dbname=" . $db;
 
@@ -127,7 +151,7 @@ class Database
 
                        try {
                                $this->connection = @new PDO($connect, $user, $pass);
-                               $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
+                               $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->pdo_emulate_prepares);
                                $this->connected = true;
                        } catch (PDOException $e) {
                                $this->connected = false;
@@ -161,6 +185,10 @@ class Database
                return $this->connected;
        }
 
+       public function setTestmode(bool $test)
+       {
+               $this->testmode = $test;
+       }
        /**
         * Sets the logger for DBA
         *
@@ -226,7 +254,7 @@ class Database
        }
 
        /**
-        * @brief Returns the MySQL server version string
+        * Returns the MySQL server version string
         *
         * This function discriminate between the deprecated mysql API and the current
         * object-oriented mysqli API. Example of returned string: 5.5.46-0+deb8u1
@@ -249,7 +277,7 @@ class Database
        }
 
        /**
-        * @brief Returns the selected database name
+        * Returns the selected database name
         *
         * @return string
         * @throws \Exception
@@ -262,7 +290,7 @@ class Database
        }
 
        /**
-        * @brief Analyze a database query and log this if some conditions are met.
+        * Analyze a database query and log this if some conditions are met.
         *
         * @param string $query The database query that will be analyzed
         *
@@ -291,7 +319,7 @@ class Database
                }
 
                $watchlist = explode(',', $this->configCache->get('system', 'db_log_index_watch'));
-               $blacklist = explode(',', $this->configCache->get('system', 'db_log_index_blacklist'));
+               $denylist = explode(',', $this->configCache->get('system', 'db_log_index_denylist'));
 
                while ($row = $this->fetch($r)) {
                        if ((intval($this->configCache->get('system', 'db_loglimit_index')) > 0)) {
@@ -305,7 +333,7 @@ class Database
                                $log = true;
                        }
 
-                       if (in_array($row['key'], $blacklist) || ($row['key'] == "")) {
+                       if (in_array($row['key'], $denylist) || ($row['key'] == "")) {
                                $log = false;
                        }
 
@@ -315,13 +343,13 @@ class Database
                                                                                                      $row['key'] . "\t" . $row['rows'] . "\t" . $row['Extra'] . "\t" .
                                                                                                      basename($backtrace[1]["file"]) . "\t" .
                                                                                                      $backtrace[1]["line"] . "\t" . $backtrace[2]["function"] . "\t" .
-                                                                                                     substr($query, 0, 2000) . "\n", FILE_APPEND);
+                                                                                                     substr($query, 0, 4000) . "\n", FILE_APPEND);
                        }
                }
        }
 
        /**
-        * Removes every not whitelisted character from the identifier string
+        * Removes every not allowlisted character from the identifier string
         *
         * @param string $identifier
         *
@@ -378,8 +406,8 @@ class Database
        }
 
        /**
-        * @brief Replaces ANY_VALUE() function by MIN() function,
-        *  if the database server does not support ANY_VALUE().
+        * Replaces ANY_VALUE() function by MIN() function,
+        * if the database server does not support ANY_VALUE().
         *
         * Considerations for Standard SQL, or MySQL with ONLY_FULL_GROUP_BY (default since 5.7.5).
         * ANY_VALUE() is available from MySQL 5.7.5 https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html
@@ -400,7 +428,7 @@ class Database
        }
 
        /**
-        * @brief Replaces the ? placeholders with the parameters in the $args array
+        * Replaces the ? placeholders with the parameters in the $args array
         *
         * @param string $sql  SQL query
         * @param array  $args The parameters that are to replace the ? placeholders
@@ -411,8 +439,10 @@ class Database
        {
                $offset = 0;
                foreach ($args AS $param => $value) {
-                       if (is_int($args[$param]) || is_float($args[$param])) {
+                       if (is_int($args[$param]) || is_float($args[$param]) || is_bool($args[$param])) {
                                $replace = intval($args[$param]);
+                       } elseif (is_null($args[$param])) {
+                               $replace = 'NULL';
                        } else {
                                $replace = "'" . $this->escape($args[$param]) . "'";
                        }
@@ -427,7 +457,8 @@ class Database
        }
 
        /**
-        * @brief Executes a prepared statement that returns data
+        * Executes a prepared statement that returns data
+        *
         * @usage Example: $r = p("SELECT * FROM `item` WHERE `guid` = ?", $guid);
         *
         * Please only use it with complicated queries.
@@ -474,6 +505,7 @@ class Database
                        $sql = "/*" . System::callstack() . " */ " . $sql;
                }
 
+               $is_error            = false;
                $this->error         = '';
                $this->errorno       = 0;
                $this->affected_rows = 0;
@@ -497,12 +529,13 @@ class Database
                switch ($this->driver) {
                        case 'pdo':
                                // If there are no arguments we use "query"
-                               if (count($args) == 0) {
-                                       if (!$retval = $this->connection->query($sql)) {
+                               if ($this->emulate_prepares || count($args) == 0) {
+                                       if (!$retval = $this->connection->query($this->replaceParameters($sql, $args))) {
                                                $errorInfo     = $this->connection->errorInfo();
                                                $this->error   = $errorInfo[2];
                                                $this->errorno = $errorInfo[1];
                                                $retval        = false;
+                                               $is_error      = true;
                                                break;
                                        }
                                        $this->affected_rows = $retval->rowCount();
@@ -515,6 +548,7 @@ class Database
                                        $this->error   = $errorInfo[2];
                                        $this->errorno = $errorInfo[1];
                                        $retval        = false;
+                                       $is_error      = true;
                                        break;
                                }
 
@@ -532,6 +566,7 @@ class Database
                                        $this->error   = $errorInfo[2];
                                        $this->errorno = $errorInfo[1];
                                        $retval        = false;
+                                       $is_error      = true;
                                } else {
                                        $retval              = $stmt;
                                        $this->affected_rows = $retval->rowCount();
@@ -544,12 +579,13 @@ class Database
                                $can_be_prepared = in_array($command, ['select', 'update', 'insert', 'delete']);
 
                                // The fallback routine is called as well when there are no arguments
-                               if (!$can_be_prepared || (count($args) == 0)) {
+                               if ($this->emulate_prepares || !$can_be_prepared || (count($args) == 0)) {
                                        $retval = $this->connection->query($this->replaceParameters($sql, $args));
                                        if ($this->connection->errno) {
                                                $this->error   = $this->connection->error;
                                                $this->errorno = $this->connection->errno;
                                                $retval        = false;
+                                               $is_error      = true;
                                        } else {
                                                if (isset($retval->num_rows)) {
                                                        $this->affected_rows = $retval->num_rows;
@@ -566,6 +602,7 @@ class Database
                                        $this->error   = $stmt->error;
                                        $this->errorno = $stmt->errno;
                                        $retval        = false;
+                                       $is_error      = true;
                                        break;
                                }
 
@@ -593,6 +630,7 @@ class Database
                                        $this->error   = $this->connection->error;
                                        $this->errorno = $this->connection->errno;
                                        $retval        = false;
+                                       $is_error      = true;
                                } else {
                                        $stmt->store_result();
                                        $retval              = $stmt;
@@ -601,15 +639,29 @@ class Database
                                break;
                }
 
+               // See issue https://github.com/friendica/friendica/issues/8572
+               // Ensure that we always get an error message on an error.
+               if ($is_error && empty($this->errorno)) {
+                       $this->errorno = -1;
+               }
+
+               if ($is_error && empty($this->error)) {
+                       $this->error = 'Unknown database error';
+               }
+
                // We are having an own error logging in the function "e"
                if (($this->errorno != 0) && !$called_from_e) {
                        // We have to preserve the error code, somewhere in the logging it get lost
                        $error   = $this->error;
                        $errorno = $this->errorno;
 
+                       if ($this->testmode) {
+                               throw new Exception(DI::l10n()->t('Database error %d "%s" at "%s"', $errorno, $error, $this->replaceParameters($sql, $args)));
+                       }
+
                        $this->logger->error('DB Error', [
-                               'code'      => $this->errorno,
-                               'error'     => $this->error,
+                               'code'      => $errorno,
+                               'error'     => $error,
                                'callstack' => System::callstack(8),
                                'params'    => $this->replaceParameters($sql, $args),
                        ]);
@@ -620,21 +672,21 @@ class Database
                                        // It doesn't make sense to continue when the database connection was lost
                                        if ($this->in_retrial) {
                                                $this->logger->notice('Giving up retrial because of database error', [
-                                                       'code'  => $this->errorno,
-                                                       'error' => $this->error,
+                                                       'code'  => $errorno,
+                                                       'error' => $error,
                                                ]);
                                        } else {
                                                $this->logger->notice('Couldn\'t reconnect after database error', [
-                                                       'code'  => $this->errorno,
-                                                       'error' => $this->error,
+                                                       'code'  => $errorno,
+                                                       'error' => $error,
                                                ]);
                                        }
                                        exit(1);
                                } else {
                                        // We try it again
                                        $this->logger->notice('Reconnected after database error', [
-                                               'code'  => $this->errorno,
-                                               'error' => $this->error,
+                                               'code'  => $errorno,
+                                               'error' => $error,
                                        ]);
                                        $this->in_retrial = true;
                                        $ret              = $this->p($sql, $args);
@@ -647,7 +699,7 @@ class Database
                        $this->errorno = $errorno;
                }
 
-               $this->profiler->saveTimestamp($stamp1, 'database', System::callstack());
+               $this->profiler->saveTimestamp($stamp1, 'database');
 
                if ($this->configCache->get('system', 'db_log')) {
                        $stamp2   = microtime(true);
@@ -660,14 +712,14 @@ class Database
                                @file_put_contents($this->configCache->get('system', 'db_log'), DateTimeFormat::utcNow() . "\t" . $duration . "\t" .
                                                                                                basename($backtrace[1]["file"]) . "\t" .
                                                                                                $backtrace[1]["line"] . "\t" . $backtrace[2]["function"] . "\t" .
-                                                                                               substr($this->replaceParameters($sql, $args), 0, 2000) . "\n", FILE_APPEND);
+                                                                                               substr($this->replaceParameters($sql, $args), 0, 4000) . "\n", FILE_APPEND);
                        }
                }
                return $retval;
        }
 
        /**
-        * @brief Executes a prepared statement like UPDATE or INSERT that doesn't return data
+        * Executes a prepared statement like UPDATE or INSERT that doesn't return data
         *
         * Please use DBA::delete, DBA::insert, DBA::update, ... instead
         *
@@ -706,9 +758,13 @@ class Database
                        $error   = $this->error;
                        $errorno = $this->errorno;
 
+                       if ($this->testmode) {
+                               throw new Exception(DI::l10n()->t('Database error %d "%s" at "%s"', $errorno, $error, $this->replaceParameters($sql, $params)));
+                       }
+
                        $this->logger->error('DB Error', [
-                               'code'      => $this->errorno,
-                               'error'     => $this->error,
+                               'code'      => $errorno,
+                               'error'     => $error,
                                'callstack' => System::callstack(8),
                                'params'    => $this->replaceParameters($sql, $params),
                        ]);
@@ -717,8 +773,8 @@ class Database
                        // A reconnect like in $this->p could be dangerous with modifications
                        if ($errorno == 2006) {
                                $this->logger->notice('Giving up because of database error', [
-                                       'code'  => $this->errorno,
-                                       'error' => $this->error,
+                                       'code'  => $errorno,
+                                       'error' => $error,
                                ]);
                                exit(1);
                        }
@@ -727,13 +783,13 @@ class Database
                        $this->errorno = $errorno;
                }
 
-               $this->profiler->saveTimestamp($stamp, "database_write", System::callstack());
+               $this->profiler->saveTimestamp($stamp, "database_write");
 
                return $retval;
        }
 
        /**
-        * @brief Check if data exists
+        * Check if data exists
         *
         * @param string|array $table     Table name or array [schema => table]
         * @param array        $condition array of fields for condition
@@ -777,7 +833,7 @@ class Database
         *
         * Please use DBA::selectFirst or DBA::exists whenever this is possible.
         *
-        * @brief Fetches the first row
+        * Fetches the first row
         *
         * @param string $sql SQL statement
         *
@@ -802,7 +858,7 @@ class Database
        }
 
        /**
-        * @brief Returns the number of affected rows of the last statement
+        * Returns the number of affected rows of the last statement
         *
         * @return int Number of rows
         */
@@ -812,7 +868,7 @@ class Database
        }
 
        /**
-        * @brief Returns the number of columns of a statement
+        * Returns the number of columns of a statement
         *
         * @param object Statement object
         *
@@ -833,7 +889,7 @@ class Database
        }
 
        /**
-        * @brief Returns the number of rows of a statement
+        * Returns the number of rows of a statement
         *
         * @param PDOStatement|mysqli_result|mysqli_stmt Statement object
         *
@@ -854,7 +910,7 @@ class Database
        }
 
        /**
-        * @brief Fetch a single row
+        * Fetch a single row
         *
         * @param mixed $stmt statement object
         *
@@ -908,13 +964,13 @@ class Database
                                }
                }
 
-               $this->profiler->saveTimestamp($stamp1, 'database', System::callstack());
+               $this->profiler->saveTimestamp($stamp1, 'database');
 
                return $columns;
        }
 
        /**
-        * @brief Insert a row into a table
+        * Insert a row into a table
         *
         * @param string|array $table               Table name or array [schema => table]
         * @param array        $param               parameter array
@@ -923,7 +979,7 @@ class Database
         * @return boolean was the insert successful?
         * @throws \Exception
         */
-       public function insert($table, $param, $on_duplicate_update = false)
+       public function insert($table, array $param, bool $on_duplicate_update = false)
        {
                if (empty($table) || empty($param)) {
                        $this->logger->info('Table and fields have to be set');
@@ -951,7 +1007,7 @@ class Database
        }
 
        /**
-        * @brief Fetch the id of the last insert command
+        * Fetch the id of the last insert command
         *
         * @return integer Last inserted id
         */
@@ -969,7 +1025,7 @@ class Database
        }
 
        /**
-        * @brief Locks a table for exclusive write access
+        * Locks a table for exclusive write access
         *
         * This function can be extended in the future to accept a table array as well.
         *
@@ -991,7 +1047,7 @@ class Database
                $success = $this->e("LOCK TABLES " . DBA::buildTableString($table) . " WRITE");
 
                if ($this->driver == 'pdo') {
-                       $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
+                       $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->pdo_emulate_prepares);
                }
 
                if (!$success) {
@@ -1007,7 +1063,7 @@ class Database
        }
 
        /**
-        * @brief Unlocks all locked tables
+        * Unlocks all locked tables
         *
         * @return boolean was the unlock successful?
         * @throws \Exception
@@ -1024,7 +1080,7 @@ class Database
                $success = $this->e("UNLOCK TABLES");
 
                if ($this->driver == 'pdo') {
-                       $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
+                       $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->pdo_emulate_prepares);
                        $this->e("SET autocommit=1");
                } else {
                        $this->connection->autocommit(true);
@@ -1035,7 +1091,7 @@ class Database
        }
 
        /**
-        * @brief Starts a transaction
+        * Starts a transaction
         *
         * @return boolean Was the command executed successfully?
         */
@@ -1081,7 +1137,7 @@ class Database
        }
 
        /**
-        * @brief Does a commit
+        * Does a commit
         *
         * @return boolean Was the command executed successfully?
         */
@@ -1095,7 +1151,7 @@ class Database
        }
 
        /**
-        * @brief Does a rollback
+        * Does a rollback
         *
         * @return boolean Was the command executed successfully?
         */
@@ -1121,7 +1177,7 @@ class Database
        }
 
        /**
-        * @brief Build the array with the table relations
+        * Build the array with the table relations
         *
         * The array is build from the database definitions in DBStructure.php
         *
@@ -1143,7 +1199,7 @@ class Database
        }
 
        /**
-        * @brief Delete a row from a table
+        * Delete a row from a table
         *
         * Note: this methods does NOT accept schema => table arrays because of the complex relation stuff.
         *
@@ -1244,7 +1300,7 @@ class Database
 
                        if ((count($command['conditions']) > 1) || is_int($first_key)) {
                                $sql = "DELETE FROM " . DBA::quoteIdentifier($command['table']) . " " . $condition_string;
-                               $this->logger->debug($this->replaceParameters($sql, $conditions));
+                               $this->logger->info($this->replaceParameters($sql, $conditions), ['callstack' => System::callstack(6), 'internal_callstack' => $callstack]);
 
                                if (!$this->e($sql, $conditions)) {
                                        if ($do_transaction) {
@@ -1274,7 +1330,7 @@ class Database
                                        $sql = "DELETE FROM " . DBA::quoteIdentifier($table) . " WHERE " . DBA::quoteIdentifier($field) . " IN (" .
                                               substr(str_repeat("?, ", count($field_values)), 0, -2) . ");";
 
-                                       $this->logger->debug($this->replaceParameters($sql, $field_values));
+                                       $this->logger->info($this->replaceParameters($sql, $field_values), ['callstack' => System::callstack(6), 'internal_callstack' => $callstack]);
 
                                        if (!$this->e($sql, $field_values)) {
                                                if ($do_transaction) {
@@ -1292,7 +1348,7 @@ class Database
        }
 
        /**
-        * @brief Updates rows
+        * Updates rows
         *
         * Updates rows in the database. When $old_fields is set to an array,
         * the system will only do an update if the fields in that array changed.
@@ -1327,10 +1383,6 @@ class Database
                        return false;
                }
 
-               $table_string = DBA::buildTableString($table);
-
-               $condition_string = DBA::buildCondition($condition);
-
                if (is_bool($old_fields)) {
                        $do_insert = $old_fields;
 
@@ -1345,29 +1397,26 @@ class Database
                        }
                }
 
-               $do_update = (count($old_fields) == 0);
-
                foreach ($old_fields AS $fieldname => $content) {
-                       if (isset($fields[$fieldname])) {
-                               if (($fields[$fieldname] == $content) && !is_null($content)) {
-                                       unset($fields[$fieldname]);
-                               } else {
-                                       $do_update = true;
-                               }
+                       if (isset($fields[$fieldname]) && !is_null($content) && ($fields[$fieldname] == $content)) {
+                               unset($fields[$fieldname]);
                        }
                }
 
-               if (!$do_update || (count($fields) == 0)) {
+               if (count($fields) == 0) {
                        return true;
                }
 
+               $table_string = DBA::buildTableString($table);
+
+               $condition_string = DBA::buildCondition($condition);
+
                $sql = "UPDATE " . $table_string . " SET "
                        . implode(" = ?, ", array_map([DBA::class, 'quoteIdentifier'], array_keys($fields))) . " = ?"
                        . $condition_string;
 
-               $params1 = array_values($fields);
-               $params2 = array_values($condition);
-               $params  = array_merge_recursive($params1, $params2);
+               // Combines the updated fields parameter values with the condition parameter values
+               $params  = array_merge(array_values($fields), $condition);
 
                return $this->e($sql, $params);
        }
@@ -1375,12 +1424,10 @@ class Database
        /**
         * Retrieve a single record from a table and returns it in an associative array
         *
-        * @brief Retrieve a single record from a table
-        *
-        * @param string $table
-        * @param array  $fields
-        * @param array  $condition
-        * @param array  $params
+        * @param string|array $table
+        * @param array        $fields
+        * @param array        $condition
+        * @param array        $params
         *
         * @return bool|array
         * @throws \Exception
@@ -1401,7 +1448,7 @@ class Database
        }
 
        /**
-        * @brief Select rows from a table and fills an array with the data
+        * Select rows from a table and fills an array with the data
         *
         * @param string|array $table     Table name or array [schema => table]
         * @param array        $fields    Array of selected fields, empty for all
@@ -1412,32 +1459,38 @@ class Database
         * @throws \Exception
         * @see   self::select
         */
-       public function selectToArray(string $table, array $fields = [], array $condition = [], array $params = [])
+       public function selectToArray($table, array $fields = [], array $condition = [], array $params = [])
        {
                return $this->toArray($this->select($table, $fields, $condition, $params));
        }
 
        /**
-        * @brief Select rows from a table
+        * 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 = [])
@@ -1466,10 +1519,11 @@ class Database
        }
 
        /**
-        * @brief Counts the rows from a table satisfying the provided condition
+        * Counts the rows from a table satisfying the provided condition
         *
         * @param string|array $table     Table name or array [schema => table]
         * @param array        $condition Array of fields for condition
+        * @param array        $params    Array of several parameters
         *
         * @return int
         *
@@ -1483,7 +1537,7 @@ class Database
         * $count = DBA::count($table, $condition);
         * @throws \Exception
         */
-       public function count($table, array $condition = [])
+       public function count($table, array $condition = [], array $params = [])
        {
                if (empty($table)) {
                        return false;
@@ -1493,7 +1547,15 @@ class Database
 
                $condition_string = DBA::buildCondition($condition);
 
-               $sql = "SELECT COUNT(*) AS `count` FROM " . $table_string . $condition_string;
+               if (empty($params['expression'])) {
+                       $expression = '*';
+               } elseif (!empty($params['distinct'])) {
+                       $expression = "DISTINCT " . DBA::quoteIdentifier($params['expression']);
+               } else {
+                       $expression = DBA::quoteIdentifier($params['expression']);
+               }
+
+               $sql = "SELECT COUNT(" . $expression . ") AS `count` FROM " . $table_string . $condition_string;
 
                $row = $this->fetchFirst($sql, $condition);
 
@@ -1501,7 +1563,7 @@ class Database
        }
 
        /**
-        * @brief Fills an array with data from a query
+        * Fills an array with data from a query
         *
         * @param object $stmt statement object
         * @param bool   $do_close
@@ -1527,7 +1589,7 @@ class Database
        }
 
        /**
-        * @brief Returns the error number of the last query
+        * Returns the error number of the last query
         *
         * @return string Error number (0 if no error)
         */
@@ -1537,7 +1599,7 @@ class Database
        }
 
        /**
-        * @brief Returns the error message of the last query
+        * Returns the error message of the last query
         *
         * @return string Error message ('' if no error)
         */
@@ -1547,7 +1609,7 @@ class Database
        }
 
        /**
-        * @brief Closes the current statement
+        * Closes the current statement
         *
         * @param object $stmt statement object
         *
@@ -1582,13 +1644,13 @@ class Database
                                break;
                }
 
-               $this->profiler->saveTimestamp($stamp1, 'database', System::callstack());
+               $this->profiler->saveTimestamp($stamp1, 'database');
 
                return $ret;
        }
 
        /**
-        * @brief Return a list of database processes
+        * Return a list of database processes
         *
         * @return array
         *      'list' => List of processes, separated in their different states
@@ -1622,6 +1684,18 @@ class Database
                return (["list" => $statelist, "amount" => $processes]);
        }
 
+       /**
+        * Fetch a database variable
+        *
+        * @param string $name
+        * @return string content
+        */
+       public function getVariable(string $name)
+       {
+               $result = $this->fetchFirst("SHOW GLOBAL VARIABLES WHERE `Variable_name` = ?", $name);
+               return $result['Value'] ?? null;
+       }
+
        /**
         * Checks if $array is a filled array with at least one entry.
         *
@@ -1644,7 +1718,7 @@ class Database
        }
 
        /**
-        * @brief Callback function for "esc_array"
+        * Callback function for "esc_array"
         *
         * @param mixed   $value         Array value
         * @param string  $key           Array key
@@ -1673,7 +1747,7 @@ class Database
        }
 
        /**
-        * @brief Escapes a whole array
+        * Escapes a whole array
         *
         * @param mixed   $arr           Array with values to be escaped
         * @param boolean $add_quotation add quotation marks for string values