3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Database;
24 use Friendica\Core\Config\Cache;
25 use Friendica\Core\System;
26 use Friendica\Network\HTTPException\InternalServerErrorException;
27 use Friendica\Util\DateTimeFormat;
28 use Friendica\Util\Profiler;
35 use Psr\Log\LoggerInterface;
38 * This class is for the low level database stuff that does driver specific things.
42 protected $connected = false;
47 protected $configCache;
53 * @var LoggerInterface
56 protected $server_info = '';
57 /** @var PDO|mysqli */
58 protected $connection;
60 protected $emulate_prepares = false;
61 protected $pdo_emulate_prepares = false;
62 private $error = false;
64 private $affected_rows = 0;
65 protected $in_transaction = false;
66 protected $in_retrial = false;
67 protected $testmode = false;
68 private $relation = [];
70 public function __construct(Cache $configCache, Profiler $profiler, LoggerInterface $logger)
72 // We are storing these values for being able to perform a reconnect
73 $this->configCache = $configCache;
74 $this->profiler = $profiler;
75 $this->logger = $logger;
79 if ($this->isConnected()) {
80 // Loads DB_UPDATE_VERSION constant
81 DBStructure::definition($configCache->get('system', 'basepath'), false);
85 public function connect()
87 if (!is_null($this->connection) && $this->connected()) {
88 return $this->connected;
91 // Reset connected state
92 $this->connected = false;
95 $serveraddr = trim($this->configCache->get('database', 'hostname'));
96 $serverdata = explode(':', $serveraddr);
97 $server = $serverdata[0];
98 if (count($serverdata) > 1) {
99 $port = trim($serverdata[1]);
102 if (!empty(trim($this->configCache->get('database', 'port')))) {
103 $port = trim($this->configCache->get('database', 'port'));
106 $server = trim($server);
107 $user = trim($this->configCache->get('database', 'username'));
108 $pass = trim($this->configCache->get('database', 'password'));
109 $db = trim($this->configCache->get('database', 'database'));
110 $charset = trim($this->configCache->get('database', 'charset'));
112 if (!(strlen($server) && strlen($user))) {
116 $persistent = (bool)$this->configCache->get('database', 'persistent');
118 $this->emulate_prepares = (bool)$this->configCache->get('database', 'emulate_prepares');
119 $this->pdo_emulate_prepares = (bool)$this->configCache->get('database', 'pdo_emulate_prepares');
121 if (!$this->configCache->get('database', 'disable_pdo') && class_exists('\PDO') && in_array('mysql', PDO::getAvailableDrivers())) {
122 $this->driver = 'pdo';
123 $connect = "mysql:host=" . $server . ";dbname=" . $db;
126 $connect .= ";port=" . $port;
130 $connect .= ";charset=" . $charset;
134 $this->connection = @new PDO($connect, $user, $pass, [PDO::ATTR_PERSISTENT => $persistent]);
135 $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->pdo_emulate_prepares);
136 $this->connected = true;
137 } catch (PDOException $e) {
138 $this->connected = false;
142 if (!$this->connected && class_exists('\mysqli')) {
143 $this->driver = 'mysqli';
146 $this->connection = @new mysqli($server, $user, $pass, $db, $port);
148 $this->connection = @new mysqli($server, $user, $pass, $db);
151 if (!mysqli_connect_errno()) {
152 $this->connected = true;
155 $this->connection->set_charset($charset);
160 // No suitable SQL driver was found.
161 if (!$this->connected) {
162 $this->driver = null;
163 $this->connection = null;
166 return $this->connected;
169 public function setTestmode(bool $test)
171 $this->testmode = $test;
174 * Sets the logger for DBA
176 * @note this is necessary because if we want to load the logger configuration
177 * from the DB, but there's an error, we would print out an exception.
178 * So the logger gets updated after the logger configuration can be retrieved
181 * @param LoggerInterface $logger
183 public function setLogger(LoggerInterface $logger)
185 $this->logger = $logger;
189 * Sets the profiler for DBA
191 * @param Profiler $profiler
193 public function setProfiler(Profiler $profiler)
195 $this->profiler = $profiler;
198 * Disconnects the current database connection
200 public function disconnect()
202 if (!is_null($this->connection)) {
203 switch ($this->driver) {
205 $this->connection = null;
208 $this->connection->close();
209 $this->connection = null;
214 $this->driver = null;
215 $this->connected = false;
219 * Perform a reconnect of an existing database connection
221 public function reconnect()
224 return $this->connect();
228 * Return the database object.
232 public function getConnection()
234 return $this->connection;
238 * Returns the MySQL server version string
240 * This function discriminate between the deprecated mysql API and the current
241 * object-oriented mysqli API. Example of returned string: 5.5.46-0+deb8u1
245 public function serverInfo()
247 if ($this->server_info == '') {
248 switch ($this->driver) {
250 $this->server_info = $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION);
253 $this->server_info = $this->connection->server_info;
257 return $this->server_info;
261 * Returns the selected database name
266 public function databaseName()
268 $ret = $this->p("SELECT DATABASE() AS `db`");
269 $data = $this->toArray($ret);
270 return $data[0]['db'];
274 * Analyze a database query and log this if some conditions are met.
276 * @param string $query The database query that will be analyzed
280 private function logIndex($query)
283 if (!$this->configCache->get('system', 'db_log_index')) {
287 // Don't explain an explain statement
288 if (strtolower(substr($query, 0, 7)) == "explain") {
292 // Only do the explain on "select", "update" and "delete"
293 if (!in_array(strtolower(substr($query, 0, 6)), ["select", "update", "delete"])) {
297 $r = $this->p("EXPLAIN " . $query);
298 if (!$this->isResult($r)) {
302 $watchlist = explode(',', $this->configCache->get('system', 'db_log_index_watch'));
303 $denylist = explode(',', $this->configCache->get('system', 'db_log_index_denylist'));
305 while ($row = $this->fetch($r)) {
306 if ((intval($this->configCache->get('system', 'db_loglimit_index')) > 0)) {
307 $log = (in_array($row['key'], $watchlist) &&
308 ($row['rows'] >= intval($this->configCache->get('system', 'db_loglimit_index'))));
313 if ((intval($this->configCache->get('system', 'db_loglimit_index_high')) > 0) && ($row['rows'] >= intval($this->configCache->get('system', 'db_loglimit_index_high')))) {
317 if (in_array($row['key'], $denylist) || ($row['key'] == "")) {
322 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
323 @file_put_contents($this->configCache->get('system', 'db_log_index'), DateTimeFormat::utcNow() . "\t" .
324 $row['key'] . "\t" . $row['rows'] . "\t" . $row['Extra'] . "\t" .
325 basename($backtrace[1]["file"]) . "\t" .
326 $backtrace[1]["line"] . "\t" . $backtrace[2]["function"] . "\t" .
327 substr($query, 0, 4000) . "\n", FILE_APPEND);
333 * Removes every not allowlisted character from the identifier string
335 * @param string $identifier
337 * @return string sanitized identifier
340 private function sanitizeIdentifier($identifier)
342 return preg_replace('/[^A-Za-z0-9_\-]+/', '', $identifier);
345 public function escape($str)
347 if ($this->connected) {
348 switch ($this->driver) {
350 return substr(@$this->connection->quote($str, PDO::PARAM_STR), 1, -1);
353 return @$this->connection->real_escape_string($str);
356 return str_replace("'", "\\'", $str);
360 public function isConnected()
362 return $this->connected;
365 public function connected()
369 if (is_null($this->connection)) {
373 switch ($this->driver) {
375 $r = $this->p("SELECT 1");
376 if ($this->isResult($r)) {
377 $row = $this->toArray($r);
378 $connected = ($row[0]['1'] == '1');
382 $connected = $this->connection->ping();
390 * Replaces ANY_VALUE() function by MIN() function,
391 * if the database server does not support ANY_VALUE().
393 * Considerations for Standard SQL, or MySQL with ONLY_FULL_GROUP_BY (default since 5.7.5).
394 * ANY_VALUE() is available from MySQL 5.7.5 https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html
395 * A standard fall-back is to use MIN().
397 * @param string $sql An SQL string without the values
399 * @return string The input SQL string modified if necessary.
401 public function anyValueFallback($sql)
403 $server_info = $this->serverInfo();
404 if (version_compare($server_info, '5.7.5', '<') ||
405 (stripos($server_info, 'MariaDB') !== false)) {
406 $sql = str_ireplace('ANY_VALUE(', 'MIN(', $sql);
412 * Replaces the ? placeholders with the parameters in the $args array
414 * @param string $sql SQL query
415 * @param array $args The parameters that are to replace the ? placeholders
417 * @return string The replaced SQL query
419 private function replaceParameters($sql, $args)
422 foreach ($args AS $param => $value) {
423 if (is_int($args[$param]) || is_float($args[$param]) || is_bool($args[$param])) {
424 $replace = intval($args[$param]);
425 } elseif (is_null($args[$param])) {
428 $replace = "'" . $this->escape($args[$param]) . "'";
431 $pos = strpos($sql, '?', $offset);
432 if ($pos !== false) {
433 $sql = substr_replace($sql, $replace, $pos, 1);
435 $offset = $pos + strlen($replace);
441 * Executes a prepared statement that returns data
443 * @usage Example: $r = p("SELECT * FROM `item` WHERE `guid` = ?", $guid);
445 * Please only use it with complicated queries.
446 * For all regular queries please use DBA::select or DBA::exists
448 * @param string $sql SQL statement
450 * @return bool|object statement object or result object
453 public function p($sql)
456 $stamp1 = microtime(true);
458 $params = DBA::getParam(func_get_args());
460 // Renumber the array keys to be sure that they fit
463 foreach ($params AS $param) {
464 // Avoid problems with some MySQL servers and boolean values. See issue #3645
465 if (is_bool($param)) {
466 $param = (int)$param;
468 $args[++$i] = $param;
471 if (!$this->connected) {
475 if ((substr_count($sql, '?') != count($args)) && (count($args) > 0)) {
476 // Question: Should we continue or stop the query here?
477 $this->logger->warning('Query parameters mismatch.', ['query' => $sql, 'args' => $args, 'callstack' => System::callstack()]);
480 $sql = DBA::cleanQuery($sql);
481 $sql = $this->anyValueFallback($sql);
485 if ($this->configCache->get('system', 'db_callstack') !== null) {
486 $sql = "/*" . System::callstack() . " */ " . $sql;
492 $this->affected_rows = 0;
494 // We have to make some things different if this function is called from "e"
495 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
497 if (isset($trace[1])) {
498 $called_from = $trace[1];
500 // We use just something that is defined to avoid warnings
501 $called_from = $trace[0];
503 // We are having an own error logging in the function "e"
504 $called_from_e = ($called_from['function'] == 'e');
506 if (!isset($this->connection)) {
507 throw new InternalServerErrorException('The Connection is empty, although connected is set true.');
510 switch ($this->driver) {
512 // If there are no arguments we use "query"
513 if ($this->emulate_prepares || count($args) == 0) {
514 if (!$retval = $this->connection->query($this->replaceParameters($sql, $args))) {
515 $errorInfo = $this->connection->errorInfo();
516 $this->error = $errorInfo[2];
517 $this->errorno = $errorInfo[1];
522 $this->affected_rows = $retval->rowCount();
526 /** @var $stmt mysqli_stmt|PDOStatement */
527 if (!$stmt = $this->connection->prepare($sql)) {
528 $errorInfo = $this->connection->errorInfo();
529 $this->error = $errorInfo[2];
530 $this->errorno = $errorInfo[1];
536 foreach ($args AS $param => $value) {
537 if (is_int($args[$param])) {
538 $data_type = PDO::PARAM_INT;
540 $data_type = PDO::PARAM_STR;
542 $stmt->bindParam($param, $args[$param], $data_type);
545 if (!$stmt->execute()) {
546 $errorInfo = $stmt->errorInfo();
547 $this->error = $errorInfo[2];
548 $this->errorno = $errorInfo[1];
553 $this->affected_rows = $retval->rowCount();
557 // There are SQL statements that cannot be executed with a prepared statement
558 $parts = explode(' ', $orig_sql);
559 $command = strtolower($parts[0]);
560 $can_be_prepared = in_array($command, ['select', 'update', 'insert', 'delete']);
562 // The fallback routine is called as well when there are no arguments
563 if ($this->emulate_prepares || !$can_be_prepared || (count($args) == 0)) {
564 $retval = $this->connection->query($this->replaceParameters($sql, $args));
565 if ($this->connection->errno) {
566 $this->error = $this->connection->error;
567 $this->errorno = $this->connection->errno;
571 if (isset($retval->num_rows)) {
572 $this->affected_rows = $retval->num_rows;
574 $this->affected_rows = $this->connection->affected_rows;
580 $stmt = $this->connection->stmt_init();
582 if (!$stmt->prepare($sql)) {
583 $this->error = $stmt->error;
584 $this->errorno = $stmt->errno;
592 foreach ($args AS $param => $value) {
593 if (is_int($args[$param])) {
595 } elseif (is_float($args[$param])) {
597 } elseif (is_string($args[$param])) {
602 $values[] = &$args[$param];
605 if (count($values) > 0) {
606 array_unshift($values, $param_types);
607 call_user_func_array([$stmt, 'bind_param'], $values);
610 if (!$stmt->execute()) {
611 $this->error = $this->connection->error;
612 $this->errorno = $this->connection->errno;
616 $stmt->store_result();
618 $this->affected_rows = $retval->affected_rows;
623 // See issue https://github.com/friendica/friendica/issues/8572
624 // Ensure that we always get an error message on an error.
625 if ($is_error && empty($this->errorno)) {
629 if ($is_error && empty($this->error)) {
630 $this->error = 'Unknown database error';
633 // We are having an own error logging in the function "e"
634 if (($this->errorno != 0) && !$called_from_e) {
635 // We have to preserve the error code, somewhere in the logging it get lost
636 $error = $this->error;
637 $errorno = $this->errorno;
639 if ($this->testmode) {
640 throw new DatabaseException($error, $errorno, $this->replaceParameters($sql, $args));
643 $this->logger->error('DB Error', [
646 'callstack' => System::callstack(8),
647 'params' => $this->replaceParameters($sql, $args),
650 // On a lost connection we try to reconnect - but only once.
651 if ($errorno == 2006) {
652 if ($this->in_retrial || !$this->reconnect()) {
653 // It doesn't make sense to continue when the database connection was lost
654 if ($this->in_retrial) {
655 $this->logger->notice('Giving up retrial because of database error', [
660 $this->logger->notice('Couldn\'t reconnect after database error', [
668 $this->logger->notice('Reconnected after database error', [
672 $this->in_retrial = true;
673 $ret = $this->p($sql, $args);
674 $this->in_retrial = false;
679 $this->error = $error;
680 $this->errorno = $errorno;
683 $this->profiler->saveTimestamp($stamp1, 'database');
685 if ($this->configCache->get('system', 'db_log')) {
686 $stamp2 = microtime(true);
687 $duration = (float)($stamp2 - $stamp1);
689 if (($duration > $this->configCache->get('system', 'db_loglimit'))) {
690 $duration = round($duration, 3);
691 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
693 @file_put_contents($this->configCache->get('system', 'db_log'), DateTimeFormat::utcNow() . "\t" . $duration . "\t" .
694 basename($backtrace[1]["file"]) . "\t" .
695 $backtrace[1]["line"] . "\t" . $backtrace[2]["function"] . "\t" .
696 substr($this->replaceParameters($sql, $args), 0, 4000) . "\n", FILE_APPEND);
703 * Executes a prepared statement like UPDATE or INSERT that doesn't return data
705 * Please use DBA::delete, DBA::insert, DBA::update, ... instead
707 * @param string $sql SQL statement
709 * @return boolean Was the query successfull? False is returned only if an error occurred
712 public function e($sql)
715 $stamp = microtime(true);
717 $params = DBA::getParam(func_get_args());
719 // In a case of a deadlock we are repeating the query 20 times
723 $stmt = $this->p($sql, $params);
725 if (is_bool($stmt)) {
727 } elseif (is_object($stmt)) {
735 } while (($this->errorno == 1213) && (--$timeout > 0));
737 if ($this->errorno != 0) {
738 // We have to preserve the error code, somewhere in the logging it get lost
739 $error = $this->error;
740 $errorno = $this->errorno;
742 if ($this->testmode) {
743 throw new DatabaseException($error, $errorno, $this->replaceParameters($sql, $params));
746 $this->logger->error('DB Error', [
749 'callstack' => System::callstack(8),
750 'params' => $this->replaceParameters($sql, $params),
753 // On a lost connection we simply quit.
754 // A reconnect like in $this->p could be dangerous with modifications
755 if ($errorno == 2006) {
756 $this->logger->notice('Giving up because of database error', [
763 $this->error = $error;
764 $this->errorno = $errorno;
767 $this->profiler->saveTimestamp($stamp, "database_write");
773 * Check if data exists
775 * @param string|array $table Table name or array [schema => table]
776 * @param array $condition array of fields for condition
778 * @return boolean Are there rows for that condition?
781 public function exists($table, $condition)
789 if (empty($condition)) {
790 return DBStructure::existsTable($table);
794 $first_key = key($condition);
795 if (!is_int($first_key)) {
796 $fields = [$first_key];
799 $stmt = $this->select($table, $fields, $condition, ['limit' => 1]);
801 if (is_bool($stmt)) {
804 $retval = ($this->numRows($stmt) > 0);
813 * Fetches the first row
815 * Please use DBA::selectFirst or DBA::exists whenever this is possible.
817 * Fetches the first row
819 * @param string $sql SQL statement
821 * @return array first row of query
824 public function fetchFirst($sql)
826 $params = DBA::getParam(func_get_args());
828 $stmt = $this->p($sql, $params);
830 if (is_bool($stmt)) {
833 $retval = $this->fetch($stmt);
842 * Returns the number of affected rows of the last statement
844 * @return int Number of rows
846 public function affectedRows()
848 return $this->affected_rows;
852 * Returns the number of columns of a statement
854 * @param object Statement object
856 * @return int Number of columns
858 public function columnCount($stmt)
860 if (!is_object($stmt)) {
863 switch ($this->driver) {
865 return $stmt->columnCount();
867 return $stmt->field_count;
873 * Returns the number of rows of a statement
875 * @param PDOStatement|mysqli_result|mysqli_stmt Statement object
877 * @return int Number of rows
879 public function numRows($stmt)
881 if (!is_object($stmt)) {
884 switch ($this->driver) {
886 return $stmt->rowCount();
888 return $stmt->num_rows;
896 * @param mixed $stmt statement object
898 * @return array current row
900 public function fetch($stmt)
903 $stamp1 = microtime(true);
907 if (!is_object($stmt)) {
911 switch ($this->driver) {
913 $columns = $stmt->fetch(PDO::FETCH_ASSOC);
916 if (get_class($stmt) == 'mysqli_result') {
917 $columns = $stmt->fetch_assoc();
921 // This code works, but is slow
923 // Bind the result to a result array
927 for ($x = 0; $x < $stmt->field_count; $x++) {
928 $cols[] = &$cols_num[$x];
931 call_user_func_array([$stmt, 'bind_result'], $cols);
933 if (!$stmt->fetch()) {
938 // We need to get the field names for the array keys
939 // It seems that there is no better way to do this.
940 $result = $stmt->result_metadata();
941 $fields = $result->fetch_fields();
943 foreach ($cols_num AS $param => $col) {
944 $columns[$fields[$param]->name] = $col;
948 $this->profiler->saveTimestamp($stamp1, 'database');
954 * Insert a row into a table
956 * @param string|array $table Table name or array [schema => table]
957 * @param array $param parameter array
958 * @param bool $on_duplicate_update Do an update on a duplicate entry
960 * @return boolean was the insert successful?
963 public function insert($table, array $param, bool $on_duplicate_update = false)
965 if (empty($table) || empty($param)) {
966 $this->logger->info('Table and fields have to be set');
970 $table_string = DBA::buildTableString($table);
972 $fields_string = implode(', ', array_map([DBA::class, 'quoteIdentifier'], array_keys($param)));
974 $values_string = substr(str_repeat("?, ", count($param)), 0, -2);
976 $sql = "INSERT INTO " . $table_string . " (" . $fields_string . ") VALUES (" . $values_string . ")";
978 if ($on_duplicate_update) {
979 $fields_string = implode(' = ?, ', array_map([DBA::class, 'quoteIdentifier'], array_keys($param)));
981 $sql .= " ON DUPLICATE KEY UPDATE " . $fields_string . " = ?";
983 $values = array_values($param);
984 $param = array_merge_recursive($values, $values);
987 return $this->e($sql, $param);
991 * Inserts a row with the provided data in the provided table.
992 * If the data corresponds to an existing row through a UNIQUE or PRIMARY index constraints, it updates the row instead.
994 * @param string|array $table Table name or array [schema => table]
995 * @param array $param parameter array
997 * @return boolean was the insert successful?
1000 public function replace($table, array $param)
1002 if (empty($table) || empty($param)) {
1003 $this->logger->info('Table and fields have to be set');
1007 $table_string = DBA::buildTableString($table);
1009 $fields_string = implode(', ', array_map([DBA::class, 'quoteIdentifier'], array_keys($param)));
1011 $values_string = substr(str_repeat("?, ", count($param)), 0, -2);
1013 $sql = "REPLACE " . $table_string . " (" . $fields_string . ") VALUES (" . $values_string . ")";
1015 return $this->e($sql, $param);
1019 * Fetch the id of the last insert command
1021 * @return integer Last inserted id
1023 public function lastInsertId()
1025 switch ($this->driver) {
1027 $id = $this->connection->lastInsertId();
1030 $id = $this->connection->insert_id;
1037 * Locks a table for exclusive write access
1039 * This function can be extended in the future to accept a table array as well.
1041 * @param string|array $table Table name or array [schema => table]
1043 * @return boolean was the lock successful?
1044 * @throws \Exception
1046 public function lock($table)
1048 // See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
1049 if ($this->driver == 'pdo') {
1050 $this->e("SET autocommit=0");
1051 $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
1053 $this->connection->autocommit(false);
1056 $success = $this->e("LOCK TABLES " . DBA::buildTableString($table) . " WRITE");
1058 if ($this->driver == 'pdo') {
1059 $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->pdo_emulate_prepares);
1063 if ($this->driver == 'pdo') {
1064 $this->e("SET autocommit=1");
1066 $this->connection->autocommit(true);
1069 $this->in_transaction = true;
1075 * Unlocks all locked tables
1077 * @return boolean was the unlock successful?
1078 * @throws \Exception
1080 public function unlock()
1082 // See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
1083 $this->performCommit();
1085 if ($this->driver == 'pdo') {
1086 $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
1089 $success = $this->e("UNLOCK TABLES");
1091 if ($this->driver == 'pdo') {
1092 $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->pdo_emulate_prepares);
1093 $this->e("SET autocommit=1");
1095 $this->connection->autocommit(true);
1098 $this->in_transaction = false;
1103 * Starts a transaction
1105 * @return boolean Was the command executed successfully?
1107 public function transaction()
1109 if (!$this->performCommit()) {
1113 switch ($this->driver) {
1115 if (!$this->connection->inTransaction() && !$this->connection->beginTransaction()) {
1121 if (!$this->connection->begin_transaction()) {
1127 $this->in_transaction = true;
1131 protected function performCommit()
1133 switch ($this->driver) {
1135 if (!$this->connection->inTransaction()) {
1139 return $this->connection->commit();
1142 return $this->connection->commit();
1151 * @return boolean Was the command executed successfully?
1153 public function commit()
1155 if (!$this->performCommit()) {
1158 $this->in_transaction = false;
1165 * @return boolean Was the command executed successfully?
1167 public function rollback()
1171 switch ($this->driver) {
1173 if (!$this->connection->inTransaction()) {
1177 $ret = $this->connection->rollBack();
1181 $ret = $this->connection->rollback();
1184 $this->in_transaction = false;
1189 * Build the array with the table relations
1191 * The array is build from the database definitions in DBStructure.php
1193 * This process must only be started once, since the value is cached.
1195 private function buildRelationData()
1197 $definition = DBStructure::definition($this->configCache->get('system', 'basepath'));
1199 foreach ($definition AS $table => $structure) {
1200 foreach ($structure['fields'] AS $field => $field_struct) {
1201 if (isset($field_struct['relation'])) {
1202 foreach ($field_struct['relation'] AS $rel_table => $rel_field) {
1203 $this->relation[$rel_table][$rel_field][$table][] = $field;
1211 * Delete a row from a table
1213 * Note: this methods does NOT accept schema => table arrays because of the complex relation stuff.
1215 * @param string $table Table name
1216 * @param array $conditions Field condition(s)
1217 * @param array $options
1218 * - cascade: If true we delete records in other tables that depend on the one we're deleting through
1219 * relations (default: true)
1220 * @param array $callstack Internal use: prevent endless loops
1222 * @return boolean was the delete successful?
1223 * @throws \Exception
1225 public function delete($table, array $conditions, array $options = [], array &$callstack = [])
1227 if (empty($table) || empty($conditions)) {
1228 $this->logger->info('Table and conditions have to be set');
1234 // Create a key for the loop prevention
1235 $key = $table . ':' . json_encode($conditions);
1237 // We quit when this key already exists in the callstack.
1238 if (isset($callstack[$key])) {
1242 $callstack[$key] = true;
1244 $commands[$key] = ['table' => $table, 'conditions' => $conditions];
1246 // Don't use "defaults" here, since it would set "false" to "true"
1247 if (isset($options['cascade'])) {
1248 $cascade = $options['cascade'];
1253 // To speed up the whole process we cache the table relations
1254 if ($cascade && count($this->relation) == 0) {
1255 $this->buildRelationData();
1258 // Is there a relation entry for the table?
1259 if ($cascade && isset($this->relation[$table])) {
1260 // We only allow a simple "one field" relation.
1261 $field = array_keys($this->relation[$table])[0];
1262 $rel_def = array_values($this->relation[$table])[0];
1264 // Create a key for preventing double queries
1265 $qkey = $field . '-' . $table . ':' . json_encode($conditions);
1267 // When the search field is the relation field, we don't need to fetch the rows
1268 // This is useful when the leading record is already deleted in the frontend but the rest is done in the backend
1269 if ((count($conditions) == 1) && ($field == array_keys($conditions)[0])) {
1270 foreach ($rel_def AS $rel_table => $rel_fields) {
1271 foreach ($rel_fields AS $rel_field) {
1272 $this->delete($rel_table, [$rel_field => array_values($conditions)[0]], $options, $callstack);
1275 // We quit when this key already exists in the callstack.
1276 } elseif (!isset($callstack[$qkey])) {
1277 $callstack[$qkey] = true;
1279 // Fetch all rows that are to be deleted
1280 $data = $this->select($table, [$field], $conditions);
1282 while ($row = $this->fetch($data)) {
1283 $this->delete($table, [$field => $row[$field]], $options, $callstack);
1286 $this->close($data);
1288 // Since we had split the delete command we don't need the original command anymore
1289 unset($commands[$key]);
1293 // Now we finalize the process
1294 $do_transaction = !$this->in_transaction;
1296 if ($do_transaction) {
1297 $this->transaction();
1303 foreach ($commands AS $command) {
1304 $conditions = $command['conditions'];
1306 $first_key = key($conditions);
1308 $condition_string = DBA::buildCondition($conditions);
1310 if ((count($command['conditions']) > 1) || is_int($first_key)) {
1311 $sql = "DELETE FROM " . DBA::quoteIdentifier($command['table']) . " " . $condition_string;
1312 $this->logger->info($this->replaceParameters($sql, $conditions), ['callstack' => System::callstack(6), 'internal_callstack' => $callstack]);
1314 if (!$this->e($sql, $conditions)) {
1315 if ($do_transaction) {
1321 $key_table = $command['table'];
1322 $key_condition = array_keys($command['conditions'])[0];
1323 $value = array_values($command['conditions'])[0];
1325 // Split the SQL queries in chunks of 100 values
1326 // We do the $i stuff here to make the code better readable
1327 $i = isset($counter[$key_table][$key_condition]) ? $counter[$key_table][$key_condition] : 0;
1328 if (isset($compacted[$key_table][$key_condition][$i]) && count($compacted[$key_table][$key_condition][$i]) > 100) {
1332 $compacted[$key_table][$key_condition][$i][$value] = $value;
1333 $counter[$key_table][$key_condition] = $i;
1336 foreach ($compacted AS $table => $values) {
1337 foreach ($values AS $field => $field_value_list) {
1338 foreach ($field_value_list AS $field_values) {
1339 $sql = "DELETE FROM " . DBA::quoteIdentifier($table) . " WHERE " . DBA::quoteIdentifier($field) . " IN (" .
1340 substr(str_repeat("?, ", count($field_values)), 0, -2) . ");";
1342 $this->logger->info($this->replaceParameters($sql, $field_values), ['callstack' => System::callstack(6), 'internal_callstack' => $callstack]);
1344 if (!$this->e($sql, $field_values)) {
1345 if ($do_transaction) {
1353 if ($do_transaction) {
1362 * Updates rows in the database. When $old_fields is set to an array,
1363 * the system will only do an update if the fields in that array changed.
1366 * Only the values in $old_fields are compared.
1367 * This is an intentional behaviour.
1370 * We include the timestamp field in $fields but not in $old_fields.
1371 * Then the row will only get the new timestamp when the other fields had changed.
1373 * When $old_fields is set to a boolean value the system will do this compare itself.
1374 * When $old_fields is set to "true" the system will do an insert if the row doesn't exists.
1377 * Only set $old_fields to a boolean value when you are sure that you will update a single row.
1378 * When you set $old_fields to "true" then $fields must contain all relevant fields!
1380 * @param string|array $table Table name or array [schema => table]
1381 * @param array $fields contains the fields that are updated
1382 * @param array $condition condition array with the key values
1383 * @param array|boolean $old_fields array with the old field values that are about to be replaced (true = update on duplicate)
1385 * @return boolean was the update successfull?
1386 * @throws \Exception
1388 public function update($table, $fields, $condition, $old_fields = [])
1390 if (empty($table) || empty($fields) || empty($condition)) {
1391 $this->logger->info('Table, fields and condition have to be set');
1395 if (is_bool($old_fields)) {
1396 $do_insert = $old_fields;
1398 $old_fields = $this->selectFirst($table, [], $condition);
1400 if (is_bool($old_fields)) {
1402 $values = array_merge($condition, $fields);
1403 return $this->replace($table, $values);
1409 foreach ($old_fields AS $fieldname => $content) {
1410 if (isset($fields[$fieldname]) && !is_null($content) && ($fields[$fieldname] == $content)) {
1411 unset($fields[$fieldname]);
1415 if (count($fields) == 0) {
1419 $table_string = DBA::buildTableString($table);
1421 $condition_string = DBA::buildCondition($condition);
1423 $sql = "UPDATE " . $table_string . " SET "
1424 . implode(" = ?, ", array_map([DBA::class, 'quoteIdentifier'], array_keys($fields))) . " = ?"
1425 . $condition_string;
1427 // Combines the updated fields parameter values with the condition parameter values
1428 $params = array_merge(array_values($fields), $condition);
1430 return $this->e($sql, $params);
1434 * Retrieve a single record from a table and returns it in an associative array
1436 * @param string|array $table
1437 * @param array $fields
1438 * @param array $condition
1439 * @param array $params
1441 * @return bool|array
1442 * @throws \Exception
1443 * @see $this->select
1445 public function selectFirst($table, array $fields = [], array $condition = [], $params = [])
1447 $params['limit'] = 1;
1448 $result = $this->select($table, $fields, $condition, $params);
1450 if (is_bool($result)) {
1453 $row = $this->fetch($result);
1454 $this->close($result);
1460 * Select rows from a table and fills an array with the data
1462 * @param string|array $table Table name or array [schema => table]
1463 * @param array $fields Array of selected fields, empty for all
1464 * @param array $condition Array of fields for condition
1465 * @param array $params Array of several parameters
1467 * @return array Data array
1468 * @throws \Exception
1471 public function selectToArray($table, array $fields = [], array $condition = [], array $params = [])
1473 return $this->toArray($this->select($table, $fields, $condition, $params));
1477 * Select rows from a table
1483 * $table = ['schema' => 'table'];
1484 * @see DBA::buildTableString()
1486 * $fields = ['id', 'uri', 'uid', 'network'];
1488 * $condition = ['uid' => 1, 'network' => 'dspr', 'blocked' => true];
1490 * $condition = ['`uid` = ? AND `network` IN (?, ?)', 1, 'dfrn', 'dspr'];
1491 * @see DBA::buildCondition()
1493 * $params = ['order' => ['id', 'received' => true, 'created' => 'ASC'), 'limit' => 10];
1494 * @see DBA::buildParameter()
1496 * $data = DBA::select($table, $fields, $condition, $params);
1498 * @param string|array $table Table name or array [schema => table]
1499 * @param array $fields Array of selected fields, empty for all
1500 * @param array $condition Array of fields for condition
1501 * @param array $params Array of several parameters
1502 * @return boolean|object
1503 * @throws \Exception
1505 public function select($table, array $fields = [], array $condition = [], array $params = [])
1507 if (empty($table)) {
1511 if (count($fields) > 0) {
1512 $select_string = implode(', ', array_map([DBA::class, 'quoteIdentifier'], $fields));
1514 $select_string = '*';
1517 $table_string = DBA::buildTableString($table);
1519 $condition_string = DBA::buildCondition($condition);
1521 $param_string = DBA::buildParameter($params);
1523 $sql = "SELECT " . $select_string . " FROM " . $table_string . $condition_string . $param_string;
1525 $result = $this->p($sql, $condition);
1531 * Counts the rows from a table satisfying the provided condition
1533 * @param string|array $table Table name or array [schema => table]
1534 * @param array $condition Array of fields for condition
1535 * @param array $params Array of several parameters
1542 * $condition = ["uid" => 1, "network" => 'dspr'];
1544 * $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr'];
1546 * $count = DBA::count($table, $condition);
1547 * @throws \Exception
1549 public function count($table, array $condition = [], array $params = [])
1551 if (empty($table)) {
1555 $table_string = DBA::buildTableString($table);
1557 $condition_string = DBA::buildCondition($condition);
1559 if (empty($params['expression'])) {
1561 } elseif (!empty($params['distinct'])) {
1562 $expression = "DISTINCT " . DBA::quoteIdentifier($params['expression']);
1564 $expression = DBA::quoteIdentifier($params['expression']);
1567 $sql = "SELECT COUNT(" . $expression . ") AS `count` FROM " . $table_string . $condition_string;
1569 $row = $this->fetchFirst($sql, $condition);
1571 return $row['count'];
1575 * Fills an array with data from a query
1577 * @param object $stmt statement object
1578 * @param bool $do_close
1580 * @return array Data array
1582 public function toArray($stmt, $do_close = true)
1584 if (is_bool($stmt)) {
1589 while ($row = $this->fetch($stmt)) {
1594 $this->close($stmt);
1601 * Returns the error number of the last query
1603 * @return string Error number (0 if no error)
1605 public function errorNo()
1607 return $this->errorno;
1611 * Returns the error message of the last query
1613 * @return string Error message ('' if no error)
1615 public function errorMessage()
1617 return $this->error;
1621 * Closes the current statement
1623 * @param object $stmt statement object
1625 * @return boolean was the close successful?
1627 public function close($stmt)
1630 $stamp1 = microtime(true);
1632 if (!is_object($stmt)) {
1636 switch ($this->driver) {
1638 $ret = $stmt->closeCursor();
1641 // MySQLi offers both a mysqli_stmt and a mysqli_result class.
1642 // We should be careful not to assume the object type of $stmt
1643 // because DBA::p() has been able to return both types.
1644 if ($stmt instanceof mysqli_stmt) {
1645 $stmt->free_result();
1646 $ret = $stmt->close();
1647 } elseif ($stmt instanceof mysqli_result) {
1656 $this->profiler->saveTimestamp($stamp1, 'database');
1662 * Return a list of database processes
1665 * 'list' => List of processes, separated in their different states
1666 * 'amount' => Number of concurrent database processes
1667 * @throws \Exception
1669 public function processlist()
1671 $ret = $this->p("SHOW PROCESSLIST");
1672 $data = $this->toArray($ret);
1676 foreach ($data as $process) {
1677 $state = trim($process["State"]);
1679 // Filter out all non blocking processes
1680 if (!in_array($state, ["", "init", "statistics", "updating"])) {
1687 foreach ($states as $state => $usage) {
1688 if ($statelist != "") {
1691 $statelist .= $state . ": " . $usage;
1693 return (["list" => $statelist, "amount" => $processes]);
1697 * Fetch a database variable
1699 * @param string $name
1700 * @return string content
1702 public function getVariable(string $name)
1704 $result = $this->fetchFirst("SHOW GLOBAL VARIABLES WHERE `Variable_name` = ?", $name);
1705 return $result['Value'] ?? null;
1709 * Checks if $array is a filled array with at least one entry.
1711 * @param mixed $array A filled array with at least one entry
1713 * @return boolean Whether $array is a filled array or an object with rows
1715 public function isResult($array)
1717 // It could be a return value from an update statement
1718 if (is_bool($array)) {
1722 if (is_object($array)) {
1723 return $this->numRows($array) > 0;
1726 return (is_array($array) && (count($array) > 0));
1730 * Callback function for "esc_array"
1732 * @param mixed $value Array value
1733 * @param string $key Array key
1734 * @param boolean $add_quotation add quotation marks for string values
1738 private function escapeArrayCallback(&$value, $key, $add_quotation)
1740 if (!$add_quotation) {
1741 if (is_bool($value)) {
1742 $value = ($value ? '1' : '0');
1744 $value = $this->escape($value);
1749 if (is_bool($value)) {
1750 $value = ($value ? 'true' : 'false');
1751 } elseif (is_float($value) || is_integer($value)) {
1752 $value = (string)$value;
1754 $value = "'" . $this->escape($value) . "'";
1759 * Escapes a whole array
1761 * @param mixed $arr Array with values to be escaped
1762 * @param boolean $add_quotation add quotation marks for string values
1766 public function escapeArray(&$arr, $add_quotation = false)
1768 array_walk($arr, [$this, 'escapeArrayCallback'], $add_quotation);