Mysql.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\db\connector;
  12. use PDO;
  13. use think\db\Connection;
  14. use think\Log;
  15. /**
  16. * mysql数据库驱动
  17. */
  18. class Mysql extends Connection
  19. {
  20. protected $builder = '\\think\\db\\builder\\Mysql';
  21. /**
  22. * 解析pdo连接的dsn信息
  23. * @access protected
  24. * @param array $config 连接信息
  25. * @return string
  26. */
  27. protected function parseDsn($config)
  28. {
  29. if (!empty($config['socket'])) {
  30. $dsn = 'mysql:unix_socket=' . $config['socket'];
  31. } elseif (!empty($config['hostport'])) {
  32. $dsn = 'mysql:host=' . $config['hostname'] . ';port=' . $config['hostport'];
  33. } else {
  34. $dsn = 'mysql:host=' . $config['hostname'];
  35. }
  36. $dsn .= ';dbname=' . $config['database'];
  37. if (!empty($config['charset'])) {
  38. $dsn .= ';charset=' . $config['charset'];
  39. }
  40. return $dsn;
  41. }
  42. /**
  43. * 取得数据表的字段信息
  44. * @access public
  45. * @param string $tableName
  46. * @return array
  47. */
  48. public function getFields($tableName)
  49. {
  50. list($tableName) = explode(' ', $tableName);
  51. if (false === strpos($tableName, '`')) {
  52. if (strpos($tableName, '.')) {
  53. $tableName = str_replace('.', '`.`', $tableName);
  54. }
  55. $tableName = '`' . $tableName . '`';
  56. }
  57. $sql = 'SHOW COLUMNS FROM ' . $tableName;
  58. $pdo = $this->query($sql, [], false, true);
  59. $result = $pdo->fetchAll(PDO::FETCH_ASSOC);
  60. $info = [];
  61. if ($result) {
  62. foreach ($result as $key => $val) {
  63. $val = array_change_key_case($val);
  64. $info[$val['field']] = [
  65. 'name' => $val['field'],
  66. 'type' => $val['type'],
  67. 'notnull' => (bool) ('' === $val['null']), // not null is empty, null is yes
  68. 'default' => $val['default'],
  69. 'primary' => (strtolower($val['key']) == 'pri'),
  70. 'autoinc' => (strtolower($val['extra']) == 'auto_increment'),
  71. ];
  72. }
  73. }
  74. return $this->fieldCase($info);
  75. }
  76. /**
  77. * 取得数据库的表信息
  78. * @access public
  79. * @param string $dbName
  80. * @return array
  81. */
  82. public function getTables($dbName = '')
  83. {
  84. $sql = !empty($dbName) ? 'SHOW TABLES FROM ' . $dbName : 'SHOW TABLES ';
  85. $pdo = $this->query($sql, [], false, true);
  86. $result = $pdo->fetchAll(PDO::FETCH_ASSOC);
  87. $info = [];
  88. foreach ($result as $key => $val) {
  89. $info[$key] = current($val);
  90. }
  91. return $info;
  92. }
  93. /**
  94. * SQL性能分析
  95. * @access protected
  96. * @param string $sql
  97. * @return array
  98. */
  99. protected function getExplain($sql)
  100. {
  101. $pdo = $this->linkID->query("EXPLAIN " . $sql);
  102. $result = $pdo->fetch(PDO::FETCH_ASSOC);
  103. $result = array_change_key_case($result);
  104. if (isset($result['extra'])) {
  105. if (strpos($result['extra'], 'filesort') || strpos($result['extra'], 'temporary')) {
  106. Log::record('SQL:' . $this->queryStr . '[' . $result['extra'] . ']', 'warn');
  107. }
  108. }
  109. return $result;
  110. }
  111. protected function supportSavepoint()
  112. {
  113. return true;
  114. }
  115. }