ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 foreach ($slaves as $slave) {
94 array_push(
95 $this->dbSlaves,
96 $this->connect(
97 $slave['dsn'],
98 $slave['username'],
99 $slave['password'],
100 $driverOptions
101 )
102 );
103 }
104
105 $this->tablePrefix = $config->getString('database.prefix', '');
106 }
connect($dsn, $username, $password, $options)
This function connects to a database.
Definition: Database.php:144
$config
Definition: bootstrap.php:15

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 181 of file Database.php.

182 {
183 return $this->tablePrefix.$table;
184 }

◆ 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 144 of file Database.php.

145 {
146 try {
147 $db = new \PDO($dsn, $username, $password, $options);
148 $db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
149
150 return $db;
151 } catch (\PDOException $e) {
152 throw new \Exception("Database error: ".$e->getMessage());
153 }
154 }
$password
Definition: cron.php:14
foreach($paths as $path) $dsn
Definition: migrateto20.php:56

References $dsn, PHPMailer\PHPMailer\$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 233 of file Database.php.

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

References $stmt.

◆ 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 116 of file Database.php.

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

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:116
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 289 of file Database.php.

290 {
291 return $this->lastError;
292 }
$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 163 of file Database.php.

164 {
165 if (count($this->dbSlaves) > 0) {
166 $slaveId = rand(0, count($this->dbSlaves) - 1);
167 return $this->dbSlaves[$slaveId];
168 } else {
169 return $this->dbMaster;
170 }
171 }
$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 197 of file Database.php.

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

References PHPMailer\PHPMailer\$params, $query, and $stmt.

◆ 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 276 of file Database.php.

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

References PHPMailer\PHPMailer\$params, and $stmt.

◆ 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 255 of file Database.php.

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

References PHPMailer\PHPMailer\$params, and $stmt.

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: