1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15: namespace Cake\Database\Driver;
16:
17: use Cake\Database\Dialect\PostgresDialectTrait;
18: use Cake\Database\Driver;
19: use PDO;
20:
21: 22: 23:
24: class Postgres extends Driver
25: {
26: use PostgresDialectTrait;
27:
28: 29: 30: 31: 32:
33: protected $_baseConfig = [
34: 'persistent' => true,
35: 'host' => 'localhost',
36: 'username' => 'root',
37: 'password' => '',
38: 'database' => 'cake',
39: 'schema' => 'public',
40: 'port' => 5432,
41: 'encoding' => 'utf8',
42: 'timezone' => null,
43: 'flags' => [],
44: 'init' => [],
45: ];
46:
47: 48: 49: 50: 51:
52: public function connect()
53: {
54: if ($this->_connection) {
55: return true;
56: }
57: $config = $this->_config;
58: $config['flags'] += [
59: PDO::ATTR_PERSISTENT => $config['persistent'],
60: PDO::ATTR_EMULATE_PREPARES => false,
61: PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
62: ];
63: if (empty($config['unix_socket'])) {
64: $dsn = "pgsql:host={$config['host']};port={$config['port']};dbname={$config['database']}";
65: } else {
66: $dsn = "pgsql:dbname={$config['database']}";
67: }
68:
69: $this->_connect($dsn, $config);
70: $this->_connection = $connection = $this->getConnection();
71: if (!empty($config['encoding'])) {
72: $this->setEncoding($config['encoding']);
73: }
74:
75: if (!empty($config['schema'])) {
76: $this->setSchema($config['schema']);
77: }
78:
79: if (!empty($config['timezone'])) {
80: $config['init'][] = sprintf('SET timezone = %s', $connection->quote($config['timezone']));
81: }
82:
83: foreach ($config['init'] as $command) {
84: $connection->exec($command);
85: }
86:
87: return true;
88: }
89:
90: 91: 92: 93: 94:
95: public function enabled()
96: {
97: return in_array('pgsql', PDO::getAvailableDrivers(), true);
98: }
99:
100: 101: 102: 103: 104: 105:
106: public function setEncoding($encoding)
107: {
108: $this->connect();
109: $this->_connection->exec('SET NAMES ' . $this->_connection->quote($encoding));
110: }
111:
112: 113: 114: 115: 116: 117: 118:
119: public function setSchema($schema)
120: {
121: $this->connect();
122: $this->_connection->exec('SET search_path TO ' . $this->_connection->quote($schema));
123: }
124:
125: 126: 127:
128: public function supportsDynamicConstraints()
129: {
130: return true;
131: }
132: }
133: