Mysql.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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\db\Query;
  15. /**
  16. * mysql数据库驱动
  17. */
  18. class Mysql extends Connection
  19. {
  20. protected $builder = '\\think\\db\\builder\\Mysql';
  21. /**
  22. * 初始化
  23. * @access protected
  24. * @return void
  25. */
  26. protected function initialize()
  27. {
  28. // Point类型支持
  29. Query::extend('point', function ($query, $field, $value = null, $fun = 'GeomFromText', $type = 'POINT') {
  30. if (!is_null($value)) {
  31. $query->data($field, ['point', $value, $fun, $type]);
  32. } else {
  33. if (is_string($field)) {
  34. $field = explode(',', $field);
  35. }
  36. $query->setOption('point', $field);
  37. }
  38. return $query;
  39. });
  40. }
  41. /**
  42. * 解析pdo连接的dsn信息
  43. * @access protected
  44. * @param array $config 连接信息
  45. * @return string
  46. */
  47. protected function parseDsn($config)
  48. {
  49. if (!empty($config['socket'])) {
  50. $dsn = 'mysql:unix_socket=' . $config['socket'];
  51. } elseif (!empty($config['hostport'])) {
  52. $dsn = 'mysql:host=' . $config['hostname'] . ';port=' . $config['hostport'];
  53. } else {
  54. $dsn = 'mysql:host=' . $config['hostname'];
  55. }
  56. $dsn .= ';dbname=' . $config['database'];
  57. if (!empty($config['charset'])) {
  58. $dsn .= ';charset=' . $config['charset'];
  59. }
  60. return $dsn;
  61. }
  62. /**
  63. * 取得数据表的字段信息
  64. * @access public
  65. * @param string $tableName
  66. * @return array
  67. */
  68. public function getFields($tableName)
  69. {
  70. list($tableName) = explode(' ', $tableName);
  71. if (false === strpos($tableName, '`')) {
  72. if (strpos($tableName, '.')) {
  73. $tableName = str_replace('.', '`.`', $tableName);
  74. }
  75. $tableName = '`' . $tableName . '`';
  76. }
  77. $sql = 'SHOW COLUMNS FROM ' . $tableName;
  78. $pdo = $this->query($sql, [], false, true);
  79. $result = $pdo->fetchAll(PDO::FETCH_ASSOC);
  80. $info = [];
  81. if ($result) {
  82. foreach ($result as $key => $val) {
  83. $val = array_change_key_case($val);
  84. $info[$val['field']] = [
  85. 'name' => $val['field'],
  86. 'type' => $val['type'],
  87. 'notnull' => (bool) ('' === $val['null']), // not null is empty, null is yes
  88. 'default' => $val['default'],
  89. 'primary' => (strtolower($val['key']) == 'pri'),
  90. 'autoinc' => (strtolower($val['extra']) == 'auto_increment'),
  91. ];
  92. }
  93. }
  94. return $this->fieldCase($info);
  95. }
  96. /**
  97. * 取得数据库的表信息
  98. * @access public
  99. * @param string $dbName
  100. * @return array
  101. */
  102. public function getTables($dbName = '')
  103. {
  104. $sql = !empty($dbName) ? 'SHOW TABLES FROM ' . $dbName : 'SHOW TABLES ';
  105. $pdo = $this->query($sql, [], false, true);
  106. $result = $pdo->fetchAll(PDO::FETCH_ASSOC);
  107. $info = [];
  108. foreach ($result as $key => $val) {
  109. $info[$key] = current($val);
  110. }
  111. return $info;
  112. }
  113. /**
  114. * SQL性能分析
  115. * @access protected
  116. * @param string $sql
  117. * @return array
  118. */
  119. protected function getExplain($sql)
  120. {
  121. $pdo = $this->linkID->prepare("EXPLAIN " . $this->queryStr);
  122. foreach ($this->bind as $key => $val) {
  123. // 占位符
  124. $param = is_int($key) ? $key + 1 : ':' . $key;
  125. if (is_array($val)) {
  126. if (PDO::PARAM_INT == $val[1] && '' === $val[0]) {
  127. $val[0] = 0;
  128. } elseif (self::PARAM_FLOAT == $val[1]) {
  129. $val[0] = is_string($val[0]) ? (float) $val[0] : $val[0];
  130. $val[1] = PDO::PARAM_STR;
  131. }
  132. $result = $pdo->bindValue($param, $val[0], $val[1]);
  133. } else {
  134. $result = $pdo->bindValue($param, $val);
  135. }
  136. }
  137. $pdo->execute();
  138. $result = $pdo->fetch(PDO::FETCH_ASSOC);
  139. $result = array_change_key_case($result);
  140. if (isset($result['extra'])) {
  141. if (strpos($result['extra'], 'filesort') || strpos($result['extra'], 'temporary')) {
  142. $this->log('SQL:' . $this->queryStr . '[' . $result['extra'] . ']', 'warn');
  143. }
  144. }
  145. return $result;
  146. }
  147. protected function supportSavepoint()
  148. {
  149. return true;
  150. }
  151. /**
  152. * 启动XA事务
  153. * @access public
  154. * @param string $xid XA事务id
  155. * @return void
  156. */
  157. public function startTransXa($xid)
  158. {
  159. $this->initConnect(true);
  160. if (!$this->linkID) {
  161. return false;
  162. }
  163. $this->execute("XA START '$xid'");
  164. }
  165. /**
  166. * 预编译XA事务
  167. * @access public
  168. * @param string $xid XA事务id
  169. * @return void
  170. */
  171. public function prepareXa($xid)
  172. {
  173. $this->initConnect(true);
  174. $this->execute("XA END '$xid'");
  175. $this->execute("XA PREPARE '$xid'");
  176. }
  177. /**
  178. * 提交XA事务
  179. * @access public
  180. * @param string $xid XA事务id
  181. * @return void
  182. */
  183. public function commitXa($xid)
  184. {
  185. $this->initConnect(true);
  186. $this->execute("XA COMMIT '$xid'");
  187. }
  188. /**
  189. * 回滚XA事务
  190. * @access public
  191. * @param string $xid XA事务id
  192. * @return void
  193. */
  194. public function rollbackXa($xid)
  195. {
  196. $this->initConnect(true);
  197. $this->execute("XA ROLLBACK '$xid'");
  198. }
  199. }