Mysql.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2017 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. $dsn = 'mysql:dbname=' . $config['database'] . ';host=' . $config['hostname'];
  30. if (!empty($config['hostport'])) {
  31. $dsn .= ';port=' . $config['hostport'];
  32. } elseif (!empty($config['socket'])) {
  33. $dsn .= ';unix_socket=' . $config['socket'];
  34. }
  35. if (!empty($config['charset'])) {
  36. $dsn .= ';charset=' . $config['charset'];
  37. }
  38. return $dsn;
  39. }
  40. /**
  41. * 取得数据表的字段信息
  42. * @access public
  43. * @param string $tableName
  44. * @return array
  45. */
  46. public function getFields($tableName)
  47. {
  48. list($tableName) = explode(' ', $tableName);
  49. if (false === strpos($tableName, '`')) {
  50. if (strpos($tableName, '.')) {
  51. $tableName = str_replace('.', '`.`', $tableName);
  52. }
  53. $tableName = '`' . $tableName . '`';
  54. }
  55. $sql = 'SHOW COLUMNS FROM ' . $tableName;
  56. $pdo = $this->query($sql, [], false, true);
  57. $result = $pdo->fetchAll(PDO::FETCH_ASSOC);
  58. $info = [];
  59. if ($result) {
  60. foreach ($result as $key => $val) {
  61. $val = array_change_key_case($val);
  62. $info[$val['field']] = [
  63. 'name' => $val['field'],
  64. 'type' => $val['type'],
  65. 'notnull' => (bool) ('' === $val['null']), // not null is empty, null is yes
  66. 'default' => $val['default'],
  67. 'primary' => (strtolower($val['key']) == 'pri'),
  68. 'autoinc' => (strtolower($val['extra']) == 'auto_increment'),
  69. ];
  70. }
  71. }
  72. return $this->fieldCase($info);
  73. }
  74. /**
  75. * 取得数据库的表信息
  76. * @access public
  77. * @param string $dbName
  78. * @return array
  79. */
  80. public function getTables($dbName = '')
  81. {
  82. $sql = !empty($dbName) ? 'SHOW TABLES FROM ' . $dbName : 'SHOW TABLES ';
  83. $pdo = $this->query($sql, [], false, true);
  84. $result = $pdo->fetchAll(PDO::FETCH_ASSOC);
  85. $info = [];
  86. foreach ($result as $key => $val) {
  87. $info[$key] = current($val);
  88. }
  89. return $info;
  90. }
  91. /**
  92. * SQL性能分析
  93. * @access protected
  94. * @param string $sql
  95. * @return array
  96. */
  97. protected function getExplain($sql)
  98. {
  99. $pdo = $this->linkID->query("EXPLAIN " . $sql);
  100. $result = $pdo->fetch(PDO::FETCH_ASSOC);
  101. $result = array_change_key_case($result);
  102. if (isset($result['extra'])) {
  103. if (strpos($result['extra'], 'filesort') || strpos($result['extra'], 'temporary')) {
  104. Log::record('SQL:' . $this->queryStr . '[' . $result['extra'] . ']', 'warn');
  105. }
  106. }
  107. return $result;
  108. }
  109. protected function supportSavepoint()
  110. {
  111. return true;
  112. }
  113. }