Skip to content

Commit

Permalink
Merge pull request #35 from tractorcow/pulls/docs
Browse files Browse the repository at this point in the history
Updated docs
  • Loading branch information
Sean Harvey committed Aug 16, 2016
2 parents 9309cf3 + 97d8750 commit 551ccfd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
16 changes: 12 additions & 4 deletions README.md
Expand Up @@ -8,19 +8,27 @@ Allows SilverStripe to use SQL Server databases.

* Sean Harvey (Nickname: halkyon)
<sean (at) silverstripe (dot) com>

* 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.

Expand All @@ -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

Expand Down
4 changes: 3 additions & 1 deletion _register_database.php
Expand Up @@ -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 <a href="http://www.php.net/manual/en/book.pdo.php">PDO Extension</a> or
the <a href="http://www.php.net/manual/en/ref.pdo-sqlsrv.php">SQL Server PDO Driver</a>
Expand Down
25 changes: 24 additions & 1 deletion code/MSSQLDatabaseConfigurationHelper.php
Expand Up @@ -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 {
Expand All @@ -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
*
Expand Down

0 comments on commit 551ccfd

Please sign in to comment.