<commit_before><?php
/**
 * @copyright Copyright (C) 2010-2022, the Friendica project
 *
 * @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\ValueObject\Cache;
use Friendica\Core\System;
use Friendica\Network\HTTPException\ServiceUnavailableException;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Profiler;
use mysqli;
use mysqli_result;
use mysqli_stmt;
use PDO;
use PDOException;
use PDOStatement;
use Psr\Log\LoggerInterface;

/**
 * This class is for the low level database stuff that does driver specific things.
 */
class Database
{
	const PDO = 'pdo';
	const MYSQLI = 'mysqli';

	const INSERT_DEFAULT = 0;
	const INSERT_UPDATE = 1;
	const INSERT_IGNORE = 2;

	protected $connected = false;

	/**
	 * @var \Friendica\Core\Config\ValueObject\Cache
	 */
	protected $configCache;
	/**
	 * @var Profiler
	 */
	protected $profiler;
	/**
	 * @var LoggerInterface
	 */
	protected $logger;
	protected $server_info    = '';
	/** @var PDO|mysqli */
	protected $connection;
	protected $driver;
	protected $pdo_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(Cache $configCache, Profiler $profiler, LoggerInterface $logger)
	{
		// We are storing these values for being able to perform a reconnect
		$this->configCache   = $configCache;
		$this->profiler      = $profiler;
		$this->logger        = $logger;

		$this->connect();

		if ($this->isConnected()) {
			// Loads DB_UPDATE_VERSION constant
			DBStructure::definition($configCache->get('system', 'basepath'), false);
		}
	}

	public function connect()
	{
		if (!is_null($this->connection) && $this->connected()) {
			return $this->connected;
		}

		// Reset connected state
		$this->connected = false;

		$port       = 0;
		$serveraddr = trim($this->configCache->get('database', 'hostname'));
		$serverdata = explode(':', $serveraddr);
		$server     = $serverdata[0];
		if (count($serverdata) > 1) {
			$port = trim($serverdata[1]);
		}

		if (!empty(trim($this->configCache->get('database', 'port')))) {
			$port = trim($this->configCache->get('database', 'port'));
		}

		$server  = trim($server);
		$user    = trim($this->configCache->get('database', 'username'));
		$pass    = trim($this->configCache->get('database', 'password'));
		$db      = trim($this->configCache->get('database', 'database'));
		$charset = trim($this->configCache->get('database', 'charset'));

		if (!(strlen($server) && strlen($user))) {
			return false;
		}

		$persistent = (bool)$this->configCache->get('database', 'persistent');

		$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 = self::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, [PDO::ATTR_PERSISTENT => $persistent]);
				$this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->pdo_emulate_prepares);
				$this->connected = true;
			} catch (PDOException $e) {
				$this->connected = false;
			}
		}

		if (!$this->connected && class_exists('\mysqli')) {
			$this->driver = self::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;
<commit_msg>Don't throw an exception when the database query fails<commit_after><?php
/**
 * @copyright Copyright (C) 2010-2022, the Friendica project
 *
 * @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\ValueObject\Cache;
use Friendica\Core\System;
use Friendica\Network\HTTPException\ServiceUnavailableException;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Profiler;
use mysqli;
use mysqli_result;
use mysqli_stmt;
use PDO;
use PDOException;
use PDOStatement;
use Psr\Log\LoggerInterface;

/**
 * This class is for the low level database stuff that does driver specific things.
 */
class Database
{
	const PDO = 'pdo';
	const MYSQLI = 'mysqli';

	const INSERT_DEFAULT = 0;
	const INSERT_UPDATE = 1;
	const INSERT_IGNORE = 2;

	protected $connected = false;

	/**
	 * @var \Friendica\Core\Config\ValueObject\Cache
	 */
	protected $configCache;
	/**
	 * @var Profiler
	 */
	protected $profiler;
	/**
	 * @var LoggerInterface
	 */
	protected $logger;
	protected $server_info    = '';
	/** @var PDO|mysqli */
	protected $connection;
	protected $driver;
	protected $pdo_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(Cache $configCache, Profiler $profiler, LoggerInterface $logger)
	{
		// We are storing these values for being able to perform a reconnect
		$this->configCache   = $configCache;
		$this->profiler      = $profiler;
		$this->logger        = $logger;

		$this->connect();

		if ($this->isConnected()) {
			// Loads DB_UPDATE_VERSION constant
			DBStructure::definition($configCache->get('system', 'basepath'), false);
		}
	}

	public function connect()
	{
		if (!is_null($this->connection) && $this->connected()) {
			return $this->connected;
		}

		// Reset connected state
		$this->connected = false;

		$port       = 0;
		$serveraddr = trim($this->configCache->get('database', 'hostname'));
		$serverdata = explode(':', $serveraddr);
		$server     = $serverdata[0];
		if (count($serverdata) > 1) {
			$port = trim($serverdata[1]);
		}

		if (!empty(trim($this->configCache->get('database', 'port')))) {
			$port = trim($this->configCache->get('database', 'port'));
		}

		$server  = trim($server);
		$user    = trim($this->configCache->get('database', 'username'));
		$pass    = trim($this->configCache->get('database', 'password'));
		$db      = trim($this->configCache->get('database', 'database'));
		$charset = trim($this->configCache->get('database', 'charset'));

		if (!(strlen($server) && strlen($user))) {
			return false;
		}

		$persistent = (bool)$this->configCache->get('database', 'persistent');

		$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 = self::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, [PDO::ATTR_PERSISTENT => $persistent]);
				$this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->pdo_emulate_prepares);
				$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
				$this->connected = true;
			} catch (PDOException $e) {
				$this->connected = false;
			}
		}

		if (!$this->connected && class_exists('\mysqli')) {
			$this->driver = self::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;

{'fraction_non_alphanumeric': 0.155, 'fraction_numerical': 0.004565217391304348, 'mean_word_length': 3.875993640699523, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 101, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3711890', 'n_tokens_mistral': 3221, 'n_tokens_neox': 2909, 'n_words': 776}