<commit_before><?php

/**
 * Spoon Library
 *
 * This source file is part of the Spoon Library. More information,
 * documentation and tutorials can be found @ http://www.spoon-library.com
 *
 * @package		spoon
 * @subpackage	database
 *
 *
 * @author		<NAME> <<EMAIL>>
 * @since		1.1.0
 */


/**
 * This class provides most of the base methods implemented by almost
 * every database system
 *
 * @package		spoon
 * @subpackage	database
 *
 *
 * @author		<NAME> <<EMAIL>>
 * @author		<NAME> <<EMAIL>>
 * @since		1.1.0
 */
class SpoonDatabase
{
	/**
	 * Database name
	 *
	 * @var	string
	 */
	private $database;


	/**
	 * Debug setting. Queries are logged when enabled
	 *
	 * @var	bool
	 */
	private $debug = false;


	/**
	 * Database driver
	 *
	 * @var	string
	 */
	private $driver;


	/**
	 * Database handler object
	 *
	 * @var	PDO
	 */
	private $handler;


	/**
	 * Database hostname
	 *
	 * @var	string
	 */
	private $hostname;


	/**
	 * Database password
	 *
	 * @var	string
	 */
	private $password;


	/**
	 * Database port
	 *
	 * @var int
	 */
	private $port;


	/**
	 * List of all executed queries and their parameters
	 *
	 * @var	array
	 */
	private $queries = array();


	/**
	 * Database username
	 *
	 * @var	string
	 */
	private $username;


	/**
	 * Creates a database connection instance.
	 *
	 * @param	string $driver			The driver to use. Available drivers depend on your server configuration.
	 * @param	string $hostname		The host or IP of your database server.
	 * @param	string $username		The username to authenticate on your database server.
	 * @param	string $password		The password to authenticate on your database server.
	 * @param	string $database		The name of the database to use.
	 * @param	int[optional] $port		The port to connect on.
	 */
	public function __construct($driver, $hostname, $username, $password, $database, $port = null)
	{
		$this->setDriver($driver);
		$this->setHostname($hostname);
		$this->setUsername($username);
		$this->setPassword($password);
		$this->setDatabase($database);
		$this->setPort($port);
	}


	/**
	 * Creates a new database connection if it was not yet made.
	 */
	private function connect()
	{
		// not yet connected
		if(!$this->handler)
		{
			try
			{
				// build dsn
				if($this->port !== null) $dsn = $this->driver . ':host=' . $this->hostname . ';port=' . $this->port . ';dbname=' . $this->database;
				else $dsn = $this->driver . ':host=' . $this->hostname . ';dbname=' . $this->database;

				// create handler
				$this->handler = new PDO($dsn, $this->username, $this->password);
				$this->handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			}

			catch(PDOException $e)
			{
				throw new SpoonDatabaseException('A database connection could not be established.', 0, $this->password);
			}
		}

		// set nasty option
		$this->handler->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
	}


