Skip to content

Commit

Permalink
Added the ability to close database connections for long running task…
Browse files Browse the repository at this point in the history
…s, and brought the comments up to spec
  • Loading branch information
rikh42 committed Feb 18, 2013
1 parent 7ade932 commit cbbb729
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 68 deletions.
5 changes: 5 additions & 0 deletions core/ConnectionInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public function getPDO()
return $this->pdo;
}

public function resetPDO()
{
$this->pdo = null;
}

public function getConnectionString()
{
return 'mysql:host='.$this->host.';port='.$this->port.';dbname='.$this->database;
Expand Down
201 changes: 133 additions & 68 deletions core/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
use \PDO;
use \PDOException;

//=====================================
// Database
// The core Database management layer
//=====================================


/**
* The core Database management layer
*/
class Database extends ContainerAware implements DatabaseInterface
{
/**
Expand Down Expand Up @@ -47,10 +48,12 @@ class Database extends ContainerAware implements DatabaseInterface
const FETCH_ROW = 2;
const FETCH_ONE = 3;

//=====================================
// __construct
// attempts to initialise the database connection
//=====================================



/**
* attempts to initialise the database connection
*/
public function __construct()
{
// set some defaults
Expand All @@ -75,11 +78,14 @@ public function init()
$this->setActiveConnection('read');
}

//=====================================
// addConnection
// Adds information about a connection to the database object
// but does not do anything with it
//=====================================



/**
* Adds information about a connection to the database object
* but does not do anything with it
* @param $name
*/
public function addConnection($name)
{
// get the container
Expand All @@ -104,10 +110,13 @@ public function addConnection($name)
$this->logger->debug("Added connection info to Database handler", array('host'=>$host, 'user'=>$user, 'database'=>$database));
}

//=====================================
// setActiveConnection
// makes the named connection the active one, connecting to the database if required.
//=====================================


/**
* makes the named connection the active one, connecting to the database if required.
* @param $name
* @return bool
*/
public function setActiveConnection($name)
{
// If there isn't a connection of that name defined, fail
Expand All @@ -123,11 +132,14 @@ public function setActiveConnection($name)
return true;
}

//=====================================
// enableActiveConnection
// Call this before accessing the pdo object.
// This basically performs a lazy connection to the database
//=====================================


/**
* Call this before accessing the pdo object.
* This basically performs a lazy connection to the database
* @throws \RuntimeException
* @throws \PDOException
*/
protected function enableActiveConnection()
{
if ($this->pdo != null) {
Expand All @@ -140,6 +152,7 @@ protected function enableActiveConnection()
}

// We have a connection. Has it already been set up?
/* @var $info ConnectionInfo */
$info = $this->connections[$this->activeConnection];
if ($info->getPDO() == null) {
// now try and reconnect
Expand Down Expand Up @@ -168,10 +181,10 @@ protected function enableActiveConnection()
$this->pdo = $info->getPDO();
}

//=====================================
// resetResults
// Clears the last results etc at the start of a new query
//=====================================

/**
* Clears the last results etc at the start of a new query
*/
protected function resetResults()
{
// reset a few things
Expand All @@ -180,22 +193,50 @@ protected function resetResults()
$this->lastRowsAffected = 0;
}

//=====================================
// isOpenConnection
// returns true if there is a valid connection to a database
// As connections are only created when needed (ie when a the first query is performed)
// this will always return false until after an attempt to make a query.
//=====================================


/**
* returns true if there is a valid connection to a database
* As connections are only created when needed (ie when a the first query is performed)
* this will always return false until after an attempt to make a query.
* @return bool
*/
public function isOpenConnection()
{
return ($this->pdo != null);
}

//=====================================
// autoBind
// binds the named params in the query to values passed in
// eg. $params['int:iUser'] = 5; would bind the value 5 to the named argument :iUser as an integer
//=====================================

/**
* Close all the connections. This is not needed in a normal web application.
* However, if you are working with an application that runs for long periods
* (for example a command line queue processor), then you may need to close the connections
* to prevent them timing out and falling into a broken state.
*/
public function closeAllConnections()
{
// get rid of the connection we are using
$this->pdo = null;

// loop over all the connections we have and ensure they are all closed.
foreach ($this->connections as $connection)
{
// Get rid of the cached connections as well
/* @var $connection ConnectionInfo */
$connection->resetPDO();
}
}




/**
* binds the named params in the query to values passed in
* eg. $params['int:iUser'] = 5; would bind the value 5 to the named argument :iUser as an integer
* @param \PDOStatement $statement
* @param $query
* @param $params
*/
protected function autoBind(\PDOStatement $statement, $query, $params)
{
// If the params passed in is an array, bind its contents, else ignore it
Expand Down Expand Up @@ -261,11 +302,14 @@ protected function autoBind(\PDOStatement $statement, $query, $params)
}
}

//=====================================
// selectQuery
// Help function to handle all the select queries
// $fetchMode. One of FETCH_ALL, FETCH_ROW or FETCH_ONE
//=====================================
/**
* Help function to handle all the select queries
* @param $query
* @param $params
* @param $fetchMethod - One of FETCH_ALL, FETCH_ROW or FETCH_ONE
* @return bool|null
* @throws \RuntimeException
*/
protected function selectQuery($query, $params, $fetchMethod)
{
try {
Expand Down Expand Up @@ -327,37 +371,55 @@ protected function selectQuery($query, $params, $fetchMethod)
}
}

//=====================================
// all
// Get all the results back from a query.
//=====================================


/**
* Get all the results back from a query.
* @param $query - SQL Query
* @param array|null $params
* @return null|array
*/
public function all($query, $params=null)
{
return $this->selectQuery($query, $params, self::FETCH_ALL);
}

//=====================================
// row
// Get a single row of data back from the query
//=====================================



/**
* Get a single row of data back from the query
* @param $query - The SQL Query
* @param array|null $params
* @return stdClass|null
*/
public function row($query, $params=null)
{
return $this->selectQuery($query, $params, self::FETCH_ROW);
}

//=====================================
// one
// Get a single value back from the query
//=====================================


/**
* Get a single value back from the query
* @param $query - An SQL Query
* @param array|null $params
* @return mixed|null
*/
public function one($query, $params=null)
{
return $this->selectQuery($query, $params, self::FETCH_ONE);
}

//=====================================
// query
// general insert or update query
//=====================================


/**
* general insert or update query
* @param $query
* @param array|null $params
* @return int
* @throws \RuntimeException
*/
public function query($query, $params=null)
{
try {
Expand Down Expand Up @@ -396,21 +458,24 @@ public function query($query, $params=null)
}
}

//=====================================
// getLastInsertID
// gets the index of the row inserted in the last query.
// It is reset to 0 with every query made to the database, so you will need to get
// this value right away if you need it.
//=====================================


/**
* gets the index of the row inserted in the last query.
* It is reset to 0 with every query made to the database, so you will need to get
* this value right away if you need it.
* @return int
*/
public function getLastInsertID()
{
return (int) $this->lastInsertID;
}

//=====================================
// getLastInsertIDString
// gets the index of the row inserted in the last query as a string.
//=====================================

/**
* gets the index of the row inserted in the last query as a string.
* @return int
*/
public function getLastInsertIDString()
{
return $this->lastInsertID;
Expand Down
1 change: 1 addition & 0 deletions core/DatabaseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function init();
public function addConnection($name);
public function setActiveConnection($name);
public function isOpenConnection();
public function closeAllConnections();

// Queries
public function all($query, $params = null);
Expand Down

0 comments on commit cbbb729

Please sign in to comment.