]> git.mxchange.org Git - friendica.git/blobdiff - src/Database/Database.php
Merge branch '2023.09-rc' into move-mentionbutton-to-navbar
[friendica.git] / src / Database / Database.php
index f4959bf7e9028ea8818c569ac2f379f5a8e7fcfd..9366f1b31416e74e11d9ca6944fc3b1d229c1969 100644 (file)
@@ -80,6 +80,8 @@ class Database
        protected $dbaDefinition;
        /** @var ViewDefinition */
        protected $viewDefinition;
+       /** @var string|null */
+       private $currentTable;
 
        public function __construct(IManageConfigValues $config, DbaDefinition $dbaDefinition, ViewDefinition $viewDefinition)
        {
@@ -503,7 +505,7 @@ class Database
         */
        public function p(string $sql)
        {
-
+               $this->currentTable = null;
                $this->profiler->startRecording('database');
                $stamp1 = microtime(true);
 
@@ -750,8 +752,8 @@ class Database
                                @file_put_contents(
                                        $this->config->get('system', 'db_log'),
                                        DateTimeFormat::utcNow() . "\t" . $duration . "\t" .
-                                       basename($backtrace[1]["file"]) . "\t" .
-                                       $backtrace[1]["line"] . "\t" . $backtrace[2]["function"] . "\t" .
+                                       basename($backtrace[1]['file']) . "\t" .
+                                       $backtrace[1]['line'] . "\t" . $backtrace[2]['function'] . "\t" .
                                        substr($this->replaceParameters($sql, $args), 0, 4000) . "\n",
                                        FILE_APPEND
                                );
@@ -973,8 +975,8 @@ class Database
                switch ($this->driver) {
                        case self::PDO:
                                $columns = $stmt->fetch(PDO::FETCH_ASSOC);
-                               if (!empty($stmt->table) && is_array($columns)) {
-                                       $columns = $this->castFields($stmt->table, $columns);
+                               if (!empty($this->currentTable) && is_array($columns)) {
+                                       $columns = $this->castFields($this->currentTable, $columns);
                                }
                                break;
                        case self::MYSQLI:
@@ -1520,8 +1522,8 @@ class Database
 
                $result = $this->p($sql, $condition);
 
-               if (($this->driver == self::PDO) && !empty($result) && is_string($table)) {
-                       $result->table = $table;
+               if ($this->driver == self::PDO && !empty($result)) {
+                       $this->currentTable = $table;
                }
 
                return $result;
@@ -1781,6 +1783,24 @@ class Database
                return $this->e("OPTIMIZE TABLE " . DBA::buildTableString([$table])) !== false;
        }
 
+       /**
+        * Kill sleeping database processes
+        *
+        * @return void
+        */
+       public function deleteSleepingProcesses()
+       {
+               $processes = $this->p("SHOW FULL PROCESSLIST");
+               while ($process = $this->fetch($processes)) {
+                       if (($process['Command'] != 'Sleep') || ($process['Time'] < 300) || ($process['db'] != $this->databaseName())) {
+                               continue;
+                       }
+
+                       $this->e("KILL ?", $process['Id']);
+               }
+               $this->close($processes);
+       }
+
        /**
         * Fetch a database variable
         *