3 namespace Friendica\Database;
5 // Do not use Core\Config in this class at risk of infinite loop.
6 // Please use App->getConfigVariable() instead.
7 //use Friendica\Core\Config;
9 use Friendica\Core\System;
10 use Friendica\Util\DateTimeFormat;
18 require_once 'include/dba.php';
21 * @class MySQL database class
23 * This class is for the low level database stuff that does driver specific things.
27 public static $connected = false;
29 private static $server_info = '';
30 private static $connection;
31 private static $driver;
32 private static $error = false;
33 private static $errorno = 0;
34 private static $affected_rows = 0;
35 private static $in_transaction = false;
36 private static $in_retrial = false;
37 private static $relation = [];
38 private static $db_serveraddr = '';
39 private static $db_user = '';
40 private static $db_pass = '';
41 private static $db_name = '';
42 private static $db_charset = '';
44 public static function connect($serveraddr, $user, $pass, $db, $charset = null)
46 if (!is_null(self::$connection) && self::connected()) {
50 // We are storing these values for being able to perform a reconnect
51 self::$db_serveraddr = $serveraddr;
52 self::$db_user = $user;
53 self::$db_pass = $pass;
55 self::$db_charset = $charset;
58 $serveraddr = trim($serveraddr);
60 $serverdata = explode(':', $serveraddr);
61 $server = $serverdata[0];
63 if (count($serverdata) > 1) {
64 $port = trim($serverdata[1]);
67 $server = trim($server);
71 $charset = trim($charset);
73 if (!(strlen($server) && strlen($user))) {
77 if (class_exists('\PDO') && in_array('mysql', PDO::getAvailableDrivers())) {
78 self::$driver = 'pdo';
79 $connect = "mysql:host=".$server.";dbname=".$db;
82 $connect .= ";port=".$port;
86 $connect .= ";charset=".$charset;
90 self::$connection = @new PDO($connect, $user, $pass);
91 self::$connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
92 self::$connected = true;
93 } catch (PDOException $e) {
97 if (!self::$connected && class_exists('\mysqli')) {
98 self::$driver = 'mysqli';
101 self::$connection = @new mysqli($server, $user, $pass, $db, $port);
103 self::$connection = @new mysqli($server, $user, $pass, $db);
106 if (!mysqli_connect_errno()) {
107 self::$connected = true;
110 self::$connection->set_charset($charset);
115 // No suitable SQL driver was found.
116 if (!self::$connected) {
117 self::$driver = null;
118 self::$connection = null;
121 return self::$connected;
125 * Disconnects the current database connection
127 public static function disconnect()
129 if (is_null(self::$connection)) {
133 switch (self::$driver) {
135 self::$connection = null;
138 self::$connection->close();
139 self::$connection = null;
145 * Perform a reconnect of an existing database connection
147 public static function reconnect() {
150 $ret = self::connect(self::$db_serveraddr, self::$db_user, self::$db_pass, self::$db_name, self::$db_charset);
155 * Return the database object.
158 public static function getConnection()
160 return self::$connection;
164 * @brief Returns the MySQL server version string
166 * This function discriminate between the deprecated mysql API and the current
167 * object-oriented mysqli API. Example of returned string: 5.5.46-0+deb8u1
171 public static function serverInfo() {
172 if (self::$server_info == '') {
173 switch (self::$driver) {
175 self::$server_info = self::$connection->getAttribute(PDO::ATTR_SERVER_VERSION);
178 self::$server_info = self::$connection->server_info;
182 return self::$server_info;
186 * @brief Returns the selected database name
190 public static function databaseName() {
191 $ret = self::p("SELECT DATABASE() AS `db`");
192 $data = self::toArray($ret);
193 return $data[0]['db'];
197 * @brief Analyze a database query and log this if some conditions are met.
199 * @param string $query The database query that will be analyzed
201 private static function logIndex($query) {
204 if (!$a->getConfigVariable('system', 'db_log_index')) {
208 // Don't explain an explain statement
209 if (strtolower(substr($query, 0, 7)) == "explain") {
213 // Only do the explain on "select", "update" and "delete"
214 if (!in_array(strtolower(substr($query, 0, 6)), ["select", "update", "delete"])) {
218 $r = self::p("EXPLAIN ".$query);
219 if (!self::isResult($r)) {
223 $watchlist = explode(',', $a->getConfigVariable('system', 'db_log_index_watch'));
224 $blacklist = explode(',', $a->getConfigVariable('system', 'db_log_index_blacklist'));
226 while ($row = self::fetch($r)) {
227 if ((intval($a->getConfigVariable('system', 'db_loglimit_index')) > 0)) {
228 $log = (in_array($row['key'], $watchlist) &&
229 ($row['rows'] >= intval($a->getConfigVariable('system', 'db_loglimit_index'))));
234 if ((intval($a->getConfigVariable('system', 'db_loglimit_index_high')) > 0) && ($row['rows'] >= intval($a->getConfigVariable('system', 'db_loglimit_index_high')))) {
238 if (in_array($row['key'], $blacklist) || ($row['key'] == "")) {
243 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
244 @file_put_contents($a->getConfigVariable('system', 'db_log_index'), DateTimeFormat::utcNow()."\t".
245 $row['key']."\t".$row['rows']."\t".$row['Extra']."\t".
246 basename($backtrace[1]["file"])."\t".
247 $backtrace[1]["line"]."\t".$backtrace[2]["function"]."\t".
248 substr($query, 0, 2000)."\n", FILE_APPEND);
253 public static function escape($str) {
254 if (self::$connected) {
255 switch (self::$driver) {
257 return substr(@self::$connection->quote($str, PDO::PARAM_STR), 1, -1);
259 return @self::$connection->real_escape_string($str);
262 return str_replace("'", "\\'", $str);
266 public static function connected() {
269 if (is_null(self::$connection)) {
273 switch (self::$driver) {
275 $r = self::p("SELECT 1");
276 if (self::isResult($r)) {
277 $row = self::toArray($r);
278 $connected = ($row[0]['1'] == '1');
282 $connected = self::$connection->ping();
289 * @brief Replaces ANY_VALUE() function by MIN() function,
290 * if the database server does not support ANY_VALUE().
292 * Considerations for Standard SQL, or MySQL with ONLY_FULL_GROUP_BY (default since 5.7.5).
293 * ANY_VALUE() is available from MySQL 5.7.5 https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html
294 * A standard fall-back is to use MIN().
296 * @param string $sql An SQL string without the values
297 * @return string The input SQL string modified if necessary.
299 public static function anyValueFallback($sql) {
300 $server_info = self::serverInfo();
301 if (version_compare($server_info, '5.7.5', '<') ||
302 (stripos($server_info, 'MariaDB') !== false)) {
303 $sql = str_ireplace('ANY_VALUE(', 'MIN(', $sql);
309 * @brief beautifies the query - useful for "SHOW PROCESSLIST"
311 * This is safe when we bind the parameters later.
312 * The parameter values aren't part of the SQL.
314 * @param string $sql An SQL string without the values
315 * @return string The input SQL string modified if necessary.
317 public static function cleanQuery($sql) {
318 $search = ["\t", "\n", "\r", " "];
319 $replace = [' ', ' ', ' ', ' '];
322 $sql = str_replace($search, $replace, $sql);
323 } while ($oldsql != $sql);
330 * @brief Replaces the ? placeholders with the parameters in the $args array
332 * @param string $sql SQL query
333 * @param array $args The parameters that are to replace the ? placeholders
334 * @return string The replaced SQL query
336 private static function replaceParameters($sql, $args) {
338 foreach ($args AS $param => $value) {
339 if (is_int($args[$param]) || is_float($args[$param])) {
340 $replace = intval($args[$param]);
342 $replace = "'".self::escape($args[$param])."'";
345 $pos = strpos($sql, '?', $offset);
346 if ($pos !== false) {
347 $sql = substr_replace($sql, $replace, $pos, 1);
349 $offset = $pos + strlen($replace);
355 * @brief Convert parameter array to an universal form
356 * @param array $args Parameter array
357 * @return array universalized parameter array
359 private static function getParam($args) {
362 // When the second function parameter is an array then use this as the parameter array
363 if ((count($args) > 0) && (is_array($args[1]))) {
371 * @brief Executes a prepared statement that returns data
372 * @usage Example: $r = p("SELECT * FROM `item` WHERE `guid` = ?", $guid);
374 * Please only use it with complicated queries.
375 * For all regular queries please use dba::select or dba::exists
377 * @param string $sql SQL statement
378 * @return bool|object statement object or result object
380 public static function p($sql) {
383 $stamp1 = microtime(true);
385 $params = self::getParam(func_get_args());
387 // Renumber the array keys to be sure that they fit
390 foreach ($params AS $param) {
391 // Avoid problems with some MySQL servers and boolean values. See issue #3645
392 if (is_bool($param)) {
393 $param = (int)$param;
395 $args[++$i] = $param;
398 if (!self::$connected) {
402 if ((substr_count($sql, '?') != count($args)) && (count($args) > 0)) {
403 // Question: Should we continue or stop the query here?
404 logger('Parameter mismatch. Query "'.$sql.'" - Parameters '.print_r($args, true), LOGGER_DEBUG);
407 $sql = self::cleanQuery($sql);
408 $sql = self::anyValueFallback($sql);
412 if ($a->getConfigValue('system', 'db_callstack')) {
413 $sql = "/*".System::callstack()." */ ".$sql;
418 self::$affected_rows = 0;
420 // We have to make some things different if this function is called from "e"
421 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
423 if (isset($trace[1])) {
424 $called_from = $trace[1];
426 // We use just something that is defined to avoid warnings
427 $called_from = $trace[0];
429 // We are having an own error logging in the function "e"
430 $called_from_e = ($called_from['function'] == 'e');
432 switch (self::$driver) {
434 // If there are no arguments we use "query"
435 if (count($args) == 0) {
436 if (!$retval = self::$connection->query($sql)) {
437 $errorInfo = self::$connection->errorInfo();
438 self::$error = $errorInfo[2];
439 self::$errorno = $errorInfo[1];
443 self::$affected_rows = $retval->rowCount();
447 if (!$stmt = self::$connection->prepare($sql)) {
448 $errorInfo = self::$connection->errorInfo();
449 self::$error = $errorInfo[2];
450 self::$errorno = $errorInfo[1];
455 foreach ($args AS $param => $value) {
456 if (is_int($args[$param])) {
457 $data_type = PDO::PARAM_INT;
459 $data_type = PDO::PARAM_STR;
461 $stmt->bindParam($param, $args[$param], $data_type);
464 if (!$stmt->execute()) {
465 $errorInfo = $stmt->errorInfo();
466 self::$error = $errorInfo[2];
467 self::$errorno = $errorInfo[1];
471 self::$affected_rows = $retval->rowCount();
475 // There are SQL statements that cannot be executed with a prepared statement
476 $parts = explode(' ', $orig_sql);
477 $command = strtolower($parts[0]);
478 $can_be_prepared = in_array($command, ['select', 'update', 'insert', 'delete']);
480 // The fallback routine is called as well when there are no arguments
481 if (!$can_be_prepared || (count($args) == 0)) {
482 $retval = self::$connection->query(self::replaceParameters($sql, $args));
483 if (self::$connection->errno) {
484 self::$error = self::$connection->error;
485 self::$errorno = self::$connection->errno;
488 if (isset($retval->num_rows)) {
489 self::$affected_rows = $retval->num_rows;
491 self::$affected_rows = self::$connection->affected_rows;
497 $stmt = self::$connection->stmt_init();
499 if (!$stmt->prepare($sql)) {
500 self::$error = $stmt->error;
501 self::$errorno = $stmt->errno;
508 foreach ($args AS $param => $value) {
509 if (is_int($args[$param])) {
511 } elseif (is_float($args[$param])) {
513 } elseif (is_string($args[$param])) {
518 $values[] = &$args[$param];
521 if (count($values) > 0) {
522 array_unshift($values, $param_types);
523 call_user_func_array([$stmt, 'bind_param'], $values);
526 if (!$stmt->execute()) {
527 self::$error = self::$connection->error;
528 self::$errorno = self::$connection->errno;
531 $stmt->store_result();
533 self::$affected_rows = $retval->affected_rows;
538 // We are having an own error logging in the function "e"
539 if ((self::$errorno != 0) && !$called_from_e) {
540 // We have to preserve the error code, somewhere in the logging it get lost
541 $error = self::$error;
542 $errorno = self::$errorno;
544 logger('DB Error '.self::$errorno.': '.self::$error."\n".
545 System::callstack(8)."\n".self::replaceParameters($sql, $args));
547 // On a lost connection we try to reconnect - but only once.
548 if ($errorno == 2006) {
549 if (self::$in_retrial || !self::reconnect()) {
550 // It doesn't make sense to continue when the database connection was lost
551 if (self::$in_retrial) {
552 logger('Giving up retrial because of database error '.$errorno.': '.$error);
554 logger("Couldn't reconnect after database error ".$errorno.': '.$error);
559 logger('Reconnected after database error '.$errorno.': '.$error);
560 self::$in_retrial = true;
561 $ret = self::p($sql, $args);
562 self::$in_retrial = false;
567 self::$error = $error;
568 self::$errorno = $errorno;
571 $a->save_timestamp($stamp1, 'database');
573 if ($a->getConfigValue('system', 'db_log')) {
574 $stamp2 = microtime(true);
575 $duration = (float)($stamp2 - $stamp1);
577 if (($duration > $a->getConfigValue('system', 'db_loglimit'))) {
578 $duration = round($duration, 3);
579 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
581 @file_put_contents($a->getConfigValue('system', 'db_log'), DateTimeFormat::utcNow()."\t".$duration."\t".
582 basename($backtrace[1]["file"])."\t".
583 $backtrace[1]["line"]."\t".$backtrace[2]["function"]."\t".
584 substr(self::replaceParameters($sql, $args), 0, 2000)."\n", FILE_APPEND);
591 * @brief Executes a prepared statement like UPDATE or INSERT that doesn't return data
593 * Please use dba::delete, dba::insert, dba::update, ... instead
595 * @param string $sql SQL statement
596 * @return boolean Was the query successfull? False is returned only if an error occurred
598 public static function e($sql) {
601 $stamp = microtime(true);
603 $params = self::getParam(func_get_args());
605 // In a case of a deadlock we are repeating the query 20 times
609 $stmt = self::p($sql, $params);
611 if (is_bool($stmt)) {
613 } elseif (is_object($stmt)) {
621 } while ((self::$errorno == 1213) && (--$timeout > 0));
623 if (self::$errorno != 0) {
624 // We have to preserve the error code, somewhere in the logging it get lost
625 $error = self::$error;
626 $errorno = self::$errorno;
628 logger('DB Error '.self::$errorno.': '.self::$error."\n".
629 System::callstack(8)."\n".self::replaceParameters($sql, $params));
631 // On a lost connection we simply quit.
632 // A reconnect like in self::p could be dangerous with modifications
633 if ($errorno == 2006) {
634 logger('Giving up because of database error '.$errorno.': '.$error);
638 self::$error = $error;
639 self::$errorno = $errorno;
642 $a->save_timestamp($stamp, "database_write");
648 * @brief Check if data exists
650 * @param string $table Table name
651 * @param array $condition array of fields for condition
653 * @return boolean Are there rows for that condition?
655 public static function exists($table, $condition) {
662 if (empty($condition)) {
663 return DBStructure::existsTable($table);
667 $first_key = key($condition);
668 if (!is_int($first_key)) {
669 $fields = [$first_key];
672 $stmt = self::select($table, $fields, $condition, ['limit' => 1]);
674 if (is_bool($stmt)) {
677 $retval = (self::numRows($stmt) > 0);
686 * Fetches the first row
688 * Please use dba::selectFirst or dba::exists whenever this is possible.
690 * @brief Fetches the first row
691 * @param string $sql SQL statement
692 * @return array first row of query
694 public static function fetchFirst($sql) {
695 $params = self::getParam(func_get_args());
697 $stmt = self::p($sql, $params);
699 if (is_bool($stmt)) {
702 $retval = self::fetch($stmt);
711 * @brief Returns the number of affected rows of the last statement
713 * @return int Number of rows
715 public static function affectedRows() {
716 return self::$affected_rows;
720 * @brief Returns the number of columns of a statement
722 * @param object Statement object
723 * @return int Number of columns
725 public static function columnCount($stmt) {
726 if (!is_object($stmt)) {
729 switch (self::$driver) {
731 return $stmt->columnCount();
733 return $stmt->field_count;
738 * @brief Returns the number of rows of a statement
740 * @param PDOStatement|mysqli_result|mysqli_stmt Statement object
741 * @return int Number of rows
743 public static function numRows($stmt) {
744 if (!is_object($stmt)) {
747 switch (self::$driver) {
749 return $stmt->rowCount();
751 return $stmt->num_rows;
757 * @brief Fetch a single row
759 * @param mixed $stmt statement object
760 * @return array current row
762 public static function fetch($stmt) {
765 $stamp1 = microtime(true);
769 if (!is_object($stmt)) {
773 switch (self::$driver) {
775 $columns = $stmt->fetch(PDO::FETCH_ASSOC);
778 if (get_class($stmt) == 'mysqli_result') {
779 $columns = $stmt->fetch_assoc();
783 // This code works, but is slow
785 // Bind the result to a result array
789 for ($x = 0; $x < $stmt->field_count; $x++) {
790 $cols[] = &$cols_num[$x];
793 call_user_func_array([$stmt, 'bind_result'], $cols);
795 if (!$stmt->fetch()) {
800 // We need to get the field names for the array keys
801 // It seems that there is no better way to do this.
802 $result = $stmt->result_metadata();
803 $fields = $result->fetch_fields();
805 foreach ($cols_num AS $param => $col) {
806 $columns[$fields[$param]->name] = $col;
810 $a->save_timestamp($stamp1, 'database');
816 * @brief Insert a row into a table
818 * @param string $table Table name
819 * @param array $param parameter array
820 * @param bool $on_duplicate_update Do an update on a duplicate entry
822 * @return boolean was the insert successfull?
824 public static function insert($table, $param, $on_duplicate_update = false) {
826 if (empty($table) || empty($param)) {
827 logger('Table and fields have to be set');
831 $sql = "INSERT INTO `".self::escape($table)."` (`".implode("`, `", array_keys($param))."`) VALUES (".
832 substr(str_repeat("?, ", count($param)), 0, -2).")";
834 if ($on_duplicate_update) {
835 $sql .= " ON DUPLICATE KEY UPDATE `".implode("` = ?, `", array_keys($param))."` = ?";
837 $values = array_values($param);
838 $param = array_merge_recursive($values, $values);
841 return self::e($sql, $param);
845 * @brief Fetch the id of the last insert command
847 * @return integer Last inserted id
849 public static function lastInsertId() {
850 switch (self::$driver) {
852 $id = self::$connection->lastInsertId();
855 $id = self::$connection->insert_id;
862 * @brief Locks a table for exclusive write access
864 * This function can be extended in the future to accept a table array as well.
866 * @param string $table Table name
868 * @return boolean was the lock successful?
870 public static function lock($table) {
871 // See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
872 if (self::$driver == 'pdo') {
873 self::e("SET autocommit=0");
874 self::$connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
876 self::$connection->autocommit(false);
879 $success = self::e("LOCK TABLES `".self::escape($table)."` WRITE");
881 if (self::$driver == 'pdo') {
882 self::$connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
886 if (self::$driver == 'pdo') {
887 self::e("SET autocommit=1");
889 self::$connection->autocommit(true);
892 self::$in_transaction = true;
898 * @brief Unlocks all locked tables
900 * @return boolean was the unlock successful?
902 public static function unlock() {
903 // See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
904 self::performCommit();
906 if (self::$driver == 'pdo') {
907 self::$connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
910 $success = self::e("UNLOCK TABLES");
912 if (self::$driver == 'pdo') {
913 self::$connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
914 self::e("SET autocommit=1");
916 self::$connection->autocommit(true);
919 self::$in_transaction = false;
924 * @brief Starts a transaction
926 * @return boolean Was the command executed successfully?
928 public static function transaction() {
929 if (!self::performCommit()) {
933 switch (self::$driver) {
935 if (self::$connection->inTransaction()) {
938 if (!self::$connection->beginTransaction()) {
943 if (!self::$connection->begin_transaction()) {
949 self::$in_transaction = true;
953 private static function performCommit()
955 switch (self::$driver) {
957 if (!self::$connection->inTransaction()) {
960 return self::$connection->commit();
962 return self::$connection->commit();
968 * @brief Does a commit
970 * @return boolean Was the command executed successfully?
972 public static function commit() {
973 if (!self::performCommit()) {
976 self::$in_transaction = false;
981 * @brief Does a rollback
983 * @return boolean Was the command executed successfully?
985 public static function rollback() {
988 switch (self::$driver) {
990 if (!self::$connection->inTransaction()) {
994 $ret = self::$connection->rollBack();
997 $ret = self::$connection->rollback();
1000 self::$in_transaction = false;
1005 * @brief Build the array with the table relations
1007 * The array is build from the database definitions in DBStructure.php
1009 * This process must only be started once, since the value is cached.
1011 private static function buildRelationData() {
1012 $definition = DBStructure::definition();
1014 foreach ($definition AS $table => $structure) {
1015 foreach ($structure['fields'] AS $field => $field_struct) {
1016 if (isset($field_struct['relation'])) {
1017 foreach ($field_struct['relation'] AS $rel_table => $rel_field) {
1018 self::$relation[$rel_table][$rel_field][$table][] = $field;
1026 * @brief Delete a row from a table
1028 * @param string $table Table name
1029 * @param array $conditions Field condition(s)
1030 * @param array $options
1031 * - cascade: If true we delete records in other tables that depend on the one we're deleting through
1032 * relations (default: true)
1033 * @param boolean $in_process Internal use: Only do a commit after the last delete
1034 * @param array $callstack Internal use: prevent endless loops
1036 * @return boolean|array was the delete successful? When $in_process is set: deletion data
1038 public static function delete($table, array $conditions, array $options = [], $in_process = false, array &$callstack = [])
1040 if (empty($table) || empty($conditions)) {
1041 logger('Table and conditions have to be set');
1047 // Create a key for the loop prevention
1048 $key = $table . ':' . json_encode($conditions);
1050 // We quit when this key already exists in the callstack.
1051 if (isset($callstack[$key])) {
1055 $callstack[$key] = true;
1057 $table = self::escape($table);
1059 $commands[$key] = ['table' => $table, 'conditions' => $conditions];
1061 // Don't use "defaults" here, since it would set "false" to "true"
1062 if (isset($options['cascade'])) {
1063 $cascade = $options['cascade'];
1068 // To speed up the whole process we cache the table relations
1069 if ($cascade && count(self::$relation) == 0) {
1070 self::buildRelationData();
1073 // Is there a relation entry for the table?
1074 if ($cascade && isset(self::$relation[$table])) {
1075 // We only allow a simple "one field" relation.
1076 $field = array_keys(self::$relation[$table])[0];
1077 $rel_def = array_values(self::$relation[$table])[0];
1079 // Create a key for preventing double queries
1080 $qkey = $field . '-' . $table . ':' . json_encode($conditions);
1082 // When the search field is the relation field, we don't need to fetch the rows
1083 // This is useful when the leading record is already deleted in the frontend but the rest is done in the backend
1084 if ((count($conditions) == 1) && ($field == array_keys($conditions)[0])) {
1085 foreach ($rel_def AS $rel_table => $rel_fields) {
1086 foreach ($rel_fields AS $rel_field) {
1087 $retval = self::delete($rel_table, [$rel_field => array_values($conditions)[0]], $options, true, $callstack);
1088 $commands = array_merge($commands, $retval);
1091 // We quit when this key already exists in the callstack.
1092 } elseif (!isset($callstack[$qkey])) {
1094 $callstack[$qkey] = true;
1096 // Fetch all rows that are to be deleted
1097 $data = self::select($table, [$field], $conditions);
1099 while ($row = self::fetch($data)) {
1100 // Now we accumulate the delete commands
1101 $retval = self::delete($table, [$field => $row[$field]], $options, true, $callstack);
1102 $commands = array_merge($commands, $retval);
1107 // Since we had split the delete command we don't need the original command anymore
1108 unset($commands[$key]);
1113 // Now we finalize the process
1114 $do_transaction = !self::$in_transaction;
1116 if ($do_transaction) {
1117 self::transaction();
1123 foreach ($commands AS $command) {
1124 $conditions = $command['conditions'];
1126 $first_key = key($conditions);
1128 $condition_string = self::buildCondition($conditions);
1130 if ((count($command['conditions']) > 1) || is_int($first_key)) {
1131 $sql = "DELETE FROM `" . $command['table'] . "`" . $condition_string;
1132 logger(self::replaceParameters($sql, $conditions), LOGGER_DATA);
1134 if (!self::e($sql, $conditions)) {
1135 if ($do_transaction) {
1141 $key_table = $command['table'];
1142 $key_condition = array_keys($command['conditions'])[0];
1143 $value = array_values($command['conditions'])[0];
1145 // Split the SQL queries in chunks of 100 values
1146 // We do the $i stuff here to make the code better readable
1147 $i = isset($counter[$key_table][$key_condition]) ? $counter[$key_table][$key_condition] : 0;
1148 if (isset($compacted[$key_table][$key_condition][$i]) && count($compacted[$key_table][$key_condition][$i]) > 100) {
1152 $compacted[$key_table][$key_condition][$i][$value] = $value;
1153 $counter[$key_table][$key_condition] = $i;
1156 foreach ($compacted AS $table => $values) {
1157 foreach ($values AS $field => $field_value_list) {
1158 foreach ($field_value_list AS $field_values) {
1159 $sql = "DELETE FROM `" . $table . "` WHERE `" . $field . "` IN (" .
1160 substr(str_repeat("?, ", count($field_values)), 0, -2) . ");";
1162 logger(self::replaceParameters($sql, $field_values), LOGGER_DATA);
1164 if (!self::e($sql, $field_values)) {
1165 if ($do_transaction) {
1173 if ($do_transaction) {
1183 * @brief Updates rows
1185 * Updates rows in the database. When $old_fields is set to an array,
1186 * the system will only do an update if the fields in that array changed.
1189 * Only the values in $old_fields are compared.
1190 * This is an intentional behaviour.
1193 * We include the timestamp field in $fields but not in $old_fields.
1194 * Then the row will only get the new timestamp when the other fields had changed.
1196 * When $old_fields is set to a boolean value the system will do this compare itself.
1197 * When $old_fields is set to "true" the system will do an insert if the row doesn't exists.
1200 * Only set $old_fields to a boolean value when you are sure that you will update a single row.
1201 * When you set $old_fields to "true" then $fields must contain all relevant fields!
1203 * @param string $table Table name
1204 * @param array $fields contains the fields that are updated
1205 * @param array $condition condition array with the key values
1206 * @param array|boolean $old_fields array with the old field values that are about to be replaced (true = update on duplicate)
1208 * @return boolean was the update successfull?
1210 public static function update($table, $fields, $condition, $old_fields = []) {
1212 if (empty($table) || empty($fields) || empty($condition)) {
1213 logger('Table, fields and condition have to be set');
1217 $table = self::escape($table);
1219 $condition_string = self::buildCondition($condition);
1221 if (is_bool($old_fields)) {
1222 $do_insert = $old_fields;
1224 $old_fields = self::selectFirst($table, [], $condition);
1226 if (is_bool($old_fields)) {
1228 $values = array_merge($condition, $fields);
1229 return self::insert($table, $values, $do_insert);
1235 $do_update = (count($old_fields) == 0);
1237 foreach ($old_fields AS $fieldname => $content) {
1238 if (isset($fields[$fieldname])) {
1239 if ($fields[$fieldname] == $content) {
1240 unset($fields[$fieldname]);
1247 if (!$do_update || (count($fields) == 0)) {
1251 $sql = "UPDATE `".$table."` SET `".
1252 implode("` = ?, `", array_keys($fields))."` = ?".$condition_string;
1254 $params1 = array_values($fields);
1255 $params2 = array_values($condition);
1256 $params = array_merge_recursive($params1, $params2);
1258 return self::e($sql, $params);
1262 * Retrieve a single record from a table and returns it in an associative array
1264 * @brief Retrieve a single record from a table
1265 * @param string $table
1266 * @param array $fields
1267 * @param array $condition
1268 * @param array $params
1269 * @return bool|array
1272 public static function selectFirst($table, array $fields = [], array $condition = [], $params = [])
1274 $params['limit'] = 1;
1275 $result = self::select($table, $fields, $condition, $params);
1277 if (is_bool($result)) {
1280 $row = self::fetch($result);
1281 self::close($result);
1287 * @brief Select rows from a table
1289 * @param string $table Table name
1290 * @param array $fields Array of selected fields, empty for all
1291 * @param array $condition Array of fields for condition
1292 * @param array $params Array of several parameters
1294 * @return boolean|object
1298 * $fields = array("id", "uri", "uid", "network");
1300 * $condition = array("uid" => 1, "network" => 'dspr');
1302 * $condition = array("`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr');
1304 * $params = array("order" => array("id", "received" => true), "limit" => 10);
1306 * $data = dba::select($table, $fields, $condition, $params);
1308 public static function select($table, array $fields = [], array $condition = [], array $params = [])
1314 $table = self::escape($table);
1316 if (count($fields) > 0) {
1317 $select_fields = "`" . implode("`, `", array_values($fields)) . "`";
1319 $select_fields = "*";
1322 $condition_string = self::buildCondition($condition);
1324 $param_string = self::buildParameter($params);
1326 $sql = "SELECT " . $select_fields . " FROM `" . $table . "`" . $condition_string . $param_string;
1328 $result = self::p($sql, $condition);
1334 * @brief Counts the rows from a table satisfying the provided condition
1336 * @param string $table Table name
1337 * @param array $condition array of fields for condition
1344 * $condition = ["uid" => 1, "network" => 'dspr'];
1346 * $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr'];
1348 * $count = dba::count($table, $condition);
1350 public static function count($table, array $condition = [])
1356 $condition_string = self::buildCondition($condition);
1358 $sql = "SELECT COUNT(*) AS `count` FROM `".$table."`".$condition_string;
1360 $row = self::fetchFirst($sql, $condition);
1362 return $row['count'];
1366 * @brief Returns the SQL condition string built from the provided condition array
1368 * This function operates with two modes.
1369 * - Supplied with a filed/value associative array, it builds simple strict
1370 * equality conditions linked by AND.
1371 * - Supplied with a flat list, the first element is the condition string and
1372 * the following arguments are the values to be interpolated
1374 * $condition = ["uid" => 1, "network" => 'dspr'];
1376 * $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr'];
1378 * In either case, the provided array is left with the parameters only
1380 * @param array $condition
1383 public static function buildCondition(array &$condition = [])
1385 $condition_string = '';
1386 if (count($condition) > 0) {
1388 $first_key = key($condition);
1389 if (is_int($first_key)) {
1390 $condition_string = " WHERE (" . array_shift($condition) . ")";
1393 $condition_string = "";
1394 foreach ($condition as $field => $value) {
1395 if ($condition_string != "") {
1396 $condition_string .= " AND ";
1398 if (is_array($value)) {
1399 /* Workaround for MySQL Bug #64791.
1400 * Never mix data types inside any IN() condition.
1401 * In case of mixed types, cast all as string.
1402 * Logic needs to be consistent with dba::p() data types.
1406 foreach ($value as $single_value) {
1407 if (is_int($single_value)) {
1414 if ($is_int && $is_alpha) {
1415 foreach ($value as &$ref) {
1417 $ref = (string)$ref;
1420 unset($ref); //Prevent accidental re-use.
1423 $new_values = array_merge($new_values, array_values($value));
1424 $placeholders = substr(str_repeat("?, ", count($value)), 0, -2);
1425 $condition_string .= "`" . $field . "` IN (" . $placeholders . ")";
1427 $new_values[$field] = $value;
1428 $condition_string .= "`" . $field . "` = ?";
1431 $condition_string = " WHERE (" . $condition_string . ")";
1432 $condition = $new_values;
1436 return $condition_string;
1440 * @brief Returns the SQL parameter string built from the provided parameter array
1442 * @param array $params
1445 public static function buildParameter(array $params = [])
1448 if (isset($params['order'])) {
1449 $order_string = " ORDER BY ";
1450 foreach ($params['order'] AS $fields => $order) {
1451 if (!is_int($fields)) {
1452 $order_string .= "`" . $fields . "` " . ($order ? "DESC" : "ASC") . ", ";
1454 $order_string .= "`" . $order . "`, ";
1457 $order_string = substr($order_string, 0, -2);
1461 if (isset($params['limit']) && is_int($params['limit'])) {
1462 $limit_string = " LIMIT " . $params['limit'];
1465 if (isset($params['limit']) && is_array($params['limit'])) {
1466 $limit_string = " LIMIT " . intval($params['limit'][0]) . ", " . intval($params['limit'][1]);
1469 return $order_string.$limit_string;
1473 * @brief Fills an array with data from a query
1475 * @param object $stmt statement object
1476 * @return array Data array
1478 public static function toArray($stmt, $do_close = true) {
1479 if (is_bool($stmt)) {
1484 while ($row = self::fetch($stmt)) {
1494 * @brief Returns the error number of the last query
1496 * @return string Error number (0 if no error)
1498 public static function errorNo() {
1499 return self::$errorno;
1503 * @brief Returns the error message of the last query
1505 * @return string Error message ('' if no error)
1507 public static function errorMessage() {
1508 return self::$error;
1512 * @brief Closes the current statement
1514 * @param object $stmt statement object
1515 * @return boolean was the close successful?
1517 public static function close($stmt) {
1520 $stamp1 = microtime(true);
1522 if (!is_object($stmt)) {
1526 switch (self::$driver) {
1528 $ret = $stmt->closeCursor();
1531 // MySQLi offers both a mysqli_stmt and a mysqli_result class.
1532 // We should be careful not to assume the object type of $stmt
1533 // because dba::p() has been able to return both types.
1534 if ($stmt instanceof mysqli_stmt) {
1535 $stmt->free_result();
1536 $ret = $stmt->close();
1537 } elseif ($stmt instanceof mysqli_result) {
1546 $a->save_timestamp($stamp1, 'database');
1552 * @brief Return a list of database processes
1555 * 'list' => List of processes, separated in their different states
1556 * 'amount' => Number of concurrent database processes
1558 public static function processlist()
1560 $ret = self::p("SHOW PROCESSLIST");
1561 $data = self::toArray($ret);
1567 foreach ($data as $process) {
1568 $state = trim($process["State"]);
1570 // Filter out all non blocking processes
1571 if (!in_array($state, ["", "init", "statistics", "updating"])) {
1578 foreach ($states as $state => $usage) {
1579 if ($statelist != "") {
1582 $statelist .= $state.": ".$usage;
1584 return(["list" => $statelist, "amount" => $processes]);
1588 * Checks if $array is a filled array with at least one entry.
1590 * @param mixed $array A filled array with at least one entry
1592 * @return boolean Whether $array is a filled array or an object with rows
1594 public static function isResult($array)
1596 // It could be a return value from an update statement
1597 if (is_bool($array)) {
1601 if (is_object($array)) {
1602 return self::numRows($array) > 0;
1605 return (is_array($array) && (count($array) > 0));
1609 * @brief Callback function for "esc_array"
1611 * @param mixed $value Array value
1612 * @param string $key Array key
1613 * @param boolean $add_quotation add quotation marks for string values
1616 private static function escapeArrayCallback(&$value, $key, $add_quotation)
1618 if (!$add_quotation) {
1619 if (is_bool($value)) {
1620 $value = ($value ? '1' : '0');
1622 $value = self::escape($value);
1627 if (is_bool($value)) {
1628 $value = ($value ? 'true' : 'false');
1629 } elseif (is_float($value) || is_integer($value)) {
1630 $value = (string) $value;
1632 $value = "'" . self::escape($value) . "'";
1637 * @brief Escapes a whole array
1639 * @param mixed $arr Array with values to be escaped
1640 * @param boolean $add_quotation add quotation marks for string values
1643 public static function escapeArray(&$arr, $add_quotation = false)
1645 array_walk($arr, 'self::escapeArrayCallback', $add_quotation);