|
1
|
|
|
<?php |
|
2
|
|
|
// +---------------------------------------------------------------------- |
|
|
|
|
|
|
3
|
|
|
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ] |
|
4
|
|
|
// +---------------------------------------------------------------------- |
|
5
|
|
|
// | Copyright (c) 2006-2012 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
|
|
|
|
|
12
|
|
|
namespace think\db\connector; |
|
13
|
|
|
|
|
14
|
|
|
use PDO; |
|
15
|
|
|
use think\db\Connection; |
|
16
|
|
|
use think\db\Query; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Sqlsrv数据库驱动 |
|
20
|
|
|
*/ |
|
|
|
|
|
|
21
|
|
|
class Sqlsrv extends Connection |
|
22
|
|
|
{ |
|
23
|
|
|
// PDO连接参数 |
|
24
|
|
|
protected $params = [ |
|
25
|
|
|
PDO::ATTR_CASE => PDO::CASE_NATURAL, |
|
26
|
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
|
27
|
|
|
PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL, |
|
28
|
|
|
PDO::ATTR_STRINGIFY_FETCHES => false, |
|
29
|
|
|
]; |
|
30
|
|
|
|
|
31
|
|
|
protected $builder = '\\think\\db\\builder\\Sqlsrv'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* 解析pdo连接的dsn信息 |
|
35
|
|
|
* @access protected |
|
36
|
|
|
* @param array $config 连接信息 |
|
37
|
|
|
* @return string |
|
38
|
|
|
*/ |
|
39
|
|
|
protected function parseDsn($config) |
|
40
|
|
|
{ |
|
41
|
|
|
$dsn = 'sqlsrv:Database=' . $config['database'] . ';Server=' . $config['hostname']; |
|
42
|
|
|
|
|
43
|
|
|
if (!empty($config['hostport'])) { |
|
44
|
|
|
$dsn .= ',' . $config['hostport']; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return $dsn; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* 取得数据表的字段信息 |
|
52
|
|
|
* @access public |
|
53
|
|
|
* @param string $tableName |
|
|
|
|
|
|
54
|
|
|
* @return array |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getFields($tableName) |
|
57
|
|
|
{ |
|
58
|
|
|
list($tableName) = explode(' ', $tableName); |
|
59
|
|
|
$tableNames = explode('.', $tableName); |
|
60
|
|
|
$tableName = isset($tableNames[1]) ? $tableNames[1] : $tableNames[0]; |
|
61
|
|
|
|
|
62
|
|
|
$sql = "SELECT column_name, data_type, column_default, is_nullable |
|
63
|
|
|
FROM information_schema.tables AS t |
|
64
|
|
|
JOIN information_schema.columns AS c |
|
65
|
|
|
ON t.table_catalog = c.table_catalog |
|
66
|
|
|
AND t.table_schema = c.table_schema |
|
67
|
|
|
AND t.table_name = c.table_name |
|
68
|
|
|
WHERE t.table_name = '$tableName'"; |
|
69
|
|
|
|
|
70
|
|
|
$pdo = $this->query($sql, [], false, true); |
|
71
|
|
|
$result = $pdo->fetchAll(PDO::FETCH_ASSOC); |
|
72
|
|
|
$info = []; |
|
73
|
|
|
|
|
74
|
|
|
if ($result) { |
|
75
|
|
|
foreach ($result as $key => $val) { |
|
76
|
|
|
$val = array_change_key_case($val); |
|
77
|
|
|
$info[$val['column_name']] = [ |
|
78
|
|
|
'name' => $val['column_name'], |
|
79
|
|
|
'type' => $val['data_type'], |
|
80
|
|
|
'notnull' => (bool) ('' === $val['is_nullable']), // not null is empty, null is yes |
|
81
|
|
|
'default' => $val['column_default'], |
|
82
|
|
|
'primary' => false, |
|
83
|
|
|
'autoinc' => false, |
|
84
|
|
|
]; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$sql = "SELECT column_name FROM information_schema.key_column_usage WHERE table_name='$tableName'"; |
|
89
|
|
|
|
|
90
|
|
|
// 调试开始 |
|
91
|
|
|
$this->debug(true); |
|
92
|
|
|
|
|
93
|
|
|
$pdo = $this->linkID->query($sql); |
|
94
|
|
|
|
|
95
|
|
|
// 调试结束 |
|
96
|
|
|
$this->debug(false, $sql); |
|
97
|
|
|
|
|
98
|
|
|
$result = $pdo->fetch(PDO::FETCH_ASSOC); |
|
99
|
|
|
|
|
100
|
|
|
if ($result) { |
|
101
|
|
|
$info[$result['column_name']]['primary'] = true; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $this->fieldCase($info); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* 取得数据表的字段信息 |
|
109
|
|
|
* @access public |
|
110
|
|
|
* @param string $dbName |
|
|
|
|
|
|
111
|
|
|
* @return array |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getTables($dbName = '') |
|
114
|
|
|
{ |
|
115
|
|
|
$sql = "SELECT TABLE_NAME |
|
116
|
|
|
FROM INFORMATION_SCHEMA.TABLES |
|
117
|
|
|
WHERE TABLE_TYPE = 'BASE TABLE' |
|
118
|
|
|
"; |
|
119
|
|
|
|
|
120
|
|
|
$pdo = $this->query($sql, [], false, true); |
|
121
|
|
|
$result = $pdo->fetchAll(PDO::FETCH_ASSOC); |
|
122
|
|
|
$info = []; |
|
123
|
|
|
|
|
124
|
|
|
foreach ($result as $key => $val) { |
|
125
|
|
|
$info[$key] = current($val); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return $info; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* 得到某个列的数组 |
|
133
|
|
|
* @access public |
|
134
|
|
|
* @param Query $query 查询对象 |
|
135
|
|
|
* @param string $field 字段名 多个字段用逗号分隔 |
|
136
|
|
|
* @param string $key 索引 |
|
137
|
|
|
* @return array |
|
138
|
|
|
*/ |
|
139
|
|
|
public function column(Query $query, $field, $key = '') |
|
140
|
|
|
{ |
|
141
|
|
|
$options = $query->getOptions(); |
|
142
|
|
|
|
|
143
|
|
|
if (empty($options['fetch_sql']) && !empty($options['cache'])) { |
|
144
|
|
|
// 判断查询缓存 |
|
145
|
|
|
$cache = $options['cache']; |
|
146
|
|
|
|
|
147
|
|
|
$guid = is_string($cache['key']) ? $cache['key'] : $this->getCacheKey($query, $field); |
|
148
|
|
|
|
|
149
|
|
|
$result = Container::get('cache')->get($guid); |
|
|
|
|
|
|
150
|
|
|
|
|
151
|
|
|
if (false !== $result) { |
|
152
|
|
|
return $result; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
if (isset($options['field'])) { |
|
157
|
|
|
$query->removeOption('field'); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
if (is_null($field)) { |
|
|
|
|
|
|
161
|
|
|
$field = '*'; |
|
162
|
|
|
} elseif ($key && '*' != $field) { |
|
163
|
|
|
$field = $key . ',' . $field; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
if (is_string($field)) { |
|
|
|
|
|
|
167
|
|
|
$field = array_map('trim', explode(',', $field)); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
$query->setOption('field', $field); |
|
171
|
|
|
|
|
172
|
|
|
// 生成查询SQL |
|
173
|
|
|
$sql = $this->builder->select($query); |
|
174
|
|
|
|
|
175
|
|
|
$bind = $query->getBind(); |
|
176
|
|
|
|
|
177
|
|
|
if (!empty($options['fetch_sql'])) { |
|
178
|
|
|
// 获取实际执行的SQL语句 |
|
179
|
|
|
return $this->getRealSql($sql, $bind); |
|
|
|
|
|
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
// 执行查询操作 |
|
183
|
|
|
$pdo = $this->query($sql, $bind, $options['master'], true); |
|
184
|
|
|
|
|
185
|
|
|
if (1 == $pdo->columnCount()) { |
|
186
|
|
|
$result = $pdo->fetchAll(PDO::FETCH_COLUMN); |
|
187
|
|
|
} else { |
|
188
|
|
|
$resultSet = $pdo->fetchAll(PDO::FETCH_ASSOC); |
|
189
|
|
|
|
|
190
|
|
|
if ('*' == $field && $key) { |
|
191
|
|
|
$result = array_column($resultSet, null, $key); |
|
192
|
|
|
} elseif ($resultSet) { |
|
193
|
|
|
$fields = array_keys($resultSet[0]); |
|
194
|
|
|
$count = count($fields); |
|
195
|
|
|
$key1 = array_shift($fields); |
|
196
|
|
|
$key2 = $fields ? array_shift($fields) : ''; |
|
197
|
|
|
$key = $key ?: $key1; |
|
198
|
|
|
|
|
199
|
|
|
if (strpos($key, '.')) { |
|
200
|
|
|
list($alias, $key) = explode('.', $key); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
if (3 == $count) { |
|
204
|
|
|
$column = $key2; |
|
205
|
|
|
} elseif ($count < 3) { |
|
206
|
|
|
$column = $key1; |
|
207
|
|
|
} else { |
|
208
|
|
|
$column = null; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
$result = array_column($resultSet, $column, $key); |
|
212
|
|
|
} else { |
|
213
|
|
|
$result = []; |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
if (isset($cache) && isset($guid)) { |
|
218
|
|
|
// 缓存数据 |
|
219
|
|
|
$this->cacheData($guid, $result, $cache); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
return $result; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* SQL性能分析 |
|
227
|
|
|
* @access protected |
|
228
|
|
|
* @param string $sql |
|
|
|
|
|
|
229
|
|
|
* @return array |
|
230
|
|
|
*/ |
|
231
|
|
|
protected function getExplain($sql) |
|
232
|
|
|
{ |
|
233
|
|
|
return []; |
|
234
|
|
|
} |
|
235
|
|
|
} |
|
236
|
|
|
|