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) && self::connected()) {
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 (class_exists('\PDO') && in_array('mysql', PDO::getAvailableDrivers())) {
58 self::$driver = 'pdo';
59 $connect = "mysql:host=".$server.";dbname=".$db;
62 $connect .= ";port=".$port;
65 if (isset($a->config["system"]["db_charset"])) {
66 $connect .= ";charset=".$a->config["system"]["db_charset"];
69 self::$db = @new PDO($connect, $user, $pass);
70 self::$connected = true;
71 } catch (PDOException $e) {
75 if (!self::$connected && class_exists('mysqli')) {
76 self::$driver = 'mysqli';
77 self::$db = @new mysqli($server, $user, $pass, $db, $port);
78 if (!mysqli_connect_errno()) {
79 self::$connected = true;
81 if (isset($a->config["system"]["db_charset"])) {
82 self::$db->set_charset($a->config["system"]["db_charset"]);
87 // No suitable SQL driver was found.
88 if (!self::$connected) {
92 $a->save_timestamp($stamp1, "network");
94 return self::$connected;
97 public static function reconnect() {
98 // This variable is only defined here again to prevent warning messages
99 // It is a local variable and should hopefully not interfere with the global one.
100 $a = new App(dirname(__DIR__));
102 // We have to the the variable to "null" to force a new connection
104 include '.htconfig.php';
106 $ret = self::connect($db_host, $db_user, $db_pass, $db_data);
107 unset($db_host, $db_user, $db_pass, $db_data);
113 * Disconnects the current database connection
115 public static function disconnect()
117 if (is_null(self::$db)) {
121 switch (self::$driver) {
133 * Return the database object.
136 public static function get_db()
142 * @brief Returns the MySQL server version string
144 * This function discriminate between the deprecated mysql API and the current
145 * object-oriented mysqli API. Example of returned string: 5.5.46-0+deb8u1
149 public static function server_info() {
150 if (self::$_server_info == '') {
151 switch (self::$driver) {
153 self::$_server_info = self::$db->getAttribute(PDO::ATTR_SERVER_VERSION);
156 self::$_server_info = self::$db->server_info;
160 return self::$_server_info;
164 * @brief Returns the selected database name
168 public static function database_name() {
169 $ret = self::p("SELECT DATABASE() AS `db`");
170 $data = self::inArray($ret);
171 return $data[0]['db'];
175 * @brief Analyze a database query and log this if some conditions are met.
177 * @param string $query The database query that will be analyzed
179 private static function logIndex($query) {
182 if (empty($a->config["system"]["db_log_index"])) {
186 // Don't explain an explain statement
187 if (strtolower(substr($query, 0, 7)) == "explain") {
191 // Only do the explain on "select", "update" and "delete"
192 if (!in_array(strtolower(substr($query, 0, 6)), ["select", "update", "delete"])) {
196 $r = self::p("EXPLAIN ".$query);
197 if (!DBM::is_result($r)) {
201 $watchlist = explode(',', $a->config["system"]["db_log_index_watch"]);
202 $blacklist = explode(',', $a->config["system"]["db_log_index_blacklist"]);
204 while ($row = dba::fetch($r)) {
205 if ((intval($a->config["system"]["db_loglimit_index"]) > 0)) {
206 $log = (in_array($row['key'], $watchlist) &&
207 ($row['rows'] >= intval($a->config["system"]["db_loglimit_index"])));
212 if ((intval($a->config["system"]["db_loglimit_index_high"]) > 0) && ($row['rows'] >= intval($a->config["system"]["db_loglimit_index_high"]))) {
216 if (in_array($row['key'], $blacklist) || ($row['key'] == "")) {
221 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
222 @file_put_contents($a->config["system"]["db_log_index"], DateTimeFormat::utcNow()."\t".
223 $row['key']."\t".$row['rows']."\t".$row['Extra']."\t".
224 basename($backtrace[1]["file"])."\t".
225 $backtrace[1]["line"]."\t".$backtrace[2]["function"]."\t".
226 substr($query, 0, 2000)."\n", FILE_APPEND);
231 public static function escape($str) {
232 switch (self::$driver) {
234 return substr(@self::$db->quote($str, PDO::PARAM_STR), 1, -1);
236 return @self::$db->real_escape_string($str);
240 public static function connected() {
243 switch (self::$driver) {
245 $r = dba::p("SELECT 1");
246 if (DBM::is_result($r)) {
247 $row = dba::inArray($r);
248 $connected = ($row[0]['1'] == '1');
252 $connected = self::$db->ping();
259 * @brief Replaces ANY_VALUE() function by MIN() function,
260 * if the database server does not support ANY_VALUE().
262 * Considerations for Standard SQL, or MySQL with ONLY_FULL_GROUP_BY (default since 5.7.5).
263 * ANY_VALUE() is available from MySQL 5.7.5 https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html
264 * A standard fall-back is to use MIN().
266 * @param string $sql An SQL string without the values
267 * @return string The input SQL string modified if necessary.
269 public static function any_value_fallback($sql) {
270 $server_info = self::server_info();
271 if (version_compare($server_info, '5.7.5', '<') ||
272 (stripos($server_info, 'MariaDB') !== false)) {
273 $sql = str_ireplace('ANY_VALUE(', 'MIN(', $sql);
279 * @brief beautifies the query - useful for "SHOW PROCESSLIST"
281 * This is safe when we bind the parameters later.
282 * The parameter values aren't part of the SQL.
284 * @param string $sql An SQL string without the values
285 * @return string The input SQL string modified if necessary.
287 public static function clean_query($sql) {
288 $search = ["\t", "\n", "\r", " "];
289 $replace = [' ', ' ', ' ', ' '];
292 $sql = str_replace($search, $replace, $sql);
293 } while ($oldsql != $sql);
300 * @brief Replaces the ? placeholders with the parameters in the $args array
302 * @param string $sql SQL query
303 * @param array $args The parameters that are to replace the ? placeholders
304 * @return string The replaced SQL query
306 private static function replaceParameters($sql, $args) {
308 foreach ($args AS $param => $value) {
309 if (is_int($args[$param]) || is_float($args[$param])) {
310 $replace = intval($args[$param]);
312 $replace = "'".self::escape($args[$param])."'";
315 $pos = strpos($sql, '?', $offset);
316 if ($pos !== false) {
317 $sql = substr_replace($sql, $replace, $pos, 1);
319 $offset = $pos + strlen($replace);
325 * @brief Convert parameter array to an universal form
326 * @param array $args Parameter array
327 * @return array universalized parameter array
329 private static function getParam($args) {
332 // When the second function parameter is an array then use this as the parameter array
333 if ((count($args) > 0) && (is_array($args[1]))) {
341 * @brief Executes a prepared statement that returns data
342 * @usage Example: $r = p("SELECT * FROM `item` WHERE `guid` = ?", $guid);
344 * Please only use it with complicated queries.
345 * For all regular queries please use dba::select or dba::exists
347 * @param string $sql SQL statement
348 * @return bool|object statement object
350 public static function p($sql) {
353 $stamp1 = microtime(true);
355 $params = self::getParam(func_get_args());
357 // Renumber the array keys to be sure that they fit
360 foreach ($params AS $param) {
361 // Avoid problems with some MySQL servers and boolean values. See issue #3645
362 if (is_bool($param)) {
363 $param = (int)$param;
365 $args[++$i] = $param;
368 if (!self::$connected) {
372 if ((substr_count($sql, '?') != count($args)) && (count($args) > 0)) {
373 // Question: Should we continue or stop the query here?
374 logger('Parameter mismatch. Query "'.$sql.'" - Parameters '.print_r($args, true), LOGGER_DEBUG);
377 $sql = self::clean_query($sql);
378 $sql = self::any_value_fallback($sql);
382 if (x($a->config,'system') && x($a->config['system'], 'db_callstack')) {
383 $sql = "/*".System::callstack()." */ ".$sql;
388 self::$affected_rows = 0;
390 // We have to make some things different if this function is called from "e"
391 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
393 if (isset($trace[1])) {
394 $called_from = $trace[1];
396 // We use just something that is defined to avoid warnings
397 $called_from = $trace[0];
399 // We are having an own error logging in the function "e"
400 $called_from_e = ($called_from['function'] == 'e');
402 switch (self::$driver) {
404 // If there are no arguments we use "query"
405 if (count($args) == 0) {
406 if (!$retval = self::$db->query($sql)) {
407 $errorInfo = self::$db->errorInfo();
408 self::$error = $errorInfo[2];
409 self::$errorno = $errorInfo[1];
413 self::$affected_rows = $retval->rowCount();
417 if (!$stmt = self::$db->prepare($sql)) {
418 $errorInfo = self::$db->errorInfo();
419 self::$error = $errorInfo[2];
420 self::$errorno = $errorInfo[1];
425 foreach ($args AS $param => $value) {
426 $stmt->bindParam($param, $args[$param]);
429 if (!$stmt->execute()) {
430 $errorInfo = $stmt->errorInfo();
431 self::$error = $errorInfo[2];
432 self::$errorno = $errorInfo[1];
436 self::$affected_rows = $retval->rowCount();
440 // There are SQL statements that cannot be executed with a prepared statement
441 $parts = explode(' ', $orig_sql);
442 $command = strtolower($parts[0]);
443 $can_be_prepared = in_array($command, ['select', 'update', 'insert', 'delete']);
445 // The fallback routine is called as well when there are no arguments
446 if (!$can_be_prepared || (count($args) == 0)) {
447 $retval = self::$db->query(self::replaceParameters($sql, $args));
448 if (self::$db->errno) {
449 self::$error = self::$db->error;
450 self::$errorno = self::$db->errno;
453 if (isset($retval->num_rows)) {
454 self::$affected_rows = $retval->num_rows;
456 self::$affected_rows = self::$db->affected_rows;
462 $stmt = self::$db->stmt_init();
464 if (!$stmt->prepare($sql)) {
465 self::$error = $stmt->error;
466 self::$errorno = $stmt->errno;
473 foreach ($args AS $param => $value) {
474 if (is_int($args[$param])) {
476 } elseif (is_float($args[$param])) {
478 } elseif (is_string($args[$param])) {
483 $values[] = &$args[$param];
486 if (count($values) > 0) {
487 array_unshift($values, $param_types);
488 call_user_func_array([$stmt, 'bind_param'], $values);
491 if (!$stmt->execute()) {
492 self::$error = self::$db->error;
493 self::$errorno = self::$db->errno;
496 $stmt->store_result();
498 self::$affected_rows = $retval->affected_rows;
503 // We are having an own error logging in the function "e"
504 if ((self::$errorno != 0) && !$called_from_e) {
505 // We have to preserve the error code, somewhere in the logging it get lost
506 $error = self::$error;
507 $errorno = self::$errorno;
509 logger('DB Error '.self::$errorno.': '.self::$error."\n".
510 System::callstack(8)."\n".self::replaceParameters($sql, $args));
512 // On a lost connection we try to reconnect - but only once.
513 if ($errorno == 2006) {
514 if (self::$in_retrial || !self::reconnect()) {
515 // It doesn't make sense to continue when the database connection was lost
516 if (self::$in_retrial) {
517 logger('Giving up retrial because of database error '.$errorno.': '.$error);
519 logger("Couldn't reconnect after database error ".$errorno.': '.$error);
524 logger('Reconnected after database error '.$errorno.': '.$error);
525 self::$in_retrial = true;
526 return self::p($sql, $args);
530 self::$error = $error;
531 self::$errorno = $errorno;
534 $a->save_timestamp($stamp1, 'database');
536 if (x($a->config,'system') && x($a->config['system'], 'db_log')) {
538 $stamp2 = microtime(true);
539 $duration = (float)($stamp2 - $stamp1);
541 if (($duration > $a->config["system"]["db_loglimit"])) {
542 $duration = round($duration, 3);
543 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
545 @file_put_contents($a->config["system"]["db_log"], DateTimeFormat::utcNow()."\t".$duration."\t".
546 basename($backtrace[1]["file"])."\t".
547 $backtrace[1]["line"]."\t".$backtrace[2]["function"]."\t".
548 substr(self::replaceParameters($sql, $args), 0, 2000)."\n", FILE_APPEND);
555 * @brief Executes a prepared statement like UPDATE or INSERT that doesn't return data
557 * Please use dba::delete, dba::insert, dba::update, ... instead
559 * @param string $sql SQL statement
560 * @return boolean Was the query successfull? False is returned only if an error occurred
562 public static function e($sql) {
565 $stamp = microtime(true);
567 $params = self::getParam(func_get_args());
569 // In a case of a deadlock we are repeating the query 20 times
573 $stmt = self::p($sql, $params);
575 if (is_bool($stmt)) {
577 } elseif (is_object($stmt)) {
585 } while ((self::$errorno == 1213) && (--$timeout > 0));
587 if (self::$errorno != 0) {
588 // We have to preserve the error code, somewhere in the logging it get lost
589 $error = self::$error;
590 $errorno = self::$errorno;
592 logger('DB Error '.self::$errorno.': '.self::$error."\n".
593 System::callstack(8)."\n".self::replaceParameters($sql, $params));
595 self::$error = $error;
596 self::$errorno = $errorno;
599 $a->save_timestamp($stamp, "database_write");
605 * @brief Check if data exists
607 * @param string $table Table name
608 * @param array $condition array of fields for condition
610 * @return boolean Are there rows for that condition?
612 public static function exists($table, $condition) {
620 $first_key = key($condition);
621 if (!is_int($first_key)) {
622 $fields = [$first_key];
625 $stmt = self::select($table, $fields, $condition, ['limit' => 1]);
627 if (is_bool($stmt)) {
630 $retval = (self::num_rows($stmt) > 0);
639 * Fetches the first row
641 * Please use dba::selectFirst or dba::exists whenever this is possible.
643 * @brief Fetches the first row
644 * @param string $sql SQL statement
645 * @return array first row of query
647 public static function fetch_first($sql) {
648 $params = self::getParam(func_get_args());
650 $stmt = self::p($sql, $params);
652 if (is_bool($stmt)) {
655 $retval = self::fetch($stmt);
664 * @brief Returns the number of affected rows of the last statement
666 * @return int Number of rows
668 public static function affected_rows() {
669 return self::$affected_rows;
673 * @brief Returns the number of columns of a statement
675 * @param object Statement object
676 * @return int Number of columns
678 public static function columnCount($stmt) {
679 if (!is_object($stmt)) {
682 switch (self::$driver) {
684 return $stmt->columnCount();
686 return $stmt->field_count;
691 * @brief Returns the number of rows of a statement
693 * @param PDOStatement|mysqli_result|mysqli_stmt Statement object
694 * @return int Number of rows
696 public static function num_rows($stmt) {
697 if (!is_object($stmt)) {
700 switch (self::$driver) {
702 return $stmt->rowCount();
704 return $stmt->num_rows;
710 * @brief Fetch a single row
712 * @param mixed $stmt statement object
713 * @return array current row
715 public static function fetch($stmt) {
718 $stamp1 = microtime(true);
722 if (!is_object($stmt)) {
726 switch (self::$driver) {
728 $columns = $stmt->fetch(PDO::FETCH_ASSOC);
731 if (get_class($stmt) == 'mysqli_result') {
732 $columns = $stmt->fetch_assoc();
736 // This code works, but is slow
738 // Bind the result to a result array
742 for ($x = 0; $x < $stmt->field_count; $x++) {
743 $cols[] = &$cols_num[$x];
746 call_user_func_array([$stmt, 'bind_result'], $cols);
748 if (!$stmt->fetch()) {
753 // We need to get the field names for the array keys
754 // It seems that there is no better way to do this.
755 $result = $stmt->result_metadata();
756 $fields = $result->fetch_fields();
758 foreach ($cols_num AS $param => $col) {
759 $columns[$fields[$param]->name] = $col;
763 $a->save_timestamp($stamp1, 'database');
769 * @brief Insert a row into a table
771 * @param string $table Table name
772 * @param array $param parameter array
773 * @param bool $on_duplicate_update Do an update on a duplicate entry
775 * @return boolean was the insert successfull?
777 public static function insert($table, $param, $on_duplicate_update = false) {
779 if (empty($table) || empty($param)) {
780 logger('Table and fields have to be set');
784 $sql = "INSERT INTO `".self::escape($table)."` (`".implode("`, `", array_keys($param))."`) VALUES (".
785 substr(str_repeat("?, ", count($param)), 0, -2).")";
787 if ($on_duplicate_update) {
788 $sql .= " ON DUPLICATE KEY UPDATE `".implode("` = ?, `", array_keys($param))."` = ?";
790 $values = array_values($param);
791 $param = array_merge_recursive($values, $values);
794 return self::e($sql, $param);
798 * @brief Fetch the id of the last insert command
800 * @return integer Last inserted id
802 public static function lastInsertId() {
803 switch (self::$driver) {
805 $id = self::$db->lastInsertId();
808 $id = self::$db->insert_id;
815 * @brief Locks a table for exclusive write access
817 * This function can be extended in the future to accept a table array as well.
819 * @param string $table Table name
821 * @return boolean was the lock successful?
823 public static function lock($table) {
824 // See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
825 self::e("SET autocommit=0");
826 $success = self::e("LOCK TABLES `".self::escape($table)."` WRITE");
828 self::e("SET autocommit=1");
830 self::$in_transaction = true;
836 * @brief Unlocks all locked tables
838 * @return boolean was the unlock successful?
840 public static function unlock() {
841 // See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
843 $success = self::e("UNLOCK TABLES");
844 self::e("SET autocommit=1");
845 self::$in_transaction = false;
850 * @brief Starts a transaction
852 * @return boolean Was the command executed successfully?
854 public static function transaction() {
855 if (!self::e('COMMIT')) {
858 if (!self::e('START TRANSACTION')) {
861 self::$in_transaction = true;
866 * @brief Does a commit
868 * @return boolean Was the command executed successfully?
870 public static function commit() {
871 if (!self::e('COMMIT')) {
874 self::$in_transaction = false;
879 * @brief Does a rollback
881 * @return boolean Was the command executed successfully?
883 public static function rollback() {
884 if (!self::e('ROLLBACK')) {
887 self::$in_transaction = false;
892 * @brief Build the array with the table relations
894 * The array is build from the database definitions in DBStructure.php
896 * This process must only be started once, since the value is cached.
898 private static function buildRelationData() {
899 $definition = DBStructure::definition();
901 foreach ($definition AS $table => $structure) {
902 foreach ($structure['fields'] AS $field => $field_struct) {
903 if (isset($field_struct['relation'])) {
904 foreach ($field_struct['relation'] AS $rel_table => $rel_field) {
905 self::$relation[$rel_table][$rel_field][$table][] = $field;
913 * @brief Delete a row from a table
915 * @param string $table Table name
916 * @param array $conditions Field condition(s)
917 * @param array $options
918 * - cascade: If true we delete records in other tables that depend on the one we're deleting through
919 * relations (default: true)
920 * @param boolean $in_process Internal use: Only do a commit after the last delete
921 * @param array $callstack Internal use: prevent endless loops
923 * @return boolean|array was the delete successful? When $in_process is set: deletion data
925 public static function delete($table, array $conditions, array $options = [], $in_process = false, array &$callstack = [])
927 if (empty($table) || empty($conditions)) {
928 logger('Table and conditions have to be set');
934 // Create a key for the loop prevention
935 $key = $table . ':' . implode(':', array_keys($conditions)) . ':' . implode(':', $conditions);
937 // We quit when this key already exists in the callstack.
938 if (isset($callstack[$key])) {
942 $callstack[$key] = true;
944 $table = self::escape($table);
946 $commands[$key] = ['table' => $table, 'conditions' => $conditions];
948 $cascade = defaults($options, 'cascade', true);
950 // To speed up the whole process we cache the table relations
951 if ($cascade && count(self::$relation) == 0) {
952 self::buildRelationData();
955 // Is there a relation entry for the table?
956 if ($cascade && isset(self::$relation[$table])) {
957 // We only allow a simple "one field" relation.
958 $field = array_keys(self::$relation[$table])[0];
959 $rel_def = array_values(self::$relation[$table])[0];
961 // Create a key for preventing double queries
962 $qkey = $field . '-' . $table . ':' . implode(':', array_keys($conditions)) . ':' . implode(':', $conditions);
964 // When the search field is the relation field, we don't need to fetch the rows
965 // This is useful when the leading record is already deleted in the frontend but the rest is done in the backend
966 if ((count($conditions) == 1) && ($field == array_keys($conditions)[0])) {
967 foreach ($rel_def AS $rel_table => $rel_fields) {
968 foreach ($rel_fields AS $rel_field) {
969 $retval = self::delete($rel_table, [$rel_field => array_values($conditions)[0]], $options, true, $callstack);
970 $commands = array_merge($commands, $retval);
973 // We quit when this key already exists in the callstack.
974 } elseif (!isset($callstack[$qkey])) {
976 $callstack[$qkey] = true;
978 // Fetch all rows that are to be deleted
979 $data = self::select($table, [$field], $conditions);
981 while ($row = self::fetch($data)) {
982 // Now we accumulate the delete commands
983 $retval = self::delete($table, [$field => $row[$field]], $options, true, $callstack);
984 $commands = array_merge($commands, $retval);
989 // Since we had split the delete command we don't need the original command anymore
990 unset($commands[$key]);
995 // Now we finalize the process
996 $do_transaction = !self::$in_transaction;
998 if ($do_transaction) {
1005 foreach ($commands AS $command) {
1006 $conditions = $command['conditions'];
1008 $first_key = key($conditions);
1010 $condition_string = self::buildCondition($conditions);
1012 if ((count($command['conditions']) > 1) || is_int($first_key)) {
1013 $sql = "DELETE FROM `" . $command['table'] . "`" . $condition_string;
1014 logger(self::replaceParameters($sql, $conditions), LOGGER_DATA);
1016 if (!self::e($sql, $conditions)) {
1017 if ($do_transaction) {
1023 $key_table = $command['table'];
1024 $key_condition = array_keys($command['conditions'])[0];
1025 $value = array_values($command['conditions'])[0];
1027 // Split the SQL queries in chunks of 100 values
1028 // We do the $i stuff here to make the code better readable
1029 $i = $counter[$key_table][$key_condition];
1030 if (isset($compacted[$key_table][$key_condition][$i]) && count($compacted[$key_table][$key_condition][$i]) > 100) {
1034 $compacted[$key_table][$key_condition][$i][$value] = $value;
1035 $counter[$key_table][$key_condition] = $i;
1038 foreach ($compacted AS $table => $values) {
1039 foreach ($values AS $field => $field_value_list) {
1040 foreach ($field_value_list AS $field_values) {
1041 $sql = "DELETE FROM `" . $table . "` WHERE `" . $field . "` IN (" .
1042 substr(str_repeat("?, ", count($field_values)), 0, -2) . ");";
1044 logger(self::replaceParameters($sql, $field_values), LOGGER_DATA);
1046 if (!self::e($sql, $field_values)) {
1047 if ($do_transaction) {
1055 if ($do_transaction) {
1065 * @brief Updates rows
1067 * Updates rows in the database. When $old_fields is set to an array,
1068 * the system will only do an update if the fields in that array changed.
1071 * Only the values in $old_fields are compared.
1072 * This is an intentional behaviour.
1075 * We include the timestamp field in $fields but not in $old_fields.
1076 * Then the row will only get the new timestamp when the other fields had changed.
1078 * When $old_fields is set to a boolean value the system will do this compare itself.
1079 * When $old_fields is set to "true" the system will do an insert if the row doesn't exists.
1082 * Only set $old_fields to a boolean value when you are sure that you will update a single row.
1083 * When you set $old_fields to "true" then $fields must contain all relevant fields!
1085 * @param string $table Table name
1086 * @param array $fields contains the fields that are updated
1087 * @param array $condition condition array with the key values
1088 * @param array|boolean $old_fields array with the old field values that are about to be replaced (true = update on duplicate)
1090 * @return boolean was the update successfull?
1092 public static function update($table, $fields, $condition, $old_fields = []) {
1094 if (empty($table) || empty($fields) || empty($condition)) {
1095 logger('Table, fields and condition have to be set');
1099 $table = self::escape($table);
1101 $condition_string = self::buildCondition($condition);
1103 if (is_bool($old_fields)) {
1104 $do_insert = $old_fields;
1106 $old_fields = self::selectFirst($table, [], $condition);
1108 if (is_bool($old_fields)) {
1110 $values = array_merge($condition, $fields);
1111 return self::insert($table, $values, $do_insert);
1117 $do_update = (count($old_fields) == 0);
1119 foreach ($old_fields AS $fieldname => $content) {
1120 if (isset($fields[$fieldname])) {
1121 if ($fields[$fieldname] == $content) {
1122 unset($fields[$fieldname]);
1129 if (!$do_update || (count($fields) == 0)) {
1133 $sql = "UPDATE `".$table."` SET `".
1134 implode("` = ?, `", array_keys($fields))."` = ?".$condition_string;
1136 $params1 = array_values($fields);
1137 $params2 = array_values($condition);
1138 $params = array_merge_recursive($params1, $params2);
1140 return self::e($sql, $params);
1144 * Retrieve a single record from a table and returns it in an associative array
1146 * @brief Retrieve a single record from a table
1147 * @param string $table
1148 * @param array $fields
1149 * @param array $condition
1150 * @param array $params
1151 * @return bool|array
1154 public static function selectFirst($table, array $fields = [], array $condition = [], $params = [])
1156 $params['limit'] = 1;
1157 $result = self::select($table, $fields, $condition, $params);
1159 if (is_bool($result)) {
1162 $row = self::fetch($result);
1163 self::close($result);
1169 * @brief Select rows from a table
1171 * @param string $table Table name
1172 * @param array $fields Array of selected fields, empty for all
1173 * @param array $condition Array of fields for condition
1174 * @param array $params Array of several parameters
1176 * @return boolean|object
1180 * $fields = array("id", "uri", "uid", "network");
1182 * $condition = array("uid" => 1, "network" => 'dspr');
1184 * $condition = array("`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr');
1186 * $params = array("order" => array("id", "received" => true), "limit" => 10);
1188 * $data = dba::select($table, $fields, $condition, $params);
1190 public static function select($table, array $fields = [], array $condition = [], array $params = [])
1196 $table = self::escape($table);
1198 if (count($fields) > 0) {
1199 $select_fields = "`" . implode("`, `", array_values($fields)) . "`";
1201 $select_fields = "*";
1204 $condition_string = self::buildCondition($condition);
1206 $param_string = self::buildParameter($params);
1208 $sql = "SELECT " . $select_fields . " FROM `" . $table . "`" . $condition_string . $param_string;
1210 $result = self::p($sql, $condition);
1216 * @brief Counts the rows from a table satisfying the provided condition
1218 * @param string $table Table name
1219 * @param array $condition array of fields for condition
1226 * $condition = ["uid" => 1, "network" => 'dspr'];
1228 * $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr'];
1230 * $count = dba::count($table, $condition);
1232 public static function count($table, array $condition = [])
1238 $condition_string = self::buildCondition($condition);
1240 $sql = "SELECT COUNT(*) AS `count` FROM `".$table."`".$condition_string;
1242 $row = self::fetch_first($sql, $condition);
1244 return $row['count'];
1248 * @brief Returns the SQL condition string built from the provided condition array
1250 * This function operates with two modes.
1251 * - Supplied with a filed/value associative array, it builds simple strict
1252 * equality conditions linked by AND.
1253 * - Supplied with a flat list, the first element is the condition string and
1254 * the following arguments are the values to be interpolated
1256 * $condition = ["uid" => 1, "network" => 'dspr'];
1258 * $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr'];
1260 * In either case, the provided array is left with the parameters only
1262 * @param array $condition
1265 public static function buildCondition(array &$condition = [])
1267 $condition_string = '';
1268 if (count($condition) > 0) {
1270 $first_key = key($condition);
1271 if (is_int($first_key)) {
1272 $condition_string = " WHERE (" . array_shift($condition) . ")";
1275 $condition_string = "";
1276 foreach ($condition as $field => $value) {
1277 if ($condition_string != "") {
1278 $condition_string .= " AND ";
1280 if (is_array($value)) {
1281 $new_values = array_merge($new_values, array_values($value));
1282 $placeholders = substr(str_repeat("?, ", count($value)), 0, -2);
1283 $condition_string .= "`" . $field . "` IN (" . $placeholders . ")";
1285 $new_values[$field] = $value;
1286 $condition_string .= "`" . $field . "` = ?";
1289 $condition_string = " WHERE (" . $condition_string . ")";
1290 $condition = $new_values;
1294 return $condition_string;
1298 * @brief Returns the SQL parameter string built from the provided parameter array
1300 * @param array $params
1303 public static function buildParameter(array $params = [])
1306 if (isset($params['order'])) {
1307 $order_string = " ORDER BY ";
1308 foreach ($params['order'] AS $fields => $order) {
1309 if (!is_int($fields)) {
1310 $order_string .= "`" . $fields . "` " . ($order ? "DESC" : "ASC") . ", ";
1312 $order_string .= "`" . $order . "`, ";
1315 $order_string = substr($order_string, 0, -2);
1319 if (isset($params['limit']) && is_int($params['limit'])) {
1320 $limit_string = " LIMIT " . $params['limit'];
1323 if (isset($params['limit']) && is_array($params['limit'])) {
1324 $limit_string = " LIMIT " . intval($params['limit'][0]) . ", " . intval($params['limit'][1]);
1327 return $order_string.$limit_string;
1331 * @brief Fills an array with data from a query
1333 * @param object $stmt statement object
1334 * @return array Data array
1336 public static function inArray($stmt, $do_close = true) {
1337 if (is_bool($stmt)) {
1342 while ($row = self::fetch($stmt)) {
1352 * @brief Returns the error number of the last query
1354 * @return string Error number (0 if no error)
1356 public static function errorNo() {
1357 return self::$errorno;
1361 * @brief Returns the error message of the last query
1363 * @return string Error message ('' if no error)
1365 public static function errorMessage() {
1366 return self::$error;
1370 * @brief Closes the current statement
1372 * @param object $stmt statement object
1373 * @return boolean was the close successful?
1375 public static function close($stmt) {
1378 $stamp1 = microtime(true);
1380 if (!is_object($stmt)) {
1384 switch (self::$driver) {
1386 $ret = $stmt->closeCursor();
1389 $stmt->free_result();
1390 $ret = $stmt->close();
1394 $a->save_timestamp($stamp1, 'database');
1400 function dbesc($str) {
1401 if (dba::$connected) {
1402 return(dba::escape($str));
1404 return(str_replace("'","\\'",$str));
1409 * @brief execute SQL query with printf style args - deprecated
1411 * Please use the dba:: functions instead:
1412 * dba::select, dba::exists, dba::insert
1413 * dba::delete, dba::update, dba::p, dba::e
1415 * @param $args Query parameters (1 to N parameters of different types)
1416 * @return array|bool Query array
1419 $args = func_get_args();
1422 if (!dba::$connected) {
1426 $sql = dba::clean_query($sql);
1427 $sql = dba::any_value_fallback($sql);
1429 $stmt = @vsprintf($sql, $args);
1431 $ret = dba::p($stmt);
1433 if (is_bool($ret)) {
1437 $columns = dba::columnCount($ret);
1439 $data = dba::inArray($ret);
1441 if ((count($data) == 0) && ($columns == 0)) {
1448 function dba_timer() {
1449 return microtime(true);