Mysql.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Database\Driver;
  16. use Cake\Database\Dialect\MysqlDialectTrait;
  17. use Cake\Database\Driver;
  18. use Cake\Database\Query;
  19. use Cake\Database\Statement\MysqlStatement;
  20. use PDO;
  21. /**
  22. * Class Mysql
  23. */
  24. class Mysql extends Driver
  25. {
  26. use MysqlDialectTrait;
  27. /**
  28. * Base configuration settings for MySQL driver
  29. *
  30. * @var array
  31. */
  32. protected $_baseConfig = [
  33. 'persistent' => true,
  34. 'host' => 'localhost',
  35. 'username' => 'root',
  36. 'password' => '',
  37. 'database' => 'cake',
  38. 'port' => '3306',
  39. 'flags' => [],
  40. 'encoding' => 'utf8mb4',
  41. 'timezone' => null,
  42. 'init' => [],
  43. ];
  44. /**
  45. * The server version
  46. *
  47. * @var string
  48. */
  49. protected $_version;
  50. /**
  51. * Whether or not the server supports native JSON
  52. *
  53. * @var bool
  54. */
  55. protected $_supportsNativeJson;
  56. /**
  57. * Establishes a connection to the database server
  58. *
  59. * @return bool true on success
  60. */
  61. public function connect()
  62. {
  63. if ($this->_connection) {
  64. return true;
  65. }
  66. $config = $this->_config;
  67. if ($config['timezone'] === 'UTC') {
  68. $config['timezone'] = '+0:00';
  69. }
  70. if (!empty($config['timezone'])) {
  71. $config['init'][] = sprintf("SET time_zone = '%s'", $config['timezone']);
  72. }
  73. if (!empty($config['encoding'])) {
  74. $config['init'][] = sprintf('SET NAMES %s', $config['encoding']);
  75. }
  76. $config['flags'] += [
  77. PDO::ATTR_PERSISTENT => $config['persistent'],
  78. PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
  79. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  80. ];
  81. if (!empty($config['ssl_key']) && !empty($config['ssl_cert'])) {
  82. $config['flags'][PDO::MYSQL_ATTR_SSL_KEY] = $config['ssl_key'];
  83. $config['flags'][PDO::MYSQL_ATTR_SSL_CERT] = $config['ssl_cert'];
  84. }
  85. if (!empty($config['ssl_ca'])) {
  86. $config['flags'][PDO::MYSQL_ATTR_SSL_CA] = $config['ssl_ca'];
  87. }
  88. if (empty($config['unix_socket'])) {
  89. $dsn = "mysql:host={$config['host']};port={$config['port']};dbname={$config['database']};charset={$config['encoding']}";
  90. } else {
  91. $dsn = "mysql:unix_socket={$config['unix_socket']};dbname={$config['database']}";
  92. }
  93. $this->_connect($dsn, $config);
  94. if (!empty($config['init'])) {
  95. $connection = $this->getConnection();
  96. foreach ((array)$config['init'] as $command) {
  97. $connection->exec($command);
  98. }
  99. }
  100. return true;
  101. }
  102. /**
  103. * Returns whether php is able to use this driver for connecting to database
  104. *
  105. * @return bool true if it is valid to use this driver
  106. */
  107. public function enabled()
  108. {
  109. return in_array('mysql', PDO::getAvailableDrivers());
  110. }
  111. /**
  112. * Prepares a sql statement to be executed
  113. *
  114. * @param string|\Cake\Database\Query $query The query to prepare.
  115. * @return \Cake\Database\StatementInterface
  116. */
  117. public function prepare($query)
  118. {
  119. $this->connect();
  120. $isObject = $query instanceof Query;
  121. $statement = $this->_connection->prepare($isObject ? $query->sql() : $query);
  122. $result = new MysqlStatement($statement, $this);
  123. if ($isObject && $query->isBufferedResultsEnabled() === false) {
  124. $result->bufferResults(false);
  125. }
  126. return $result;
  127. }
  128. /**
  129. * {@inheritDoc}
  130. */
  131. public function schema()
  132. {
  133. return $this->_config['database'];
  134. }
  135. /**
  136. * {@inheritDoc}
  137. */
  138. public function supportsDynamicConstraints()
  139. {
  140. return true;
  141. }
  142. /**
  143. * Returns true if the server supports native JSON columns
  144. *
  145. * @return bool
  146. */
  147. public function supportsNativeJson()
  148. {
  149. if ($this->_supportsNativeJson !== null) {
  150. return $this->_supportsNativeJson;
  151. }
  152. if ($this->_version === null) {
  153. $this->_version = $this->_connection->getAttribute(PDO::ATTR_SERVER_VERSION);
  154. }
  155. return $this->_supportsNativeJson = version_compare($this->_version, '5.7.0', '>=');
  156. }
  157. }