3 * @file src/Database/DBM.php
5 namespace Friendica\Database;
10 * @brief This class contain functions for the database management
12 * This class contains functions that doesn't need to know if pdo, mysqli or whatever is used.
17 * @brief Return a list of database processes
20 * 'list' => List of processes, separated in their different states
21 * 'amount' => Number of concurrent database processes
23 public static function processlist()
25 $r = q("SHOW PROCESSLIST");
30 foreach ($r as $process) {
31 $state = trim($process["State"]);
33 // Filter out all non blocking processes
34 if (!in_array($state, array("", "init", "statistics", "updating"))) {
41 foreach ($states as $state => $usage) {
42 if ($statelist != "") {
45 $statelist .= $state.": ".$usage;
47 return(array("list" => $statelist, "amount" => $processes));
51 * Checks if $array is a filled array with at least one entry.
53 * @param mixed $array A filled array with at least one entry
55 * @return boolean Whether $array is a filled array or an object with rows
57 public static function is_result($array)
59 // It could be a return value from an update statement
60 if (is_bool($array)) {
64 if (is_object($array)) {
65 return dba::num_rows($array) > 0;
68 return (is_array($array) && (count($array) > 0));
72 * @brief Callback function for "esc_array"
74 * @param mixed $value Array value
75 * @param string $key Array key
76 * @param boolean $add_quotation add quotation marks for string values
79 private static function esc_array_callback(&$value, $key, $add_quotation)
81 if (!$add_quotation) {
82 if (is_bool($value)) {
83 $value = ($value ? '1' : '0');
85 $value = dbesc($value);
90 if (is_bool($value)) {
91 $value = ($value ? 'true' : 'false');
92 } elseif (is_float($value) || is_integer($value)) {
93 $value = (string)$value;
95 $value = "'".dbesc($value)."'";
100 * @brief Escapes a whole array
102 * @param mixed $arr Array with values to be escaped
103 * @param boolean $add_quotation add quotation marks for string values
106 public static function esc_array(&$arr, $add_quotation = false)
108 array_walk($arr, 'self::esc_array_callback', $add_quotation);
112 * Checks Converts any date string into a SQL compatible date string
114 * @param string $date a date string in any format
116 * @return string SQL style date string
118 public static function date($date = 'now')
120 $timestamp = strtotime($date);
122 // Don't allow lower date strings as '0001-01-01 00:00:00'
123 if ($timestamp < -62135596800) {
124 $timestamp = -62135596800;
127 return date('Y-m-d H:i:s', (int)$timestamp);