2010-02-02 00:26:22 +01:00
|
|
|
<?php
|
2013-04-03 06:19:26 +02:00
|
|
|
|
2010-02-02 00:26:22 +01:00
|
|
|
/**
|
|
|
|
* Specific support for SQL Azure databases running on Windows Azure.
|
2010-10-20 05:17:19 +02:00
|
|
|
* Currently only supports the SQLSRV driver from Microsoft.
|
2010-02-02 00:26:22 +01:00
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
* @package mssql
|
|
|
|
*/
|
|
|
|
class MSSQLAzureDatabase extends MSSQLDatabase {
|
2013-04-03 06:19:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* List of parameters used to create new Azure connections between databases
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $parameters = array();
|
2010-02-02 00:26:22 +01:00
|
|
|
|
2013-04-03 06:19:26 +02:00
|
|
|
public function fullTextEnabled() {
|
|
|
|
return false;
|
|
|
|
}
|
2010-02-02 00:26:22 +01:00
|
|
|
|
|
|
|
public function __construct($parameters) {
|
2010-02-02 03:41:52 +01:00
|
|
|
$this->connectDatabase($parameters);
|
2010-02-02 03:19:43 +01:00
|
|
|
}
|
2010-02-02 00:26:22 +01:00
|
|
|
|
2010-02-02 03:30:50 +01:00
|
|
|
/**
|
|
|
|
* Connect to a SQL Azure database with the given parameters.
|
|
|
|
* @param array $parameters Connection parameters set by environment
|
2013-04-03 06:19:26 +02:00
|
|
|
* - server: The server, eg, localhost
|
|
|
|
* - username: The username to log on with
|
|
|
|
* - password: The password to log on with
|
|
|
|
* - database: The database to connect to
|
|
|
|
* - windowsauthentication: Not supported for Azure
|
2010-02-02 03:30:50 +01:00
|
|
|
*/
|
2013-04-03 06:19:26 +02:00
|
|
|
protected function connect($parameters) {
|
|
|
|
$this->parameters = $parameters;
|
|
|
|
$this->connectDatabase($parameters['database']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Connect to a database using the provided parameters
|
|
|
|
*
|
|
|
|
* @param string $database
|
|
|
|
*/
|
|
|
|
protected function connectDatabase($database) {
|
|
|
|
$parameters = $this->parameters;
|
|
|
|
$parameters['database'] = $database;
|
|
|
|
$parameters['multipleactiveresultsets'] = 0;
|
|
|
|
|
|
|
|
// Ensure that driver is available (required by PDO)
|
|
|
|
if(empty($parameters['driver'])) {
|
|
|
|
$parameters['driver'] = $this->getDatabaseServer();
|
|
|
|
}
|
2010-02-02 03:19:43 +01:00
|
|
|
|
2013-04-03 06:19:26 +02:00
|
|
|
// Notify connector of parameters, instructing the connector
|
|
|
|
// to connect immediately to the Azure database
|
|
|
|
$this->connector->connect($parameters, true);
|
2010-02-02 00:26:22 +01:00
|
|
|
|
2013-04-03 06:19:26 +02:00
|
|
|
// Configure the connection
|
2010-02-02 03:19:43 +01:00
|
|
|
$this->query('SET QUOTED_IDENTIFIER ON');
|
|
|
|
$this->query('SET TEXTSIZE 2147483647');
|
|
|
|
}
|
2010-02-02 00:26:22 +01:00
|
|
|
|
2010-02-02 03:19:43 +01:00
|
|
|
/**
|
|
|
|
* Switches to the given database.
|
|
|
|
*
|
|
|
|
* IMPORTANT: SQL Azure doesn't support "USE", so we need
|
|
|
|
* to reinitialize the database connection with the requested
|
|
|
|
* database name.
|
2013-04-03 06:19:26 +02:00
|
|
|
* @see http://msdn.microsoft.com/en-us/library/windowsazure/ee336288.aspx
|
2010-02-02 03:19:43 +01:00
|
|
|
*
|
2013-04-03 06:19:26 +02:00
|
|
|
* @param type $name The database name to switch to
|
|
|
|
* @param type $create
|
|
|
|
* @param type $errorLevel
|
2010-02-02 03:19:43 +01:00
|
|
|
*/
|
2013-04-03 06:19:26 +02:00
|
|
|
public function selectDatabase($name, $create = false, $errorLevel = E_USER_ERROR) {
|
|
|
|
$this->fullTextEnabled = null;
|
|
|
|
if (!$this->schemaManager->databaseExists($name)) {
|
|
|
|
// Check DB creation permisson
|
|
|
|
if (!$create) {
|
|
|
|
if ($errorLevel !== false) {
|
|
|
|
user_error("Attempted to connect to non-existing database \"$name\"", $errorLevel);
|
|
|
|
}
|
|
|
|
// Unselect database
|
|
|
|
$this->connector->unloadDatabase();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$this->schemaManager->createDatabase($name);
|
|
|
|
}
|
|
|
|
$this->connectDatabase($name);
|
|
|
|
return true;
|
2010-02-02 00:26:22 +01:00
|
|
|
}
|
2013-04-03 06:19:26 +02:00
|
|
|
}
|