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

Bugfix. While SimpleSAML\Database::query() returns a PDOStatement,...

Bugfix. While SimpleSAML\Database::query() returns a PDOStatement, SimpleSAML\Database::exec() doesn't. Make both return an integer with the amount of rows affected. Add a return value to SimpleSAML_Metadata_MetadataStorageHandlerPdo::initDatabase().
parent c20bf182
No related branches found
No related tags found
No related merge requests found
...@@ -260,14 +260,15 @@ class Database ...@@ -260,14 +260,15 @@ class Database
* @param string $stmt Prepared SQL statement * @param string $stmt Prepared SQL statement
* @param array $params Parameters * @param array $params Parameters
* *
* @return \PDOStatement object * @return int The number of rows affected by the query.
*/ */
public function write($stmt, $params = array()) public function write($stmt, $params = array())
{ {
$db = $this->dbMaster; $db = $this->dbMaster;
if (is_array($params)) { if (is_array($params)) {
return $this->query($db, $stmt, $params); $obj = $this->query($db, $stmt, $params);
return $obj->rowCount();
} else { } else {
return $this->exec($db, $stmt); return $this->exec($db, $stmt);
} }
......
...@@ -196,18 +196,18 @@ class SimpleSAML_Metadata_MetaDataStorageHandlerPdo extends SimpleSAML_Metadata_ ...@@ -196,18 +196,18 @@ class SimpleSAML_Metadata_MetaDataStorageHandlerPdo extends SimpleSAML_Metadata_
); );
if ($retrivedEntityIDs !== false && count($retrivedEntityIDs) > 0) { if ($retrivedEntityIDs !== false && count($retrivedEntityIDs) > 0) {
$stmt = $this->db->write( $rows = $this->db->write(
"UPDATE $tableName SET entity_data = :entity_data WHERE entity_id = :entity_id", "UPDATE $tableName SET entity_data = :entity_data WHERE entity_id = :entity_id",
$params $params
); );
} else { } else {
$stmt = $this->db->write( $rows = $this->db->write(
"INSERT INTO $tableName (entity_id, entity_data) VALUES (:entity_id, :entity_data)", "INSERT INTO $tableName (entity_id, entity_data) VALUES (:entity_id, :entity_data)",
$params $params
); );
} }
return 1 === $stmt->rowCount(); return $rows === 1;
} }
...@@ -229,16 +229,29 @@ class SimpleSAML_Metadata_MetaDataStorageHandlerPdo extends SimpleSAML_Metadata_ ...@@ -229,16 +229,29 @@ class SimpleSAML_Metadata_MetaDataStorageHandlerPdo extends SimpleSAML_Metadata_
/** /**
* Initialize the configured database * Initialize the configured database
*
* @return int|false The number of SQL statements successfully executed, false if some error occurred.
*/ */
public function initDatabase() public function initDatabase()
{ {
$stmt = 0;
$fine = true;
foreach ($this->supportedSets as $set) { foreach ($this->supportedSets as $set) {
$tableName = $this->getTableName($set); $tableName = $this->getTableName($set);
$this->db->write( $rows = $this->db->write(
"CREATE TABLE IF NOT EXISTS $tableName (entity_id VARCHAR(255) PRIMARY KEY NOT NULL, entity_data ". "CREATE TABLE IF NOT EXISTS $tableName (entity_id VARCHAR(255) PRIMARY KEY NOT NULL, entity_data ".
"TEXT NOT NULL)" "TEXT NOT NULL)"
); );
if ($rows === 0) {
$fine = false;
} else {
$stmt += $rows;
}
}
if (!$fine) {
return false;
} }
return $stmt;
} }
} }
...@@ -251,7 +251,7 @@ class SimpleSAML_DatabaseTest extends PHPUnit_Framework_TestCase ...@@ -251,7 +251,7 @@ class SimpleSAML_DatabaseTest extends PHPUnit_Framework_TestCase
"INSERT INTO $table (ssp_key, ssp_value) VALUES (:ssp_key, :ssp_value)", "INSERT INTO $table (ssp_key, ssp_value) VALUES (:ssp_key, :ssp_value)",
array('ssp_key' => array($ssp_key, PDO::PARAM_INT), 'ssp_value' => $ssp_value) array('ssp_key' => array($ssp_key, PDO::PARAM_INT), 'ssp_value' => $ssp_value)
); );
$this->assertEquals(1, $stmt->rowCount(), "Could not insert data into $table."); $this->assertEquals(1, $stmt, "Could not insert data into $table.");
$query2 = $this->db->read("SELECT * FROM $table WHERE ssp_key = :ssp_key", array('ssp_key' => $ssp_key)); $query2 = $this->db->read("SELECT * FROM $table WHERE ssp_key = :ssp_key", array('ssp_key' => $ssp_key));
$data = $query2->fetch(); $data = $query2->fetch();
......
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