Postgres.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Database\Driver;
  16. use Cake\Database\Dialect\PostgresDialectTrait;
  17. use Cake\Database\Driver;
  18. use PDO;
  19. /**
  20. * Class Postgres
  21. */
  22. class Postgres extends Driver
  23. {
  24. use PostgresDialectTrait;
  25. /**
  26. * Base configuration settings for Postgres driver
  27. *
  28. * @var array
  29. */
  30. protected $_baseConfig = [
  31. 'persistent' => true,
  32. 'host' => 'localhost',
  33. 'username' => 'root',
  34. 'password' => '',
  35. 'database' => 'cake',
  36. 'schema' => 'public',
  37. 'port' => 5432,
  38. 'encoding' => 'utf8',
  39. 'timezone' => null,
  40. 'flags' => [],
  41. 'init' => [],
  42. ];
  43. /**
  44. * Establishes a connection to the database server
  45. *
  46. * @return bool true on success
  47. */
  48. public function connect()
  49. {
  50. if ($this->_connection) {
  51. return true;
  52. }
  53. $config = $this->_config;
  54. $config['flags'] += [
  55. PDO::ATTR_PERSISTENT => $config['persistent'],
  56. PDO::ATTR_EMULATE_PREPARES => false,
  57. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
  58. ];
  59. if (empty($config['unix_socket'])) {
  60. $dsn = "pgsql:host={$config['host']};port={$config['port']};dbname={$config['database']}";
  61. } else {
  62. $dsn = "pgsql:dbname={$config['database']}";
  63. }
  64. $this->_connect($dsn, $config);
  65. $this->_connection = $connection = $this->getConnection();
  66. if (!empty($config['encoding'])) {
  67. $this->setEncoding($config['encoding']);
  68. }
  69. if (!empty($config['schema'])) {
  70. $this->setSchema($config['schema']);
  71. }
  72. if (!empty($config['timezone'])) {
  73. $config['init'][] = sprintf('SET timezone = %s', $connection->quote($config['timezone']));
  74. }
  75. foreach ($config['init'] as $command) {
  76. $connection->exec($command);
  77. }
  78. return true;
  79. }
  80. /**
  81. * Returns whether php is able to use this driver for connecting to database
  82. *
  83. * @return bool true if it is valid to use this driver
  84. */
  85. public function enabled()
  86. {
  87. return in_array('pgsql', PDO::getAvailableDrivers());
  88. }
  89. /**
  90. * Sets connection encoding
  91. *
  92. * @param string $encoding The encoding to use.
  93. * @return void
  94. */
  95. public function setEncoding($encoding)
  96. {
  97. $this->connect();
  98. $this->_connection->exec('SET NAMES ' . $this->_connection->quote($encoding));
  99. }
  100. /**
  101. * Sets connection default schema, if any relation defined in a query is not fully qualified
  102. * postgres will fallback to looking the relation into defined default schema
  103. *
  104. * @param string $schema The schema names to set `search_path` to.
  105. * @return void
  106. */
  107. public function setSchema($schema)
  108. {
  109. $this->connect();
  110. $this->_connection->exec('SET search_path TO ' . $this->_connection->quote($schema));
  111. }
  112. /**
  113. * {@inheritDoc}
  114. */
  115. public function supportsDynamicConstraints()
  116. {
  117. return true;
  118. }
  119. }