WIP DBA Refactoring

This commit is contained in:
Philipp Holzer 2019-04-10 23:49:41 +02:00
parent b4a6e068a9
commit 93aa585b51
No known key found for this signature in database
GPG Key ID: 517BE60E2CE5C8A5
11 changed files with 1545 additions and 1239 deletions

View File

@ -53,7 +53,9 @@
"npm-asset/moment": "^2.20.1",
"npm-asset/fullcalendar": "^3.0.1",
"npm-asset/cropperjs": "1.2.2",
"npm-asset/imagesloaded": "4.1.4"
"npm-asset/imagesloaded": "4.1.4",
"ext-mysqli": "^7.0",
"ext-PDO": "^7.0"
},
"repositories": [
{

View File

@ -0,0 +1,60 @@
<?php
namespace Friendica\Database\Connection;
abstract class AbstractConnection implements IConnection
{
/**
* The connection state of the database
* @var bool
*/
protected $isConnected;
protected $dbUser;
protected $dbPass;
protected $dbName;
protected $dbHost;
protected $dbPort;
protected $dbCharset;
protected $serverInfo;
public function __construct($serverAddress, $user, $pass, $db, $charset = null)
{
$this->isConnected = false;
$this->dbHost = $serverAddress;
$this->dbUser = $user;
$this->dbPass = $pass;
$this->dbName = $db;
$this->dbCharset = $charset;
$serverAddress = trim($this->dbHost);
$serverAddressData = explode(':', $serverAddress);
$server = $serverAddressData[0];
if (count($serverAddressData) > 1) {
$this->dbPort = trim($serverAddressData[1]);
} else {
$this->dbPort = 0;
}
$this->dbHost = trim($server);
$this->dbUser = trim($this->dbUser);
$this->dbPass = trim($this->dbPass);
$this->dbName = trim($this->dbName);
$this->dbCharset = trim($this->dbCharset);
$this->serverInfo = '';
}
/**
* {@inheritDoc}
*/
public function escape($sql)
{
// fallback, if no explicit escaping is set for a connection
return str_replace("'", "\\'", $sql);
}
}

View File

@ -0,0 +1,13 @@
<?php
namespace Friendica\Database\Connection;
use Exception;
/**
* A DB connection exception
*/
class ConnectionException extends Exception
{
}

View File

@ -0,0 +1,114 @@
<?php
namespace Friendica\Database\Connection;
interface IConnection
{
/**
* Connecting to the current database
*
* @return bool True, if connection was successful
*/
function connect();
/**
* Returns true if the database is connected
*
* @param bool $force Force a database in-deep check
*
* @return bool
*/
function isConnected($force = false);
/**
* Disconnects the current database connection
*/
function disconnect();
/**
* Perform a reconnect of an existing database connection
*
* @return bool Wsa the reconnect successful?
*/
function reconnect();
/**
* Returns the server version string
*
* @return string
*/
function getServerInfo();
/**
* Escapes a given existing statement based on the SQL connection
*
* @param string $sql
*
* @return string
*/
function escape($sql);
/**
* Closes the current statement
*
* @param object $stmt
*
* @return bool
*/
function closeStatement($stmt);
/**
* Commits the executed statements into the current connected database
*
* @return bool
*/
function performCommit();
/**
* Starts a transaction for the current connected database
*
* @return bool
*/
function startTransaction();
/**
* Does a rollback of the current transaction
*
* @return bool
*/
function rollbackTransaction();
/**
* Fetches a row of the current cursor
*
* @param object $stmt
*
* @return array
*/
function fetch($stmt);
/**
* Executes a given SQL in context of the current connected database
*
* @param string $sql
* @param array $args
*
* @return object
*
* @throws ConnectionException In case the execution doesn't work
*/
function executePrepared($sql, array $args = []);
/**
* Returns the number of affected rows for a given statement
*
* @param object $stmt
*
* @return int
*
* @throws ConnectionException In case the statement isn't valid
*/
function getNumRows($stmt);
}

View File

@ -0,0 +1,267 @@
<?php
namespace Friendica\Database\Connection;
use Friendica\Database\Database;
use mysqli;
use mysqli_result;
use mysqli_stmt;
class MysqlIConnection extends AbstractConnection implements IConnection
{
/**
* The connection to the database
* @var mysqli?
*/
private $connection = null;
/**
* {@inheritDoc}
*/
public function connect()
{
if ($this->isConnected()) {
return true;
}
if ($this->dbPort > 0) {
$this->connection = @new mysqli($this->dbHost, $this->dbUser, $this->dbPass, $this->dbName, $this->dbPort);
} else {
$this->connection = @new mysqli($this->dbHost, $this->dbUser, $this->dbPass, $this->dbName);
}
if (!mysqli_connect_errno()) {
$this->isConnected = true;
if ($this->dbCharset) {
$this->connection->set_charset($this->dbCharset);
}
return true;
} else {
return false;
}
}
/**
* {@inheritDoc}
*/
function isConnected($force = false)
{
if (!$force) {
return $this->isConnected;
}
if (!isset($this->connection)) {
return false;
}
return $this->connection->ping();
}
/**
* {@inheritDoc}
*/
function disconnect()
{
if (!isset($this->connection)) {
return;
}
$this->connection->close();
$this->connection = null;
}
/**
* {@inheritDoc}
*/
public function reconnect()
{
$this->disconnect();
return $this->connect();
}
/**
* {@inheritDoc}
*/
public function getServerInfo()
{
$this->serverInfo = $this->connection->server_info;
}
/**
* {@inheritDoc}
*/
public function escape($sql)
{
return @$this->connection->real_escape_string($sql);
}
/**
* {@inheritDoc}
* @param mysqli_stmt|mysqli_result
*/
public function closeStatement($stmt)
{
// MySQLi offers both a mysqli_stmt and a mysqli_result class.
// We should be careful not to assume the object type of $stmt
// because DBA::p() has been able to return both types.
if ($stmt instanceof mysqli_stmt) {
$stmt->free_result();
return $stmt->close();
} elseif ($stmt instanceof mysqli_result) {
$stmt->free();
return true;
} else {
return false;
}
}
/**
* {@inheritDoc}
*/
public function performCommit()
{
return $this->connection->commit();
}
/**
* {@inheritDoc}
*/
public function startTransaction()
{
return $this->connection->begin_transaction();
}
/**
* {@inheritDoc}
*
* @param mysqli_stmt|mysqli_result
*
* @throws ConnectionException In case of a wrong statement
*/
public function fetch($stmt)
{
if ($stmt instanceof mysqli_result) {
return $stmt->fetch_assoc();
}
if ($stmt instanceof mysqli_stmt) {
// This code works, but is slow
// Bind the result to a result array
$cols = [];
$cols_num = [];
for ($x = 0; $x < $stmt->field_count; $x++) {
$cols[] = &$cols_num[$x];
}
call_user_func_array([$stmt, 'bind_result'], $cols);
if (!$stmt->fetch()) {
return false;
}
// The slow part:
// We need to get the field names for the array keys
// It seems that there is no better way to do this.
$result = $stmt->result_metadata();
$fields = $result->fetch_fields();
foreach ($cols_num AS $param => $col) {
$columns[$fields[$param]->name] = $col;
}
}
throw new ConnectionException('Wrong statement for this connection');
}
/**
* {@inheritDoc}
*/
public function executePrepared($sql, array $args = [])
{
// There are SQL statements that cannot be executed with a prepared statement
$parts = explode(' ', $sql);
$command = strtolower($parts[0]);
$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)) {
$retval = $this->connection->query(Database::replaceParameters($sql, $args));
if ($this->connection->errno) {
throw new ConnectionException($this->connection->error, $this->connection->errno);
}
return $retval;
}
$stmt = $this->connection->stmt_init();
if (!$stmt->prepare($sql)) {
throw new ConnectionException($this->connection->error, $this->connection->errno);
}
$param_types = '';
$values = [];
foreach ($args AS $param => $value) {
if (is_int($args[$param])) {
$param_types .= 'i';
} elseif (is_float($args[$param])) {
$param_types .= 'd';
} elseif (is_string($args[$param])) {
$param_types .= 's';
} else {
$param_types .= 'b';
}
$values[] = &$args[$param];
}
if (count($values) > 0) {
array_unshift($values, $param_types);
call_user_func_array([$stmt, 'bind_param'], $values);
}
if (!$stmt->execute()) {
throw new ConnectionException($this->connection->error, $this->connection->errno);
} else {
$stmt->store_result();
return $stmt;
}
}
/**
* {@inheritDoc}
*
* @param mysqli_stmt|mysqli_result
*/
public function getNumRows($stmt)
{
if ($stmt instanceof mysqli_stmt) {
if (!isset($stmt->num_rows)) {
return $this->connection->affected_rows;
} else {
return $stmt->num_rows;
}
} elseif ($stmt instanceof mysqli_result) {
if (!isset($stmt->num_rows)) {
return $this->connection->affected_rows;
} else {
return $stmt->num_rows;
}
} else {
throw new ConnectionException('Wrong statement for this connection');
}
}
/**
* {@inheritDoc}
*/
public function rollbackTransaction()
{
return $this->connection->rollback();
}
}

