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)) {
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 * @brief Returns the MySQL server version string
98 * This function discriminate between the deprecated mysql API and the current
99 * object-oriented mysqli API. Example of returned string: 5.5.46-0+deb8u1
103 public static function server_info() {
104 if (self::$_server_info == '') {
105 switch (self::$driver) {
107 self::$_server_info = self::$db->getAttribute(PDO::ATTR_SERVER_VERSION);
110 self::$_server_info = self::$db->server_info;
114 return self::$_server_info;
118 * @brief Returns the selected database name
122 public static function database_name() {
123 $ret = self::p("SELECT DATABASE() AS `db`");
124 $data = self::inArray($ret);
125 return $data[0]['db'];
129 * @brief Analyze a database query and log this if some conditions are met.
131 * @param string $query The database query that will be analyzed
133 private static function logIndex($query) {
136 if (empty($a->config["system"]["db_log_index"])) {
140 // Don't explain an explain statement
141 if (strtolower(substr($query, 0, 7)) == "explain") {
145 // Only do the explain on "select", "update" and "delete"
146 if (!in_array(strtolower(substr($query, 0, 6)), ["select", "update", "delete"])) {
150 $r = self::p("EXPLAIN ".$query);
151 if (!DBM::is_result($r)) {
155 $watchlist = explode(',', $a->config["system"]["db_log_index_watch"]);
156 $blacklist = explode(',', $a->config["system"]["db_log_index_blacklist"]);
158 while ($row = dba::fetch($r)) {
159 if ((intval($a->config["system"]["db_loglimit_index"]) > 0)) {
160 $log = (in_array($row['key'], $watchlist) &&
161 ($row['rows'] >= intval($a->config["system"]["db_loglimit_index"])));
166 if ((intval($a->config["system"]["db_loglimit_index_high"]) > 0) && ($row['rows'] >= intval($a->config["system"]["db_loglimit_index_high"]))) {
170 if (in_array($row['key'], $blacklist) || ($row['key'] == "")) {
175 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
176 @file_put_contents($a->config["system"]["db_log_index"], DateTimeFormat::utcNow()."\t".
177 $row['key']."\t".$row['rows']."\t".$row['Extra']."\t".
178 basename($backtrace[1]["file"])."\t".
179 $backtrace[1]["line"]."\t".$backtrace[2]["function"]."\t".
180 substr($query, 0, 2000)."\n", FILE_APPEND);
185 public static function escape($str) {
186 switch (self::$driver) {
188 return substr(@self::$db->quote($str, PDO::PARAM_STR), 1, -1);
190 return @self::$db->real_escape_string($str);
194 public static function connected() {
197 switch (self::$driver) {
199 $r = dba::p("SELECT 1");
200 if (DBM::is_result($r)) {
201 $row = dba::inArray($r);
202 $connected = ($row[0]['1'] == '1');
206 $connected = self::$db->ping();
213 * @brief Replaces ANY_VALUE() function by MIN() function,
214 * if the database server does not support ANY_VALUE().
216 * Considerations for Standard SQL, or MySQL with ONLY_FULL_GROUP_BY (default since 5.7.5).
217 * ANY_VALUE() is available from MySQL 5.7.5 https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html
218 * A standard fall-back is to use MIN().
220 * @param string $sql An SQL string without the values
221 * @return string The input SQL string modified if necessary.
223 public static function any_value_fallback($sql) {
224 $server_info = self::server_info();
225 if (version_compare($server_info, '5.7.5', '<') ||
226 (stripos($server_info, 'MariaDB') !== false)) {
227 $sql = str_ireplace('ANY_VALUE(', 'MIN(', $sql);
233 * @brief beautifies the query - useful for "SHOW PROCESSLIST"
235 * This is safe when we bind the parameters later.
236 * The parameter values aren't part of the SQL.
238 * @param string $sql An SQL string without the values
239 * @return string The input SQL string modified if necessary.
241 public static function clean_query($sql) {
242 $search = ["\t", "\n", "\r", " "];
243 $replace = [' ', ' ', ' ', ' '];
246 $sql = str_replace($search, $replace, $sql);
247 } while ($oldsql != $sql);
254 * @brief Replaces the ? placeholders with the parameters in the $args array
256 * @param string $sql SQL query
257 * @param array $args The parameters that are to replace the ? placeholders
258 * @return string The replaced SQL query
260 private static function replaceParameters($sql, $args) {
262 foreach ($args AS $param => $value) {
263 if (is_int($args[$param]) || is_float($args[$param])) {
264 $replace = intval($args[$param]);
266 $replace = "'".self::escape($args[$param])."'";
269 $pos = strpos($sql, '?', $offset);
270 if ($pos !== false) {
271 $sql = substr_replace($sql, $replace, $pos, 1);
273 $offset = $pos + strlen($replace);
279 * @brief Convert parameter array to an universal form
280 * @param array $args Parameter array
281 * @return array universalized parameter array
283 private static function getParam($args) {
286 // When the second function parameter is an array then use this as the parameter array
287 if ((count($args) > 0) && (is_array($args[1]))) {
295 * @brief Executes a prepared statement that returns data
296 * @usage Example: $r = p("SELECT * FROM `item` WHERE `guid` = ?", $guid);
298 * Please only use it with complicated queries.
299 * For all regular queries please use dba::select or dba::exists
301 * @param string $sql SQL statement
302 * @return bool|object statement object
304 public static function p($sql) {
307 $stamp1 = microtime(true);
309 $params = self::getParam(func_get_args());
311 // Renumber the array keys to be sure that they fit
314 foreach ($params AS $param) {
315 // Avoid problems with some MySQL servers and boolean values. See issue #3645
316 if (is_bool($param)) {
317 $param = (int)$param;
319 $args[++$i] = $param;
322 if (!self::$connected) {
326 if ((substr_count($sql, '?') != count($args)) && (count($args) > 0)) {
327 // Question: Should we continue or stop the query here?
328 logger('Parameter mismatch. Query "'.$sql.'" - Parameters '.print_r($args, true), LOGGER_DEBUG);
331 $sql = self::clean_query($sql);
332 $sql = self::any_value_fallback($sql);
336 if (x($a->config,'system') && x($a->config['system'], 'db_callstack')) {
337 $sql = "/*".System::callstack()." */ ".$sql;
342 self::$affected_rows = 0;
344 // We have to make some things different if this function is called from "e"
345 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
347 if (isset($trace[1])) {
348 $called_from = $trace[1];
350 // We use just something that is defined to avoid warnings
351 $called_from = $trace[0];
353 // We are having an own error logging in the function "e"
354 $called_from_e = ($called_from['function'] == 'e');
356 switch (self::$driver) {
358 // If there are no arguments we use "query"
359 if (count($args) == 0) {
360 if (!$retval = self::$db->query($sql)) {
361 $errorInfo = self::$db->errorInfo();
362 self::$error = $errorInfo[2];
363 self::$errorno = $errorInfo[1];
367 self::$affected_rows = $retval->rowCount();
371 if (!$stmt = self::$db->prepare($sql)) {
372 $errorInfo = self::$db->errorInfo();
373 self::$error = $errorInfo[2];
374 self::$errorno = $errorInfo[1];
379 foreach ($args AS $param => $value) {
380 $stmt->bindParam($param, $args[$param]);
383 if (!$stmt->execute()) {
384 $errorInfo = $stmt->errorInfo();
385 self::$error = $errorInfo[2];
386 self::$errorno = $errorInfo[1];
390 self::$affected_rows = $retval->rowCount();
394 // There are SQL statements that cannot be executed with a prepared statement
395 $parts = explode(' ', $orig_sql);
396 $command = strtolower($parts[0]);
397 $can_be_prepared = in_array($command, ['select', 'update', 'insert', 'delete']);
399 // The fallback routine is called as well when there are no arguments
400 if (!$can_be_prepared || (count($args) == 0)) {
401 $retval = self::$db->query(self::replaceParameters($sql, $args));
402 if (self::$db->errno) {
403 self::$error = self::$db->error;
404 self::$errorno = self::$db->errno;
407 if (isset($retval->num_rows)) {
408 self::$affected_rows = $retval->num_rows;
410 self::$affected_rows = self::$db->affected_rows;
416 $stmt = self::$db->stmt_init();
418 if (!$stmt->prepare($sql)) {
419 self::$error = $stmt->error;
420 self::$errorno = $stmt->errno;
427 foreach ($args AS $param => $value) {
428 if (is_int($args[$param])) {
430 } elseif (is_float($args[$param])) {
432 } elseif (is_string($args[$param])) {
437 $values[] = &$args[$param];
440 if (count($values) > 0) {
441 array_unshift($values, $params);
442 call_user_func_array([$stmt, 'bind_param'], $values);
445 if (!$stmt->execute()) {
446 self::$error = self::$db->error;
447 self::$errorno = self::$db->errno;
450 $stmt->store_result();
452 self::$affected_rows = $retval->affected_rows;
457 // We are having an own error logging in the function "e"
458 if ((self::$errorno != 0) && !$called_from_e) {
459 // We have to preserve the error code, somewhere in the logging it get lost
460 $error = self::$error;
461 $errorno = self::$errorno;
463 logger('DB Error '.self::$errorno.': '.self::$error."\n".
464 System::callstack(8)."\n".self::replaceParameters($sql, $params));
466 self::$error = $error;
467 self::$errorno = $errorno;
470 $a->save_timestamp($stamp1, 'database');
472 if (x($a->config,'system') && x($a->config['system'], 'db_log')) {
474 $stamp2 = microtime(true);
475 $duration = (float)($stamp2 - $stamp1);
477 if (($duration > $a->config["system"]["db_loglimit"])) {
478 $duration = round($duration, 3);
479 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
481 @file_put_contents($a->config["system"]["db_log"], DateTimeFormat::utcNow()."\t".$duration."\t".
482 basename($backtrace[1]["file"])."\t".
483 $backtrace[1]["line"]."\t".$backtrace[2]["function"]."\t".
484 substr(self::replaceParameters($sql, $args), 0, 2000)."\n", FILE_APPEND);
491 * @brief Executes a prepared statement like UPDATE or INSERT that doesn't return data
493 * Please use dba::delete, dba::insert, dba::update, ... instead
495 * @param string $sql SQL statement
496 * @return boolean Was the query successfull? False is returned only if an error occurred
498 public static function e($sql) {
501 $stamp = microtime(true);
503 $params = self::getParam(func_get_args());
505 // In a case of a deadlock we are repeating the query 20 times
509 $stmt = self::p($sql, $params);
511 if (is_bool($stmt)) {
513 } elseif (is_object($stmt)) {
521 } while ((self::$errorno == 1213) && (--$timeout > 0));
523 if (self::$errorno != 0) {
524 // We have to preserve the error code, somewhere in the logging it get lost
525 $error = self::$error;
526 $errorno = self::$errorno;
528 logger('DB Error '.self::$errorno.': '.self::$error."\n".
529 System::callstack(8)."\n".self::replaceParameters($sql, $params));
531 self::$error = $error;
532 self::$errorno = $errorno;
535 $a->save_timestamp($stamp, "database_write");
541 * @brief Check if data exists
543 * @param string $table Table name
544 * @param array $condition array of fields for condition
546 * @return boolean Are there rows for that condition?
548 public static function exists($table, $condition) {
556 $first_key = key($condition);
557 if (!is_int($first_key)) {
558 $fields = [$first_key];
561 $stmt = self::select($table, $fields, $condition, ['limit' => 1]);
563 if (is_bool($stmt)) {
566 $retval = (self::num_rows($stmt) > 0);
575 * Fetches the first row
577 * Please use dba::selectFirst or dba::exists whenever this is possible.
579 * @brief Fetches the first row
580 * @param string $sql SQL statement
581 * @return array first row of query
583 public static function fetch_first($sql) {
584 $params = self::getParam(func_get_args());
586 $stmt = self::p($sql, $params);
588 if (is_bool($stmt)) {
591 $retval = self::fetch($stmt);
600 * @brief Returns the number of affected rows of the last statement
602 * @return int Number of rows
604 public static function affected_rows() {
605 return self::$affected_rows;
609 * @brief Returns the number of columns of a statement
611 * @param object Statement object
612 * @return int Number of columns
614 public static function columnCount($stmt) {
615 if (!is_object($stmt)) {
618 switch (self::$driver) {
620 return $stmt->columnCount();
622 return $stmt->field_count;
627 * @brief Returns the number of rows of a statement
629 * @param PDOStatement|mysqli_result|mysqli_stmt Statement object
630 * @return int Number of rows
632 public static function num_rows($stmt) {
633 if (!is_object($stmt)) {
636 switch (self::$driver) {
638 return $stmt->rowCount();
640 return $stmt->num_rows;
646 * @brief Fetch a single row
648 * @param mixed $stmt statement object
649 * @return array current row
651 public static function fetch($stmt) {
654 $stamp1 = microtime(true);
658 if (!is_object($stmt)) {
662 switch (self::$driver) {
664 $columns = $stmt->fetch(PDO::FETCH_ASSOC);
667 if (get_class($stmt) == 'mysqli_result') {
668 $columns = $stmt->fetch_assoc();
672 // This code works, but is slow
674 // Bind the result to a result array
678 for ($x = 0; $x < $stmt->field_count; $x++) {
679 $cols[] = &$cols_num[$x];
682 call_user_func_array([$stmt, 'bind_result'], $cols);
684 if (!$stmt->fetch()) {
689 // We need to get the field names for the array keys
690 // It seems that there is no better way to do this.
691 $result = $stmt->result_metadata();
692 $fields = $result->fetch_fields();
694 foreach ($cols_num AS $param => $col) {
695 $columns[$fields[$param]->name] = $col;
699 $a->save_timestamp($stamp1, 'database');
705 * @brief Insert a row into a table
707 * @param string $table Table name
708 * @param array $param parameter array
709 * @param bool $on_duplicate_update Do an update on a duplicate entry
711 * @return boolean was the insert successfull?
713 public static function insert($table, $param, $on_duplicate_update = false) {
715 if (empty($table) || empty($param)) {
716 logger('Table and fields have to be set');
720 $sql = "INSERT INTO `".self::escape($table)."` (`".implode("`, `", array_keys($param))."`) VALUES (".
721 substr(str_repeat("?, ", count($param)), 0, -2).")";
723 if ($on_duplicate_update) {
724 $sql .= " ON DUPLICATE KEY UPDATE `".implode("` = ?, `", array_keys($param))."` = ?";
726 $values = array_values($param);
727 $param = array_merge_recursive($values, $values);
730 return self::e($sql, $param);
734 * @brief Fetch the id of the last insert command
736 * @return integer Last inserted id
738 public static function lastInsertId() {
739 switch (self::$driver) {
741 $id = self::$db->lastInsertId();
744 $id = self::$db->insert_id;
751 * @brief Locks a table for exclusive write access
753 * This function can be extended in the future to accept a table array as well.
755 * @param string $table Table name
757 * @return boolean was the lock successful?
759 public static function lock($table) {
760 // See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
761 self::e("SET autocommit=0");
762 $success = self::e("LOCK TABLES `".self::escape($table)."` WRITE");
764 self::e("SET autocommit=1");
766 self::$in_transaction = true;
772 * @brief Unlocks all locked tables
774 * @return boolean was the unlock successful?
776 public static function unlock() {
777 // See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
779 $success = self::e("UNLOCK TABLES");
780 self::e("SET autocommit=1");
781 self::$in_transaction = false;
786 * @brief Starts a transaction
788 * @return boolean Was the command executed successfully?
790 public static function transaction() {
791 if (!self::e('COMMIT')) {
794 if (!self::e('START TRANSACTION')) {
797 self::$in_transaction = true;
802 * @brief Does a commit
804 * @return boolean Was the command executed successfully?
806 public static function commit() {
807 if (!self::e('COMMIT')) {
810 self::$in_transaction = false;
815 * @brief Does a rollback
817 * @return boolean Was the command executed successfully?
819 public static function rollback() {
820 if (!self::e('ROLLBACK')) {
823 self::$in_transaction = false;
828 * @brief Build the array with the table relations
830 * The array is build from the database definitions in DBStructure.php
832 * This process must only be started once, since the value is cached.
834 private static function buildRelationData() {
835 $definition = DBStructure::definition();
837 foreach ($definition AS $table => $structure) {
838 foreach ($structure['fields'] AS $field => $field_struct) {
839 if (isset($field_struct['relation'])) {
840 foreach ($field_struct['relation'] AS $rel_table => $rel_field) {
841 self::$relation[$rel_table][$rel_field][$table][] = $field;
849 * @brief Delete a row from a table
851 * @param string $table Table name
852 * @param array $conditions Field condition(s)
853 * @param array $options
854 * - cascade: If true we delete records in other tables that depend on the one we're deleting through
855 * relations (default: true)
856 * @param boolean $in_process Internal use: Only do a commit after the last delete
857 * @param array $callstack Internal use: prevent endless loops
859 * @return boolean|array was the delete successful? When $in_process is set: deletion data
861 public static function delete($table, array $conditions, array $options = [], $in_process = false, array &$callstack = [])
863 if (empty($table) || empty($conditions)) {
864 logger('Table and conditions have to be set');
870 // Create a key for the loop prevention
871 $key = $table . ':' . implode(':', array_keys($conditions)) . ':' . implode(':', $conditions);
873 // We quit when this key already exists in the callstack.
874 if (isset($callstack[$key])) {
878 $callstack[$key] = true;
880 $table = self::escape($table);
882 $commands[$key] = ['table' => $table, 'conditions' => $conditions];
884 $cascade = defaults($options, 'cascade', true);
886 // To speed up the whole process we cache the table relations
887 if ($cascade && count(self::$relation) == 0) {
888 self::buildRelationData();
891 // Is there a relation entry for the table?
892 if ($cascade && isset(self::$relation[$table])) {
893 // We only allow a simple "one field" relation.
894 $field = array_keys(self::$relation[$table])[0];
895 $rel_def = array_values(self::$relation[$table])[0];
897 // Create a key for preventing double queries
898 $qkey = $field . '-' . $table . ':' . implode(':', array_keys($conditions)) . ':' . implode(':', $conditions);
900 // When the search field is the relation field, we don't need to fetch the rows
901 // This is useful when the leading record is already deleted in the frontend but the rest is done in the backend
902 if ((count($conditions) == 1) && ($field == array_keys($conditions)[0])) {
903 foreach ($rel_def AS $rel_table => $rel_fields) {
904 foreach ($rel_fields AS $rel_field) {
905 $retval = self::delete($rel_table, [$rel_field => array_values($conditions)[0]], $options, true, $callstack);
906 $commands = array_merge($commands, $retval);
909 // We quit when this key already exists in the callstack.
910 } elseif (!isset($callstack[$qkey])) {
912 $callstack[$qkey] = true;
914 // Fetch all rows that are to be deleted
915 $data = self::select($table, [$field], $conditions);
917 while ($row = self::fetch($data)) {
918 // Now we accumulate the delete commands
919 $retval = self::delete($table, [$field => $row[$field]], $options, true, $callstack);
920 $commands = array_merge($commands, $retval);
925 // Since we had split the delete command we don't need the original command anymore
926 unset($commands[$key]);
931 // Now we finalize the process
932 $do_transaction = !self::$in_transaction;
934 if ($do_transaction) {
941 foreach ($commands AS $command) {
942 $conditions = $command['conditions'];
944 $first_key = key($conditions);
946 $condition_string = self::buildCondition($conditions);
948 if ((count($command['conditions']) > 1) || is_int($first_key)) {
949 $sql = "DELETE FROM `" . $command['table'] . "`" . $condition_string;
950 logger(self::replaceParameters($sql, $conditions), LOGGER_DATA);
952 if (!self::e($sql, $conditions)) {
953 if ($do_transaction) {
959 $key_table = $command['table'];
960 $key_condition = array_keys($command['conditions'])[0];
961 $value = array_values($command['conditions'])[0];
963 // Split the SQL queries in chunks of 100 values
964 // We do the $i stuff here to make the code better readable
965 $i = $counter[$key_table][$key_condition];
966 if (isset($compacted[$key_table][$key_condition][$i]) && count($compacted[$key_table][$key_condition][$i]) > 100) {
970 $compacted[$key_table][$key_condition][$i][$value] = $value;
971 $counter[$key_table][$key_condition] = $i;
974 foreach ($compacted AS $table => $values) {
975 foreach ($values AS $field => $field_value_list) {
976 foreach ($field_value_list AS $field_values) {
977 $sql = "DELETE FROM `" . $table . "` WHERE `" . $field . "` IN (" .
978 substr(str_repeat("?, ", count($field_values)), 0, -2) . ");";
980 logger(self::replaceParameters($sql, $field_values), LOGGER_DATA);
982 if (!self::e($sql, $field_values)) {
983 if ($do_transaction) {
991 if ($do_transaction) {
1001 * @brief Updates rows
1003 * Updates rows in the database. When $old_fields is set to an array,
1004 * the system will only do an update if the fields in that array changed.
1007 * Only the values in $old_fields are compared.
1008 * This is an intentional behaviour.
1011 * We include the timestamp field in $fields but not in $old_fields.
1012 * Then the row will only get the new timestamp when the other fields had changed.
1014 * When $old_fields is set to a boolean value the system will do this compare itself.
1015 * When $old_fields is set to "true" the system will do an insert if the row doesn't exists.
1018 * Only set $old_fields to a boolean value when you are sure that you will update a single row.
1019 * When you set $old_fields to "true" then $fields must contain all relevant fields!
1021 * @param string $table Table name
1022 * @param array $fields contains the fields that are updated
1023 * @param array $condition condition array with the key values
1024 * @param array|boolean $old_fields array with the old field values that are about to be replaced (true = update on duplicate)
1026 * @return boolean was the update successfull?
1028 public static function update($table, $fields, $condition, $old_fields = []) {
1030 if (empty($table) || empty($fields) || empty($condition)) {
1031 logger('Table, fields and condition have to be set');
1035 $table = self::escape($table);
1037 $condition_string = self::buildCondition($condition);
1039 if (is_bool($old_fields)) {
1040 $do_insert = $old_fields;
1042 $old_fields = self::selectFirst($table, [], $condition);
1044 if (is_bool($old_fields)) {
1046 $values = array_merge($condition, $fields);
1047 return self::insert($table, $values, $do_insert);
1053 $do_update = (count($old_fields) == 0);
1055 foreach ($old_fields AS $fieldname => $content) {
1056 if (isset($fields[$fieldname])) {
1057 if ($fields[$fieldname] == $content) {
1058 unset($fields[$fieldname]);
1065 if (!$do_update || (count($fields) == 0)) {
1069 $sql = "UPDATE `".$table."` SET `".
1070 implode("` = ?, `", array_keys($fields))."` = ?".$condition_string;
1072 $params1 = array_values($fields);
1073 $params2 = array_values($condition);
1074 $params = array_merge_recursive($params1, $params2);
1076 return self::e($sql, $params);
1080 * Retrieve a single record from a table and returns it in an associative array
1082 * @brief Retrieve a single record from a table
1083 * @param string $table
1084 * @param array $fields
1085 * @param array $condition
1086 * @param array $params
1087 * @return bool|array
1090 public static function selectFirst($table, array $fields = [], array $condition = [], $params = [])
1092 $params['limit'] = 1;
1093 $result = self::select($table, $fields, $condition, $params);
1095 if (is_bool($result)) {
1098 $row = self::fetch($result);
1099 self::close($result);
1105 * @brief Select rows from a table
1107 * @param string $table Table name
1108 * @param array $fields Array of selected fields, empty for all
1109 * @param array $condition Array of fields for condition
1110 * @param array $params Array of several parameters
1112 * @return boolean|object
1116 * $fields = array("id", "uri", "uid", "network");
1118 * $condition = array("uid" => 1, "network" => 'dspr');
1120 * $condition = array("`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr');
1122 * $params = array("order" => array("id", "received" => true), "limit" => 10);
1124 * $data = dba::select($table, $fields, $condition, $params);
1126 public static function select($table, array $fields = [], array $condition = [], array $params = [])
1132 $table = self::escape($table);
1134 if (count($fields) > 0) {
1135 $select_fields = "`" . implode("`, `", array_values($fields)) . "`";
1137 $select_fields = "*";
1140 $condition_string = self::buildCondition($condition);
1143 if (isset($params['order'])) {
1144 $order_string = " ORDER BY ";
1145 foreach ($params['order'] AS $fields => $order) {
1146 if (!is_int($fields)) {
1147 $order_string .= "`" . $fields . "` " . ($order ? "DESC" : "ASC") . ", ";
1149 $order_string .= "`" . $order . "`, ";
1152 $order_string = substr($order_string, 0, -2);
1156 if (isset($params['limit']) && is_int($params['limit'])) {
1157 $limit_string = " LIMIT " . $params['limit'];
1160 if (isset($params['limit']) && is_array($params['limit'])) {
1161 $limit_string = " LIMIT " . intval($params['limit'][0]) . ", " . intval($params['limit'][1]);
1164 $sql = "SELECT " . $select_fields . " FROM `" . $table . "`" . $condition_string . $order_string . $limit_string;
1166 $result = self::p($sql, $condition);
1172 * @brief Counts the rows from a table satisfying the provided condition
1174 * @param string $table Table name
1175 * @param array $condition array of fields for condition
1182 * $condition = ["uid" => 1, "network" => 'dspr'];
1184 * $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr'];
1186 * $count = dba::count($table, $condition);
1188 public static function count($table, array $condition = [])
1194 $condition_string = self::buildCondition($condition);
1196 $sql = "SELECT COUNT(*) AS `count` FROM `".$table."`".$condition_string;
1198 $row = self::fetch_first($sql, $condition);
1200 return $row['count'];
1204 * @brief Returns the SQL condition string built from the provided condition array
1206 * This function operates with two modes.
1207 * - Supplied with a filed/value associative array, it builds simple strict
1208 * equality conditions linked by AND.
1209 * - Supplied with a flat list, the first element is the condition string and
1210 * the following arguments are the values to be interpolated
1212 * $condition = ["uid" => 1, "network" => 'dspr'];
1214 * $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr'];
1216 * In either case, the provided array is left with the parameters only
1218 * @param array $condition
1221 private static function buildCondition(array &$condition = [])
1223 $condition_string = '';
1224 if (count($condition) > 0) {
1226 $first_key = key($condition);
1227 if (is_int($first_key)) {
1228 $condition_string = " WHERE ".array_shift($condition);
1231 $condition_string = "";
1232 foreach ($condition as $field => $value) {
1233 if ($condition_string != "") {
1234 $condition_string .= " AND ";
1236 if (is_array($value)) {
1237 $new_values = array_merge($new_values, array_values($value));
1238 $placeholders = substr(str_repeat("?, ", count($value)), 0, -2);
1239 $condition_string .= "`" . $field . "` IN (" . $placeholders . ")";
1241 $new_values[$field] = $value;
1242 $condition_string .= "`" . $field . "` = ?";
1245 $condition_string = " WHERE " . $condition_string;
1246 $condition = $new_values;
1250 return $condition_string;
1254 * @brief Fills an array with data from a query
1256 * @param object $stmt statement object
1257 * @return array Data array
1259 public static function inArray($stmt, $do_close = true) {
1260 if (is_bool($stmt)) {
1265 while ($row = self::fetch($stmt)) {
1275 * @brief Returns the error number of the last query
1277 * @return string Error number (0 if no error)
1279 public static function errorNo() {
1280 return self::$errorno;
1284 * @brief Returns the error message of the last query
1286 * @return string Error message ('' if no error)
1288 public static function errorMessage() {
1289 return self::$error;
1293 * @brief Closes the current statement
1295 * @param object $stmt statement object
1296 * @return boolean was the close successful?
1298 public static function close($stmt) {
1301 $stamp1 = microtime(true);
1303 if (!is_object($stmt)) {
1307 switch (self::$driver) {
1309 $ret = $stmt->closeCursor();
1312 $stmt->free_result();
1313 $ret = $stmt->close();
1317 $a->save_timestamp($stamp1, 'database');
1323 function dbesc($str) {
1324 if (dba::$connected) {
1325 return(dba::escape($str));
1327 return(str_replace("'","\\'",$str));
1332 * @brief execute SQL query with printf style args - deprecated
1334 * Please use the dba:: functions instead:
1335 * dba::select, dba::exists, dba::insert
1336 * dba::delete, dba::update, dba::p, dba::e
1338 * @param $args Query parameters (1 to N parameters of different types)
1339 * @return array|bool Query array
1342 $args = func_get_args();
1345 if (!dba::$connected) {
1349 $sql = dba::clean_query($sql);
1350 $sql = dba::any_value_fallback($sql);
1352 $stmt = @vsprintf($sql, $args);
1354 $ret = dba::p($stmt);
1356 if (is_bool($ret)) {
1360 $columns = dba::columnCount($ret);
1362 $data = dba::inArray($ret);
1364 if ((count($data) == 0) && ($columns == 0)) {
1371 function dba_timer() {
1372 return microtime(true);