From 5f8fb30c401654c689306c5371b75ffd8c2596ea Mon Sep 17 00:00:00 2001 From: Oli Griffiths Date: Mon, 9 May 2016 00:35:51 -0400 Subject: [PATCH 1/3] Auto connect to DB on first query --- code/database/driver/abstract.php | 7 ++++++- code/database/driver/interface.php | 3 ++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/code/database/driver/abstract.php b/code/database/driver/abstract.php index 8503129972..7f0ef91721 100644 --- a/code/database/driver/abstract.php +++ b/code/database/driver/abstract.php @@ -196,10 +196,15 @@ 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 $autoconnect If connection hasn't been established, auto connect * @return resource */ - public function getConnection() + public function getConnection($autoconnect = true) { + if (!$this->_connection && $autoconnect) { + $this->connect(); + } + return $this->_connection; } diff --git a/code/database/driver/interface.php b/code/database/driver/interface.php index 6b02b05113..378a42b49d 100644 --- a/code/database/driver/interface.php +++ b/code/database/driver/interface.php @@ -44,9 +44,10 @@ 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 $autoconnect If connection hasn't been established, auto connect * @return resource */ - public function getConnection(); + public function getConnection($autoconnect = true); /** * Set the connection From fa8ecf05250ad15596e67b0302bb611ee2ed5d2c Mon Sep 17 00:00:00 2001 From: Oli Griffiths Date: Mon, 9 May 2016 00:39:38 -0400 Subject: [PATCH 2/3] Throw exception is auto connection fails --- code/database/driver/abstract.php | 11 ++++++++--- code/database/driver/interface.php | 5 +++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/code/database/driver/abstract.php b/code/database/driver/abstract.php index 7f0ef91721..f3e120fa2f 100644 --- a/code/database/driver/abstract.php +++ b/code/database/driver/abstract.php @@ -196,13 +196,18 @@ 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 $autoconnect If connection hasn't been established, auto connect + * @param bool $auto_connect If connection hasn't been established, auto connect * @return resource + * @throws \RuntimeException if auto connection fails */ - public function getConnection($autoconnect = true) + public function getConnection($auto_connect = true) { - if (!$this->_connection && $autoconnect) { + if (!$this->_connection && $auto_connect) { $this->connect(); + + if (!$this->_connection) { + 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 378a42b49d..62ef1ecd66 100644 --- a/code/database/driver/interface.php +++ b/code/database/driver/interface.php @@ -44,10 +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 $autoconnect If connection hasn't been established, auto connect + * @param bool $auto_connect If connection hasn't been established, auto connect * @return resource + * @throws \RuntimeException if auto connection fails */ - public function getConnection($autoconnect = true); + public function getConnection($auto_connect = true); /** * Set the connection From fdf3c7464a2e862a9c82825abb35f4efb665674b Mon Sep 17 00:00:00 2001 From: Oli Griffiths Date: Tue, 10 May 2016 23:49:17 -0400 Subject: [PATCH 3/3] Use isConnected() in DatabaseDriver getConnection() --- code/database/driver/abstract.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/code/database/driver/abstract.php b/code/database/driver/abstract.php index f3e120fa2f..6c047c45ba 100644 --- a/code/database/driver/abstract.php +++ b/code/database/driver/abstract.php @@ -202,10 +202,10 @@ abstract function setDatabase($database); */ public function getConnection($auto_connect = true) { - if (!$this->_connection && $auto_connect) { + if (!$this->isConnected() && $auto_connect) { $this->connect(); - if (!$this->_connection) { + if (!$this->isConnected()) { throw new \RuntimeException('Database auto connection failed'); } }