postgres.php 1.24 KB
Newer Older
1 2 3 4 5
<?php namespace Laravel\Database\Connectors; use PDO;

class Postgres extends Connector {

	/**
6 7 8 9 10 11 12 13 14 15 16 17
	 * The PDO connection options.
	 *
	 * @var array
	 */
	protected $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,
	);

	/**
18
	 * Establish a PDO database connection.
19 20 21 22 23 24
	 *
	 * @param  array  $config
	 * @return PDO
	 */
	public function connect($config)
	{
25
		extract($config);
26

27
		$dsn = "pgsql:host={$host};dbname={$database}";
28

Taylor Otwell committed
29 30 31 32
		// The developer has the freedom of specifying a port for the PostgresSQL
		// database or the default port (5432) will be used by PDO to create the
		// connection to the database for the developer.
		if (isset($config['port']))
33
		{
Taylor Otwell committed
34
			$dsn .= ";port={$config['port']}";
35 36 37 38
		}

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

Taylor Otwell committed
39 40 41
		// If a character set has been specified, we'll execute a query against
		// the database to set the correct character set. By default, this is
		// set to UTF-8 which should be fine for most scenarios.
42 43
		if (isset($config['charset']))
		{
44
			$connection->prepare("SET NAMES '{$config['charset']}'")->execute();
45 46
		}

47
		return $connection;
48 49 50
	}

}