View File

@ -0,0 +1,200 @@
<?php
namespace Friendica\Database\Connection;
use PDO;
use PDOStatement;
class PDOConnection extends AbstractConnection implements IConnection
{
/**
* The connection to the database
*
* @var PDO?
*/
private $connection = null;
/**
* {@inheritDoc}
*/
public function connect()
{
if ($this->isConnected()) {
return true;
}
$connect = "mysql:host=" . $this->dbHost . ";dbname=" . $this->dbName;
if ($this->dbPort > 0) {
$connect .= ";port=" . $this->dbPort;
}
if ($this->dbCharset) {
$connect .= ";charset=" . $this->dbCharset;
}
try {
$this->connection = @new PDO($connect, $this->dbUser, $this->dbPass);
$this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$this->isConnected = true;
return true;
} catch (\PDOException $e) {
/// @TODO At least log exception, don't ignore it!
return false;
}
}
/**
* {@inheritDoc}
*/
function isConnected($force = false)
{
if (!$force) {
return $this->isConnected;
}
if (!isset($this->connection)) {
return false;
}
return $this->reconnect();
}
/**
* {@inheritDoc}
*/
function disconnect()
{
if (!isset($this->connection)) {
return;
}
$this->connection = null;
}
/**
* {@inheritDoc}
*/
public function reconnect()
{
$this->disconnect();
return $this->connect();
}
/**
* {@inheritDoc}
*/
function getServerInfo()
{
$this->serverInfo = $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION);
}
/**
* {@inheritDoc}
*/
public function escape($sql)
{
return substr(@$this->connection->quote($sql, PDO::PARAM_STR), 1, -1);
}
/**
* {@inheritDoc}
* @param PDOStatement $stmt
*/
public function closeStatement($stmt)
{
return $stmt->closeCursor();
}
/**
* {@inheritDoc}
*/
public function performCommit()
{
if (!$this->connection->inTransaction()) {
return true;
}
return $this->connection->commit();
}
/**
* {@inheritDoc}
*/
public function startTransaction()
{
!$this->connection->inTransaction() && !$this->connection->beginTransaction();
}
/**
* {@inheritDoc}
*
* @param PDOStatement $stmt
*/
public function fetch($stmt)
{
return $stmt->fetch(PDO::FETCH_ASSOC);
}
/**
* {@inheritDoc}
*/
public function executePrepared($sql, array $args = [])
{
// If there are no arguments we use "query"
if (count($args) == 0) {
if (!$retval = $this->connection->query($sql)) {
$errorInfo = $this->connection->errorInfo();
throw new ConnectionException($errorInfo[2], $errorInfo[1]);
}
return $retval;
}
if (!$stmt = $this->connection->prepare($sql)) {
$errorInfo = $this->connection->errorInfo();
throw new ConnectionException($errorInfo[2], $errorInfo[1]);
}
foreach ($args AS $param => $value) {
if (is_int($args[$param])) {
$data_type = PDO::PARAM_INT;
} else {
$data_type = PDO::PARAM_STR;
}
$stmt->bindParam($param, $args[$param], $data_type);
}
if (!$stmt->execute()) {
$errorInfo = $stmt->errorInfo();
throw new ConnectionException($errorInfo[2], $errorInfo[1]);
} else {
return $stmt;
}
}
/**
* {@inheritDoc}
*
* @param PDOStatement
*/
public function getNumRows($stmt)
{
if ($stmt instanceof PDOStatement) {
return $stmt->rowCount();
} else {
throw new ConnectionException('Wrong statement for this connection');
}
}
/**
* {@inheritDoc}
*/
public function rollbackTransaction()
{
if (!$this->connection->inTransaction()) {
return true;
}
return $this->connection->rollBack();
}
}

