Merge pull request #5717 from tractorcow/pulls/3/optimistic-connect

API Add optimistic_live_connect to SS_Database
This commit is contained in:
Daniel Hensby 2016-07-12 00:13:16 +01:00 committed by GitHub
commit 4ce7e9cd5c

View File

@ -16,6 +16,15 @@ abstract class SS_Database {
*/ */
protected $connector = null; protected $connector = null;
/**
* In cases where your environment does not have 'SHOW DATABASES' permission,
* you can set this to true. Then selectDatabase() will always connect without
* doing databaseExists() check.
*
* @var bool
*/
private static $optimistic_connect = false;
/** /**
* Get the current connector * Get the current connector
* *
@ -630,18 +639,23 @@ abstract class SS_Database {
* @return boolean Flag indicating success * @return boolean Flag indicating success
*/ */
public function selectDatabase($name, $create = false, $errorLevel = E_USER_ERROR) { public function selectDatabase($name, $create = false, $errorLevel = E_USER_ERROR) {
if (!$this->schemaManager->databaseExists($name)) { // In case our live environment is locked down, we can bypass a SHOW DATABASE check
// Check DB creation permisson $canConnect = Config::inst()->get(get_class($this), 'optimistic_connect')
if (!$create) { || $this->schemaManager->databaseExists($name);
if ($errorLevel !== false) { if($canConnect) {
user_error("Attempted to connect to non-existing database \"$name\"", $errorLevel); return $this->connector->selectDatabase($name);
}
// Unselect database
$this->connector->unloadDatabase();
return false;
}
$this->schemaManager->createDatabase($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);
return $this->connector->selectDatabase($name); return $this->connector->selectDatabase($name);
} }