ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
SimpleSAML\Database Class Reference
+ Collaboration diagram for SimpleSAML\Database:

Public Member Functions

 applyPrefix ($table)
 This function simply applies the table prefix to a supplied table name. More...
 
 write ($stmt, $params=array())
 This executes queries directly on the master. More...
 
 read ($stmt, $params=array())
 This executes queries on a database server that is determined by this::getSlave(). More...
 
 getLastError ()
 Return an array with information about the last operation performed in the database. More...
 

Static Public Member Functions

static getInstance ($altConfig=null)
 Retrieves the current database instance. More...
 

Private Member Functions

 __construct ($config)
 Private constructor that restricts instantiation to getInstance(). More...
 
 connect ($dsn, $username, $password, $options)
 This function connects to a database. More...
 
 getSlave ()
 This function randomly selects a slave database server to query. More...
 
 query ($db, $stmt, $params)
 This function queries the database. More...
 
 exec ($db, $stmt)
 This function queries the database without using a prepared statement. More...
 

Static Private Member Functions

static generateInstanceId ($config)
 Generate an Instance ID based on the database configuration. More...
 

Private Attributes

 $dbMaster
 PDO Object for the Master database server. More...
 
 $dbSlaves = array()
 Array of PDO Objects for configured database slaves. More...
 
 $tablePrefix
 Prefix to apply to the tables. More...
 
 $lastError
 Array with information on the last error occurred. More...
 

Static Private Attributes

static $instance = array()
 This variable holds the instance of the session - Singleton approach. More...
 

Detailed Description

Definition at line 19 of file Database.php.

Constructor & Destructor Documentation

◆ __construct()

SimpleSAML\Database::__construct (   $config)
private

Private constructor that restricts instantiation to getInstance().

Parameters
\SimpleSAML_Configuration$configInstance of the SimpleSAML_Configuration class

Definition at line 76 of file Database.php.

77 {
78 $driverOptions = $config->getArray('database.driver_options', array());
79 if ($config->getBoolean('database.persistent', true)) {
80 $driverOptions = array(\PDO::ATTR_PERSISTENT => true);
81 }
82
83 // connect to the master
84 $this->dbMaster = $this->connect(
85 $config->getString('database.dsn'),
86 $config->getString('database.username', null),
87 $config->getString('database.password', null),
88 $driverOptions
89 );
90
91 // connect to any configured slaves
92 $slaves = $config->getArray('database.slaves', array());
93 if (count($slaves >= 1)) {
94 foreach ($slaves as $slave) {
95 array_push(
96 $this->dbSlaves,
97 $this->connect(
98 $slave['dsn'],
99 $slave['username'],
100 $slave['password'],
101 $driverOptions
102 )
103 );
104 }
105 }
106
107 $this->tablePrefix = $config->getString('database.prefix', '');
108 }
connect($dsn, $username, $password, $options)
This function connects to a database.
Definition: Database.php:146

References $config.

Member Function Documentation

◆ applyPrefix()

SimpleSAML\Database::applyPrefix (   $table)

This function simply applies the table prefix to a supplied table name.

Parameters
string$tableTable to apply prefix to, if configured
Returns
string Table with configured prefix

Definition at line 183 of file Database.php.

184 {
185 return $this->tablePrefix.$table;
186 }

◆ connect()

SimpleSAML\Database::connect (   $dsn,
  $username,
  $password,
  $options 
)
private

This function connects to a database.

Parameters
string$dsnDatabase connection string
string$usernameSQL user
string$passwordSQL password
array$optionsPDO options
Exceptions

Exception If an error happens while trying to connect to the database.

Returns
\PDO object

Definition at line 146 of file Database.php.

147 {
148 try {
149 $db = new \PDO($dsn, $username, $password, $options);
150 $db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
151
152 return $db;
153 } catch (\PDOException $e) {
154 throw new \Exception("Database error: ".$e->getMessage());
155 }
156 }
if(!isset( $_REQUEST[ 'ReturnTo'])) if(!isset($_REQUEST['AuthId'])) $options
Definition: as_login.php:20
$password
Definition: pwgen.php:17

References $options, and $password.

◆ exec()

SimpleSAML\Database::exec (   $db,
  $stmt 
)
private

This function queries the database without using a prepared statement.

Parameters
\PDO$dbPDO object to use
string$stmtAn SQL statement to execute, previously escaped.
Exceptions

Exception If an error happens while trying to execute the query.

Returns
int The number of rows affected.

Definition at line 235 of file Database.php.

236 {
237 assert('is_object($db)');
238 assert('is_string($stmt)');
239
240 try {
241 return $db->exec($stmt);
242 } catch (\PDOException $e) {
243 $this->lastError = $db->errorInfo();
244 throw new \Exception("Database error: ".$e->getMessage());
245 }
246 }

◆ generateInstanceId()

static SimpleSAML\Database::generateInstanceId (   $config)
staticprivate

Generate an Instance ID based on the database configuration.

Parameters
\SimpleSAML_Configuration$configConfiguration class
Returns
string $instanceId

Definition at line 118 of file Database.php.

119 {
120 $assembledConfig = array(
121 'master' => 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),
127 ),
128 'slaves' => $config->getArray('database.slaves', array()),
129 );
130
131 return sha1(serialize($assembledConfig));
132 }

