<commit_before><?php namespace System\DB;

use System\Config;

class Connector {

	/**
	 * The PDO connection options.
	 *
	 * @var array
	 */
	public static $options = array(
			\PDO::ATTR_CASE => \PDO::CASE_LOWER,
			\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
			\PDO::ATTR_ORACLE_NULLS => \PDO::NULL_NATURAL,
			\PDO::ATTR_STRINGIFY_FETCHES => false,
	);

	/**
	 * Establish a PDO database connection.
	 *
	 * @param  string  $connection
	 * @return PDO
	 */
	public static function connect($connection)
	{
		$config = static::configuration($connection);

		switch ($config->driver)
		{
			case 'sqlite':
				return static::connect_to_sqlite($config);

			case 'mysql':
			case 'pgsql':
				return static::connect_to_server($config);
		}

		throw new \Exception('Database driver '.$config->driver.' is not supported.');
	}

	/**
	 * Establish a PDO connection to a SQLite database.
	 *
	 * SQLite database paths can be specified either relative to the application/db
	 * directory, or as an absolute path to any location on the file system.
	 *
	 * @param  array  $config
	 * @return PDO
	 */
	private static function connect_to_sqlite($config)
	{
		if (file_exists($path = APP_PATH.'storage/db/'.$config->database.'.sqlite'))
		{
			return new \PDO('sqlite:'.$path, null, null, static::$options);
		}
		elseif (file_exists($config->database))
		{
			return new \PDO('sqlite:'.$config->database, null, null, static::$options);
		}
		else
		{
			throw new \Exception("SQLite database [".$config->database."] could not be found.");
		}
	}

	/**
	 * Connect to a MySQL or PostgreSQL database server.
	 *
	 * @param  array  $config
	 * @return PDO
	 */
	private static function connect_to_server($config)
	{
		$dsn = $config->driver.':host='.$config->host.';dbname='.$config->database;

		if (isset($config->port))
		{
			$dsn .= ';port='.$config->port;
		}

		$connection = new \PDO($dsn, $config->username, $config->password, static::$options);

		if (isset($config->charset))
		{
			$connection->prepare("SET NAMES '".$config->charset."'")->execute();
		}

		return $connection;
	}

	/**
	 * Get the configuration options for a database connection.
	 *
	 * @param  string  $connection
	 * @return object
	 */
	private static function configuration($connection)
	{
		$config = Config::get('db.connections');

		if ( ! array_key_exists($connection, $config))
		{
			throw new \Exception("Database connection [$connection] is not defined.");
		}<commit_msg>Added support for odbc connections.<commit_after><?php namespace System\DB;

use System\Config;

class Connector {

	/**
	 * The PDO connection options.
	 *
	 * @var array
	 */
	public static $options = array(
			\PDO::ATTR_CASE => \PDO::CASE_LOWER,
			\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
			\PDO::ATTR_ORACLE_NULLS => \PDO::NULL_NATURAL,
			\PDO::ATTR_STRINGIFY_FETCHES => false,
	);

	/**
	 * Establish a PDO database connection.
	 *
	 * @param  string  $connection
	 * @return PDO
	 */
	public static function connect($connection)
	{
		$config = static::configuration($connection);

		switch ($config->driver)
		{
			case 'sqlite':
				return static::connect_to_sqlite($config);

			case 'mysql':
			case 'pgsql':
				return static::connect_to_server($config);
			
			case 'odbc':
				return static::connect_to_odbc($config);
		}

		throw new \Exception('Database driver '.$config->driver.' is not supported.');
	}

	/**
	 * Establish a PDO connection to a SQLite database.
	 *
	 * SQLite database paths can be specified either relative to the application/db
	 * directory, or as an absolute path to any location on the file system.
	 *
	 * @param  object  $config
	 * @return PDO
	 */
	private static function connect_to_sqlite($config)
	{
		if (file_exists($path = APP_PATH.'storage/db/'.$config->database.'.sqlite'))
		{
			return new \PDO('sqlite:'.$path, null, null, static::$options);
		}
		elseif (file_exists($config->database))
		{
			return new \PDO('sqlite:'.$config->database, null, null, static::$options);
		}
		else
		{
			throw new \Exception("SQLite database [".$config->database."] could not be found.");
		}
	}

	/**
	 * Connect to a MySQL or PostgreSQL database server.
	 *
	 * @param  object  $config
	 * @return PDO
	 */
	private static function connect_to_server($config)
	{
		$dsn = $config->driver.':host='.$config->host.';dbname='.$config->database;

		if (isset($config->port))
		{
			$dsn .= ';port='.$config->port;
		}

		$connection = new \PDO($dsn, $config->username, $config->password, static::$options);

		if (isset($config->charset))
		{
			$connection->prepare("SET NAMES '".$config->charset."'")->execute();
		}

		return $connection;
	}

	/**
	 * Connect to an ODBC data source.
	 *
	 * @param  object  $config
	 * @return PDO
	 */
	private static function connect_to_odbc($config)
	{
		return new \PDO($config->dsn, $config->username, $config->password, static::$options);
	}

	/**
	 * Get the configuration options for a database connection.
	 *
	 * @param  string  $connection
	 * @return object
	 */
	private static function configuration($connection)
	{
		$config = Config::get('db.connections');

		if ( ! array_key_exists($connection, $config))
		{
			throw new \Exception("Database connection [$connection] is not defined.");
		}
{'fraction_non_alphanumeric': 0.15793484473232997, 'fraction_numerical': 0.0, 'mean_word_length': 3.6133567662565906, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5181991', 'n_tokens_mistral': 1899, 'n_tokens_neox': 1680, 'n_words': 419}