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