References $config.

◆ getInstance()

static SimpleSAML\Database::getInstance (   $altConfig = null)
static

Retrieves the current database instance.

Will create a new one if there isn't an existing connection.

Parameters
\SimpleSAML_Configuration$altConfigOptional: Instance of a SimpleSAML_Configuration class
Returns
\SimpleSAML\Database The shared database connection.

Definition at line 55 of file Database.php.

56 {
57 $config = ($altConfig) ? $altConfig : \SimpleSAML_Configuration::getInstance();
58 $instanceId = self::generateInstanceId($config);
59
60 // check if we already have initialized the session
61 if (isset(self::$instance[$instanceId])) {
62 return self::$instance[$instanceId];
63 }
64
65 // create a new session
66 self::$instance[$instanceId] = new Database($config);
67 return self::$instance[$instanceId];
68 }
static generateInstanceId($config)
Generate an Instance ID based on the database configuration.
Definition: Database.php:118
static getInstance($instancename='simplesaml')
Get a configuration file by its instance name.

References $config, and SimpleSAML_Configuration\getInstance().

Referenced by SimpleSAML_Metadata_MetaDataStorageHandlerPdo\__construct().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getLastError()

SimpleSAML\Database::getLastError ( )

Return an array with information about the last operation performed in the database.

Returns
array The array with error information.

Definition at line 291 of file Database.php.

292 {
293 return $this->lastError;
294 }
$lastError
Array with information on the last error occurred.
Definition: Database.php:45

◆ getSlave()

SimpleSAML\Database::getSlave ( )
private

This function randomly selects a slave database server to query.

In the event no slaves are configured, it will return the master.

Returns
\PDO object

Definition at line 165 of file Database.php.

166 {
167 if (count($this->dbSlaves) > 0) {
168 $slaveId = rand(0, count($this->dbSlaves) - 1);
169 return $this->dbSlaves[$slaveId];
170 } else {
171 return $this->dbMaster;
172 }
173 }
$dbMaster
PDO Object for the Master database server.
Definition: Database.php:30

◆ query()

SimpleSAML\Database::query (   $db,
  $stmt,
  $params 
)
private

This function queries the database.

Parameters
\PDO$dbPDO object to use
string$stmtPrepared SQL statement
array$paramsParameters
Exceptions

Exception If an error happens while trying to execute the query.

Returns
\PDOStatement object

Definition at line 199 of file Database.php.

200 {
201 assert('is_object($db)');
202 assert('is_string($stmt)');
203 assert('is_array($params)');
204
205 try {
206 $query = $db->prepare($stmt);
207
208 foreach ($params as $param => $value) {
209 if (is_array($value)) {
210 $query->bindValue(":$param", $value[0], ($value[1]) ? $value[1] : \PDO::PARAM_STR);
211 } else {
212 $query->bindValue(":$param", $value, \PDO::PARAM_STR);
213 }
214 }
215
216 $query->execute();
217
218 return $query;
219 } catch (\PDOException $e) {
220 $this->lastError = $db->errorInfo();
221 throw new \Exception("Database error: ".$e->getMessage());
222 }
223 }
$query
$params
Definition: disable.php:11

References $params, and $query.

◆ read()

SimpleSAML\Database::read (   $stmt,
  $params = array() 
)

This executes queries on a database server that is determined by this::getSlave().

Parameters
string$stmtPrepared SQL statement
array$paramsParameters
Returns
\PDOStatement object

Definition at line 278 of file Database.php.

279 {
280 $db = $this->getSlave();
281
282 return $this->query($db, $stmt, $params);
283 }
query($db, $stmt, $params)
This function queries the database.
Definition: Database.php:199
getSlave()
This function randomly selects a slave database server to query.
Definition: Database.php:165

References $params.

◆ write()

SimpleSAML\Database::write (   $stmt,
  $params = array() 
)

This executes queries directly on the master.

Parameters
string$stmtPrepared SQL statement
array$paramsParameters
Returns
int The number of rows affected by the query.

Definition at line 257 of file Database.php.

258 {
259 $db = $this->dbMaster;
260
261 if (is_array($params)) {
262 $obj = $this->query($db, $stmt, $params);
263 return $obj->rowCount();
264 } else {
265 return $this->exec($db, $stmt);
266 }
267 }
exec($db, $stmt)
This function queries the database without using a prepared statement.
Definition: Database.php:235

References $params.

Field Documentation

◆ $dbMaster

SimpleSAML\Database::$dbMaster
private

PDO Object for the Master database server.

Definition at line 30 of file Database.php.

◆ $dbSlaves

SimpleSAML\Database::$dbSlaves = array()
private

Array of PDO Objects for configured database slaves.

Definition at line 35 of file Database.php.

◆ $instance

SimpleSAML\Database::$instance = array()
staticprivate

This variable holds the instance of the session - Singleton approach.

Definition at line 25 of file Database.php.

◆ $lastError

SimpleSAML\Database::$lastError
private

Array with information on the last error occurred.

Definition at line 45 of file Database.php.

◆ $tablePrefix

SimpleSAML\Database::$tablePrefix
private

Prefix to apply to the tables.

Definition at line 40 of file Database.php.


The documentation for this class was generated from the following file: