1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\MSSQL; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Specific support for SQL Azure databases running on Windows Azure. |
7
|
|
|
* Currently only supports the SQLSRV driver from Microsoft. |
8
|
|
|
* |
9
|
|
|
* Some important things about SQL Azure: |
10
|
|
|
* |
11
|
|
|
* Selecting a database is not supported. |
12
|
|
|
* |
13
|
|
|
* In order to change the database currently in use, you need to connect to |
14
|
|
|
* the database using the "Database" parameter with sqlsrv_connect() |
15
|
|
|
* |
16
|
|
|
* Multiple active result sets are not supported. This means you can't |
17
|
|
|
* have two query results open at once. |
18
|
|
|
* |
19
|
|
|
* Fulltext indexes are not supported. |
20
|
|
|
*/ |
21
|
|
|
class MSSQLAzureDatabase extends MSSQLDatabase |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* List of parameters used to create new Azure connections between databases |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $parameters = []; |
30
|
|
|
|
31
|
|
|
public function fullTextEnabled() |
32
|
|
|
{ |
33
|
|
|
return false; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Connect to a SQL Azure database with the given parameters. |
38
|
|
|
* @param array $parameters Connection parameters set by environment |
39
|
|
|
* - server: The server, eg, localhost |
40
|
|
|
* - username: The username to log on with |
41
|
|
|
* - password: The password to log on with |
42
|
|
|
* - database: The database to connect to |
43
|
|
|
* - windowsauthentication: Not supported for Azure |
44
|
|
|
*/ |
45
|
|
|
public function connect($parameters) |
46
|
|
|
{ |
47
|
|
|
$this->parameters = $parameters; |
48
|
|
|
$this->connectDatabase($parameters['database']); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Connect to a database using the provided parameters |
53
|
|
|
* |
54
|
|
|
* @param string $database |
55
|
|
|
*/ |
56
|
|
|
protected function connectDatabase($database) |
57
|
|
|
{ |
58
|
|
|
$parameters = $this->parameters; |
59
|
|
|
$parameters['database'] = $database; |
60
|
|
|
$parameters['returndatesasstrings'] = 1; |
61
|
|
|
$parameters['multipleactiveresultsets'] = 1; |
62
|
|
|
|
63
|
|
|
// Ensure that driver is available (required by PDO) |
64
|
|
|
if (empty($parameters['driver'])) { |
65
|
|
|
$parameters['driver'] = $this->getDatabaseServer(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// Notify connector of parameters, instructing the connector |
69
|
|
|
// to connect immediately to the Azure database |
70
|
|
|
$this->connector->connect($parameters, true); |
71
|
|
|
|
72
|
|
|
// Configure the connection |
73
|
|
|
$this->query('SET QUOTED_IDENTIFIER ON'); |
74
|
|
|
$this->query('SET TEXTSIZE 2147483647'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Switches to the given database. |
79
|
|
|
* |
80
|
|
|
* IMPORTANT: SQL Azure doesn't support "USE", so we need |
81
|
|
|
* to reinitialize the database connection with the requested |
82
|
|
|
* database name. |
83
|
|
|
* @see http://msdn.microsoft.com/en-us/library/windowsazure/ee336288.aspx |
84
|
|
|
* |
85
|
|
|
* @param string $name The database name to switch to |
86
|
|
|
* @param bool $create |
87
|
|
|
* @param bool|int $errorLevel |
88
|
|
|
* @return bool |
89
|
|
|
*/ |
90
|
|
|
public function selectDatabase($name, $create = false, $errorLevel = E_USER_ERROR) |
91
|
|
|
{ |
92
|
|
|
$this->fullTextEnabled = null; |
93
|
|
|
|
94
|
|
|
if (!$this->schemaManager->databaseExists($name)) { |
95
|
|
|
// Check DB creation permisson |
96
|
|
|
if (!$create) { |
97
|
|
|
if ($errorLevel !== false) { |
98
|
|
|
user_error("Attempted to connect to non-existing database \"$name\"", $errorLevel); |
99
|
|
|
} |
100
|
|
|
// Unselect database |
101
|
|
|
$this->connector->unloadDatabase(); |
102
|
|
|
return false; |
103
|
|
|
} |
104
|
|
|
$this->schemaManager->createDatabase($name); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$this->connectDatabase($name); |
108
|
|
|
|
109
|
|
|
return true; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|