]> git.mxchange.org Git - friendica.git/blobdiff - src/Database/Database.php
Merge remote-tracking branch 'upstream/develop' into server-detection
[friendica.git] / src / Database / Database.php
index e5dec2d75e6b074b9da6802e9c9509b64ae2db8d..3276e90e5294bd9cc35f4871e0156381dd227362 100644 (file)
@@ -23,6 +23,8 @@ namespace Friendica\Database;
 
 use Friendica\Core\Config\ValueObject\Cache;
 use Friendica\Core\System;
+use Friendica\Database\Definition\DbaDefinition;
+use Friendica\Database\Definition\ViewDefinition;
 use Friendica\Network\HTTPException\ServiceUnavailableException;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Profiler;
@@ -73,20 +75,21 @@ class Database
        protected $in_retrial     = false;
        protected $testmode       = false;
        private $relation       = [];
+       /** @var DbaDefinition */
+       protected $dbaDefinition;
+       /** @var ViewDefinition */
+       protected $viewDefinition;
 
-       public function __construct(Cache $configCache, Profiler $profiler, LoggerInterface $logger)
+       public function __construct(Cache $configCache, Profiler $profiler, DbaDefinition $dbaDefinition, ViewDefinition $viewDefinition, LoggerInterface $logger)
        {
                // We are storing these values for being able to perform a reconnect
-               $this->configCache   = $configCache;
-               $this->profiler      = $profiler;
-               $this->logger        = $logger;
+               $this->configCache    = $configCache;
+               $this->profiler       = $profiler;
+               $this->logger         = $logger;
+               $this->dbaDefinition  = $dbaDefinition;
+               $this->viewDefinition = $viewDefinition;
 
                $this->connect();
-
-               if ($this->isConnected()) {
-                       // Loads DB_UPDATE_VERSION constant
-                       DBStructure::definition($configCache->get('system', 'basepath'), false);
-               }
        }
 
        /**
@@ -825,6 +828,7 @@ class Database
         *
         * @return boolean Are there rows for that condition?
         * @throws \Exception
+        * @todo Please unwrap the DBStructure::existsTable() call so this method has one behavior only: checking existence on records
         */
        public function exists(string $table, array $condition): bool
        {
@@ -1608,10 +1612,10 @@ class Database
 
                $types = [];
 
-               $tables = DBStructure::definition('', false);
+               $tables = $this->dbaDefinition->getAll();
                if (empty($tables[$table])) {
                        // When a matching table wasn't found we check if it is a view
-                       $views = View::definition('', false);
+                       $views = $this->viewDefinition->getAll();
                        if (empty($views[$table])) {
                                return $fields;
                        }
@@ -1719,29 +1723,29 @@ class Database
         */
        public function processlist(): array
        {
-               $ret  = $this->p("SHOW PROCESSLIST");
+               $ret  = $this->p('SHOW PROCESSLIST');
                $data = $this->toArray($ret);
 
                $processes = 0;
                $states    = [];
                foreach ($data as $process) {
-                       $state = trim($process["State"]);
+                       $state = trim($process['State']);
 
                        // Filter out all non blocking processes
-                       if (!in_array($state, ["", "init", "statistics", "updating"])) {
+                       if (!in_array($state, ['', 'init', 'statistics', 'updating'])) {
                                ++$states[$state];
                                ++$processes;
                        }
                }
 
-               $statelist = "";
+               $statelist = '';
                foreach ($states as $state => $usage) {
-                       if ($statelist != "") {
-                               $statelist .= ", ";
+                       if ($statelist != '') {
+                               $statelist .= ', ';
                        }
-                       $statelist .= $state . ": " . $usage;
+                       $statelist .= $state . ': ' . $usage;
                }
-               return (["list" => $statelist, "amount" => $processes]);
+               return (['list' => $statelist, 'amount' => $processes]);
        }
 
        /**
@@ -1761,7 +1765,6 @@ class Database
         * Checks if $array is a filled array with at least one entry.
         *
         * @param mixed $array A filled array with at least one entry
-        *
         * @return boolean Whether $array is a filled array or an object with rows
         */
        public function isResult($array): bool
@@ -1842,6 +1845,7 @@ class Database
                $upds = implode(', ', $upd);
 
                $r = $this->e(sprintf("UPDATE %s SET %s;", DBA::quoteIdentifier($table), $upds));
+
                if (!$this->isResult($r)) {
                        throw new \RuntimeException("Failed updating `$table`: " . $this->errorMessage());
                }