25 private static $instance =
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),
93 if (count($slaves >= 1)) {
94 foreach ($slaves as $slave) {
107 $this->tablePrefix =
$config->getString(
'database.prefix',
'');
120 $assembledConfig =
array(
122 'database.dsn' =>
$config->getString(
'database.dsn'),
123 'database.username' =>
$config->getString(
'database.username', null),
124 'database.password' =>
$config->getString(
'database.password', null),
125 'database.prefix' =>
$config->getString(
'database.prefix',
''),
126 'database.persistent' =>
$config->getBoolean(
'database.persistent',
false),
128 'slaves' =>
$config->getArray(
'database.slaves',
array()),
131 return sha1(serialize($assembledConfig));
150 $db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
153 }
catch (\PDOException $e) {
154 throw new \Exception(
"Database error: ".$e->getMessage());
167 if (count($this->dbSlaves) > 0) {
168 $slaveId = rand(0, count($this->dbSlaves) - 1);
169 return $this->dbSlaves[$slaveId];
171 return $this->dbMaster;
185 return $this->tablePrefix.$table;
201 assert(
'is_object($db)');
202 assert(
'is_string($stmt)');
203 assert(
'is_array($params)');
206 $query = $db->prepare($stmt);
208 foreach (
$params as $param => $value) {
209 if (is_array($value)) {
210 $query->bindValue(
":$param", $value[0], ($value[1]) ? $value[1] : \PDO::PARAM_STR);
212 $query->bindValue(
":$param", $value, \PDO::PARAM_STR);
219 }
catch (\PDOException $e) {
220 $this->lastError = $db->errorInfo();
221 throw new \Exception(
"Database error: ".$e->getMessage());
235 private function exec($db, $stmt)
237 assert(
'is_object($db)');
238 assert(
'is_string($stmt)');
241 return $db->exec($stmt);
242 }
catch (\PDOException $e) {
243 $this->lastError = $db->errorInfo();
244 throw new \Exception(
"Database error: ".$e->getMessage());
259 $db = $this->dbMaster;
262 $obj = $this->query($db, $stmt,
$params);
263 return $obj->rowCount();
265 return $this->exec($db, $stmt);
280 $db = $this->getSlave();
282 return $this->query($db, $stmt,
$params);
293 return $this->lastError;
static getInstance($altConfig=null)
Retrieves the current database instance.
exec($db, $stmt)
This function queries the database without using a prepared statement.
__construct($config)
Private constructor that restricts instantiation to getInstance().
$dbMaster
PDO Object for the Master database server.
static generateInstanceId($config)
Generate an Instance ID based on the database configuration.
$tablePrefix
Prefix to apply to the tables.
connect($dsn, $username, $password, $options)
This function connects to a database.
getSlave()
This function randomly selects a slave database server to query.
Attribute-related utility methods.
write($stmt, $params=array())
This executes queries directly on the master.
query($db, $stmt, $params)
This function queries the database.
getLastError()
Return an array with information about the last operation performed in the database.
Create styles array
The data for the language used.
read($stmt, $params=array())
This executes queries on a database server that is determined by this::getSlave().
if(empty($password)) $table
$lastError
Array with information on the last error occurred.
applyPrefix($table)
This function simply applies the table prefix to a supplied table name.
static getInstance($instancename='simplesaml')
Get a configuration file by its instance name.
if(!isset($_REQUEST['ReturnTo'])) if(!isset($_REQUEST['AuthId'])) $options