diff --git a/code/database/driver/abstract.php b/code/database/driver/abstract.php index 8503129972..6c047c45ba 100644 --- a/code/database/driver/abstract.php +++ b/code/database/driver/abstract.php @@ -196,10 +196,20 @@ abstract function setDatabase($database); * Provides access to the underlying database connection. Useful for when * you need to call a proprietary method such as postgresql's lo_* methods * + * @param bool $auto_connect If connection hasn't been established, auto connect * @return resource + * @throws \RuntimeException if auto connection fails */ - public function getConnection() + public function getConnection($auto_connect = true) { + if (!$this->isConnected() && $auto_connect) { + $this->connect(); + + if (!$this->isConnected()) { + throw new \RuntimeException('Database auto connection failed'); + } + } + return $this->_connection; } diff --git a/code/database/driver/interface.php b/code/database/driver/interface.php index 6b02b05113..62ef1ecd66 100644 --- a/code/database/driver/interface.php +++ b/code/database/driver/interface.php @@ -44,9 +44,11 @@ public function disconnect(); * Provides access to the underlying database connection. Useful for when you need to call a proprietary method * on the database driver * + * @param bool $auto_connect If connection hasn't been established, auto connect * @return resource + * @throws \RuntimeException if auto connection fails */ - public function getConnection(); + public function getConnection($auto_connect = true); /** * Set the connection