Skip to content
Snippets Groups Projects
Commit 0528937a authored by Jaime Perez Crespo's avatar Jaime Perez Crespo
Browse files

Minor improvements to SimpleSAML\Database.

parent 84547358
No related branches found
No related tags found
No related merge requests found
...@@ -2,17 +2,15 @@ ...@@ -2,17 +2,15 @@
namespace SimpleSAML; namespace SimpleSAML;
/** /**
* This file implements functions to read and write to a group of database * This file implements functions to read and write to a group of database servers.
* servers.
* *
* This database class supports a single database, or a master/slave * This database class supports a single database, or a master/slave configuration with as many defined slaves as a
* configuration with as many defined slaves as a user would like. * user would like.
* *
* The goal of this class is to provide a single mechanism to connect to a database * The goal of this class is to provide a single mechanism to connect to a database that can be reused by any component
* that can be reused by any component within SimpleSAMLphp including modules. * within SimpleSAMLphp including modules. When using this class, the global configuration should be passed here, but in
* When using this class, the global configuration should be passed here, but * the case of a module that has a good reason to use a different database, such as sqlauth, an alternative config file
* in the case of a module that has a good reason to use a different database, * can be provided.
* such as sqlauth, an alternative config file can be provided.
* *
* @author Tyler Antonio, University of Alberta. <tantonio@ualberta.ca> * @author Tyler Antonio, University of Alberta. <tantonio@ualberta.ca>
* @package SimpleSAMLphp * @package SimpleSAMLphp
...@@ -32,8 +30,7 @@ class Database ...@@ -32,8 +30,7 @@ class Database
private $dbMaster; private $dbMaster;
/** /**
* Array of PDO Objects for configured database * Array of PDO Objects for configured database slaves
* slaves
*/ */
private $dbSlaves = array(); private $dbSlaves = array();
...@@ -112,8 +109,7 @@ class Database ...@@ -112,8 +109,7 @@ class Database
/** /**
* Generate an Instance ID based on the database * Generate an Instance ID based on the database configuration.
* configuration.
* *
* @param \SimpleSAML_Configuration $config Configuration class * @param \SimpleSAML_Configuration $config Configuration class
* *
...@@ -161,9 +157,8 @@ class Database ...@@ -161,9 +157,8 @@ class Database
/** /**
* This function randomly selects a slave database server * This function randomly selects a slave database server to query. In the event no slaves are configured, it will
* to query. In the event no slaves are configured, it * return the master.
* will return the master.
* *
* @return \PDO object * @return \PDO object
*/ */
...@@ -229,14 +224,13 @@ class Database ...@@ -229,14 +224,13 @@ class Database
/** /**
* This function queries the database without using a * This function queries the database without using a prepared statement.
* prepared statement.
* *
* @param \PDO $db PDO object to use * @param \PDO $db PDO object to use
* @param string $stmt Prepared SQL statement * @param string $stmt An SQL statement to execute, previously escaped.
* *
* @throws \Exception If an error happens while trying to execute the query. * @throws \Exception If an error happens while trying to execute the query.
* @return \PDOStatement object * @return int The number of rows affected.
*/ */
private function exec($db, $stmt) private function exec($db, $stmt)
{ {
...@@ -244,9 +238,7 @@ class Database ...@@ -244,9 +238,7 @@ class Database
assert('is_string($stmt)'); assert('is_string($stmt)');
try { try {
$query = $db->exec($stmt); return $db->exec($stmt);
return $query;
} catch (\PDOException $e) { } catch (\PDOException $e) {
$this->lastError = $db->errorInfo(); $this->lastError = $db->errorInfo();
throw new \Exception("Database error: ".$e->getMessage()); throw new \Exception("Database error: ".$e->getMessage());
...@@ -276,8 +268,7 @@ class Database ...@@ -276,8 +268,7 @@ class Database
/** /**
* This executes queries on a database server * This executes queries on a database server that is determined by this::getSlave().
* that is determined by this::getSlave()
* *
* @param string $stmt Prepared SQL statement * @param string $stmt Prepared SQL statement
* @param array $params Parameters * @param array $params Parameters
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment