3 namespace Friendica\Database;
5 use Friendica\Core\Config\Cache\ConfigCache;
6 use Friendica\Core\System;
7 use Friendica\Network\HTTPException\InternalServerErrorException;
8 use Friendica\Util\DateTimeFormat;
9 use Friendica\Util\Profiler;
16 use Psr\Log\LoggerInterface;
19 * @class MySQL database class
21 * This class is for the low level database stuff that does driver specific things.
25 protected $connected = false;
30 protected $configCache;
36 * @var LoggerInterface
39 protected $server_info = '';
40 /** @var PDO|mysqli */
41 protected $connection;
43 private $error = false;
45 private $affected_rows = 0;
46 protected $in_transaction = false;
47 protected $in_retrial = false;
48 private $relation = [];
50 public function __construct(ConfigCache $configCache, Profiler $profiler, LoggerInterface $logger, array $server = [])
52 // We are storing these values for being able to perform a reconnect
53 $this->configCache = $configCache;
54 $this->profiler = $profiler;
55 $this->logger = $logger;
57 $this->readServerVariables($server);
60 if ($this->isConnected()) {
61 // Loads DB_UPDATE_VERSION constant
62 DBStructure::definition($configCache->get('system', 'basepath'), false);
66 private function readServerVariables(array $server)
68 // Use environment variables for mysql if they are set beforehand
69 if (!empty($server['MYSQL_HOST'])
70 && !empty($server['MYSQL_USERNAME'] || !empty($server['MYSQL_USER']))
71 && $server['MYSQL_PASSWORD'] !== false
72 && !empty($server['MYSQL_DATABASE']))
74 $db_host = $server['MYSQL_HOST'];
75 if (!empty($server['MYSQL_PORT'])) {
76 $db_host .= ':' . $server['MYSQL_PORT'];
78 $this->configCache->set('database', 'hostname', $db_host);
80 if (!empty($server['MYSQL_USERNAME'])) {
81 $this->configCache->set('database', 'username', $server['MYSQL_USERNAME']);
83 $this->configCache->set('database', 'username', $server['MYSQL_USER']);
85 $this->configCache->set('database', 'password', (string) $server['MYSQL_PASSWORD']);
86 $this->configCache->set('database', 'database', $server['MYSQL_DATABASE']);
90 public function connect()
92 if (!is_null($this->connection) && $this->connected()) {
97 $serveraddr = trim($this->configCache->get('database', 'hostname'));
98 $serverdata = explode(':', $serveraddr);
99 $server = $serverdata[0];
100 if (count($serverdata) > 1) {
101 $port = trim($serverdata[1]);
103 $server = trim($server);
104 $user = trim($this->configCache->get('database', 'username'));
105 $pass = trim($this->configCache->get('database', 'password'));
106 $db = trim($this->configCache->get('database', 'database'));
107 $charset = trim($this->configCache->get('database', 'charset'));
109 if (!(strlen($server) && strlen($user))) {
113 if (class_exists('\PDO') && in_array('mysql', PDO::getAvailableDrivers())) {
114 $this->driver = 'pdo';
115 $connect = "mysql:host=" . $server . ";dbname=" . $db;
118 $connect .= ";port=" . $port;
122 $connect .= ";charset=" . $charset;
126 $this->connection = @new PDO($connect, $user, $pass);
127 $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
128 $this->connected = true;
129 } catch (PDOException $e) {
130 $this->connected = false;
134 if (!$this->connected && class_exists('\mysqli')) {
135 $this->driver = 'mysqli';
138 $this->connection = @new mysqli($server, $user, $pass, $db, $port);
140 $this->connection = @new mysqli($server, $user, $pass, $db);
143 if (!mysqli_connect_errno()) {
144 $this->connected = true;
147 $this->connection->set_charset($charset);
152 // No suitable SQL driver was found.
153 if (!$this->connected) {
154 $this->driver = null;
155 $this->connection = null;
158 return $this->connected;
162 * Sets the logger for DBA
164 * @note this is necessary because if we want to load the logger configuration
165 * from the DB, but there's an error, we would print out an exception.
166 * So the logger gets updated after the logger configuration can be retrieved
169 * @param LoggerInterface $logger
171 public function setLogger(LoggerInterface $logger)
173 $this->logger = $logger;
177 * Sets the profiler for DBA
179 * @param Profiler $profiler
181 public function setProfiler(Profiler $profiler)
183 $this->profiler = $profiler;
186 * Disconnects the current database connection
188 public function disconnect()
190 if (is_null($this->connection)) {
194 switch ($this->driver) {
196 $this->connection = null;
199 $this->connection->close();
200 $this->connection = null;
206 * Perform a reconnect of an existing database connection
208 public function reconnect()
211 return $this->connect();
215 * Return the database object.
219 public function getConnection()
221 return $this->connection;
225 * @brief Returns the MySQL server version string
227 * This function discriminate between the deprecated mysql API and the current
228 * object-oriented mysqli API. Example of returned string: 5.5.46-0+deb8u1
232 public function serverInfo()
234 if ($this->server_info == '') {
235 switch ($this->driver) {
237 $this->server_info = $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION);
240 $this->server_info = $this->connection->server_info;
244 return $this->server_info;
248 * @brief Returns the selected database name
253 public function databaseName()
255 $ret = $this->p("SELECT DATABASE() AS `db`");
256 $data = $this->toArray($ret);
257 return $data[0]['db'];
261 * @brief Analyze a database query and log this if some conditions are met.
263 * @param string $query The database query that will be analyzed
267 private function logIndex($query)
270 if (!$this->configCache->get('system', 'db_log_index')) {
274 // Don't explain an explain statement
275 if (strtolower(substr($query, 0, 7)) == "explain") {
279 // Only do the explain on "select", "update" and "delete"
280 if (!in_array(strtolower(substr($query, 0, 6)), ["select", "update", "delete"])) {
284 $r = $this->p("EXPLAIN " . $query);
285 if (!$this->isResult($r)) {
289 $watchlist = explode(',', $this->configCache->get('system', 'db_log_index_watch'));
290 $blacklist = explode(',', $this->configCache->get('system', 'db_log_index_blacklist'));
292 while ($row = $this->fetch($r)) {
293 if ((intval($this->configCache->get('system', 'db_loglimit_index')) > 0)) {
294 $log = (in_array($row['key'], $watchlist) &&
295 ($row['rows'] >= intval($this->configCache->get('system', 'db_loglimit_index'))));
300 if ((intval($this->configCache->get('system', 'db_loglimit_index_high')) > 0) && ($row['rows'] >= intval($this->configCache->get('system', 'db_loglimit_index_high')))) {
304 if (in_array($row['key'], $blacklist) || ($row['key'] == "")) {
309 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
310 @file_put_contents($this->configCache->get('system', 'db_log_index'), DateTimeFormat::utcNow() . "\t" .
311 $row['key'] . "\t" . $row['rows'] . "\t" . $row['Extra'] . "\t" .
312 basename($backtrace[1]["file"]) . "\t" .
313 $backtrace[1]["line"] . "\t" . $backtrace[2]["function"] . "\t" .
314 substr($query, 0, 2000) . "\n", FILE_APPEND);
320 * Removes every not whitelisted character from the identifier string
322 * @param string $identifier
324 * @return string sanitized identifier
327 private function sanitizeIdentifier($identifier)
329 return preg_replace('/[^A-Za-z0-9_\-]+/', '', $identifier);
332 public function escape($str)
334 if ($this->connected) {
335 switch ($this->driver) {
337 return substr(@$this->connection->quote($str, PDO::PARAM_STR), 1, -1);
340 return @$this->connection->real_escape_string($str);
343 return str_replace("'", "\\'", $str);
347 public function isConnected()
349 return $this->connected;
352 public function connected()
356 if (is_null($this->connection)) {
360 switch ($this->driver) {
362 $r = $this->p("SELECT 1");
363 if ($this->isResult($r)) {
364 $row = $this->toArray($r);
365 $connected = ($row[0]['1'] == '1');
369 $connected = $this->connection->ping();
376 * @brief Replaces ANY_VALUE() function by MIN() function,
377 * if the database server does not support ANY_VALUE().
379 * Considerations for Standard SQL, or MySQL with ONLY_FULL_GROUP_BY (default since 5.7.5).
380 * ANY_VALUE() is available from MySQL 5.7.5 https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html
381 * A standard fall-back is to use MIN().
383 * @param string $sql An SQL string without the values
385 * @return string The input SQL string modified if necessary.
387 public function anyValueFallback($sql)
389 $server_info = $this->serverInfo();
390 if (version_compare($server_info, '5.7.5', '<') ||
391 (stripos($server_info, 'MariaDB') !== false)) {
392 $sql = str_ireplace('ANY_VALUE(', 'MIN(', $sql);
398 * @brief Replaces the ? placeholders with the parameters in the $args array
400 * @param string $sql SQL query
401 * @param array $args The parameters that are to replace the ? placeholders
403 * @return string The replaced SQL query
405 private function replaceParameters($sql, $args)
408 foreach ($args AS $param => $value) {
409 if (is_int($args[$param]) || is_float($args[$param])) {
410 $replace = intval($args[$param]);
412 $replace = "'" . $this->escape($args[$param]) . "'";
415 $pos = strpos($sql, '?', $offset);
416 if ($pos !== false) {
417 $sql = substr_replace($sql, $replace, $pos, 1);
419 $offset = $pos + strlen($replace);
425 * @brief Executes a prepared statement that returns data
426 * @usage Example: $r = p("SELECT * FROM `item` WHERE `guid` = ?", $guid);
428 * Please only use it with complicated queries.
429 * For all regular queries please use DBA::select or DBA::exists
431 * @param string $sql SQL statement
433 * @return bool|object statement object or result object
436 public function p($sql)
439 $stamp1 = microtime(true);
441 $params = DBA::getParam(func_get_args());
443 // Renumber the array keys to be sure that they fit
446 foreach ($params AS $param) {
447 // Avoid problems with some MySQL servers and boolean values. See issue #3645
448 if (is_bool($param)) {
449 $param = (int)$param;
451 $args[++$i] = $param;
454 if (!$this->connected) {
458 if ((substr_count($sql, '?') != count($args)) && (count($args) > 0)) {
459 // Question: Should we continue or stop the query here?
460 $this->logger->warning('Query parameters mismatch.', ['query' => $sql, 'args' => $args, 'callstack' => System::callstack()]);
463 $sql = DBA::cleanQuery($sql);
464 $sql = $this->anyValueFallback($sql);
468 if ($this->configCache->get('system', 'db_callstack') !== null) {
469 $sql = "/*" . System::callstack() . " */ " . $sql;
474 $this->affected_rows = 0;
476 // We have to make some things different if this function is called from "e"
477 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
479 if (isset($trace[1])) {
480 $called_from = $trace[1];
482 // We use just something that is defined to avoid warnings
483 $called_from = $trace[0];
485 // We are having an own error logging in the function "e"
486 $called_from_e = ($called_from['function'] == 'e');
488 if (!isset($this->connection)) {
489 throw new InternalServerErrorException('The Connection is empty, although connected is set true.');
492 switch ($this->driver) {
494 // If there are no arguments we use "query"
495 if (count($args) == 0) {
496 if (!$retval = $this->connection->query($sql)) {
497 $errorInfo = $this->connection->errorInfo();
498 $this->error = $errorInfo[2];
499 $this->errorno = $errorInfo[1];
503 $this->affected_rows = $retval->rowCount();
507 /** @var $stmt mysqli_stmt|PDOStatement */
508 if (!$stmt = $this->connection->prepare($sql)) {
509 $errorInfo = $this->connection->errorInfo();
510 $this->error = $errorInfo[2];
511 $this->errorno = $errorInfo[1];
516 foreach ($args AS $param => $value) {
517 if (is_int($args[$param])) {
518 $data_type = PDO::PARAM_INT;
520 $data_type = PDO::PARAM_STR;
522 $stmt->bindParam($param, $args[$param], $data_type);
525 if (!$stmt->execute()) {
526 $errorInfo = $stmt->errorInfo();
527 $this->error = $errorInfo[2];
528 $this->errorno = $errorInfo[1];
532 $this->affected_rows = $retval->rowCount();
536 // There are SQL statements that cannot be executed with a prepared statement
537 $parts = explode(' ', $orig_sql);
538 $command = strtolower($parts[0]);
539 $can_be_prepared = in_array($command, ['select', 'update', 'insert', 'delete']);
541 // The fallback routine is called as well when there are no arguments
542 if (!$can_be_prepared || (count($args) == 0)) {
543 $retval = $this->connection->query($this->replaceParameters($sql, $args));
544 if ($this->connection->errno) {
545 $this->error = $this->connection->error;
546 $this->errorno = $this->connection->errno;
549 if (isset($retval->num_rows)) {
550 $this->affected_rows = $retval->num_rows;
552 $this->affected_rows = $this->connection->affected_rows;
558 $stmt = $this->connection->stmt_init();
560 if (!$stmt->prepare($sql)) {
561 $this->error = $stmt->error;
562 $this->errorno = $stmt->errno;
569 foreach ($args AS $param => $value) {
570 if (is_int($args[$param])) {
572 } elseif (is_float($args[$param])) {
574 } elseif (is_string($args[$param])) {
579 $values[] = &$args[$param];
582 if (count($values) > 0) {
583 array_unshift($values, $param_types);
584 call_user_func_array([$stmt, 'bind_param'], $values);
587 if (!$stmt->execute()) {
588 $this->error = $this->connection->error;
589 $this->errorno = $this->connection->errno;
592 $stmt->store_result();
594 $this->affected_rows = $retval->affected_rows;
599 // We are having an own error logging in the function "e"
600 if (($this->errorno != 0) && !$called_from_e) {
601 // We have to preserve the error code, somewhere in the logging it get lost
602 $error = $this->error;
603 $errorno = $this->errorno;
605 $this->logger->error('DB Error', [
606 'code' => $this->errorno,
607 'error' => $this->error,
608 'callstack' => System::callstack(8),
609 'params' => $this->replaceParameters($sql, $args),
612 // On a lost connection we try to reconnect - but only once.
613 if ($errorno == 2006) {
614 if ($this->in_retrial || !$this->reconnect()) {
615 // It doesn't make sense to continue when the database connection was lost
616 if ($this->in_retrial) {
617 $this->logger->notice('Giving up retrial because of database error', [
618 'code' => $this->errorno,
619 'error' => $this->error,
622 $this->logger->notice('Couldn\'t reconnect after database error', [
623 'code' => $this->errorno,
624 'error' => $this->error,
630 $this->logger->notice('Reconnected after database error', [
631 'code' => $this->errorno,
632 'error' => $this->error,
634 $this->in_retrial = true;
635 $ret = $this->p($sql, $args);
636 $this->in_retrial = false;
641 $this->error = $error;
642 $this->errorno = $errorno;
645 $this->profiler->saveTimestamp($stamp1, 'database', System::callstack());
647 if ($this->configCache->get('system', 'db_log')) {
648 $stamp2 = microtime(true);
649 $duration = (float)($stamp2 - $stamp1);
651 if (($duration > $this->configCache->get('system', 'db_loglimit'))) {
652 $duration = round($duration, 3);
653 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
655 @file_put_contents($this->configCache->get('system', 'db_log'), DateTimeFormat::utcNow() . "\t" . $duration . "\t" .
656 basename($backtrace[1]["file"]) . "\t" .
657 $backtrace[1]["line"] . "\t" . $backtrace[2]["function"] . "\t" .
658 substr($this->replaceParameters($sql, $args), 0, 2000) . "\n", FILE_APPEND);
665 * @brief Executes a prepared statement like UPDATE or INSERT that doesn't return data
667 * Please use DBA::delete, DBA::insert, DBA::update, ... instead
669 * @param string $sql SQL statement
671 * @return boolean Was the query successfull? False is returned only if an error occurred
674 public function e($sql)
677 $stamp = microtime(true);
679 $params = DBA::getParam(func_get_args());
681 // In a case of a deadlock we are repeating the query 20 times
685 $stmt = $this->p($sql, $params);
687 if (is_bool($stmt)) {
689 } elseif (is_object($stmt)) {
697 } while (($this->errorno == 1213) && (--$timeout > 0));
699 if ($this->errorno != 0) {
700 // We have to preserve the error code, somewhere in the logging it get lost
701 $error = $this->error;
702 $errorno = $this->errorno;
704 $this->logger->error('DB Error', [
705 'code' => $this->errorno,
706 'error' => $this->error,
707 'callstack' => System::callstack(8),
708 'params' => $this->replaceParameters($sql, $params),
711 // On a lost connection we simply quit.
712 // A reconnect like in $this->p could be dangerous with modifications
713 if ($errorno == 2006) {
714 $this->logger->notice('Giving up because of database error', [
715 'code' => $this->errorno,
716 'error' => $this->error,
721 $this->error = $error;
722 $this->errorno = $errorno;
725 $this->profiler->saveTimestamp($stamp, "database_write", System::callstack());
731 * @brief Check if data exists
733 * @param string|array $table Table name or array [schema => table]
734 * @param array $condition array of fields for condition
736 * @return boolean Are there rows for that condition?
739 public function exists($table, $condition)
747 if (empty($condition)) {
748 return DBStructure::existsTable($table);
752 $first_key = key($condition);
753 if (!is_int($first_key)) {
754 $fields = [$first_key];
757 $stmt = $this->select($table, $fields, $condition, ['limit' => 1]);
759 if (is_bool($stmt)) {
762 $retval = ($this->numRows($stmt) > 0);
771 * Fetches the first row
773 * Please use DBA::selectFirst or DBA::exists whenever this is possible.
775 * @brief Fetches the first row
777 * @param string $sql SQL statement
779 * @return array first row of query
782 public function fetchFirst($sql)
784 $params = DBA::getParam(func_get_args());
786 $stmt = $this->p($sql, $params);
788 if (is_bool($stmt)) {
791 $retval = $this->fetch($stmt);
800 * @brief Returns the number of affected rows of the last statement
802 * @return int Number of rows
804 public function affectedRows()
806 return $this->affected_rows;
810 * @brief Returns the number of columns of a statement
812 * @param object Statement object
814 * @return int Number of columns
816 public function columnCount($stmt)
818 if (!is_object($stmt)) {
821 switch ($this->driver) {
823 return $stmt->columnCount();
825 return $stmt->field_count;
831 * @brief Returns the number of rows of a statement
833 * @param PDOStatement|mysqli_result|mysqli_stmt Statement object
835 * @return int Number of rows
837 public function numRows($stmt)
839 if (!is_object($stmt)) {
842 switch ($this->driver) {
844 return $stmt->rowCount();
846 return $stmt->num_rows;
852 * @brief Fetch a single row
854 * @param mixed $stmt statement object
856 * @return array current row
858 public function fetch($stmt)
861 $stamp1 = microtime(true);
865 if (!is_object($stmt)) {
869 switch ($this->driver) {
871 $columns = $stmt->fetch(PDO::FETCH_ASSOC);
874 if (get_class($stmt) == 'mysqli_result') {
875 $columns = $stmt->fetch_assoc();
879 // This code works, but is slow
881 // Bind the result to a result array
885 for ($x = 0; $x < $stmt->field_count; $x++) {
886 $cols[] = &$cols_num[$x];
889 call_user_func_array([$stmt, 'bind_result'], $cols);
891 if (!$stmt->fetch()) {
896 // We need to get the field names for the array keys
897 // It seems that there is no better way to do this.
898 $result = $stmt->result_metadata();
899 $fields = $result->fetch_fields();
901 foreach ($cols_num AS $param => $col) {
902 $columns[$fields[$param]->name] = $col;
906 $this->profiler->saveTimestamp($stamp1, 'database', System::callstack());
912 * @brief Insert a row into a table
914 * @param string|array $table Table name or array [schema => table]
915 * @param array $param parameter array
916 * @param bool $on_duplicate_update Do an update on a duplicate entry
918 * @return boolean was the insert successful?
921 public function insert($table, $param, $on_duplicate_update = false)
923 if (empty($table) || empty($param)) {
924 $this->logger->info('Table and fields have to be set');
928 $table_string = DBA::buildTableString($table);
930 $fields_string = implode(', ', array_map([DBA::class, 'quoteIdentifier'], array_keys($param)));
932 $values_string = substr(str_repeat("?, ", count($param)), 0, -2);
934 $sql = "INSERT INTO " . $table_string . " (" . $fields_string . ") VALUES (" . $values_string . ")";
936 if ($on_duplicate_update) {
937 $fields_string = implode(' = ?, ', array_map([DBA::class, 'quoteIdentifier'], array_keys($param)));
939 $sql .= " ON DUPLICATE KEY UPDATE " . $fields_string . " = ?";
941 $values = array_values($param);
942 $param = array_merge_recursive($values, $values);
945 return $this->e($sql, $param);
949 * @brief Fetch the id of the last insert command
951 * @return integer Last inserted id
953 public function lastInsertId()
955 switch ($this->driver) {
957 $id = $this->connection->lastInsertId();
960 $id = $this->connection->insert_id;
967 * @brief Locks a table for exclusive write access
969 * This function can be extended in the future to accept a table array as well.
971 * @param string|array $table Table name or array [schema => table]
973 * @return boolean was the lock successful?
976 public function lock($table)
978 // See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
979 if ($this->driver == 'pdo') {
980 $this->e("SET autocommit=0");
981 $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
983 $this->connection->autocommit(false);
986 $success = $this->e("LOCK TABLES " . DBA::buildTableString($table) . " WRITE");
988 if ($this->driver == 'pdo') {
989 $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
993 if ($this->driver == 'pdo') {
994 $this->e("SET autocommit=1");
996 $this->connection->autocommit(true);
999 $this->in_transaction = true;
1005 * @brief Unlocks all locked tables
1007 * @return boolean was the unlock successful?
1008 * @throws \Exception
1010 public function unlock()
1012 // See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
1013 $this->performCommit();
1015 if ($this->driver == 'pdo') {
1016 $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
1019 $success = $this->e("UNLOCK TABLES");
1021 if ($this->driver == 'pdo') {
1022 $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
1023 $this->e("SET autocommit=1");
1025 $this->connection->autocommit(true);
1028 $this->in_transaction = false;
1033 * @brief Starts a transaction
1035 * @return boolean Was the command executed successfully?
1037 public function transaction()
1039 if (!$this->performCommit()) {
1043 switch ($this->driver) {
1045 if (!$this->connection->inTransaction() && !$this->connection->beginTransaction()) {
1051 if (!$this->connection->begin_transaction()) {
1057 $this->in_transaction = true;
1061 protected function performCommit()
1063 switch ($this->driver) {
1065 if (!$this->connection->inTransaction()) {
1069 return $this->connection->commit();
1072 return $this->connection->commit();
1079 * @brief Does a commit
1081 * @return boolean Was the command executed successfully?
1083 public function commit()
1085 if (!$this->performCommit()) {
1088 $this->in_transaction = false;
1093 * @brief Does a rollback
1095 * @return boolean Was the command executed successfully?
1097 public function rollback()
1101 switch ($this->driver) {
1103 if (!$this->connection->inTransaction()) {
1107 $ret = $this->connection->rollBack();
1111 $ret = $this->connection->rollback();
1114 $this->in_transaction = false;
1119 * @brief Build the array with the table relations
1121 * The array is build from the database definitions in DBStructure.php
1123 * This process must only be started once, since the value is cached.
1125 private function buildRelationData()
1127 $definition = DBStructure::definition($this->configCache->get('system', 'basepath'));
1129 foreach ($definition AS $table => $structure) {
1130 foreach ($structure['fields'] AS $field => $field_struct) {
1131 if (isset($field_struct['relation'])) {
1132 foreach ($field_struct['relation'] AS $rel_table => $rel_field) {
1133 $this->relation[$rel_table][$rel_field][$table][] = $field;
1141 * @brief Delete a row from a table
1143 * Note: this methods does NOT accept schema => table arrays because of the complex relation stuff.
1145 * @param string $table Table name
1146 * @param array $conditions Field condition(s)
1147 * @param array $options
1148 * - cascade: If true we delete records in other tables that depend on the one we're deleting through
1149 * relations (default: true)
1150 * @param array $callstack Internal use: prevent endless loops
1152 * @return boolean was the delete successful?
1153 * @throws \Exception
1155 public function delete($table, array $conditions, array $options = [], array &$callstack = [])
1157 if (empty($table) || empty($conditions)) {
1158 $this->logger->info('Table and conditions have to be set');
1164 // Create a key for the loop prevention
1165 $key = $table . ':' . json_encode($conditions);
1167 // We quit when this key already exists in the callstack.
1168 if (isset($callstack[$key])) {
1172 $callstack[$key] = true;
1174 $commands[$key] = ['table' => $table, 'conditions' => $conditions];
1176 // Don't use "defaults" here, since it would set "false" to "true"
1177 if (isset($options['cascade'])) {
1178 $cascade = $options['cascade'];
1183 // To speed up the whole process we cache the table relations
1184 if ($cascade && count($this->relation) == 0) {
1185 $this->buildRelationData();
1188 // Is there a relation entry for the table?
1189 if ($cascade && isset($this->relation[$table])) {
1190 // We only allow a simple "one field" relation.
1191 $field = array_keys($this->relation[$table])[0];
1192 $rel_def = array_values($this->relation[$table])[0];
1194 // Create a key for preventing double queries
1195 $qkey = $field . '-' . $table . ':' . json_encode($conditions);
1197 // When the search field is the relation field, we don't need to fetch the rows
1198 // This is useful when the leading record is already deleted in the frontend but the rest is done in the backend
1199 if ((count($conditions) == 1) && ($field == array_keys($conditions)[0])) {
1200 foreach ($rel_def AS $rel_table => $rel_fields) {
1201 foreach ($rel_fields AS $rel_field) {
1202 $this->delete($rel_table, [$rel_field => array_values($conditions)[0]], $options, $callstack);
1205 // We quit when this key already exists in the callstack.
1206 } elseif (!isset($callstack[$qkey])) {
1207 $callstack[$qkey] = true;
1209 // Fetch all rows that are to be deleted
1210 $data = $this->select($table, [$field], $conditions);
1212 while ($row = $this->fetch($data)) {
1213 $this->delete($table, [$field => $row[$field]], $options, $callstack);
1216 $this->close($data);
1218 // Since we had split the delete command we don't need the original command anymore
1219 unset($commands[$key]);
1223 // Now we finalize the process
1224 $do_transaction = !$this->in_transaction;
1226 if ($do_transaction) {
1227 $this->transaction();
1233 foreach ($commands AS $command) {
1234 $conditions = $command['conditions'];
1236 $first_key = key($conditions);
1238 $condition_string = DBA::buildCondition($conditions);
1240 if ((count($command['conditions']) > 1) || is_int($first_key)) {
1241 $sql = "DELETE FROM " . DBA::quoteIdentifier($command['table']) . " " . $condition_string;
1242 $this->logger->debug($this->replaceParameters($sql, $conditions));
1244 if (!$this->e($sql, $conditions)) {
1245 if ($do_transaction) {
1251 $key_table = $command['table'];
1252 $key_condition = array_keys($command['conditions'])[0];
1253 $value = array_values($command['conditions'])[0];
1255 // Split the SQL queries in chunks of 100 values
1256 // We do the $i stuff here to make the code better readable
1257 $i = isset($counter[$key_table][$key_condition]) ? $counter[$key_table][$key_condition] : 0;
1258 if (isset($compacted[$key_table][$key_condition][$i]) && count($compacted[$key_table][$key_condition][$i]) > 100) {
1262 $compacted[$key_table][$key_condition][$i][$value] = $value;
1263 $counter[$key_table][$key_condition] = $i;
1266 foreach ($compacted AS $table => $values) {
1267 foreach ($values AS $field => $field_value_list) {
1268 foreach ($field_value_list AS $field_values) {
1269 $sql = "DELETE FROM " . DBA::quoteIdentifier($table) . " WHERE " . DBA::quoteIdentifier($field) . " IN (" .
1270 substr(str_repeat("?, ", count($field_values)), 0, -2) . ");";
1272 $this->logger->debug($this->replaceParameters($sql, $field_values));
1274 if (!$this->e($sql, $field_values)) {
1275 if ($do_transaction) {
1283 if ($do_transaction) {
1290 * @brief Updates rows
1292 * Updates rows in the database. When $old_fields is set to an array,
1293 * the system will only do an update if the fields in that array changed.
1296 * Only the values in $old_fields are compared.
1297 * This is an intentional behaviour.
1300 * We include the timestamp field in $fields but not in $old_fields.
1301 * Then the row will only get the new timestamp when the other fields had changed.
1303 * When $old_fields is set to a boolean value the system will do this compare itself.
1304 * When $old_fields is set to "true" the system will do an insert if the row doesn't exists.
1307 * Only set $old_fields to a boolean value when you are sure that you will update a single row.
1308 * When you set $old_fields to "true" then $fields must contain all relevant fields!
1310 * @param string|array $table Table name or array [schema => table]
1311 * @param array $fields contains the fields that are updated
1312 * @param array $condition condition array with the key values
1313 * @param array|boolean $old_fields array with the old field values that are about to be replaced (true = update on duplicate)
1315 * @return boolean was the update successfull?
1316 * @throws \Exception
1318 public function update($table, $fields, $condition, $old_fields = [])
1320 if (empty($table) || empty($fields) || empty($condition)) {
1321 $this->logger->info('Table, fields and condition have to be set');
1325 $table_string = DBA::buildTableString($table);
1327 $condition_string = DBA::buildCondition($condition);
1329 if (is_bool($old_fields)) {
1330 $do_insert = $old_fields;
1332 $old_fields = $this->selectFirst($table, [], $condition);
1334 if (is_bool($old_fields)) {
1336 $values = array_merge($condition, $fields);
1337 return $this->insert($table, $values, $do_insert);
1343 $do_update = (count($old_fields) == 0);
1345 foreach ($old_fields AS $fieldname => $content) {
1346 if (isset($fields[$fieldname])) {
1347 if (($fields[$fieldname] == $content) && !is_null($content)) {
1348 unset($fields[$fieldname]);
1355 if (!$do_update || (count($fields) == 0)) {
1359 $sql = "UPDATE " . $table_string . " SET "
1360 . implode(" = ?, ", array_map([DBA::class, 'quoteIdentifier'], array_keys($fields))) . " = ?"
1361 . $condition_string;
1363 $params1 = array_values($fields);
1364 $params2 = array_values($condition);
1365 $params = array_merge_recursive($params1, $params2);
1367 return $this->e($sql, $params);
1371 * Retrieve a single record from a table and returns it in an associative array
1373 * @brief Retrieve a single record from a table
1375 * @param string $table
1376 * @param array $fields
1377 * @param array $condition
1378 * @param array $params
1380 * @return bool|array
1381 * @throws \Exception
1382 * @see $this->select
1384 public function selectFirst($table, array $fields = [], array $condition = [], $params = [])
1386 $params['limit'] = 1;
1387 $result = $this->select($table, $fields, $condition, $params);
1389 if (is_bool($result)) {
1392 $row = $this->fetch($result);
1393 $this->close($result);
1399 * @brief Select rows from a table and fills an array with the data
1401 * @param string|array $table Table name or array [schema => table]
1402 * @param array $fields Array of selected fields, empty for all
1403 * @param array $condition Array of fields for condition
1404 * @param array $params Array of several parameters
1406 * @return array Data array
1407 * @throws \Exception
1410 public function selectToArray(string $table, array $fields = [], array $condition = [], array $params = [])
1412 return $this->toArray($this->select($table, $fields, $condition, $params));
1416 * @brief Select rows from a table
1418 * @param string|array $table Table name or array [schema => table]
1419 * @param array $fields Array of selected fields, empty for all
1420 * @param array $condition Array of fields for condition
1421 * @param array $params Array of several parameters
1423 * @return boolean|object
1427 * $fields = array("id", "uri", "uid", "network");
1429 * $condition = array("uid" => 1, "network" => 'dspr');
1431 * $condition = array("`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr');
1433 * $params = array("order" => array("id", "received" => true), "limit" => 10);
1435 * $data = DBA::select($table, $fields, $condition, $params);
1436 * @throws \Exception
1438 public function select($table, array $fields = [], array $condition = [], array $params = [])
1440 if (empty($table)) {
1444 if (count($fields) > 0) {
1445 $select_string = implode(', ', array_map([DBA::class, 'quoteIdentifier'], $fields));
1447 $select_string = '*';
1450 $table_string = DBA::buildTableString($table);
1452 $condition_string = DBA::buildCondition($condition);
1454 $param_string = DBA::buildParameter($params);
1456 $sql = "SELECT " . $select_string . " FROM " . $table_string . $condition_string . $param_string;
1458 $result = $this->p($sql, $condition);
1464 * @brief Counts the rows from a table satisfying the provided condition
1466 * @param string|array $table Table name or array [schema => table]
1467 * @param array $condition Array of fields for condition
1468 * @param array $params Array of several parameters
1475 * $condition = ["uid" => 1, "network" => 'dspr'];
1477 * $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr'];
1479 * $count = DBA::count($table, $condition);
1480 * @throws \Exception
1482 public function count($table, array $condition = [], array $params = [])
1484 if (empty($table)) {
1488 $table_string = DBA::buildTableString($table);
1490 $condition_string = DBA::buildCondition($condition);
1492 if (empty($params['expression'])) {
1494 } elseif (!empty($params['distinct'])) {
1495 $expression = "DISTINCT " . DBA::quoteIdentifier($params['expression']);
1497 $expression = DBA::quoteIdentifier($params['expression']);
1500 $sql = "SELECT COUNT(" . $expression . ") AS `count` FROM " . $table_string . $condition_string;
1502 $row = $this->fetchFirst($sql, $condition);
1504 return $row['count'];
1508 * @brief Fills an array with data from a query
1510 * @param object $stmt statement object
1511 * @param bool $do_close
1513 * @return array Data array
1515 public function toArray($stmt, $do_close = true)
1517 if (is_bool($stmt)) {
1522 while ($row = $this->fetch($stmt)) {
1527 $this->close($stmt);
1534 * @brief Returns the error number of the last query
1536 * @return string Error number (0 if no error)
1538 public function errorNo()
1540 return $this->errorno;
1544 * @brief Returns the error message of the last query
1546 * @return string Error message ('' if no error)
1548 public function errorMessage()
1550 return $this->error;
1554 * @brief Closes the current statement
1556 * @param object $stmt statement object
1558 * @return boolean was the close successful?
1560 public function close($stmt)
1563 $stamp1 = microtime(true);
1565 if (!is_object($stmt)) {
1569 switch ($this->driver) {
1571 $ret = $stmt->closeCursor();
1574 // MySQLi offers both a mysqli_stmt and a mysqli_result class.
1575 // We should be careful not to assume the object type of $stmt
1576 // because DBA::p() has been able to return both types.
1577 if ($stmt instanceof mysqli_stmt) {
1578 $stmt->free_result();
1579 $ret = $stmt->close();
1580 } elseif ($stmt instanceof mysqli_result) {
1589 $this->profiler->saveTimestamp($stamp1, 'database', System::callstack());
1595 * @brief Return a list of database processes
1598 * 'list' => List of processes, separated in their different states
1599 * 'amount' => Number of concurrent database processes
1600 * @throws \Exception
1602 public function processlist()
1604 $ret = $this->p("SHOW PROCESSLIST");
1605 $data = $this->toArray($ret);
1609 foreach ($data as $process) {
1610 $state = trim($process["State"]);
1612 // Filter out all non blocking processes
1613 if (!in_array($state, ["", "init", "statistics", "updating"])) {
1620 foreach ($states as $state => $usage) {
1621 if ($statelist != "") {
1624 $statelist .= $state . ": " . $usage;
1626 return (["list" => $statelist, "amount" => $processes]);
1630 * Checks if $array is a filled array with at least one entry.
1632 * @param mixed $array A filled array with at least one entry
1634 * @return boolean Whether $array is a filled array or an object with rows
1636 public function isResult($array)
1638 // It could be a return value from an update statement
1639 if (is_bool($array)) {
1643 if (is_object($array)) {
1644 return $this->numRows($array) > 0;
1647 return (is_array($array) && (count($array) > 0));
1651 * @brief Callback function for "esc_array"
1653 * @param mixed $value Array value
1654 * @param string $key Array key
1655 * @param boolean $add_quotation add quotation marks for string values
1659 private function escapeArrayCallback(&$value, $key, $add_quotation)
1661 if (!$add_quotation) {
1662 if (is_bool($value)) {
1663 $value = ($value ? '1' : '0');
1665 $value = $this->escape($value);
1670 if (is_bool($value)) {
1671 $value = ($value ? 'true' : 'false');
1672 } elseif (is_float($value) || is_integer($value)) {
1673 $value = (string)$value;
1675 $value = "'" . $this->escape($value) . "'";
1680 * @brief Escapes a whole array
1682 * @param mixed $arr Array with values to be escaped
1683 * @param boolean $add_quotation add quotation marks for string values
1687 public function escapeArray(&$arr, $add_quotation = false)
1689 array_walk($arr, [$this, 'escapeArrayCallback'], $add_quotation);