|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Class MSSqlSrv |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource MSSqlSrv.php |
|
6
|
|
|
* @created 28.06.2017 |
|
7
|
|
|
* @package chillerlan\Database\Drivers |
|
8
|
|
|
* @author Smiley <[email protected]> |
|
9
|
|
|
* @copyright 2017 Smiley |
|
10
|
|
|
* @license MIT |
|
11
|
|
|
* |
|
12
|
|
|
* @noinspection PhpComposerExtensionStubsInspection |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace chillerlan\Database\Drivers; |
|
16
|
|
|
|
|
17
|
|
|
use chillerlan\Database\Dialects\MSSQL; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @property resource $db |
|
21
|
|
|
*/ |
|
22
|
|
|
class MSSqlSrv extends DriverAbstract{ |
|
23
|
|
|
|
|
24
|
|
|
protected string $dialect = MSSQL::class; |
|
25
|
|
|
|
|
26
|
|
|
/** @inheritdoc */ |
|
27
|
|
|
protected function raw_query(string $sql, string $index = null, bool $assoc = null){ |
|
28
|
|
|
return $this->__getResult(sqlsrv_query($this->db, $sql), $index, $assoc); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** @inheritdoc */ |
|
32
|
|
|
protected function prepared_query(string $sql, array $values = null, string $index = null, bool $assoc = null){ |
|
33
|
|
|
return $this->__getResult(sqlsrv_query($this->db, $sql, $values), $index, $assoc); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** @inheritdoc */ |
|
37
|
|
|
protected function multi_query(string $sql, array $values){ |
|
38
|
|
|
$r = []; |
|
39
|
|
|
|
|
40
|
|
|
// @todo: sqlsrv_prepare/sqlsrv_execute |
|
41
|
|
|
foreach($values as $row){ |
|
42
|
|
|
$r[] = $this->prepared_query($sql, $row); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
foreach($r as $result){ |
|
46
|
|
|
|
|
47
|
|
|
if(!$result){ |
|
48
|
|
|
return false; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return true; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** @inheritdoc */ |
|
57
|
|
|
protected function multi_callback_query(string $sql, iterable $data, $callback){ |
|
58
|
|
|
$r = []; |
|
59
|
|
|
|
|
60
|
|
|
// @todo: sqlsrv_prepare/sqlsrv_execute |
|
61
|
|
|
foreach($data as $i => $row){ |
|
62
|
|
|
$r[] = $this->prepared_query($sql, call_user_func_array($callback, [$row, $i])); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
foreach($r as $result){ |
|
66
|
|
|
|
|
67
|
|
|
if(!$result){ |
|
68
|
|
|
return false; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return true; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param $result |
|
78
|
|
|
* @param string|null $index |
|
79
|
|
|
* @param bool $assoc |
|
80
|
|
|
* |
|
81
|
|
|
* @return bool|\chillerlan\Database\Result |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function __getResult($result, string $index = null, bool $assoc = null){ |
|
84
|
|
|
|
|
85
|
|
|
if(is_bool($result)){ |
|
86
|
|
|
return $result; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$r = parent::getResult('sqlsrv_fetch_array', [$result, ($assoc ?? true) ? SQLSRV_FETCH_ASSOC : SQLSRV_FETCH_NUMERIC], $index, $assoc); |
|
90
|
|
|
|
|
91
|
|
|
sqlsrv_free_stmt($result); |
|
92
|
|
|
|
|
93
|
|
|
return $r; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** @inheritdoc */ |
|
97
|
|
|
public function connect():DriverInterface{ |
|
98
|
|
|
|
|
99
|
|
|
if(gettype($this->db) === 'resource'){ |
|
100
|
|
|
return $this; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$options = []; |
|
104
|
|
|
|
|
105
|
|
|
$host = $this->options->host; |
|
106
|
|
|
|
|
107
|
|
|
if(is_numeric($this->options->port)){ |
|
108
|
|
|
$host .= ', '.$this->options->port; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
if($this->options->database){ |
|
112
|
|
|
$options['Database'] = $this->options->database; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
if($this->options->username){ |
|
116
|
|
|
$options['UID'] = $this->options->username; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
if($this->options->password){ |
|
120
|
|
|
$options['PWD'] = $this->options->password; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
if($this->options->mssql_timeout){ |
|
124
|
|
|
$options['LoginTimeout'] = $this->options->mssql_timeout; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
if($this->options->mssql_charset){ |
|
128
|
|
|
$options['CharacterSet'] = $this->options->mssql_charset; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
if($this->options->mssql_encrypt){ |
|
132
|
|
|
$options['Encrypt'] = $this->options->mssql_encrypt; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
$this->db = sqlsrv_connect($host, $options); |
|
136
|
|
|
|
|
137
|
|
|
if(!$this->db){ |
|
138
|
|
|
$errors = sqlsrv_errors(); |
|
139
|
|
|
|
|
140
|
|
|
if(is_array($errors) && isset($errors['SQLSTATE'], $errors['code'], $errors['message'])){ |
|
141
|
|
|
throw new DriverException('db error: [MSSqlSrv]: [SQLSTATE '.$errors['SQLSTATE'].'] ('.$errors['code'].') '.$errors['message']); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
throw new DriverException('db error: [MSSqlSrv]: could not connect: '.print_r($errors, true)); |
|
|
|
|
|
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
return $this; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** @inheritdoc */ |
|
151
|
|
|
public function disconnect():bool{ |
|
152
|
|
|
|
|
153
|
|
|
if(gettype($this->db) === 'resource'){ |
|
154
|
|
|
return sqlsrv_close($this->db); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
return true; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** @inheritdoc */ |
|
161
|
|
|
public function getClientInfo():string{ |
|
162
|
|
|
|
|
163
|
|
|
if(gettype($this->db) === 'resource'){ |
|
164
|
|
|
$info = sqlsrv_client_info($this->db); |
|
165
|
|
|
|
|
166
|
|
|
return $info['DriverVer'].' - '.$info['ExtensionVer'].' - '.$info['DriverODBCVer']; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
return 'disconnected, no info available'; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** @inheritdoc */ |
|
173
|
|
|
public function getServerInfo():?string{ |
|
174
|
|
|
|
|
175
|
|
|
if(gettype($this->db) === 'resource'){ |
|
176
|
|
|
$info = sqlsrv_server_info($this->db); |
|
177
|
|
|
|
|
178
|
|
|
return PHP_OS.' - '.$info['SQLServerVersion'].' - '.$info['SQLServerName'].'/'.$info['CurrentDatabase']; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
return 'disconnected, no info available'; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** @inheritdoc */ |
|
185
|
|
|
protected function __escape(string $data):string { |
|
186
|
|
|
return $data; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
} |
|
190
|
|
|
|