PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 1.3.1
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v1.3.1
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / core / Db / Adapter / Pdo / Mssql.php
matomo / app / core / Db / Adapter / Pdo Last commit date
Mssql.php 6 years ago Mysql.php 6 years ago Pgsql.php 6 years ago
Mssql.php
267 lines
1 <?php
2 /**
3 * Piwik - free/libre analytics platform
4 *
5 * @link https://matomo.org
6 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
7 *
8 */
9 namespace Piwik\Db\Adapter\Pdo;
10
11 use Exception;
12 use PDO;
13 use PDOException;
14 use Piwik\Config;
15 use Piwik\Db\AdapterInterface;
16 use Piwik\Piwik;
17 use Zend_Db;
18 use Zend_Db_Adapter_Exception;
19 use Zend_Db_Adapter_Pdo_Mssql;
20 use Zend_Db_Profiler;
21
22 /**
23 */
24 class Mssql extends Zend_Db_Adapter_Pdo_Mssql implements AdapterInterface
25 {
26 /**
27 * Returns connection handle
28 *
29 * @throws Zend_Db_Adapter_Exception
30 * @return resource
31 */
32 public function getConnection()
33 {
34 // if we already have a PDO object, no need to re-connect.
35 if ($this->_connection) {
36 return $this->_connection;
37 }
38
39 $this->_pdoType = "sqlsrv";
40 // get the dsn first, because some adapters alter the $_pdoType
41 //$dsn = $this->_dsn();
42
43 // check for PDO extension
44 if (!extension_loaded('pdo')) {
45 /**
46 * @see Zend_Db_Adapter_Exception
47 */
48 throw new \Zend_Db_Adapter_Exception('The PDO extension is required for this adapter but the extension is not loaded');
49 }
50
51 // check the PDO driver is available
52 if (!in_array($this->_pdoType, PDO::getAvailableDrivers())) {
53 /**
54 * @see Zend_Db_Adapter_Exception
55 */
56 throw new \Zend_Db_Adapter_Exception('The ' . $this->_pdoType . ' driver is not currently installed');
57 }
58
59 // create PDO connection
60 $q = $this->_profiler->queryStart('connect', Zend_Db_Profiler::CONNECT);
61
62 // add the persistence flag if we find it in our config array
63 if (isset($this->_config['persistent']) && ($this->_config['persistent'] == true)) {
64 $this->_config['driver_options'][PDO::ATTR_PERSISTENT] = true;
65 }
66
67 try {
68 $serverName = $this->_config["host"];
69 $database = $this->_config["dbname"];
70 if (is_null($database)) {
71 $database = 'master';
72 }
73 $uid = $this->_config['username'];
74 $pwd = $this->_config['password'];
75 if ($this->_config["port"] != "") {
76 $serverName = $serverName . "," . $this->_config["port"];
77 }
78
79 $this->_connection = new PDO("sqlsrv:$serverName", $uid, $pwd, array('Database' => $database));
80
81 if ($this->_connection === false) {
82 die(self::FormatErrors(sqlsrv_errors()));
83 }
84
85 /*
86 $this->_connection = new PDO(
87 $dsn,
88 $this->_config['username'],
89 $this->_config['password'],
90 $this->_config['driver_options']
91 );
92 */
93
94 $this->_profiler->queryEnd($q);
95
96 // set the PDO connection to perform case-folding on array keys, or not
97 $this->_connection->setAttribute(PDO::ATTR_CASE, $this->_caseFolding);
98 $this->_connection->setAttribute(PDO::SQLSRV_ENCODING_UTF8, true);
99
100 // always use exceptions.
101 $this->_connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
102
103 return $this->_connection;
104 } catch (PDOException $e) {
105 /**
106 * @see Zend_Db_Adapter_Exception
107 */
108 throw new \Zend_Db_Adapter_Exception($e->getMessage(), $e->getCode(), $e);
109 }
110 }
111
112 /**
113 * Reset the configuration variables in this adapter.
114 */
115 public function resetConfig()
116 {
117 $this->_config = array();
118 }
119
120 /**
121 * Return default port.
122 *
123 * @return int
124 */
125 public static function getDefaultPort()
126 {
127 return 1433;
128 }
129
130 /**
131 * Check MSSQL version
132 *
133 * @throws Exception
134 */
135 public function checkServerVersion()
136 {
137 $serverVersion = $this->getServerVersion();
138 $requiredVersion = Config::getInstance()->General['minimum_mssql_version'];
139
140 if (version_compare($serverVersion, $requiredVersion) === -1) {
141 throw new Exception(Piwik::translate('General_ExceptionDatabaseVersion', array('MSSQL', $serverVersion, $requiredVersion)));
142 }
143 }
144
145 /**
146 * Returns the Mssql server version
147 *
148 * @return null|string
149 */
150 public function getServerVersion()
151 {
152 try {
153 $stmt = $this->query("SELECT CAST(SERVERPROPERTY('productversion') as VARCHAR) as productversion");
154 $result = $stmt->fetchAll(Zend_Db::FETCH_NUM);
155 if (count($result)) {
156 return $result[0][0];
157 }
158 } catch (PDOException $e) {
159 }
160
161 return null;
162 }
163
164 /**
165 * Check client version compatibility against database server
166 *
167 * @throws Exception
168 */
169 public function checkClientVersion()
170 {
171 $serverVersion = $this->getServerVersion();
172 $clientVersion = $this->getClientVersion();
173
174 if (version_compare($serverVersion, '10') >= 0
175 && version_compare($clientVersion, '10') < 0
176 ) {
177 throw new Exception(Piwik::translate('General_ExceptionIncompatibleClientServerVersions', array('MSSQL', $clientVersion, $serverVersion)));
178 }
179 }
180
181 /**
182 * Returns true if this adapter's required extensions are enabled
183 *
184 * @return bool
185 */
186 public static function isEnabled()
187 {
188 return extension_loaded('PDO') && extension_loaded('pdo_sqlsrv');
189 }
190
191 /**
192 * Returns true if this adapter supports blobs as fields
193 *
194 * @return bool
195 */
196 public function hasBlobDataType()
197 {
198 return true;
199 }
200
201 /**
202 * Returns true if this adapter supports bulk loading
203 *
204 * @return bool
205 */
206 public function hasBulkLoader()
207 {
208 /**
209 * BULK INSERT doesn't have a way to escape a terminator that appears in a value
210 *
211 * @link http://msdn.microsoft.com/en-us/library/ms188365.aspx
212 */
213 return false;
214 }
215
216 /**
217 * Test error number
218 *
219 * @param Exception $e
220 * @param string $errno
221 * @return bool
222 */
223 public function isErrNo($e, $errno)
224 {
225 if (preg_match('/(?:\[|\s)([0-9]{4})(?:\]|\s)/', $e->getMessage(), $match)) {
226 return $match[1] == $errno;
227 }
228
229 return false;
230 }
231
232 /**
233 * Is the connection character set equal to utf8?
234 *
235 * @return bool
236 */
237 public function isConnectionUTF8()
238 {
239 //check the getconnection, it's specified on the connection string.
240 return true;
241 }
242
243 /**
244 * Retrieve client version in PHP style
245 *
246 * @throws Exception
247 * @return string
248 */
249 public function getClientVersion()
250 {
251 $this->_connect();
252 try {
253 $version = $this->_connection->getAttribute(PDO::ATTR_CLIENT_VERSION);
254 $requiredVersion = Config::getInstance()->General['minimum_mssql_client_version'];
255 if (version_compare($version['DriverVer'], $requiredVersion) === -1) {
256 throw new Exception(Piwik::translate('General_ExceptionDatabaseVersion', array('MSSQL', $version['DriverVer'], $requiredVersion)));
257 } else {
258 return $version['DriverVer'];
259 }
260 } catch (PDOException $e) {
261 // In case of the driver doesn't support getting attributes
262 }
263
264 return null;
265 }
266 }
267