2 namespace Friendica\Database;
6 * @brief This class contain functions for the database management
8 * This class contains functions that doesn't need to know if pdo, mysqli or whatever is used.
12 * @brief Return a list of database processes
15 * 'list' => List of processes, separated in their different states
16 * 'amount' => Number of concurrent database processes
18 public static function processlist() {
19 $r = q("SHOW PROCESSLIST");
24 foreach ($r AS $process) {
25 $state = trim($process["State"]);
27 // Filter out all non blocking processes
28 if (!in_array($state, array("", "init", "statistics", "updating"))) {
35 foreach ($states AS $state => $usage) {
38 $statelist .= $state.": ".$usage;
40 return(array("list" => $statelist, "amount" => $processes));
44 * Checks if $array is a filled array with at least one entry.
46 * @param $array mixed A filled array with at least one entry
47 * @return Whether $array is a filled array or an object with rows
49 public static function is_result($array) {
50 // It could be a return value from an update statement
51 if (is_bool($array)) {
55 if (is_object($array)) {
56 return dba::num_rows($array) > 0;
59 return (is_array($array) && (count($array) > 0));
63 * @brief Callback function for "esc_array"
65 * @param mixed $value Array value
66 * @param string $key Array key
67 * @param boolean $add_quotation add quotation marks for string values
69 private static function esc_array_callback(&$value, $key, $add_quotation) {
71 if (!$add_quotation) {
72 if (is_bool($value)) {
73 $value = ($value ? '1' : '0');
75 $value = dbesc($value);
80 if (is_bool($value)) {
81 $value = ($value ? 'true' : 'false');
82 } elseif (is_float($value) || is_integer($value)) {
83 $value = (string)$value;
85 $value = "'".dbesc($value)."'";
90 * @brief Escapes a whole array
92 * @param mixed $arr Array with values to be escaped
93 * @param boolean $add_quotation add quotation marks for string values
95 public static function esc_array(&$arr, $add_quotation = false) {
96 array_walk($arr, 'self::esc_array_callback', $add_quotation);
100 * Checks Converts any date string into a SQL compatible date string
102 * @param string $date a date string in any format
103 * @return string SQL style date string
105 public static function date($date = 'now') {
106 $timestamp = strtotime($date);
108 // Don't allow lower date strings as '0001-01-01 00:00:00'
109 if ($timestamp < -62135596800) {
110 $timestamp = -62135596800;
113 return date('Y-m-d H:i:s', (int)$timestamp);