From 2a1cee6f3a39f9e81b1b8f5cfe62adf8ba7d0799 Mon Sep 17 00:00:00 2001 From: Sean Harvey Date: Tue, 2 Feb 2010 02:19:43 +0000 Subject: [PATCH] ENHANCEMENT Added MSSQLAzureDatabase->selectDatabase() support - it doesn't work the same way as SQL Server, we have to re-create the database connection --- code/MSSQLAzureDatabase.php | 50 ++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/code/MSSQLAzureDatabase.php b/code/MSSQLAzureDatabase.php index cfc8958..0d52a33 100644 --- a/code/MSSQLAzureDatabase.php +++ b/code/MSSQLAzureDatabase.php @@ -23,28 +23,48 @@ class MSSQLAzureDatabase extends MSSQLDatabase { protected $fullTextEnabled = false; public function __construct($parameters) { - $this->mssql = false; + $this->connectDatabase($parameters); + } - $connectionInfo = array( + protected function connectDatabase($parameters) { + $this->dbConn = sqlsrv_connect($parameters['server'], array( 'Database' => $parameters['database'], 'UID' => $parameters['username'], 'PWD' => $parameters['password'], 'MultipleActiveResultSets' => '0' - ); + )); - $this->dbConn = sqlsrv_connect($parameters['server'], $connectionInfo); + $this->tableList = $this->fieldList = $this->indexList = null; + $this->database = $parameters['database']; + $this->active = true; + $this->mssql = false; // mssql functions don't work with this database + $this->fullTextEnabled = false; - if(!$this->dbConn) { - $this->databaseError("Couldn't connect to MS SQL database"); - } else { - $this->database = $parameters['database']; - $this->active = true; - $this->fullTextEnabled = false; - - // Configure the connection - $this->query('SET QUOTED_IDENTIFIER ON'); - $this->query('SET TEXTSIZE 2147483647'); - } + // Configure the connection + $this->query('SET QUOTED_IDENTIFIER ON'); + $this->query('SET TEXTSIZE 2147483647'); + } + + /** + * Switches to the given database. + * + * If the database doesn't exist, you should call + * createDatabase() after calling selectDatabase() + * + * IMPORTANT: SQL Azure doesn't support "USE", so we need + * to reinitialize the database connection with the requested + * database name. + * + * @param string $dbname The database name to switch to + */ + public function selectDatabase($dbname) { + global $databaseConfig; + $parameters = array(); + $parameters['database'] = $dbname; + $parameters['server'] = $databaseConfig['server']; + $parameters['username'] = $databaseConfig['username']; + $parameters['password'] = $databaseConfig['password']; + $this->connectDatabase($parameters); } } \ No newline at end of file