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()) {
93 return $this->connected;
96 // Reset connected state
97 $this->connected = false;
100 $serveraddr = trim($this->configCache->get('database', 'hostname'));
101 $serverdata = explode(':', $serveraddr);
102 $server = $serverdata[0];
103 if (count($serverdata) > 1) {
104 $port = trim($serverdata[1]);
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 if (class_exists('\PDO') && in_array('mysql', PDO::getAvailableDrivers())) {
117 $this->driver = 'pdo';
118 $connect = "mysql:host=" . $server . ";dbname=" . $db;
121 $connect .= ";port=" . $port;
125 $connect .= ";charset=" . $charset;
129 $this->connection = @new PDO($connect, $user, $pass);
130 $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
131 $this->connected = true;
132 } catch (PDOException $e) {
133 $this->connected = false;
137 if (!$this->connected && class_exists('\mysqli')) {
138 $this->driver = 'mysqli';
141 $this->connection = @new mysqli($server, $user, $pass, $db, $port);
143 $this->connection = @new mysqli($server, $user, $pass, $db);
146 if (!mysqli_connect_errno()) {
147 $this->connected = true;
150 $this->connection->set_charset($charset);
155 // No suitable SQL driver was found.
156 if (!$this->connected) {
157 $this->driver = null;
158 $this->connection = null;
161 return $this->connected;
165 * Sets the logger for DBA
167 * @note this is necessary because if we want to load the logger configuration
168 * from the DB, but there's an error, we would print out an exception.
169 * So the logger gets updated after the logger configuration can be retrieved
172 * @param LoggerInterface $logger
174 public function setLogger(LoggerInterface $logger)
176 $this->logger = $logger;
180 * Sets the profiler for DBA
182 * @param Profiler $profiler
184 public function setProfiler(Profiler $profiler)
186 $this->profiler = $profiler;
189 * Disconnects the current database connection
191 public function disconnect()
193 if (!is_null($this->connection)) {
194 switch ($this->driver) {
196 $this->connection = null;
199 $this->connection->close();
200 $this->connection = null;
205 $this->driver = null;
206 $this->connected = false;
210 * Perform a reconnect of an existing database connection
212 public function reconnect()
215 return $this->connect();
219 * Return the database object.
223 public function getConnection()
225 return $this->connection;
229 * @brief Returns the MySQL server version string
231 * This function discriminate between the deprecated mysql API and the current
232 * object-oriented mysqli API. Example of returned string: 5.5.46-0+deb8u1
236 public function serverInfo()
238 if ($this->server_info == '') {
239 switch ($this->driver) {
241 $this->server_info = $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION);
244 $this->server_info = $this->connection->server_info;
248 return $this->server_info;
252 * @brief Returns the selected database name
257 public function databaseName()
259 $ret = $this->p("SELECT DATABASE() AS `db`");
260 $data = $this->toArray($ret);
261 return $data[0]['db'];
265 * @brief Analyze a database query and log this if some conditions are met.
267 * @param string $query The database query that will be analyzed
271 private function logIndex($query)
274 if (!$this->configCache->get('system', 'db_log_index')) {
278 // Don't explain an explain statement
279 if (strtolower(substr($query, 0, 7)) == "explain") {
283 // Only do the explain on "select", "update" and "delete"
284 if (!in_array(strtolower(substr($query, 0, 6)), ["select", "update", "delete"])) {
288 $r = $this->p("EXPLAIN " . $query);
289 if (!$this->isResult($r)) {
293 $watchlist = explode(',', $this->configCache->get('system', 'db_log_index_watch'));
294 $blacklist = explode(',', $this->configCache->get('system', 'db_log_index_blacklist'));
296 while ($row = $this->fetch($r)) {
297 if ((intval($this->configCache->get('system', 'db_loglimit_index')) > 0)) {
298 $log = (in_array($row['key'], $watchlist) &&
299 ($row['rows'] >= intval($this->configCache->get('system', 'db_loglimit_index'))));
304 if ((intval($this->configCache->get('system', 'db_loglimit_index_high')) > 0) && ($row['rows'] >= intval($this->configCache->get('system', 'db_loglimit_index_high')))) {
308 if (in_array($row['key'], $blacklist) || ($row['key'] == "")) {
313 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
314 @file_put_contents($this->configCache->get('system', 'db_log_index'), DateTimeFormat::utcNow() . "\t" .
315 $row['key'] . "\t" . $row['rows'] . "\t" . $row['Extra'] . "\t" .
316 basename($backtrace[1]["file"]) . "\t" .
317 $backtrace[1]["line"] . "\t" . $backtrace[2]["function"] . "\t" .
318 substr($query, 0, 2000) . "\n", FILE_APPEND);
324 * Removes every not whitelisted character from the identifier string
326 * @param string $identifier
328 * @return string sanitized identifier
331 private function sanitizeIdentifier($identifier)
333 return preg_replace('/[^A-Za-z0-9_\-]+/', '', $identifier);
336 public function escape($str)
338 if ($this->connected) {
339 switch ($this->driver) {
341 return substr(@$this->connection->quote($str, PDO::PARAM_STR), 1, -1);
344 return @$this->connection->real_escape_string($str);
347 return str_replace("'", "\\'", $str);
351 public function isConnected()
353 return $this->connected;
356 public function connected()
360 if (is_null($this->connection)) {
364 switch ($this->driver) {
366 $r = $this->p("SELECT 1");
367 if ($this->isResult($r)) {
368 $row = $this->toArray($r);
369 $connected = ($row[0]['1'] == '1');
373 $connected = $this->connection->ping();
381 * @brief Replaces ANY_VALUE() function by MIN() function,
382 * if the database server does not support ANY_VALUE().
384 * Considerations for Standard SQL, or MySQL with ONLY_FULL_GROUP_BY (default since 5.7.5).
385 * ANY_VALUE() is available from MySQL 5.7.5 https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html
386 * A standard fall-back is to use MIN().
388 * @param string $sql An SQL string without the values
390 * @return string The input SQL string modified if necessary.
392 public function anyValueFallback($sql)
394 $server_info = $this->serverInfo();
395 if (version_compare($server_info, '5.7.5', '<') ||
396 (stripos($server_info, 'MariaDB') !== false)) {
397 $sql = str_ireplace('ANY_VALUE(', 'MIN(', $sql);
403 * @brief Replaces the ? placeholders with the parameters in the $args array
405 * @param string $sql SQL query
406 * @param array $args The parameters that are to replace the ? placeholders
408 * @return string The replaced SQL query
410 private function replaceParameters($sql, $args)
413 foreach ($args AS $param => $value) {
414 if (is_int($args[$param]) || is_float($args[$param])) {
415 $replace = intval($args[$param]);
417 $replace = "'" . $this->escape($args[$param]) . "'";
420 $pos = strpos($sql, '?', $offset);
421 if ($pos !== false) {
422 $sql = substr_replace($sql, $replace, $pos, 1);
424 $offset = $pos + strlen($replace);
430 * @brief Executes a prepared statement that returns data
431 * @usage Example: $r = p("SELECT * FROM `item` WHERE `guid` = ?", $guid);
433 * Please only use it with complicated queries.
434 * For all regular queries please use DBA::select or DBA::exists
436 * @param string $sql SQL statement
438 * @return bool|object statement object or result object
441 public function p($sql)
444 $stamp1 = microtime(true);
446 $params = DBA::getParam(func_get_args());
448 // Renumber the array keys to be sure that they fit
451 foreach ($params AS $param) {
452 // Avoid problems with some MySQL servers and boolean values. See issue #3645
453 if (is_bool($param)) {
454 $param = (int)$param;
456 $args[++$i] = $param;
459 if (!$this->connected) {
463 if ((substr_count($sql, '?') != count($args)) && (count($args) > 0)) {
464 // Question: Should we continue or stop the query here?
465 $this->logger->warning('Query parameters mismatch.', ['query' => $sql, 'args' => $args, 'callstack' => System::callstack()]);
468 $sql = DBA::cleanQuery($sql);
469 $sql = $this->anyValueFallback($sql);
473 if ($this->configCache->get('system', 'db_callstack') !== null) {
474 $sql = "/*" . System::callstack() . " */ " . $sql;
479 $this->affected_rows = 0;
481 // We have to make some things different if this function is called from "e"
482 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
484 if (isset($trace[1])) {
485 $called_from = $trace[1];
487 // We use just something that is defined to avoid warnings
488 $called_from = $trace[0];
490 // We are having an own error logging in the function "e"
491 $called_from_e = ($called_from['function'] == 'e');
493 if (!isset($this->connection)) {
494 throw new InternalServerErrorException('The Connection is empty, although connected is set true.');
497 switch ($this->driver) {
499 // If there are no arguments we use "query"
500 if (count($args) == 0) {
501 if (!$retval = $this->connection->query($sql)) {
502 $errorInfo = $this->connection->errorInfo();
503 $this->error = $errorInfo[2];
504 $this->errorno = $errorInfo[1];
508 $this->affected_rows = $retval->rowCount();
512 /** @var $stmt mysqli_stmt|PDOStatement */
513 if (!$stmt = $this->connection->prepare($sql)) {
514 $errorInfo = $this->connection->errorInfo();
515 $this->error = $errorInfo[2];
516 $this->errorno = $errorInfo[1];
521 foreach ($args AS $param => $value) {
522 if (is_int($args[$param])) {
523 $data_type = PDO::PARAM_INT;
525 $data_type = PDO::PARAM_STR;
527 $stmt->bindParam($param, $args[$param], $data_type);
530 if (!$stmt->execute()) {
531 $errorInfo = $stmt->errorInfo();
532 $this->error = $errorInfo[2];
533 $this->errorno = $errorInfo[1];
537 $this->affected_rows = $retval->rowCount();
541 // There are SQL statements that cannot be executed with a prepared statement
542 $parts = explode(' ', $orig_sql);
543 $command = strtolower($parts[0]);
544 $can_be_prepared = in_array($command, ['select', 'update', 'insert', 'delete']);
546 // The fallback routine is called as well when there are no arguments
547 if (!$can_be_prepared || (count($args) == 0)) {
548 $retval = $this->connection->query($this->replaceParameters($sql, $args));
549 if ($this->connection->errno) {
550 $this->error = $this->connection->error;
551 $this->errorno = $this->connection->errno;
554 if (isset($retval->num_rows)) {
555 $this->affected_rows = $retval->num_rows;
557 $this->affected_rows = $this->connection->affected_rows;
563 $stmt = $this->connection->stmt_init();
565 if (!$stmt->prepare($sql)) {
566 $this->error = $stmt->error;
567 $this->errorno = $stmt->errno;
574 foreach ($args AS $param => $value) {
575 if (is_int($args[$param])) {
577 } elseif (is_float($args[$param])) {
579 } elseif (is_string($args[$param])) {
584 $values[] = &$args[$param];
587 if (count($values) > 0) {
588 array_unshift($values, $param_types);
589 call_user_func_array([$stmt, 'bind_param'], $values);
592 if (!$stmt->execute()) {
593 $this->error = $this->connection->error;
594 $this->errorno = $this->connection->errno;
597 $stmt->store_result();
599 $this->affected_rows = $retval->affected_rows;
604 // We are having an own error logging in the function "e"
605 if (($this->errorno != 0) && !$called_from_e) {
606 // We have to preserve the error code, somewhere in the logging it get lost
607 $error = $this->error;
608 $errorno = $this->errorno;
610 $this->logger->error('DB Error', [
611 'code' => $this->errorno,
612 'error' => $this->error,
613 'callstack' => System::callstack(8),
614 'params' => $this->replaceParameters($sql, $args),
617 // On a lost connection we try to reconnect - but only once.
618 if ($errorno == 2006) {
619 if ($this->in_retrial || !$this->reconnect()) {
620 // It doesn't make sense to continue when the database connection was lost
621 if ($this->in_retrial) {
622 $this->logger->notice('Giving up retrial because of database error', [
623 'code' => $this->errorno,
624 'error' => $this->error,
627 $this->logger->notice('Couldn\'t reconnect after database error', [
628 'code' => $this->errorno,
629 'error' => $this->error,
635 $this->logger->notice('Reconnected after database error', [
636 'code' => $this->errorno,
637 'error' => $this->error,
639 $this->in_retrial = true;
640 $ret = $this->p($sql, $args);
641 $this->in_retrial = false;
646 $this->error = $error;
647 $this->errorno = $errorno;
650 $this->profiler->saveTimestamp($stamp1, 'database', System::callstack());
652 if ($this->configCache->get('system', 'db_log')) {
653 $stamp2 = microtime(true);
654 $duration = (float)($stamp2 - $stamp1);
656 if (($duration > $this->configCache->get('system', 'db_loglimit'))) {
657 $duration = round($duration, 3);
658 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
660 @file_put_contents($this->configCache->get('system', 'db_log'), DateTimeFormat::utcNow() . "\t" . $duration . "\t" .
661 basename($backtrace[1]["file"]) . "\t" .
662 $backtrace[1]["line"] . "\t" . $backtrace[2]["function"] . "\t" .
663 substr($this->replaceParameters($sql, $args), 0, 2000) . "\n", FILE_APPEND);
670 * @brief Executes a prepared statement like UPDATE or INSERT that doesn't return data
672 * Please use DBA::delete, DBA::insert, DBA::update, ... instead
674 * @param string $sql SQL statement
676 * @return boolean Was the query successfull? False is returned only if an error occurred
679 public function e($sql)
682 $stamp = microtime(true);
684 $params = DBA::getParam(func_get_args());
686 // In a case of a deadlock we are repeating the query 20 times
690 $stmt = $this->p($sql, $params);
692 if (is_bool($stmt)) {
694 } elseif (is_object($stmt)) {
702 } while (($this->errorno == 1213) && (--$timeout > 0));
704 if ($this->errorno != 0) {
705 // We have to preserve the error code, somewhere in the logging it get lost
706 $error = $this->error;
707 $errorno = $this->errorno;
709 $this->logger->error('DB Error', [
710 'code' => $this->errorno,
711 'error' => $this->error,
712 'callstack' => System::callstack(8),
713 'params' => $this->replaceParameters($sql, $params),
716 // On a lost connection we simply quit.
717 // A reconnect like in $this->p could be dangerous with modifications
718 if ($errorno == 2006) {
719 $this->logger->notice('Giving up because of database error', [
720 'code' => $this->errorno,
721 'error' => $this->error,
726 $this->error = $error;
727 $this->errorno = $errorno;
730 $this->profiler->saveTimestamp($stamp, "database_write", System::callstack());
736 * @brief Check if data exists
738 * @param string|array $table Table name or array [schema => table]
739 * @param array $condition array of fields for condition
741 * @return boolean Are there rows for that condition?
744 public function exists($table, $condition)
752 if (empty($condition)) {
753 return DBStructure::existsTable($table);
757 $first_key = key($condition);
758 if (!is_int($first_key)) {
759 $fields = [$first_key];
762 $stmt = $this->select($table, $fields, $condition, ['limit' => 1]);
764 if (is_bool($stmt)) {
767 $retval = ($this->numRows($stmt) > 0);
776 * Fetches the first row
778 * Please use DBA::selectFirst or DBA::exists whenever this is possible.
780 * @brief Fetches the first row
782 * @param string $sql SQL statement
784 * @return array first row of query
787 public function fetchFirst($sql)
789 $params = DBA::getParam(func_get_args());
791 $stmt = $this->p($sql, $params);
793 if (is_bool($stmt)) {
796 $retval = $this->fetch($stmt);
805 * @brief Returns the number of affected rows of the last statement
807 * @return int Number of rows
809 public function affectedRows()
811 return $this->affected_rows;
815 * @brief Returns the number of columns of a statement
817 * @param object Statement object
819 * @return int Number of columns
821 public function columnCount($stmt)
823 if (!is_object($stmt)) {
826 switch ($this->driver) {
828 return $stmt->columnCount();
830 return $stmt->field_count;
836 * @brief Returns the number of rows of a statement
838 * @param PDOStatement|mysqli_result|mysqli_stmt Statement object
840 * @return int Number of rows
842 public function numRows($stmt)
844 if (!is_object($stmt)) {
847 switch ($this->driver) {
849 return $stmt->rowCount();
851 return $stmt->num_rows;
857 * @brief Fetch a single row
859 * @param mixed $stmt statement object
861 * @return array current row
863 public function fetch($stmt)
866 $stamp1 = microtime(true);
870 if (!is_object($stmt)) {
874 switch ($this->driver) {
876 $columns = $stmt->fetch(PDO::FETCH_ASSOC);
879 if (get_class($stmt) == 'mysqli_result') {
880 $columns = $stmt->fetch_assoc();
884 // This code works, but is slow
886 // Bind the result to a result array
890 for ($x = 0; $x < $stmt->field_count; $x++) {
891 $cols[] = &$cols_num[$x];
894 call_user_func_array([$stmt, 'bind_result'], $cols);
896 if (!$stmt->fetch()) {
901 // We need to get the field names for the array keys
902 // It seems that there is no better way to do this.
903 $result = $stmt->result_metadata();
904 $fields = $result->fetch_fields();
906 foreach ($cols_num AS $param => $col) {
907 $columns[$fields[$param]->name] = $col;
911 $this->profiler->saveTimestamp($stamp1, 'database', System::callstack());
917 * @brief Insert a row into a table
919 * @param string|array $table Table name or array [schema => table]
920 * @param array $param parameter array
921 * @param bool $on_duplicate_update Do an update on a duplicate entry
923 * @return boolean was the insert successful?
926 public function insert($table, $param, $on_duplicate_update = false)
928 if (empty($table) || empty($param)) {
929 $this->logger->info('Table and fields have to be set');
933 $table_string = DBA::buildTableString($table);
935 $fields_string = implode(', ', array_map([DBA::class, 'quoteIdentifier'], array_keys($param)));
937 $values_string = substr(str_repeat("?, ", count($param)), 0, -2);
939 $sql = "INSERT INTO " . $table_string . " (" . $fields_string . ") VALUES (" . $values_string . ")";
941 if ($on_duplicate_update) {
942 $fields_string = implode(' = ?, ', array_map([DBA::class, 'quoteIdentifier'], array_keys($param)));
944 $sql .= " ON DUPLICATE KEY UPDATE " . $fields_string . " = ?";
946 $values = array_values($param);
947 $param = array_merge_recursive($values, $values);
950 return $this->e($sql, $param);
954 * @brief Fetch the id of the last insert command
956 * @return integer Last inserted id
958 public function lastInsertId()
960 switch ($this->driver) {
962 $id = $this->connection->lastInsertId();
965 $id = $this->connection->insert_id;
972 * @brief Locks a table for exclusive write access
974 * This function can be extended in the future to accept a table array as well.
976 * @param string|array $table Table name or array [schema => table]
978 * @return boolean was the lock successful?
981 public function lock($table)
983 // See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
984 if ($this->driver == 'pdo') {
985 $this->e("SET autocommit=0");
986 $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
988 $this->connection->autocommit(false);
991 $success = $this->e("LOCK TABLES " . DBA::buildTableString($table) . " WRITE");
993 if ($this->driver == 'pdo') {
994 $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
998 if ($this->driver == 'pdo') {
999 $this->e("SET autocommit=1");
1001 $this->connection->autocommit(true);
1004 $this->in_transaction = true;
1010 * @brief Unlocks all locked tables
1012 * @return boolean was the unlock successful?
1013 * @throws \Exception
1015 public function unlock()
1017 // See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
1018 $this->performCommit();
1020 if ($this->driver == 'pdo') {
1021 $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
1024 $success = $this->e("UNLOCK TABLES");
1026 if ($this->driver == 'pdo') {
1027 $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
1028 $this->e("SET autocommit=1");
1030 $this->connection->autocommit(true);
1033 $this->in_transaction = false;
1038 * @brief Starts a transaction
1040 * @return boolean Was the command executed successfully?
1042 public function transaction()
1044 if (!$this->performCommit()) {
1048 switch ($this->driver) {
1050 if (!$this->connection->inTransaction() && !$this->connection->beginTransaction()) {
1056 if (!$this->connection->begin_transaction()) {
1062 $this->in_transaction = true;
1066 protected function performCommit()
1068 switch ($this->driver) {
1070 if (!$this->connection->inTransaction()) {
1074 return $this->connection->commit();
1077 return $this->connection->commit();
1084 * @brief Does a commit
1086 * @return boolean Was the command executed successfully?
1088 public function commit()
1090 if (!$this->performCommit()) {
1093 $this->in_transaction = false;
1098 * @brief Does a rollback
1100 * @return boolean Was the command executed successfully?
1102 public function rollback()
1106 switch ($this->driver) {
1108 if (!$this->connection->inTransaction()) {
1112 $ret = $this->connection->rollBack();
1116 $ret = $this->connection->rollback();
1119 $this->in_transaction = false;
1124 * @brief Build the array with the table relations
1126 * The array is build from the database definitions in DBStructure.php
1128 * This process must only be started once, since the value is cached.
1130 private function buildRelationData()
1132 $definition = DBStructure::definition($this->configCache->get('system', 'basepath'));
1134 foreach ($definition AS $table => $structure) {
1135 foreach ($structure['fields'] AS $field => $field_struct) {
1136 if (isset($field_struct['relation'])) {
1137 foreach ($field_struct['relation'] AS $rel_table => $rel_field) {
1138 $this->relation[$rel_table][$rel_field][$table][] = $field;
1146 * @brief Delete a row from a table
1148 * Note: this methods does NOT accept schema => table arrays because of the complex relation stuff.
1150 * @param string $table Table name
1151 * @param array $conditions Field condition(s)
1152 * @param array $options
1153 * - cascade: If true we delete records in other tables that depend on the one we're deleting through
1154 * relations (default: true)
1155 * @param array $callstack Internal use: prevent endless loops
1157 * @return boolean was the delete successful?
1158 * @throws \Exception
1160 public function delete($table, array $conditions, array $options = [], array &$callstack = [])
1162 if (empty($table) || empty($conditions)) {
1163 $this->logger->info('Table and conditions have to be set');
1169 // Create a key for the loop prevention
1170 $key = $table . ':' . json_encode($conditions);
1172 // We quit when this key already exists in the callstack.
1173 if (isset($callstack[$key])) {
1177 $callstack[$key] = true;
1179 $commands[$key] = ['table' => $table, 'conditions' => $conditions];
1181 // Don't use "defaults" here, since it would set "false" to "true"
1182 if (isset($options['cascade'])) {
1183 $cascade = $options['cascade'];
1188 // To speed up the whole process we cache the table relations
1189 if ($cascade && count($this->relation) == 0) {
1190 $this->buildRelationData();
1193 // Is there a relation entry for the table?
1194 if ($cascade && isset($this->relation[$table])) {
1195 // We only allow a simple "one field" relation.
1196 $field = array_keys($this->relation[$table])[0];
1197 $rel_def = array_values($this->relation[$table])[0];
1199 // Create a key for preventing double queries
1200 $qkey = $field . '-' . $table . ':' . json_encode($conditions);
1202 // When the search field is the relation field, we don't need to fetch the rows
1203 // This is useful when the leading record is already deleted in the frontend but the rest is done in the backend
1204 if ((count($conditions) == 1) && ($field == array_keys($conditions)[0])) {
1205 foreach ($rel_def AS $rel_table => $rel_fields) {
1206 foreach ($rel_fields AS $rel_field) {
1207 $this->delete($rel_table, [$rel_field => array_values($conditions)[0]], $options, $callstack);
1210 // We quit when this key already exists in the callstack.
1211 } elseif (!isset($callstack[$qkey])) {
1212 $callstack[$qkey] = true;
1214 // Fetch all rows that are to be deleted
1215 $data = $this->select($table, [$field], $conditions);
1217 while ($row = $this->fetch($data)) {
1218 $this->delete($table, [$field => $row[$field]], $options, $callstack);
1221 $this->close($data);
1223 // Since we had split the delete command we don't need the original command anymore
1224 unset($commands[$key]);
1228 // Now we finalize the process
1229 $do_transaction = !$this->in_transaction;
1231 if ($do_transaction) {
1232 $this->transaction();
1238 foreach ($commands AS $command) {
1239 $conditions = $command['conditions'];
1241 $first_key = key($conditions);
1243 $condition_string = DBA::buildCondition($conditions);
1245 if ((count($command['conditions']) > 1) || is_int($first_key)) {
1246 $sql = "DELETE FROM " . DBA::quoteIdentifier($command['table']) . " " . $condition_string;
1247 $this->logger->debug($this->replaceParameters($sql, $conditions));
1249 if (!$this->e($sql, $conditions)) {
1250 if ($do_transaction) {
1256 $key_table = $command['table'];
1257 $key_condition = array_keys($command['conditions'])[0];
1258 $value = array_values($command['conditions'])[0];
1260 // Split the SQL queries in chunks of 100 values
1261 // We do the $i stuff here to make the code better readable
1262 $i = isset($counter[$key_table][$key_condition]) ? $counter[$key_table][$key_condition] : 0;
1263 if (isset($compacted[$key_table][$key_condition][$i]) && count($compacted[$key_table][$key_condition][$i]) > 100) {
1267 $compacted[$key_table][$key_condition][$i][$value] = $value;
1268 $counter[$key_table][$key_condition] = $i;
1271 foreach ($compacted AS $table => $values) {
1272 foreach ($values AS $field => $field_value_list) {
1273 foreach ($field_value_list AS $field_values) {
1274 $sql = "DELETE FROM " . DBA::quoteIdentifier($table) . " WHERE " . DBA::quoteIdentifier($field) . " IN (" .
1275 substr(str_repeat("?, ", count($field_values)), 0, -2) . ");";
1277 $this->logger->debug($this->replaceParameters($sql, $field_values));
1279 if (!$this->e($sql, $field_values)) {
1280 if ($do_transaction) {
1288 if ($do_transaction) {
1295 * @brief Updates rows
1297 * Updates rows in the database. When $old_fields is set to an array,
1298 * the system will only do an update if the fields in that array changed.
1301 * Only the values in $old_fields are compared.
1302 * This is an intentional behaviour.
1305 * We include the timestamp field in $fields but not in $old_fields.
1306 * Then the row will only get the new timestamp when the other fields had changed.
1308 * When $old_fields is set to a boolean value the system will do this compare itself.
1309 * When $old_fields is set to "true" the system will do an insert if the row doesn't exists.
1312 * Only set $old_fields to a boolean value when you are sure that you will update a single row.
1313 * When you set $old_fields to "true" then $fields must contain all relevant fields!
1315 * @param string|array $table Table name or array [schema => table]
1316 * @param array $fields contains the fields that are updated
1317 * @param array $condition condition array with the key values
1318 * @param array|boolean $old_fields array with the old field values that are about to be replaced (true = update on duplicate)
1320 * @return boolean was the update successfull?
1321 * @throws \Exception
1323 public function update($table, $fields, $condition, $old_fields = [])
1325 if (empty($table) || empty($fields) || empty($condition)) {
1326 $this->logger->info('Table, fields and condition have to be set');
1330 $table_string = DBA::buildTableString($table);
1332 $condition_string = DBA::buildCondition($condition);
1334 if (is_bool($old_fields)) {
1335 $do_insert = $old_fields;
1337 $old_fields = $this->selectFirst($table, [], $condition);
1339 if (is_bool($old_fields)) {
1341 $values = array_merge($condition, $fields);
1342 return $this->insert($table, $values, $do_insert);
1348 $do_update = (count($old_fields) == 0);
1350 foreach ($old_fields AS $fieldname => $content) {
1351 if (isset($fields[$fieldname])) {
1352 if (($fields[$fieldname] == $content) && !is_null($content)) {
1353 unset($fields[$fieldname]);
1360 if (!$do_update || (count($fields) == 0)) {
1364 $sql = "UPDATE " . $table_string . " SET "
1365 . implode(" = ?, ", array_map([DBA::class, 'quoteIdentifier'], array_keys($fields))) . " = ?"
1366 . $condition_string;
1368 $params1 = array_values($fields);
1369 $params2 = array_values($condition);
1370 $params = array_merge_recursive($params1, $params2);
1372 return $this->e($sql, $params);
1376 * Retrieve a single record from a table and returns it in an associative array
1378 * @brief Retrieve a single record from a table
1380 * @param string $table
1381 * @param array $fields
1382 * @param array $condition
1383 * @param array $params
1385 * @return bool|array
1386 * @throws \Exception
1387 * @see $this->select
1389 public function selectFirst($table, array $fields = [], array $condition = [], $params = [])
1391 $params['limit'] = 1;
1392 $result = $this->select($table, $fields, $condition, $params);
1394 if (is_bool($result)) {
1397 $row = $this->fetch($result);
1398 $this->close($result);
1404 * @brief Select rows from a table and fills an array with the data
1406 * @param string|array $table Table name or array [schema => table]
1407 * @param array $fields Array of selected fields, empty for all
1408 * @param array $condition Array of fields for condition
1409 * @param array $params Array of several parameters
1411 * @return array Data array
1412 * @throws \Exception
1415 public function selectToArray(string $table, array $fields = [], array $condition = [], array $params = [])
1417 return $this->toArray($this->select($table, $fields, $condition, $params));
1421 * @brief Select rows from a table
1423 * @param string|array $table Table name or array [schema => table]
1424 * @param array $fields Array of selected fields, empty for all
1425 * @param array $condition Array of fields for condition
1426 * @param array $params Array of several parameters
1428 * @return boolean|object
1432 * $fields = array("id", "uri", "uid", "network");
1434 * $condition = array("uid" => 1, "network" => 'dspr');
1436 * $condition = array("`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr');
1438 * $params = array("order" => array("id", "received" => true), "limit" => 10);
1440 * $data = DBA::select($table, $fields, $condition, $params);
1441 * @throws \Exception
1443 public function select($table, array $fields = [], array $condition = [], array $params = [])
1445 if (empty($table)) {
1449 if (count($fields) > 0) {
1450 $select_string = implode(', ', array_map([DBA::class, 'quoteIdentifier'], $fields));
1452 $select_string = '*';
1455 $table_string = DBA::buildTableString($table);
1457 $condition_string = DBA::buildCondition($condition);
1459 $param_string = DBA::buildParameter($params);
1461 $sql = "SELECT " . $select_string . " FROM " . $table_string . $condition_string . $param_string;
1463 $result = $this->p($sql, $condition);
1469 * @brief Counts the rows from a table satisfying the provided condition
1471 * @param string|array $table Table name or array [schema => table]
1472 * @param array $condition Array of fields for condition
1473 * @param array $params Array of several parameters
1480 * $condition = ["uid" => 1, "network" => 'dspr'];
1482 * $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr'];
1484 * $count = DBA::count($table, $condition);
1485 * @throws \Exception
1487 public function count($table, array $condition = [], array $params = [])
1489 if (empty($table)) {
1493 $table_string = DBA::buildTableString($table);
1495 $condition_string = DBA::buildCondition($condition);
1497 if (empty($params['expression'])) {
1499 } elseif (!empty($params['distinct'])) {
1500 $expression = "DISTINCT " . DBA::quoteIdentifier($params['expression']);
1502 $expression = DBA::quoteIdentifier($params['expression']);
1505 $sql = "SELECT COUNT(" . $expression . ") AS `count` FROM " . $table_string . $condition_string;
1507 $row = $this->fetchFirst($sql, $condition);
1509 return $row['count'];
1513 * @brief Fills an array with data from a query
1515 * @param object $stmt statement object
1516 * @param bool $do_close
1518 * @return array Data array
1520 public function toArray($stmt, $do_close = true)
1522 if (is_bool($stmt)) {
1527 while ($row = $this->fetch($stmt)) {
1532 $this->close($stmt);
1539 * @brief Returns the error number of the last query
1541 * @return string Error number (0 if no error)
1543 public function errorNo()
1545 return $this->errorno;
1549 * @brief Returns the error message of the last query
1551 * @return string Error message ('' if no error)
1553 public function errorMessage()
1555 return $this->error;
1559 * @brief Closes the current statement
1561 * @param object $stmt statement object
1563 * @return boolean was the close successful?
1565 public function close($stmt)
1568 $stamp1 = microtime(true);
1570 if (!is_object($stmt)) {
1574 switch ($this->driver) {
1576 $ret = $stmt->closeCursor();
1579 // MySQLi offers both a mysqli_stmt and a mysqli_result class.
1580 // We should be careful not to assume the object type of $stmt
1581 // because DBA::p() has been able to return both types.
1582 if ($stmt instanceof mysqli_stmt) {
1583 $stmt->free_result();
1584 $ret = $stmt->close();
1585 } elseif ($stmt instanceof mysqli_result) {
1594 $this->profiler->saveTimestamp($stamp1, 'database', System::callstack());
1600 * @brief Return a list of database processes
1603 * 'list' => List of processes, separated in their different states
1604 * 'amount' => Number of concurrent database processes
1605 * @throws \Exception
1607 public function processlist()
1609 $ret = $this->p("SHOW PROCESSLIST");
1610 $data = $this->toArray($ret);
1614 foreach ($data as $process) {
1615 $state = trim($process["State"]);
1617 // Filter out all non blocking processes
1618 if (!in_array($state, ["", "init", "statistics", "updating"])) {
1625 foreach ($states as $state => $usage) {
1626 if ($statelist != "") {
1629 $statelist .= $state . ": " . $usage;
1631 return (["list" => $statelist, "amount" => $processes]);
1635 * Checks if $array is a filled array with at least one entry.
1637 * @param mixed $array A filled array with at least one entry
1639 * @return boolean Whether $array is a filled array or an object with rows
1641 public function isResult($array)
1643 // It could be a return value from an update statement
1644 if (is_bool($array)) {
1648 if (is_object($array)) {
1649 return $this->numRows($array) > 0;
1652 return (is_array($array) && (count($array) > 0));
1656 * @brief Callback function for "esc_array"
1658 * @param mixed $value Array value
1659 * @param string $key Array key
1660 * @param boolean $add_quotation add quotation marks for string values
1664 private function escapeArrayCallback(&$value, $key, $add_quotation)
1666 if (!$add_quotation) {
1667 if (is_bool($value)) {
1668 $value = ($value ? '1' : '0');
1670 $value = $this->escape($value);
1675 if (is_bool($value)) {
1676 $value = ($value ? 'true' : 'false');
1677 } elseif (is_float($value) || is_integer($value)) {
1678 $value = (string)$value;
1680 $value = "'" . $this->escape($value) . "'";
1685 * @brief Escapes a whole array
1687 * @param mixed $arr Array with values to be escaped
1688 * @param boolean $add_quotation add quotation marks for string values
1692 public function escapeArray(&$arr, $add_quotation = false)
1694 array_walk($arr, [$this, 'escapeArrayCallback'], $add_quotation);