Skip to content

Commit

Permalink
Fixed isInstalled() if there are more BAV installations in different …
Browse files Browse the repository at this point in the history
…databases

Fixed undefined variables $stmt
Docblock fixes
  • Loading branch information
Clemens Weiß committed Oct 21, 2016
1 parent 2ad288c commit f64e572
Showing 1 changed file with 62 additions and 27 deletions.
89 changes: 62 additions & 27 deletions classes/dataBackend/pdo/PDODataBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class PDODataBackend extends SQLDataBackend
private $prefix = '';

/**
* @param \PDO $pdo
* @param String $prefix the prefix of the table names.
*/
public function __construct(\PDO $pdo, $prefix = "bav_")
Expand Down Expand Up @@ -205,10 +206,10 @@ public function update()
}
$fileBackend->uninstall();

} catch (Exception $e) {
} catch (\Exception $e) {
try {
if ($useTA) {
$this->pdo->rollback();
$this->pdo->rollBack();

}
throw $e;
Expand Down Expand Up @@ -260,7 +261,7 @@ public function install()
FOREIGN KEY (bank) REFERENCES {$this->prefix}bank(id)
)$createOptions"
);

try {
$this->pdo->exec("CREATE INDEX bic ON {$this->prefix}agency (bic)");

Expand Down Expand Up @@ -359,40 +360,47 @@ protected function getNewBank($bankID)
return $this->getBankObject($result);

} catch (\PDOException $e) {
$stmt->closeCursor();
if (isset($stmt)) {
$stmt->closeCursor();
}
throw new DataBackendIOException();

} catch (MissingAttributesDataBackendIOException $e) {
$stmt->closeCursor();
if (isset($stmt)) {
$stmt->closeCursor();
}
throw new \LogicException($e);

}
}

/**
* @param array $result
* @return bool
*/
private function isValidBankResult(Array $result)
{
return array_key_exists('id', $result)
&& array_key_exists('validator', $result);
&& array_key_exists('validator', $result);
}

/**
* @param array $result
* @return bool
*/
private function isValidAgencyResult(Array $result)
{
return array_key_exists('id', $result)
&& array_key_exists('name', $result)
&& array_key_exists('shortTerm', $result)
&& array_key_exists('city', $result)
&& array_key_exists('postcode', $result)
&& array_key_exists('bic', $result)
&& array_key_exists('pan', $result);
&& array_key_exists('name', $result)
&& array_key_exists('shortTerm', $result)
&& array_key_exists('city', $result)
&& array_key_exists('postcode', $result)
&& array_key_exists('bic', $result)
&& array_key_exists('pan', $result);
}

/**
* @param array $fetchedResult
* @return Bank
* @throws MissingAttributesDataBackendIOException
*/
Expand All @@ -406,6 +414,8 @@ private function getBankObject(Array $fetchedResult)
}

/**
* @param Bank $bank
* @param array $fetchedResult
* @return Agency
* @throws MissingAttributesDataBackendIOException
*/
Expand All @@ -432,6 +442,7 @@ private function getAgencyObject(Bank $bank, Array $fetchedResult)
}

/**
* @param Bank $bank
* @throws DataBackendException
* @return Agency
* @see DataBackend::getMainAgency()
Expand All @@ -454,17 +465,22 @@ public function getMainAgency(Bank $bank)
return $this->getAgencyObject($bank, $result);

} catch (\PDOException $e) {
$stmt->closeCursor();
if (isset($stmt)) {
$stmt->closeCursor();
}
throw new DataBackendIOException($e->getMessage(), 0, $e);

} catch (MissingAttributesDataBackendIOException $e) {
$stmt->closeCursor();
if (isset($stmt)) {
$stmt->closeCursor();
}
throw new \LogicException($e);

}
}

/**
* @param Bank $bank
* @throws DataBackendException
* @return Agency[]
* @see DataBackend::getAgenciesForBank()
Expand All @@ -488,11 +504,15 @@ public function getAgenciesForBank(Bank $bank)
return $agencies;

} catch (\PDOException $e) {
$stmt->closeCursor();
if (isset($stmt)) {
$stmt->closeCursor();
}
throw new DataBackendIOException($e->getMessage(), 0, $e);

} catch (MissingAttributesDataBackendIOException $e) {
$stmt->closeCursor();
if (isset($stmt)) {
$stmt->closeCursor();
}
throw new \LogicException($e);

}
Expand Down Expand Up @@ -523,7 +543,9 @@ public function getLastUpdate()
return $result["value"];

} catch (\PDOException $e) {
$stmt->closeCursor();
if (isset($stmt)) {
$stmt->closeCursor();
}
throw new DataBackendIOException($e->getMessage(), $e->getCode(), $e);

}
Expand All @@ -544,17 +566,19 @@ public function isInstalled()
"SELECT count(*) FROM sqlite_master
WHERE type='table' AND name = '{$this->prefix}meta'";
break;

default:
$dbname = $this->pdo->query('select database()')->fetchColumn();
$query =
"SELECT CASE WHEN EXISTS(
(SELECT * FROM information_schema.tables
WHERE table_name='{$this->prefix}meta')
WHERE table_name='{$this->prefix}meta'
AND table_schema='$dbname')
) THEN 1 ELSE 0 END";
break;

}

$stmt = $this->statementContainer->prepare($query);
$stmt->execute();
$result = $stmt->fetch();
Expand All @@ -566,7 +590,9 @@ public function isInstalled()
return $result[0] == 1;

} catch (\PDOException $e) {
$stmt->closeCursor();
if (isset($stmt)) {
$stmt->closeCursor();
}
throw new DataBackendIOException($e->getMessage(), 0, $e);

}
Expand All @@ -577,6 +603,7 @@ public function isInstalled()
*
* @param string $bic BIC
* @return bool
* @throws DataBackendIOException
*/
public function isValidBIC($bic)
{
Expand All @@ -585,12 +612,14 @@ public function isValidBIC($bic)
"SELECT bic FROM {$this->prefix}agency WHERE bic = :bic GROUP BY (bic)"
);
$stmt->execute(array(":bic" => $bic));

$rows = $stmt->fetchAll();
return ! empty($rows);

} catch (\PDOException $e) {
$stmt->closeCursor();
if (isset($stmt)) {
$stmt->closeCursor();
}
throw new DataBackendIOException($e->getMessage(), 0, $e);

}
Expand All @@ -601,6 +630,8 @@ public function isValidBIC($bic)
*
* @param string $bic BIC
* @return Agency[]
* @throws DataBackendIOException
* @throws \LogicException
*/
public function getBICAgencies($bic)
{
Expand All @@ -619,16 +650,20 @@ public function getBICAgencies($bic)
return $agencies;

} catch (\PDOException $e) {
$stmt->closeCursor();
if (isset($stmt)) {
$stmt->closeCursor();
}
throw new DataBackendIOException($e->getMessage(), 0, $e);

} catch (MissingAttributesDataBackendIOException $e) {
$stmt->closeCursor();
if (isset($stmt)) {
$stmt->closeCursor();
}
throw new \LogicException($e);

}
}

public function free()
{
parent::free();
Expand Down

0 comments on commit f64e572

Please sign in to comment.