4 use Friendica\Core\L10n;
5 use Friendica\Core\System;
6 use Friendica\Database\DBM;
7 use Friendica\Database\DBStructure;
8 use Friendica\Util\DateTimeFormat;
11 * @class MySQL database class
13 * This class is for the low level database stuff that does driver specific things.
17 public static $connected = false;
19 private static $_server_info = '';
21 private static $driver;
22 private static $error = false;
23 private static $errorno = 0;
24 private static $affected_rows = 0;
25 private static $in_transaction = false;
26 private static $in_retrial = false;
27 private static $relation = [];
29 public static function connect($serveraddr, $user, $pass, $db) {
30 if (!is_null(self::$db)) {
36 $stamp1 = microtime(true);
38 $serveraddr = trim($serveraddr);
40 $serverdata = explode(':', $serveraddr);
41 $server = $serverdata[0];
43 if (count($serverdata) > 1) {
44 $port = trim($serverdata[1]);
47 $server = trim($server);
52 if (!(strlen($server) && strlen($user))) {
57 if ($a->mode == App::MODE_INSTALL) {
58 // server has to be a non-empty string that is not 'localhost' and not an IP
59 if (strlen($server) && ($server !== 'localhost') && filter_var($server, FILTER_VALIDATE_IP) === false) {
60 if (! dns_get_record($server, DNS_A + DNS_CNAME)) {
61 self::$error = L10n::t('Cannot locate DNS info for database server \'%s\'', $server);
67 if (class_exists('\PDO') && in_array('mysql', PDO::getAvailableDrivers())) {
68 self::$driver = 'pdo';
69 $connect = "mysql:host=".$server.";dbname=".$db;
72 $connect .= ";port=".$port;
75 if (isset($a->config["system"]["db_charset"])) {
76 $connect .= ";charset=".$a->config["system"]["db_charset"];
79 self::$db = @new PDO($connect, $user, $pass);
80 self::$connected = true;
81 } catch (PDOException $e) {
85 if (!self::$connected && class_exists('mysqli')) {
86 self::$driver = 'mysqli';
87 self::$db = @new mysqli($server, $user, $pass, $db, $port);
88 if (!mysqli_connect_errno()) {
89 self::$connected = true;
91 if (isset($a->config["system"]["db_charset"])) {
92 self::$db->set_charset($a->config["system"]["db_charset"]);
97 // No suitable SQL driver was found.
98 if (!self::$connected) {
102 $a->save_timestamp($stamp1, "network");
104 return self::$connected;
107 public static function reconnect() {
108 // This variable is only defined here again to prevent warning messages
109 // It is a local variable and should hopefully not interfere with the global one.
110 $a = new App(dirname(__DIR__));
112 // We have to the the variable to "null" to force a new connection
114 include '.htconfig.php';
116 $ret = self::connect($db_host, $db_user, $db_pass, $db_data);
117 unset($db_host, $db_user, $db_pass, $db_data);
123 * @brief Returns the MySQL server version string
125 * This function discriminate between the deprecated mysql API and the current
126 * object-oriented mysqli API. Example of returned string: 5.5.46-0+deb8u1
130 public static function server_info() {
131 if (self::$_server_info == '') {
132 switch (self::$driver) {
134 self::$_server_info = self::$db->getAttribute(PDO::ATTR_SERVER_VERSION);
137 self::$_server_info = self::$db->server_info;
141 return self::$_server_info;
145 * @brief Returns the selected database name
149 public static function database_name() {
150 $ret = self::p("SELECT DATABASE() AS `db`");
151 $data = self::inArray($ret);
152 return $data[0]['db'];
156 * @brief Analyze a database query and log this if some conditions are met.
158 * @param string $query The database query that will be analyzed
160 private static function logIndex($query) {
163 if (empty($a->config["system"]["db_log_index"])) {
167 // Don't explain an explain statement
168 if (strtolower(substr($query, 0, 7)) == "explain") {
172 // Only do the explain on "select", "update" and "delete"
173 if (!in_array(strtolower(substr($query, 0, 6)), ["select", "update", "delete"])) {
177 $r = self::p("EXPLAIN ".$query);
178 if (!DBM::is_result($r)) {
182 $watchlist = explode(',', $a->config["system"]["db_log_index_watch"]);
183 $blacklist = explode(',', $a->config["system"]["db_log_index_blacklist"]);
185 while ($row = dba::fetch($r)) {
186 if ((intval($a->config["system"]["db_loglimit_index"]) > 0)) {
187 $log = (in_array($row['key'], $watchlist) &&
188 ($row['rows'] >= intval($a->config["system"]["db_loglimit_index"])));
193 if ((intval($a->config["system"]["db_loglimit_index_high"]) > 0) && ($row['rows'] >= intval($a->config["system"]["db_loglimit_index_high"]))) {
197 if (in_array($row['key'], $blacklist) || ($row['key'] == "")) {
202 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
203 @file_put_contents($a->config["system"]["db_log_index"], DateTimeFormat::utcNow()."\t".
204 $row['key']."\t".$row['rows']."\t".$row['Extra']."\t".
205 basename($backtrace[1]["file"])."\t".
206 $backtrace[1]["line"]."\t".$backtrace[2]["function"]."\t".
207 substr($query, 0, 2000)."\n", FILE_APPEND);
212 public static function escape($str) {
213 switch (self::$driver) {
215 return substr(@self::$db->quote($str, PDO::PARAM_STR), 1, -1);
217 return @self::$db->real_escape_string($str);
221 public static function connected() {
224 switch (self::$driver) {
226 $r = dba::p("SELECT 1");
227 if (DBM::is_result($r)) {
228 $row = dba::inArray($r);
229 $connected = ($row[0]['1'] == '1');
233 $connected = self::$db->ping();
240 * @brief Replaces ANY_VALUE() function by MIN() function,
241 * if the database server does not support ANY_VALUE().
243 * Considerations for Standard SQL, or MySQL with ONLY_FULL_GROUP_BY (default since 5.7.5).
244 * ANY_VALUE() is available from MySQL 5.7.5 https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html
245 * A standard fall-back is to use MIN().
247 * @param string $sql An SQL string without the values
248 * @return string The input SQL string modified if necessary.
250 public static function any_value_fallback($sql) {
251 $server_info = self::server_info();
252 if (version_compare($server_info, '5.7.5', '<') ||
253 (stripos($server_info, 'MariaDB') !== false)) {
254 $sql = str_ireplace('ANY_VALUE(', 'MIN(', $sql);
260 * @brief beautifies the query - useful for "SHOW PROCESSLIST"
262 * This is safe when we bind the parameters later.
263 * The parameter values aren't part of the SQL.
265 * @param string $sql An SQL string without the values
266 * @return string The input SQL string modified if necessary.
268 public static function clean_query($sql) {
269 $search = ["\t", "\n", "\r", " "];
270 $replace = [' ', ' ', ' ', ' '];
273 $sql = str_replace($search, $replace, $sql);
274 } while ($oldsql != $sql);
281 * @brief Replaces the ? placeholders with the parameters in the $args array
283 * @param string $sql SQL query
284 * @param array $args The parameters that are to replace the ? placeholders
285 * @return string The replaced SQL query
287 private static function replaceParameters($sql, $args) {
289 foreach ($args AS $param => $value) {
290 if (is_int($args[$param]) || is_float($args[$param])) {
291 $replace = intval($args[$param]);
293 $replace = "'".self::escape($args[$param])."'";
296 $pos = strpos($sql, '?', $offset);
297 if ($pos !== false) {
298 $sql = substr_replace($sql, $replace, $pos, 1);
300 $offset = $pos + strlen($replace);
306 * @brief Convert parameter array to an universal form
307 * @param array $args Parameter array
308 * @return array universalized parameter array
310 private static function getParam($args) {
313 // When the second function parameter is an array then use this as the parameter array
314 if ((count($args) > 0) && (is_array($args[1]))) {
322 * @brief Executes a prepared statement that returns data
323 * @usage Example: $r = p("SELECT * FROM `item` WHERE `guid` = ?", $guid);
325 * Please only use it with complicated queries.
326 * For all regular queries please use dba::select or dba::exists
328 * @param string $sql SQL statement
329 * @return bool|object statement object
331 public static function p($sql) {
334 $stamp1 = microtime(true);
336 $params = self::getParam(func_get_args());
338 // Renumber the array keys to be sure that they fit
341 foreach ($params AS $param) {
342 // Avoid problems with some MySQL servers and boolean values. See issue #3645
343 if (is_bool($param)) {
344 $param = (int)$param;
346 $args[++$i] = $param;
349 if (!self::$connected) {
353 if ((substr_count($sql, '?') != count($args)) && (count($args) > 0)) {
354 // Question: Should we continue or stop the query here?
355 logger('Parameter mismatch. Query "'.$sql.'" - Parameters '.print_r($args, true), LOGGER_DEBUG);
358 $sql = self::clean_query($sql);
359 $sql = self::any_value_fallback($sql);
363 if (x($a->config,'system') && x($a->config['system'], 'db_callstack')) {
364 $sql = "/*".System::callstack()." */ ".$sql;
369 self::$affected_rows = 0;
371 // We have to make some things different if this function is called from "e"
372 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
374 if (isset($trace[1])) {
375 $called_from = $trace[1];
377 // We use just something that is defined to avoid warnings
378 $called_from = $trace[0];
380 // We are having an own error logging in the function "e"
381 $called_from_e = ($called_from['function'] == 'e');
383 switch (self::$driver) {
385 // If there are no arguments we use "query"
386 if (count($args) == 0) {
387 if (!$retval = self::$db->query($sql)) {
388 $errorInfo = self::$db->errorInfo();
389 self::$error = $errorInfo[2];
390 self::$errorno = $errorInfo[1];
394 self::$affected_rows = $retval->rowCount();
398 if (!$stmt = self::$db->prepare($sql)) {
399 $errorInfo = self::$db->errorInfo();
400 self::$error = $errorInfo[2];
401 self::$errorno = $errorInfo[1];
406 foreach ($args AS $param => $value) {
407 $stmt->bindParam($param, $args[$param]);
410 if (!$stmt->execute()) {
411 $errorInfo = $stmt->errorInfo();
412 self::$error = $errorInfo[2];
413 self::$errorno = $errorInfo[1];
417 self::$affected_rows = $retval->rowCount();
421 // There are SQL statements that cannot be executed with a prepared statement
422 $parts = explode(' ', $orig_sql);
423 $command = strtolower($parts[0]);
424 $can_be_prepared = in_array($command, ['select', 'update', 'insert', 'delete']);
426 // The fallback routine is called as well when there are no arguments
427 if (!$can_be_prepared || (count($args) == 0)) {
428 $retval = self::$db->query(self::replaceParameters($sql, $args));
429 if (self::$db->errno) {
430 self::$error = self::$db->error;
431 self::$errorno = self::$db->errno;
434 if (isset($retval->num_rows)) {
435 self::$affected_rows = $retval->num_rows;
437 self::$affected_rows = self::$db->affected_rows;
443 $stmt = self::$db->stmt_init();
445 if (!$stmt->prepare($sql)) {
446 self::$error = $stmt->error;
447 self::$errorno = $stmt->errno;
454 foreach ($args AS $param => $value) {
455 if (is_int($args[$param])) {
457 } elseif (is_float($args[$param])) {
459 } elseif (is_string($args[$param])) {
464 $values[] = &$args[$param];
467 if (count($values) > 0) {
468 array_unshift($values, $param_types);
469 call_user_func_array([$stmt, 'bind_param'], $values);
472 if (!$stmt->execute()) {
473 self::$error = self::$db->error;
474 self::$errorno = self::$db->errno;
477 $stmt->store_result();
479 self::$affected_rows = $retval->affected_rows;
484 // We are having an own error logging in the function "e"
485 if ((self::$errorno != 0) && !$called_from_e) {
486 // We have to preserve the error code, somewhere in the logging it get lost
487 $error = self::$error;
488 $errorno = self::$errorno;
490 logger('DB Error '.self::$errorno.': '.self::$error."\n".
491 System::callstack(8)."\n".self::replaceParameters($sql, $args));
493 // On a lost connection we try to reconnect - but only once.
494 if ($errorno == 2006) {
495 if (self::$in_retrial || !self::reconnect()) {
496 // It doesn't make sense to continue when the database connection was lost
497 if (self::$in_retrial) {
498 logger('Giving up retrial because of database error '.$errorno.': '.$error);
500 logger("Couldn't reconnect after database error ".$errorno.': '.$error);
505 logger('Reconnected after database error '.$errorno.': '.$error);
506 self::$in_retrial = true;
507 return self::p($sql, $args);
511 self::$error = $error;
512 self::$errorno = $errorno;
515 $a->save_timestamp($stamp1, 'database');
517 if (x($a->config,'system') && x($a->config['system'], 'db_log')) {
519 $stamp2 = microtime(true);
520 $duration = (float)($stamp2 - $stamp1);
522 if (($duration > $a->config["system"]["db_loglimit"])) {
523 $duration = round($duration, 3);
524 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
526 @file_put_contents($a->config["system"]["db_log"], DateTimeFormat::utcNow()."\t".$duration."\t".
527 basename($backtrace[1]["file"])."\t".
528 $backtrace[1]["line"]."\t".$backtrace[2]["function"]."\t".
529 substr(self::replaceParameters($sql, $args), 0, 2000)."\n", FILE_APPEND);
536 * @brief Executes a prepared statement like UPDATE or INSERT that doesn't return data
538 * Please use dba::delete, dba::insert, dba::update, ... instead
540 * @param string $sql SQL statement
541 * @return boolean Was the query successfull? False is returned only if an error occurred
543 public static function e($sql) {
546 $stamp = microtime(true);
548 $params = self::getParam(func_get_args());
550 // In a case of a deadlock we are repeating the query 20 times
554 $stmt = self::p($sql, $params);
556 if (is_bool($stmt)) {
558 } elseif (is_object($stmt)) {
566 } while ((self::$errorno == 1213) && (--$timeout > 0));
568 if (self::$errorno != 0) {
569 // We have to preserve the error code, somewhere in the logging it get lost
570 $error = self::$error;
571 $errorno = self::$errorno;
573 logger('DB Error '.self::$errorno.': '.self::$error."\n".
574 System::callstack(8)."\n".self::replaceParameters($sql, $params));
576 self::$error = $error;
577 self::$errorno = $errorno;
580 $a->save_timestamp($stamp, "database_write");
586 * @brief Check if data exists
588 * @param string $table Table name
589 * @param array $condition array of fields for condition
591 * @return boolean Are there rows for that condition?
593 public static function exists($table, $condition) {
601 $first_key = key($condition);
602 if (!is_int($first_key)) {
603 $fields = [$first_key];
606 $stmt = self::select($table, $fields, $condition, ['limit' => 1]);
608 if (is_bool($stmt)) {
611 $retval = (self::num_rows($stmt) > 0);
620 * Fetches the first row
622 * Please use dba::selectFirst or dba::exists whenever this is possible.
624 * @brief Fetches the first row
625 * @param string $sql SQL statement
626 * @return array first row of query
628 public static function fetch_first($sql) {
629 $params = self::getParam(func_get_args());
631 $stmt = self::p($sql, $params);
633 if (is_bool($stmt)) {
636 $retval = self::fetch($stmt);
645 * @brief Returns the number of affected rows of the last statement
647 * @return int Number of rows
649 public static function affected_rows() {
650 return self::$affected_rows;
654 * @brief Returns the number of columns of a statement
656 * @param object Statement object
657 * @return int Number of columns
659 public static function columnCount($stmt) {
660 if (!is_object($stmt)) {
663 switch (self::$driver) {
665 return $stmt->columnCount();
667 return $stmt->field_count;
672 * @brief Returns the number of rows of a statement
674 * @param PDOStatement|mysqli_result|mysqli_stmt Statement object
675 * @return int Number of rows
677 public static function num_rows($stmt) {
678 if (!is_object($stmt)) {
681 switch (self::$driver) {
683 return $stmt->rowCount();
685 return $stmt->num_rows;
691 * @brief Fetch a single row
693 * @param mixed $stmt statement object
694 * @return array current row
696 public static function fetch($stmt) {
699 $stamp1 = microtime(true);
703 if (!is_object($stmt)) {
707 switch (self::$driver) {
709 $columns = $stmt->fetch(PDO::FETCH_ASSOC);
712 if (get_class($stmt) == 'mysqli_result') {
713 $columns = $stmt->fetch_assoc();
717 // This code works, but is slow
719 // Bind the result to a result array
723 for ($x = 0; $x < $stmt->field_count; $x++) {
724 $cols[] = &$cols_num[$x];
727 call_user_func_array([$stmt, 'bind_result'], $cols);
729 if (!$stmt->fetch()) {
734 // We need to get the field names for the array keys
735 // It seems that there is no better way to do this.
736 $result = $stmt->result_metadata();
737 $fields = $result->fetch_fields();
739 foreach ($cols_num AS $param => $col) {
740 $columns[$fields[$param]->name] = $col;
744 $a->save_timestamp($stamp1, 'database');
750 * @brief Insert a row into a table
752 * @param string $table Table name
753 * @param array $param parameter array
754 * @param bool $on_duplicate_update Do an update on a duplicate entry
756 * @return boolean was the insert successfull?
758 public static function insert($table, $param, $on_duplicate_update = false) {
760 if (empty($table) || empty($param)) {
761 logger('Table and fields have to be set');
765 $sql = "INSERT INTO `".self::escape($table)."` (`".implode("`, `", array_keys($param))."`) VALUES (".
766 substr(str_repeat("?, ", count($param)), 0, -2).")";
768 if ($on_duplicate_update) {
769 $sql .= " ON DUPLICATE KEY UPDATE `".implode("` = ?, `", array_keys($param))."` = ?";
771 $values = array_values($param);
772 $param = array_merge_recursive($values, $values);
775 return self::e($sql, $param);
779 * @brief Fetch the id of the last insert command
781 * @return integer Last inserted id
783 public static function lastInsertId() {
784 switch (self::$driver) {
786 $id = self::$db->lastInsertId();
789 $id = self::$db->insert_id;
796 * @brief Locks a table for exclusive write access
798 * This function can be extended in the future to accept a table array as well.
800 * @param string $table Table name
802 * @return boolean was the lock successful?
804 public static function lock($table) {
805 // See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
806 self::e("SET autocommit=0");
807 $success = self::e("LOCK TABLES `".self::escape($table)."` WRITE");
809 self::e("SET autocommit=1");
811 self::$in_transaction = true;
817 * @brief Unlocks all locked tables
819 * @return boolean was the unlock successful?
821 public static function unlock() {
822 // See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
824 $success = self::e("UNLOCK TABLES");
825 self::e("SET autocommit=1");
826 self::$in_transaction = false;
831 * @brief Starts a transaction
833 * @return boolean Was the command executed successfully?
835 public static function transaction() {
836 if (!self::e('COMMIT')) {
839 if (!self::e('START TRANSACTION')) {
842 self::$in_transaction = true;
847 * @brief Does a commit
849 * @return boolean Was the command executed successfully?
851 public static function commit() {
852 if (!self::e('COMMIT')) {
855 self::$in_transaction = false;
860 * @brief Does a rollback
862 * @return boolean Was the command executed successfully?
864 public static function rollback() {
865 if (!self::e('ROLLBACK')) {
868 self::$in_transaction = false;
873 * @brief Build the array with the table relations
875 * The array is build from the database definitions in DBStructure.php
877 * This process must only be started once, since the value is cached.
879 private static function buildRelationData() {
880 $definition = DBStructure::definition();
882 foreach ($definition AS $table => $structure) {
883 foreach ($structure['fields'] AS $field => $field_struct) {
884 if (isset($field_struct['relation'])) {
885 foreach ($field_struct['relation'] AS $rel_table => $rel_field) {
886 self::$relation[$rel_table][$rel_field][$table][] = $field;
894 * @brief Delete a row from a table
896 * @param string $table Table name
897 * @param array $conditions Field condition(s)
898 * @param array $options
899 * - cascade: If true we delete records in other tables that depend on the one we're deleting through
900 * relations (default: true)
901 * @param boolean $in_process Internal use: Only do a commit after the last delete
902 * @param array $callstack Internal use: prevent endless loops
904 * @return boolean|array was the delete successful? When $in_process is set: deletion data
906 public static function delete($table, array $conditions, array $options = [], $in_process = false, array &$callstack = [])
908 if (empty($table) || empty($conditions)) {
909 logger('Table and conditions have to be set');
915 // Create a key for the loop prevention
916 $key = $table . ':' . implode(':', array_keys($conditions)) . ':' . implode(':', $conditions);
918 // We quit when this key already exists in the callstack.
919 if (isset($callstack[$key])) {
923 $callstack[$key] = true;
925 $table = self::escape($table);
927 $commands[$key] = ['table' => $table, 'conditions' => $conditions];
929 $cascade = defaults($options, 'cascade', true);
931 // To speed up the whole process we cache the table relations
932 if ($cascade && count(self::$relation) == 0) {
933 self::buildRelationData();
936 // Is there a relation entry for the table?
937 if ($cascade && isset(self::$relation[$table])) {
938 // We only allow a simple "one field" relation.
939 $field = array_keys(self::$relation[$table])[0];
940 $rel_def = array_values(self::$relation[$table])[0];
942 // Create a key for preventing double queries
943 $qkey = $field . '-' . $table . ':' . implode(':', array_keys($conditions)) . ':' . implode(':', $conditions);
945 // When the search field is the relation field, we don't need to fetch the rows
946 // This is useful when the leading record is already deleted in the frontend but the rest is done in the backend
947 if ((count($conditions) == 1) && ($field == array_keys($conditions)[0])) {
948 foreach ($rel_def AS $rel_table => $rel_fields) {
949 foreach ($rel_fields AS $rel_field) {
950 $retval = self::delete($rel_table, [$rel_field => array_values($conditions)[0]], $options, true, $callstack);
951 $commands = array_merge($commands, $retval);
954 // We quit when this key already exists in the callstack.
955 } elseif (!isset($callstack[$qkey])) {
957 $callstack[$qkey] = true;
959 // Fetch all rows that are to be deleted
960 $data = self::select($table, [$field], $conditions);
962 while ($row = self::fetch($data)) {
963 // Now we accumulate the delete commands
964 $retval = self::delete($table, [$field => $row[$field]], $options, true, $callstack);
965 $commands = array_merge($commands, $retval);
970 // Since we had split the delete command we don't need the original command anymore
971 unset($commands[$key]);
976 // Now we finalize the process
977 $do_transaction = !self::$in_transaction;
979 if ($do_transaction) {
986 foreach ($commands AS $command) {
987 $conditions = $command['conditions'];
989 $first_key = key($conditions);
991 $condition_string = self::buildCondition($conditions);
993 if ((count($command['conditions']) > 1) || is_int($first_key)) {
994 $sql = "DELETE FROM `" . $command['table'] . "`" . $condition_string;
995 logger(self::replaceParameters($sql, $conditions), LOGGER_DATA);
997 if (!self::e($sql, $conditions)) {
998 if ($do_transaction) {
1004 $key_table = $command['table'];
1005 $key_condition = array_keys($command['conditions'])[0];
1006 $value = array_values($command['conditions'])[0];
1008 // Split the SQL queries in chunks of 100 values
1009 // We do the $i stuff here to make the code better readable
1010 $i = $counter[$key_table][$key_condition];
1011 if (isset($compacted[$key_table][$key_condition][$i]) && count($compacted[$key_table][$key_condition][$i]) > 100) {
1015 $compacted[$key_table][$key_condition][$i][$value] = $value;
1016 $counter[$key_table][$key_condition] = $i;
1019 foreach ($compacted AS $table => $values) {
1020 foreach ($values AS $field => $field_value_list) {
1021 foreach ($field_value_list AS $field_values) {
1022 $sql = "DELETE FROM `" . $table . "` WHERE `" . $field . "` IN (" .
1023 substr(str_repeat("?, ", count($field_values)), 0, -2) . ");";
1025 logger(self::replaceParameters($sql, $field_values), LOGGER_DATA);
1027 if (!self::e($sql, $field_values)) {
1028 if ($do_transaction) {
1036 if ($do_transaction) {
1046 * @brief Updates rows
1048 * Updates rows in the database. When $old_fields is set to an array,
1049 * the system will only do an update if the fields in that array changed.
1052 * Only the values in $old_fields are compared.
1053 * This is an intentional behaviour.
1056 * We include the timestamp field in $fields but not in $old_fields.
1057 * Then the row will only get the new timestamp when the other fields had changed.
1059 * When $old_fields is set to a boolean value the system will do this compare itself.
1060 * When $old_fields is set to "true" the system will do an insert if the row doesn't exists.
1063 * Only set $old_fields to a boolean value when you are sure that you will update a single row.
1064 * When you set $old_fields to "true" then $fields must contain all relevant fields!
1066 * @param string $table Table name
1067 * @param array $fields contains the fields that are updated
1068 * @param array $condition condition array with the key values
1069 * @param array|boolean $old_fields array with the old field values that are about to be replaced (true = update on duplicate)
1071 * @return boolean was the update successfull?
1073 public static function update($table, $fields, $condition, $old_fields = []) {
1075 if (empty($table) || empty($fields) || empty($condition)) {
1076 logger('Table, fields and condition have to be set');
1080 $table = self::escape($table);
1082 $condition_string = self::buildCondition($condition);
1084 if (is_bool($old_fields)) {
1085 $do_insert = $old_fields;
1087 $old_fields = self::selectFirst($table, [], $condition);
1089 if (is_bool($old_fields)) {
1091 $values = array_merge($condition, $fields);
1092 return self::insert($table, $values, $do_insert);
1098 $do_update = (count($old_fields) == 0);
1100 foreach ($old_fields AS $fieldname => $content) {
1101 if (isset($fields[$fieldname])) {
1102 if ($fields[$fieldname] == $content) {
1103 unset($fields[$fieldname]);
1110 if (!$do_update || (count($fields) == 0)) {
1114 $sql = "UPDATE `".$table."` SET `".
1115 implode("` = ?, `", array_keys($fields))."` = ?".$condition_string;
1117 $params1 = array_values($fields);
1118 $params2 = array_values($condition);
1119 $params = array_merge_recursive($params1, $params2);
1121 return self::e($sql, $params);
1125 * Retrieve a single record from a table and returns it in an associative array
1127 * @brief Retrieve a single record from a table
1128 * @param string $table
1129 * @param array $fields
1130 * @param array $condition
1131 * @param array $params
1132 * @return bool|array
1135 public static function selectFirst($table, array $fields = [], array $condition = [], $params = [])
1137 $params['limit'] = 1;
1138 $result = self::select($table, $fields, $condition, $params);
1140 if (is_bool($result)) {
1143 $row = self::fetch($result);
1144 self::close($result);
1150 * @brief Select rows from a table
1152 * @param string $table Table name
1153 * @param array $fields Array of selected fields, empty for all
1154 * @param array $condition Array of fields for condition
1155 * @param array $params Array of several parameters
1157 * @return boolean|object
1161 * $fields = array("id", "uri", "uid", "network");
1163 * $condition = array("uid" => 1, "network" => 'dspr');
1165 * $condition = array("`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr');
1167 * $params = array("order" => array("id", "received" => true), "limit" => 10);
1169 * $data = dba::select($table, $fields, $condition, $params);
1171 public static function select($table, array $fields = [], array $condition = [], array $params = [])
1177 $table = self::escape($table);
1179 if (count($fields) > 0) {
1180 $select_fields = "`" . implode("`, `", array_values($fields)) . "`";
1182 $select_fields = "*";
1185 $condition_string = self::buildCondition($condition);
1188 if (isset($params['order'])) {
1189 $order_string = " ORDER BY ";
1190 foreach ($params['order'] AS $fields => $order) {
1191 if (!is_int($fields)) {
1192 $order_string .= "`" . $fields . "` " . ($order ? "DESC" : "ASC") . ", ";
1194 $order_string .= "`" . $order . "`, ";
1197 $order_string = substr($order_string, 0, -2);
1201 if (isset($params['limit']) && is_int($params['limit'])) {
1202 $limit_string = " LIMIT " . $params['limit'];
1205 if (isset($params['limit']) && is_array($params['limit'])) {
1206 $limit_string = " LIMIT " . intval($params['limit'][0]) . ", " . intval($params['limit'][1]);
1209 $sql = "SELECT " . $select_fields . " FROM `" . $table . "`" . $condition_string . $order_string . $limit_string;
1211 $result = self::p($sql, $condition);
1217 * @brief Counts the rows from a table satisfying the provided condition
1219 * @param string $table Table name
1220 * @param array $condition array of fields for condition
1227 * $condition = ["uid" => 1, "network" => 'dspr'];
1229 * $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr'];
1231 * $count = dba::count($table, $condition);
1233 public static function count($table, array $condition = [])
1239 $condition_string = self::buildCondition($condition);
1241 $sql = "SELECT COUNT(*) AS `count` FROM `".$table."`".$condition_string;
1243 $row = self::fetch_first($sql, $condition);
1245 return $row['count'];
1249 * @brief Returns the SQL condition string built from the provided condition array
1251 * This function operates with two modes.
1252 * - Supplied with a filed/value associative array, it builds simple strict
1253 * equality conditions linked by AND.
1254 * - Supplied with a flat list, the first element is the condition string and
1255 * the following arguments are the values to be interpolated
1257 * $condition = ["uid" => 1, "network" => 'dspr'];
1259 * $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr'];
1261 * In either case, the provided array is left with the parameters only
1263 * @param array $condition
1266 private static function buildCondition(array &$condition = [])
1268 $condition_string = '';
1269 if (count($condition) > 0) {
1271 $first_key = key($condition);
1272 if (is_int($first_key)) {
1273 $condition_string = " WHERE ".array_shift($condition);
1276 $condition_string = "";
1277 foreach ($condition as $field => $value) {
1278 if ($condition_string != "") {
1279 $condition_string .= " AND ";
1281 if (is_array($value)) {
1282 $new_values = array_merge($new_values, array_values($value));
1283 $placeholders = substr(str_repeat("?, ", count($value)), 0, -2);
1284 $condition_string .= "`" . $field . "` IN (" . $placeholders . ")";
1286 $new_values[$field] = $value;
1287 $condition_string .= "`" . $field . "` = ?";
1290 $condition_string = " WHERE " . $condition_string;
1291 $condition = $new_values;
1295 return $condition_string;
1299 * @brief Fills an array with data from a query
1301 * @param object $stmt statement object
1302 * @return array Data array
1304 public static function inArray($stmt, $do_close = true) {
1305 if (is_bool($stmt)) {
1310 while ($row = self::fetch($stmt)) {
1320 * @brief Returns the error number of the last query
1322 * @return string Error number (0 if no error)
1324 public static function errorNo() {
1325 return self::$errorno;
1329 * @brief Returns the error message of the last query
1331 * @return string Error message ('' if no error)
1333 public static function errorMessage() {
1334 return self::$error;
1338 * @brief Closes the current statement
1340 * @param object $stmt statement object
1341 * @return boolean was the close successful?
1343 public static function close($stmt) {
1346 $stamp1 = microtime(true);
1348 if (!is_object($stmt)) {
1352 switch (self::$driver) {
1354 $ret = $stmt->closeCursor();
1357 $stmt->free_result();
1358 $ret = $stmt->close();
1362 $a->save_timestamp($stamp1, 'database');
1368 function dbesc($str) {
1369 if (dba::$connected) {
1370 return(dba::escape($str));
1372 return(str_replace("'","\\'",$str));
1377 * @brief execute SQL query with printf style args - deprecated
1379 * Please use the dba:: functions instead:
1380 * dba::select, dba::exists, dba::insert
1381 * dba::delete, dba::update, dba::p, dba::e
1383 * @param $args Query parameters (1 to N parameters of different types)
1384 * @return array|bool Query array
1387 $args = func_get_args();
1390 if (!dba::$connected) {
1394 $sql = dba::clean_query($sql);
1395 $sql = dba::any_value_fallback($sql);
1397 $stmt = @vsprintf($sql, $args);
1399 $ret = dba::p($stmt);
1401 if (is_bool($ret)) {
1405 $columns = dba::columnCount($ret);
1407 $data = dba::inArray($ret);
1409 if ((count($data) == 0) && ($columns == 0)) {
1416 function dba_timer() {
1417 return microtime(true);