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;
17 * @class MySQL database class
19 * This class is for the low level database stuff that does driver specific things.
23 public static $connected = false;
25 private static $server_info = '';
27 private static $driver;
28 private static $error = false;
29 private static $errorno = 0;
30 private static $affected_rows = 0;
31 private static $in_transaction = false;
32 private static $in_retrial = false;
33 private static $relation = [];
34 private static $db_serveraddr = '';
35 private static $db_user = '';
36 private static $db_pass = '';
37 private static $db_name = '';
38 private static $db_charset = '';
40 public static function connect($serveraddr, $user, $pass, $db, $charset = null)
42 if (!is_null(self::$db) && self::connected()) {
46 // We are storing these values for being able to perform a reconnect
47 self::$db_serveraddr = $serveraddr;
48 self::$db_user = $user;
49 self::$db_pass = $pass;
51 self::$db_charset = $charset;
54 $serveraddr = trim($serveraddr);
56 $serverdata = explode(':', $serveraddr);
57 $server = $serverdata[0];
59 if (count($serverdata) > 1) {
60 $port = trim($serverdata[1]);
63 $server = trim($server);
67 $charset = trim($charset);
69 if (!(strlen($server) && strlen($user))) {
73 if (class_exists('\PDO') && in_array('mysql', PDO::getAvailableDrivers())) {
74 self::$driver = 'pdo';
75 $connect = "mysql:host=".$server.";dbname=".$db;
78 $connect .= ";port=".$port;
82 $connect .= ";charset=".$charset;
86 self::$db = @new PDO($connect, $user, $pass);
87 self::$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
88 self::$connected = true;
89 } catch (PDOException $e) {
93 if (!self::$connected && class_exists('\mysqli')) {
94 self::$driver = 'mysqli';
97 self::$db = @new mysqli($server, $user, $pass, $db, $port);
99 self::$db = @new mysqli($server, $user, $pass, $db);
102 if (!mysqli_connect_errno()) {
103 self::$connected = true;
106 self::$db->set_charset($charset);
111 // No suitable SQL driver was found.
112 if (!self::$connected) {
113 self::$driver = null;
117 return self::$connected;
121 * Disconnects the current database connection
123 public static function disconnect()
125 if (is_null(self::$db)) {
129 switch (self::$driver) {
141 * Perform a reconnect of an existing database connection
143 public static function reconnect() {
146 $ret = self::connect(self::$db_serveraddr, self::$db_user, self::$db_pass, self::$db_name, self::$db_charset);
151 * Return the database object.
154 public static function get_db()
160 * @brief Returns the MySQL server version string
162 * This function discriminate between the deprecated mysql API and the current
163 * object-oriented mysqli API. Example of returned string: 5.5.46-0+deb8u1
167 public static function server_info() {
168 if (self::$server_info == '') {
169 switch (self::$driver) {
171 self::$server_info = self::$db->getAttribute(PDO::ATTR_SERVER_VERSION);
174 self::$server_info = self::$db->server_info;
178 return self::$server_info;
182 * @brief Returns the selected database name
186 public static function database_name() {
187 $ret = self::p("SELECT DATABASE() AS `db`");
188 $data = self::inArray($ret);
189 return $data[0]['db'];
193 * @brief Analyze a database query and log this if some conditions are met.
195 * @param string $query The database query that will be analyzed
197 private static function logIndex($query) {
200 if (!$a->getConfigVariable('system', 'db_log_index')) {
204 // Don't explain an explain statement
205 if (strtolower(substr($query, 0, 7)) == "explain") {
209 // Only do the explain on "select", "update" and "delete"
210 if (!in_array(strtolower(substr($query, 0, 6)), ["select", "update", "delete"])) {
214 $r = self::p("EXPLAIN ".$query);
215 if (!DBM::is_result($r)) {
219 $watchlist = explode(',', $a->getConfigVariable('system', 'db_log_index_watch'));
220 $blacklist = explode(',', $a->getConfigVariable('system', 'db_log_index_blacklist'));
222 while ($row = self::fetch($r)) {
223 if ((intval($a->getConfigVariable('system', 'db_loglimit_index')) > 0)) {
224 $log = (in_array($row['key'], $watchlist) &&
225 ($row['rows'] >= intval($a->getConfigVariable('system', 'db_loglimit_index'))));
230 if ((intval($a->getConfigVariable('system', 'db_loglimit_index_high')) > 0) && ($row['rows'] >= intval($a->getConfigVariable('system', 'db_loglimit_index_high')))) {
234 if (in_array($row['key'], $blacklist) || ($row['key'] == "")) {
239 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
240 @file_put_contents($a->getConfigVariable('system', 'db_log_index'), DateTimeFormat::utcNow()."\t".
241 $row['key']."\t".$row['rows']."\t".$row['Extra']."\t".
242 basename($backtrace[1]["file"])."\t".
243 $backtrace[1]["line"]."\t".$backtrace[2]["function"]."\t".
244 substr($query, 0, 2000)."\n", FILE_APPEND);
249 public static function escape($str) {
250 switch (self::$driver) {
252 return substr(@self::$db->quote($str, PDO::PARAM_STR), 1, -1);
254 return @self::$db->real_escape_string($str);
258 public static function connected() {
261 if (is_null(self::$db)) {
265 switch (self::$driver) {
267 $r = self::p("SELECT 1");
268 if (DBM::is_result($r)) {
269 $row = self::inArray($r);
270 $connected = ($row[0]['1'] == '1');
274 $connected = self::$db->ping();
281 * @brief Replaces ANY_VALUE() function by MIN() function,
282 * if the database server does not support ANY_VALUE().
284 * Considerations for Standard SQL, or MySQL with ONLY_FULL_GROUP_BY (default since 5.7.5).
285 * ANY_VALUE() is available from MySQL 5.7.5 https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html
286 * A standard fall-back is to use MIN().
288 * @param string $sql An SQL string without the values
289 * @return string The input SQL string modified if necessary.
291 public static function any_value_fallback($sql) {
292 $server_info = self::server_info();
293 if (version_compare($server_info, '5.7.5', '<') ||
294 (stripos($server_info, 'MariaDB') !== false)) {
295 $sql = str_ireplace('ANY_VALUE(', 'MIN(', $sql);
301 * @brief beautifies the query - useful for "SHOW PROCESSLIST"
303 * This is safe when we bind the parameters later.
304 * The parameter values aren't part of the SQL.
306 * @param string $sql An SQL string without the values
307 * @return string The input SQL string modified if necessary.
309 public static function clean_query($sql) {
310 $search = ["\t", "\n", "\r", " "];
311 $replace = [' ', ' ', ' ', ' '];
314 $sql = str_replace($search, $replace, $sql);
315 } while ($oldsql != $sql);
322 * @brief Replaces the ? placeholders with the parameters in the $args array
324 * @param string $sql SQL query
325 * @param array $args The parameters that are to replace the ? placeholders
326 * @return string The replaced SQL query
328 private static function replaceParameters($sql, $args) {
330 foreach ($args AS $param => $value) {
331 if (is_int($args[$param]) || is_float($args[$param])) {
332 $replace = intval($args[$param]);
334 $replace = "'".self::escape($args[$param])."'";
337 $pos = strpos($sql, '?', $offset);
338 if ($pos !== false) {
339 $sql = substr_replace($sql, $replace, $pos, 1);
341 $offset = $pos + strlen($replace);
347 * @brief Convert parameter array to an universal form
348 * @param array $args Parameter array
349 * @return array universalized parameter array
351 private static function getParam($args) {
354 // When the second function parameter is an array then use this as the parameter array
355 if ((count($args) > 0) && (is_array($args[1]))) {
363 * @brief Executes a prepared statement that returns data
364 * @usage Example: $r = p("SELECT * FROM `item` WHERE `guid` = ?", $guid);
366 * Please only use it with complicated queries.
367 * For all regular queries please use dba::select or dba::exists
369 * @param string $sql SQL statement
370 * @return bool|object statement object or result object
372 public static function p($sql) {
375 $stamp1 = microtime(true);
377 $params = self::getParam(func_get_args());
379 // Renumber the array keys to be sure that they fit
382 foreach ($params AS $param) {
383 // Avoid problems with some MySQL servers and boolean values. See issue #3645
384 if (is_bool($param)) {
385 $param = (int)$param;
387 $args[++$i] = $param;
390 if (!self::$connected) {
394 if ((substr_count($sql, '?') != count($args)) && (count($args) > 0)) {
395 // Question: Should we continue or stop the query here?
396 logger('Parameter mismatch. Query "'.$sql.'" - Parameters '.print_r($args, true), LOGGER_DEBUG);
399 $sql = self::clean_query($sql);
400 $sql = self::any_value_fallback($sql);
404 if ($a->getConfigValue('system', 'db_callstack')) {
405 $sql = "/*".System::callstack()." */ ".$sql;
410 self::$affected_rows = 0;
412 // We have to make some things different if this function is called from "e"
413 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
415 if (isset($trace[1])) {
416 $called_from = $trace[1];
418 // We use just something that is defined to avoid warnings
419 $called_from = $trace[0];
421 // We are having an own error logging in the function "e"
422 $called_from_e = ($called_from['function'] == 'e');
424 switch (self::$driver) {
426 // If there are no arguments we use "query"
427 if (count($args) == 0) {
428 if (!$retval = self::$db->query($sql)) {
429 $errorInfo = self::$db->errorInfo();
430 self::$error = $errorInfo[2];
431 self::$errorno = $errorInfo[1];
435 self::$affected_rows = $retval->rowCount();
439 if (!$stmt = self::$db->prepare($sql)) {
440 $errorInfo = self::$db->errorInfo();
441 self::$error = $errorInfo[2];
442 self::$errorno = $errorInfo[1];
447 foreach ($args AS $param => $value) {
448 if (is_int($args[$param])) {
449 $data_type = PDO::PARAM_INT;
451 $data_type = PDO::PARAM_STR;
453 $stmt->bindParam($param, $args[$param], $data_type);
456 if (!$stmt->execute()) {
457 $errorInfo = $stmt->errorInfo();
458 self::$error = $errorInfo[2];
459 self::$errorno = $errorInfo[1];
463 self::$affected_rows = $retval->rowCount();
467 // There are SQL statements that cannot be executed with a prepared statement
468 $parts = explode(' ', $orig_sql);
469 $command = strtolower($parts[0]);
470 $can_be_prepared = in_array($command, ['select', 'update', 'insert', 'delete']);
472 // The fallback routine is called as well when there are no arguments
473 if (!$can_be_prepared || (count($args) == 0)) {
474 $retval = self::$db->query(self::replaceParameters($sql, $args));
475 if (self::$db->errno) {
476 self::$error = self::$db->error;
477 self::$errorno = self::$db->errno;
480 if (isset($retval->num_rows)) {
481 self::$affected_rows = $retval->num_rows;
483 self::$affected_rows = self::$db->affected_rows;
489 $stmt = self::$db->stmt_init();
491 if (!$stmt->prepare($sql)) {
492 self::$error = $stmt->error;
493 self::$errorno = $stmt->errno;
500 foreach ($args AS $param => $value) {
501 if (is_int($args[$param])) {
503 } elseif (is_float($args[$param])) {
505 } elseif (is_string($args[$param])) {
510 $values[] = &$args[$param];
513 if (count($values) > 0) {
514 array_unshift($values, $param_types);
515 call_user_func_array([$stmt, 'bind_param'], $values);
518 if (!$stmt->execute()) {
519 self::$error = self::$db->error;
520 self::$errorno = self::$db->errno;
523 $stmt->store_result();
525 self::$affected_rows = $retval->affected_rows;
530 // We are having an own error logging in the function "e"
531 if ((self::$errorno != 0) && !$called_from_e) {
532 // We have to preserve the error code, somewhere in the logging it get lost
533 $error = self::$error;
534 $errorno = self::$errorno;
536 logger('DB Error '.self::$errorno.': '.self::$error."\n".
537 System::callstack(8)."\n".self::replaceParameters($sql, $args));
539 // On a lost connection we try to reconnect - but only once.
540 if ($errorno == 2006) {
541 if (self::$in_retrial || !self::reconnect()) {
542 // It doesn't make sense to continue when the database connection was lost
543 if (self::$in_retrial) {
544 logger('Giving up retrial because of database error '.$errorno.': '.$error);
546 logger("Couldn't reconnect after database error ".$errorno.': '.$error);
551 logger('Reconnected after database error '.$errorno.': '.$error);
552 self::$in_retrial = true;
553 $ret = self::p($sql, $args);
554 self::$in_retrial = false;
559 self::$error = $error;
560 self::$errorno = $errorno;
563 $a->save_timestamp($stamp1, 'database');
565 if ($a->getConfigValue('system', 'db_log')) {
566 $stamp2 = microtime(true);
567 $duration = (float)($stamp2 - $stamp1);
569 if (($duration > $a->getConfigValue('system', 'db_loglimit'))) {
570 $duration = round($duration, 3);
571 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
573 @file_put_contents($a->getConfigValue('system', 'db_log'), DateTimeFormat::utcNow()."\t".$duration."\t".
574 basename($backtrace[1]["file"])."\t".
575 $backtrace[1]["line"]."\t".$backtrace[2]["function"]."\t".
576 substr(self::replaceParameters($sql, $args), 0, 2000)."\n", FILE_APPEND);
583 * @brief Executes a prepared statement like UPDATE or INSERT that doesn't return data
585 * Please use dba::delete, dba::insert, dba::update, ... instead
587 * @param string $sql SQL statement
588 * @return boolean Was the query successfull? False is returned only if an error occurred
590 public static function e($sql) {
593 $stamp = microtime(true);
595 $params = self::getParam(func_get_args());
597 // In a case of a deadlock we are repeating the query 20 times
601 $stmt = self::p($sql, $params);
603 if (is_bool($stmt)) {
605 } elseif (is_object($stmt)) {
613 } while ((self::$errorno == 1213) && (--$timeout > 0));
615 if (self::$errorno != 0) {
616 // We have to preserve the error code, somewhere in the logging it get lost
617 $error = self::$error;
618 $errorno = self::$errorno;
620 logger('DB Error '.self::$errorno.': '.self::$error."\n".
621 System::callstack(8)."\n".self::replaceParameters($sql, $params));
623 // On a lost connection we simply quit.
624 // A reconnect like in self::p could be dangerous with modifications
625 if ($errorno == 2006) {
626 logger('Giving up because of database error '.$errorno.': '.$error);
630 self::$error = $error;
631 self::$errorno = $errorno;
634 $a->save_timestamp($stamp, "database_write");
640 * @brief Check if data exists
642 * @param string $table Table name
643 * @param array $condition array of fields for condition
645 * @return boolean Are there rows for that condition?
647 public static function exists($table, $condition) {
654 if (empty($condition)) {
655 return DBStructure::existsTable($table);
659 $first_key = key($condition);
660 if (!is_int($first_key)) {
661 $fields = [$first_key];
664 $stmt = self::select($table, $fields, $condition, ['limit' => 1]);
666 if (is_bool($stmt)) {
669 $retval = (self::num_rows($stmt) > 0);
678 * Fetches the first row
680 * Please use dba::selectFirst or dba::exists whenever this is possible.
682 * @brief Fetches the first row
683 * @param string $sql SQL statement
684 * @return array first row of query
686 public static function fetch_first($sql) {
687 $params = self::getParam(func_get_args());
689 $stmt = self::p($sql, $params);
691 if (is_bool($stmt)) {
694 $retval = self::fetch($stmt);
703 * @brief Returns the number of affected rows of the last statement
705 * @return int Number of rows
707 public static function affectedRows() {
708 return self::$affected_rows;
712 * @brief Returns the number of columns of a statement
714 * @param object Statement object
715 * @return int Number of columns
717 public static function columnCount($stmt) {
718 if (!is_object($stmt)) {
721 switch (self::$driver) {
723 return $stmt->columnCount();
725 return $stmt->field_count;
730 * @brief Returns the number of rows of a statement
732 * @param PDOStatement|mysqli_result|mysqli_stmt Statement object
733 * @return int Number of rows
735 public static function num_rows($stmt) {
736 if (!is_object($stmt)) {
739 switch (self::$driver) {
741 return $stmt->rowCount();
743 return $stmt->num_rows;
749 * @brief Fetch a single row
751 * @param mixed $stmt statement object
752 * @return array current row
754 public static function fetch($stmt) {
757 $stamp1 = microtime(true);
761 if (!is_object($stmt)) {
765 switch (self::$driver) {
767 $columns = $stmt->fetch(PDO::FETCH_ASSOC);
770 if (get_class($stmt) == 'mysqli_result') {
771 $columns = $stmt->fetch_assoc();
775 // This code works, but is slow
777 // Bind the result to a result array
781 for ($x = 0; $x < $stmt->field_count; $x++) {
782 $cols[] = &$cols_num[$x];
785 call_user_func_array([$stmt, 'bind_result'], $cols);
787 if (!$stmt->fetch()) {
792 // We need to get the field names for the array keys
793 // It seems that there is no better way to do this.
794 $result = $stmt->result_metadata();
795 $fields = $result->fetch_fields();
797 foreach ($cols_num AS $param => $col) {
798 $columns[$fields[$param]->name] = $col;
802 $a->save_timestamp($stamp1, 'database');
808 * @brief Insert a row into a table
810 * @param string $table Table name
811 * @param array $param parameter array
812 * @param bool $on_duplicate_update Do an update on a duplicate entry
814 * @return boolean was the insert successfull?
816 public static function insert($table, $param, $on_duplicate_update = false) {
818 if (empty($table) || empty($param)) {
819 logger('Table and fields have to be set');
823 $sql = "INSERT INTO `".self::escape($table)."` (`".implode("`, `", array_keys($param))."`) VALUES (".
824 substr(str_repeat("?, ", count($param)), 0, -2).")";
826 if ($on_duplicate_update) {
827 $sql .= " ON DUPLICATE KEY UPDATE `".implode("` = ?, `", array_keys($param))."` = ?";
829 $values = array_values($param);
830 $param = array_merge_recursive($values, $values);
833 return self::e($sql, $param);
837 * @brief Fetch the id of the last insert command
839 * @return integer Last inserted id
841 public static function lastInsertId() {
842 switch (self::$driver) {
844 $id = self::$db->lastInsertId();
847 $id = self::$db->insert_id;
854 * @brief Locks a table for exclusive write access
856 * This function can be extended in the future to accept a table array as well.
858 * @param string $table Table name
860 * @return boolean was the lock successful?
862 public static function lock($table) {
863 // See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
864 if (self::$driver == 'pdo') {
865 self::e("SET autocommit=0");
866 self::$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
868 self::$db->autocommit(false);
871 $success = self::e("LOCK TABLES `".self::escape($table)."` WRITE");
873 if (self::$driver == 'pdo') {
874 self::$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
878 if (self::$driver == 'pdo') {
879 self::e("SET autocommit=1");
881 self::$db->autocommit(true);
884 self::$in_transaction = true;
890 * @brief Unlocks all locked tables
892 * @return boolean was the unlock successful?
894 public static function unlock() {
895 // See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
896 self::performCommit();
898 if (self::$driver == 'pdo') {
899 self::$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
902 $success = self::e("UNLOCK TABLES");
904 if (self::$driver == 'pdo') {
905 self::$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
906 self::e("SET autocommit=1");
908 self::$db->autocommit(true);
911 self::$in_transaction = false;
916 * @brief Starts a transaction
918 * @return boolean Was the command executed successfully?
920 public static function transaction() {
921 if (!self::performCommit()) {
925 switch (self::$driver) {
927 if (self::$db->inTransaction()) {
930 if (!self::$db->beginTransaction()) {
935 if (!self::$db->begin_transaction()) {
941 self::$in_transaction = true;
945 private static function performCommit()
947 switch (self::$driver) {
949 if (!self::$db->inTransaction()) {
952 return self::$db->commit();
954 return self::$db->commit();
960 * @brief Does a commit
962 * @return boolean Was the command executed successfully?
964 public static function commit() {
965 if (!self::performCommit()) {
968 self::$in_transaction = false;
973 * @brief Does a rollback
975 * @return boolean Was the command executed successfully?
977 public static function rollback() {
980 switch (self::$driver) {
982 if (!self::$db->inTransaction()) {
986 $ret = self::$db->rollBack();
989 $ret = self::$db->rollback();
992 self::$in_transaction = false;
997 * @brief Build the array with the table relations
999 * The array is build from the database definitions in DBStructure.php
1001 * This process must only be started once, since the value is cached.
1003 private static function buildRelationData() {
1004 $definition = DBStructure::definition();
1006 foreach ($definition AS $table => $structure) {
1007 foreach ($structure['fields'] AS $field => $field_struct) {
1008 if (isset($field_struct['relation'])) {
1009 foreach ($field_struct['relation'] AS $rel_table => $rel_field) {
1010 self::$relation[$rel_table][$rel_field][$table][] = $field;
1018 * @brief Delete a row from a table
1020 * @param string $table Table name
1021 * @param array $conditions Field condition(s)
1022 * @param array $options
1023 * - cascade: If true we delete records in other tables that depend on the one we're deleting through
1024 * relations (default: true)
1025 * @param boolean $in_process Internal use: Only do a commit after the last delete
1026 * @param array $callstack Internal use: prevent endless loops
1028 * @return boolean|array was the delete successful? When $in_process is set: deletion data
1030 public static function delete($table, array $conditions, array $options = [], $in_process = false, array &$callstack = [])
1032 if (empty($table) || empty($conditions)) {
1033 logger('Table and conditions have to be set');
1039 // Create a key for the loop prevention
1040 $key = $table . ':' . json_encode($conditions);
1042 // We quit when this key already exists in the callstack.
1043 if (isset($callstack[$key])) {
1047 $callstack[$key] = true;
1049 $table = self::escape($table);
1051 $commands[$key] = ['table' => $table, 'conditions' => $conditions];
1053 $cascade = defaults($options, 'cascade', true);
1055 // To speed up the whole process we cache the table relations
1056 if ($cascade && count(self::$relation) == 0) {
1057 self::buildRelationData();
1060 // Is there a relation entry for the table?
1061 if ($cascade && isset(self::$relation[$table])) {
1062 // We only allow a simple "one field" relation.
1063 $field = array_keys(self::$relation[$table])[0];
1064 $rel_def = array_values(self::$relation[$table])[0];
1066 // Create a key for preventing double queries
1067 $qkey = $field . '-' . $table . ':' . json_encode($conditions);
1069 // When the search field is the relation field, we don't need to fetch the rows
1070 // This is useful when the leading record is already deleted in the frontend but the rest is done in the backend
1071 if ((count($conditions) == 1) && ($field == array_keys($conditions)[0])) {
1072 foreach ($rel_def AS $rel_table => $rel_fields) {
1073 foreach ($rel_fields AS $rel_field) {
1074 $retval = self::delete($rel_table, [$rel_field => array_values($conditions)[0]], $options, true, $callstack);
1075 $commands = array_merge($commands, $retval);
1078 // We quit when this key already exists in the callstack.
1079 } elseif (!isset($callstack[$qkey])) {
1081 $callstack[$qkey] = true;
1083 // Fetch all rows that are to be deleted
1084 $data = self::select($table, [$field], $conditions);
1086 while ($row = self::fetch($data)) {
1087 // Now we accumulate the delete commands
1088 $retval = self::delete($table, [$field => $row[$field]], $options, true, $callstack);
1089 $commands = array_merge($commands, $retval);
1094 // Since we had split the delete command we don't need the original command anymore
1095 unset($commands[$key]);
1100 // Now we finalize the process
1101 $do_transaction = !self::$in_transaction;
1103 if ($do_transaction) {
1104 self::transaction();
1110 foreach ($commands AS $command) {
1111 $conditions = $command['conditions'];
1113 $first_key = key($conditions);
1115 $condition_string = self::buildCondition($conditions);
1117 if ((count($command['conditions']) > 1) || is_int($first_key)) {
1118 $sql = "DELETE FROM `" . $command['table'] . "`" . $condition_string;
1119 logger(self::replaceParameters($sql, $conditions), LOGGER_DATA);
1121 if (!self::e($sql, $conditions)) {
1122 if ($do_transaction) {
1128 $key_table = $command['table'];
1129 $key_condition = array_keys($command['conditions'])[0];
1130 $value = array_values($command['conditions'])[0];
1132 // Split the SQL queries in chunks of 100 values
1133 // We do the $i stuff here to make the code better readable
1134 $i = isset($counter[$key_table][$key_condition]) ? $counter[$key_table][$key_condition] : 0;
1135 if (isset($compacted[$key_table][$key_condition][$i]) && count($compacted[$key_table][$key_condition][$i]) > 100) {
1139 $compacted[$key_table][$key_condition][$i][$value] = $value;
1140 $counter[$key_table][$key_condition] = $i;
1143 foreach ($compacted AS $table => $values) {
1144 foreach ($values AS $field => $field_value_list) {
1145 foreach ($field_value_list AS $field_values) {
1146 $sql = "DELETE FROM `" . $table . "` WHERE `" . $field . "` IN (" .
1147 substr(str_repeat("?, ", count($field_values)), 0, -2) . ");";
1149 logger(self::replaceParameters($sql, $field_values), LOGGER_DATA);
1151 if (!self::e($sql, $field_values)) {
1152 if ($do_transaction) {
1160 if ($do_transaction) {
1170 * @brief Updates rows
1172 * Updates rows in the database. When $old_fields is set to an array,
1173 * the system will only do an update if the fields in that array changed.
1176 * Only the values in $old_fields are compared.
1177 * This is an intentional behaviour.
1180 * We include the timestamp field in $fields but not in $old_fields.
1181 * Then the row will only get the new timestamp when the other fields had changed.
1183 * When $old_fields is set to a boolean value the system will do this compare itself.
1184 * When $old_fields is set to "true" the system will do an insert if the row doesn't exists.
1187 * Only set $old_fields to a boolean value when you are sure that you will update a single row.
1188 * When you set $old_fields to "true" then $fields must contain all relevant fields!
1190 * @param string $table Table name
1191 * @param array $fields contains the fields that are updated
1192 * @param array $condition condition array with the key values
1193 * @param array|boolean $old_fields array with the old field values that are about to be replaced (true = update on duplicate)
1195 * @return boolean was the update successfull?
1197 public static function update($table, $fields, $condition, $old_fields = []) {
1199 if (empty($table) || empty($fields) || empty($condition)) {
1200 logger('Table, fields and condition have to be set');
1204 $table = self::escape($table);
1206 $condition_string = self::buildCondition($condition);
1208 if (is_bool($old_fields)) {
1209 $do_insert = $old_fields;
1211 $old_fields = self::selectFirst($table, [], $condition);
1213 if (is_bool($old_fields)) {
1215 $values = array_merge($condition, $fields);
1216 return self::insert($table, $values, $do_insert);
1222 $do_update = (count($old_fields) == 0);
1224 foreach ($old_fields AS $fieldname => $content) {
1225 if (isset($fields[$fieldname])) {
1226 if ($fields[$fieldname] == $content) {
1227 unset($fields[$fieldname]);
1234 if (!$do_update || (count($fields) == 0)) {
1238 $sql = "UPDATE `".$table."` SET `".
1239 implode("` = ?, `", array_keys($fields))."` = ?".$condition_string;
1241 $params1 = array_values($fields);
1242 $params2 = array_values($condition);
1243 $params = array_merge_recursive($params1, $params2);
1245 return self::e($sql, $params);
1249 * Retrieve a single record from a table and returns it in an associative array
1251 * @brief Retrieve a single record from a table
1252 * @param string $table
1253 * @param array $fields
1254 * @param array $condition
1255 * @param array $params
1256 * @return bool|array
1259 public static function selectFirst($table, array $fields = [], array $condition = [], $params = [])
1261 $params['limit'] = 1;
1262 $result = self::select($table, $fields, $condition, $params);
1264 if (is_bool($result)) {
1267 $row = self::fetch($result);
1268 self::close($result);
1274 * @brief Select rows from a table
1276 * @param string $table Table name
1277 * @param array $fields Array of selected fields, empty for all
1278 * @param array $condition Array of fields for condition
1279 * @param array $params Array of several parameters
1281 * @return boolean|object
1285 * $fields = array("id", "uri", "uid", "network");
1287 * $condition = array("uid" => 1, "network" => 'dspr');
1289 * $condition = array("`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr');
1291 * $params = array("order" => array("id", "received" => true), "limit" => 10);
1293 * $data = dba::select($table, $fields, $condition, $params);
1295 public static function select($table, array $fields = [], array $condition = [], array $params = [])
1301 $table = self::escape($table);
1303 if (count($fields) > 0) {
1304 $select_fields = "`" . implode("`, `", array_values($fields)) . "`";
1306 $select_fields = "*";
1309 $condition_string = self::buildCondition($condition);
1311 $param_string = self::buildParameter($params);
1313 $sql = "SELECT " . $select_fields . " FROM `" . $table . "`" . $condition_string . $param_string;
1315 $result = self::p($sql, $condition);
1321 * @brief Counts the rows from a table satisfying the provided condition
1323 * @param string $table Table name
1324 * @param array $condition array of fields for condition
1331 * $condition = ["uid" => 1, "network" => 'dspr'];
1333 * $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr'];
1335 * $count = dba::count($table, $condition);
1337 public static function count($table, array $condition = [])
1343 $condition_string = self::buildCondition($condition);
1345 $sql = "SELECT COUNT(*) AS `count` FROM `".$table."`".$condition_string;
1347 $row = self::fetch_first($sql, $condition);
1349 return $row['count'];
1353 * @brief Returns the SQL condition string built from the provided condition array
1355 * This function operates with two modes.
1356 * - Supplied with a filed/value associative array, it builds simple strict
1357 * equality conditions linked by AND.
1358 * - Supplied with a flat list, the first element is the condition string and
1359 * the following arguments are the values to be interpolated
1361 * $condition = ["uid" => 1, "network" => 'dspr'];
1363 * $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr'];
1365 * In either case, the provided array is left with the parameters only
1367 * @param array $condition
1370 public static function buildCondition(array &$condition = [])
1372 $condition_string = '';
1373 if (count($condition) > 0) {
1375 $first_key = key($condition);
1376 if (is_int($first_key)) {
1377 $condition_string = " WHERE (" . array_shift($condition) . ")";
1380 $condition_string = "";
1381 foreach ($condition as $field => $value) {
1382 if ($condition_string != "") {
1383 $condition_string .= " AND ";
1385 if (is_array($value)) {
1386 /* Workaround for MySQL Bug #64791.
1387 * Never mix data types inside any IN() condition.
1388 * In case of mixed types, cast all as string.
1389 * Logic needs to be consistent with dba::p() data types.
1393 foreach ($value as $single_value) {
1394 if (is_int($single_value)) {
1401 if ($is_int && $is_alpha) {
1402 foreach ($value as &$ref) {
1404 $ref = (string)$ref;
1407 unset($ref); //Prevent accidental re-use.
1410 $new_values = array_merge($new_values, array_values($value));
1411 $placeholders = substr(str_repeat("?, ", count($value)), 0, -2);
1412 $condition_string .= "`" . $field . "` IN (" . $placeholders . ")";
1414 $new_values[$field] = $value;
1415 $condition_string .= "`" . $field . "` = ?";
1418 $condition_string = " WHERE (" . $condition_string . ")";
1419 $condition = $new_values;
1423 return $condition_string;
1427 * @brief Returns the SQL parameter string built from the provided parameter array
1429 * @param array $params
1432 public static function buildParameter(array $params = [])
1435 if (isset($params['order'])) {
1436 $order_string = " ORDER BY ";
1437 foreach ($params['order'] AS $fields => $order) {
1438 if (!is_int($fields)) {
1439 $order_string .= "`" . $fields . "` " . ($order ? "DESC" : "ASC") . ", ";
1441 $order_string .= "`" . $order . "`, ";
1444 $order_string = substr($order_string, 0, -2);
1448 if (isset($params['limit']) && is_int($params['limit'])) {
1449 $limit_string = " LIMIT " . $params['limit'];
1452 if (isset($params['limit']) && is_array($params['limit'])) {
1453 $limit_string = " LIMIT " . intval($params['limit'][0]) . ", " . intval($params['limit'][1]);
1456 return $order_string.$limit_string;
1460 * @brief Fills an array with data from a query
1462 * @param object $stmt statement object
1463 * @return array Data array
1465 public static function inArray($stmt, $do_close = true) {
1466 if (is_bool($stmt)) {
1471 while ($row = self::fetch($stmt)) {
1481 * @brief Returns the error number of the last query
1483 * @return string Error number (0 if no error)
1485 public static function errorNo() {
1486 return self::$errorno;
1490 * @brief Returns the error message of the last query
1492 * @return string Error message ('' if no error)
1494 public static function errorMessage() {
1495 return self::$error;
1499 * @brief Closes the current statement
1501 * @param object $stmt statement object
1502 * @return boolean was the close successful?
1504 public static function close($stmt) {
1507 $stamp1 = microtime(true);
1509 if (!is_object($stmt)) {
1513 switch (self::$driver) {
1515 $ret = $stmt->closeCursor();
1518 // MySQLi offers both a mysqli_stmt and a mysqli_result class.
1519 // We should be careful not to assume the object type of $stmt
1520 // because dba::p() has been able to return both types.
1521 if ($stmt instanceof mysqli_stmt) {
1522 $stmt->free_result();
1523 $ret = $stmt->close();
1524 } elseif ($stmt instanceof mysqli_result) {
1533 $a->save_timestamp($stamp1, 'database');