From 6e2797ffc0e9632b60acc5a66e52aeb44f0e2b78 Mon Sep 17 00:00:00 2001 From: Andrew O'Neil Date: Fri, 10 Feb 2017 10:20:16 +1100 Subject: [PATCH] Fixes for using dblib PDO driver. These fixes allow *nix environments to connect to SQL Server using the dblib PDO driver and the silverstripe mssql module. - Only set MYSQL_ATTR_INIT_COMMAND when using the mysql driver, this constant isn't defined if the mysql pdo driver isn't installed - Supress warnings on getting the server version, attributes aren't supported by the dblib driver - Explicitly check for errors on sql exec, checking the return value isn't reliable for statements with no return value (e.g. USE database) --- model/connect/PDOConnector.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/model/connect/PDOConnector.php b/model/connect/PDOConnector.php index f1007b2ca..e02480966 100644 --- a/model/connect/PDOConnector.php +++ b/model/connect/PDOConnector.php @@ -156,9 +156,10 @@ class PDOConnector extends DBConnector { if(!isset($charset)) { $charset = $connCharset; } - $options = array( - PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . $charset . ' COLLATE ' . $connCollation - ); + $options = array(); + if($parameters['driver'] == 'mysql') { + $options[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES ' . $charset . ' COLLATE ' . $connCollation; + } if(self::is_emulate_prepare()) { $options[PDO::ATTR_EMULATE_PREPARES] = true; } @@ -178,7 +179,7 @@ class PDOConnector extends DBConnector { } public function getVersion() { - return $this->pdoConnection->getAttribute(PDO::ATTR_SERVER_VERSION); + return @$this->pdoConnection->getAttribute(PDO::ATTR_SERVER_VERSION); } public function escapeString($value) { @@ -227,7 +228,7 @@ class PDOConnector extends DBConnector { $result = $this->pdoConnection->exec($sql); // Check for errors - if ($result !== false) { + if (!$this->hasError($result)) { return $this->rowCount = $result; }