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 = [];
28 private static $db_serveraddr = '';
29 private static $db_user = '';
30 private static $db_pass = '';
31 private static $db_name = '';
33 public static function connect($serveraddr, $user, $pass, $db) {
34 if (!is_null(self::$db) && self::connected()) {
40 $stamp1 = microtime(true);
42 // We are storing these values for being able to perform a reconnect
43 self::$db_serveraddr = $serveraddr;
44 self::$db_user = $user;
45 self::$db_pass = $pass;
48 $serveraddr = trim($serveraddr);
50 $serverdata = explode(':', $serveraddr);
51 $server = $serverdata[0];
53 if (count($serverdata) > 1) {
54 $port = trim($serverdata[1]);
57 $server = trim($server);
62 if (!(strlen($server) && strlen($user))) {
66 if (class_exists('\PDO') && in_array('mysql', PDO::getAvailableDrivers())) {
67 self::$driver = 'pdo';
68 $connect = "mysql:host=".$server.";dbname=".$db;
71 $connect .= ";port=".$port;
74 if (isset($a->config["system"]["db_charset"])) {
75 $connect .= ";charset=".$a->config["system"]["db_charset"];
78 self::$db = @new PDO($connect, $user, $pass);
80 //self::$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
81 //self::$db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
82 self::$connected = true;
83 } catch (PDOException $e) {
87 if (!self::$connected && class_exists('mysqli')) {
88 self::$driver = 'mysqli';
89 self::$db = @new mysqli($server, $user, $pass, $db, $port);
90 if (!mysqli_connect_errno()) {
91 self::$connected = true;
93 if (isset($a->config["system"]["db_charset"])) {
94 self::$db->set_charset($a->config["system"]["db_charset"]);
99 // No suitable SQL driver was found.
100 if (!self::$connected) {
101 self::$driver = null;
104 $a->save_timestamp($stamp1, "network");
106 return self::$connected;
110 * Disconnects the current database connection
112 public static function disconnect()
114 if (is_null(self::$db)) {
118 switch (self::$driver) {
130 * Perform a reconnect of an existing database connection
132 public static function reconnect() {
135 $ret = self::connect(self::$db_serveraddr, self::$db_user, self::$db_pass, self::$db_name);
140 * Return the database object.
143 public static function get_db()
149 * @brief Returns the MySQL server version string
151 * This function discriminate between the deprecated mysql API and the current
152 * object-oriented mysqli API. Example of returned string: 5.5.46-0+deb8u1
156 public static function server_info() {
157 if (self::$_server_info == '') {
158 switch (self::$driver) {
160 self::$_server_info = self::$db->getAttribute(PDO::ATTR_SERVER_VERSION);
163 self::$_server_info = self::$db->server_info;
167 return self::$_server_info;
171 * @brief Returns the selected database name
175 public static function database_name() {
176 $ret = self::p("SELECT DATABASE() AS `db`");
177 $data = self::inArray($ret);
178 return $data[0]['db'];
182 * @brief Analyze a database query and log this if some conditions are met.
184 * @param string $query The database query that will be analyzed
186 private static function logIndex($query) {
189 if (empty($a->config["system"]["db_log_index"])) {
193 // Don't explain an explain statement
194 if (strtolower(substr($query, 0, 7)) == "explain") {
198 // Only do the explain on "select", "update" and "delete"
199 if (!in_array(strtolower(substr($query, 0, 6)), ["select", "update", "delete"])) {
203 $r = self::p("EXPLAIN ".$query);
204 if (!DBM::is_result($r)) {
208 $watchlist = explode(',', $a->config["system"]["db_log_index_watch"]);
209 $blacklist = explode(',', $a->config["system"]["db_log_index_blacklist"]);
211 while ($row = dba::fetch($r)) {
212 if ((intval($a->config["system"]["db_loglimit_index"]) > 0)) {
213 $log = (in_array($row['key'], $watchlist) &&
214 ($row['rows'] >= intval($a->config["system"]["db_loglimit_index"])));
219 if ((intval($a->config["system"]["db_loglimit_index_high"]) > 0) && ($row['rows'] >= intval($a->config["system"]["db_loglimit_index_high"]))) {
223 if (in_array($row['key'], $blacklist) || ($row['key'] == "")) {
228 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
229 @file_put_contents($a->config["system"]["db_log_index"], DateTimeFormat::utcNow()."\t".
230 $row['key']."\t".$row['rows']."\t".$row['Extra']."\t".
231 basename($backtrace[1]["file"])."\t".
232 $backtrace[1]["line"]."\t".$backtrace[2]["function"]."\t".
233 substr($query, 0, 2000)."\n", FILE_APPEND);
238 public static function escape($str) {
239 switch (self::$driver) {
241 return substr(@self::$db->quote($str, PDO::PARAM_STR), 1, -1);
243 return @self::$db->real_escape_string($str);
247 public static function connected() {
250 switch (self::$driver) {
252 $r = dba::p("SELECT 1");
253 if (DBM::is_result($r)) {
254 $row = dba::inArray($r);
255 $connected = ($row[0]['1'] == '1');
259 $connected = self::$db->ping();
266 * @brief Replaces ANY_VALUE() function by MIN() function,
267 * if the database server does not support ANY_VALUE().
269 * Considerations for Standard SQL, or MySQL with ONLY_FULL_GROUP_BY (default since 5.7.5).
270 * ANY_VALUE() is available from MySQL 5.7.5 https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html
271 * A standard fall-back is to use MIN().
273 * @param string $sql An SQL string without the values
274 * @return string The input SQL string modified if necessary.
276 public static function any_value_fallback($sql) {
277 $server_info = self::server_info();
278 if (version_compare($server_info, '5.7.5', '<') ||
279 (stripos($server_info, 'MariaDB') !== false)) {
280 $sql = str_ireplace('ANY_VALUE(', 'MIN(', $sql);
286 * @brief beautifies the query - useful for "SHOW PROCESSLIST"
288 * This is safe when we bind the parameters later.
289 * The parameter values aren't part of the SQL.
291 * @param string $sql An SQL string without the values
292 * @return string The input SQL string modified if necessary.
294 public static function clean_query($sql) {
295 $search = ["\t", "\n", "\r", " "];
296 $replace = [' ', ' ', ' ', ' '];
299 $sql = str_replace($search, $replace, $sql);
300 } while ($oldsql != $sql);
307 * @brief Replaces the ? placeholders with the parameters in the $args array
309 * @param string $sql SQL query
310 * @param array $args The parameters that are to replace the ? placeholders
311 * @return string The replaced SQL query
313 private static function replaceParameters($sql, $args) {
315 foreach ($args AS $param => $value) {
316 if (is_int($args[$param]) || is_float($args[$param])) {
317 $replace = intval($args[$param]);
319 $replace = "'".self::escape($args[$param])."'";
322 $pos = strpos($sql, '?', $offset);
323 if ($pos !== false) {
324 $sql = substr_replace($sql, $replace, $pos, 1);
326 $offset = $pos + strlen($replace);
332 * @brief Convert parameter array to an universal form
333 * @param array $args Parameter array
334 * @return array universalized parameter array
336 private static function getParam($args) {
339 // When the second function parameter is an array then use this as the parameter array
340 if ((count($args) > 0) && (is_array($args[1]))) {
348 * @brief Executes a prepared statement that returns data
349 * @usage Example: $r = p("SELECT * FROM `item` WHERE `guid` = ?", $guid);
351 * Please only use it with complicated queries.
352 * For all regular queries please use dba::select or dba::exists
354 * @param string $sql SQL statement
355 * @return bool|object statement object or result object
357 public static function p($sql) {
360 $stamp1 = microtime(true);
362 $params = self::getParam(func_get_args());
364 // Renumber the array keys to be sure that they fit
367 foreach ($params AS $param) {
368 // Avoid problems with some MySQL servers and boolean values. See issue #3645
369 if (is_bool($param)) {
370 $param = (int)$param;
372 $args[++$i] = $param;
375 if (!self::$connected) {
379 if ((substr_count($sql, '?') != count($args)) && (count($args) > 0)) {
380 // Question: Should we continue or stop the query here?
381 logger('Parameter mismatch. Query "'.$sql.'" - Parameters '.print_r($args, true), LOGGER_DEBUG);
384 $sql = self::clean_query($sql);
385 $sql = self::any_value_fallback($sql);
389 if (x($a->config,'system') && x($a->config['system'], 'db_callstack')) {
390 $sql = "/*".System::callstack()." */ ".$sql;
395 self::$affected_rows = 0;
397 // We have to make some things different if this function is called from "e"
398 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
400 if (isset($trace[1])) {
401 $called_from = $trace[1];
403 // We use just something that is defined to avoid warnings
404 $called_from = $trace[0];
406 // We are having an own error logging in the function "e"
407 $called_from_e = ($called_from['function'] == 'e');
409 switch (self::$driver) {
411 // If there are no arguments we use "query"
412 if (count($args) == 0) {
413 if (!$retval = self::$db->query($sql)) {
414 $errorInfo = self::$db->errorInfo();
415 self::$error = $errorInfo[2];
416 self::$errorno = $errorInfo[1];
420 self::$affected_rows = $retval->rowCount();
424 if (!$stmt = self::$db->prepare($sql)) {
425 $errorInfo = self::$db->errorInfo();
426 self::$error = $errorInfo[2];
427 self::$errorno = $errorInfo[1];
432 foreach ($args AS $param => $value) {
433 if (is_int($args[$param])) {
434 $data_type = PDO::PARAM_INT;
436 $data_type = PDO::PARAM_STR;
438 $stmt->bindParam($param, $args[$param], $data_type);
441 if (!$stmt->execute()) {
442 $errorInfo = $stmt->errorInfo();
443 self::$error = $errorInfo[2];
444 self::$errorno = $errorInfo[1];
448 self::$affected_rows = $retval->rowCount();
452 // There are SQL statements that cannot be executed with a prepared statement
453 $parts = explode(' ', $orig_sql);
454 $command = strtolower($parts[0]);
455 $can_be_prepared = in_array($command, ['select', 'update', 'insert', 'delete']);
457 // The fallback routine is called as well when there are no arguments
458 if (!$can_be_prepared || (count($args) == 0)) {
459 $retval = self::$db->query(self::replaceParameters($sql, $args));
460 if (self::$db->errno) {
461 self::$error = self::$db->error;
462 self::$errorno = self::$db->errno;
465 if (isset($retval->num_rows)) {
466 self::$affected_rows = $retval->num_rows;
468 self::$affected_rows = self::$db->affected_rows;
474 $stmt = self::$db->stmt_init();
476 if (!$stmt->prepare($sql)) {
477 self::$error = $stmt->error;
478 self::$errorno = $stmt->errno;
485 foreach ($args AS $param => $value) {
486 if (is_int($args[$param])) {
488 } elseif (is_float($args[$param])) {
490 } elseif (is_string($args[$param])) {
495 $values[] = &$args[$param];
498 if (count($values) > 0) {
499 array_unshift($values, $param_types);
500 call_user_func_array([$stmt, 'bind_param'], $values);
503 if (!$stmt->execute()) {
504 self::$error = self::$db->error;
505 self::$errorno = self::$db->errno;
508 $stmt->store_result();
510 self::$affected_rows = $retval->affected_rows;
515 // We are having an own error logging in the function "e"
516 if ((self::$errorno != 0) && !$called_from_e) {
517 // We have to preserve the error code, somewhere in the logging it get lost
518 $error = self::$error;
519 $errorno = self::$errorno;
521 logger('DB Error '.self::$errorno.': '.self::$error."\n".
522 System::callstack(8)."\n".self::replaceParameters($sql, $args));
524 // On a lost connection we try to reconnect - but only once.
525 if ($errorno == 2006) {
526 if (self::$in_retrial || !self::reconnect()) {
527 // It doesn't make sense to continue when the database connection was lost
528 if (self::$in_retrial) {
529 logger('Giving up retrial because of database error '.$errorno.': '.$error);
531 logger("Couldn't reconnect after database error ".$errorno.': '.$error);
536 logger('Reconnected after database error '.$errorno.': '.$error);
537 self::$in_retrial = true;
538 $ret = self::p($sql, $args);
539 self::$in_retrial = false;
544 self::$error = $error;
545 self::$errorno = $errorno;
548 $a->save_timestamp($stamp1, 'database');
550 if (x($a->config,'system') && x($a->config['system'], 'db_log')) {
552 $stamp2 = microtime(true);
553 $duration = (float)($stamp2 - $stamp1);
555 if (($duration > $a->config["system"]["db_loglimit"])) {
556 $duration = round($duration, 3);
557 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
559 @file_put_contents($a->config["system"]["db_log"], DateTimeFormat::utcNow()."\t".$duration."\t".
560 basename($backtrace[1]["file"])."\t".
561 $backtrace[1]["line"]."\t".$backtrace[2]["function"]."\t".
562 substr(self::replaceParameters($sql, $args), 0, 2000)."\n", FILE_APPEND);
569 * @brief Executes a prepared statement like UPDATE or INSERT that doesn't return data
571 * Please use dba::delete, dba::insert, dba::update, ... instead
573 * @param string $sql SQL statement
574 * @return boolean Was the query successfull? False is returned only if an error occurred
576 public static function e($sql) {
579 $stamp = microtime(true);
581 $params = self::getParam(func_get_args());
583 // In a case of a deadlock we are repeating the query 20 times
587 $stmt = self::p($sql, $params);
589 if (is_bool($stmt)) {
591 } elseif (is_object($stmt)) {
599 } while ((self::$errorno == 1213) && (--$timeout > 0));
601 if (self::$errorno != 0) {
602 // We have to preserve the error code, somewhere in the logging it get lost
603 $error = self::$error;
604 $errorno = self::$errorno;
606 logger('DB Error '.self::$errorno.': '.self::$error."\n".
607 System::callstack(8)."\n".self::replaceParameters($sql, $params));
609 // On a lost connection we simply quit.
610 // A reconnect like in self::p could be dangerous with modifications
611 if ($errorno == 2006) {
612 logger('Giving up because of database error '.$errorno.': '.$error);
616 self::$error = $error;
617 self::$errorno = $errorno;
620 $a->save_timestamp($stamp, "database_write");
626 * @brief Check if data exists
628 * @param string $table Table name
629 * @param array $condition array of fields for condition
631 * @return boolean Are there rows for that condition?
633 public static function exists($table, $condition) {
641 $first_key = key($condition);
642 if (!is_int($first_key)) {
643 $fields = [$first_key];
646 $stmt = self::select($table, $fields, $condition, ['limit' => 1]);
648 if (is_bool($stmt)) {
651 $retval = (self::num_rows($stmt) > 0);
660 * Fetches the first row
662 * Please use dba::selectFirst or dba::exists whenever this is possible.
664 * @brief Fetches the first row
665 * @param string $sql SQL statement
666 * @return array first row of query
668 public static function fetch_first($sql) {
669 $params = self::getParam(func_get_args());
671 $stmt = self::p($sql, $params);
673 if (is_bool($stmt)) {
676 $retval = self::fetch($stmt);
685 * @brief Returns the number of affected rows of the last statement
687 * @return int Number of rows
689 public static function affected_rows() {
690 return self::$affected_rows;
694 * @brief Returns the number of columns of a statement
696 * @param object Statement object
697 * @return int Number of columns
699 public static function columnCount($stmt) {
700 if (!is_object($stmt)) {
703 switch (self::$driver) {
705 return $stmt->columnCount();
707 return $stmt->field_count;
712 * @brief Returns the number of rows of a statement
714 * @param PDOStatement|mysqli_result|mysqli_stmt Statement object
715 * @return int Number of rows
717 public static function num_rows($stmt) {
718 if (!is_object($stmt)) {
721 switch (self::$driver) {
723 return $stmt->rowCount();
725 return $stmt->num_rows;
731 * @brief Fetch a single row
733 * @param mixed $stmt statement object
734 * @return array current row
736 public static function fetch($stmt) {
739 $stamp1 = microtime(true);
743 if (!is_object($stmt)) {
747 switch (self::$driver) {
749 $columns = $stmt->fetch(PDO::FETCH_ASSOC);
752 if (get_class($stmt) == 'mysqli_result') {
753 $columns = $stmt->fetch_assoc();
757 // This code works, but is slow
759 // Bind the result to a result array
763 for ($x = 0; $x < $stmt->field_count; $x++) {
764 $cols[] = &$cols_num[$x];
767 call_user_func_array([$stmt, 'bind_result'], $cols);
769 if (!$stmt->fetch()) {
774 // We need to get the field names for the array keys
775 // It seems that there is no better way to do this.
776 $result = $stmt->result_metadata();
777 $fields = $result->fetch_fields();
779 foreach ($cols_num AS $param => $col) {
780 $columns[$fields[$param]->name] = $col;
784 $a->save_timestamp($stamp1, 'database');
790 * @brief Insert a row into a table
792 * @param string $table Table name
793 * @param array $param parameter array
794 * @param bool $on_duplicate_update Do an update on a duplicate entry
796 * @return boolean was the insert successfull?
798 public static function insert($table, $param, $on_duplicate_update = false) {
800 if (empty($table) || empty($param)) {
801 logger('Table and fields have to be set');
805 $sql = "INSERT INTO `".self::escape($table)."` (`".implode("`, `", array_keys($param))."`) VALUES (".
806 substr(str_repeat("?, ", count($param)), 0, -2).")";
808 if ($on_duplicate_update) {
809 $sql .= " ON DUPLICATE KEY UPDATE `".implode("` = ?, `", array_keys($param))."` = ?";
811 $values = array_values($param);
812 $param = array_merge_recursive($values, $values);
815 return self::e($sql, $param);
819 * @brief Fetch the id of the last insert command
821 * @return integer Last inserted id
823 public static function lastInsertId() {
824 switch (self::$driver) {
826 $id = self::$db->lastInsertId();
829 $id = self::$db->insert_id;
836 * @brief Locks a table for exclusive write access
838 * This function can be extended in the future to accept a table array as well.
840 * @param string $table Table name
842 * @return boolean was the lock successful?
844 public static function lock($table) {
845 // See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
846 self::e("SET autocommit=0");
847 $success = self::e("LOCK TABLES `".self::escape($table)."` WRITE");
849 self::e("SET autocommit=1");
851 self::$in_transaction = true;
857 * @brief Unlocks all locked tables
859 * @return boolean was the unlock successful?
861 public static function unlock() {
862 // See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
864 $success = self::e("UNLOCK TABLES");
865 self::e("SET autocommit=1");
866 self::$in_transaction = false;
871 * @brief Starts a transaction
873 * @return boolean Was the command executed successfully?
875 public static function transaction() {
876 if (!self::e('COMMIT')) {
879 if (!self::e('START TRANSACTION')) {
882 self::$in_transaction = true;
887 * @brief Does a commit
889 * @return boolean Was the command executed successfully?
891 public static function commit() {
892 if (!self::e('COMMIT')) {
895 self::$in_transaction = false;
900 * @brief Does a rollback
902 * @return boolean Was the command executed successfully?
904 public static function rollback() {
905 if (!self::e('ROLLBACK')) {
908 self::$in_transaction = false;
913 * @brief Build the array with the table relations
915 * The array is build from the database definitions in DBStructure.php
917 * This process must only be started once, since the value is cached.
919 private static function buildRelationData() {
920 $definition = DBStructure::definition();
922 foreach ($definition AS $table => $structure) {
923 foreach ($structure['fields'] AS $field => $field_struct) {
924 if (isset($field_struct['relation'])) {
925 foreach ($field_struct['relation'] AS $rel_table => $rel_field) {
926 self::$relation[$rel_table][$rel_field][$table][] = $field;
934 * @brief Delete a row from a table
936 * @param string $table Table name
937 * @param array $conditions Field condition(s)
938 * @param array $options
939 * - cascade: If true we delete records in other tables that depend on the one we're deleting through
940 * relations (default: true)
941 * @param boolean $in_process Internal use: Only do a commit after the last delete
942 * @param array $callstack Internal use: prevent endless loops
944 * @return boolean|array was the delete successful? When $in_process is set: deletion data
946 public static function delete($table, array $conditions, array $options = [], $in_process = false, array &$callstack = [])
948 if (empty($table) || empty($conditions)) {
949 logger('Table and conditions have to be set');
955 // Create a key for the loop prevention
956 $key = $table . ':' . implode(':', array_keys($conditions)) . ':' . implode(':', $conditions);
958 // We quit when this key already exists in the callstack.
959 if (isset($callstack[$key])) {
963 $callstack[$key] = true;
965 $table = self::escape($table);
967 $commands[$key] = ['table' => $table, 'conditions' => $conditions];
969 $cascade = defaults($options, 'cascade', true);
971 // To speed up the whole process we cache the table relations
972 if ($cascade && count(self::$relation) == 0) {
973 self::buildRelationData();
976 // Is there a relation entry for the table?
977 if ($cascade && isset(self::$relation[$table])) {
978 // We only allow a simple "one field" relation.
979 $field = array_keys(self::$relation[$table])[0];
980 $rel_def = array_values(self::$relation[$table])[0];
982 // Create a key for preventing double queries
983 $qkey = $field . '-' . $table . ':' . implode(':', array_keys($conditions)) . ':' . implode(':', $conditions);
985 // When the search field is the relation field, we don't need to fetch the rows
986 // This is useful when the leading record is already deleted in the frontend but the rest is done in the backend
987 if ((count($conditions) == 1) && ($field == array_keys($conditions)[0])) {
988 foreach ($rel_def AS $rel_table => $rel_fields) {
989 foreach ($rel_fields AS $rel_field) {
990 $retval = self::delete($rel_table, [$rel_field => array_values($conditions)[0]], $options, true, $callstack);
991 $commands = array_merge($commands, $retval);
994 // We quit when this key already exists in the callstack.
995 } elseif (!isset($callstack[$qkey])) {
997 $callstack[$qkey] = true;
999 // Fetch all rows that are to be deleted
1000 $data = self::select($table, [$field], $conditions);
1002 while ($row = self::fetch($data)) {
1003 // Now we accumulate the delete commands
1004 $retval = self::delete($table, [$field => $row[$field]], $options, true, $callstack);
1005 $commands = array_merge($commands, $retval);
1010 // Since we had split the delete command we don't need the original command anymore
1011 unset($commands[$key]);
1016 // Now we finalize the process
1017 $do_transaction = !self::$in_transaction;
1019 if ($do_transaction) {
1020 self::transaction();
1026 foreach ($commands AS $command) {
1027 $conditions = $command['conditions'];
1029 $first_key = key($conditions);
1031 $condition_string = self::buildCondition($conditions);
1033 if ((count($command['conditions']) > 1) || is_int($first_key)) {
1034 $sql = "DELETE FROM `" . $command['table'] . "`" . $condition_string;
1035 logger(self::replaceParameters($sql, $conditions), LOGGER_DATA);
1037 if (!self::e($sql, $conditions)) {
1038 if ($do_transaction) {
1044 $key_table = $command['table'];
1045 $key_condition = array_keys($command['conditions'])[0];
1046 $value = array_values($command['conditions'])[0];
1048 // Split the SQL queries in chunks of 100 values
1049 // We do the $i stuff here to make the code better readable
1050 $i = $counter[$key_table][$key_condition];
1051 if (isset($compacted[$key_table][$key_condition][$i]) && count($compacted[$key_table][$key_condition][$i]) > 100) {
1055 $compacted[$key_table][$key_condition][$i][$value] = $value;
1056 $counter[$key_table][$key_condition] = $i;
1059 foreach ($compacted AS $table => $values) {
1060 foreach ($values AS $field => $field_value_list) {
1061 foreach ($field_value_list AS $field_values) {
1062 $sql = "DELETE FROM `" . $table . "` WHERE `" . $field . "` IN (" .
1063 substr(str_repeat("?, ", count($field_values)), 0, -2) . ");";
1065 logger(self::replaceParameters($sql, $field_values), LOGGER_DATA);
1067 if (!self::e($sql, $field_values)) {
1068 if ($do_transaction) {
1076 if ($do_transaction) {
1086 * @brief Updates rows
1088 * Updates rows in the database. When $old_fields is set to an array,
1089 * the system will only do an update if the fields in that array changed.
1092 * Only the values in $old_fields are compared.
1093 * This is an intentional behaviour.
1096 * We include the timestamp field in $fields but not in $old_fields.
1097 * Then the row will only get the new timestamp when the other fields had changed.
1099 * When $old_fields is set to a boolean value the system will do this compare itself.
1100 * When $old_fields is set to "true" the system will do an insert if the row doesn't exists.
1103 * Only set $old_fields to a boolean value when you are sure that you will update a single row.
1104 * When you set $old_fields to "true" then $fields must contain all relevant fields!
1106 * @param string $table Table name
1107 * @param array $fields contains the fields that are updated
1108 * @param array $condition condition array with the key values
1109 * @param array|boolean $old_fields array with the old field values that are about to be replaced (true = update on duplicate)
1111 * @return boolean was the update successfull?
1113 public static function update($table, $fields, $condition, $old_fields = []) {
1115 if (empty($table) || empty($fields) || empty($condition)) {
1116 logger('Table, fields and condition have to be set');
1120 $table = self::escape($table);
1122 $condition_string = self::buildCondition($condition);
1124 if (is_bool($old_fields)) {
1125 $do_insert = $old_fields;
1127 $old_fields = self::selectFirst($table, [], $condition);
1129 if (is_bool($old_fields)) {
1131 $values = array_merge($condition, $fields);
1132 return self::insert($table, $values, $do_insert);
1138 $do_update = (count($old_fields) == 0);
1140 foreach ($old_fields AS $fieldname => $content) {
1141 if (isset($fields[$fieldname])) {
1142 if ($fields[$fieldname] == $content) {
1143 unset($fields[$fieldname]);
1150 if (!$do_update || (count($fields) == 0)) {
1154 $sql = "UPDATE `".$table."` SET `".
1155 implode("` = ?, `", array_keys($fields))."` = ?".$condition_string;
1157 $params1 = array_values($fields);
1158 $params2 = array_values($condition);
1159 $params = array_merge_recursive($params1, $params2);
1161 return self::e($sql, $params);
1165 * Retrieve a single record from a table and returns it in an associative array
1167 * @brief Retrieve a single record from a table
1168 * @param string $table
1169 * @param array $fields
1170 * @param array $condition
1171 * @param array $params
1172 * @return bool|array
1175 public static function selectFirst($table, array $fields = [], array $condition = [], $params = [])
1177 $params['limit'] = 1;
1178 $result = self::select($table, $fields, $condition, $params);
1180 if (is_bool($result)) {
1183 $row = self::fetch($result);
1184 self::close($result);
1190 * @brief Select rows from a table
1192 * @param string $table Table name
1193 * @param array $fields Array of selected fields, empty for all
1194 * @param array $condition Array of fields for condition
1195 * @param array $params Array of several parameters
1197 * @return boolean|object
1201 * $fields = array("id", "uri", "uid", "network");
1203 * $condition = array("uid" => 1, "network" => 'dspr');
1205 * $condition = array("`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr');
1207 * $params = array("order" => array("id", "received" => true), "limit" => 10);
1209 * $data = dba::select($table, $fields, $condition, $params);
1211 public static function select($table, array $fields = [], array $condition = [], array $params = [])
1217 $table = self::escape($table);
1219 if (count($fields) > 0) {
1220 $select_fields = "`" . implode("`, `", array_values($fields)) . "`";
1222 $select_fields = "*";
1225 $condition_string = self::buildCondition($condition);
1227 $param_string = self::buildParameter($params);
1229 $sql = "SELECT " . $select_fields . " FROM `" . $table . "`" . $condition_string . $param_string;
1231 $result = self::p($sql, $condition);
1237 * @brief Counts the rows from a table satisfying the provided condition
1239 * @param string $table Table name
1240 * @param array $condition array of fields for condition
1247 * $condition = ["uid" => 1, "network" => 'dspr'];
1249 * $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr'];
1251 * $count = dba::count($table, $condition);
1253 public static function count($table, array $condition = [])
1259 $condition_string = self::buildCondition($condition);
1261 $sql = "SELECT COUNT(*) AS `count` FROM `".$table."`".$condition_string;
1263 $row = self::fetch_first($sql, $condition);
1265 return $row['count'];
1269 * @brief Returns the SQL condition string built from the provided condition array
1271 * This function operates with two modes.
1272 * - Supplied with a filed/value associative array, it builds simple strict
1273 * equality conditions linked by AND.
1274 * - Supplied with a flat list, the first element is the condition string and
1275 * the following arguments are the values to be interpolated
1277 * $condition = ["uid" => 1, "network" => 'dspr'];
1279 * $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr'];
1281 * In either case, the provided array is left with the parameters only
1283 * @param array $condition
1286 public static function buildCondition(array &$condition = [])
1288 $condition_string = '';
1289 if (count($condition) > 0) {
1291 $first_key = key($condition);
1292 if (is_int($first_key)) {
1293 $condition_string = " WHERE (" . array_shift($condition) . ")";
1296 $condition_string = "";
1297 foreach ($condition as $field => $value) {
1298 if ($condition_string != "") {
1299 $condition_string .= " AND ";
1301 if (is_array($value)) {
1303 * Workaround for MySQL Bug #64791.
1304 * Never mix data types inside any IN() condition.
1305 * In case of mixed types, cast all as string.
1306 * Logic needs to be consistent with dba::p() data types.
1310 foreach ($value as $single_value) {
1311 if (is_int($single_value)) {
1318 if ($is_int && $is_alpha) {
1319 foreach ($value as &$ref) {
1321 $ref = (string)$ref;
1324 unset($ref); //Prevent accidental re-use.
1327 $new_values = array_merge($new_values, array_values($value));
1328 $placeholders = substr(str_repeat("?, ", count($value)), 0, -2);
1329 $condition_string .= "`" . $field . "` IN (" . $placeholders . ")";
1331 $new_values[$field] = $value;
1332 $condition_string .= "`" . $field . "` = ?";
1335 $condition_string = " WHERE (" . $condition_string . ")";
1336 $condition = $new_values;
1340 return $condition_string;
1344 * @brief Returns the SQL parameter string built from the provided parameter array
1346 * @param array $params
1349 public static function buildParameter(array $params = [])
1352 if (isset($params['order'])) {
1353 $order_string = " ORDER BY ";
1354 foreach ($params['order'] AS $fields => $order) {
1355 if (!is_int($fields)) {
1356 $order_string .= "`" . $fields . "` " . ($order ? "DESC" : "ASC") . ", ";
1358 $order_string .= "`" . $order . "`, ";
1361 $order_string = substr($order_string, 0, -2);
1365 if (isset($params['limit']) && is_int($params['limit'])) {
1366 $limit_string = " LIMIT " . $params['limit'];
1369 if (isset($params['limit']) && is_array($params['limit'])) {
1370 $limit_string = " LIMIT " . intval($params['limit'][0]) . ", " . intval($params['limit'][1]);
1373 return $order_string.$limit_string;
1377 * @brief Fills an array with data from a query
1379 * @param object $stmt statement object
1380 * @return array Data array
1382 public static function inArray($stmt, $do_close = true) {
1383 if (is_bool($stmt)) {
1388 while ($row = self::fetch($stmt)) {
1398 * @brief Returns the error number of the last query
1400 * @return string Error number (0 if no error)
1402 public static function errorNo() {
1403 return self::$errorno;
1407 * @brief Returns the error message of the last query
1409 * @return string Error message ('' if no error)
1411 public static function errorMessage() {
1412 return self::$error;
1416 * @brief Closes the current statement
1418 * @param object $stmt statement object
1419 * @return boolean was the close successful?
1421 public static function close($stmt) {
1424 $stamp1 = microtime(true);
1426 if (!is_object($stmt)) {
1430 switch (self::$driver) {
1432 $ret = $stmt->closeCursor();
1435 // MySQLi offers both a mysqli_stmt and a mysqli_result class.
1436 // We should be careful not to assume the object type of $stmt
1437 // because dba::p() has been able to return both types.
1438 if ($stmt instanceof mysqli_stmt) {
1439 $stmt->free_result();
1440 $ret = $stmt->close();
1441 } elseif ($stmt instanceof mysqli_result) {
1450 $a->save_timestamp($stamp1, 'database');
1456 function dbesc($str) {
1457 if (dba::$connected) {
1458 return(dba::escape($str));
1460 return(str_replace("'","\\'",$str));
1465 * @brief execute SQL query with printf style args - deprecated
1467 * Please use the dba:: functions instead:
1468 * dba::select, dba::exists, dba::insert
1469 * dba::delete, dba::update, dba::p, dba::e
1471 * @param $args Query parameters (1 to N parameters of different types)
1472 * @return array|bool Query array
1475 $args = func_get_args();
1478 if (!dba::$connected) {
1482 $sql = dba::clean_query($sql);
1483 $sql = dba::any_value_fallback($sql);
1485 $stmt = @vsprintf($sql, $args);
1487 $ret = dba::p($stmt);
1489 if (is_bool($ret)) {
1493 $columns = dba::columnCount($ret);
1495 $data = dba::inArray($ret);
1497 if ((count($data) == 0) && ($columns == 0)) {
1504 function dba_timer() {
1505 return microtime(true);