Postgres.php 2.9 KB

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