postgres.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php namespace Laravel\Database\Connectors; use PDO;
  2. class Postgres extends Connector {
  3. /**
  4. * The PDO connection options.
  5. *
  6. * @var array
  7. */
  8. protected $options = array(
  9. PDO::ATTR_CASE => PDO::CASE_LOWER,
  10. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  11. PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,
  12. PDO::ATTR_STRINGIFY_FETCHES => false,
  13. );
  14. /**
  15. * Establish a PDO database connection.
  16. *
  17. * @param array $config
  18. * @return PDO
  19. */
  20. public function connect($config)
  21. {
  22. extract($config);
  23. $host_dsn = isset($host) ? 'host='.$host.';' : '';
  24. $dsn = "pgsql:{$host_dsn}dbname={$database}";
  25. // The developer has the freedom of specifying a port for the PostgresSQL
  26. // database or the default port (5432) will be used by PDO to create the
  27. // connection to the database for the developer.
  28. if (isset($config['port']))
  29. {
  30. $dsn .= ";port={$config['port']}";
  31. }
  32. $connection = new PDO($dsn, $username, $password, $this->options($config));
  33. // If a character set has been specified, we'll execute a query against
  34. // the database to set the correct character set. By default, this is
  35. // set to UTF-8 which should be fine for most scenarios.
  36. if (isset($config['charset']))
  37. {
  38. $connection->prepare("SET NAMES '{$config['charset']}'")->execute();
  39. }
  40. // If a schema has been specified, we'll execute a query against
  41. // the database to set the search path.
  42. if (isset($config['schema']))
  43. {
  44. $connection->prepare("SET search_path TO {$config['schema']}")->execute();
  45. }
  46. return $connection;
  47. }
  48. }