diff --git a/README.md b/README.md index de27fc1..b70b9d4 100644 --- a/README.md +++ b/README.md @@ -8,19 +8,27 @@ Allows SilverStripe to use SQL Server databases. * Sean Harvey (Nickname: halkyon) + + * Damian Mooyman (@tractorcow) ## Requirements - * SilverStripe 3.0+ + * SilverStripe 4+ * SQL Server 2008, 2008 R2, or 2012. +`mssql` PHP api is no longer supported as of 2.0 + ### *nix - * PHP with mssql extension and [FreeTDS](http://freetds.org) +Linux support is only available via the PDO extension. This requires: + + * [dblib](http://www.php.net/manual/en/ref.pdo-dblib.php) + * [FreeTDS](http://freetds.org) ### Windows - * PHP with [SQL Server Driver for PHP](http://www.microsoft.com/en-us/download/details.aspx?id=20098) "sqlsrv" 3.0+ +On windows you can either connect via PDO or `sqlsrv`. Both options require the +[SQL Server Driver for PHP](https://msdn.microsoft.com/library/dn865013.aspx?f=255&MSPPError=-2147217396). "sqlsrv" 3.0+ Note: [SQL Server Express](http://www.microsoft.com/express/Database/) can also be used which is provided free by Microsoft. However, it has limitations such as 10GB maximum database storage. @@ -29,7 +37,7 @@ Note: [SQL Server Express](http://www.microsoft.com/express/Database/) can also These steps will install the latest SilverStripe stable, along with this module using [Composer](http://getcomposer.org/): * Install SilverStripe: `composer create-project silverstripe/installer /my/website/folder` - * Install module: `cd /my/website/folder && composer require silverstripe/mssql "*"` + * Install module: `cd /my/website/folder && composer require silverstripe/mssql ^2` * Open the SilverStripe installer by browsing to install.php, e.g. **http://localhost/silverstripe/install.php** * Select **SQL Server 2008+** in the database list and enter your SQL Server database details diff --git a/_register_database.php b/_register_database.php index db5de30..ac64146 100644 --- a/_register_database.php +++ b/_register_database.php @@ -2,11 +2,13 @@ // PDO connector for MS SQL Server /** @skipUpgrade */ +use SilverStripe\MSSQL\MSSQLDatabaseConfigurationHelper; + DatabaseAdapterRegistry::register(array( 'class' => 'MSSQLPDODatabase', 'title' => 'SQL Server 2008 (using PDO)', 'helperPath' => dirname(__FILE__).'/code/MSSQLDatabaseConfigurationHelper.php', - 'supported' => (class_exists('PDO') && in_array('sqlsrv', PDO::getAvailableDrivers())), + 'supported' => !!MSSQLDatabaseConfigurationHelper::getPDODriver(), 'missingExtensionText' => 'Either the PDO Extension or the SQL Server PDO Driver diff --git a/code/MSSQLDatabaseConfigurationHelper.php b/code/MSSQLDatabaseConfigurationHelper.php index 4b52bd6..8d25bef 100644 --- a/code/MSSQLDatabaseConfigurationHelper.php +++ b/code/MSSQLDatabaseConfigurationHelper.php @@ -65,8 +65,14 @@ protected function createConnection($databaseConfig, &$error) } return null; case 'MSSQLPDODatabase': + $driver = $this->getPDODriver(); + if (!$driver) { + $error = 'No supported PDO driver'; + return null; + } + // May throw a PDOException if fails - $conn = @new PDO('sqlsrv:Server='.$databaseConfig['server'], $databaseConfig['username'], $databaseConfig['password']); + $conn = @new PDO($driver.':Server='.$databaseConfig['server'], $databaseConfig['username'], $databaseConfig['password']); if ($conn) { return $conn; } else { @@ -83,6 +89,23 @@ protected function createConnection($databaseConfig, &$error) } } + /** + * Get supported PDO driver + * + * @return null + */ + public static function getPDODriver() { + if (!class_exists('PDO')) { + return null; + } + foreach(PDO::getAvailableDrivers() as $driver) { + if(in_array($driver, array('sqlsrv', 'dblib'))) { + return $driver; + } + } + return null; + } + /** * Helper function to quote a string value *