25    private static $instance = array();
 
   35    private $dbSlaves = array();
 
   58        $instanceId = self::generateInstanceId(
$config);
 
   61        if (isset(self::$instance[$instanceId])) {
 
   62            return self::$instance[$instanceId];
 
   67        return self::$instance[$instanceId];
 
   78        $driverOptions = 
$config->getArray(
'database.driver_options', array());
 
   79        if (
$config->getBoolean(
'database.persistent', 
true)) {
 
   80            $driverOptions = array(\PDO::ATTR_PERSISTENT => 
true);
 
   84        $this->dbMaster = $this->connect(
 
   85            $config->getString(
'database.dsn'),
 
   86            $config->getString(
'database.username', 
null),
 
   87            $config->getString(
'database.password', 
null),
 
   92        $slaves = 
$config->getArray(
'database.slaves', array());
 
   93        foreach ($slaves as $slave) {
 
  105        $this->tablePrefix = 
$config->getString(
'database.prefix', 
'');
 
  118        $assembledConfig = array(
 
  120                'database.dsn'        => 
$config->getString(
'database.dsn'),
 
  121                'database.username'   => 
$config->getString(
'database.username', 
null),
 
  122                'database.password'   => 
$config->getString(
'database.password', 
null),
 
  123                'database.prefix'     => 
$config->getString(
'database.prefix', 
''),
 
  124                'database.persistent' => 
$config->getBoolean(
'database.persistent', 
false),
 
  126            'slaves' => 
$config->getArray(
'database.slaves', array()),
 
  129        return sha1(serialize($assembledConfig));
 
  148            $db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
 
  151        } 
catch (\PDOException $e) {
 
  152            throw new \Exception(
"Database error: ".$e->getMessage());
 
  165        if (count($this->dbSlaves) > 0) {
 
  166            $slaveId = rand(0, count($this->dbSlaves) - 1);
 
  167            return $this->dbSlaves[$slaveId];
 
  169            return $this->dbMaster;
 
  183        return $this->tablePrefix.$table;
 
  199        assert(is_object($db));
 
  200        assert(is_string(
$stmt));
 
  206            foreach (
$params as $param => $value) {
 
  207                if (is_array($value)) {
 
  208                    $query->bindValue(
":$param", $value[0], ($value[1]) ? $value[1] : \PDO::PARAM_STR);
 
  210                    $query->bindValue(
":$param", $value, \PDO::PARAM_STR);
 
  217        } 
catch (\PDOException $e) {
 
  218            $this->lastError = $db->errorInfo();
 
  219            throw new \Exception(
"Database error: ".$e->getMessage());
 
  235        assert(is_object($db));
 
  236        assert(is_string(
$stmt));
 
  239            return $db->exec(
$stmt);
 
  240        } 
catch (\PDOException $e) {
 
  241            $this->lastError = $db->errorInfo();
 
  242            throw new \Exception(
"Database error: ".$e->getMessage());
 
  257        $db = $this->dbMaster;
 
  261            return $obj->rowCount();
 
  263            return $this->exec($db, 
$stmt);
 
  278        $db = $this->getSlave();
 
  291        return $this->lastError;
 
An exception for terminatinating execution or to throw for unit testing.
getLastError()
Return an array with information about the last operation performed in the database.
write($stmt, $params=array())
This executes queries directly on the master.
static getInstance($altConfig=null)
Retrieves the current database instance.
query($db, $stmt, $params)
This function queries the database.
connect($dsn, $username, $password, $options)
This function connects to a database.
exec($db, $stmt)
This function queries the database without using a prepared statement.
read($stmt, $params=array())
This executes queries on a database server that is determined by this::getSlave().
$tablePrefix
Prefix to apply to the tables.
applyPrefix($table)
This function simply applies the table prefix to a supplied table name.
__construct($config)
Private constructor that restricts instantiation to getInstance().
$dbMaster
PDO Object for the Master database server.
$lastError
Array with information on the last error occurred.
static generateInstanceId($config)
Generate an Instance ID based on the database configuration.
getSlave()
This function randomly selects a slave database server to query.
static getInstance($instancename='simplesaml')
Get a configuration file by its instance name.
foreach($paths as $path) $dsn
Attribute-related utility methods.
if(empty($password)) $table