postgres.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php namespace Laravel\Database\Connectors; use PDO;
  2. class Postgres extends Connector {
  3. /**
  4. * Establish a PDO database connection.
  5. *
  6. * @param array $config
  7. * @return PDO
  8. */
  9. public function connect($config)
  10. {
  11. extract($config);
  12. // Format the initial Postgres PDO connection string. These options are required
  13. // for every Postgres connection that is established. The connection strings
  14. // have the following convention: "pgsql:host=hostname;dbname=database"
  15. $dsn = "pgsql:host={$host};dbname={$database}";
  16. // Check for any optional Postgres PDO options. These options are not required
  17. // to establish a PDO connection; however, may be needed in certain server
  18. // or hosting environments used by the developer.
  19. foreach (array('port') as $key => $value)
  20. {
  21. if (isset($config[$key]))
  22. {
  23. $dsn .= ";{$key}={$value}";
  24. }
  25. }
  26. $connection = new PDO($dsn, $username, $password, $this->options($config));
  27. if (isset($config['charset']))
  28. {
  29. $connection->prepare("SET NAMES '{$charset}'")->execute();
  30. }
  31. return $connection;
  32. }
  33. }