ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Database.php
Go to the documentation of this file.
1<?php
2namespace SimpleSAML;
3
20{
21
25 private static $instance = array();
26
30 private $dbMaster;
31
35 private $dbSlaves = array();
36
40 private $tablePrefix;
41
45 private $lastError;
46
47
55 public static function getInstance($altConfig = null)
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 }
69
70
76 private function __construct($config)
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 }
107
108
116 private static function generateInstanceId($config)
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 }
131
132
144 private function connect($dsn, $username, $password, $options)
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 }
155
156
163 private function getSlave()
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 }
172
173
181 public function applyPrefix($table)
182 {
183 return $this->tablePrefix.$table;
184 }
185
186
197 private function query($db, $stmt, $params)
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 }
222
223
233 private function exec($db, $stmt)
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 }
245
246
255 public function write($stmt, $params = array())
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 }
266
267
276 public function read($stmt, $params = array())
277 {
278 $db = $this->getSlave();
279
280 return $this->query($db, $stmt, $params);
281 }
282
283
289 public function getLastError()
290 {
291 return $this->lastError;
292 }
293}
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.
Definition: Database.php:289
write($stmt, $params=array())
This executes queries directly on the master.
Definition: Database.php:255
static getInstance($altConfig=null)
Retrieves the current database instance.
Definition: Database.php:55
query($db, $stmt, $params)
This function queries the database.
Definition: Database.php:197
connect($dsn, $username, $password, $options)
This function connects to a database.
Definition: Database.php:144
exec($db, $stmt)
This function queries the database without using a prepared statement.
Definition: Database.php:233
read($stmt, $params=array())
This executes queries on a database server that is determined by this::getSlave().
Definition: Database.php:276
$tablePrefix
Prefix to apply to the tables.
Definition: Database.php:40
applyPrefix($table)
This function simply applies the table prefix to a supplied table name.
Definition: Database.php:181
__construct($config)
Private constructor that restricts instantiation to getInstance().
Definition: Database.php:76
$dbMaster
PDO Object for the Master database server.
Definition: Database.php:30
$lastError
Array with information on the last error occurred.
Definition: Database.php:45
static generateInstanceId($config)
Generate an Instance ID based on the database configuration.
Definition: Database.php:116
getSlave()
This function randomly selects a slave database server to query.
Definition: Database.php:163
static getInstance($instancename='simplesaml')
Get a configuration file by its instance name.
$password
Definition: cron.php:14
$config
Definition: bootstrap.php:15
foreach($paths as $path) $dsn
Definition: migrateto20.php:56
$stmt
Attribute-related utility methods.
$query
if(empty($password)) $table
Definition: pwgen.php:24