Skip to content

Instantly share code, notes, and snippets.

@ry167 ry167/SQLConnections.php
Last active Aug 29, 2015

Embed
What would you like to do?
SQL Connection Manager
<?php
namespace Ry167\SQL;
use InvalidArgumentException;
/**
* SQL Connection Manager
*
* Manages a multiple SQL Connections and their initiation.
* @author Ryan Halliday <ry167@ry167.com>
* @link http://ry167.com
* @version 0.1
*/
class Connections{
/**
* Stores available connection options
* @var array
*/
private $Connections = array(
'default' => null,
);
/**
* Stores available read-only connections
* @var array
*/
private $Reads = array();
/**
* Stores available write connections
* @var array
*/
private $Writes = array();
/**
* Default options for a connection to have
* @var array
*/
private $Options = array();
/**
* What class to initiate upon calling a get*() method.
* @var string
*/
private $Class = '\PDO';
/**
* Constructs the connector class and potentially adds a default connection
* @param string $dsn
* @param string $username
* @param string $password
* @param array $options
*/
public function __construct($dsn = null, $username = null, $password = null, $options = array()){
if (!is_null($dsn)) $this->addConnection('default',$dsn,$username,$password,$options);
}
/**
* Set the class to initiate upon calling a get*() method.
* @param string $class
*/
public function setClass($class){
$this->Class = $class;
}
/**
* Set default options for connections. Their own settings will override these defaults
* @param mixed|array $key Can be an array of Key => Value pairs of options or the key for the $val parameter.
* @param mixed $val Value to be set for the previous key.
*/
public function setOptions($key,$val = null){
if (is_array($key)){
$this->Options = $this->Options + $key;
}
else $this->Options[$key] = $val;
}
/**
* Adds a named array of Connection settings to the Connections array
* @param string $name Type of connection to add. Should be 'write', 'read' or 'default' unless it is being uniquely named, which is allowed.
* @param string $dsn Data Source Name for PDO
* @param string $username Username for PDO
* @param string $password Password for PDO
* @param array $options
*/
public function add($name, $dsn, $username = null, $password = null, $options = array()){
if (strtolower($name) == 'write' || strtolower($name) == 'read'){
$this->{ucfirst(strtolower($name))."s"}[] = array(
'dsn' => $dsn,
'username' => $username,
'password' => $password,
'options' => $options
);
}
else{
$this->Connections[$name] = array(
'dsn' => $dsn,
'username' => $username,
'password' => $password,
'options' => $options
);
}
}
/**
* Get a named connection
* @param string $name Name of the connection
* @throws InvalidArgumentException If there is no connection of the name specified
*/
public function get($name){
if (isset($this->Connections[$name]) && !is_null($this->Connections)){
$Options = $this->Options + $this->Connections[$name]['options'];
return new $this->Class($this->Connections[$name]['dsn'],$this->Connections[$name]['username'],
$this->Connections[$name]['password'],$Options);
}
else throw new InvalidArgumentException("No default connection available and none specified");
}
/**
* Initiate the default connection
* @see Connections::get() This is just an alias.
*/
public function getDefault(){
return $this->get('default');
}
/**
* Gets a random writable connection
* @return mixed Instance of $this->Class (Default: PDO)
* @throws InvalidArgumentException If there is no write connections specified
*/
public function getWrite(){
if (count($this->Writes) > 0){
$Num = mt_rand(0,count($this->Writes)-1);
$Options = $this->Options + $this->Writes[$Num]['options'];
return new $this->Class($this->Writes[$Num]['dsn'],$this->Writes[$Num]['username'],
$this->Writes[$Num]['password'],$Options);
}
else throw new InvalidArgumentException("There must be at least one write connection defined to be able to get one");
}
/**
* Gets a random readable connection
* @return mixed Instance of $this->Class (Default: PDO)
* @throws InvalidArgumentException If there is no read connections specified
*/
public function getRead(){
if (count($this->Reads) > 0){
$Num = mt_rand(0,count($this->Reads)-1);
$Options = $this->Options + $this->Reads[$Num]['options'];
return new $this->Class($this->Reads[$Num]['dsn'],$this->Reads[$Num]['username'],
$this->Reads[$Num]['password'],$Options);
}
else throw new InvalidArgumentException("There must be at least one read connection defined to be able to get one");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.