PostgresConnector.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. $charset = $config['charset'];
  53. $connection->prepare("set names '$charset'")->execute();
  54. }
  55. /**
  56. * Set the timezone on the connection.
  57. *
  58. * @param \PDO $connection
  59. * @param array $config
  60. * @return void
  61. */
  62. protected function configureTimezone($connection, array $config)
  63. {
  64. if (isset($config['timezone'])) {
  65. $timezone = $config['timezone'];
  66. $connection->prepare("set time zone '{$timezone}'")->execute();
  67. }
  68. }
  69. /**
  70. * Set the schema on the connection.
  71. *
  72. * @param \PDO $connection
  73. * @param array $config
  74. * @return void
  75. */
  76. protected function configureSchema($connection, $config)
  77. {
  78. if (isset($config['schema'])) {
  79. $schema = $this->formatSchema($config['schema']);
  80. $connection->prepare("set search_path to {$schema}")->execute();
  81. }
  82. }
  83. /**
  84. * Format the schema for the DSN.
  85. *
  86. * @param array|string $schema
  87. * @return string
  88. */
  89. protected function formatSchema($schema)
  90. {
  91. if (is_array($schema)) {
  92. return '"'.implode('", "', $schema).'"';
  93. }
  94. return '"'.$schema.'"';
  95. }
  96. /**
  97. * Set the schema on the connection.
  98. *
  99. * @param \PDO $connection
  100. * @param array $config
  101. * @return void
  102. */
  103. protected function configureApplicationName($connection, $config)
  104. {
  105. if (isset($config['application_name'])) {
  106. $applicationName = $config['application_name'];
  107. $connection->prepare("set application_name to '$applicationName'")->execute();
  108. }
  109. }
  110. /**
  111. * Create a DSN string from a configuration.
  112. *
  113. * @param array $config
  114. * @return string
  115. */
  116. protected function getDsn(array $config)
  117. {
  118. // First we will create the basic DSN setup as well as the port if it is in
  119. // in the configuration options. This will give us the basic DSN we will
  120. // need to establish the PDO connections and return them back for use.
  121. extract($config, EXTR_SKIP);
  122. $host = isset($host) ? "host={$host};" : '';
  123. $dsn = "pgsql:{$host}dbname={$database}";
  124. // If a port was specified, we will add it to this Postgres DSN connections
  125. // format. Once we have done that we are ready to return this connection
  126. // string back out for usage, as this has been fully constructed here.
  127. if (isset($config['port'])) {
  128. $dsn .= ";port={$port}";
  129. }
  130. return $this->addSslOptions($dsn, $config);
  131. }
  132. /**
  133. * Add the SSL options to the DSN.
  134. *
  135. * @param string $dsn
  136. * @param array $config
  137. * @return string
  138. */
  139. protected function addSslOptions($dsn, array $config)
  140. {
  141. foreach (['sslmode', 'sslcert', 'sslkey', 'sslrootcert'] as $option) {
  142. if (isset($config[$option])) {
  143. $dsn .= ";{$option}={$config[$option]}";
  144. }
  145. }
  146. return $dsn;
  147. }
  148. }