PostgresConnector.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace Illuminate\Database\Connectors;
  3. use PDO;
  4. class PostgresConnector extends Connector implements ConnectorInterface
  5. {
  6. /**
  7. * The default PDO connection options.
  8. *
  9. * @var array
  10. */
  11. protected $options = [
  12. PDO::ATTR_CASE => PDO::CASE_NATURAL,
  13. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  14. PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,
  15. PDO::ATTR_STRINGIFY_FETCHES => false,
  16. ];
  17. /**
  18. * Establish a database connection.
  19. *
  20. * @param array $config
  21. * @return \PDO
  22. */
  23. public function connect(array $config)
  24. {
  25. // First we'll create the basic DSN and connection instance connecting to the
  26. // using the configuration option specified by the developer. We will also
  27. // set the default character set on the connections to UTF-8 by default.
  28. $connection = $this->createConnection(
  29. $this->getDsn($config), $config, $this->getOptions($config)
  30. );
  31. $this->configureEncoding($connection, $config);
  32. // Next, we will check to see if a timezone has been specified in this config
  33. // and if it has we will issue a statement to modify the timezone with the
  34. // database. Setting this DB timezone is an optional configuration item.
  35. $this->configureTimezone($connection, $config);
  36. $this->configureSchema($connection, $config);
  37. // Postgres allows an application_name to be set by the user and this name is
  38. // used to when monitoring the application with pg_stat_activity. So we'll
  39. // determine if the option has been specified and run a statement if so.
  40. $this->configureApplicationName($connection, $config);
  41. return $connection;
  42. }
  43. /**
  44. * Set the connection character set and collation.
  45. *
  46. * @param \PDO $connection
  47. * @param array $config
  48. * @return void
  49. */
  50. protected function configureEncoding($connection, $config)
  51. {
  52. if (! isset($config['charset'])) {
  53. return;
  54. }
  55. $connection->prepare("set names '{$config['charset']}'")->execute();
  56. }
  57. /**
  58. * Set the timezone on the connection.
  59. *
  60. * @param \PDO $connection
  61. * @param array $config
  62. * @return void
  63. */
  64. protected function configureTimezone($connection, array $config)
  65. {
  66. if (isset($config['timezone'])) {
  67. $timezone = $config['timezone'];
  68. $connection->prepare("set time zone '{$timezone}'")->execute();
  69. }
  70. }
  71. /**
  72. * Set the schema on the connection.
  73. *
  74. * @param \PDO $connection
  75. * @param array $config
  76. * @return void
  77. */
  78. protected function configureSchema($connection, $config)
  79. {
  80. if (isset($config['schema'])) {
  81. $schema = $this->formatSchema($config['schema']);
  82. $connection->prepare("set search_path to {$schema}")->execute();
  83. }
  84. }
  85. /**
  86. * Format the schema for the DSN.
  87. *
  88. * @param array|string $schema
  89. * @return string
  90. */
  91. protected function formatSchema($schema)
  92. {
  93. if (is_array($schema)) {
  94. return '"'.implode('", "', $schema).'"';
  95. }
  96. return '"'.$schema.'"';
  97. }
  98. /**
  99. * Set the schema on the connection.
  100. *
  101. * @param \PDO $connection
  102. * @param array $config
  103. * @return void
  104. */
  105. protected function configureApplicationName($connection, $config)
  106. {
  107. if (isset($config['application_name'])) {
  108. $applicationName = $config['application_name'];
  109. $connection->prepare("set application_name to '$applicationName'")->execute();
  110. }
  111. }
  112. /**
  113. * Create a DSN string from a configuration.
  114. *
  115. * @param array $config
  116. * @return string
  117. */
  118. protected function getDsn(array $config)
  119. {
  120. // First we will create the basic DSN setup as well as the port if it is in
  121. // in the configuration options. This will give us the basic DSN we will
  122. // need to establish the PDO connections and return them back for use.
  123. extract($config, EXTR_SKIP);
  124. $host = isset($host) ? "host={$host};" : '';
  125. $dsn = "pgsql:{$host}dbname={$database}";
  126. // If a port was specified, we will add it to this Postgres DSN connections
  127. // format. Once we have done that we are ready to return this connection
  128. // string back out for usage, as this has been fully constructed here.
  129. if (isset($config['port'])) {
  130. $dsn .= ";port={$port}";
  131. }
  132. return $this->addSslOptions($dsn, $config);
  133. }
  134. /**
  135. * Add the SSL options to the DSN.
  136. *
  137. * @param string $dsn
  138. * @param array $config
  139. * @return string
  140. */
  141. protected function addSslOptions($dsn, array $config)
  142. {
  143. foreach (['sslmode', 'sslcert', 'sslkey', 'sslrootcert'] as $option) {
  144. if (isset($config[$option])) {
  145. $dsn .= ";{$option}={$config[$option]}";
  146. }
  147. }
  148. return $dsn;
  149. }
  150. }