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