1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class MSSqlSrvPDO |
4
|
|
|
* |
5
|
|
|
* @filesource MSSqlSrvPDO.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
|
|
|
use chillerlan\Settings\SettingsContainerInterface; |
19
|
|
|
use PDO; |
20
|
|
|
use Psr\{ |
21
|
|
|
Log\LoggerInterface, SimpleCache\CacheInterface |
22
|
|
|
}; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @property \PDO $db |
26
|
|
|
*/ |
27
|
|
|
class MSSqlSrvPDO extends PDODriverAbstract{ |
28
|
|
|
|
29
|
|
|
protected string $drivername = 'sqlsrv'; |
30
|
|
|
protected string $dialect = MSSQL::class; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* MSSqlSrvPDO constructor. |
34
|
|
|
* |
35
|
|
|
* @param \chillerlan\Settings\SettingsContainerInterface $options |
36
|
|
|
* @param \Psr\SimpleCache\CacheInterface|null $cache |
37
|
|
|
* @param \Psr\Log\LoggerInterface|null $log |
38
|
|
|
*/ |
39
|
|
|
public function __construct(SettingsContainerInterface $options, CacheInterface $cache = null, LoggerInterface $log = null){ |
40
|
|
|
unset($this->pdo_options[PDO::ATTR_EMULATE_PREPARES]); |
41
|
|
|
|
42
|
|
|
// @todo |
43
|
|
|
$this->pdo_options[PDO::SQLSRV_ATTR_QUERY_TIMEOUT] = $options->mssql_timeout; // doesn't seem to have an effect |
44
|
|
|
$this->pdo_options[PDO::SQLSRV_ATTR_FETCHES_NUMERIC_TYPE] = true; |
45
|
|
|
$this->pdo_options[PDO::SQLSRV_ATTR_ENCODING ] = PDO::SQLSRV_ENCODING_UTF8; |
46
|
|
|
|
47
|
|
|
parent::__construct($options, $cache); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** @inheritdoc */ |
51
|
|
|
protected function getDSN():string{ |
52
|
|
|
$dsn = $this->drivername; |
53
|
|
|
|
54
|
|
|
$dsn .= ':Server='.$this->options->host; |
55
|
|
|
|
56
|
|
|
if(is_numeric($this->options->port)){ |
57
|
|
|
$dsn .= ','.$this->options->port; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$dsn .= ';Database='.$this->options->database; |
61
|
|
|
|
62
|
|
|
return $dsn; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** @inheritdoc */ |
66
|
|
|
public function getClientInfo():string{ |
67
|
|
|
$info = $this->db->getAttribute(PDO::ATTR_CLIENT_VERSION); |
68
|
|
|
|
69
|
|
|
if(is_array($info) && !empty($info)){ |
70
|
|
|
return $info['DriverVer'].' - '.$info['ExtensionVer'].' - '.$info['DriverODBCVer']; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return 'disconnected, no info available'; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** @inheritdoc */ |
77
|
|
|
public function getServerInfo():?string{ |
78
|
|
|
$info = $this->db->getAttribute(PDO::ATTR_SERVER_INFO); |
79
|
|
|
|
80
|
|
|
if(is_array($info) && !empty($info)){ |
81
|
|
|
return PHP_OS.' - '.$info['SQLServerVersion'].' - '.$info['SQLServerName'].'/'.$info['CurrentDatabase']; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return 'disconnected, no info available'; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** @inheritdoc */ |
88
|
|
|
public function raw(string $sql, string $index = null, bool $assoc = null){ |
89
|
|
|
|
90
|
|
|
try{ |
91
|
|
|
return $this->raw_query($sql, $index, $assoc); |
92
|
|
|
} |
93
|
|
|
catch(\Exception $e){ |
94
|
|
|
return $this->silenceNonErrorException($e); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** @inheritdoc */ |
100
|
|
|
public function prepared(string $sql, array $values = null, string $index = null, bool $assoc = null){ |
101
|
|
|
|
102
|
|
|
try{ |
103
|
|
|
return $this->prepared_query($sql, $values, $index, $assoc); |
104
|
|
|
} |
105
|
|
|
catch(\Exception $e){ |
106
|
|
|
return $this->silenceNonErrorException($e); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param \Exception $e |
113
|
|
|
* |
114
|
|
|
* @return bool |
115
|
|
|
* @throws \chillerlan\Database\Drivers\DriverException |
116
|
|
|
*/ |
117
|
|
|
protected function silenceNonErrorException(\Exception $e){ |
118
|
|
|
$message = $e->getMessage(); |
119
|
|
|
|
120
|
|
|
// @todo silence. |
121
|
|
|
$x = explode(':', $message, 2); |
122
|
|
|
|
123
|
|
|
if(trim($x[0]) === 'SQLSTATE[IMSSP]'){ |
124
|
|
|
|
125
|
|
|
if(strpos(strtolower($x[1]), 'no fields') > 0){ |
126
|
|
|
return true; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
throw new DriverException('sql error: ['.get_called_class().'::raw()]'.$message); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
} |
135
|
|
|
|