Skip to content

Commit

Permalink
Merge 367fc17 into 34fb105
Browse files Browse the repository at this point in the history
  • Loading branch information
wilr committed Oct 13, 2020
2 parents 34fb105 + 367fc17 commit 85662e8
Show file tree
Hide file tree
Showing 11 changed files with 343 additions and 241 deletions.
64 changes: 40 additions & 24 deletions README.md
@@ -1,22 +1,17 @@
# SQL Server Database Module

Allows SilverStripe to use SQL Server databases.
Allows SilverStripe to use SQL Server databases including SQL databases on Azure.

[![Build status](https://ci.appveyor.com/api/projects/status/hep0l5kbhu64n7l3/branch/master?svg=true)](https://ci.appveyor.com/project/sminnee/silverstripe-mssql-nwvfq/branch/master)

## Maintainer Contact

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

* Damian Mooyman (@tractorcow)
[![Build status](https://ci.appveyor.com/api/projects/status/hep0l5kbhu64n7l3/branch/master?svg=true)](https://ci.appveyor.com/project/silverstripe/silverstripe-mssql/branch/master)
[![Version](http://img.shields.io/packagist/v/silverstripe/mssql.svg?style=flat-square)](https://packagist.org/packages/silverstripe/mssql)
[![License](http://img.shields.io/packagist/l/silverstripe/mssql.svg?style=flat-square)](LICENSE)

## Requirements

* SilverStripe 4+
* SQL Server 2008, 2008 R2, or 2012.

`mssql` PHP api is no longer supported as of 2.0
`mssql` PHP api is no longer supported as of 2.0.

### *nix

Expand All @@ -30,16 +25,29 @@ Linux support is only available via the PDO extension. This requires:
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.
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.

## Installation

These steps will install the latest SilverStripe stable, along with this module using [Composer](http://getcomposer.org/):
```
composer require silverstripe/mssql ^2
```

The following environment variables will need to be set:

```
SS_DATABASE_CLASS="MSSQLAzureDatabase" # or: `MSSQLDatabase` or `MSSQLPDODatabase`
SS_DATABASE_SERVER=""
SS_DATABASE_NAME=""
SS_DATABASE_USERNAME=""
SS_DATABASE_PASSWORD=""
```

Note on OSX / Linux machines you will need to install the ODBC drivers and the PHP extension `sqlsrv` or `pdo_sqlsrv`

* Install SilverStripe: `composer create-project silverstripe/installer /my/website/folder`
* 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
* https://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/install-microsoft-odbc-driver-sql-server-macos
* https://docs.microsoft.com/en-us/sql/connect/php/installation-tutorial-linux-mac

## Troubleshooting

Expand All @@ -49,12 +57,18 @@ A: Please ensure you have enabled TCP access using **SQL Server Configuration Ma

*Q: I just installed SQL Server, but it says that it cannot connect*

A: Sometimes SQL Server will be installed as a non-default instance name, e.g. "SQLExpress" instead of "MSSQLSERVER" (the default.)
If this is the case, you'll need to declare the instance name when setting the server in your PHP database configuration. For example: **(local)\SQLExpress**. The first part before the slash indicates the server host, or IP address. In this case, (local) indicates localhost, which is the same server PHP is running on. The second part is the SQL Server instance name to connect to.
A: Sometimes SQL Server will be installed as a non-default instance name, e.g. "SQLExpress" instead of "MSSQLSERVER"
(the default.) If this is the case, you'll need to declare the instance name when setting the server in your PHP
database configuration. For example: **(local)\SQLExpress**. The first part before the slash indicates the server host,
or IP address. In this case, (local) indicates localhost, which is the same server PHP is running on. The second part
is the SQL Server instance name to connect to.

*Q: I'm getting unicode SQL Server errors connecting to SQL Server database (e.g. Unicode data in a Unicode-only collation or ntext data cannot be sent to clients using DB-Library (such as ISQL) or ODBC version 3.7 or earlier)*
*Q: I'm getting unicode SQL Server errors connecting to SQL Server database (e.g. Unicode data in a Unicode-only
collation or ntext data cannot be sent to clients using DB-Library (such as ISQL) or ODBC version 3.7 or earlier)*

A: If you are using FreeTDS make sure you're using TDS version 8.0 in **freetds.conf**. If on Windows, ensure you use the [SQL Server Driver for PHP](http://www.microsoft.com/downloads/en/details.aspx?displaylang=en&FamilyID=ccdf728b-1ea0-48a8-a84a-5052214caad9) and **NOT** the mssql drivers provided by PHP.
A: If you are using FreeTDS make sure you're using TDS version 8.0 in **freetds.conf**. If on Windows, ensure you use
the [SQL Server Driver for PHP](http://www.microsoft.com/downloads/en/details.aspx?displaylang=en&FamilyID=ccdf728b-1ea0-48a8-a84a-5052214caad9)
and **NOT** the mssql drivers provided by PHP.

*Q: Using FreeTDS I can't connect to my SQL Server database. An error in PHP says the server doesn't exist*

Expand All @@ -69,10 +83,12 @@ Then you can use "myserver" (the bit in square brackets above) as the server nam
Note that if you're running Macports, the file is located in **/opt/local/etc/freetds/freetds.conf**.

Alternatively, if you don't want to keep adding more entries to the freetds.conf to nominate more SQL Server locations,
you can instead use the full the host/ip and port combination, such as "myserver:1433" (1433 being the default SQL Server port.)
and ensure the "tds version = 8.0" is set globally in the freetds.conf file.
you can instead use the full the host/ip and port combination, such as "myserver:1433" (1433 being the default SQL
Server port.) and ensure the "tds version = 8.0" is set globally in the freetds.conf file.

**Note**: Use *tabs* not spaces when editing freetds.conf, otherwise it will not load the configuration you have specified!
**Note**: Use *tabs* not spaces when editing freetds.conf, otherwise it will not load the configuration you have
specified!

**Note**: Certain distributions of Linux use [SELinux](http://fedoraproject.org/wiki/SELinux) which could block access to your SQL Server database. A rule may need to be added to allow this traffic through.
**Note**: Certain distributions of Linux use [SELinux](http://fedoraproject.org/wiki/SELinux) which could block access
to your SQL Server database. A rule may need to be added to allow this traffic through.

10 changes: 5 additions & 5 deletions _register_database.php
Expand Up @@ -9,12 +9,12 @@
'class' => 'MSSQLPDODatabase',
'module' => 'mssql',
'title' => 'SQL Server 2008 (using PDO)',
'helperPath' => __DIR__.'/code/MSSQLDatabaseConfigurationHelper.php',
'helperPath' => __DIR__.'/src/MSSQLDatabaseConfigurationHelper.php',
'helperClass' => MSSQLDatabaseConfigurationHelper::class,
'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>
'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>
are unavailable. Please install or enable these and refresh this page.'
));

Expand All @@ -24,7 +24,7 @@
'class' => 'MSSQLDatabase',
'module' => 'mssql',
'title' => 'SQL Server 2008 (using sqlsrv)',
'helperPath' => __DIR__.'/code/MSSQLDatabaseConfigurationHelper.php',
'helperPath' => __DIR__.'/src/MSSQLDatabaseConfigurationHelper.php',
'helperClass' => MSSQLDatabaseConfigurationHelper::class,
'supported' => function_exists('sqlsrv_connect'),
'missingExtensionText' =>
Expand All @@ -45,7 +45,7 @@
'class' => 'MSSQLAzureDatabase',
'module' => 'mssql',
'title' => 'MS Azure Database (using sqlsrv)',
'helperPath' => __DIR__.'/code/MSSQLDatabaseConfigurationHelper.php',
'helperPath' => __DIR__.'/src/MSSQLDatabaseConfigurationHelper.php',
'helperClass' => MSSQLDatabaseConfigurationHelper::class,
'supported' => function_exists('sqlsrv_connect'),
'missingExtensionText' =>
Expand Down
67 changes: 36 additions & 31 deletions composer.json
@@ -1,35 +1,40 @@
{
"name": "silverstripe/mssql",
"description": "Adds MSSQL support to SilverStripe",
"type": "silverstripe-vendormodule",
"keywords": ["silverstripe", "mssql", "database"],
"authors": [
{
"name": "Sam Minnee",
"email": "sam@silverstripe.com"
},
{
"name": "Sean Harvey",
"email": "sean@silverstripe.com"
}
],
"require": {
"silverstripe/framework": "^4"
},
"suggest": {
"name": "silverstripe/mssql",
"description": "Adds MSSQL support to SilverStripe",
"type": "silverstripe-vendormodule",
"license": "BSD-3-Clause",
"keywords": ["silverstripe", "mssql", "database"],
"authors": [
{
"name": "Sam Minnee",
"email": "sam@silverstripe.com"
},
{
"name": "Sean Harvey",
"email": "sean@silverstripe.com"
}
],
"require": {
"silverstripe/framework": "^4"
},
"suggest": {
"ext-sqlsrv": "Required to support MSSQLDatabase as the server type",
"ext-pdo_sqlsrv": "Required to support MSSQLPDODatabase as the server type"
},
"extra": {
"branch-alias": {
"dev-master": "3.x-dev"
}
},
"autoload": {
"psr-4": {
"SilverStripe\\MSSQL\\": "code/"
}
},
"prefer-stable": true,
"minimum-stability": "dev"
},
"scripts": {
"lint": "phpcs src/ tests/php/",
"lint-clean": "phpcbf src/ tests/php/"
},
"extra": {
"branch-alias": {
"dev-master": "3.x-dev"
}
},
"autoload": {
"psr-4": {
"SilverStripe\\MSSQL\\": "src/"
}
},
"prefer-stable": true,
"minimum-stability": "dev"
}
16 changes: 7 additions & 9 deletions code/MSSQLAzureDatabase.php → src/MSSQLAzureDatabase.php
Expand Up @@ -9,15 +9,14 @@
* Some important things about SQL Azure:
*
* Selecting a database is not supported.
*
* In order to change the database currently in use, you need to connect to
* the database using the "Database" parameter with sqlsrv_connect()
*
* Multiple active result sets are not supported. This means you can't
* have two query results open at once.
*
* Fulltext indexes are not supported.
*
* @author Sean Harvey <sean at silverstripe dot com>
*/
class MSSQLAzureDatabase extends MSSQLDatabase
{
Expand All @@ -27,18 +26,13 @@ class MSSQLAzureDatabase extends MSSQLDatabase
*
* @var array
*/
protected $parameters = array();
protected $parameters = [];

public function fullTextEnabled()
{
return false;
}

public function __construct($parameters)
{
$this->connectDatabase($parameters);
}

/**
* Connect to a SQL Azure database with the given parameters.
* @param array $parameters Connection parameters set by environment
Expand All @@ -63,7 +57,8 @@ protected function connectDatabase($database)
{
$parameters = $this->parameters;
$parameters['database'] = $database;
$parameters['multipleactiveresultsets'] = 0;
$parameters['returndatesasstrings'] = 1;
$parameters['multipleactiveresultsets'] = 1;

// Ensure that driver is available (required by PDO)
if (empty($parameters['driver'])) {
Expand Down Expand Up @@ -95,6 +90,7 @@ protected function connectDatabase($database)
public function selectDatabase($name, $create = false, $errorLevel = E_USER_ERROR)
{
$this->fullTextEnabled = null;

if (!$this->schemaManager->databaseExists($name)) {
// Check DB creation permisson
if (!$create) {
Expand All @@ -107,7 +103,9 @@ public function selectDatabase($name, $create = false, $errorLevel = E_USER_ERRO
}
$this->schemaManager->createDatabase($name);
}

$this->connectDatabase($name);

return true;
}
}
36 changes: 2 additions & 34 deletions code/MSSQLDatabase.php → src/MSSQLDatabase.php
Expand Up @@ -15,40 +15,6 @@

/**
* Microsoft SQL Server 2008+ connector class.
*
* <h2>Connecting using Windows</h2>
*
* If you've got your website running on Windows, it's highly recommended you
* use Microsoft SQL Server Driver for PHP "sqlsrv".
*
* A complete guide to installing a Windows IIS + PHP + SQL Server web stack can be
* found here: http://doc.silverstripe.org/installation-on-windows-server-manual-iis
*
* @see http://sqlsrvphp.codeplex.com/
*
* <h2>Connecting using Linux or Mac OS X</h2>
*
* The following commands assume you used the default package manager
* to install PHP with the operating system.
*
* Debian, and Ubuntu:
* <code>apt-get install php5-sybase</code>
*
* Fedora, CentOS and RedHat:
* <code>yum install php-mssql</code>
*
* Mac OS X (MacPorts):
* <code>port install php5-mssql</code>
*
* These packages will install the mssql extension for PHP, as well
* as FreeTDS, which will let you connect to SQL Server.
*
* More information available in the SilverStripe developer wiki:
* @see http://doc.silverstripe.org/modules:mssql
* @see http://doc.silverstripe.org/installation-on-windows-server-manual-iis
*
* References:
* @see http://freetds.org
*/
class MSSQLDatabase extends Database
{
Expand Down Expand Up @@ -485,6 +451,7 @@ public function transactionRollback($savepoint = false)
if (!$this->transactionNesting) {
return false;
}

--$this->transactionNesting;
if ($this->transactionNesting > 0) {
$this->transactionRollback('NESTEDTRANSACTION' . $this->transactionNesting);
Expand Down Expand Up @@ -538,6 +505,7 @@ protected function inspectQuery($sql)
{
// Any DDL discards transactions.
$isDDL = $this->getConnector()->isQueryDDL($sql);

if ($isDDL) {
$this->resetTransactionNesting();
}
Expand Down
Expand Up @@ -45,9 +45,12 @@ protected function createConnection($databaseConfig, &$error)
// Azure has additional parameter requirements
if ($this->isAzure($databaseConfig)) {
$parameters['database'] = $databaseConfig['database'];
$parameters['multipleactiveresultsets'] = 0;
$parameters['multipleactiveresultsets'] = 1;
$parameters['returndatesasstrings'] = 1;
}

$conn = @sqlsrv_connect($databaseConfig['server'], $parameters);

if ($conn) {
return $conn;
}
Expand Down Expand Up @@ -96,11 +99,13 @@ public static function getPDODriver() {
if (!class_exists('PDO')) {
return null;
}

foreach(PDO::getAvailableDrivers() as $driver) {
if(in_array($driver, array('sqlsrv', 'dblib'))) {
if (in_array($driver, array('sqlsrv', 'dblib'))) {
return $driver;
}
}

return null;
}

Expand All @@ -122,6 +127,7 @@ protected function quote($conn, $value)
} else {
user_error('Invalid database connection', E_USER_ERROR);
}

return null;
}

Expand Down Expand Up @@ -150,6 +156,7 @@ protected function query($conn, $sql)
}
}
}

return $items;
}

Expand Down Expand Up @@ -256,6 +263,7 @@ public function requireDatabaseAlterPermissions($databaseConfig)
// Make sure to select the current database when checking permission against this database
$this->query($conn, "USE \"{$databaseConfig['database']}\"");
}

$permissions = $this->query($conn, "select COUNT(*) from sys.fn_my_permissions(NULL,'DATABASE') WHERE permission_name like 'create table';");
$success = $permissions[0] > 0;
}
Expand Down

0 comments on commit 85662e8

Please sign in to comment.