File diff suppressed because it is too large Load Diff

790
src/Database/Database.php Normal file
View File

@ -0,0 +1,790 @@
<?php
namespace Friendica\Database;
use Friendica\Core\Config\Cache\IConfigCache;
use Friendica\Core\System;
use Friendica\Database\Connection\ConnectionException;
use Friendica\Database\Connection\IConnection;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Logger\VoidLogger;
use Friendica\Util\Profiler;
use Psr\Log\LoggerInterface;
class Database implements IDatabase, IDatabaseLock
{
/**
* The profile the database calls
* @var Profiler
*/
private $profiler;
/**
* The basic configuration cache
* @var IConfigCache
*/
private $configCache;
/**
* The connection instance of the database
* @var IConnection
*/
private $connection;
/**
* The logger instance
* @var LoggerInterface
*/
private $logger;
/**
* True, if the database is currently in a transaction
* @var bool
*/
private $inTransaction;
/**
* Saves the whole DB relation for performance reason
* @var array
*/
private $dbRelation;
public function __construct(IConnection $connection, IConfigCache $configCache, Profiler $profiler)
{
// At startup, the logger isn't set
$this->logger = new VoidLogger();
$this->configCache = $configCache;
$this->profiler = $profiler;
$this->connection = $connection;
$this->connection->connect();
}
/**
* {@inheritDoc}
*/
public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;
}
/**
* {@inheritDoc}
*/
public function getConnection()
{
return $this->connection;
}
/**
* {@inheritdoc}
* @throws \Exception
*/
public function getDatabaseName()
{
$ret = $this->prepared("SELECT DATABASE() AS `db`");
$data = $this->toArray($ret);
return $data[0]['db'];
}
/**
* {@inheritDoc}
*/
public function close($stmt)
{
$stamp1 = microtime(true);
if (!is_object($stmt)) {
return false;
}
$ret = $this->connection->closeStatement($stmt);
$this->profiler->saveTimestamp($stamp1, 'database', System::callstack());
return $ret;
}
/**
* {@inheritDoc}
*/
public function transaction()
{
if (!$this->connection->performCommit() ||
!$this->connection->startTransaction()) {
return false;
}
$this->inTransaction = true;
return true;
}
/**
* {@inheritDoc}
*/
public function exists($table, array $condition)
{
return DBA::exists($table, $condition);
}
public function count($table, array $condition = [])
{
return DBA::count($table, $condition);
}
public function fetch($stmt)
{
$stamp1 = microtime(true);
if (!is_object($stmt)) {
return false;
}
$columns = $this->getConnection()->fetch($stmt);
$this->profiler->saveTimestamp($stamp1, 'database', System::callstack());
return $columns;
}
/**
* {@inheritDoc}
*/
public function commit()
{
$conn = $this->connection;
if (!$conn->performCommit()) {
return false;
}
$this->inTransaction = false;
return true;
}
/**
* {@inheritDoc}
*/
public function rollback()
{
$conn = $this->connection;
$ret = $conn->rollbackTransaction();
$this->inTransaction = false;
return $ret;
}
public function insert($table, array $param, $on_duplicate_update = false)
{
return DBA::insert($table, $param, $on_duplicate_update);
}
/**
* {@inheritDoc}
*/
public function delete($table, array $conditions, $cascade = true, array &$callstack = [])
{
$conn = $this->connection;
$logger = $this->logger;
if (empty($table) || empty($conditions)) {
$logger->info('Table and conditions have to be set');
return false;
}
$commands = [];
// Create a key for the loop prevention
$key = $table . ':' . json_encode($conditions);
// We quit when this key already exists in the callstack.
if (isset($callstack[$key])) {
return $commands;
}
$callstack[$key] = true;
$table = $conn->escape($table);
$commands[$key] = ['table' => $table, 'conditions' => $conditions];
// To speed up the whole process we cache the table relations
if ($cascade && count($this->dbRelation) == 0) {
$this->buildRelationData();
}
// Is there a relation entry for the table?
if ($cascade && isset($this->dbRelation[$table])) {
// We only allow a simple "one field" relation.
$field = array_keys($this->dbRelation[$table])[0];
$rel_def = array_values($this->dbRelation[$table])[0];
// Create a key for preventing double queries
$qkey = $field . '-' . $table . ':' . json_encode($conditions);
// When the search field is the relation field, we don't need to fetch the rows
// This is useful when the leading record is already deleted in the frontend but the rest is done in the backend
if ((count($conditions) == 1) && ($field == array_keys($conditions)[0])) {
foreach ($rel_def AS $rel_table => $rel_fields) {
foreach ($rel_fields AS $rel_field) {
$this->delete($rel_table, [$rel_field => array_values($conditions)[0]], $cascade, $callstack);
}
}
// We quit when this key already exists in the callstack.
} elseif (!isset($callstack[$qkey])) {
$callstack[$qkey] = true;
// Fetch all rows that are to be deleted
$data = self::select($table, [$field], $conditions);
while ($row = self::fetch($data)) {
$this->delete($table, [$field => $row[$field]], $cascade, $callstack);
}
$this->close($data);
// Since we had split the delete command we don't need the original command anymore
unset($commands[$key]);
}
}
// Now we finalize the process
$do_transaction = !$this->inTransaction;
if ($do_transaction) {
$this->transaction();
}
$compacted = [];
$counter = [];
foreach ($commands AS $command) {
$conditions = $command['conditions'];
reset($conditions);
$first_key = key($conditions);
$condition_string = self::buildCondition($conditions);
if ((count($command['conditions']) > 1) || is_int($first_key)) {
$sql = "DELETE FROM `" . $command['table'] . "`" . $condition_string;
$logger->debug(self::replaceParameters($sql, $conditions));
if (!$this->execute($sql, $conditions)) {
if ($do_transaction) {
$this->rollback();
}
return false;
}
} else {
$key_table = $command['table'];
$key_condition = array_keys($command['conditions'])[0];
$value = array_values($command['conditions'])[0];
// Split the SQL queries in chunks of 100 values
// We do the $i stuff here to make the code better readable
$i = isset($counter[$key_table][$key_condition]) ? $counter[$key_table][$key_condition] : 0;
if (isset($compacted[$key_table][$key_condition][$i]) && count($compacted[$key_table][$key_condition][$i]) > 100) {
++$i;
}
$compacted[$key_table][$key_condition][$i][$value] = $value;
$counter[$key_table][$key_condition] = $i;
}
}
foreach ($compacted AS $table => $values) {
foreach ($values AS $field => $field_value_list) {
foreach ($field_value_list AS $field_values) {
$sql = "DELETE FROM `" . $table . "` WHERE `" . $field . "` IN (" .
substr(str_repeat("?, ", count($field_values)), 0, -2) . ");";
$logger->debug(self::replaceParameters($sql, $field_values));
if (!$this->execute($sql, $field_values)) {
if ($do_transaction) {
$this->rollback();
}
return false;
}
}
}
}
if ($do_transaction) {
$this->commit();
}
return true;
}
/**
* {@inheritDoc}
*/
public function update($table, array $fields, array $condition, $old_fields = [])
{
$conn = $this->connection;
$logger = $this->logger;
if (empty($table) || empty($fields) || empty($condition)) {
$logger->info('Table, fields and condition have to be set');
return false;
}
$table = $conn->escape($table);
$condition_string = self::buildCondition($condition);
if (is_bool($old_fields)) {
$do_insert = $old_fields;
$old_fields = $this->selectFirst($table, [], $condition);
if (is_bool($old_fields)) {
if ($do_insert) {
$values = array_merge($condition, $fields);
return $this->insert($table, $values, $do_insert);
}
$old_fields = [];
}
}
$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 (!$do_update || (count($fields) == 0)) {
return true;
}
$sql = "UPDATE `".$table."` SET `".
implode("` = ?, `", array_keys($fields))."` = ?".$condition_string;
$params1 = array_values($fields);
$params2 = array_values($condition);
$params = array_merge_recursive($params1, $params2);
return $this->execute($sql, $params);
}
/**
* {@inheritDoc}
*/
public function select($table, array $fields = [], array $condition = [], array $params = [])
{
$conn = $this->connection;
if ($table == '') {
return false;
}
$table = $conn->escape($table);
if (count($fields) > 0) {
$select_fields = "`" . implode("`, `", array_values($fields)) . "`";
} else {
$select_fields = "*";
}
$condition_string = self::buildCondition($condition);
$param_string = self::buildParameter($params);
$sql = "SELECT " . $select_fields . " FROM `" . $table . "`" . $condition_string . $param_string;
$result = $this->prepared($sql, $condition);
return $result;
}
/**
* {@inheritDoc}
*/
public function selectFirst($table, array $fields = [], array $condition = [], array $params = [])
{
$params['limit'] = 1;
$result = $this->select($table, $fields, $condition, $params);
if (is_bool($result)) {
return $result;
} else {
$row = $this->fetch($result);
$this->close($result);
return $row;
}
}
public function lock($table)
{
return DBA::lock($table);
}
public function unlock()
{
return DBA::unlock();
}
/**
* {@inheritDoc}
*/
public function prepared($sql)
{
$logger = $this->logger;
$profiler = $this->profiler;
$config = $this->configCache;
$conn = $this->connection;
$stamp1 = microtime(true);
$params = $this->getParameters(func_get_args());
// Renumber the array keys to be sure that they fit
$i = 0;
$args = [];
foreach ($params AS $param) {
// Avoid problems with some MySQL servers and boolean values. See issue #3645
if (is_bool($param)) {
$param = (int)$param;
}
$args[++$i] = $param;
}
if (!$conn->isConnected()) {
return false;
}
if ((substr_count($sql, '?') != count($args)) && (count($args) > 0)) {
// Question: Should we continue or stop the query here?
$logger->warning('Query parameters mismatch.', ['query' => $sql, 'args' => $args, 'callstack' => System::callstack()]);
}
$sql = $this->cleanQuery($sql);
$sql = $this->anyValueFallback($sql);
if ($config->get('system', 'db_callstack') !== null) {
$sql = "/*".System::callstack()." */ ".$sql;
}
self::$error = '';
self::$errorno = 0;
self::$affected_rows = 0;
// We have to make some things different if this function is called from "e"
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
if (isset($trace[1])) {
$called_from = $trace[1];
} else {
// We use just something that is defined to avoid warnings
$called_from = $trace[0];
}
// We are having an own error logging in the function "e"
$called_from_e = ($called_from['function'] == 'e');
try {
$retval = $conn->executePrepared($sql, $args);
self::$affected_rows = $conn->getNumRows($retval);
} catch (ConnectionException $exception) {
// We are having an own error logging in the function "e"
if (($exception->getCode() != 0) && !$called_from_e) {
// We have to preserve the error code, somewhere in the logging it get lost
$error = $exception->getMessage();
$errorno = $exception->getCode();
$this->logger->error('DB Error ' . self::$errorno . ': ' . self::$error . "\n" .
System::callstack(8) . "\n" . self::replaceParameters($sql, $args));
// On a lost connection we try to reconnect - but only once.
if ($errorno == 2006) {
if (self::$in_retrial || !$conn->reconnect()) {
// It doesn't make sense to continue when the database connection was lost
if (self::$in_retrial) {
$logger->notice('Giving up retrial because of database error ' . $errorno . ': ' . $error);
} else {
$logger->notice("Couldn't reconnect after database error " . $errorno . ': ' . $error);
}
exit(1);
} else {
// We try it again
$logger->notice('Reconnected after database error ' . $errorno . ': ' . $error);
self::$in_retrial = true;
$ret = $this->prepared($sql, $args);
self::$in_retrial = false;
return $ret;
}
}
self::$error = $error;
self::$errorno = $errorno;
}
}
$profiler->saveTimestamp($stamp1, 'database', System::callstack());
if ($config->get('system', 'db_log')) {
$stamp2 = microtime(true);
$duration = (float)($stamp2 - $stamp1);
if (($duration > $config->get('system', 'db_loglimit'))) {
$duration = round($duration, 3);
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
@file_put_contents($config->get('system', 'db_log'), DateTimeFormat::utcNow() . $duration . "\t" .
basename($backtrace[1]["file"])."\t" .
$backtrace[1]["line"]."\t".$backtrace[2]["function"]."\t" .
substr(self::replaceParameters($sql, $args), 0, 2000)."\n", FILE_APPEND);
}
}
return $retval;
}
/**
* {@inheritDoc}
*/
public function execute($sql)
{
$conn = $this->connection;
$profiler = $this->profiler;
$logger = $this->logger;
$stamp = microtime(true);
$params = self::getParameters(func_get_args());
// In a case of a deadlock we are repeating the query 20 times
$timeout = 20;
$errorno = 0;
do {
try {
$stmt = $this->prepared($sql, $params);
if (is_bool($stmt)) {
$retval = $stmt;
} elseif (is_object($stmt)) {
$retval = true;
} else {
$retval = false;
}
$this->close($stmt);
} catch (ConnectionException $exception) {
$errorno = $exception->getCode();
}
} while (($errorno == 1213) && (--$timeout > 0));
if ($errorno != 0) {
// We have to preserve the error code, somewhere in the logging it get lost
$error = self::$error;
$errorno = self::$errorno;
$logger->error('DB Error ' . self::$errorno . ': ' . self::$error . "\n" .
System::callstack(8)."\n".self::replaceParameters($sql, $params));
// On a lost connection we simply quit.
// A reconnect like in self::p could be dangerous with modifications
if ($errorno == 2006) {
$logger->notice('Giving up because of database error '.$errorno.': '.$error);
exit(1);
}
self::$error = $error;
self::$errorno = $errorno;
}
$profiler->saveTimestamp($stamp, "database_write", System::callstack());
return $retval;
}
/**
* @brief Replaces the ? placeholders with the parameters in the $args array
*
* @param string $sql SQL query
* @param array $args The parameters that are to replace the ? placeholders
*
* @return string The replaced SQL query
*/
public static function replaceParameters($sql, array $args)
{
$offset = 0;
foreach ($args AS $param => $value) {
if (is_int($args[$param]) || is_float($args[$param])) {
$replace = intval($args[$param]);
} else {
$replace = "'" . $this->escape($args[$param]) . "'";
}
$pos = strpos($sql, '?', $offset);
if ($pos !== false) {
$sql = substr_replace($sql, $replace, $pos, 1);
}
$offset = $pos + strlen($replace);
}
return $sql;
}
/**
* Convert parameter array to an universal form
*
* @param array $args Parameter array
*
* @return array universalized parameter array
*/
public static function getParameters(array $args)
{
unset($args[0]);
// When the second function parameter is an array then use this as the parameter array
if ((count($args) > 0) && (is_array($args[1]))) {
return $args[1];
} else {
return $args;
}
}
/**
* @brief Returns the SQL condition string built from the provided condition array
*
* This function operates with two modes.
* - Supplied with a filed/value associative array, it builds simple strict
* equality conditions linked by AND.
* - Supplied with a flat list, the first element is the condition string and
* the following arguments are the values to be interpolated
*
* $condition = ["uid" => 1, "network" => 'dspr'];
* or:
* $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr'];
*
* In either case, the provided array is left with the parameters only
*
* @param array $condition
* @return string
*/
public static function buildCondition(array &$condition = [])
{
$condition_string = '';
if (count($condition) > 0) {
reset($condition);
$first_key = key($condition);
if (is_int($first_key)) {
$condition_string = " WHERE (" . array_shift($condition) . ")";
} else {
$new_values = [];
$condition_string = "";
foreach ($condition as $field => $value) {
if ($condition_string != "") {
$condition_string .= " AND ";
}
if (is_array($value)) {
/* Workaround for MySQL Bug #64791.
* Never mix data types inside any IN() condition.
* In case of mixed types, cast all as string.
* Logic needs to be consistent with DBA::p() data types.
*/
$is_int = false;
$is_alpha = false;
foreach ($value as $single_value) {
if (is_int($single_value)) {
$is_int = true;
} else {
$is_alpha = true;
}
}
if ($is_int && $is_alpha) {
foreach ($value as &$ref) {
if (is_int($ref)) {
$ref = (string)$ref;
}
}
unset($ref); //Prevent accidental re-use.
}
$new_values = array_merge($new_values, array_values($value));
$placeholders = substr(str_repeat("?, ", count($value)), 0, -2);
$condition_string .= "`" . $field . "` IN (" . $placeholders . ")";
} else {
$new_values[$field] = $value;
$condition_string .= "`" . $field . "` = ?";
}
}
$condition_string = " WHERE (" . $condition_string . ")";
$condition = $new_values;
}
}
return $condition_string;
}
/**
* Build the array with the table relations
*
* The array is build from the database definitions in DBStructure.php
*
* This process must only be started once, since the value is cached.
*/
private function buildRelationData()
{
if ($this->exists('config', ['cat' => 'system', 'k' => 'basepath'])) {
$basePath = $this->select('config', 'v', ['cat' => 'system', 'k' => 'basepath']);
} else {
$basePath = $this->configCache->get('system', 'basepath');
}
$definition = DBStructure::definition($basePath);
foreach ($definition AS $table => $structure) {
foreach ($structure['fields'] AS $field => $field_struct) {
if (isset($field_struct['relation'])) {
foreach ($field_struct['relation'] AS $rel_table => $rel_field) {
$this->dbRelation[$rel_table][$rel_field][$table][] = $field;
}
}
}
}
}
/**
* @brief Callback function for "esc_array"
*
* @param mixed $value Array value
* @param string $key Array key
* @param boolean $add_quotation add quotation marks for string values
* @return void
*/
private function escapeArrayCallback(&$value, $key, $add_quotation)
{
$conn = $this->connection;
if (!$add_quotation) {
if (is_bool($value)) {
$value = ($value ? '1' : '0');
} else {
$value = $conn->escape($value);
}
return;
}
if (is_bool($value)) {
$value = ($value ? 'true' : 'false');
} elseif (is_float($value) || is_integer($value)) {
$value = (string) $value;
} else {
$value = "'" . $conn->escape($value) . "'";
}
}
}

