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\MysqlDialectTrait;
18: use Cake\Database\Driver;
19: use Cake\Database\Query;
20: use Cake\Database\Statement\MysqlStatement;
21: use PDO;
22:
23: 24: 25:
26: class Mysql extends Driver
27: {
28:
29: use MysqlDialectTrait;
30:
31: 32: 33: 34: 35:
36: protected $_baseConfig = [
37: 'persistent' => true,
38: 'host' => 'localhost',
39: 'username' => 'root',
40: 'password' => '',
41: 'database' => 'cake',
42: 'port' => '3306',
43: 'flags' => [],
44: 'encoding' => 'utf8mb4',
45: 'timezone' => null,
46: 'init' => [],
47: ];
48:
49: 50: 51: 52: 53:
54: protected $_version;
55:
56: 57: 58: 59: 60:
61: protected $_supportsNativeJson;
62:
63: 64: 65: 66: 67:
68: public function connect()
69: {
70: if ($this->_connection) {
71: return true;
72: }
73: $config = $this->_config;
74:
75: if ($config['timezone'] === 'UTC') {
76: $config['timezone'] = '+0:00';
77: }
78:
79: if (!empty($config['timezone'])) {
80: $config['init'][] = sprintf("SET time_zone = '%s'", $config['timezone']);
81: }
82: if (!empty($config['encoding'])) {
83: $config['init'][] = sprintf('SET NAMES %s', $config['encoding']);
84: }
85:
86: $config['flags'] += [
87: PDO::ATTR_PERSISTENT => $config['persistent'],
88: PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
89: PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
90: ];
91:
92: if (!empty($config['ssl_key']) && !empty($config['ssl_cert'])) {
93: $config['flags'][PDO::MYSQL_ATTR_SSL_KEY] = $config['ssl_key'];
94: $config['flags'][PDO::MYSQL_ATTR_SSL_CERT] = $config['ssl_cert'];
95: }
96: if (!empty($config['ssl_ca'])) {
97: $config['flags'][PDO::MYSQL_ATTR_SSL_CA] = $config['ssl_ca'];
98: }
99:
100: if (empty($config['unix_socket'])) {
101: $dsn = "mysql:host={$config['host']};port={$config['port']};dbname={$config['database']};charset={$config['encoding']}";
102: } else {
103: $dsn = "mysql:unix_socket={$config['unix_socket']};dbname={$config['database']}";
104: }
105:
106: $this->_connect($dsn, $config);
107:
108: if (!empty($config['init'])) {
109: $connection = $this->getConnection();
110: foreach ((array)$config['init'] as $command) {
111: $connection->exec($command);
112: }
113: }
114:
115: return true;
116: }
117:
118: 119: 120: 121: 122:
123: public function enabled()
124: {
125: return in_array('mysql', PDO::getAvailableDrivers());
126: }
127:
128: 129: 130: 131: 132: 133:
134: public function prepare($query)
135: {
136: $this->connect();
137: $isObject = $query instanceof Query;
138: $statement = $this->_connection->prepare($isObject ? $query->sql() : $query);
139: $result = new MysqlStatement($statement, $this);
140: if ($isObject && $query->isBufferedResultsEnabled() === false) {
141: $result->bufferResults(false);
142: }
143:
144: return $result;
145: }
146:
147: 148: 149:
150: public function schema()
151: {
152: return $this->_config['database'];
153: }
154:
155: 156: 157:
158: public function supportsDynamicConstraints()
159: {
160: return true;
161: }
162:
163: 164: 165: 166: 167:
168: public function supportsNativeJson()
169: {
170: if ($this->_supportsNativeJson !== null) {
171: return $this->_supportsNativeJson;
172: }
173:
174: if ($this->_version === null) {
175: $this->_version = $this->_connection->getAttribute(PDO::ATTR_SERVER_VERSION);
176: }
177:
178: return $this->_supportsNativeJson = version_compare($this->_version, '5.7.0', '>=');
179: }
180: }
181: