3 namespace Friendica\Database;
5 use Friendica\Core\Config\Cache\IConfigCache;
6 use Friendica\Core\Logger;
7 use Friendica\Core\System;
8 use Friendica\Util\DateTimeFormat;
9 use Friendica\Util\Profiler;
18 * @class MySQL database class
20 * This class is for the low level database stuff that does driver specific things.
25 * Lowest possible date value
27 const NULL_DATE = '0001-01-01';
29 * Lowest possible datetime value
31 const NULL_DATETIME = '0001-01-01 00:00:00';
33 public static $connected = false;
38 private static $configCache;
42 private static $profiler;
43 private static $server_info = '';
44 private static $connection;
45 private static $driver;
46 private static $error = false;
47 private static $errorno = 0;
48 private static $affected_rows = 0;
49 private static $in_transaction = false;
50 private static $in_retrial = false;
51 private static $relation = [];
52 private static $db_serveraddr = '';
53 private static $db_user = '';
54 private static $db_pass = '';
55 private static $db_name = '';
56 private static $db_charset = '';
58 public static function connect(IConfigCache $configCache, Profiler $profiler, $serveraddr, $user, $pass, $db, $charset = null)
60 if (!is_null(self::$connection) && self::connected()) {
64 // We are storing these values for being able to perform a reconnect
65 self::$configCache = $configCache;
66 self::$profiler = $profiler;
67 self::$db_serveraddr = $serveraddr;
68 self::$db_user = $user;
69 self::$db_pass = $pass;
71 self::$db_charset = $charset;
74 $serveraddr = trim($serveraddr);
76 $serverdata = explode(':', $serveraddr);
77 $server = $serverdata[0];
79 if (count($serverdata) > 1) {
80 $port = trim($serverdata[1]);
83 $server = trim($server);
87 $charset = trim($charset);
89 if (!(strlen($server) && strlen($user))) {
93 if (class_exists('\PDO') && in_array('mysql', PDO::getAvailableDrivers())) {
94 self::$driver = 'pdo';
95 $connect = "mysql:host=".$server.";dbname=".$db;
98 $connect .= ";port=".$port;
102 $connect .= ";charset=".$charset;
106 self::$connection = @new PDO($connect, $user, $pass);
107 self::$connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
108 self::$connected = true;
109 } catch (PDOException $e) {
110 /// @TODO At least log exception, don't ignore it!
114 if (!self::$connected && class_exists('\mysqli')) {
115 self::$driver = 'mysqli';
118 self::$connection = @new mysqli($server, $user, $pass, $db, $port);
120 self::$connection = @new mysqli($server, $user, $pass, $db);
123 if (!mysqli_connect_errno()) {
124 self::$connected = true;
127 self::$connection->set_charset($charset);
132 // No suitable SQL driver was found.
133 if (!self::$connected) {
134 self::$driver = null;
135 self::$connection = null;
138 return self::$connected;
142 * Disconnects the current database connection
144 public static function disconnect()
146 if (is_null(self::$connection)) {
150 switch (self::$driver) {
152 self::$connection = null;
155 self::$connection->close();
156 self::$connection = null;
162 * Perform a reconnect of an existing database connection
164 public static function reconnect() {
167 $ret = self::connect(self::$configCache, self::$profiler, self::$db_serveraddr, self::$db_user, self::$db_pass, self::$db_name, self::$db_charset);
172 * Return the database object.
175 public static function getConnection()
177 return self::$connection;
181 * @brief Returns the MySQL server version string
183 * This function discriminate between the deprecated mysql API and the current
184 * object-oriented mysqli API. Example of returned string: 5.5.46-0+deb8u1
188 public static function serverInfo() {
189 if (self::$server_info == '') {
190 switch (self::$driver) {
192 self::$server_info = self::$connection->getAttribute(PDO::ATTR_SERVER_VERSION);
195 self::$server_info = self::$connection->server_info;
199 return self::$server_info;
203 * @brief Returns the selected database name
208 public static function databaseName() {
209 $ret = self::p("SELECT DATABASE() AS `db`");
210 $data = self::toArray($ret);
211 return $data[0]['db'];
215 * @brief Analyze a database query and log this if some conditions are met.
217 * @param string $query The database query that will be analyzed
220 private static function logIndex($query) {
222 if (!self::$configCache->get('system', 'db_log_index')) {
226 // Don't explain an explain statement
227 if (strtolower(substr($query, 0, 7)) == "explain") {
231 // Only do the explain on "select", "update" and "delete"
232 if (!in_array(strtolower(substr($query, 0, 6)), ["select", "update", "delete"])) {
236 $r = self::p("EXPLAIN ".$query);
237 if (!self::isResult($r)) {
241 $watchlist = explode(',', self::$configCache->get('system', 'db_log_index_watch'));
242 $blacklist = explode(',', self::$configCache->get('system', 'db_log_index_blacklist'));
244 while ($row = self::fetch($r)) {
245 if ((intval(self::$configCache->get('system', 'db_loglimit_index')) > 0)) {
246 $log = (in_array($row['key'], $watchlist) &&
247 ($row['rows'] >= intval(self::$configCache->get('system', 'db_loglimit_index'))));
252 if ((intval(self::$configCache->get('system', 'db_loglimit_index_high')) > 0) && ($row['rows'] >= intval(self::$configCache->get('system', 'db_loglimit_index_high')))) {
256 if (in_array($row['key'], $blacklist) || ($row['key'] == "")) {
261 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
262 @file_put_contents(self::$configCache->get('system', 'db_log_index'), DateTimeFormat::utcNow()."\t".
263 $row['key']."\t".$row['rows']."\t".$row['Extra']."\t".
264 basename($backtrace[1]["file"])."\t".
265 $backtrace[1]["line"]."\t".$backtrace[2]["function"]."\t".
266 substr($query, 0, 2000)."\n", FILE_APPEND);
271 public static function escape($str) {
272 if (self::$connected) {
273 switch (self::$driver) {
275 return substr(@self::$connection->quote($str, PDO::PARAM_STR), 1, -1);
278 return @self::$connection->real_escape_string($str);
281 return str_replace("'", "\\'", $str);
285 public static function connected() {
288 if (is_null(self::$connection)) {
292 switch (self::$driver) {
294 $r = self::p("SELECT 1");
295 if (self::isResult($r)) {
296 $row = self::toArray($r);
297 $connected = ($row[0]['1'] == '1');
301 $connected = self::$connection->ping();
308 * @brief Replaces ANY_VALUE() function by MIN() function,
309 * if the database server does not support ANY_VALUE().
311 * Considerations for Standard SQL, or MySQL with ONLY_FULL_GROUP_BY (default since 5.7.5).
312 * ANY_VALUE() is available from MySQL 5.7.5 https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html
313 * A standard fall-back is to use MIN().
315 * @param string $sql An SQL string without the values
316 * @return string The input SQL string modified if necessary.
318 public static function anyValueFallback($sql) {
319 $server_info = self::serverInfo();
320 if (version_compare($server_info, '5.7.5', '<') ||
321 (stripos($server_info, 'MariaDB') !== false)) {
322 $sql = str_ireplace('ANY_VALUE(', 'MIN(', $sql);
328 * @brief beautifies the query - useful for "SHOW PROCESSLIST"
330 * This is safe when we bind the parameters later.
331 * The parameter values aren't part of the SQL.
333 * @param string $sql An SQL string without the values
334 * @return string The input SQL string modified if necessary.
336 public static function cleanQuery($sql) {
337 $search = ["\t", "\n", "\r", " "];
338 $replace = [' ', ' ', ' ', ' '];
341 $sql = str_replace($search, $replace, $sql);
342 } while ($oldsql != $sql);
349 * @brief Replaces the ? placeholders with the parameters in the $args array
351 * @param string $sql SQL query
352 * @param array $args The parameters that are to replace the ? placeholders
353 * @return string The replaced SQL query
355 private static function replaceParameters($sql, $args) {
357 foreach ($args AS $param => $value) {
358 if (is_int($args[$param]) || is_float($args[$param])) {
359 $replace = intval($args[$param]);
361 $replace = "'".self::escape($args[$param])."'";
364 $pos = strpos($sql, '?', $offset);
365 if ($pos !== false) {
366 $sql = substr_replace($sql, $replace, $pos, 1);
368 $offset = $pos + strlen($replace);
374 * @brief Convert parameter array to an universal form
375 * @param array $args Parameter array
376 * @return array universalized parameter array
378 private static function getParam($args) {
381 // When the second function parameter is an array then use this as the parameter array
382 if ((count($args) > 0) && (is_array($args[1]))) {
390 * @brief Executes a prepared statement that returns data
391 * @usage Example: $r = p("SELECT * FROM `item` WHERE `guid` = ?", $guid);
393 * Please only use it with complicated queries.
394 * For all regular queries please use DBA::select or DBA::exists
396 * @param string $sql SQL statement
397 * @return bool|object statement object or result object
400 public static function p($sql) {
402 $stamp1 = microtime(true);
404 $params = self::getParam(func_get_args());
406 // Renumber the array keys to be sure that they fit
409 foreach ($params AS $param) {
410 // Avoid problems with some MySQL servers and boolean values. See issue #3645
411 if (is_bool($param)) {
412 $param = (int)$param;
414 $args[++$i] = $param;
417 if (!self::$connected) {
421 if ((substr_count($sql, '?') != count($args)) && (count($args) > 0)) {
422 // Question: Should we continue or stop the query here?
423 Logger::log('Parameter mismatch. Query "'.$sql.'" - Parameters '.print_r($args, true), Logger::DEBUG);
426 $sql = self::cleanQuery($sql);
427 $sql = self::anyValueFallback($sql);
431 if (self::$configCache->get('system', 'db_callstack') !== null) {
432 $sql = "/*".System::callstack()." */ ".$sql;
437 self::$affected_rows = 0;
439 // We have to make some things different if this function is called from "e"
440 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
442 if (isset($trace[1])) {
443 $called_from = $trace[1];
445 // We use just something that is defined to avoid warnings
446 $called_from = $trace[0];
448 // We are having an own error logging in the function "e"
449 $called_from_e = ($called_from['function'] == 'e');
451 switch (self::$driver) {
453 // If there are no arguments we use "query"
454 if (count($args) == 0) {
455 if (!$retval = self::$connection->query($sql)) {
456 $errorInfo = self::$connection->errorInfo();
457 self::$error = $errorInfo[2];
458 self::$errorno = $errorInfo[1];
462 self::$affected_rows = $retval->rowCount();
466 if (!$stmt = self::$connection->prepare($sql)) {
467 $errorInfo = self::$connection->errorInfo();
468 self::$error = $errorInfo[2];
469 self::$errorno = $errorInfo[1];
474 foreach ($args AS $param => $value) {
475 if (is_int($args[$param])) {
476 $data_type = PDO::PARAM_INT;
478 $data_type = PDO::PARAM_STR;
480 $stmt->bindParam($param, $args[$param], $data_type);
483 if (!$stmt->execute()) {
484 $errorInfo = $stmt->errorInfo();
485 self::$error = $errorInfo[2];
486 self::$errorno = $errorInfo[1];
490 self::$affected_rows = $retval->rowCount();
494 // There are SQL statements that cannot be executed with a prepared statement
495 $parts = explode(' ', $orig_sql);
496 $command = strtolower($parts[0]);
497 $can_be_prepared = in_array($command, ['select', 'update', 'insert', 'delete']);
499 // The fallback routine is called as well when there are no arguments
500 if (!$can_be_prepared || (count($args) == 0)) {
501 $retval = self::$connection->query(self::replaceParameters($sql, $args));
502 if (self::$connection->errno) {
503 self::$error = self::$connection->error;
504 self::$errorno = self::$connection->errno;
507 if (isset($retval->num_rows)) {
508 self::$affected_rows = $retval->num_rows;
510 self::$affected_rows = self::$connection->affected_rows;
516 $stmt = self::$connection->stmt_init();
518 if (!$stmt->prepare($sql)) {
519 self::$error = $stmt->error;
520 self::$errorno = $stmt->errno;
527 foreach ($args AS $param => $value) {
528 if (is_int($args[$param])) {
530 } elseif (is_float($args[$param])) {
532 } elseif (is_string($args[$param])) {
537 $values[] = &$args[$param];
540 if (count($values) > 0) {
541 array_unshift($values, $param_types);
542 call_user_func_array([$stmt, 'bind_param'], $values);
545 if (!$stmt->execute()) {
546 self::$error = self::$connection->error;
547 self::$errorno = self::$connection->errno;
550 $stmt->store_result();
552 self::$affected_rows = $retval->affected_rows;
557 // We are having an own error logging in the function "e"
558 if ((self::$errorno != 0) && !$called_from_e) {
559 // We have to preserve the error code, somewhere in the logging it get lost
560 $error = self::$error;
561 $errorno = self::$errorno;
563 Logger::log('DB Error '.self::$errorno.': '.self::$error."\n".
564 System::callstack(8)."\n".self::replaceParameters($sql, $args));
566 // On a lost connection we try to reconnect - but only once.
567 if ($errorno == 2006) {
568 if (self::$in_retrial || !self::reconnect()) {
569 // It doesn't make sense to continue when the database connection was lost
570 if (self::$in_retrial) {
571 Logger::log('Giving up retrial because of database error '.$errorno.': '.$error);
573 Logger::log("Couldn't reconnect after database error ".$errorno.': '.$error);
578 Logger::log('Reconnected after database error '.$errorno.': '.$error);
579 self::$in_retrial = true;
580 $ret = self::p($sql, $args);
581 self::$in_retrial = false;
586 self::$error = $error;
587 self::$errorno = $errorno;
590 self::$profiler->saveTimestamp($stamp1, 'database', System::callstack());
592 if (self::$configCache->get('system', 'db_log')) {
593 $stamp2 = microtime(true);
594 $duration = (float)($stamp2 - $stamp1);
596 if (($duration > self::$configCache->get('system', 'db_loglimit'))) {
597 $duration = round($duration, 3);
598 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
600 @file_put_contents(self::$configCache->get('system', 'db_log'), DateTimeFormat::utcNow()."\t".$duration."\t".
601 basename($backtrace[1]["file"])."\t".
602 $backtrace[1]["line"]."\t".$backtrace[2]["function"]."\t".
603 substr(self::replaceParameters($sql, $args), 0, 2000)."\n", FILE_APPEND);
610 * @brief Executes a prepared statement like UPDATE or INSERT that doesn't return data
612 * Please use DBA::delete, DBA::insert, DBA::update, ... instead
614 * @param string $sql SQL statement
615 * @return boolean Was the query successfull? False is returned only if an error occurred
618 public static function e($sql) {
620 $stamp = microtime(true);
622 $params = self::getParam(func_get_args());
624 // In a case of a deadlock we are repeating the query 20 times
628 $stmt = self::p($sql, $params);
630 if (is_bool($stmt)) {
632 } elseif (is_object($stmt)) {
640 } while ((self::$errorno == 1213) && (--$timeout > 0));
642 if (self::$errorno != 0) {
643 // We have to preserve the error code, somewhere in the logging it get lost
644 $error = self::$error;
645 $errorno = self::$errorno;
647 Logger::log('DB Error '.self::$errorno.': '.self::$error."\n".
648 System::callstack(8)."\n".self::replaceParameters($sql, $params));
650 // On a lost connection we simply quit.
651 // A reconnect like in self::p could be dangerous with modifications
652 if ($errorno == 2006) {
653 Logger::log('Giving up because of database error '.$errorno.': '.$error);
657 self::$error = $error;
658 self::$errorno = $errorno;
661 self::$profiler->saveTimestamp($stamp, "database_write", System::callstack());
667 * @brief Check if data exists
669 * @param string $table Table name
670 * @param array $condition array of fields for condition
672 * @return boolean Are there rows for that condition?
675 public static function exists($table, $condition) {
682 if (empty($condition)) {
683 return DBStructure::existsTable($table);
687 $first_key = key($condition);
688 if (!is_int($first_key)) {
689 $fields = [$first_key];
692 $stmt = self::select($table, $fields, $condition, ['limit' => 1]);
694 if (is_bool($stmt)) {
697 $retval = (self::numRows($stmt) > 0);
706 * Fetches the first row
708 * Please use DBA::selectFirst or DBA::exists whenever this is possible.
710 * @brief Fetches the first row
711 * @param string $sql SQL statement
712 * @return array first row of query
715 public static function fetchFirst($sql) {
716 $params = self::getParam(func_get_args());
718 $stmt = self::p($sql, $params);
720 if (is_bool($stmt)) {
723 $retval = self::fetch($stmt);
732 * @brief Returns the number of affected rows of the last statement
734 * @return int Number of rows
736 public static function affectedRows() {
737 return self::$affected_rows;
741 * @brief Returns the number of columns of a statement
743 * @param object Statement object
744 * @return int Number of columns
746 public static function columnCount($stmt) {
747 if (!is_object($stmt)) {
750 switch (self::$driver) {
752 return $stmt->columnCount();
754 return $stmt->field_count;
759 * @brief Returns the number of rows of a statement
761 * @param PDOStatement|mysqli_result|mysqli_stmt Statement object
762 * @return int Number of rows
764 public static function numRows($stmt) {
765 if (!is_object($stmt)) {
768 switch (self::$driver) {
770 return $stmt->rowCount();
772 return $stmt->num_rows;
778 * @brief Fetch a single row
780 * @param mixed $stmt statement object
781 * @return array current row
783 public static function fetch($stmt) {
785 $stamp1 = microtime(true);
789 if (!is_object($stmt)) {
793 switch (self::$driver) {
795 $columns = $stmt->fetch(PDO::FETCH_ASSOC);
798 if (get_class($stmt) == 'mysqli_result') {
799 $columns = $stmt->fetch_assoc();
803 // This code works, but is slow
805 // Bind the result to a result array
809 for ($x = 0; $x < $stmt->field_count; $x++) {
810 $cols[] = &$cols_num[$x];
813 call_user_func_array([$stmt, 'bind_result'], $cols);
815 if (!$stmt->fetch()) {
820 // We need to get the field names for the array keys
821 // It seems that there is no better way to do this.
822 $result = $stmt->result_metadata();
823 $fields = $result->fetch_fields();
825 foreach ($cols_num AS $param => $col) {
826 $columns[$fields[$param]->name] = $col;
830 self::$profiler->saveTimestamp($stamp1, 'database', System::callstack());
836 * @brief Insert a row into a table
838 * @param string $table Table name
839 * @param array $param parameter array
840 * @param bool $on_duplicate_update Do an update on a duplicate entry
842 * @return boolean was the insert successful?
845 public static function insert($table, $param, $on_duplicate_update = false) {
847 if (empty($table) || empty($param)) {
848 Logger::log('Table and fields have to be set');
852 $sql = "INSERT INTO `".self::escape($table)."` (`".implode("`, `", array_keys($param))."`) VALUES (".
853 substr(str_repeat("?, ", count($param)), 0, -2).")";
855 if ($on_duplicate_update) {
856 $sql .= " ON DUPLICATE KEY UPDATE `".implode("` = ?, `", array_keys($param))."` = ?";
858 $values = array_values($param);
859 $param = array_merge_recursive($values, $values);
862 return self::e($sql, $param);
866 * @brief Fetch the id of the last insert command
868 * @return integer Last inserted id
870 public static function lastInsertId() {
871 switch (self::$driver) {
873 $id = self::$connection->lastInsertId();
876 $id = self::$connection->insert_id;
883 * @brief Locks a table for exclusive write access
885 * This function can be extended in the future to accept a table array as well.
887 * @param string $table Table name
889 * @return boolean was the lock successful?
892 public static function lock($table) {
893 // See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
894 if (self::$driver == 'pdo') {
895 self::e("SET autocommit=0");
896 self::$connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
898 self::$connection->autocommit(false);
901 $success = self::e("LOCK TABLES `".self::escape($table)."` WRITE");
903 if (self::$driver == 'pdo') {
904 self::$connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
908 if (self::$driver == 'pdo') {
909 self::e("SET autocommit=1");
911 self::$connection->autocommit(true);
914 self::$in_transaction = true;
920 * @brief Unlocks all locked tables
922 * @return boolean was the unlock successful?
925 public static function unlock() {
926 // See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
927 self::performCommit();
929 if (self::$driver == 'pdo') {
930 self::$connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
933 $success = self::e("UNLOCK TABLES");
935 if (self::$driver == 'pdo') {
936 self::$connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
937 self::e("SET autocommit=1");
939 self::$connection->autocommit(true);
942 self::$in_transaction = false;
947 * @brief Starts a transaction
949 * @return boolean Was the command executed successfully?
951 public static function transaction() {
952 if (!self::performCommit()) {
956 switch (self::$driver) {
958 if (!self::$connection->inTransaction() && !self::$connection->beginTransaction()) {
964 if (!self::$connection->begin_transaction()) {
970 self::$in_transaction = true;
974 private static function performCommit()
976 switch (self::$driver) {
978 if (!self::$connection->inTransaction()) {
982 return self::$connection->commit();
985 return self::$connection->commit();
992 * @brief Does a commit
994 * @return boolean Was the command executed successfully?
996 public static function commit() {
997 if (!self::performCommit()) {
1000 self::$in_transaction = false;
1005 * @brief Does a rollback
1007 * @return boolean Was the command executed successfully?
1009 public static function rollback() {
1012 switch (self::$driver) {
1014 if (!self::$connection->inTransaction()) {
1018 $ret = self::$connection->rollBack();
1022 $ret = self::$connection->rollback();
1025 self::$in_transaction = false;
1030 * @brief Build the array with the table relations
1032 * The array is build from the database definitions in DBStructure.php
1034 * This process must only be started once, since the value is cached.
1036 private static function buildRelationData() {
1037 $definition = DBStructure::definition(self::$configCache->get('system', 'basepath'));
1039 foreach ($definition AS $table => $structure) {
1040 foreach ($structure['fields'] AS $field => $field_struct) {
1041 if (isset($field_struct['relation'])) {
1042 foreach ($field_struct['relation'] AS $rel_table => $rel_field) {
1043 self::$relation[$rel_table][$rel_field][$table][] = $field;
1051 * @brief Delete a row from a table
1053 * @param string $table Table name
1054 * @param array $conditions Field condition(s)
1055 * @param array $options
1056 * - cascade: If true we delete records in other tables that depend on the one we're deleting through
1057 * relations (default: true)
1058 * @param array $callstack Internal use: prevent endless loops
1060 * @return boolean was the delete successful?
1061 * @throws \Exception
1063 public static function delete($table, array $conditions, array $options = [], array &$callstack = [])
1065 if (empty($table) || empty($conditions)) {
1066 Logger::log('Table and conditions have to be set');
1072 // Create a key for the loop prevention
1073 $key = $table . ':' . json_encode($conditions);
1075 // We quit when this key already exists in the callstack.
1076 if (isset($callstack[$key])) {
1080 $callstack[$key] = true;
1082 $table = self::escape($table);
1084 $commands[$key] = ['table' => $table, 'conditions' => $conditions];
1086 // Don't use "defaults" here, since it would set "false" to "true"
1087 if (isset($options['cascade'])) {
1088 $cascade = $options['cascade'];
1093 // To speed up the whole process we cache the table relations
1094 if ($cascade && count(self::$relation) == 0) {
1095 self::buildRelationData();
1098 // Is there a relation entry for the table?
1099 if ($cascade && isset(self::$relation[$table])) {
1100 // We only allow a simple "one field" relation.
1101 $field = array_keys(self::$relation[$table])[0];
1102 $rel_def = array_values(self::$relation[$table])[0];
1104 // Create a key for preventing double queries
1105 $qkey = $field . '-' . $table . ':' . json_encode($conditions);
1107 // When the search field is the relation field, we don't need to fetch the rows
1108 // This is useful when the leading record is already deleted in the frontend but the rest is done in the backend
1109 if ((count($conditions) == 1) && ($field == array_keys($conditions)[0])) {
1110 foreach ($rel_def AS $rel_table => $rel_fields) {
1111 foreach ($rel_fields AS $rel_field) {
1112 self::delete($rel_table, [$rel_field => array_values($conditions)[0]], $options, $callstack);
1115 // We quit when this key already exists in the callstack.
1116 } elseif (!isset($callstack[$qkey])) {
1117 $callstack[$qkey] = true;
1119 // Fetch all rows that are to be deleted
1120 $data = self::select($table, [$field], $conditions);
1122 while ($row = self::fetch($data)) {
1123 self::delete($table, [$field => $row[$field]], $options, $callstack);
1128 // Since we had split the delete command we don't need the original command anymore
1129 unset($commands[$key]);
1133 // Now we finalize the process
1134 $do_transaction = !self::$in_transaction;
1136 if ($do_transaction) {
1137 self::transaction();
1143 foreach ($commands AS $command) {
1144 $conditions = $command['conditions'];
1146 $first_key = key($conditions);
1148 $condition_string = self::buildCondition($conditions);
1150 if ((count($command['conditions']) > 1) || is_int($first_key)) {
1151 $sql = "DELETE FROM `" . $command['table'] . "`" . $condition_string;
1152 Logger::log(self::replaceParameters($sql, $conditions), Logger::DATA);
1154 if (!self::e($sql, $conditions)) {
1155 if ($do_transaction) {
1161 $key_table = $command['table'];
1162 $key_condition = array_keys($command['conditions'])[0];
1163 $value = array_values($command['conditions'])[0];
1165 // Split the SQL queries in chunks of 100 values
1166 // We do the $i stuff here to make the code better readable
1167 $i = isset($counter[$key_table][$key_condition]) ? $counter[$key_table][$key_condition] : 0;
1168 if (isset($compacted[$key_table][$key_condition][$i]) && count($compacted[$key_table][$key_condition][$i]) > 100) {
1172 $compacted[$key_table][$key_condition][$i][$value] = $value;
1173 $counter[$key_table][$key_condition] = $i;
1176 foreach ($compacted AS $table => $values) {
1177 foreach ($values AS $field => $field_value_list) {
1178 foreach ($field_value_list AS $field_values) {
1179 $sql = "DELETE FROM `" . $table . "` WHERE `" . $field . "` IN (" .
1180 substr(str_repeat("?, ", count($field_values)), 0, -2) . ");";
1182 Logger::log(self::replaceParameters($sql, $field_values), Logger::DATA);
1184 if (!self::e($sql, $field_values)) {
1185 if ($do_transaction) {
1193 if ($do_transaction) {
1200 * @brief Updates rows
1202 * Updates rows in the database. When $old_fields is set to an array,
1203 * the system will only do an update if the fields in that array changed.
1206 * Only the values in $old_fields are compared.
1207 * This is an intentional behaviour.
1210 * We include the timestamp field in $fields but not in $old_fields.
1211 * Then the row will only get the new timestamp when the other fields had changed.
1213 * When $old_fields is set to a boolean value the system will do this compare itself.
1214 * When $old_fields is set to "true" the system will do an insert if the row doesn't exists.
1217 * Only set $old_fields to a boolean value when you are sure that you will update a single row.
1218 * When you set $old_fields to "true" then $fields must contain all relevant fields!
1220 * @param string $table Table name
1221 * @param array $fields contains the fields that are updated
1222 * @param array $condition condition array with the key values
1223 * @param array|boolean $old_fields array with the old field values that are about to be replaced (true = update on duplicate)
1225 * @return boolean was the update successfull?
1226 * @throws \Exception
1228 public static function update($table, $fields, $condition, $old_fields = []) {
1230 if (empty($table) || empty($fields) || empty($condition)) {
1231 Logger::log('Table, fields and condition have to be set');
1235 $table = self::escape($table);
1237 $condition_string = self::buildCondition($condition);
1239 if (is_bool($old_fields)) {
1240 $do_insert = $old_fields;
1242 $old_fields = self::selectFirst($table, [], $condition);
1244 if (is_bool($old_fields)) {
1246 $values = array_merge($condition, $fields);
1247 return self::insert($table, $values, $do_insert);
1253 $do_update = (count($old_fields) == 0);
1255 foreach ($old_fields AS $fieldname => $content) {
1256 if (isset($fields[$fieldname])) {
1257 if (($fields[$fieldname] == $content) && !is_null($content)) {
1258 unset($fields[$fieldname]);
1265 if (!$do_update || (count($fields) == 0)) {
1269 $sql = "UPDATE `".$table."` SET `".
1270 implode("` = ?, `", array_keys($fields))."` = ?".$condition_string;
1272 $params1 = array_values($fields);
1273 $params2 = array_values($condition);
1274 $params = array_merge_recursive($params1, $params2);
1276 return self::e($sql, $params);
1280 * Retrieve a single record from a table and returns it in an associative array
1282 * @brief Retrieve a single record from a table
1283 * @param string $table
1284 * @param array $fields
1285 * @param array $condition
1286 * @param array $params
1287 * @return bool|array
1288 * @throws \Exception
1291 public static function selectFirst($table, array $fields = [], array $condition = [], $params = [])
1293 $params['limit'] = 1;
1294 $result = self::select($table, $fields, $condition, $params);
1296 if (is_bool($result)) {
1299 $row = self::fetch($result);
1300 self::close($result);
1306 * @brief Select rows from a table
1308 * @param string $table Table name
1309 * @param array $fields Array of selected fields, empty for all
1310 * @param array $condition Array of fields for condition
1311 * @param array $params Array of several parameters
1313 * @return boolean|object
1317 * $fields = array("id", "uri", "uid", "network");
1319 * $condition = array("uid" => 1, "network" => 'dspr');
1321 * $condition = array("`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr');
1323 * $params = array("order" => array("id", "received" => true), "limit" => 10);
1325 * $data = DBA::select($table, $fields, $condition, $params);
1326 * @throws \Exception
1328 public static function select($table, array $fields = [], array $condition = [], array $params = [])
1334 $table = self::escape($table);
1336 if (count($fields) > 0) {
1337 $select_fields = "`" . implode("`, `", array_values($fields)) . "`";
1339 $select_fields = "*";
1342 $condition_string = self::buildCondition($condition);
1344 $param_string = self::buildParameter($params);
1346 $sql = "SELECT " . $select_fields . " FROM `" . $table . "`" . $condition_string . $param_string;
1348 $result = self::p($sql, $condition);
1354 * @brief Counts the rows from a table satisfying the provided condition
1356 * @param string $table Table name
1357 * @param array $condition array of fields for condition
1364 * $condition = ["uid" => 1, "network" => 'dspr'];
1366 * $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr'];
1368 * $count = DBA::count($table, $condition);
1369 * @throws \Exception
1371 public static function count($table, array $condition = [])
1377 $condition_string = self::buildCondition($condition);
1379 $sql = "SELECT COUNT(*) AS `count` FROM `".$table."`".$condition_string;
1381 $row = self::fetchFirst($sql, $condition);
1383 return $row['count'];
1387 * @brief Returns the SQL condition string built from the provided condition array
1389 * This function operates with two modes.
1390 * - Supplied with a filed/value associative array, it builds simple strict
1391 * equality conditions linked by AND.
1392 * - Supplied with a flat list, the first element is the condition string and
1393 * the following arguments are the values to be interpolated
1395 * $condition = ["uid" => 1, "network" => 'dspr'];
1397 * $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr'];
1399 * In either case, the provided array is left with the parameters only
1401 * @param array $condition
1404 public static function buildCondition(array &$condition = [])
1406 $condition_string = '';
1407 if (count($condition) > 0) {
1409 $first_key = key($condition);
1410 if (is_int($first_key)) {
1411 $condition_string = " WHERE (" . array_shift($condition) . ")";
1414 $condition_string = "";
1415 foreach ($condition as $field => $value) {
1416 if ($condition_string != "") {
1417 $condition_string .= " AND ";
1419 if (is_array($value)) {
1420 /* Workaround for MySQL Bug #64791.
1421 * Never mix data types inside any IN() condition.
1422 * In case of mixed types, cast all as string.
1423 * Logic needs to be consistent with DBA::p() data types.
1427 foreach ($value as $single_value) {
1428 if (is_int($single_value)) {
1435 if ($is_int && $is_alpha) {
1436 foreach ($value as &$ref) {
1438 $ref = (string)$ref;
1441 unset($ref); //Prevent accidental re-use.
1444 $new_values = array_merge($new_values, array_values($value));
1445 $placeholders = substr(str_repeat("?, ", count($value)), 0, -2);
1446 $condition_string .= "`" . $field . "` IN (" . $placeholders . ")";
1448 $new_values[$field] = $value;
1449 $condition_string .= "`" . $field . "` = ?";
1452 $condition_string = " WHERE (" . $condition_string . ")";
1453 $condition = $new_values;
1457 return $condition_string;
1461 * @brief Returns the SQL parameter string built from the provided parameter array
1463 * @param array $params
1466 public static function buildParameter(array $params = [])
1469 if (isset($params['order'])) {
1470 $order_string = " ORDER BY ";
1471 foreach ($params['order'] AS $fields => $order) {
1472 if (!is_int($fields)) {
1473 $order_string .= "`" . $fields . "` " . ($order ? "DESC" : "ASC") . ", ";
1475 $order_string .= "`" . $order . "`, ";
1478 $order_string = substr($order_string, 0, -2);
1482 if (isset($params['limit']) && is_int($params['limit'])) {
1483 $limit_string = " LIMIT " . intval($params['limit']);
1486 if (isset($params['limit']) && is_array($params['limit'])) {
1487 $limit_string = " LIMIT " . intval($params['limit'][0]) . ", " . intval($params['limit'][1]);
1490 return $order_string.$limit_string;
1494 * @brief Fills an array with data from a query
1496 * @param object $stmt statement object
1497 * @param bool $do_close
1498 * @return array Data array
1500 public static function toArray($stmt, $do_close = true) {
1501 if (is_bool($stmt)) {
1506 while ($row = self::fetch($stmt)) {
1516 * @brief Returns the error number of the last query
1518 * @return string Error number (0 if no error)
1520 public static function errorNo() {
1521 return self::$errorno;
1525 * @brief Returns the error message of the last query
1527 * @return string Error message ('' if no error)
1529 public static function errorMessage() {
1530 return self::$error;
1534 * @brief Closes the current statement
1536 * @param object $stmt statement object
1537 * @return boolean was the close successful?
1539 public static function close($stmt) {
1541 $stamp1 = microtime(true);
1543 if (!is_object($stmt)) {
1547 switch (self::$driver) {
1549 $ret = $stmt->closeCursor();
1552 // MySQLi offers both a mysqli_stmt and a mysqli_result class.
1553 // We should be careful not to assume the object type of $stmt
1554 // because DBA::p() has been able to return both types.
1555 if ($stmt instanceof mysqli_stmt) {
1556 $stmt->free_result();
1557 $ret = $stmt->close();
1558 } elseif ($stmt instanceof mysqli_result) {
1567 self::$profiler->saveTimestamp($stamp1, 'database', System::callstack());
1573 * @brief Return a list of database processes
1576 * 'list' => List of processes, separated in their different states
1577 * 'amount' => Number of concurrent database processes
1578 * @throws \Exception
1580 public static function processlist()
1582 $ret = self::p("SHOW PROCESSLIST");
1583 $data = self::toArray($ret);
1587 foreach ($data as $process) {
1588 $state = trim($process["State"]);
1590 // Filter out all non blocking processes
1591 if (!in_array($state, ["", "init", "statistics", "updating"])) {
1598 foreach ($states as $state => $usage) {
1599 if ($statelist != "") {
1602 $statelist .= $state.": ".$usage;
1604 return(["list" => $statelist, "amount" => $processes]);
1608 * Checks if $array is a filled array with at least one entry.
1610 * @param mixed $array A filled array with at least one entry
1612 * @return boolean Whether $array is a filled array or an object with rows
1614 public static function isResult($array)
1616 // It could be a return value from an update statement
1617 if (is_bool($array)) {
1621 if (is_object($array)) {
1622 return self::numRows($array) > 0;
1625 return (is_array($array) && (count($array) > 0));
1629 * @brief Callback function for "esc_array"
1631 * @param mixed $value Array value
1632 * @param string $key Array key
1633 * @param boolean $add_quotation add quotation marks for string values
1636 private static function escapeArrayCallback(&$value, $key, $add_quotation)
1638 if (!$add_quotation) {
1639 if (is_bool($value)) {
1640 $value = ($value ? '1' : '0');
1642 $value = self::escape($value);
1647 if (is_bool($value)) {
1648 $value = ($value ? 'true' : 'false');
1649 } elseif (is_float($value) || is_integer($value)) {
1650 $value = (string) $value;
1652 $value = "'" . self::escape($value) . "'";
1657 * @brief Escapes a whole array
1659 * @param mixed $arr Array with values to be escaped
1660 * @param boolean $add_quotation add quotation marks for string values
1663 public static function escapeArray(&$arr, $add_quotation = false)
1665 array_walk($arr, 'self::escapeArrayCallback', $add_quotation);