<?php
+/**
+ * @copyright Copyright (C) 2020, Friendica
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
namespace Friendica\Database;
-use Friendica\Core\Config\Cache\ConfigCache;
+use Exception;
+use Friendica\Core\Config\Cache;
use Friendica\Core\System;
+use Friendica\DI;
use Friendica\Network\HTTPException\InternalServerErrorException;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Profiler;
use Psr\Log\LoggerInterface;
/**
- * @class MySQL database class
- *
* This class is for the low level database stuff that does driver specific things.
*/
class Database
protected $connected = false;
/**
- * @var ConfigCache
+ * @var Cache
*/
protected $configCache;
/**
/** @var PDO|mysqli */
protected $connection;
protected $driver;
+ private $emulate_prepares = false;
private $error = false;
private $errorno = 0;
private $affected_rows = 0;
protected $in_transaction = false;
protected $in_retrial = false;
+ protected $testmode = false;
private $relation = [];
- public function __construct(ConfigCache $configCache, Profiler $profiler, LoggerInterface $logger, array $server = [])
+ public function __construct(Cache $configCache, Profiler $profiler, LoggerInterface $logger, array $server = [])
{
// We are storing these values for being able to perform a reconnect
$this->configCache = $configCache;
return false;
}
- if (class_exists('\PDO') && in_array('mysql', PDO::getAvailableDrivers())) {
+ $this->emulate_prepares = (bool)$this->configCache->get('database', 'emulate_prepares');
+ $this->pdo_emulate_prepares = (bool)$this->configCache->get('database', 'pdo_emulate_prepares');
+
+ if (!$this->configCache->get('database', 'disable_pdo') && class_exists('\PDO') && in_array('mysql', PDO::getAvailableDrivers())) {
$this->driver = 'pdo';
$connect = "mysql:host=" . $server . ";dbname=" . $db;
try {
$this->connection = @new PDO($connect, $user, $pass);
- $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
+ $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->pdo_emulate_prepares);
$this->connected = true;
} catch (PDOException $e) {
$this->connected = false;
return $this->connected;
}
+ public function setTestmode(bool $test)
+ {
+ $this->testmode = $test;
+ }
/**
* Sets the logger for DBA
*
}
$watchlist = explode(',', $this->configCache->get('system', 'db_log_index_watch'));
- $blacklist = explode(',', $this->configCache->get('system', 'db_log_index_blacklist'));
+ $denylist = explode(',', $this->configCache->get('system', 'db_log_index_denylist'));
while ($row = $this->fetch($r)) {
if ((intval($this->configCache->get('system', 'db_loglimit_index')) > 0)) {
$log = true;
}
- if (in_array($row['key'], $blacklist) || ($row['key'] == "")) {
+ if (in_array($row['key'], $denylist) || ($row['key'] == "")) {
$log = false;
}
$row['key'] . "\t" . $row['rows'] . "\t" . $row['Extra'] . "\t" .
basename($backtrace[1]["file"]) . "\t" .
$backtrace[1]["line"] . "\t" . $backtrace[2]["function"] . "\t" .
- substr($query, 0, 2000) . "\n", FILE_APPEND);
+ substr($query, 0, 4000) . "\n", FILE_APPEND);
}
}
}
/**
- * Removes every not whitelisted character from the identifier string
+ * Removes every not allowlisted character from the identifier string
*
* @param string $identifier
*
{
$offset = 0;
foreach ($args AS $param => $value) {
- if (is_int($args[$param]) || is_float($args[$param])) {
+ if (is_int($args[$param]) || is_float($args[$param]) || is_bool($args[$param])) {
$replace = intval($args[$param]);
+ } elseif (is_null($args[$param])) {
+ $replace = 'NULL';
} else {
$replace = "'" . $this->escape($args[$param]) . "'";
}
/**
* Executes a prepared statement that returns data
+ *
* @usage Example: $r = p("SELECT * FROM `item` WHERE `guid` = ?", $guid);
*
* Please only use it with complicated queries.
$sql = "/*" . System::callstack() . " */ " . $sql;
}
+ $is_error = false;
$this->error = '';
$this->errorno = 0;
$this->affected_rows = 0;
switch ($this->driver) {
case 'pdo':
// If there are no arguments we use "query"
- if (count($args) == 0) {
- if (!$retval = $this->connection->query($sql)) {
+ if ($this->emulate_prepares || count($args) == 0) {
+ if (!$retval = $this->connection->query($this->replaceParameters($sql, $args))) {
$errorInfo = $this->connection->errorInfo();
$this->error = $errorInfo[2];
$this->errorno = $errorInfo[1];
$retval = false;
+ $is_error = true;
break;
}
$this->affected_rows = $retval->rowCount();
$this->error = $errorInfo[2];
$this->errorno = $errorInfo[1];
$retval = false;
+ $is_error = true;
break;
}
$this->error = $errorInfo[2];
$this->errorno = $errorInfo[1];
$retval = false;
+ $is_error = true;
} else {
$retval = $stmt;
$this->affected_rows = $retval->rowCount();
$can_be_prepared = in_array($command, ['select', 'update', 'insert', 'delete']);
// The fallback routine is called as well when there are no arguments
- if (!$can_be_prepared || (count($args) == 0)) {
+ if ($this->emulate_prepares || !$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;
$retval = false;
+ $is_error = true;
} else {
if (isset($retval->num_rows)) {
$this->affected_rows = $retval->num_rows;
$this->error = $stmt->error;
$this->errorno = $stmt->errno;
$retval = false;
+ $is_error = true;
break;
}
$this->error = $this->connection->error;
$this->errorno = $this->connection->errno;
$retval = false;
+ $is_error = true;
} else {
$stmt->store_result();
$retval = $stmt;
break;
}
+ // See issue https://github.com/friendica/friendica/issues/8572
+ // Ensure that we always get an error message on an error.
+ if ($is_error && empty($this->errorno)) {
+ $this->errorno = -1;
+ }
+
+ if ($is_error && empty($this->error)) {
+ $this->error = 'Unknown database error';
+ }
+
// We are having an own error logging in the function "e"
if (($this->errorno != 0) && !$called_from_e) {
// We have to preserve the error code, somewhere in the logging it get lost
$error = $this->error;
$errorno = $this->errorno;
+ if ($this->testmode) {
+ throw new Exception(DI::l10n()->t('Database error %d "%s" at "%s"', $errorno, $error, $this->replaceParameters($sql, $args)));
+ }
+
$this->logger->error('DB Error', [
- 'code' => $this->errorno,
- 'error' => $this->error,
+ 'code' => $errorno,
+ 'error' => $error,
'callstack' => System::callstack(8),
'params' => $this->replaceParameters($sql, $args),
]);
// It doesn't make sense to continue when the database connection was lost
if ($this->in_retrial) {
$this->logger->notice('Giving up retrial because of database error', [
- 'code' => $this->errorno,
- 'error' => $this->error,
+ 'code' => $errorno,
+ 'error' => $error,
]);
} else {
$this->logger->notice('Couldn\'t reconnect after database error', [
- 'code' => $this->errorno,
- 'error' => $this->error,
+ 'code' => $errorno,
+ 'error' => $error,
]);
}
exit(1);
} else {
// We try it again
$this->logger->notice('Reconnected after database error', [
- 'code' => $this->errorno,
- 'error' => $this->error,
+ 'code' => $errorno,
+ 'error' => $error,
]);
$this->in_retrial = true;
$ret = $this->p($sql, $args);
$this->errorno = $errorno;
}
- $this->profiler->saveTimestamp($stamp1, 'database', System::callstack());
+ $this->profiler->saveTimestamp($stamp1, 'database');
if ($this->configCache->get('system', 'db_log')) {
$stamp2 = microtime(true);
@file_put_contents($this->configCache->get('system', 'db_log'), DateTimeFormat::utcNow() . "\t" . $duration . "\t" .
basename($backtrace[1]["file"]) . "\t" .
$backtrace[1]["line"] . "\t" . $backtrace[2]["function"] . "\t" .
- substr($this->replaceParameters($sql, $args), 0, 2000) . "\n", FILE_APPEND);
+ substr($this->replaceParameters($sql, $args), 0, 4000) . "\n", FILE_APPEND);
}
}
return $retval;
$error = $this->error;
$errorno = $this->errorno;
+ if ($this->testmode) {
+ throw new Exception(DI::l10n()->t('Database error %d "%s" at "%s"', $errorno, $error, $this->replaceParameters($sql, $params)));
+ }
+
$this->logger->error('DB Error', [
- 'code' => $this->errorno,
- 'error' => $this->error,
+ 'code' => $errorno,
+ 'error' => $error,
'callstack' => System::callstack(8),
'params' => $this->replaceParameters($sql, $params),
]);
// A reconnect like in $this->p could be dangerous with modifications
if ($errorno == 2006) {
$this->logger->notice('Giving up because of database error', [
- 'code' => $this->errorno,
- 'error' => $this->error,
+ 'code' => $errorno,
+ 'error' => $error,
]);
exit(1);
}
$this->errorno = $errorno;
}
- $this->profiler->saveTimestamp($stamp, "database_write", System::callstack());
+ $this->profiler->saveTimestamp($stamp, "database_write");
return $retval;
}
}
}
- $this->profiler->saveTimestamp($stamp1, 'database', System::callstack());
+ $this->profiler->saveTimestamp($stamp1, 'database');
return $columns;
}
* @return boolean was the insert successful?
* @throws \Exception
*/
- public function insert($table, $param, $on_duplicate_update = false)
+ public function insert($table, array $param, bool $on_duplicate_update = false)
{
if (empty($table) || empty($param)) {
$this->logger->info('Table and fields have to be set');
$success = $this->e("LOCK TABLES " . DBA::buildTableString($table) . " WRITE");
if ($this->driver == 'pdo') {
- $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
+ $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->pdo_emulate_prepares);
}
if (!$success) {
$success = $this->e("UNLOCK TABLES");
if ($this->driver == 'pdo') {
- $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
+ $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->pdo_emulate_prepares);
$this->e("SET autocommit=1");
} else {
$this->connection->autocommit(true);
if ((count($command['conditions']) > 1) || is_int($first_key)) {
$sql = "DELETE FROM " . DBA::quoteIdentifier($command['table']) . " " . $condition_string;
- $this->logger->debug($this->replaceParameters($sql, $conditions));
+ $this->logger->info($this->replaceParameters($sql, $conditions), ['callstack' => System::callstack(6), 'internal_callstack' => $callstack]);
if (!$this->e($sql, $conditions)) {
if ($do_transaction) {
$sql = "DELETE FROM " . DBA::quoteIdentifier($table) . " WHERE " . DBA::quoteIdentifier($field) . " IN (" .
substr(str_repeat("?, ", count($field_values)), 0, -2) . ");";
- $this->logger->debug($this->replaceParameters($sql, $field_values));
+ $this->logger->info($this->replaceParameters($sql, $field_values), ['callstack' => System::callstack(6), 'internal_callstack' => $callstack]);
if (!$this->e($sql, $field_values)) {
if ($do_transaction) {
}
}
- $do_update = (count($old_fields) == 0);
-
foreach ($old_fields AS $fieldname => $content) {
- if (isset($fields[$fieldname])) {
- if (($fields[$fieldname] == $content) && !is_null($content)) {
- unset($fields[$fieldname]);
- } else {
- $do_update = true;
- }
+ if (isset($fields[$fieldname]) && !is_null($content) && ($fields[$fieldname] == $content)) {
+ unset($fields[$fieldname]);
}
}
- if (!$do_update || (count($fields) == 0)) {
+ if (count($fields) == 0) {
return true;
}
/**
* Select rows from a table
*
- * @param string|array $table Table name or array [schema => table]
- * @param array $fields Array of selected fields, empty for all
- * @param array $condition Array of fields for condition
- * @param array $params Array of several parameters
- *
- * @return boolean|object
*
* Example:
- * $table = "item";
- * $fields = array("id", "uri", "uid", "network");
+ * $table = 'item';
+ * or:
+ * $table = ['schema' => 'table'];
+ * @see DBA::buildTableString()
*
- * $condition = array("uid" => 1, "network" => 'dspr');
+ * $fields = ['id', 'uri', 'uid', 'network'];
+ *
+ * $condition = ['uid' => 1, 'network' => 'dspr', 'blocked' => true];
* or:
- * $condition = array("`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr');
+ * $condition = ['`uid` = ? AND `network` IN (?, ?)', 1, 'dfrn', 'dspr'];
+ * @see DBA::buildCondition()
*
- * $params = array("order" => array("id", "received" => true), "limit" => 10);
+ * $params = ['order' => ['id', 'received' => true, 'created' => 'ASC'), 'limit' => 10];
+ * @see DBA::buildParameter()
*
* $data = DBA::select($table, $fields, $condition, $params);
+ *
+ * @param string|array $table Table name or array [schema => table]
+ * @param array $fields Array of selected fields, empty for all
+ * @param array $condition Array of fields for condition
+ * @param array $params Array of several parameters
+ * @return boolean|object
* @throws \Exception
*/
public function select($table, array $fields = [], array $condition = [], array $params = [])
break;
}
- $this->profiler->saveTimestamp($stamp1, 'database', System::callstack());
+ $this->profiler->saveTimestamp($stamp1, 'database');
return $ret;
}
return (["list" => $statelist, "amount" => $processes]);
}
+ /**
+ * Fetch a database variable
+ *
+ * @param string $name
+ * @return string content
+ */
+ public function getVariable(string $name)
+ {
+ $result = $this->fetchFirst("SHOW GLOBAL VARIABLES WHERE `Variable_name` = ?", $name);
+ return $result['Value'] ?? null;
+ }
+
/**
* Checks if $array is a filled array with at least one entry.
*