3 namespace Friendica\Database;
5 use Friendica\Core\Config\Cache\IConfigCache;
6 use Friendica\Core\System;
7 use Friendica\Util\DateTimeFormat;
8 use Friendica\Util\Profiler;
15 use Psr\Log\LoggerInterface;
18 * @class MySQL database class
20 * This class is for the low level database stuff that does driver specific things.
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;
44 * @var LoggerInterface
46 private static $logger;
47 private static $server_info = '';
48 /** @var PDO|mysqli */
49 private static $connection;
50 private static $driver;
51 private static $error = false;
52 private static $errorno = 0;
53 private static $affected_rows = 0;
54 private static $in_transaction = false;
55 private static $in_retrial = false;
56 private static $relation = [];
57 private static $db_serveraddr = '';
58 private static $db_user = '';
59 private static $db_pass = '';
60 private static $db_name = '';
61 private static $db_charset = '';
63 public static function connect(IConfigCache $configCache, Profiler $profiler, LoggerInterface $logger, $serveraddr, $user, $pass, $db, $charset = null)
65 if (!is_null(self::$connection) && self::connected()) {
69 // We are storing these values for being able to perform a reconnect
70 self::$configCache = $configCache;
71 self::$profiler = $profiler;
72 self::$logger = $logger;
73 self::$db_serveraddr = $serveraddr;
74 self::$db_user = $user;
75 self::$db_pass = $pass;
77 self::$db_charset = $charset;
80 $serveraddr = trim($serveraddr);
82 $serverdata = explode(':', $serveraddr);
83 $server = $serverdata[0];
85 if (count($serverdata) > 1) {
86 $port = trim($serverdata[1]);
89 $server = trim($server);
93 $charset = trim($charset);
95 if (!(strlen($server) && strlen($user))) {
99 if (class_exists('\PDO') && in_array('mysql', PDO::getAvailableDrivers())) {
100 self::$driver = 'pdo';
101 $connect = "mysql:host=".$server.";dbname=".$db;
104 $connect .= ";port=".$port;
108 $connect .= ";charset=".$charset;
112 self::$connection = @new PDO($connect, $user, $pass);
113 self::$connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
114 self::$connected = true;
115 } catch (PDOException $e) {
116 /// @TODO At least log exception, don't ignore it!
120 if (!self::$connected && class_exists('\mysqli')) {
121 self::$driver = 'mysqli';
124 self::$connection = @new mysqli($server, $user, $pass, $db, $port);
126 self::$connection = @new mysqli($server, $user, $pass, $db);
129 if (!mysqli_connect_errno()) {
130 self::$connected = true;
133 self::$connection->set_charset($charset);
138 // No suitable SQL driver was found.
139 if (!self::$connected) {
140 self::$driver = null;
141 self::$connection = null;
144 return self::$connected;
148 * Sets the logger for DBA
150 * @note this is necessary because if we want to load the logger configuration
151 * from the DB, but there's an error, we would print out an exception.
152 * So the logger gets updated after the logger configuration can be retrieved
155 * @param LoggerInterface $logger
157 public static function setLogger(LoggerInterface $logger)
159 self::$logger = $logger;
163 * Disconnects the current database connection
165 public static function disconnect()
167 if (is_null(self::$connection)) {
171 switch (self::$driver) {
173 self::$connection = null;
176 self::$connection->close();
177 self::$connection = null;
183 * Perform a reconnect of an existing database connection
185 public static function reconnect() {
188 $ret = self::connect(self::$configCache, self::$profiler, self::$logger, self::$db_serveraddr, self::$db_user, self::$db_pass, self::$db_name, self::$db_charset);
193 * Return the database object.
196 public static function getConnection()
198 return self::$connection;
202 * @brief Returns the MySQL server version string
204 * This function discriminate between the deprecated mysql API and the current
205 * object-oriented mysqli API. Example of returned string: 5.5.46-0+deb8u1
209 public static function serverInfo() {
210 if (self::$server_info == '') {
211 switch (self::$driver) {
213 self::$server_info = self::$connection->getAttribute(PDO::ATTR_SERVER_VERSION);
216 self::$server_info = self::$connection->server_info;
220 return self::$server_info;
224 * @brief Returns the selected database name
229 public static function databaseName() {
230 $ret = self::p("SELECT DATABASE() AS `db`");
231 $data = self::toArray($ret);
232 return $data[0]['db'];
236 * @brief Analyze a database query and log this if some conditions are met.
238 * @param string $query The database query that will be analyzed
241 private static function logIndex($query) {
243 if (!self::$configCache->get('system', 'db_log_index')) {
247 // Don't explain an explain statement
248 if (strtolower(substr($query, 0, 7)) == "explain") {
252 // Only do the explain on "select", "update" and "delete"
253 if (!in_array(strtolower(substr($query, 0, 6)), ["select", "update", "delete"])) {
257 $r = self::p("EXPLAIN ".$query);
258 if (!self::isResult($r)) {
262 $watchlist = explode(',', self::$configCache->get('system', 'db_log_index_watch'));
263 $blacklist = explode(',', self::$configCache->get('system', 'db_log_index_blacklist'));
265 while ($row = self::fetch($r)) {
266 if ((intval(self::$configCache->get('system', 'db_loglimit_index')) > 0)) {
267 $log = (in_array($row['key'], $watchlist) &&
268 ($row['rows'] >= intval(self::$configCache->get('system', 'db_loglimit_index'))));
273 if ((intval(self::$configCache->get('system', 'db_loglimit_index_high')) > 0) && ($row['rows'] >= intval(self::$configCache->get('system', 'db_loglimit_index_high')))) {
277 if (in_array($row['key'], $blacklist) || ($row['key'] == "")) {
282 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
283 @file_put_contents(self::$configCache->get('system', 'db_log_index'), DateTimeFormat::utcNow()."\t".
284 $row['key']."\t".$row['rows']."\t".$row['Extra']."\t".
285 basename($backtrace[1]["file"])."\t".
286 $backtrace[1]["line"]."\t".$backtrace[2]["function"]."\t".
287 substr($query, 0, 2000)."\n", FILE_APPEND);
293 * Removes every not whitelisted character from the identifier string
295 * @param string $identifier
297 * @return string sanitized identifier
300 private static function sanitizeIdentifier($identifier)
302 return preg_replace('/[^A-Za-z0-9_\-]+/', '', $identifier);
305 public static function escape($str) {
306 if (self::$connected) {
307 switch (self::$driver) {
309 return substr(@self::$connection->quote($str, PDO::PARAM_STR), 1, -1);
312 return @self::$connection->real_escape_string($str);
315 return str_replace("'", "\\'", $str);
319 public static function connected() {
322 if (is_null(self::$connection)) {
326 switch (self::$driver) {
328 $r = self::p("SELECT 1");
329 if (self::isResult($r)) {
330 $row = self::toArray($r);
331 $connected = ($row[0]['1'] == '1');
335 $connected = self::$connection->ping();
342 * @brief Replaces ANY_VALUE() function by MIN() function,
343 * if the database server does not support ANY_VALUE().
345 * Considerations for Standard SQL, or MySQL with ONLY_FULL_GROUP_BY (default since 5.7.5).
346 * ANY_VALUE() is available from MySQL 5.7.5 https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html
347 * A standard fall-back is to use MIN().
349 * @param string $sql An SQL string without the values
350 * @return string The input SQL string modified if necessary.
352 public static function anyValueFallback($sql) {
353 $server_info = self::serverInfo();
354 if (version_compare($server_info, '5.7.5', '<') ||
355 (stripos($server_info, 'MariaDB') !== false)) {
356 $sql = str_ireplace('ANY_VALUE(', 'MIN(', $sql);
362 * @brief beautifies the query - useful for "SHOW PROCESSLIST"
364 * This is safe when we bind the parameters later.
365 * The parameter values aren't part of the SQL.
367 * @param string $sql An SQL string without the values
368 * @return string The input SQL string modified if necessary.
370 public static function cleanQuery($sql) {
371 $search = ["\t", "\n", "\r", " "];
372 $replace = [' ', ' ', ' ', ' '];
375 $sql = str_replace($search, $replace, $sql);
376 } while ($oldsql != $sql);
383 * @brief Replaces the ? placeholders with the parameters in the $args array
385 * @param string $sql SQL query
386 * @param array $args The parameters that are to replace the ? placeholders
387 * @return string The replaced SQL query
389 private static function replaceParameters($sql, $args) {
391 foreach ($args AS $param => $value) {
392 if (is_int($args[$param]) || is_float($args[$param])) {
393 $replace = intval($args[$param]);
395 $replace = "'".self::escape($args[$param])."'";
398 $pos = strpos($sql, '?', $offset);
399 if ($pos !== false) {
400 $sql = substr_replace($sql, $replace, $pos, 1);
402 $offset = $pos + strlen($replace);
408 * @brief Convert parameter array to an universal form
409 * @param array $args Parameter array
410 * @return array universalized parameter array
412 private static function getParam($args) {
415 // When the second function parameter is an array then use this as the parameter array
416 if ((count($args) > 0) && (is_array($args[1]))) {
424 * @brief Executes a prepared statement that returns data
425 * @usage Example: $r = p("SELECT * FROM `item` WHERE `guid` = ?", $guid);
427 * Please only use it with complicated queries.
428 * For all regular queries please use DBA::select or DBA::exists
430 * @param string $sql SQL statement
431 * @return bool|object statement object or result object
434 public static function p($sql) {
436 $stamp1 = microtime(true);
438 $params = self::getParam(func_get_args());
440 // Renumber the array keys to be sure that they fit
443 foreach ($params AS $param) {
444 // Avoid problems with some MySQL servers and boolean values. See issue #3645
445 if (is_bool($param)) {
446 $param = (int)$param;
448 $args[++$i] = $param;
451 if (!self::$connected) {
455 if ((substr_count($sql, '?') != count($args)) && (count($args) > 0)) {
456 // Question: Should we continue or stop the query here?
457 self::$logger->warning('Query parameters mismatch.', ['query' => $sql, 'args' => $args, 'callstack' => System::callstack()]);
460 $sql = self::cleanQuery($sql);
461 $sql = self::anyValueFallback($sql);
465 if (self::$configCache->get('system', 'db_callstack') !== null) {
466 $sql = "/*".System::callstack()." */ ".$sql;
471 self::$affected_rows = 0;
473 // We have to make some things different if this function is called from "e"
474 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
476 if (isset($trace[1])) {
477 $called_from = $trace[1];
479 // We use just something that is defined to avoid warnings
480 $called_from = $trace[0];
482 // We are having an own error logging in the function "e"
483 $called_from_e = ($called_from['function'] == 'e');
485 switch (self::$driver) {
487 // If there are no arguments we use "query"
488 if (count($args) == 0) {
489 if (!$retval = self::$connection->query($sql)) {
490 $errorInfo = self::$connection->errorInfo();
491 self::$error = $errorInfo[2];
492 self::$errorno = $errorInfo[1];
496 self::$affected_rows = $retval->rowCount();
500 /** @var $stmt mysqli_stmt|PDOStatement */
501 if (!$stmt = self::$connection->prepare($sql)) {
502 $errorInfo = self::$connection->errorInfo();
503 self::$error = $errorInfo[2];
504 self::$errorno = $errorInfo[1];
509 foreach ($args AS $param => $value) {
510 if (is_int($args[$param])) {
511 $data_type = PDO::PARAM_INT;
513 $data_type = PDO::PARAM_STR;
515 $stmt->bindParam($param, $args[$param], $data_type);
518 if (!$stmt->execute()) {
519 $errorInfo = $stmt->errorInfo();
520 self::$error = $errorInfo[2];
521 self::$errorno = $errorInfo[1];
525 self::$affected_rows = $retval->rowCount();
529 // There are SQL statements that cannot be executed with a prepared statement
530 $parts = explode(' ', $orig_sql);
531 $command = strtolower($parts[0]);
532 $can_be_prepared = in_array($command, ['select', 'update', 'insert', 'delete']);
534 // The fallback routine is called as well when there are no arguments
535 if (!$can_be_prepared || (count($args) == 0)) {
536 $retval = self::$connection->query(self::replaceParameters($sql, $args));
537 if (self::$connection->errno) {
538 self::$error = self::$connection->error;
539 self::$errorno = self::$connection->errno;
542 if (isset($retval->num_rows)) {
543 self::$affected_rows = $retval->num_rows;
545 self::$affected_rows = self::$connection->affected_rows;
551 $stmt = self::$connection->stmt_init();
553 if (!$stmt->prepare($sql)) {
554 self::$error = $stmt->error;
555 self::$errorno = $stmt->errno;
562 foreach ($args AS $param => $value) {
563 if (is_int($args[$param])) {
565 } elseif (is_float($args[$param])) {
567 } elseif (is_string($args[$param])) {
572 $values[] = &$args[$param];
575 if (count($values) > 0) {
576 array_unshift($values, $param_types);
577 call_user_func_array([$stmt, 'bind_param'], $values);
580 if (!$stmt->execute()) {
581 self::$error = self::$connection->error;
582 self::$errorno = self::$connection->errno;
585 $stmt->store_result();
587 self::$affected_rows = $retval->affected_rows;
592 // We are having an own error logging in the function "e"
593 if ((self::$errorno != 0) && !$called_from_e) {
594 // We have to preserve the error code, somewhere in the logging it get lost
595 $error = self::$error;
596 $errorno = self::$errorno;
598 self::$logger->error('DB Error', [
599 'code' => self::$errorno,
600 'error' => self::$error,
601 'callstack' => System::callstack(8),
602 'params' => self::replaceParameters($sql, $args),
605 // On a lost connection we try to reconnect - but only once.
606 if ($errorno == 2006) {
607 if (self::$in_retrial || !self::reconnect()) {
608 // It doesn't make sense to continue when the database connection was lost
609 if (self::$in_retrial) {
610 self::$logger->notice('Giving up retrial because of database error', [
611 'code' => self::$errorno,
612 'error' => self::$error,
615 self::$logger->notice('Couldn\'t reconnect after database error', [
616 'code' => self::$errorno,
617 'error' => self::$error,
623 self::$logger->notice('Reconnected after database error', [
624 'code' => self::$errorno,
625 'error' => self::$error,
627 self::$in_retrial = true;
628 $ret = self::p($sql, $args);
629 self::$in_retrial = false;
634 self::$error = $error;
635 self::$errorno = $errorno;
638 self::$profiler->saveTimestamp($stamp1, 'database', System::callstack());
640 if (self::$configCache->get('system', 'db_log')) {
641 $stamp2 = microtime(true);
642 $duration = (float)($stamp2 - $stamp1);
644 if (($duration > self::$configCache->get('system', 'db_loglimit'))) {
645 $duration = round($duration, 3);
646 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
648 @file_put_contents(self::$configCache->get('system', 'db_log'), DateTimeFormat::utcNow()."\t".$duration."\t".
649 basename($backtrace[1]["file"])."\t".
650 $backtrace[1]["line"]."\t".$backtrace[2]["function"]."\t".
651 substr(self::replaceParameters($sql, $args), 0, 2000)."\n", FILE_APPEND);
658 * @brief Executes a prepared statement like UPDATE or INSERT that doesn't return data
660 * Please use DBA::delete, DBA::insert, DBA::update, ... instead
662 * @param string $sql SQL statement
663 * @return boolean Was the query successfull? False is returned only if an error occurred
666 public static function e($sql) {
668 $stamp = microtime(true);
670 $params = self::getParam(func_get_args());
672 // In a case of a deadlock we are repeating the query 20 times
676 $stmt = self::p($sql, $params);
678 if (is_bool($stmt)) {
680 } elseif (is_object($stmt)) {
688 } while ((self::$errorno == 1213) && (--$timeout > 0));
690 if (self::$errorno != 0) {
691 // We have to preserve the error code, somewhere in the logging it get lost
692 $error = self::$error;
693 $errorno = self::$errorno;
695 self::$logger->error('DB Error', [
696 'code' => self::$errorno,
697 'error' => self::$error,
698 'callstack' => System::callstack(8),
699 'params' => self::replaceParameters($sql, $params),
702 // On a lost connection we simply quit.
703 // A reconnect like in self::p could be dangerous with modifications
704 if ($errorno == 2006) {
705 self::$logger->notice('Giving up because of database error', [
706 'code' => self::$errorno,
707 'error' => self::$error,
712 self::$error = $error;
713 self::$errorno = $errorno;
716 self::$profiler->saveTimestamp($stamp, "database_write", System::callstack());
722 * @brief Check if data exists
724 * @param string $table Table name
725 * @param array $condition array of fields for condition
727 * @return boolean Are there rows for that condition?
730 public static function exists($table, $condition) {
737 if (empty($condition)) {
738 return DBStructure::existsTable($table);
742 $first_key = key($condition);
743 if (!is_int($first_key)) {
744 $fields = [$first_key];
747 $stmt = self::select($table, $fields, $condition, ['limit' => 1]);
749 if (is_bool($stmt)) {
752 $retval = (self::numRows($stmt) > 0);
761 * Fetches the first row
763 * Please use DBA::selectFirst or DBA::exists whenever this is possible.
765 * @brief Fetches the first row
766 * @param string $sql SQL statement
767 * @return array first row of query
770 public static function fetchFirst($sql) {
771 $params = self::getParam(func_get_args());
773 $stmt = self::p($sql, $params);
775 if (is_bool($stmt)) {
778 $retval = self::fetch($stmt);
787 * @brief Returns the number of affected rows of the last statement
789 * @return int Number of rows
791 public static function affectedRows() {
792 return self::$affected_rows;
796 * @brief Returns the number of columns of a statement
798 * @param object Statement object
799 * @return int Number of columns
801 public static function columnCount($stmt) {
802 if (!is_object($stmt)) {
805 switch (self::$driver) {
807 return $stmt->columnCount();
809 return $stmt->field_count;
814 * @brief Returns the number of rows of a statement
816 * @param PDOStatement|mysqli_result|mysqli_stmt Statement object
817 * @return int Number of rows
819 public static function numRows($stmt) {
820 if (!is_object($stmt)) {
823 switch (self::$driver) {
825 return $stmt->rowCount();
827 return $stmt->num_rows;
833 * @brief Fetch a single row
835 * @param mixed $stmt statement object
836 * @return array current row
838 public static function fetch($stmt) {
840 $stamp1 = microtime(true);
844 if (!is_object($stmt)) {
848 switch (self::$driver) {
850 $columns = $stmt->fetch(PDO::FETCH_ASSOC);
853 if (get_class($stmt) == 'mysqli_result') {
854 $columns = $stmt->fetch_assoc();
858 // This code works, but is slow
860 // Bind the result to a result array
864 for ($x = 0; $x < $stmt->field_count; $x++) {
865 $cols[] = &$cols_num[$x];
868 call_user_func_array([$stmt, 'bind_result'], $cols);
870 if (!$stmt->fetch()) {
875 // We need to get the field names for the array keys
876 // It seems that there is no better way to do this.
877 $result = $stmt->result_metadata();
878 $fields = $result->fetch_fields();
880 foreach ($cols_num AS $param => $col) {
881 $columns[$fields[$param]->name] = $col;
885 self::$profiler->saveTimestamp($stamp1, 'database', System::callstack());
891 * @brief Insert a row into a table
893 * @param string/array $table Table name
895 * @return string formatted and sanitzed table name
898 public static function formatTableName($table)
900 if (is_string($table)) {
901 return "`" . self::sanitizeIdentifier($table) . "`";
904 if (!is_array($table)) {
908 $scheme = key($table);
910 return "`" . self::sanitizeIdentifier($scheme) . "`.`" . self::sanitizeIdentifier($table[$scheme]) . "`";
914 * @brief Insert a row into a table
916 * @param string $table Table name
917 * @param array $param parameter array
918 * @param bool $on_duplicate_update Do an update on a duplicate entry
920 * @return boolean was the insert successful?
923 public static function insert($table, $param, $on_duplicate_update = false) {
925 if (empty($table) || empty($param)) {
926 self::$logger->info('Table and fields have to be set');
930 $sql = "INSERT INTO " . self::formatTableName($table) . " (`".implode("`, `", array_keys($param))."`) VALUES (".
931 substr(str_repeat("?, ", count($param)), 0, -2).")";
933 if ($on_duplicate_update) {
934 $sql .= " ON DUPLICATE KEY UPDATE `".implode("` = ?, `", array_keys($param))."` = ?";
936 $values = array_values($param);
937 $param = array_merge_recursive($values, $values);
940 return self::e($sql, $param);
944 * @brief Fetch the id of the last insert command
946 * @return integer Last inserted id
948 public static function lastInsertId() {
949 switch (self::$driver) {
951 $id = self::$connection->lastInsertId();
954 $id = self::$connection->insert_id;
961 * @brief Locks a table for exclusive write access
963 * This function can be extended in the future to accept a table array as well.
965 * @param string $table Table name
967 * @return boolean was the lock successful?
970 public static function lock($table) {
971 // See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
972 if (self::$driver == 'pdo') {
973 self::e("SET autocommit=0");
974 self::$connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
976 self::$connection->autocommit(false);
979 $success = self::e("LOCK TABLES " . self::formatTableName($table) ." WRITE");
981 if (self::$driver == 'pdo') {
982 self::$connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
986 if (self::$driver == 'pdo') {
987 self::e("SET autocommit=1");
989 self::$connection->autocommit(true);
992 self::$in_transaction = true;
998 * @brief Unlocks all locked tables
1000 * @return boolean was the unlock successful?
1001 * @throws \Exception
1003 public static function unlock() {
1004 // See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
1005 self::performCommit();
1007 if (self::$driver == 'pdo') {
1008 self::$connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
1011 $success = self::e("UNLOCK TABLES");
1013 if (self::$driver == 'pdo') {
1014 self::$connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
1015 self::e("SET autocommit=1");
1017 self::$connection->autocommit(true);
1020 self::$in_transaction = false;
1025 * @brief Starts a transaction
1027 * @return boolean Was the command executed successfully?
1029 public static function transaction() {
1030 if (!self::performCommit()) {
1034 switch (self::$driver) {
1036 if (!self::$connection->inTransaction() && !self::$connection->beginTransaction()) {
1042 if (!self::$connection->begin_transaction()) {
1048 self::$in_transaction = true;
1052 private static function performCommit()
1054 switch (self::$driver) {
1056 if (!self::$connection->inTransaction()) {
1060 return self::$connection->commit();
1063 return self::$connection->commit();
1070 * @brief Does a commit
1072 * @return boolean Was the command executed successfully?
1074 public static function commit() {
1075 if (!self::performCommit()) {
1078 self::$in_transaction = false;
1083 * @brief Does a rollback
1085 * @return boolean Was the command executed successfully?
1087 public static function rollback() {
1090 switch (self::$driver) {
1092 if (!self::$connection->inTransaction()) {
1096 $ret = self::$connection->rollBack();
1100 $ret = self::$connection->rollback();
1103 self::$in_transaction = false;
1108 * @brief Build the array with the table relations
1110 * The array is build from the database definitions in DBStructure.php
1112 * This process must only be started once, since the value is cached.
1114 private static function buildRelationData() {
1115 $definition = DBStructure::definition(self::$configCache->get('system', 'basepath'));
1117 foreach ($definition AS $table => $structure) {
1118 foreach ($structure['fields'] AS $field => $field_struct) {
1119 if (isset($field_struct['relation'])) {
1120 foreach ($field_struct['relation'] AS $rel_table => $rel_field) {
1121 self::$relation[$rel_table][$rel_field][$table][] = $field;
1129 * @brief Delete a row from a table
1131 * @param string $table Table name
1132 * @param array $conditions Field condition(s)
1133 * @param array $options
1134 * - cascade: If true we delete records in other tables that depend on the one we're deleting through
1135 * relations (default: true)
1136 * @param array $callstack Internal use: prevent endless loops
1138 * @return boolean was the delete successful?
1139 * @throws \Exception
1141 public static function delete($table, array $conditions, array $options = [], array &$callstack = [])
1143 if (empty($table) || empty($conditions)) {
1144 self::$logger->info('Table and conditions have to be set');
1150 // Create a key for the loop prevention
1151 $key = $table . ':' . json_encode($conditions);
1153 // We quit when this key already exists in the callstack.
1154 if (isset($callstack[$key])) {
1158 $callstack[$key] = true;
1160 $table = self::sanitizeIdentifier($table);
1162 $commands[$key] = ['table' => $table, 'conditions' => $conditions];
1164 // Don't use "defaults" here, since it would set "false" to "true"
1165 if (isset($options['cascade'])) {
1166 $cascade = $options['cascade'];
1171 // To speed up the whole process we cache the table relations
1172 if ($cascade && count(self::$relation) == 0) {
1173 self::buildRelationData();
1176 // Is there a relation entry for the table?
1177 if ($cascade && isset(self::$relation[$table])) {
1178 // We only allow a simple "one field" relation.
1179 $field = array_keys(self::$relation[$table])[0];
1180 $rel_def = array_values(self::$relation[$table])[0];
1182 // Create a key for preventing double queries
1183 $qkey = $field . '-' . $table . ':' . json_encode($conditions);
1185 // When the search field is the relation field, we don't need to fetch the rows
1186 // This is useful when the leading record is already deleted in the frontend but the rest is done in the backend
1187 if ((count($conditions) == 1) && ($field == array_keys($conditions)[0])) {
1188 foreach ($rel_def AS $rel_table => $rel_fields) {
1189 foreach ($rel_fields AS $rel_field) {
1190 self::delete($rel_table, [$rel_field => array_values($conditions)[0]], $options, $callstack);
1193 // We quit when this key already exists in the callstack.
1194 } elseif (!isset($callstack[$qkey])) {
1195 $callstack[$qkey] = true;
1197 // Fetch all rows that are to be deleted
1198 $data = self::select($table, [$field], $conditions);
1200 while ($row = self::fetch($data)) {
1201 self::delete($table, [$field => $row[$field]], $options, $callstack);
1206 // Since we had split the delete command we don't need the original command anymore
1207 unset($commands[$key]);
1211 // Now we finalize the process
1212 $do_transaction = !self::$in_transaction;
1214 if ($do_transaction) {
1215 self::transaction();
1221 foreach ($commands AS $command) {
1222 $conditions = $command['conditions'];
1224 $first_key = key($conditions);
1226 $condition_string = self::buildCondition($conditions);
1228 if ((count($command['conditions']) > 1) || is_int($first_key)) {
1229 $sql = "DELETE FROM `" . $command['table'] . "`" . $condition_string;
1230 self::$logger->debug(self::replaceParameters($sql, $conditions));
1232 if (!self::e($sql, $conditions)) {
1233 if ($do_transaction) {
1239 $key_table = $command['table'];
1240 $key_condition = array_keys($command['conditions'])[0];
1241 $value = array_values($command['conditions'])[0];
1243 // Split the SQL queries in chunks of 100 values
1244 // We do the $i stuff here to make the code better readable
1245 $i = isset($counter[$key_table][$key_condition]) ? $counter[$key_table][$key_condition] : 0;
1246 if (isset($compacted[$key_table][$key_condition][$i]) && count($compacted[$key_table][$key_condition][$i]) > 100) {
1250 $compacted[$key_table][$key_condition][$i][$value] = $value;
1251 $counter[$key_table][$key_condition] = $i;
1254 foreach ($compacted AS $table => $values) {
1255 foreach ($values AS $field => $field_value_list) {
1256 foreach ($field_value_list AS $field_values) {
1257 $sql = "DELETE FROM `" . $table . "` WHERE `" . $field . "` IN (" .
1258 substr(str_repeat("?, ", count($field_values)), 0, -2) . ");";
1260 self::$logger->debug(self::replaceParameters($sql, $field_values));
1262 if (!self::e($sql, $field_values)) {
1263 if ($do_transaction) {
1271 if ($do_transaction) {
1278 * @brief Updates rows
1280 * Updates rows in the database. When $old_fields is set to an array,
1281 * the system will only do an update if the fields in that array changed.
1284 * Only the values in $old_fields are compared.
1285 * This is an intentional behaviour.
1288 * We include the timestamp field in $fields but not in $old_fields.
1289 * Then the row will only get the new timestamp when the other fields had changed.
1291 * When $old_fields is set to a boolean value the system will do this compare itself.
1292 * When $old_fields is set to "true" the system will do an insert if the row doesn't exists.
1295 * Only set $old_fields to a boolean value when you are sure that you will update a single row.
1296 * When you set $old_fields to "true" then $fields must contain all relevant fields!
1298 * @param string $table Table name
1299 * @param array $fields contains the fields that are updated
1300 * @param array $condition condition array with the key values
1301 * @param array|boolean $old_fields array with the old field values that are about to be replaced (true = update on duplicate)
1303 * @return boolean was the update successfull?
1304 * @throws \Exception
1306 public static function update($table, $fields, $condition, $old_fields = []) {
1308 if (empty($table) || empty($fields) || empty($condition)) {
1309 self::$logger->info('Table, fields and condition have to be set');
1313 $condition_string = self::buildCondition($condition);
1315 if (is_bool($old_fields)) {
1316 $do_insert = $old_fields;
1318 $old_fields = self::selectFirst($table, [], $condition);
1320 if (is_bool($old_fields)) {
1322 $values = array_merge($condition, $fields);
1323 return self::insert($table, $values, $do_insert);
1329 $do_update = (count($old_fields) == 0);
1331 foreach ($old_fields AS $fieldname => $content) {
1332 if (isset($fields[$fieldname])) {
1333 if (($fields[$fieldname] == $content) && !is_null($content)) {
1334 unset($fields[$fieldname]);
1341 if (!$do_update || (count($fields) == 0)) {
1345 $sql = "UPDATE ". self::formatTableName($table) . " SET `".
1346 implode("` = ?, `", array_keys($fields))."` = ?".$condition_string;
1348 $params1 = array_values($fields);
1349 $params2 = array_values($condition);
1350 $params = array_merge_recursive($params1, $params2);
1352 return self::e($sql, $params);
1356 * Retrieve a single record from a table and returns it in an associative array
1358 * @brief Retrieve a single record from a table
1359 * @param string $table
1360 * @param array $fields
1361 * @param array $condition
1362 * @param array $params
1363 * @return bool|array
1364 * @throws \Exception
1367 public static function selectFirst($table, array $fields = [], array $condition = [], $params = [])
1369 $params['limit'] = 1;
1370 $result = self::select($table, $fields, $condition, $params);
1372 if (is_bool($result)) {
1375 $row = self::fetch($result);
1376 self::close($result);
1382 * @brief Select rows from a table
1384 * @param string $table Table name
1385 * @param array $fields Array of selected fields, empty for all
1386 * @param array $condition Array of fields for condition
1387 * @param array $params Array of several parameters
1389 * @return boolean|object
1393 * $fields = array("id", "uri", "uid", "network");
1395 * $condition = array("uid" => 1, "network" => 'dspr');
1397 * $condition = array("`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr');
1399 * $params = array("order" => array("id", "received" => true), "limit" => 10);
1401 * $data = DBA::select($table, $fields, $condition, $params);
1402 * @throws \Exception
1404 public static function select($table, array $fields = [], array $condition = [], array $params = [])
1406 if (empty($table)) {
1410 if (count($fields) > 0) {
1411 $select_fields = "`" . implode("`, `", array_values($fields)) . "`";
1413 $select_fields = "*";
1416 $condition_string = self::buildCondition($condition);
1418 $param_string = self::buildParameter($params);
1420 $sql = "SELECT " . $select_fields . " FROM " . self::formatTableName($table) . $condition_string . $param_string;
1422 $result = self::p($sql, $condition);
1428 * @brief Counts the rows from a table satisfying the provided condition
1430 * @param string $table Table name
1431 * @param array $condition array of fields for condition
1438 * $condition = ["uid" => 1, "network" => 'dspr'];
1440 * $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr'];
1442 * $count = DBA::count($table, $condition);
1443 * @throws \Exception
1445 public static function count($table, array $condition = [])
1447 if (empty($table)) {
1451 $condition_string = self::buildCondition($condition);
1453 $sql = "SELECT COUNT(*) AS `count` FROM " . self::formatTableName($table) . $condition_string;
1455 $row = self::fetchFirst($sql, $condition);
1457 return $row['count'];
1461 * @brief Returns the SQL condition string built from the provided condition array
1463 * This function operates with two modes.
1464 * - Supplied with a filed/value associative array, it builds simple strict
1465 * equality conditions linked by AND.
1466 * - Supplied with a flat list, the first element is the condition string and
1467 * the following arguments are the values to be interpolated
1469 * $condition = ["uid" => 1, "network" => 'dspr'];
1471 * $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr'];
1473 * In either case, the provided array is left with the parameters only
1475 * @param array $condition
1478 public static function buildCondition(array &$condition = [])
1480 $condition_string = '';
1481 if (count($condition) > 0) {
1483 $first_key = key($condition);
1484 if (is_int($first_key)) {
1485 $condition_string = " WHERE (" . array_shift($condition) . ")";
1488 $condition_string = "";
1489 foreach ($condition as $field => $value) {
1490 if ($condition_string != "") {
1491 $condition_string .= " AND ";
1493 if (is_array($value)) {
1494 /* Workaround for MySQL Bug #64791.
1495 * Never mix data types inside any IN() condition.
1496 * In case of mixed types, cast all as string.
1497 * Logic needs to be consistent with DBA::p() data types.
1501 foreach ($value as $single_value) {
1502 if (is_int($single_value)) {
1509 if ($is_int && $is_alpha) {
1510 foreach ($value as &$ref) {
1512 $ref = (string)$ref;
1515 unset($ref); //Prevent accidental re-use.
1518 $new_values = array_merge($new_values, array_values($value));
1519 $placeholders = substr(str_repeat("?, ", count($value)), 0, -2);
1520 $condition_string .= "`" . $field . "` IN (" . $placeholders . ")";
1521 } elseif (is_null($value)) {
1522 $condition_string .= "`" . $field . "` IS NULL";
1524 $new_values[$field] = $value;
1525 $condition_string .= "`" . $field . "` = ?";
1528 $condition_string = " WHERE (" . $condition_string . ")";
1529 $condition = $new_values;
1533 return $condition_string;
1537 * @brief Returns the SQL parameter string built from the provided parameter array
1539 * @param array $params
1542 public static function buildParameter(array $params = [])
1544 $groupby_string = '';
1545 if (isset($params['group_by'])) {
1546 $groupby_string = " GROUP BY ";
1547 foreach ($params['group_by'] as $fields) {
1548 $groupby_string .= "`" . $fields . "`, ";
1550 $groupby_string = substr($groupby_string, 0, -2);
1554 if (isset($params['order'])) {
1555 $order_string = " ORDER BY ";
1556 foreach ($params['order'] AS $fields => $order) {
1557 if ($order === 'RAND()') {
1558 $order_string .= "RAND(), ";
1559 } elseif (!is_int($fields)) {
1560 $order_string .= "`" . $fields . "` " . ($order ? "DESC" : "ASC") . ", ";
1562 $order_string .= "`" . $order . "`, ";
1565 $order_string = substr($order_string, 0, -2);
1569 if (isset($params['limit']) && is_numeric($params['limit'])) {
1570 $limit_string = " LIMIT " . intval($params['limit']);
1573 if (isset($params['limit']) && is_array($params['limit'])) {
1574 $limit_string = " LIMIT " . intval($params['limit'][0]) . ", " . intval($params['limit'][1]);
1577 return $groupby_string . $order_string . $limit_string;
1581 * @brief Fills an array with data from a query
1583 * @param object $stmt statement object
1584 * @param bool $do_close
1585 * @return array Data array
1587 public static function toArray($stmt, $do_close = true) {
1588 if (is_bool($stmt)) {
1593 while ($row = self::fetch($stmt)) {
1603 * @brief Returns the error number of the last query
1605 * @return string Error number (0 if no error)
1607 public static function errorNo() {
1608 return self::$errorno;
1612 * @brief Returns the error message of the last query
1614 * @return string Error message ('' if no error)
1616 public static function errorMessage() {
1617 return self::$error;
1621 * @brief Closes the current statement
1623 * @param object $stmt statement object
1624 * @return boolean was the close successful?
1626 public static function close($stmt) {
1628 $stamp1 = microtime(true);
1630 if (!is_object($stmt)) {
1634 switch (self::$driver) {
1636 $ret = $stmt->closeCursor();
1639 // MySQLi offers both a mysqli_stmt and a mysqli_result class.
1640 // We should be careful not to assume the object type of $stmt
1641 // because DBA::p() has been able to return both types.
1642 if ($stmt instanceof mysqli_stmt) {
1643 $stmt->free_result();
1644 $ret = $stmt->close();
1645 } elseif ($stmt instanceof mysqli_result) {
1654 self::$profiler->saveTimestamp($stamp1, 'database', System::callstack());
1660 * @brief Return a list of database processes
1663 * 'list' => List of processes, separated in their different states
1664 * 'amount' => Number of concurrent database processes
1665 * @throws \Exception
1667 public static function processlist()
1669 $ret = self::p("SHOW PROCESSLIST");
1670 $data = self::toArray($ret);
1674 foreach ($data as $process) {
1675 $state = trim($process["State"]);
1677 // Filter out all non blocking processes
1678 if (!in_array($state, ["", "init", "statistics", "updating"])) {
1685 foreach ($states as $state => $usage) {
1686 if ($statelist != "") {
1689 $statelist .= $state.": ".$usage;
1691 return(["list" => $statelist, "amount" => $processes]);
1695 * Checks if $array is a filled array with at least one entry.
1697 * @param mixed $array A filled array with at least one entry
1699 * @return boolean Whether $array is a filled array or an object with rows
1701 public static function isResult($array)
1703 // It could be a return value from an update statement
1704 if (is_bool($array)) {
1708 if (is_object($array)) {
1709 return self::numRows($array) > 0;
1712 return (is_array($array) && (count($array) > 0));
1716 * @brief Callback function for "esc_array"
1718 * @param mixed $value Array value
1719 * @param string $key Array key
1720 * @param boolean $add_quotation add quotation marks for string values
1723 private static function escapeArrayCallback(&$value, $key, $add_quotation)
1725 if (!$add_quotation) {
1726 if (is_bool($value)) {
1727 $value = ($value ? '1' : '0');
1729 $value = self::escape($value);
1734 if (is_bool($value)) {
1735 $value = ($value ? 'true' : 'false');
1736 } elseif (is_float($value) || is_integer($value)) {
1737 $value = (string) $value;
1739 $value = "'" . self::escape($value) . "'";
1744 * @brief Escapes a whole array
1746 * @param mixed $arr Array with values to be escaped
1747 * @param boolean $add_quotation add quotation marks for string values
1750 public static function escapeArray(&$arr, $add_quotation = false)
1752 array_walk($arr, 'self::escapeArrayCallback', $add_quotation);