3 namespace Friendica\Database;
5 use Friendica\Core\Config\Cache\ConfigCache;
6 use Friendica\Core\System;
7 use Friendica\Util\DateTimeFormat;
8 use Friendica\Util\Profiler;
15 use Psr\Log\LoggerInterface;
18 * @class MySQL database class
20 * This class is for the low level database stuff that does driver specific things.
24 private $connected = false;
35 * @var LoggerInterface
38 private $server_info = '';
39 /** @var PDO|mysqli */
42 private $error = false;
44 private $affected_rows = 0;
45 private $in_transaction = false;
46 private $in_retrial = false;
47 private $relation = [];
49 public function __construct(ConfigCache $configCache, Profiler $profiler, LoggerInterface $logger, array $server)
51 // We are storing these values for being able to perform a reconnect
52 $this->configCache = $configCache;
53 $this->profiler = $profiler;
54 $this->logger = $logger;
56 $this->readServerVariables($server);
62 private function readServerVariables(array $server)
64 // Use environment variables for mysql if they are set beforehand
65 if (!empty($server['MYSQL_HOST'])
66 && !empty($server['MYSQL_USERNAME'] || !empty($server['MYSQL_USER']))
67 && $server['MYSQL_PASSWORD'] !== false
68 && !empty($server['MYSQL_DATABASE']))
70 $db_host = $server['MYSQL_HOST'];
71 if (!empty($server['MYSQL_PORT'])) {
72 $db_host .= ':' . $server['MYSQL_PORT'];
74 $this->configCache->set('database', 'hostname', $db_host);
76 if (!empty($server['MYSQL_USERNAME'])) {
77 $this->configCache->set('database', 'username', $server['MYSQL_USERNAME']);
79 $this->configCache->set('database', 'username', $server['MYSQL_USER']);
81 $this->configCache->set('database', 'password', (string) $server['MYSQL_PASSWORD']);
82 $this->configCache->set('database', 'database', $server['MYSQL_DATABASE']);
86 public function connect()
88 if (!is_null($this->connection) && $this->connected()) {
93 $serveraddr = trim($this->configCache->get('database', 'hostname'));
94 $serverdata = explode(':', $serveraddr);
95 $server = $serverdata[0];
96 if (count($serverdata) > 1) {
97 $port = trim($serverdata[1]);
99 $server = trim($server);
100 $user = trim($this->configCache->get('database', 'username'));
101 $pass = trim($this->configCache->get('database', 'password'));
102 $db = trim($this->configCache->get('database', 'database'));
103 $charset = trim($this->configCache->get('database', 'charset'));
105 if (!(strlen($server) && strlen($user))) {
109 if (class_exists('\PDO') && in_array('mysql', PDO::getAvailableDrivers())) {
110 $this->driver = 'pdo';
111 $connect = "mysql:host=" . $server . ";dbname=" . $db;
114 $connect .= ";port=" . $port;
118 $connect .= ";charset=" . $charset;
122 $this->connection = @new PDO($connect, $user, $pass);
123 $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
124 $this->connected = true;
125 } catch (PDOException $e) {
126 /// @TODO At least log exception, don't ignore it!
130 if (!$this->connected && class_exists('\mysqli')) {
131 $this->driver = 'mysqli';
134 $this->connection = @new mysqli($server, $user, $pass, $db, $port);
136 $this->connection = @new mysqli($server, $user, $pass, $db);
139 if (!mysqli_connect_errno()) {
140 $this->connected = true;
143 $this->connection->set_charset($charset);
148 // No suitable SQL driver was found.
149 if (!$this->connected) {
150 $this->driver = null;
151 $this->connection = null;
154 return $this->connected;
158 * Sets the logger for DBA
160 * @note this is necessary because if we want to load the logger configuration
161 * from the DB, but there's an error, we would print out an exception.
162 * So the logger gets updated after the logger configuration can be retrieved
165 * @param LoggerInterface $logger
167 public function setLogger(LoggerInterface $logger)
169 $this->logger = $logger;
173 * Sets the profiler for DBA
175 * @param Profiler $profiler
177 public function setProfiler(Profiler $profiler)
179 $this->profiler = $profiler;
182 * Disconnects the current database connection
184 public function disconnect()
186 if (is_null($this->connection)) {
190 switch ($this->driver) {
192 $this->connection = null;
195 $this->connection->close();
196 $this->connection = null;
202 * Perform a reconnect of an existing database connection
204 public function reconnect()
207 return $this->connect();
211 * Return the database object.
215 public function getConnection()
217 return $this->connection;
221 * @brief Returns the MySQL server version string
223 * This function discriminate between the deprecated mysql API and the current
224 * object-oriented mysqli API. Example of returned string: 5.5.46-0+deb8u1
228 public function serverInfo()
230 if ($this->server_info == '') {
231 switch ($this->driver) {
233 $this->server_info = $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION);
236 $this->server_info = $this->connection->server_info;
240 return $this->server_info;
244 * @brief Returns the selected database name
249 public function databaseName()
251 $ret = $this->p("SELECT DATABASE() AS `db`");
252 $data = $this->toArray($ret);
253 return $data[0]['db'];
257 * @brief Analyze a database query and log this if some conditions are met.
259 * @param string $query The database query that will be analyzed
263 private function logIndex($query)
266 if (!$this->configCache->get('system', 'db_log_index')) {
270 // Don't explain an explain statement
271 if (strtolower(substr($query, 0, 7)) == "explain") {
275 // Only do the explain on "select", "update" and "delete"
276 if (!in_array(strtolower(substr($query, 0, 6)), ["select", "update", "delete"])) {
280 $r = $this->p("EXPLAIN " . $query);
281 if (!$this->isResult($r)) {
285 $watchlist = explode(',', $this->configCache->get('system', 'db_log_index_watch'));
286 $blacklist = explode(',', $this->configCache->get('system', 'db_log_index_blacklist'));
288 while ($row = $this->fetch($r)) {
289 if ((intval($this->configCache->get('system', 'db_loglimit_index')) > 0)) {
290 $log = (in_array($row['key'], $watchlist) &&
291 ($row['rows'] >= intval($this->configCache->get('system', 'db_loglimit_index'))));
296 if ((intval($this->configCache->get('system', 'db_loglimit_index_high')) > 0) && ($row['rows'] >= intval($this->configCache->get('system', 'db_loglimit_index_high')))) {
300 if (in_array($row['key'], $blacklist) || ($row['key'] == "")) {
305 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
306 @file_put_contents($this->configCache->get('system', 'db_log_index'), DateTimeFormat::utcNow() . "\t" .
307 $row['key'] . "\t" . $row['rows'] . "\t" . $row['Extra'] . "\t" .
308 basename($backtrace[1]["file"]) . "\t" .
309 $backtrace[1]["line"] . "\t" . $backtrace[2]["function"] . "\t" .
310 substr($query, 0, 2000) . "\n", FILE_APPEND);
316 * Removes every not whitelisted character from the identifier string
318 * @param string $identifier
320 * @return string sanitized identifier
323 private function sanitizeIdentifier($identifier)
325 return preg_replace('/[^A-Za-z0-9_\-]+/', '', $identifier);
328 public function escape($str)
330 if ($this->connected) {
331 switch ($this->driver) {
333 return substr(@$this->connection->quote($str, PDO::PARAM_STR), 1, -1);
336 return @$this->connection->real_escape_string($str);
339 return str_replace("'", "\\'", $str);
343 public function isConnected()
345 return $this->connected;
348 public function connected()
352 if (is_null($this->connection)) {
356 switch ($this->driver) {
358 $r = $this->p("SELECT 1");
359 if ($this->isResult($r)) {
360 $row = $this->toArray($r);
361 $connected = ($row[0]['1'] == '1');
365 $connected = $this->connection->ping();
372 * @brief Replaces ANY_VALUE() function by MIN() function,
373 * if the database server does not support ANY_VALUE().
375 * Considerations for Standard SQL, or MySQL with ONLY_FULL_GROUP_BY (default since 5.7.5).
376 * ANY_VALUE() is available from MySQL 5.7.5 https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html
377 * A standard fall-back is to use MIN().
379 * @param string $sql An SQL string without the values
381 * @return string The input SQL string modified if necessary.
383 public function anyValueFallback($sql)
385 $server_info = $this->serverInfo();
386 if (version_compare($server_info, '5.7.5', '<') ||
387 (stripos($server_info, 'MariaDB') !== false)) {
388 $sql = str_ireplace('ANY_VALUE(', 'MIN(', $sql);
394 * @brief Replaces the ? placeholders with the parameters in the $args array
396 * @param string $sql SQL query
397 * @param array $args The parameters that are to replace the ? placeholders
399 * @return string The replaced SQL query
401 private function replaceParameters($sql, $args)
404 foreach ($args AS $param => $value) {
405 if (is_int($args[$param]) || is_float($args[$param])) {
406 $replace = intval($args[$param]);
408 $replace = "'" . $this->escape($args[$param]) . "'";
411 $pos = strpos($sql, '?', $offset);
412 if ($pos !== false) {
413 $sql = substr_replace($sql, $replace, $pos, 1);
415 $offset = $pos + strlen($replace);
421 * @brief Executes a prepared statement that returns data
422 * @usage Example: $r = p("SELECT * FROM `item` WHERE `guid` = ?", $guid);
424 * Please only use it with complicated queries.
425 * For all regular queries please use DBA::select or DBA::exists
427 * @param string $sql SQL statement
429 * @return bool|object statement object or result object
432 public function p($sql)
435 $stamp1 = microtime(true);
437 $params = DBA::getParam(func_get_args());
439 // Renumber the array keys to be sure that they fit
442 foreach ($params AS $param) {
443 // Avoid problems with some MySQL servers and boolean values. See issue #3645
444 if (is_bool($param)) {
445 $param = (int)$param;
447 $args[++$i] = $param;
450 if (!$this->connected) {
454 if ((substr_count($sql, '?') != count($args)) && (count($args) > 0)) {
455 // Question: Should we continue or stop the query here?
456 $this->logger->warning('Query parameters mismatch.', ['query' => $sql, 'args' => $args, 'callstack' => System::callstack()]);
459 $sql = DBA::cleanQuery($sql);
460 $sql = $this->anyValueFallback($sql);
464 if ($this->configCache->get('system', 'db_callstack') !== null) {
465 $sql = "/*" . System::callstack() . " */ " . $sql;
470 $this->affected_rows = 0;
472 // We have to make some things different if this function is called from "e"
473 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
475 if (isset($trace[1])) {
476 $called_from = $trace[1];
478 // We use just something that is defined to avoid warnings
479 $called_from = $trace[0];
481 // We are having an own error logging in the function "e"
482 $called_from_e = ($called_from['function'] == 'e');
484 switch ($this->driver) {
486 // If there are no arguments we use "query"
487 if (count($args) == 0) {
488 if (!$retval = $this->connection->query($sql)) {
489 $errorInfo = $this->connection->errorInfo();
490 $this->error = $errorInfo[2];
491 $this->errorno = $errorInfo[1];
495 $this->affected_rows = $retval->rowCount();
499 /** @var $stmt mysqli_stmt|PDOStatement */
500 if (!$stmt = $this->connection->prepare($sql)) {
501 $errorInfo = $this->connection->errorInfo();
502 $this->error = $errorInfo[2];
503 $this->errorno = $errorInfo[1];
508 foreach ($args AS $param => $value) {
509 if (is_int($args[$param])) {
510 $data_type = PDO::PARAM_INT;
512 $data_type = PDO::PARAM_STR;
514 $stmt->bindParam($param, $args[$param], $data_type);
517 if (!$stmt->execute()) {
518 $errorInfo = $stmt->errorInfo();
519 $this->error = $errorInfo[2];
520 $this->errorno = $errorInfo[1];
524 $this->affected_rows = $retval->rowCount();
528 // There are SQL statements that cannot be executed with a prepared statement
529 $parts = explode(' ', $orig_sql);
530 $command = strtolower($parts[0]);
531 $can_be_prepared = in_array($command, ['select', 'update', 'insert', 'delete']);
533 // The fallback routine is called as well when there are no arguments
534 if (!$can_be_prepared || (count($args) == 0)) {
535 $retval = $this->connection->query($this->replaceParameters($sql, $args));
536 if ($this->connection->errno) {
537 $this->error = $this->connection->error;
538 $this->errorno = $this->connection->errno;
541 if (isset($retval->num_rows)) {
542 $this->affected_rows = $retval->num_rows;
544 $this->affected_rows = $this->connection->affected_rows;
550 $stmt = $this->connection->stmt_init();
552 if (!$stmt->prepare($sql)) {
553 $this->error = $stmt->error;
554 $this->errorno = $stmt->errno;
561 foreach ($args AS $param => $value) {
562 if (is_int($args[$param])) {
564 } elseif (is_float($args[$param])) {
566 } elseif (is_string($args[$param])) {
571 $values[] = &$args[$param];
574 if (count($values) > 0) {
575 array_unshift($values, $param_types);
576 call_user_func_array([$stmt, 'bind_param'], $values);
579 if (!$stmt->execute()) {
580 $this->error = $this->connection->error;
581 $this->errorno = $this->connection->errno;
584 $stmt->store_result();
586 $this->affected_rows = $retval->affected_rows;
591 // We are having an own error logging in the function "e"
592 if (($this->errorno != 0) && !$called_from_e) {
593 // We have to preserve the error code, somewhere in the logging it get lost
594 $error = $this->error;
595 $errorno = $this->errorno;
597 $this->logger->error('DB Error', [
598 'code' => $this->errorno,
599 'error' => $this->error,
600 'callstack' => System::callstack(8),
601 'params' => $this->replaceParameters($sql, $args),
604 // On a lost connection we try to reconnect - but only once.
605 if ($errorno == 2006) {
606 if ($this->in_retrial || !$this->reconnect()) {
607 // It doesn't make sense to continue when the database connection was lost
608 if ($this->in_retrial) {
609 $this->logger->notice('Giving up retrial because of database error', [
610 'code' => $this->errorno,
611 'error' => $this->error,
614 $this->logger->notice('Couldn\'t reconnect after database error', [
615 'code' => $this->errorno,
616 'error' => $this->error,
622 $this->logger->notice('Reconnected after database error', [
623 'code' => $this->errorno,
624 'error' => $this->error,
626 $this->in_retrial = true;
627 $ret = $this->p($sql, $args);
628 $this->in_retrial = false;
633 $this->error = $error;
634 $this->errorno = $errorno;
637 $this->profiler->saveTimestamp($stamp1, 'database', System::callstack());
639 if ($this->configCache->get('system', 'db_log')) {
640 $stamp2 = microtime(true);
641 $duration = (float)($stamp2 - $stamp1);
643 if (($duration > $this->configCache->get('system', 'db_loglimit'))) {
644 $duration = round($duration, 3);
645 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
647 @file_put_contents($this->configCache->get('system', 'db_log'), DateTimeFormat::utcNow() . "\t" . $duration . "\t" .
648 basename($backtrace[1]["file"]) . "\t" .
649 $backtrace[1]["line"] . "\t" . $backtrace[2]["function"] . "\t" .
650 substr($this->replaceParameters($sql, $args), 0, 2000) . "\n", FILE_APPEND);
657 * @brief Executes a prepared statement like UPDATE or INSERT that doesn't return data
659 * Please use DBA::delete, DBA::insert, DBA::update, ... instead
661 * @param string $sql SQL statement
663 * @return boolean Was the query successfull? False is returned only if an error occurred
666 public function e($sql)
669 $stamp = microtime(true);
671 $params = DBA::getParam(func_get_args());
673 // In a case of a deadlock we are repeating the query 20 times
677 $stmt = $this->p($sql, $params);
679 if (is_bool($stmt)) {
681 } elseif (is_object($stmt)) {
689 } while (($this->errorno == 1213) && (--$timeout > 0));
691 if ($this->errorno != 0) {
692 // We have to preserve the error code, somewhere in the logging it get lost
693 $error = $this->error;
694 $errorno = $this->errorno;
696 $this->logger->error('DB Error', [
697 'code' => $this->errorno,
698 'error' => $this->error,
699 'callstack' => System::callstack(8),
700 'params' => $this->replaceParameters($sql, $params),
703 // On a lost connection we simply quit.
704 // A reconnect like in $this->p could be dangerous with modifications
705 if ($errorno == 2006) {
706 $this->logger->notice('Giving up because of database error', [
707 'code' => $this->errorno,
708 'error' => $this->error,
713 $this->error = $error;
714 $this->errorno = $errorno;
717 $this->profiler->saveTimestamp($stamp, "database_write", System::callstack());
723 * @brief Check if data exists
725 * @param string $table Table name
726 * @param array $condition array of fields for condition
728 * @return boolean Are there rows for that condition?
731 public function exists($table, $condition)
739 if (empty($condition)) {
740 return DBStructure::existsTable($table);
744 $first_key = key($condition);
745 if (!is_int($first_key)) {
746 $fields = [$first_key];
749 $stmt = $this->select($table, $fields, $condition, ['limit' => 1]);
751 if (is_bool($stmt)) {
754 $retval = ($this->numRows($stmt) > 0);
763 * Fetches the first row
765 * Please use DBA::selectFirst or DBA::exists whenever this is possible.
767 * @brief Fetches the first row
769 * @param string $sql SQL statement
771 * @return array first row of query
774 public function fetchFirst($sql)
776 $params = DBA::getParam(func_get_args());
778 $stmt = $this->p($sql, $params);
780 if (is_bool($stmt)) {
783 $retval = $this->fetch($stmt);
792 * @brief Returns the number of affected rows of the last statement
794 * @return int Number of rows
796 public function affectedRows()
798 return $this->affected_rows;
802 * @brief Returns the number of columns of a statement
804 * @param object Statement object
806 * @return int Number of columns
808 public function columnCount($stmt)
810 if (!is_object($stmt)) {
813 switch ($this->driver) {
815 return $stmt->columnCount();
817 return $stmt->field_count;
823 * @brief Returns the number of rows of a statement
825 * @param PDOStatement|mysqli_result|mysqli_stmt Statement object
827 * @return int Number of rows
829 public function numRows($stmt)
831 if (!is_object($stmt)) {
834 switch ($this->driver) {
836 return $stmt->rowCount();
838 return $stmt->num_rows;
844 * @brief Fetch a single row
846 * @param mixed $stmt statement object
848 * @return array current row
850 public function fetch($stmt)
853 $stamp1 = microtime(true);
857 if (!is_object($stmt)) {
861 switch ($this->driver) {
863 $columns = $stmt->fetch(PDO::FETCH_ASSOC);
866 if (get_class($stmt) == 'mysqli_result') {
867 $columns = $stmt->fetch_assoc();
871 // This code works, but is slow
873 // Bind the result to a result array
877 for ($x = 0; $x < $stmt->field_count; $x++) {
878 $cols[] = &$cols_num[$x];
881 call_user_func_array([$stmt, 'bind_result'], $cols);
883 if (!$stmt->fetch()) {
888 // We need to get the field names for the array keys
889 // It seems that there is no better way to do this.
890 $result = $stmt->result_metadata();
891 $fields = $result->fetch_fields();
893 foreach ($cols_num AS $param => $col) {
894 $columns[$fields[$param]->name] = $col;
898 $this->profiler->saveTimestamp($stamp1, 'database', System::callstack());
904 * @brief Insert a row into a table
906 * @param string/array $table Table name
908 * @return string formatted and sanitzed table name
911 public function formatTableName($table)
913 if (is_string($table)) {
914 return "`" . $this->sanitizeIdentifier($table) . "`";
917 if (!is_array($table)) {
921 $scheme = key($table);
923 return "`" . $this->sanitizeIdentifier($scheme) . "`.`" . $this->sanitizeIdentifier($table[$scheme]) . "`";
927 * @brief Insert a row into a table
929 * @param string $table Table name
930 * @param array $param parameter array
931 * @param bool $on_duplicate_update Do an update on a duplicate entry
933 * @return boolean was the insert successful?
936 public function insert($table, $param, $on_duplicate_update = false)
939 if (empty($table) || empty($param)) {
940 $this->logger->info('Table and fields have to be set');
944 $sql = "INSERT INTO " . $this->formatTableName($table) . " (`" . implode("`, `", array_keys($param)) . "`) VALUES (" .
945 substr(str_repeat("?, ", count($param)), 0, -2) . ")";
947 if ($on_duplicate_update) {
948 $sql .= " ON DUPLICATE KEY UPDATE `" . implode("` = ?, `", array_keys($param)) . "` = ?";
950 $values = array_values($param);
951 $param = array_merge_recursive($values, $values);
954 return $this->e($sql, $param);
958 * @brief Fetch the id of the last insert command
960 * @return integer Last inserted id
962 public function lastInsertId()
964 switch ($this->driver) {
966 $id = $this->connection->lastInsertId();
969 $id = $this->connection->insert_id;
976 * @brief Locks a table for exclusive write access
978 * This function can be extended in the future to accept a table array as well.
980 * @param string $table Table name
982 * @return boolean was the lock successful?
985 public function lock($table)
987 // See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
988 if ($this->driver == 'pdo') {
989 $this->e("SET autocommit=0");
990 $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
992 $this->connection->autocommit(false);
995 $success = $this->e("LOCK TABLES " . $this->formatTableName($table) . " WRITE");
997 if ($this->driver == 'pdo') {
998 $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
1002 if ($this->driver == 'pdo') {
1003 $this->e("SET autocommit=1");
1005 $this->connection->autocommit(true);
1008 $this->in_transaction = true;
1014 * @brief Unlocks all locked tables
1016 * @return boolean was the unlock successful?
1017 * @throws \Exception
1019 public function unlock()
1021 // See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
1022 $this->performCommit();
1024 if ($this->driver == 'pdo') {
1025 $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
1028 $success = $this->e("UNLOCK TABLES");
1030 if ($this->driver == 'pdo') {
1031 $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
1032 $this->e("SET autocommit=1");
1034 $this->connection->autocommit(true);
1037 $this->in_transaction = false;
1042 * @brief Starts a transaction
1044 * @return boolean Was the command executed successfully?
1046 public function transaction()
1048 if (!$this->performCommit()) {
1052 switch ($this->driver) {
1054 if (!$this->connection->inTransaction() && !$this->connection->beginTransaction()) {
1060 if (!$this->connection->begin_transaction()) {
1066 $this->in_transaction = true;
1070 private function performCommit()
1072 switch ($this->driver) {
1074 if (!$this->connection->inTransaction()) {
1078 return $this->connection->commit();
1081 return $this->connection->commit();
1088 * @brief Does a commit
1090 * @return boolean Was the command executed successfully?
1092 public function commit()
1094 if (!$this->performCommit()) {
1097 $this->in_transaction = false;
1102 * @brief Does a rollback
1104 * @return boolean Was the command executed successfully?
1106 public function rollback()
1110 switch ($this->driver) {
1112 if (!$this->connection->inTransaction()) {
1116 $ret = $this->connection->rollBack();
1120 $ret = $this->connection->rollback();
1123 $this->in_transaction = false;
1128 * @brief Build the array with the table relations
1130 * The array is build from the database definitions in DBStructure.php
1132 * This process must only be started once, since the value is cached.
1134 private function buildRelationData()
1136 $definition = DBStructure::definition($this->configCache->get('system', 'basepath'));
1138 foreach ($definition AS $table => $structure) {
1139 foreach ($structure['fields'] AS $field => $field_struct) {
1140 if (isset($field_struct['relation'])) {
1141 foreach ($field_struct['relation'] AS $rel_table => $rel_field) {
1142 $this->relation[$rel_table][$rel_field][$table][] = $field;
1150 * @brief Delete a row from a table
1152 * @param string $table Table name
1153 * @param array $conditions Field condition(s)
1154 * @param array $options
1155 * - cascade: If true we delete records in other tables that depend on the one we're deleting through
1156 * relations (default: true)
1157 * @param array $callstack Internal use: prevent endless loops
1159 * @return boolean was the delete successful?
1160 * @throws \Exception
1162 public function delete($table, array $conditions, array $options = [], array &$callstack = [])
1164 if (empty($table) || empty($conditions)) {
1165 $this->logger->info('Table and conditions have to be set');
1171 // Create a key for the loop prevention
1172 $key = $table . ':' . json_encode($conditions);
1174 // We quit when this key already exists in the callstack.
1175 if (isset($callstack[$key])) {
1179 $callstack[$key] = true;
1181 $table = $this->sanitizeIdentifier($table);
1183 $commands[$key] = ['table' => $table, 'conditions' => $conditions];
1185 // Don't use "defaults" here, since it would set "false" to "true"
1186 if (isset($options['cascade'])) {
1187 $cascade = $options['cascade'];
1192 // To speed up the whole process we cache the table relations
1193 if ($cascade && count($this->relation) == 0) {
1194 $this->buildRelationData();
1197 // Is there a relation entry for the table?
1198 if ($cascade && isset($this->relation[$table])) {
1199 // We only allow a simple "one field" relation.
1200 $field = array_keys($this->relation[$table])[0];
1201 $rel_def = array_values($this->relation[$table])[0];
1203 // Create a key for preventing double queries
1204 $qkey = $field . '-' . $table . ':' . json_encode($conditions);
1206 // When the search field is the relation field, we don't need to fetch the rows
1207 // This is useful when the leading record is already deleted in the frontend but the rest is done in the backend
1208 if ((count($conditions) == 1) && ($field == array_keys($conditions)[0])) {
1209 foreach ($rel_def AS $rel_table => $rel_fields) {
1210 foreach ($rel_fields AS $rel_field) {
1211 $this->delete($rel_table, [$rel_field => array_values($conditions)[0]], $options, $callstack);
1214 // We quit when this key already exists in the callstack.
1215 } elseif (!isset($callstack[$qkey])) {
1216 $callstack[$qkey] = true;
1218 // Fetch all rows that are to be deleted
1219 $data = $this->select($table, [$field], $conditions);
1221 while ($row = $this->fetch($data)) {
1222 $this->delete($table, [$field => $row[$field]], $options, $callstack);
1225 $this->close($data);
1227 // Since we had split the delete command we don't need the original command anymore
1228 unset($commands[$key]);
1232 // Now we finalize the process
1233 $do_transaction = !$this->in_transaction;
1235 if ($do_transaction) {
1236 $this->transaction();
1242 foreach ($commands AS $command) {
1243 $conditions = $command['conditions'];
1245 $first_key = key($conditions);
1247 $condition_string = DBA::buildCondition($conditions);
1249 if ((count($command['conditions']) > 1) || is_int($first_key)) {
1250 $sql = "DELETE FROM `" . $command['table'] . "`" . $condition_string;
1251 $this->logger->debug($this->replaceParameters($sql, $conditions));
1253 if (!$this->e($sql, $conditions)) {
1254 if ($do_transaction) {
1260 $key_table = $command['table'];
1261 $key_condition = array_keys($command['conditions'])[0];
1262 $value = array_values($command['conditions'])[0];
1264 // Split the SQL queries in chunks of 100 values
1265 // We do the $i stuff here to make the code better readable
1266 $i = isset($counter[$key_table][$key_condition]) ? $counter[$key_table][$key_condition] : 0;
1267 if (isset($compacted[$key_table][$key_condition][$i]) && count($compacted[$key_table][$key_condition][$i]) > 100) {
1271 $compacted[$key_table][$key_condition][$i][$value] = $value;
1272 $counter[$key_table][$key_condition] = $i;
1275 foreach ($compacted AS $table => $values) {
1276 foreach ($values AS $field => $field_value_list) {
1277 foreach ($field_value_list AS $field_values) {
1278 $sql = "DELETE FROM `" . $table . "` WHERE `" . $field . "` IN (" .
1279 substr(str_repeat("?, ", count($field_values)), 0, -2) . ");";
1281 $this->logger->debug($this->replaceParameters($sql, $field_values));
1283 if (!$this->e($sql, $field_values)) {
1284 if ($do_transaction) {
1292 if ($do_transaction) {
1299 * @brief Updates rows
1301 * Updates rows in the database. When $old_fields is set to an array,
1302 * the system will only do an update if the fields in that array changed.
1305 * Only the values in $old_fields are compared.
1306 * This is an intentional behaviour.
1309 * We include the timestamp field in $fields but not in $old_fields.
1310 * Then the row will only get the new timestamp when the other fields had changed.
1312 * When $old_fields is set to a boolean value the system will do this compare itself.
1313 * When $old_fields is set to "true" the system will do an insert if the row doesn't exists.
1316 * Only set $old_fields to a boolean value when you are sure that you will update a single row.
1317 * When you set $old_fields to "true" then $fields must contain all relevant fields!
1319 * @param string $table Table name
1320 * @param array $fields contains the fields that are updated
1321 * @param array $condition condition array with the key values
1322 * @param array|boolean $old_fields array with the old field values that are about to be replaced (true = update on duplicate)
1324 * @return boolean was the update successfull?
1325 * @throws \Exception
1327 public function update($table, $fields, $condition, $old_fields = [])
1330 if (empty($table) || empty($fields) || empty($condition)) {
1331 $this->logger->info('Table, fields and condition have to be set');
1335 $condition_string = DBA::buildCondition($condition);
1337 if (is_bool($old_fields)) {
1338 $do_insert = $old_fields;
1340 $old_fields = $this->selectFirst($table, [], $condition);
1342 if (is_bool($old_fields)) {
1344 $values = array_merge($condition, $fields);
1345 return $this->insert($table, $values, $do_insert);
1351 $do_update = (count($old_fields) == 0);
1353 foreach ($old_fields AS $fieldname => $content) {
1354 if (isset($fields[$fieldname])) {
1355 if (($fields[$fieldname] == $content) && !is_null($content)) {
1356 unset($fields[$fieldname]);
1363 if (!$do_update || (count($fields) == 0)) {
1367 $sql = "UPDATE " . $this->formatTableName($table) . " SET `" .
1368 implode("` = ?, `", array_keys($fields)) . "` = ?" . $condition_string;
1370 $params1 = array_values($fields);
1371 $params2 = array_values($condition);
1372 $params = array_merge_recursive($params1, $params2);
1374 return $this->e($sql, $params);
1378 * Retrieve a single record from a table and returns it in an associative array
1380 * @brief Retrieve a single record from a table
1382 * @param string $table
1383 * @param array $fields
1384 * @param array $condition
1385 * @param array $params
1387 * @return bool|array
1388 * @throws \Exception
1389 * @see $this->select
1391 public function selectFirst($table, array $fields = [], array $condition = [], $params = [])
1393 $params['limit'] = 1;
1394 $result = $this->select($table, $fields, $condition, $params);
1396 if (is_bool($result)) {
1399 $row = $this->fetch($result);
1400 $this->close($result);
1406 * @brief Select rows from a table
1408 * @param string $table Table name
1409 * @param array $fields Array of selected fields, empty for all
1410 * @param array $condition Array of fields for condition
1411 * @param array $params Array of several parameters
1413 * @return boolean|object
1417 * $fields = array("id", "uri", "uid", "network");
1419 * $condition = array("uid" => 1, "network" => 'dspr');
1421 * $condition = array("`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr');
1423 * $params = array("order" => array("id", "received" => true), "limit" => 10);
1425 * $data = DBA::select($table, $fields, $condition, $params);
1426 * @throws \Exception
1428 public function select($table, array $fields = [], array $condition = [], array $params = [])
1430 if (empty($table)) {
1434 if (count($fields) > 0) {
1435 $select_fields = "`" . implode("`, `", array_values($fields)) . "`";
1437 $select_fields = "*";
1440 $condition_string = DBA::buildCondition($condition);
1442 $param_string = DBA::buildParameter($params);
1444 $sql = "SELECT " . $select_fields . " FROM " . $this->formatTableName($table) . $condition_string . $param_string;
1446 $result = $this->p($sql, $condition);
1452 * @brief Counts the rows from a table satisfying the provided condition
1454 * @param string $table Table name
1455 * @param array $condition array of fields for condition
1462 * $condition = ["uid" => 1, "network" => 'dspr'];
1464 * $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr'];
1466 * $count = DBA::count($table, $condition);
1467 * @throws \Exception
1469 public function count($table, array $condition = [])
1471 if (empty($table)) {
1475 $condition_string = DBA::buildCondition($condition);
1477 $sql = "SELECT COUNT(*) AS `count` FROM " . $this->formatTableName($table) . $condition_string;
1479 $row = $this->fetchFirst($sql, $condition);
1481 return $row['count'];
1485 * @brief Fills an array with data from a query
1487 * @param object $stmt statement object
1488 * @param bool $do_close
1490 * @return array Data array
1492 public function toArray($stmt, $do_close = true)
1494 if (is_bool($stmt)) {
1499 while ($row = $this->fetch($stmt)) {
1503 $this->close($stmt);
1509 * @brief Returns the error number of the last query
1511 * @return string Error number (0 if no error)
1513 public function errorNo()
1515 return $this->errorno;
1519 * @brief Returns the error message of the last query
1521 * @return string Error message ('' if no error)
1523 public function errorMessage()
1525 return $this->error;
1529 * @brief Closes the current statement
1531 * @param object $stmt statement object
1533 * @return boolean was the close successful?
1535 public function close($stmt)
1538 $stamp1 = microtime(true);
1540 if (!is_object($stmt)) {
1544 switch ($this->driver) {
1546 $ret = $stmt->closeCursor();
1549 // MySQLi offers both a mysqli_stmt and a mysqli_result class.
1550 // We should be careful not to assume the object type of $stmt
1551 // because DBA::p() has been able to return both types.
1552 if ($stmt instanceof mysqli_stmt) {
1553 $stmt->free_result();
1554 $ret = $stmt->close();
1555 } elseif ($stmt instanceof mysqli_result) {
1564 $this->profiler->saveTimestamp($stamp1, 'database', System::callstack());
1570 * @brief Return a list of database processes
1573 * 'list' => List of processes, separated in their different states
1574 * 'amount' => Number of concurrent database processes
1575 * @throws \Exception
1577 public function processlist()
1579 $ret = $this->p("SHOW PROCESSLIST");
1580 $data = $this->toArray($ret);
1584 foreach ($data as $process) {
1585 $state = trim($process["State"]);
1587 // Filter out all non blocking processes
1588 if (!in_array($state, ["", "init", "statistics", "updating"])) {
1595 foreach ($states as $state => $usage) {
1596 if ($statelist != "") {
1599 $statelist .= $state . ": " . $usage;
1601 return (["list" => $statelist, "amount" => $processes]);
1605 * Checks if $array is a filled array with at least one entry.
1607 * @param mixed $array A filled array with at least one entry
1609 * @return boolean Whether $array is a filled array or an object with rows
1611 public function isResult($array)
1613 // It could be a return value from an update statement
1614 if (is_bool($array)) {
1618 if (is_object($array)) {
1619 return $this->numRows($array) > 0;
1622 return (is_array($array) && (count($array) > 0));
1626 * @brief Callback function for "esc_array"
1628 * @param mixed $value Array value
1629 * @param string $key Array key
1630 * @param boolean $add_quotation add quotation marks for string values
1634 private function escapeArrayCallback(&$value, $key, $add_quotation)
1636 if (!$add_quotation) {
1637 if (is_bool($value)) {
1638 $value = ($value ? '1' : '0');
1640 $value = $this->escape($value);
1645 if (is_bool($value)) {
1646 $value = ($value ? 'true' : 'false');
1647 } elseif (is_float($value) || is_integer($value)) {
1648 $value = (string)$value;
1650 $value = "'" . $this->escape($value) . "'";
1655 * @brief Escapes a whole array
1657 * @param mixed $arr Array with values to be escaped
1658 * @param boolean $add_quotation add quotation marks for string values
1662 public function escapeArray(&$arr, $add_quotation = false)
1664 array_walk($arr, [$this, 'escapeArrayCallback'], $add_quotation);