View File

@ -2,44 +2,56 @@
namespace Friendica\Database;
use Friendica\Database\Connection\IConnection;
use Psr\Log\LoggerInterface;
interface IDatabase
{
/**
* Returns if the database is connected
* @return bool
* Sets the Logger of this database instance
* @param LoggerInterface $logger
*/
function isConnected();
function setLogger(LoggerInterface $logger);
/**
* Disconnects the current database connection
*/
function disconnect();
/**
* Perform a reconnect of an existing database connection
*
* @return bool Wsa the reconnect successful?
*/
function reconnect();
/**
* Return the database object.
* @return mixed
* Returns the current connection of the database
* @return IConnection
*/
function getConnection();
/**
* Returns the server version string
* @return string
*/
function serverInfo();
/**
* Returns the selected database name
* @return string
*/
function getDatabaseName();
/**
* Executes a prepared statement that returns data
*
* @usage Example: $r = p("SELECT * FROM `item` WHERE `guid` = ?", $guid);
*
* Please only use it with complicated queries.
* For all regular queries please use DBA::select or DBA::exists
*
* @param string $sql SQL statement
*
* @return bool|object statement object or result object
*
* @throws \Exception
*/
function prepared($sql);
/**
* Executes a prepared statement like UPDATE or INSERT that doesn't return data
*
* Please use DBA::delete, DBA::insert, DBA::update, ... instead
*
* @param string $sql SQL statement
* @return boolean Was the query successfull? False is returned only if an error occurred
* @throws \Exception
*/
function execute($sql);
/**
* Check if data exists
*
@ -84,6 +96,15 @@ interface IDatabase
*/
function transaction();
/**
* @brief Closes the current statement
*
* @param object $stmt statement object
*
* @return boolean was the close successful?
*/
function close($stmt);
/**
* Does a commit
*
@ -119,7 +140,7 @@ interface IDatabase
*
* @return bool Was the delete successful?
*/
function delete($table, array $conditions, $cascade = false);
function delete($table, array $conditions, $cascade = true);
/**
* Updates rows in the database.

View File

@ -1,272 +0,0 @@
<?php
namespace Friendica\Database;
use Friendica\Core\Config\Cache\IConfigCache;
use Friendica\Util\Profiler;
use mysqli;
use mysqli_result;
use mysqli_stmt;
use PDO;
use PDOException;
use PDOStatement;
class MysqlDatabase implements IDatabase, IDatabaseLock
{
const DRIVER_PDO = 'pdo';
const DRIVER_MYSQLI = 'mysqli';
const DRIVER_INVALID = null;
/**
* The connection state of the database
* @var bool
*/
private $connected;
private $dbUser;
private $dbPass;
private $dbName;
private $dbHost;
private $dbCharset;
private $serverInfo;
private $profiler;
private $configCache;
/**
* The connection to the database
* @var PDO|mysqli
*/
private $connection;
private $driver;
public function __construct(IConfigCache $configCache, Profiler $profiler, $serveraddr, $user, $pass, $db, $charset = null)
{
$this->configCache = $configCache;
$this->profiler = $profiler;
$this->dbHost = $serveraddr;
$this->dbUser = $user;
$this->dbPass = $pass;
$this->dbName = $db;
$this->dbCharset = $charset;
$this->serverInfo = '';
$this->connect();
}
public function isConnected()
{
return $this->connected;
}
private function connect()
{
if (!is_null($this->connection) && $this->isConnected()) {
return;
}
$port = 0;
$serveraddr = trim($this->dbHost);
$serverdata = explode(':', $serveraddr);
$server = $serverdata[0];
if (count($serverdata) > 1) {
$port = trim($serverdata[1]);
}
$server = trim($server);
$user = trim($this->dbUser);
$pass = trim($this->dbPass);
$db = trim($this->dbName);
$charset = trim($this->dbCharset);
if (!(strlen($server) && strlen($user))) {
$this->connected = false;
return;
}
if (class_exists('\PDO') && in_array('mysql', PDO::getAvailableDrivers())) {
$this->driver = self::DRIVER_PDO;
$connect = "mysql:host=".$server.";dbname=".$db;
if ($port > 0) {
$connect .= ";port=".$port;
}
if ($charset) {
$connect .= ";charset=".$charset;
}
try {
$this->connection = @new PDO($connect, $user, $pass);
$this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$this->connected = true;
} catch (PDOException $e) {
/// @TODO At least log exception, don't ignore it!
}
}
if (!$this->connected && class_exists('\mysqli')) {
$this->driver = self::DRIVER_MYSQLI;
if ($port > 0) {
$this->connection = @new mysqli($server, $user, $pass, $db, $port);
} else {
$this->connection = @new mysqli($server, $user, $pass, $db);
}
if (!mysqli_connect_errno()) {
$this->connected = true;
if ($charset) {
$this->connection->set_charset($charset);
}
}
}
// No suitable SQL driver was found.
if (!$this->connected) {
$this->driver = self::DRIVER_INVALID;
$this->connection = null;
}
}
/**
* {@inheritdoc}
*/
public function disconnect()
{
if (is_null($this->connection)) {
return;
}
switch ($this->driver) {
case self::DRIVER_PDO:
$this->connection = null;
break;
case self::DRIVER_MYSQLI:
$this->connection->close();
$this->connection = null;
break;
}
}
/**
* {@inheritdoc}
*/
public function reconnect()
{
$this->disconnect();
$this->connect();
return $this->connected;
}
/**
* {@inheritdoc}
* @return mixed|mysqli|PDO
*/
public function getConnection()
{
return $this->connection;
}
/**
* {@inheritdoc}
*/
public function serverInfo()
{
if (empty($this->serverInfo)) {
switch ($this->driver) {
case self::DRIVER_PDO:
$this->serverInfo = $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION);
break;
case self::DRIVER_MYSQLI:
$this->serverInfo = $this->connection->server_info;
break;
}
}
return $this->serverInfo;
}
/**
* {@inheritdoc}
* @throws \Exception
*/
public function getDatabaseName()
{
$ret = $this->p("SELECT DATABASE() AS `db`");
$data = $this->toArray($ret);
return $data[0]['db'];
}
public function exists($table, array $condition)
{
return DBA::exists($table, $condition);
}
public function count($table, array $condition = [])
{
return DBA::count($table, $condition);
}
public function fetch($stmt)
{
return DBA::fetch($stmt);
}
public function transaction()
{
return DBA::transaction();
}
public function commit()
{
return DBA::commit();
}
public function rollback()
{
return DBA::rollback();
}
public function insert($table, array $param, $on_duplicate_update = false)
{
return DBA::insert($table, $param, $on_duplicate_update);
}
public function delete($table, array $conditions, $cascade = true)
{
return DBA::delete($table, $conditions, [$cascade]);
}
public function update($table, array $fields, array $condition, array $old_fields = [])
{
return DBA::delete($table, $fields, $condition, $old_fields);
}
public function select($table, array $fields = [], array $condition = [], array $params = [])
{
return DBA::select($table, $fields, $condition, $params);
}
public function selectFirst($table, array $fields = [], array $condition = [], $params = [])
{
return DBA::selectFirst($table, $fields, $condition, $params);
}
public function lock($table)
{
return DBA::lock($table);
}
public function unlock()
{
return DBA::unlock();
}
}

View File

@ -5,6 +5,7 @@ namespace Friendica\Factory;
use Friendica\Core\Config\Cache;
use Friendica\Database;
use Friendica\Util\Profiler;
use PDO;
class DBFactory
{
@ -49,15 +50,23 @@ class DBFactory
$db_data = $server['MYSQL_DATABASE'];
}
$database = new Database\MysqlDatabase($basePath, $configCache, $profiler, $db_host, $db_user, $db_pass, $db_data, $charset);
if ($database->isConnected()) {
// Loads DB_UPDATE_VERSION constant
Database\DBStructure::definition($basePath, false);
if (class_exists('\PDO') && in_array('mysql', PDO::getAvailableDrivers())) {
$dbConnection = new Database\Connection\PDOConnection($db_host, $db_user, $db_pass, $db_data, $charset);
} elseif (class_exists('\mysqli')) {
$dbConnection = new Database\Connection\MysqlIConnection($db_host, $db_user, $db_pass, $db_data, $charset);
} else {
throw new \Exception('Missing connection environment');
}
unset($db_host, $db_user, $db_pass, $db_data, $charset);
$database = new Database\Database($dbConnection, $configCache, $profiler);
if ($dbConnection->isConnected()) {
// Loads DB_UPDATE_VERSION constant
Database\DBStructure::definition($basePath, false);
}
return $database;
}
}