X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FDatabase%2FDatabase.php;h=fde5bd9f10133b20fd63f8bc4a12add3a8e77d99;hb=ec8377a8c7b519dfbd0a5f18a94f52381b7e9340;hp=1c9e24adef10a748edc4c8f5ea5dd0be5c4d21bb;hpb=1c57ea7f7513963891d67cc81259ed62ef8efa58;p=friendica.git diff --git a/src/Database/Database.php b/src/Database/Database.php index 1c9e24adef..fde5bd9f10 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -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; @@ -66,27 +68,28 @@ class Database protected $connection; protected $driver = ''; protected $pdo_emulate_prepares = false; - private $error = false; + private $error = ''; private $errorno = 0; private $affected_rows = 0; protected $in_transaction = false; 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); - } } /** @@ -555,8 +558,8 @@ class Database if (count($args) == 0) { if (!$retval = $this->connection->query($this->replaceParameters($sql, $args))) { $errorInfo = $this->connection->errorInfo(); - $this->error = $errorInfo[2]; - $this->errorno = (int) $errorInfo[1]; + $this->error = (string)$errorInfo[2]; + $this->errorno = (int)$errorInfo[1]; $retval = false; $is_error = true; break; @@ -568,8 +571,8 @@ class Database /** @var $stmt mysqli_stmt|PDOStatement */ if (!$stmt = $this->connection->prepare($sql)) { $errorInfo = $this->connection->errorInfo(); - $this->error = $errorInfo[2]; - $this->errorno = (int) $errorInfo[1]; + $this->error = (string)$errorInfo[2]; + $this->errorno = (int)$errorInfo[1]; $retval = false; $is_error = true; break; @@ -588,8 +591,8 @@ class Database if (!$stmt->execute()) { $errorInfo = $stmt->errorInfo(); - $this->error = $errorInfo[2]; - $this->errorno = (int) $errorInfo[1]; + $this->error = (string)$errorInfo[2]; + $this->errorno = (int)$errorInfo[1]; $retval = false; $is_error = true; } else { @@ -607,8 +610,8 @@ class Database if (!$can_be_prepared || (count($args) == 0)) { $retval = $this->connection->query($this->replaceParameters($sql, $args)); if ($this->connection->errno) { - $this->error = $this->connection->error; - $this->errorno = $this->connection->errno; + $this->error = (string)$this->connection->error; + $this->errorno = (int)$this->connection->errno; $retval = false; $is_error = true; } else { @@ -624,8 +627,8 @@ class Database $stmt = $this->connection->stmt_init(); if (!$stmt->prepare($sql)) { - $this->error = $stmt->error; - $this->errorno = $stmt->errno; + $this->error = (string)$stmt->error; + $this->errorno = (int)$stmt->errno; $retval = false; $is_error = true; break; @@ -655,8 +658,8 @@ class Database } if (!$stmt->execute()) { - $this->error = $this->connection->error; - $this->errorno = $this->connection->errno; + $this->error = (string)$this->connection->error; + $this->errorno = (int)$this->connection->errno; $retval = false; $is_error = true; } else { @@ -723,8 +726,8 @@ class Database } } - $this->error = $error; - $this->errorno = (int) $errorno; + $this->error = (string)$error; + $this->errorno = (int)$errorno; } $this->profiler->stopRecording(); @@ -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 { @@ -1434,7 +1438,7 @@ class Database array_walk($fields, function(&$value, $key) use ($options) { $field = $value; - $value = '`' . str_replace('`', '``', $value) . '`'; + $value = DBA::quoteIdentifier($field); if (!empty($options['group_by']) && !in_array($field, $options['group_by'])) { $value = 'ANY_VALUE(' . $value . ') AS ' . $value; @@ -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,38 +1723,39 @@ 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]); } /** * Fetch a database variable * * @param string $name - * @return string content + * @return string|null content or null if inexistent + * @throws \Exception */ - public function getVariable(string $name): string + public function getVariable(string $name) { $result = $this->fetchFirst("SHOW GLOBAL VARIABLES WHERE `Variable_name` = ?", $name); return $result['Value'] ?? null; @@ -1760,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 @@ -1841,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()); }