postgres.php 1.32 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
<?php namespace Laravel\Database\Connectors; use PDO;

class Postgres extends Connector {

	/**
	 * Establish a PDO database connection for a given database configuration.
	 *
	 * @param  array  $config
	 * @return PDO
	 */
	public function connect($config)
	{
		$connection = new PDO($this->dsn($config), $config['username'], $config['password'], $this->options($config));

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

		return $connection;
	}

	/**
	 * Format the DSN connection string for a Postgres connection.
	 *
	 * @param  array   $config
	 * @return string
	 */
	protected function dsn($config)
	{
		// Format the initial Postgres PDO connection string. These options are required
		// for every Postgres connection that is established. The connection strings
		// have the following convention: "pgsql:host=hostname;dbname=database"
		$dsn = sprintf('%s:host=%s;dbname=%s', $config['driver'], $config['host'], $config['database']);

		// Check for any optional Postgres PDO options. These options are not required
		// to establish a PDO connection; however, may be needed in certain server
		// or hosting environments used by the developer.
39
		foreach (array('port') as $key => $value)
40 41 42 43 44 45 46 47
		{
			if (isset($config[$key])) $dsn .= ";{$key}={$value}";
		}

		return $dsn;
	}

}