	/**
	 * Query to delete records, which returns the number of affected rows.
	 *
	 * @return	int								The number of affected rows.
	 * @param	string $table					The table to perform the delete-statement on.
	 * @param	string[optional] $where			The WHERE-clause.
	 * @param	mixed[optional] $parameters		The parameters that will be used in the query.
	 */
	public function delete($table, $where = null, $parameters = array())
	{
		// create connection
		if(!$this->handler) $this->connect();

		// build query
		$query = 'DELETE FROM ' . $this->quoteName((string) $table);

		// add where class
		$query = ($where != '') ? $query . ' WHERE ' . (string) $where : $query;

		// set parameters
		$parameters = (array) $parameters;

		// create statement
		$statement = $this->handler->prepare($query);

		// validate statement
		if($statement === false)<commit_msg>Refactored building dsn string. Now works with PGSQL.<commit_after><?php

/**
 * Spoon Library
 *
 * This source file is part of the Spoon Library. More information,
 * documentation and tutorials can be found @ http://www.spoon-library.com
 *
 * @package		spoon
 * @subpackage	database
 *
 *
 * @author		<NAME> <<EMAIL>>
 * @since		1.1.0
 */


/**
 * This class provides most of the base methods implemented by almost
 * every database system
 *
 * @package		spoon
 * @subpackage	database
 *
 *
 * @author		<NAME> <<EMAIL>>
 * @author		<NAME> <<EMAIL>>
 * @since		1.1.0
 */
class SpoonDatabase
{
	/**
	 * Database name
	 *
	 * @var	string
	 */
	private $database;


	/**
	 * Debug setting. Queries are logged when enabled
	 *
	 * @var	bool
	 */
	private $debug = false;


	/**
	 * Database driver
	 *
	 * @var	string
	 */
	private $driver;


	/**
	 * Database handler object
	 *
	 * @var	PDO
	 */
	private $handler;


	/**
	 * Database hostname
	 *
	 * @var	string
	 */
	private $hostname;


	/**
	 * Database password
	 *
	 * @var	string
	 */
	private $password;


	/**
	 * Database port
	 *
	 * @var int
	 */
	private $port;


	/**
	 * List of all executed queries and their parameters
	 *
	 * @var	array
	 */
	private $queries = array();


	/**
	 * Database username
	 *
	 * @var	string
	 */
	private $username;


	/**
	 * Creates a database connection instance.
	 *
	 * @param	string $driver			The driver to use. Available drivers depend on your server configuration.
	 * @param	string $hostname		The host or IP of your database server.
	 * @param	string $username		The username to authenticate on your database server.
	 * @param	string $password		The password to authenticate on your database server.
	 * @param	string $database		The name of the database to use.
	 * @param	int[optional] $port		The port to connect on.
	 */
	public function __construct($driver, $hostname, $username, $password, $database, $port = null)
	{
		$this->setDriver($driver);
		$this->setHostname($hostname);
		$this->setUsername($username);
		$this->setPassword($password);
		$this->setDatabase($database);
		$this->setPort($port);
	}


	/**
	 * Creates a new database connection if it was not yet made.
	 */
	private function connect()
	{
		// not yet connected
		if(!$this->handler)
		{
			try
			{
				$dsn = $this->driver;
				$dsn .= ':host=' . $this->hostname;
				$dsn .= ';dbname=' . $this->database;
				$dsn .= ';user=' . $this->username;
				$dsn .= ';password=' . $this->password;

				if($this->port !== null)
				{
					$dsn .= ';port=' . $this->port;
				}

				// create handler
				$this->handler = new PDO($dsn, $this->username, $this->password);
				$this->handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

				// mysql only option
				if($this->driver == 'mysql')
				{
					$this->handler->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
				}
			}

			catch(PDOException $e)
			{
				throw new SpoonDatabaseException('A database connection could not be established.', 0, $this->password);
			}
		}
	}


	/**
	 * Query to delete records, which returns the number of affected rows.
	 *
	 * @return	int								The number of affected rows.
	 * @param	string $table					The table to perform the delete-statement on.
	 * @param	string[optional] $where			The WHERE-clause.
	 * @param	mixed[optional] $parameters		The parameters that will be used in the query.
	 */
	public function delete($table, $where = null, $parameters = array())
	{
		// create connection
		if(!$this->handler) $this->connect();

		// build query
		$query = 'DELETE FROM ' . $this->quoteName((string) $table);

		// add where class
		$query = ($where != '') ? $query . ' WHERE ' . (string) $where : $query;

		// set parameters
		$parameters = (array) $parameters;

		// create statement
		$statement = $this->handler->prepare($query);

		// validate statement
		if($statement === false)
{'fraction_non_alphanumeric': 0.14618200836820083, 'fraction_numerical': 0.0018305439330543933, 'mean_word_length': 2.9468524251805985, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 77, 'https://': 0, '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': '7373055', 'n_tokens_mistral': 2731, 'n_tokens_neox': 2396, 'n_words': 732}