3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Database;
24 use Friendica\Core\Config\Cache;
25 use Friendica\Core\System;
26 use Friendica\Network\HTTPException\InternalServerErrorException;
27 use Friendica\Util\DateTimeFormat;
28 use Friendica\Util\Profiler;
35 use Psr\Log\LoggerInterface;
38 * This class is for the low level database stuff that does driver specific things.
42 protected $connected = false;
47 protected $configCache;
53 * @var LoggerInterface
56 protected $server_info = '';
57 /** @var PDO|mysqli */
58 protected $connection;
60 private $error = false;
62 private $affected_rows = 0;
63 protected $in_transaction = false;
64 protected $in_retrial = false;
65 private $relation = [];
67 public function __construct(Cache $configCache, Profiler $profiler, LoggerInterface $logger, array $server = [])
69 // We are storing these values for being able to perform a reconnect
70 $this->configCache = $configCache;
71 $this->profiler = $profiler;
72 $this->logger = $logger;
74 $this->readServerVariables($server);
77 if ($this->isConnected()) {
78 // Loads DB_UPDATE_VERSION constant
79 DBStructure::definition($configCache->get('system', 'basepath'), false);
83 private function readServerVariables(array $server)
85 // Use environment variables for mysql if they are set beforehand
86 if (!empty($server['MYSQL_HOST'])
87 && (!empty($server['MYSQL_USERNAME'] || !empty($server['MYSQL_USER'])))
88 && $server['MYSQL_PASSWORD'] !== false
89 && !empty($server['MYSQL_DATABASE']))
91 $db_host = $server['MYSQL_HOST'];
92 if (!empty($server['MYSQL_PORT'])) {
93 $db_host .= ':' . $server['MYSQL_PORT'];
95 $this->configCache->set('database', 'hostname', $db_host);
97 if (!empty($server['MYSQL_USERNAME'])) {
98 $this->configCache->set('database', 'username', $server['MYSQL_USERNAME']);
100 $this->configCache->set('database', 'username', $server['MYSQL_USER']);
102 $this->configCache->set('database', 'password', (string) $server['MYSQL_PASSWORD']);
103 $this->configCache->set('database', 'database', $server['MYSQL_DATABASE']);
107 public function connect()
109 if (!is_null($this->connection) && $this->connected()) {
110 return $this->connected;
113 // Reset connected state
114 $this->connected = false;
117 $serveraddr = trim($this->configCache->get('database', 'hostname'));
118 $serverdata = explode(':', $serveraddr);
119 $server = $serverdata[0];
120 if (count($serverdata) > 1) {
121 $port = trim($serverdata[1]);
123 $server = trim($server);
124 $user = trim($this->configCache->get('database', 'username'));
125 $pass = trim($this->configCache->get('database', 'password'));
126 $db = trim($this->configCache->get('database', 'database'));
127 $charset = trim($this->configCache->get('database', 'charset'));
129 if (!(strlen($server) && strlen($user))) {
133 if (class_exists('\PDO') && in_array('mysql', PDO::getAvailableDrivers())) {
134 $this->driver = 'pdo';
135 $connect = "mysql:host=" . $server . ";dbname=" . $db;
138 $connect .= ";port=" . $port;
142 $connect .= ";charset=" . $charset;
146 $this->connection = @new PDO($connect, $user, $pass);
147 $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
148 $this->connected = true;
149 } catch (PDOException $e) {
150 $this->connected = false;
154 if (!$this->connected && class_exists('\mysqli')) {
155 $this->driver = 'mysqli';
158 $this->connection = @new mysqli($server, $user, $pass, $db, $port);
160 $this->connection = @new mysqli($server, $user, $pass, $db);
163 if (!mysqli_connect_errno()) {
164 $this->connected = true;
167 $this->connection->set_charset($charset);
172 // No suitable SQL driver was found.
173 if (!$this->connected) {
174 $this->driver = null;
175 $this->connection = null;
178 return $this->connected;
182 * Sets the logger for DBA
184 * @note this is necessary because if we want to load the logger configuration
185 * from the DB, but there's an error, we would print out an exception.
186 * So the logger gets updated after the logger configuration can be retrieved
189 * @param LoggerInterface $logger
191 public function setLogger(LoggerInterface $logger)
193 $this->logger = $logger;
197 * Sets the profiler for DBA
199 * @param Profiler $profiler
201 public function setProfiler(Profiler $profiler)
203 $this->profiler = $profiler;
206 * Disconnects the current database connection
208 public function disconnect()
210 if (!is_null($this->connection)) {
211 switch ($this->driver) {
213 $this->connection = null;
216 $this->connection->close();
217 $this->connection = null;
222 $this->driver = null;
223 $this->connected = false;
227 * Perform a reconnect of an existing database connection
229 public function reconnect()
232 return $this->connect();
236 * Return the database object.
240 public function getConnection()
242 return $this->connection;
246 * Returns the MySQL server version string
248 * This function discriminate between the deprecated mysql API and the current
249 * object-oriented mysqli API. Example of returned string: 5.5.46-0+deb8u1
253 public function serverInfo()
255 if ($this->server_info == '') {
256 switch ($this->driver) {
258 $this->server_info = $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION);
261 $this->server_info = $this->connection->server_info;
265 return $this->server_info;
269 * Returns the selected database name
274 public function databaseName()
276 $ret = $this->p("SELECT DATABASE() AS `db`");
277 $data = $this->toArray($ret);
278 return $data[0]['db'];
282 * Analyze a database query and log this if some conditions are met.
284 * @param string $query The database query that will be analyzed
288 private function logIndex($query)
291 if (!$this->configCache->get('system', 'db_log_index')) {
295 // Don't explain an explain statement
296 if (strtolower(substr($query, 0, 7)) == "explain") {
300 // Only do the explain on "select", "update" and "delete"
301 if (!in_array(strtolower(substr($query, 0, 6)), ["select", "update", "delete"])) {
305 $r = $this->p("EXPLAIN " . $query);
306 if (!$this->isResult($r)) {
310 $watchlist = explode(',', $this->configCache->get('system', 'db_log_index_watch'));
311 $blacklist = explode(',', $this->configCache->get('system', 'db_log_index_blacklist'));
313 while ($row = $this->fetch($r)) {
314 if ((intval($this->configCache->get('system', 'db_loglimit_index')) > 0)) {
315 $log = (in_array($row['key'], $watchlist) &&
316 ($row['rows'] >= intval($this->configCache->get('system', 'db_loglimit_index'))));
321 if ((intval($this->configCache->get('system', 'db_loglimit_index_high')) > 0) && ($row['rows'] >= intval($this->configCache->get('system', 'db_loglimit_index_high')))) {
325 if (in_array($row['key'], $blacklist) || ($row['key'] == "")) {
330 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
331 @file_put_contents($this->configCache->get('system', 'db_log_index'), DateTimeFormat::utcNow() . "\t" .
332 $row['key'] . "\t" . $row['rows'] . "\t" . $row['Extra'] . "\t" .
333 basename($backtrace[1]["file"]) . "\t" .
334 $backtrace[1]["line"] . "\t" . $backtrace[2]["function"] . "\t" .
335 substr($query, 0, 2000) . "\n", FILE_APPEND);
341 * Removes every not whitelisted character from the identifier string
343 * @param string $identifier
345 * @return string sanitized identifier
348 private function sanitizeIdentifier($identifier)
350 return preg_replace('/[^A-Za-z0-9_\-]+/', '', $identifier);
353 public function escape($str)
355 if ($this->connected) {
356 switch ($this->driver) {
358 return substr(@$this->connection->quote($str, PDO::PARAM_STR), 1, -1);
361 return @$this->connection->real_escape_string($str);
364 return str_replace("'", "\\'", $str);
368 public function isConnected()
370 return $this->connected;
373 public function connected()
377 if (is_null($this->connection)) {
381 switch ($this->driver) {
383 $r = $this->p("SELECT 1");
384 if ($this->isResult($r)) {
385 $row = $this->toArray($r);
386 $connected = ($row[0]['1'] == '1');
390 $connected = $this->connection->ping();
398 * Replaces ANY_VALUE() function by MIN() function,
399 * if the database server does not support ANY_VALUE().
401 * Considerations for Standard SQL, or MySQL with ONLY_FULL_GROUP_BY (default since 5.7.5).
402 * ANY_VALUE() is available from MySQL 5.7.5 https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html
403 * A standard fall-back is to use MIN().
405 * @param string $sql An SQL string without the values
407 * @return string The input SQL string modified if necessary.
409 public function anyValueFallback($sql)
411 $server_info = $this->serverInfo();
412 if (version_compare($server_info, '5.7.5', '<') ||
413 (stripos($server_info, 'MariaDB') !== false)) {
414 $sql = str_ireplace('ANY_VALUE(', 'MIN(', $sql);
420 * Replaces the ? placeholders with the parameters in the $args array
422 * @param string $sql SQL query
423 * @param array $args The parameters that are to replace the ? placeholders
425 * @return string The replaced SQL query
427 private function replaceParameters($sql, $args)
430 foreach ($args AS $param => $value) {
431 if (is_int($args[$param]) || is_float($args[$param])) {
432 $replace = intval($args[$param]);
434 $replace = "'" . $this->escape($args[$param]) . "'";
437 $pos = strpos($sql, '?', $offset);
438 if ($pos !== false) {
439 $sql = substr_replace($sql, $replace, $pos, 1);
441 $offset = $pos + strlen($replace);
447 * Executes a prepared statement that returns data
449 * @usage Example: $r = p("SELECT * FROM `item` WHERE `guid` = ?", $guid);
451 * Please only use it with complicated queries.
452 * For all regular queries please use DBA::select or DBA::exists
454 * @param string $sql SQL statement
456 * @return bool|object statement object or result object
459 public function p($sql)
462 $stamp1 = microtime(true);
464 $params = DBA::getParam(func_get_args());
466 // Renumber the array keys to be sure that they fit
469 foreach ($params AS $param) {
470 // Avoid problems with some MySQL servers and boolean values. See issue #3645
471 if (is_bool($param)) {
472 $param = (int)$param;
474 $args[++$i] = $param;
477 if (!$this->connected) {
481 if ((substr_count($sql, '?') != count($args)) && (count($args) > 0)) {
482 // Question: Should we continue or stop the query here?
483 $this->logger->warning('Query parameters mismatch.', ['query' => $sql, 'args' => $args, 'callstack' => System::callstack()]);
486 $sql = DBA::cleanQuery($sql);
487 $sql = $this->anyValueFallback($sql);
491 if ($this->configCache->get('system', 'db_callstack') !== null) {
492 $sql = "/*" . System::callstack() . " */ " . $sql;
497 $this->affected_rows = 0;
499 // We have to make some things different if this function is called from "e"
500 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
502 if (isset($trace[1])) {
503 $called_from = $trace[1];
505 // We use just something that is defined to avoid warnings
506 $called_from = $trace[0];
508 // We are having an own error logging in the function "e"
509 $called_from_e = ($called_from['function'] == 'e');
511 if (!isset($this->connection)) {
512 throw new InternalServerErrorException('The Connection is empty, although connected is set true.');
515 switch ($this->driver) {
517 // If there are no arguments we use "query"
518 if (count($args) == 0) {
519 if (!$retval = $this->connection->query($sql)) {
520 $errorInfo = $this->connection->errorInfo();
521 $this->error = $errorInfo[2];
522 $this->errorno = $errorInfo[1];
526 $this->affected_rows = $retval->rowCount();
530 /** @var $stmt mysqli_stmt|PDOStatement */
531 if (!$stmt = $this->connection->prepare($sql)) {
532 $errorInfo = $this->connection->errorInfo();
533 $this->error = $errorInfo[2];
534 $this->errorno = $errorInfo[1];
539 foreach ($args AS $param => $value) {
540 if (is_int($args[$param])) {
541 $data_type = PDO::PARAM_INT;
543 $data_type = PDO::PARAM_STR;
545 $stmt->bindParam($param, $args[$param], $data_type);
548 if (!$stmt->execute()) {
549 $errorInfo = $stmt->errorInfo();
550 $this->error = $errorInfo[2];
551 $this->errorno = $errorInfo[1];
555 $this->affected_rows = $retval->rowCount();
559 // There are SQL statements that cannot be executed with a prepared statement
560 $parts = explode(' ', $orig_sql);
561 $command = strtolower($parts[0]);
562 $can_be_prepared = in_array($command, ['select', 'update', 'insert', 'delete']);
564 // The fallback routine is called as well when there are no arguments
565 if (!$can_be_prepared || (count($args) == 0)) {
566 $retval = $this->connection->query($this->replaceParameters($sql, $args));
567 if ($this->connection->errno) {
568 $this->error = $this->connection->error;
569 $this->errorno = $this->connection->errno;
572 if (isset($retval->num_rows)) {
573 $this->affected_rows = $retval->num_rows;
575 $this->affected_rows = $this->connection->affected_rows;
581 $stmt = $this->connection->stmt_init();
583 if (!$stmt->prepare($sql)) {
584 $this->error = $stmt->error;
585 $this->errorno = $stmt->errno;
592 foreach ($args AS $param => $value) {
593 if (is_int($args[$param])) {
595 } elseif (is_float($args[$param])) {
597 } elseif (is_string($args[$param])) {
602 $values[] = &$args[$param];
605 if (count($values) > 0) {
606 array_unshift($values, $param_types);
607 call_user_func_array([$stmt, 'bind_param'], $values);
610 if (!$stmt->execute()) {
611 $this->error = $this->connection->error;
612 $this->errorno = $this->connection->errno;
615 $stmt->store_result();
617 $this->affected_rows = $retval->affected_rows;
622 // We are having an own error logging in the function "e"
623 if (($this->errorno != 0) && !$called_from_e) {
624 // We have to preserve the error code, somewhere in the logging it get lost
625 $error = $this->error;
626 $errorno = $this->errorno;
628 $this->logger->error('DB Error', [
629 'code' => $this->errorno,
630 'error' => $this->error,
631 'callstack' => System::callstack(8),
632 'params' => $this->replaceParameters($sql, $args),
635 // On a lost connection we try to reconnect - but only once.
636 if ($errorno == 2006) {
637 if ($this->in_retrial || !$this->reconnect()) {
638 // It doesn't make sense to continue when the database connection was lost
639 if ($this->in_retrial) {
640 $this->logger->notice('Giving up retrial because of database error', [
641 'code' => $this->errorno,
642 'error' => $this->error,
645 $this->logger->notice('Couldn\'t reconnect after database error', [
646 'code' => $this->errorno,
647 'error' => $this->error,
653 $this->logger->notice('Reconnected after database error', [
654 'code' => $this->errorno,
655 'error' => $this->error,
657 $this->in_retrial = true;
658 $ret = $this->p($sql, $args);
659 $this->in_retrial = false;
664 $this->error = $error;
665 $this->errorno = $errorno;
668 $this->profiler->saveTimestamp($stamp1, 'database', System::callstack());
670 if ($this->configCache->get('system', 'db_log')) {
671 $stamp2 = microtime(true);
672 $duration = (float)($stamp2 - $stamp1);
674 if (($duration > $this->configCache->get('system', 'db_loglimit'))) {
675 $duration = round($duration, 3);
676 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
678 @file_put_contents($this->configCache->get('system', 'db_log'), DateTimeFormat::utcNow() . "\t" . $duration . "\t" .
679 basename($backtrace[1]["file"]) . "\t" .
680 $backtrace[1]["line"] . "\t" . $backtrace[2]["function"] . "\t" .
681 substr($this->replaceParameters($sql, $args), 0, 2000) . "\n", FILE_APPEND);
688 * Executes a prepared statement like UPDATE or INSERT that doesn't return data
690 * Please use DBA::delete, DBA::insert, DBA::update, ... instead
692 * @param string $sql SQL statement
694 * @return boolean Was the query successfull? False is returned only if an error occurred
697 public function e($sql)
700 $stamp = microtime(true);
702 $params = DBA::getParam(func_get_args());
704 // In a case of a deadlock we are repeating the query 20 times
708 $stmt = $this->p($sql, $params);
710 if (is_bool($stmt)) {
712 } elseif (is_object($stmt)) {
720 } while (($this->errorno == 1213) && (--$timeout > 0));
722 if ($this->errorno != 0) {
723 // We have to preserve the error code, somewhere in the logging it get lost
724 $error = $this->error;
725 $errorno = $this->errorno;
727 $this->logger->error('DB Error', [
728 'code' => $this->errorno,
729 'error' => $this->error,
730 'callstack' => System::callstack(8),
731 'params' => $this->replaceParameters($sql, $params),
734 // On a lost connection we simply quit.
735 // A reconnect like in $this->p could be dangerous with modifications
736 if ($errorno == 2006) {
737 $this->logger->notice('Giving up because of database error', [
738 'code' => $this->errorno,
739 'error' => $this->error,
744 $this->error = $error;
745 $this->errorno = $errorno;
748 $this->profiler->saveTimestamp($stamp, "database_write", System::callstack());
754 * Check if data exists
756 * @param string|array $table Table name or array [schema => table]
757 * @param array $condition array of fields for condition
759 * @return boolean Are there rows for that condition?
762 public function exists($table, $condition)
770 if (empty($condition)) {
771 return DBStructure::existsTable($table);
775 $first_key = key($condition);
776 if (!is_int($first_key)) {
777 $fields = [$first_key];
780 $stmt = $this->select($table, $fields, $condition, ['limit' => 1]);
782 if (is_bool($stmt)) {
785 $retval = ($this->numRows($stmt) > 0);
794 * Fetches the first row
796 * Please use DBA::selectFirst or DBA::exists whenever this is possible.
798 * Fetches the first row
800 * @param string $sql SQL statement
802 * @return array first row of query
805 public function fetchFirst($sql)
807 $params = DBA::getParam(func_get_args());
809 $stmt = $this->p($sql, $params);
811 if (is_bool($stmt)) {
814 $retval = $this->fetch($stmt);
823 * Returns the number of affected rows of the last statement
825 * @return int Number of rows
827 public function affectedRows()
829 return $this->affected_rows;
833 * Returns the number of columns of a statement
835 * @param object Statement object
837 * @return int Number of columns
839 public function columnCount($stmt)
841 if (!is_object($stmt)) {
844 switch ($this->driver) {
846 return $stmt->columnCount();
848 return $stmt->field_count;
854 * Returns the number of rows of a statement
856 * @param PDOStatement|mysqli_result|mysqli_stmt Statement object
858 * @return int Number of rows
860 public function numRows($stmt)
862 if (!is_object($stmt)) {
865 switch ($this->driver) {
867 return $stmt->rowCount();
869 return $stmt->num_rows;
877 * @param mixed $stmt statement object
879 * @return array current row
881 public function fetch($stmt)
884 $stamp1 = microtime(true);
888 if (!is_object($stmt)) {
892 switch ($this->driver) {
894 $columns = $stmt->fetch(PDO::FETCH_ASSOC);
897 if (get_class($stmt) == 'mysqli_result') {
898 $columns = $stmt->fetch_assoc();
902 // This code works, but is slow
904 // Bind the result to a result array
908 for ($x = 0; $x < $stmt->field_count; $x++) {
909 $cols[] = &$cols_num[$x];
912 call_user_func_array([$stmt, 'bind_result'], $cols);
914 if (!$stmt->fetch()) {
919 // We need to get the field names for the array keys
920 // It seems that there is no better way to do this.
921 $result = $stmt->result_metadata();
922 $fields = $result->fetch_fields();
924 foreach ($cols_num AS $param => $col) {
925 $columns[$fields[$param]->name] = $col;
929 $this->profiler->saveTimestamp($stamp1, 'database', System::callstack());
935 * Insert a row into a table
937 * @param string|array $table Table name or array [schema => table]
938 * @param array $param parameter array
939 * @param bool $on_duplicate_update Do an update on a duplicate entry
941 * @return boolean was the insert successful?
944 public function insert($table, $param, $on_duplicate_update = false)
946 if (empty($table) || empty($param)) {
947 $this->logger->info('Table and fields have to be set');
951 $table_string = DBA::buildTableString($table);
953 $fields_string = implode(', ', array_map([DBA::class, 'quoteIdentifier'], array_keys($param)));
955 $values_string = substr(str_repeat("?, ", count($param)), 0, -2);
957 $sql = "INSERT INTO " . $table_string . " (" . $fields_string . ") VALUES (" . $values_string . ")";
959 if ($on_duplicate_update) {
960 $fields_string = implode(' = ?, ', array_map([DBA::class, 'quoteIdentifier'], array_keys($param)));
962 $sql .= " ON DUPLICATE KEY UPDATE " . $fields_string . " = ?";
964 $values = array_values($param);
965 $param = array_merge_recursive($values, $values);
968 return $this->e($sql, $param);
972 * Fetch the id of the last insert command
974 * @return integer Last inserted id
976 public function lastInsertId()
978 switch ($this->driver) {
980 $id = $this->connection->lastInsertId();
983 $id = $this->connection->insert_id;
990 * Locks a table for exclusive write access
992 * This function can be extended in the future to accept a table array as well.
994 * @param string|array $table Table name or array [schema => table]
996 * @return boolean was the lock successful?
999 public function lock($table)
1001 // See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
1002 if ($this->driver == 'pdo') {
1003 $this->e("SET autocommit=0");
1004 $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
1006 $this->connection->autocommit(false);
1009 $success = $this->e("LOCK TABLES " . DBA::buildTableString($table) . " WRITE");
1011 if ($this->driver == 'pdo') {
1012 $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
1016 if ($this->driver == 'pdo') {
1017 $this->e("SET autocommit=1");
1019 $this->connection->autocommit(true);
1022 $this->in_transaction = true;
1028 * Unlocks all locked tables
1030 * @return boolean was the unlock successful?
1031 * @throws \Exception
1033 public function unlock()
1035 // See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
1036 $this->performCommit();
1038 if ($this->driver == 'pdo') {
1039 $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
1042 $success = $this->e("UNLOCK TABLES");
1044 if ($this->driver == 'pdo') {
1045 $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
1046 $this->e("SET autocommit=1");
1048 $this->connection->autocommit(true);
1051 $this->in_transaction = false;
1056 * Starts a transaction
1058 * @return boolean Was the command executed successfully?
1060 public function transaction()
1062 if (!$this->performCommit()) {
1066 switch ($this->driver) {
1068 if (!$this->connection->inTransaction() && !$this->connection->beginTransaction()) {
1074 if (!$this->connection->begin_transaction()) {
1080 $this->in_transaction = true;
1084 protected function performCommit()
1086 switch ($this->driver) {
1088 if (!$this->connection->inTransaction()) {
1092 return $this->connection->commit();
1095 return $this->connection->commit();
1104 * @return boolean Was the command executed successfully?
1106 public function commit()
1108 if (!$this->performCommit()) {
1111 $this->in_transaction = false;
1118 * @return boolean Was the command executed successfully?
1120 public function rollback()
1124 switch ($this->driver) {
1126 if (!$this->connection->inTransaction()) {
1130 $ret = $this->connection->rollBack();
1134 $ret = $this->connection->rollback();
1137 $this->in_transaction = false;
1142 * Build the array with the table relations
1144 * The array is build from the database definitions in DBStructure.php
1146 * This process must only be started once, since the value is cached.
1148 private function buildRelationData()
1150 $definition = DBStructure::definition($this->configCache->get('system', 'basepath'));
1152 foreach ($definition AS $table => $structure) {
1153 foreach ($structure['fields'] AS $field => $field_struct) {
1154 if (isset($field_struct['relation'])) {
1155 foreach ($field_struct['relation'] AS $rel_table => $rel_field) {
1156 $this->relation[$rel_table][$rel_field][$table][] = $field;
1164 * Delete a row from a table
1166 * Note: this methods does NOT accept schema => table arrays because of the complex relation stuff.
1168 * @param string $table Table name
1169 * @param array $conditions Field condition(s)
1170 * @param array $options
1171 * - cascade: If true we delete records in other tables that depend on the one we're deleting through
1172 * relations (default: true)
1173 * @param array $callstack Internal use: prevent endless loops
1175 * @return boolean was the delete successful?
1176 * @throws \Exception
1178 public function delete($table, array $conditions, array $options = [], array &$callstack = [])
1180 if (empty($table) || empty($conditions)) {
1181 $this->logger->info('Table and conditions have to be set');
1187 // Create a key for the loop prevention
1188 $key = $table . ':' . json_encode($conditions);
1190 // We quit when this key already exists in the callstack.
1191 if (isset($callstack[$key])) {
1195 $callstack[$key] = true;
1197 $commands[$key] = ['table' => $table, 'conditions' => $conditions];
1199 // Don't use "defaults" here, since it would set "false" to "true"
1200 if (isset($options['cascade'])) {
1201 $cascade = $options['cascade'];
1206 // To speed up the whole process we cache the table relations
1207 if ($cascade && count($this->relation) == 0) {
1208 $this->buildRelationData();
1211 // Is there a relation entry for the table?
1212 if ($cascade && isset($this->relation[$table])) {
1213 // We only allow a simple "one field" relation.
1214 $field = array_keys($this->relation[$table])[0];
1215 $rel_def = array_values($this->relation[$table])[0];
1217 // Create a key for preventing double queries
1218 $qkey = $field . '-' . $table . ':' . json_encode($conditions);
1220 // When the search field is the relation field, we don't need to fetch the rows
1221 // This is useful when the leading record is already deleted in the frontend but the rest is done in the backend
1222 if ((count($conditions) == 1) && ($field == array_keys($conditions)[0])) {
1223 foreach ($rel_def AS $rel_table => $rel_fields) {
1224 foreach ($rel_fields AS $rel_field) {
1225 $this->delete($rel_table, [$rel_field => array_values($conditions)[0]], $options, $callstack);
1228 // We quit when this key already exists in the callstack.
1229 } elseif (!isset($callstack[$qkey])) {
1230 $callstack[$qkey] = true;
1232 // Fetch all rows that are to be deleted
1233 $data = $this->select($table, [$field], $conditions);
1235 while ($row = $this->fetch($data)) {
1236 $this->delete($table, [$field => $row[$field]], $options, $callstack);
1239 $this->close($data);
1241 // Since we had split the delete command we don't need the original command anymore
1242 unset($commands[$key]);
1246 // Now we finalize the process
1247 $do_transaction = !$this->in_transaction;
1249 if ($do_transaction) {
1250 $this->transaction();
1256 foreach ($commands AS $command) {
1257 $conditions = $command['conditions'];
1259 $first_key = key($conditions);
1261 $condition_string = DBA::buildCondition($conditions);
1263 if ((count($command['conditions']) > 1) || is_int($first_key)) {
1264 $sql = "DELETE FROM " . DBA::quoteIdentifier($command['table']) . " " . $condition_string;
1265 $this->logger->debug($this->replaceParameters($sql, $conditions));
1267 if (!$this->e($sql, $conditions)) {
1268 if ($do_transaction) {
1274 $key_table = $command['table'];
1275 $key_condition = array_keys($command['conditions'])[0];
1276 $value = array_values($command['conditions'])[0];
1278 // Split the SQL queries in chunks of 100 values
1279 // We do the $i stuff here to make the code better readable
1280 $i = isset($counter[$key_table][$key_condition]) ? $counter[$key_table][$key_condition] : 0;
1281 if (isset($compacted[$key_table][$key_condition][$i]) && count($compacted[$key_table][$key_condition][$i]) > 100) {
1285 $compacted[$key_table][$key_condition][$i][$value] = $value;
1286 $counter[$key_table][$key_condition] = $i;
1289 foreach ($compacted AS $table => $values) {
1290 foreach ($values AS $field => $field_value_list) {
1291 foreach ($field_value_list AS $field_values) {
1292 $sql = "DELETE FROM " . DBA::quoteIdentifier($table) . " WHERE " . DBA::quoteIdentifier($field) . " IN (" .
1293 substr(str_repeat("?, ", count($field_values)), 0, -2) . ");";
1295 $this->logger->debug($this->replaceParameters($sql, $field_values));
1297 if (!$this->e($sql, $field_values)) {
1298 if ($do_transaction) {
1306 if ($do_transaction) {
1315 * Updates rows in the database. When $old_fields is set to an array,
1316 * the system will only do an update if the fields in that array changed.
1319 * Only the values in $old_fields are compared.
1320 * This is an intentional behaviour.
1323 * We include the timestamp field in $fields but not in $old_fields.
1324 * Then the row will only get the new timestamp when the other fields had changed.
1326 * When $old_fields is set to a boolean value the system will do this compare itself.
1327 * When $old_fields is set to "true" the system will do an insert if the row doesn't exists.
1330 * Only set $old_fields to a boolean value when you are sure that you will update a single row.
1331 * When you set $old_fields to "true" then $fields must contain all relevant fields!
1333 * @param string|array $table Table name or array [schema => table]
1334 * @param array $fields contains the fields that are updated
1335 * @param array $condition condition array with the key values
1336 * @param array|boolean $old_fields array with the old field values that are about to be replaced (true = update on duplicate)
1338 * @return boolean was the update successfull?
1339 * @throws \Exception
1341 public function update($table, $fields, $condition, $old_fields = [])
1343 if (empty($table) || empty($fields) || empty($condition)) {
1344 $this->logger->info('Table, fields and condition have to be set');
1348 if (is_bool($old_fields)) {
1349 $do_insert = $old_fields;
1351 $old_fields = $this->selectFirst($table, [], $condition);
1353 if (is_bool($old_fields)) {
1355 $values = array_merge($condition, $fields);
1356 return $this->insert($table, $values, $do_insert);
1362 foreach ($old_fields AS $fieldname => $content) {
1363 if (isset($fields[$fieldname]) && !is_null($content) && ($fields[$fieldname] == $content)) {
1364 unset($fields[$fieldname]);
1368 if (count($fields) == 0) {
1372 $table_string = DBA::buildTableString($table);
1374 $condition_string = DBA::buildCondition($condition);
1376 $sql = "UPDATE " . $table_string . " SET "
1377 . implode(" = ?, ", array_map([DBA::class, 'quoteIdentifier'], array_keys($fields))) . " = ?"
1378 . $condition_string;
1380 // Combines the updated fields parameter values with the condition parameter values
1381 $params = array_merge(array_values($fields), $condition);
1383 return $this->e($sql, $params);
1387 * Retrieve a single record from a table and returns it in an associative array
1389 * @param string|array $table
1390 * @param array $fields
1391 * @param array $condition
1392 * @param array $params
1394 * @return bool|array
1395 * @throws \Exception
1396 * @see $this->select
1398 public function selectFirst($table, array $fields = [], array $condition = [], $params = [])
1400 $params['limit'] = 1;
1401 $result = $this->select($table, $fields, $condition, $params);
1403 if (is_bool($result)) {
1406 $row = $this->fetch($result);
1407 $this->close($result);
1413 * Select rows from a table and fills an array with the data
1415 * @param string|array $table Table name or array [schema => table]
1416 * @param array $fields Array of selected fields, empty for all
1417 * @param array $condition Array of fields for condition
1418 * @param array $params Array of several parameters
1420 * @return array Data array
1421 * @throws \Exception
1424 public function selectToArray($table, array $fields = [], array $condition = [], array $params = [])
1426 return $this->toArray($this->select($table, $fields, $condition, $params));
1430 * Select rows from a table
1432 * @param string|array $table Table name or array [schema => table]
1433 * @param array $fields Array of selected fields, empty for all
1434 * @param array $condition Array of fields for condition
1435 * @param array $params Array of several parameters
1437 * @return boolean|object
1441 * $fields = array("id", "uri", "uid", "network");
1443 * $condition = array("uid" => 1, "network" => 'dspr');
1445 * $condition = array("`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr');
1447 * $params = array("order" => array("id", "received" => true), "limit" => 10);
1449 * $data = DBA::select($table, $fields, $condition, $params);
1450 * @throws \Exception
1452 public function select($table, array $fields = [], array $condition = [], array $params = [])
1454 if (empty($table)) {
1458 if (count($fields) > 0) {
1459 $select_string = implode(', ', array_map([DBA::class, 'quoteIdentifier'], $fields));
1461 $select_string = '*';
1464 $table_string = DBA::buildTableString($table);
1466 $condition_string = DBA::buildCondition($condition);
1468 $param_string = DBA::buildParameter($params);
1470 $sql = "SELECT " . $select_string . " FROM " . $table_string . $condition_string . $param_string;
1472 $result = $this->p($sql, $condition);
1478 * Counts the rows from a table satisfying the provided condition
1480 * @param string|array $table Table name or array [schema => table]
1481 * @param array $condition Array of fields for condition
1482 * @param array $params Array of several parameters
1489 * $condition = ["uid" => 1, "network" => 'dspr'];
1491 * $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr'];
1493 * $count = DBA::count($table, $condition);
1494 * @throws \Exception
1496 public function count($table, array $condition = [], array $params = [])
1498 if (empty($table)) {
1502 $table_string = DBA::buildTableString($table);
1504 $condition_string = DBA::buildCondition($condition);
1506 if (empty($params['expression'])) {
1508 } elseif (!empty($params['distinct'])) {
1509 $expression = "DISTINCT " . DBA::quoteIdentifier($params['expression']);
1511 $expression = DBA::quoteIdentifier($params['expression']);
1514 $sql = "SELECT COUNT(" . $expression . ") AS `count` FROM " . $table_string . $condition_string;
1516 $row = $this->fetchFirst($sql, $condition);
1518 return $row['count'];
1522 * Fills an array with data from a query
1524 * @param object $stmt statement object
1525 * @param bool $do_close
1527 * @return array Data array
1529 public function toArray($stmt, $do_close = true)
1531 if (is_bool($stmt)) {
1536 while ($row = $this->fetch($stmt)) {
1541 $this->close($stmt);
1548 * Returns the error number of the last query
1550 * @return string Error number (0 if no error)
1552 public function errorNo()
1554 return $this->errorno;
1558 * Returns the error message of the last query
1560 * @return string Error message ('' if no error)
1562 public function errorMessage()
1564 return $this->error;
1568 * Closes the current statement
1570 * @param object $stmt statement object
1572 * @return boolean was the close successful?
1574 public function close($stmt)
1577 $stamp1 = microtime(true);
1579 if (!is_object($stmt)) {
1583 switch ($this->driver) {
1585 $ret = $stmt->closeCursor();
1588 // MySQLi offers both a mysqli_stmt and a mysqli_result class.
1589 // We should be careful not to assume the object type of $stmt
1590 // because DBA::p() has been able to return both types.
1591 if ($stmt instanceof mysqli_stmt) {
1592 $stmt->free_result();
1593 $ret = $stmt->close();
1594 } elseif ($stmt instanceof mysqli_result) {
1603 $this->profiler->saveTimestamp($stamp1, 'database', System::callstack());
1609 * Return a list of database processes
1612 * 'list' => List of processes, separated in their different states
1613 * 'amount' => Number of concurrent database processes
1614 * @throws \Exception
1616 public function processlist()
1618 $ret = $this->p("SHOW PROCESSLIST");
1619 $data = $this->toArray($ret);
1623 foreach ($data as $process) {
1624 $state = trim($process["State"]);
1626 // Filter out all non blocking processes
1627 if (!in_array($state, ["", "init", "statistics", "updating"])) {
1634 foreach ($states as $state => $usage) {
1635 if ($statelist != "") {
1638 $statelist .= $state . ": " . $usage;
1640 return (["list" => $statelist, "amount" => $processes]);
1644 * Checks if $array is a filled array with at least one entry.
1646 * @param mixed $array A filled array with at least one entry
1648 * @return boolean Whether $array is a filled array or an object with rows
1650 public function isResult($array)
1652 // It could be a return value from an update statement
1653 if (is_bool($array)) {
1657 if (is_object($array)) {
1658 return $this->numRows($array) > 0;
1661 return (is_array($array) && (count($array) > 0));
1665 * Callback function for "esc_array"
1667 * @param mixed $value Array value
1668 * @param string $key Array key
1669 * @param boolean $add_quotation add quotation marks for string values
1673 private function escapeArrayCallback(&$value, $key, $add_quotation)
1675 if (!$add_quotation) {
1676 if (is_bool($value)) {
1677 $value = ($value ? '1' : '0');
1679 $value = $this->escape($value);
1684 if (is_bool($value)) {
1685 $value = ($value ? 'true' : 'false');
1686 } elseif (is_float($value) || is_integer($value)) {
1687 $value = (string)$value;
1689 $value = "'" . $this->escape($value) . "'";
1694 * Escapes a whole array
1696 * @param mixed $arr Array with values to be escaped
1697 * @param boolean $add_quotation add quotation marks for string values
1701 public function escapeArray(&$arr, $add_quotation = false)
1703 array_walk($arr, [$this, 'escapeArrayCallback'], $add_quotation);