Commit 3d604bff by Scott

Add docBlock and use statements

parent ffb29023
...@@ -18,6 +18,9 @@ ...@@ -18,6 +18,9 @@
namespace Q2A\Database; namespace Q2A\Database;
use PDO;
use PDOException;
class DbConnection class DbConnection
{ {
protected $pdo; protected $pdo;
...@@ -47,11 +50,21 @@ class DbConnection ...@@ -47,11 +50,21 @@ class DbConnection
} }
} }
/**
* Indicates to the Q2A database layer that database connections are permitted from this point forwards (before
* this point, some plugins may not have had a chance to override some database access functions).
*/
public function allowConnect() public function allowConnect()
{ {
$this->allowConnect = true; $this->allowConnect = true;
} }
/**
* Connect to the Q2A database, optionally install the $failhandler (and call it if necessary). Uses PDO as of Q2A
* 1.9.
* @param string $failhandler
* @return mixed|void
*/
public function connect($failHandler = null) public function connect($failHandler = null)
{ {
if (!$this->allowConnect) { if (!$this->allowConnect) {
...@@ -71,20 +84,28 @@ class DbConnection ...@@ -71,20 +84,28 @@ class DbConnection
$dsn .= ';port=' . $this->config['port']; $dsn .= ';port=' . $this->config['port'];
} }
$options = array(\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION); $options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
if (QA_PERSISTENT_CONN_DB) { if (QA_PERSISTENT_CONN_DB) {
$options[\PDO::ATTR_PERSISTENT] = true; $options[PDO::ATTR_PERSISTENT] = true;
} }
try { try {
$this->pdo = new \PDO($dsn, $this->config['username'], $this->config['password'], $options); $this->pdo = new PDO($dsn, $this->config['username'], $this->config['password'], $options);
} catch (\PDOException $ex) { } catch (PDOException $ex) {
$this->failError('connect', $ex->getCode(), $ex->getMessage()); $this->failError('connect', $ex->getCode(), $ex->getMessage());
} }
qa_report_process_stage('db_connected'); qa_report_process_stage('db_connected');
} }
/**
* If a DB error occurs, call the installed fail handler (if any) otherwise report error and exit immediately.
* @param $type
* @param int $errno
* @param string $error
* @param string $query
* @return mixed
*/
public function failError($type, $errno = null, $error = null, $query = null) public function failError($type, $errno = null, $error = null, $query = null)
{ {
@error_log('PHP Question2Answer MySQL ' . $type . ' error ' . $errno . ': ' . $error . (isset($query) ? (' - Query: ' . $query) : '')); @error_log('PHP Question2Answer MySQL ' . $type . ' error ' . $errno . ': ' . $error . (isset($query) ? (' - Query: ' . $query) : ''));
...@@ -100,6 +121,12 @@ class DbConnection ...@@ -100,6 +121,12 @@ class DbConnection
} }
} }
/**
* Prepare and execute a SQL query, handling any failures. In debugging mode, track the queries and resources used.
* @param string $query
* @param array $params
* @return PDOStatement
*/
public function query($query, $params = array()) public function query($query, $params = array())
{ {
try { try {
...@@ -122,11 +149,20 @@ class DbConnection ...@@ -122,11 +149,20 @@ class DbConnection
} else { } else {
$stmt = $this->execute($query, $params); $stmt = $this->execute($query, $params);
} }
} catch (\PDOException $ex) {
return $stmt;
} catch (PDOException $ex) {
$this->failError('query', $ex->getCode(), $ex->getMessage(), $query); $this->failError('query', $ex->getCode(), $ex->getMessage(), $query);
} }
} }
/**
* Lower-level function to prepare and execute a SQL query. Automatically retries if there is a MySQL deadlock
* error.
* @param string $query
* @param array $params
* @return PDOStatement
*/
protected function execute($query, $params = array()) protected function execute($query, $params = array())
{ {
$stmt = $this->pdo->prepare($query); $stmt = $this->pdo->prepare($query);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment