Mysql.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. /**
  21. * 解析pdo连接的dsn信息
  22. * @access protected
  23. * @param array $config 连接信息
  24. * @return string
  25. */
  26. protected function parseDsn($config)
  27. {
  28. $dsn = 'mysql:dbname=' . $config['database'] . ';host=' . $config['hostname'];
  29. if (!empty($config['hostport'])) {
  30. $dsn .= ';port=' . $config['hostport'];
  31. } elseif (!empty($config['socket'])) {
  32. $dsn .= ';unix_socket=' . $config['socket'];
  33. }
  34. if (!empty($config['charset'])) {
  35. $dsn .= ';charset=' . $config['charset'];
  36. }
  37. return $dsn;
  38. }
  39. /**
  40. * 取得数据表的字段信息
  41. * @access public
  42. * @param string $tableName
  43. * @return array
  44. */
  45. public function getFields($tableName)
  46. {
  47. $this->initConnect(true);
  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. // 调试开始
  57. $this->debug(true);
  58. $pdo = $this->linkID->query($sql);
  59. // 调试结束
  60. $this->debug(false, $sql);
  61. $result = $pdo->fetchAll(PDO::FETCH_ASSOC);
  62. $info = [];
  63. if ($result) {
  64. foreach ($result as $key => $val) {
  65. $val = array_change_key_case($val);
  66. $info[$val['field']] = [
  67. 'name' => $val['field'],
  68. 'type' => $val['type'],
  69. 'notnull' => (bool) ('' === $val['null']), // not null is empty, null is yes
  70. 'default' => $val['default'],
  71. 'primary' => (strtolower($val['key']) == 'pri'),
  72. 'autoinc' => (strtolower($val['extra']) == 'auto_increment'),
  73. ];
  74. }
  75. }
  76. return $this->fieldCase($info);
  77. }
  78. /**
  79. * 取得数据库的表信息
  80. * @access public
  81. * @param string $dbName
  82. * @return array
  83. */
  84. public function getTables($dbName = '')
  85. {
  86. $this->initConnect(true);
  87. $sql = !empty($dbName) ? 'SHOW TABLES FROM ' . $dbName : 'SHOW TABLES ';
  88. // 调试开始
  89. $this->debug(true);
  90. $pdo = $this->linkID->query($sql);
  91. // 调试结束
  92. $this->debug(false, $sql);
  93. $result = $pdo->fetchAll(PDO::FETCH_ASSOC);
  94. $info = [];
  95. foreach ($result as $key => $val) {
  96. $info[$key] = current($val);
  97. }
  98. return $info;
  99. }
  100. /**
  101. * SQL性能分析
  102. * @access protected
  103. * @param string $sql
  104. * @return array
  105. */
  106. protected function getExplain($sql)
  107. {
  108. $pdo = $this->linkID->query("EXPLAIN " . $sql);
  109. $result = $pdo->fetch(PDO::FETCH_ASSOC);
  110. $result = array_change_key_case($result);
  111. if (isset($result['extra'])) {
  112. if (strpos($result['extra'], 'filesort') || strpos($result['extra'], 'temporary')) {
  113. Log::record('SQL:' . $this->queryStr . '[' . $result['extra'] . ']', 'warn');
  114. }
  115. }
  116. return $result;
  117. }
  118. protected function supportSavepoint()
  119. {
  120. return true;
  121. }
  122. }