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