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 $relation = [];
28 public static function connect($serveraddr, $user, $pass, $db) {
29 if (!is_null(self::$db) && self::connected()) {
35 $stamp1 = microtime(true);
37 $serveraddr = trim($serveraddr);
39 $serverdata = explode(':', $serveraddr);
40 $server = $serverdata[0];
42 if (count($serverdata) > 1) {
43 $port = trim($serverdata[1]);
46 $server = trim($server);
51 if (!(strlen($server) && strlen($user))) {
55 if (class_exists('\PDO') && in_array('mysql', PDO::getAvailableDrivers())) {
56 self::$driver = 'pdo';
57 $connect = "mysql:host=".$server.";dbname=".$db;
60 $connect .= ";port=".$port;
63 if (isset($a->config["system"]["db_charset"])) {
64 $connect .= ";charset=".$a->config["system"]["db_charset"];
67 self::$db = @new PDO($connect, $user, $pass);
68 self::$connected = true;
69 } catch (PDOException $e) {
73 if (!self::$connected && class_exists('mysqli')) {
74 self::$driver = 'mysqli';
75 self::$db = @new mysqli($server, $user, $pass, $db, $port);
76 if (!mysqli_connect_errno()) {
77 self::$connected = true;
79 if (isset($a->config["system"]["db_charset"])) {
80 self::$db->set_charset($a->config["system"]["db_charset"]);
85 // No suitable SQL driver was found.
86 if (!self::$connected) {
90 $a->save_timestamp($stamp1, "network");
92 return self::$connected;
96 * Disconnects the current database connection
98 public static function disconnect()
100 if (is_null(self::$db)) {
104 switch (self::$driver) {
116 * Return the database object.
119 public static function get_db()
125 * @brief Returns the MySQL server version string
127 * This function discriminate between the deprecated mysql API and the current
128 * object-oriented mysqli API. Example of returned string: 5.5.46-0+deb8u1
132 public static function server_info() {
133 if (self::$_server_info == '') {
134 switch (self::$driver) {
136 self::$_server_info = self::$db->getAttribute(PDO::ATTR_SERVER_VERSION);
139 self::$_server_info = self::$db->server_info;
143 return self::$_server_info;
147 * @brief Returns the selected database name
151 public static function database_name() {
152 $ret = self::p("SELECT DATABASE() AS `db`");
153 $data = self::inArray($ret);
154 return $data[0]['db'];
158 * @brief Analyze a database query and log this if some conditions are met.
160 * @param string $query The database query that will be analyzed
162 private static function logIndex($query) {
165 if (empty($a->config["system"]["db_log_index"])) {
169 // Don't explain an explain statement
170 if (strtolower(substr($query, 0, 7)) == "explain") {
174 // Only do the explain on "select", "update" and "delete"
175 if (!in_array(strtolower(substr($query, 0, 6)), ["select", "update", "delete"])) {
179 $r = self::p("EXPLAIN ".$query);
180 if (!DBM::is_result($r)) {
184 $watchlist = explode(',', $a->config["system"]["db_log_index_watch"]);
185 $blacklist = explode(',', $a->config["system"]["db_log_index_blacklist"]);
187 while ($row = dba::fetch($r)) {
188 if ((intval($a->config["system"]["db_loglimit_index"]) > 0)) {
189 $log = (in_array($row['key'], $watchlist) &&
190 ($row['rows'] >= intval($a->config["system"]["db_loglimit_index"])));
195 if ((intval($a->config["system"]["db_loglimit_index_high"]) > 0) && ($row['rows'] >= intval($a->config["system"]["db_loglimit_index_high"]))) {
199 if (in_array($row['key'], $blacklist) || ($row['key'] == "")) {
204 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
205 @file_put_contents($a->config["system"]["db_log_index"], DateTimeFormat::utcNow()."\t".
206 $row['key']."\t".$row['rows']."\t".$row['Extra']."\t".
207 basename($backtrace[1]["file"])."\t".
208 $backtrace[1]["line"]."\t".$backtrace[2]["function"]."\t".
209 substr($query, 0, 2000)."\n", FILE_APPEND);
214 public static function escape($str) {
215 switch (self::$driver) {
217 return substr(@self::$db->quote($str, PDO::PARAM_STR), 1, -1);
219 return @self::$db->real_escape_string($str);
223 public static function connected() {
226 switch (self::$driver) {
228 $r = dba::p("SELECT 1");
229 if (DBM::is_result($r)) {
230 $row = dba::inArray($r);
231 $connected = ($row[0]['1'] == '1');
235 $connected = self::$db->ping();
242 * @brief Replaces ANY_VALUE() function by MIN() function,
243 * if the database server does not support ANY_VALUE().
245 * Considerations for Standard SQL, or MySQL with ONLY_FULL_GROUP_BY (default since 5.7.5).
246 * ANY_VALUE() is available from MySQL 5.7.5 https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html
247 * A standard fall-back is to use MIN().
249 * @param string $sql An SQL string without the values
250 * @return string The input SQL string modified if necessary.
252 public static function any_value_fallback($sql) {
253 $server_info = self::server_info();
254 if (version_compare($server_info, '5.7.5', '<') ||
255 (stripos($server_info, 'MariaDB') !== false)) {
256 $sql = str_ireplace('ANY_VALUE(', 'MIN(', $sql);
262 * @brief beautifies the query - useful for "SHOW PROCESSLIST"
264 * This is safe when we bind the parameters later.
265 * The parameter values aren't part of the SQL.
267 * @param string $sql An SQL string without the values
268 * @return string The input SQL string modified if necessary.
270 public static function clean_query($sql) {
271 $search = ["\t", "\n", "\r", " "];
272 $replace = [' ', ' ', ' ', ' '];
275 $sql = str_replace($search, $replace, $sql);
276 } while ($oldsql != $sql);
283 * @brief Replaces the ? placeholders with the parameters in the $args array
285 * @param string $sql SQL query
286 * @param array $args The parameters that are to replace the ? placeholders
287 * @return string The replaced SQL query
289 private static function replaceParameters($sql, $args) {
291 foreach ($args AS $param => $value) {
292 if (is_int($args[$param]) || is_float($args[$param])) {
293 $replace = intval($args[$param]);
295 $replace = "'".self::escape($args[$param])."'";
298 $pos = strpos($sql, '?', $offset);
299 if ($pos !== false) {
300 $sql = substr_replace($sql, $replace, $pos, 1);
302 $offset = $pos + strlen($replace);
308 * @brief Convert parameter array to an universal form
309 * @param array $args Parameter array
310 * @return array universalized parameter array
312 private static function getParam($args) {
315 // When the second function parameter is an array then use this as the parameter array
316 if ((count($args) > 0) && (is_array($args[1]))) {
324 * @brief Executes a prepared statement that returns data
325 * @usage Example: $r = p("SELECT * FROM `item` WHERE `guid` = ?", $guid);
327 * Please only use it with complicated queries.
328 * For all regular queries please use dba::select or dba::exists
330 * @param string $sql SQL statement
331 * @return bool|object statement object
333 public static function p($sql) {
336 $stamp1 = microtime(true);
338 $params = self::getParam(func_get_args());
340 // Renumber the array keys to be sure that they fit
343 foreach ($params AS $param) {
344 // Avoid problems with some MySQL servers and boolean values. See issue #3645
345 if (is_bool($param)) {
346 $param = (int)$param;
348 $args[++$i] = $param;
351 if (!self::$connected) {
355 if ((substr_count($sql, '?') != count($args)) && (count($args) > 0)) {
356 // Question: Should we continue or stop the query here?
357 logger('Parameter mismatch. Query "'.$sql.'" - Parameters '.print_r($args, true), LOGGER_DEBUG);
360 $sql = self::clean_query($sql);
361 $sql = self::any_value_fallback($sql);
365 if (x($a->config,'system') && x($a->config['system'], 'db_callstack')) {
366 $sql = "/*".System::callstack()." */ ".$sql;
371 self::$affected_rows = 0;
373 // We have to make some things different if this function is called from "e"
374 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
376 if (isset($trace[1])) {
377 $called_from = $trace[1];
379 // We use just something that is defined to avoid warnings
380 $called_from = $trace[0];
382 // We are having an own error logging in the function "e"
383 $called_from_e = ($called_from['function'] == 'e');
385 switch (self::$driver) {
387 // If there are no arguments we use "query"
388 if (count($args) == 0) {
389 if (!$retval = self::$db->query($sql)) {
390 $errorInfo = self::$db->errorInfo();
391 self::$error = $errorInfo[2];
392 self::$errorno = $errorInfo[1];
396 self::$affected_rows = $retval->rowCount();
400 if (!$stmt = self::$db->prepare($sql)) {
401 $errorInfo = self::$db->errorInfo();
402 self::$error = $errorInfo[2];
403 self::$errorno = $errorInfo[1];
408 foreach ($args AS $param => $value) {
409 $stmt->bindParam($param, $args[$param]);
412 if (!$stmt->execute()) {
413 $errorInfo = $stmt->errorInfo();
414 self::$error = $errorInfo[2];
415 self::$errorno = $errorInfo[1];
419 self::$affected_rows = $retval->rowCount();
423 // There are SQL statements that cannot be executed with a prepared statement
424 $parts = explode(' ', $orig_sql);
425 $command = strtolower($parts[0]);
426 $can_be_prepared = in_array($command, ['select', 'update', 'insert', 'delete']);
428 // The fallback routine is called as well when there are no arguments
429 if (!$can_be_prepared || (count($args) == 0)) {
430 $retval = self::$db->query(self::replaceParameters($sql, $args));
431 if (self::$db->errno) {
432 self::$error = self::$db->error;
433 self::$errorno = self::$db->errno;
436 if (isset($retval->num_rows)) {
437 self::$affected_rows = $retval->num_rows;
439 self::$affected_rows = self::$db->affected_rows;
445 $stmt = self::$db->stmt_init();
447 if (!$stmt->prepare($sql)) {
448 self::$error = $stmt->error;
449 self::$errorno = $stmt->errno;
456 foreach ($args AS $param => $value) {
457 if (is_int($args[$param])) {
459 } elseif (is_float($args[$param])) {
461 } elseif (is_string($args[$param])) {
466 $values[] = &$args[$param];
469 if (count($values) > 0) {
470 array_unshift($values, $params);
471 call_user_func_array([$stmt, 'bind_param'], $values);
474 if (!$stmt->execute()) {
475 self::$error = self::$db->error;
476 self::$errorno = self::$db->errno;
479 $stmt->store_result();
481 self::$affected_rows = $retval->affected_rows;
486 // We are having an own error logging in the function "e"
487 if ((self::$errorno != 0) && !$called_from_e) {
488 // We have to preserve the error code, somewhere in the logging it get lost
489 $error = self::$error;
490 $errorno = self::$errorno;
492 logger('DB Error '.self::$errorno.': '.self::$error."\n".
493 System::callstack(8)."\n".self::replaceParameters($sql, $params));
495 self::$error = $error;
496 self::$errorno = $errorno;
499 $a->save_timestamp($stamp1, 'database');
501 if (x($a->config,'system') && x($a->config['system'], 'db_log')) {
503 $stamp2 = microtime(true);
504 $duration = (float)($stamp2 - $stamp1);
506 if (($duration > $a->config["system"]["db_loglimit"])) {
507 $duration = round($duration, 3);
508 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
510 @file_put_contents($a->config["system"]["db_log"], DateTimeFormat::utcNow()."\t".$duration."\t".
511 basename($backtrace[1]["file"])."\t".
512 $backtrace[1]["line"]."\t".$backtrace[2]["function"]."\t".
513 substr(self::replaceParameters($sql, $args), 0, 2000)."\n", FILE_APPEND);
520 * @brief Executes a prepared statement like UPDATE or INSERT that doesn't return data
522 * Please use dba::delete, dba::insert, dba::update, ... instead
524 * @param string $sql SQL statement
525 * @return boolean Was the query successfull? False is returned only if an error occurred
527 public static function e($sql) {
530 $stamp = microtime(true);
532 $params = self::getParam(func_get_args());
534 // In a case of a deadlock we are repeating the query 20 times
538 $stmt = self::p($sql, $params);
540 if (is_bool($stmt)) {
542 } elseif (is_object($stmt)) {
550 } while ((self::$errorno == 1213) && (--$timeout > 0));
552 if (self::$errorno != 0) {
553 // We have to preserve the error code, somewhere in the logging it get lost
554 $error = self::$error;
555 $errorno = self::$errorno;
557 logger('DB Error '.self::$errorno.': '.self::$error."\n".
558 System::callstack(8)."\n".self::replaceParameters($sql, $params));
560 self::$error = $error;
561 self::$errorno = $errorno;
564 $a->save_timestamp($stamp, "database_write");
570 * @brief Check if data exists
572 * @param string $table Table name
573 * @param array $condition array of fields for condition
575 * @return boolean Are there rows for that condition?
577 public static function exists($table, $condition) {
585 $first_key = key($condition);
586 if (!is_int($first_key)) {
587 $fields = [$first_key];
590 $stmt = self::select($table, $fields, $condition, ['limit' => 1]);
592 if (is_bool($stmt)) {
595 $retval = (self::num_rows($stmt) > 0);
604 * Fetches the first row
606 * Please use dba::selectFirst or dba::exists whenever this is possible.
608 * @brief Fetches the first row
609 * @param string $sql SQL statement
610 * @return array first row of query
612 public static function fetch_first($sql) {
613 $params = self::getParam(func_get_args());
615 $stmt = self::p($sql, $params);
617 if (is_bool($stmt)) {
620 $retval = self::fetch($stmt);
629 * @brief Returns the number of affected rows of the last statement
631 * @return int Number of rows
633 public static function affected_rows() {
634 return self::$affected_rows;
638 * @brief Returns the number of columns of a statement
640 * @param object Statement object
641 * @return int Number of columns
643 public static function columnCount($stmt) {
644 if (!is_object($stmt)) {
647 switch (self::$driver) {
649 return $stmt->columnCount();
651 return $stmt->field_count;
656 * @brief Returns the number of rows of a statement
658 * @param PDOStatement|mysqli_result|mysqli_stmt Statement object
659 * @return int Number of rows
661 public static function num_rows($stmt) {
662 if (!is_object($stmt)) {
665 switch (self::$driver) {
667 return $stmt->rowCount();
669 return $stmt->num_rows;
675 * @brief Fetch a single row
677 * @param mixed $stmt statement object
678 * @return array current row
680 public static function fetch($stmt) {
683 $stamp1 = microtime(true);
687 if (!is_object($stmt)) {
691 switch (self::$driver) {
693 $columns = $stmt->fetch(PDO::FETCH_ASSOC);
696 if (get_class($stmt) == 'mysqli_result') {
697 $columns = $stmt->fetch_assoc();
701 // This code works, but is slow
703 // Bind the result to a result array
707 for ($x = 0; $x < $stmt->field_count; $x++) {
708 $cols[] = &$cols_num[$x];
711 call_user_func_array([$stmt, 'bind_result'], $cols);
713 if (!$stmt->fetch()) {
718 // We need to get the field names for the array keys
719 // It seems that there is no better way to do this.
720 $result = $stmt->result_metadata();
721 $fields = $result->fetch_fields();
723 foreach ($cols_num AS $param => $col) {
724 $columns[$fields[$param]->name] = $col;
728 $a->save_timestamp($stamp1, 'database');
734 * @brief Insert a row into a table
736 * @param string $table Table name
737 * @param array $param parameter array
738 * @param bool $on_duplicate_update Do an update on a duplicate entry
740 * @return boolean was the insert successfull?
742 public static function insert($table, $param, $on_duplicate_update = false) {
744 if (empty($table) || empty($param)) {
745 logger('Table and fields have to be set');
749 $sql = "INSERT INTO `".self::escape($table)."` (`".implode("`, `", array_keys($param))."`) VALUES (".
750 substr(str_repeat("?, ", count($param)), 0, -2).")";
752 if ($on_duplicate_update) {
753 $sql .= " ON DUPLICATE KEY UPDATE `".implode("` = ?, `", array_keys($param))."` = ?";
755 $values = array_values($param);
756 $param = array_merge_recursive($values, $values);
759 return self::e($sql, $param);
763 * @brief Fetch the id of the last insert command
765 * @return integer Last inserted id
767 public static function lastInsertId() {
768 switch (self::$driver) {
770 $id = self::$db->lastInsertId();
773 $id = self::$db->insert_id;
780 * @brief Locks a table for exclusive write access
782 * This function can be extended in the future to accept a table array as well.
784 * @param string $table Table name
786 * @return boolean was the lock successful?
788 public static function lock($table) {
789 // See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
790 self::e("SET autocommit=0");
791 $success = self::e("LOCK TABLES `".self::escape($table)."` WRITE");
793 self::e("SET autocommit=1");
795 self::$in_transaction = true;
801 * @brief Unlocks all locked tables
803 * @return boolean was the unlock successful?
805 public static function unlock() {
806 // See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
808 $success = self::e("UNLOCK TABLES");
809 self::e("SET autocommit=1");
810 self::$in_transaction = false;
815 * @brief Starts a transaction
817 * @return boolean Was the command executed successfully?
819 public static function transaction() {
820 if (!self::e('COMMIT')) {
823 if (!self::e('START TRANSACTION')) {
826 self::$in_transaction = true;
831 * @brief Does a commit
833 * @return boolean Was the command executed successfully?
835 public static function commit() {
836 if (!self::e('COMMIT')) {
839 self::$in_transaction = false;
844 * @brief Does a rollback
846 * @return boolean Was the command executed successfully?
848 public static function rollback() {
849 if (!self::e('ROLLBACK')) {
852 self::$in_transaction = false;
857 * @brief Build the array with the table relations
859 * The array is build from the database definitions in DBStructure.php
861 * This process must only be started once, since the value is cached.
863 private static function buildRelationData() {
864 $definition = DBStructure::definition();
866 foreach ($definition AS $table => $structure) {
867 foreach ($structure['fields'] AS $field => $field_struct) {
868 if (isset($field_struct['relation'])) {
869 foreach ($field_struct['relation'] AS $rel_table => $rel_field) {
870 self::$relation[$rel_table][$rel_field][$table][] = $field;
878 * @brief Delete a row from a table
880 * @param string $table Table name
881 * @param array $conditions Field condition(s)
882 * @param array $options
883 * - cascade: If true we delete records in other tables that depend on the one we're deleting through
884 * relations (default: true)
885 * @param boolean $in_process Internal use: Only do a commit after the last delete
886 * @param array $callstack Internal use: prevent endless loops
888 * @return boolean|array was the delete successful? When $in_process is set: deletion data
890 public static function delete($table, array $conditions, array $options = [], $in_process = false, array &$callstack = [])
892 if (empty($table) || empty($conditions)) {
893 logger('Table and conditions have to be set');
899 // Create a key for the loop prevention
900 $key = $table . ':' . implode(':', array_keys($conditions)) . ':' . implode(':', $conditions);
902 // We quit when this key already exists in the callstack.
903 if (isset($callstack[$key])) {
907 $callstack[$key] = true;
909 $table = self::escape($table);
911 $commands[$key] = ['table' => $table, 'conditions' => $conditions];
913 $cascade = defaults($options, 'cascade', true);
915 // To speed up the whole process we cache the table relations
916 if ($cascade && count(self::$relation) == 0) {
917 self::buildRelationData();
920 // Is there a relation entry for the table?
921 if ($cascade && isset(self::$relation[$table])) {
922 // We only allow a simple "one field" relation.
923 $field = array_keys(self::$relation[$table])[0];
924 $rel_def = array_values(self::$relation[$table])[0];
926 // Create a key for preventing double queries
927 $qkey = $field . '-' . $table . ':' . implode(':', array_keys($conditions)) . ':' . implode(':', $conditions);
929 // When the search field is the relation field, we don't need to fetch the rows
930 // This is useful when the leading record is already deleted in the frontend but the rest is done in the backend
931 if ((count($conditions) == 1) && ($field == array_keys($conditions)[0])) {
932 foreach ($rel_def AS $rel_table => $rel_fields) {
933 foreach ($rel_fields AS $rel_field) {
934 $retval = self::delete($rel_table, [$rel_field => array_values($conditions)[0]], $options, true, $callstack);
935 $commands = array_merge($commands, $retval);
938 // We quit when this key already exists in the callstack.
939 } elseif (!isset($callstack[$qkey])) {
941 $callstack[$qkey] = true;
943 // Fetch all rows that are to be deleted
944 $data = self::select($table, [$field], $conditions);
946 while ($row = self::fetch($data)) {
947 // Now we accumulate the delete commands
948 $retval = self::delete($table, [$field => $row[$field]], $options, true, $callstack);
949 $commands = array_merge($commands, $retval);
954 // Since we had split the delete command we don't need the original command anymore
955 unset($commands[$key]);
960 // Now we finalize the process
961 $do_transaction = !self::$in_transaction;
963 if ($do_transaction) {
970 foreach ($commands AS $command) {
971 $conditions = $command['conditions'];
973 $first_key = key($conditions);
975 $condition_string = self::buildCondition($conditions);
977 if ((count($command['conditions']) > 1) || is_int($first_key)) {
978 $sql = "DELETE FROM `" . $command['table'] . "`" . $condition_string;
979 logger(self::replaceParameters($sql, $conditions), LOGGER_DATA);
981 if (!self::e($sql, $conditions)) {
982 if ($do_transaction) {
988 $key_table = $command['table'];
989 $key_condition = array_keys($command['conditions'])[0];
990 $value = array_values($command['conditions'])[0];
992 // Split the SQL queries in chunks of 100 values
993 // We do the $i stuff here to make the code better readable
994 $i = $counter[$key_table][$key_condition];
995 if (isset($compacted[$key_table][$key_condition][$i]) && count($compacted[$key_table][$key_condition][$i]) > 100) {
999 $compacted[$key_table][$key_condition][$i][$value] = $value;
1000 $counter[$key_table][$key_condition] = $i;
1003 foreach ($compacted AS $table => $values) {
1004 foreach ($values AS $field => $field_value_list) {
1005 foreach ($field_value_list AS $field_values) {
1006 $sql = "DELETE FROM `" . $table . "` WHERE `" . $field . "` IN (" .
1007 substr(str_repeat("?, ", count($field_values)), 0, -2) . ");";
1009 logger(self::replaceParameters($sql, $field_values), LOGGER_DATA);
1011 if (!self::e($sql, $field_values)) {
1012 if ($do_transaction) {
1020 if ($do_transaction) {
1030 * @brief Updates rows
1032 * Updates rows in the database. When $old_fields is set to an array,
1033 * the system will only do an update if the fields in that array changed.
1036 * Only the values in $old_fields are compared.
1037 * This is an intentional behaviour.
1040 * We include the timestamp field in $fields but not in $old_fields.
1041 * Then the row will only get the new timestamp when the other fields had changed.
1043 * When $old_fields is set to a boolean value the system will do this compare itself.
1044 * When $old_fields is set to "true" the system will do an insert if the row doesn't exists.
1047 * Only set $old_fields to a boolean value when you are sure that you will update a single row.
1048 * When you set $old_fields to "true" then $fields must contain all relevant fields!
1050 * @param string $table Table name
1051 * @param array $fields contains the fields that are updated
1052 * @param array $condition condition array with the key values
1053 * @param array|boolean $old_fields array with the old field values that are about to be replaced (true = update on duplicate)
1055 * @return boolean was the update successfull?
1057 public static function update($table, $fields, $condition, $old_fields = []) {
1059 if (empty($table) || empty($fields) || empty($condition)) {
1060 logger('Table, fields and condition have to be set');
1064 $table = self::escape($table);
1066 $condition_string = self::buildCondition($condition);
1068 if (is_bool($old_fields)) {
1069 $do_insert = $old_fields;
1071 $old_fields = self::selectFirst($table, [], $condition);
1073 if (is_bool($old_fields)) {
1075 $values = array_merge($condition, $fields);
1076 return self::insert($table, $values, $do_insert);
1082 $do_update = (count($old_fields) == 0);
1084 foreach ($old_fields AS $fieldname => $content) {
1085 if (isset($fields[$fieldname])) {
1086 if ($fields[$fieldname] == $content) {
1087 unset($fields[$fieldname]);
1094 if (!$do_update || (count($fields) == 0)) {
1098 $sql = "UPDATE `".$table."` SET `".
1099 implode("` = ?, `", array_keys($fields))."` = ?".$condition_string;
1101 $params1 = array_values($fields);
1102 $params2 = array_values($condition);
1103 $params = array_merge_recursive($params1, $params2);
1105 return self::e($sql, $params);
1109 * Retrieve a single record from a table and returns it in an associative array
1111 * @brief Retrieve a single record from a table
1112 * @param string $table
1113 * @param array $fields
1114 * @param array $condition
1115 * @param array $params
1116 * @return bool|array
1119 public static function selectFirst($table, array $fields = [], array $condition = [], $params = [])
1121 $params['limit'] = 1;
1122 $result = self::select($table, $fields, $condition, $params);
1124 if (is_bool($result)) {
1127 $row = self::fetch($result);
1128 self::close($result);
1134 * @brief Select rows from a table
1136 * @param string $table Table name
1137 * @param array $fields Array of selected fields, empty for all
1138 * @param array $condition Array of fields for condition
1139 * @param array $params Array of several parameters
1141 * @return boolean|object
1145 * $fields = array("id", "uri", "uid", "network");
1147 * $condition = array("uid" => 1, "network" => 'dspr');
1149 * $condition = array("`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr');
1151 * $params = array("order" => array("id", "received" => true), "limit" => 10);
1153 * $data = dba::select($table, $fields, $condition, $params);
1155 public static function select($table, array $fields = [], array $condition = [], array $params = [])
1161 $table = self::escape($table);
1163 if (count($fields) > 0) {
1164 $select_fields = "`" . implode("`, `", array_values($fields)) . "`";
1166 $select_fields = "*";
1169 $condition_string = self::buildCondition($condition);
1171 $param_string = self::buildParameter($params);
1173 $sql = "SELECT " . $select_fields . " FROM `" . $table . "`" . $condition_string . $param_string;
1175 $result = self::p($sql, $condition);
1181 * @brief Counts the rows from a table satisfying the provided condition
1183 * @param string $table Table name
1184 * @param array $condition array of fields for condition
1191 * $condition = ["uid" => 1, "network" => 'dspr'];
1193 * $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr'];
1195 * $count = dba::count($table, $condition);
1197 public static function count($table, array $condition = [])
1203 $condition_string = self::buildCondition($condition);
1205 $sql = "SELECT COUNT(*) AS `count` FROM `".$table."`".$condition_string;
1207 $row = self::fetch_first($sql, $condition);
1209 return $row['count'];
1213 * @brief Returns the SQL condition string built from the provided condition array
1215 * This function operates with two modes.
1216 * - Supplied with a filed/value associative array, it builds simple strict
1217 * equality conditions linked by AND.
1218 * - Supplied with a flat list, the first element is the condition string and
1219 * the following arguments are the values to be interpolated
1221 * $condition = ["uid" => 1, "network" => 'dspr'];
1223 * $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr'];
1225 * In either case, the provided array is left with the parameters only
1227 * @param array $condition
1230 public static function buildCondition(array &$condition = [])
1232 $condition_string = '';
1233 if (count($condition) > 0) {
1235 $first_key = key($condition);
1236 if (is_int($first_key)) {
1237 $condition_string = " WHERE (" . array_shift($condition) . ")";
1240 $condition_string = "";
1241 foreach ($condition as $field => $value) {
1242 if ($condition_string != "") {
1243 $condition_string .= " AND ";
1245 if (is_array($value)) {
1246 $new_values = array_merge($new_values, array_values($value));
1247 $placeholders = substr(str_repeat("?, ", count($value)), 0, -2);
1248 $condition_string .= "`" . $field . "` IN (" . $placeholders . ")";
1250 $new_values[$field] = $value;
1251 $condition_string .= "`" . $field . "` = ?";
1254 $condition_string = " WHERE (" . $condition_string . ")";
1255 $condition = $new_values;
1259 return $condition_string;
1263 * @brief Returns the SQL parameter string built from the provided parameter array
1265 * @param array $params
1268 public static function buildParameter(array $params = [])
1271 if (isset($params['order'])) {
1272 $order_string = " ORDER BY ";
1273 foreach ($params['order'] AS $fields => $order) {
1274 if (!is_int($fields)) {
1275 $order_string .= "`" . $fields . "` " . ($order ? "DESC" : "ASC") . ", ";
1277 $order_string .= "`" . $order . "`, ";
1280 $order_string = substr($order_string, 0, -2);
1284 if (isset($params['limit']) && is_int($params['limit'])) {
1285 $limit_string = " LIMIT " . $params['limit'];
1288 if (isset($params['limit']) && is_array($params['limit'])) {
1289 $limit_string = " LIMIT " . intval($params['limit'][0]) . ", " . intval($params['limit'][1]);
1292 return $order_string.$limit_string;
1296 * @brief Fills an array with data from a query
1298 * @param object $stmt statement object
1299 * @return array Data array
1301 public static function inArray($stmt, $do_close = true) {
1302 if (is_bool($stmt)) {
1307 while ($row = self::fetch($stmt)) {
1317 * @brief Returns the error number of the last query
1319 * @return string Error number (0 if no error)
1321 public static function errorNo() {
1322 return self::$errorno;
1326 * @brief Returns the error message of the last query
1328 * @return string Error message ('' if no error)
1330 public static function errorMessage() {
1331 return self::$error;
1335 * @brief Closes the current statement
1337 * @param object $stmt statement object
1338 * @return boolean was the close successful?
1340 public static function close($stmt) {
1343 $stamp1 = microtime(true);
1345 if (!is_object($stmt)) {
1349 switch (self::$driver) {
1351 $ret = $stmt->closeCursor();
1354 $stmt->free_result();
1355 $ret = $stmt->close();
1359 $a->save_timestamp($stamp1, 'database');
1365 function dbesc($str) {
1366 if (dba::$connected) {
1367 return(dba::escape($str));
1369 return(str_replace("'","\\'",$str));
1374 * @brief execute SQL query with printf style args - deprecated
1376 * Please use the dba:: functions instead:
1377 * dba::select, dba::exists, dba::insert
1378 * dba::delete, dba::update, dba::p, dba::e
1380 * @param $args Query parameters (1 to N parameters of different types)
1381 * @return array|bool Query array
1384 $args = func_get_args();
1387 if (!dba::$connected) {
1391 $sql = dba::clean_query($sql);
1392 $sql = dba::any_value_fallback($sql);
1394 $stmt = @vsprintf($sql, $args);
1396 $ret = dba::p($stmt);
1398 if (is_bool($ret)) {
1402 $columns = dba::columnCount($ret);
1404 $data = dba::inArray($ret);
1406 if ((count($data) == 0) && ($columns == 0)) {
1413 function dba_timer() {
1414 return microtime(true);