ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Database.php
Go to the documentation of this file.
1 <?php
2 namespace SimpleSAML;
3 
19 class Database
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  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  }
109 
110 
118  private static function generateInstanceId($config)
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  }
133 
134 
146  private function connect($dsn, $username, $password, $options)
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  }
157 
158 
165  private function getSlave()
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  }
174 
175 
183  public function applyPrefix($table)
184  {
185  return $this->tablePrefix.$table;
186  }
187 
188 
199  private function query($db, $stmt, $params)
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  }
224 
225 
235  private function exec($db, $stmt)
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  }
247 
248 
257  public function write($stmt, $params = array())
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  }
268 
269 
278  public function read($stmt, $params = array())
279  {
280  $db = $this->getSlave();
281 
282  return $this->query($db, $stmt, $params);
283  }
284 
285 
291  public function getLastError()
292  {
293  return $this->lastError;
294  }
295 }
$params
Definition: disable.php:11
static getInstance($altConfig=null)
Retrieves the current database instance.
Definition: Database.php:55
exec($db, $stmt)
This function queries the database without using a prepared statement.
Definition: Database.php:235
__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
static generateInstanceId($config)
Generate an Instance ID based on the database configuration.
Definition: Database.php:118
$tablePrefix
Prefix to apply to the tables.
Definition: Database.php:40
connect($dsn, $username, $password, $options)
This function connects to a database.
Definition: Database.php:146
getSlave()
This function randomly selects a slave database server to query.
Definition: Database.php:165
$password
Definition: pwgen.php:17
Attribute-related utility methods.
write($stmt, $params=array())
This executes queries directly on the master.
Definition: Database.php:257
query($db, $stmt, $params)
This function queries the database.
Definition: Database.php:199
getLastError()
Return an array with information about the last operation performed in the database.
Definition: Database.php:291
$query
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().
Definition: Database.php:278
if(empty($password)) $table
Definition: pwgen.php:24
$lastError
Array with information on the last error occurred.
Definition: Database.php:45
applyPrefix($table)
This function simply applies the table prefix to a supplied table name.
Definition: Database.php:183
static getInstance($instancename='simplesaml')
Get a configuration file by its instance name.
if(!isset($_REQUEST['ReturnTo'])) if(!isset($_REQUEST['AuthId'])) $options
Definition: as_login.php:20