Skip to content

Commit

Permalink
ID#323: introduced type declaration for database connection classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Achatz committed May 15, 2018
1 parent f774d24 commit 8730d3c
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 27 deletions.
68 changes: 54 additions & 14 deletions core/database/AbstractDatabaseHandler.php
Expand Up @@ -201,27 +201,35 @@ abstract class AbstractDatabaseHandler extends APFObject implements DatabaseConn
*
* @param string $host The database host to connect to.
*
* @return $this This instance for further usage.
*
* @author Christian Achatz
* @version
* Version 0.1, 07.05.2012<br />
*/
public function setHost($host) {
public function setHost(string $host) {
$this->dbHost = $host;

return $this;
}

/**
* Defines the database port to connect to.
* <p/>
* Can be used for manual or DI configuration.
*
* @param int $port The database port to connect to.
* @param string $port The database port to connect to.
*
* @return $this This instance for further usage.
*
* @author Christian Achatz
* @version
* Version 0.1, 07.05.2012<br />
*/
public function setPort($port) {
public function setPort(string $port) {
$this->dbPort = $port;

return $this;
}

/**
Expand All @@ -231,12 +239,16 @@ public function setPort($port) {
*
* @param string $name Th name of the database to connect to.
*
* @return $this This instance for further usage.
*
* @author Christian Achatz
* @version
* Version 0.1, 07.05.2012<br />
*/
public function setDatabaseName($name) {
public function setDatabaseName(string $name) {
$this->dbName = $name;

return $this;
}

/**
Expand All @@ -246,12 +258,16 @@ public function setDatabaseName($name) {
*
* @param string $user The database user.
*
* @return $this This instance for further usage.
*
* @author Christian Achatz
* @version
* Version 0.1, 07.05.2012<br />
*/
public function setUser($user) {
public function setUser(string $user) {
$this->dbUser = $user;

return $this;
}

/**
Expand All @@ -261,12 +277,16 @@ public function setUser($user) {
*
* @param string $pass The database password.
*
* @return $this This instance for further usage.
*
* @author Christian Achatz
* @version
* Version 0.1, 07.05.2012<br />
*/
public function setPass($pass) {
public function setPass(string $pass) {
$this->dbPass = $pass;

return $this;
}

/**
Expand All @@ -276,12 +296,16 @@ public function setPass($pass) {
*
* @param string $socket The socket descriptor.
*
* @return $this This instance for further usage.
*
* @author Christian Achatz
* @version
* Version 0.1, 07.05.2012<br />
*/
public function setSocket($socket) {
public function setSocket(string $socket) {
$this->dbSocket = $socket;

return $this;
}

/**
Expand All @@ -291,12 +315,16 @@ public function setSocket($socket) {
*
* @param string $charset The desired character set.
*
* @return $this This instance for further usage.
*
* @author Christian Achatz
* @version
* Version 0.1, 07.05.2012<br />
*/
public function setCharset($charset) {
public function setCharset(string $charset) {
$this->dbCharset = $charset;

return $this;
}

/**
Expand All @@ -306,27 +334,35 @@ public function setCharset($charset) {
*
* @param string $collation The desired collation.
*
* @return $this This instance for further usage.
*
* @author Christian Achatz
* @version
* Version 0.1, 07.05.2012<br />
*/
public function setCollation($collation) {
public function setCollation(string $collation) {
$this->dbCollation = $collation;

return $this;
}

/**
* Enables (true) or disables (false) the internal debugging feature (=statement logging).
* <p/>
* Can be used for manual or DI configuration.
*
* @param boolean $debug <em>True</em> in case the logging feature should be switched on, <em>false</em> otherwise.
* @param string $debug <em>true|1</em> in case the logging feature should be switched on, <em>false|0</em> otherwise.
*
* @return $this This instance for further usage.
*
* @author Christian Achatz
* @version
* Version 0.1, 07.05.2012<br />
*/
public function setDebug($debug) {
public function setDebug(string $debug) {
$this->dbDebug = ($debug == 'true' || $debug == '1') ? true : false;

return $this;
}

/**
Expand All @@ -336,12 +372,16 @@ public function setDebug($debug) {
*
* @param string $logTarget The name of debug log file.
*
* @return $this This instance for further usage.
*
* @author Christian Achatz
* @version
* Version 0.1, 07.05.2012<br />
*/
public function setLogTarget($logTarget) {
public function setLogTarget(string $logTarget) {
$this->dbLogTarget = $logTarget;

return $this;
}

/**
Expand All @@ -367,7 +407,7 @@ public function setup() {
* @version
* Version 0.1, 10.02.2008<br />
*/
public function init($initParam) {
public function init(array $initParam) {

if ($this->isInitialized == false) {

Expand Down Expand Up @@ -484,7 +524,7 @@ protected function initCharsetAndCollation() {
* @version
* Version 0.1, 03.02.2011<br />
*/
protected function getPreparedStatement($namespace, $name, array $params = []) {
protected function getPreparedStatement(string $namespace, string $name, array $params = []) {
try {
$config = $this->getConfiguration($namespace, $name);
} catch (ConfigurationException $e) {
Expand Down
6 changes: 3 additions & 3 deletions core/database/DatabaseConnection.php
Expand Up @@ -57,7 +57,7 @@ public function setup();
* @version
* Version 0.1, 10.02.2008<br />
*/
public function executeStatement($namespace, $statementName, array $params = [], $logStatement = false);
public function executeStatement(string $namespace, string $statementName, array $params = [], bool $logStatement = false);

/**
* Executes a statement applied as a string to the method and returns the
Expand All @@ -73,7 +73,7 @@ public function executeStatement($namespace, $statementName, array $params = [],
* @version
* Version 0.1, 10.02.2008<br />
*/
public function executeTextStatement($statement, $logStatement = false);
public function executeTextStatement(string $statement, bool $logStatement = false);

/**
* Fetches a record from the database using the given result resource.
Expand Down Expand Up @@ -101,7 +101,7 @@ public function fetchData($resultCursor, $type = self::ASSOC_FETCH_MODE);
* @version
* Version 0.1, 23.02.2008<br />
*/
public function escapeValue($value);
public function escapeValue(string $value);

/**
* Returns the amount of rows, that are affected by a previous update or delete call.
Expand Down
6 changes: 3 additions & 3 deletions core/database/MySQLiHandler.php
Expand Up @@ -68,7 +68,7 @@ public function __construct() {
* @version
* Version 0.1, 10.03.2010<br />
*/
public function executeStatement($namespace, $statementFile, array $params = [], $logStatement = false) {
public function executeStatement(string $namespace, string $statementFile, array $params = [], bool $logStatement = false) {

// load statement file content
$statement = $this->getPreparedStatement($namespace, $statementFile, $params);
Expand Down Expand Up @@ -393,7 +393,7 @@ public function getBindNumRows() {
* @version
* Version 0.1, 24.02.2010<br />
*/
public function escapeValue($value) {
public function escapeValue(string $value) {
return $this->dbConn->real_escape_string($value);
}

Expand Down Expand Up @@ -491,7 +491,7 @@ public function getNumRows($result) {
* @version
* Version 0.1, 09.03.2010<br />
*/
public function executeTextStatement($statement, $logStatement = false) {
public function executeTextStatement(string $statement, bool $logStatement = false) {

// log statements in debug mode or when requested explicitly
if ($this->dbDebug == true || $logStatement == true) {
Expand Down
8 changes: 4 additions & 4 deletions core/database/PDOHandler.php
Expand Up @@ -54,7 +54,7 @@ public function __construct() {
}
}

public function init($initParam) {
public function init(array $initParam) {

// set database type for pdo connection
if (isset($initParam['PDO'])) {
Expand Down Expand Up @@ -149,7 +149,7 @@ public function prepareStatement($statement) {
* @version
* Version 0.1, 11.04.2012<br />
*/
public function executeStatement($namespace, $statementFile, array $params = [], $logStatement = false) {
public function executeStatement(string $namespace, string $statementFile, array $params = [], bool $logStatement = false) {
// load statement file content
$statement = $this->getPreparedStatement($namespace, $statementFile, $params);

Expand Down Expand Up @@ -190,7 +190,7 @@ public function executeStatement($namespace, $statementFile, array $params = [],
* @version
* Version 0.1, 11.04.2012<br />
*/
public function executeTextStatement($statement, $logStatement = false) {
public function executeTextStatement(string $statement, bool $logStatement = false) {
// log statements in debug mode or when requested explicitly
if ($this->dbDebug == true || $logStatement == true) {
$this->dbLog->logEntry($this->dbLogTarget, '[PDOHandler::executeTextStatement()] Current statement: ' . $statement, LogEntry::SEVERITY_DEBUG);
Expand Down Expand Up @@ -256,7 +256,7 @@ public function fetchData($pdoStatement, $type = self::ASSOC_FETCH_MODE) {
* @version
* Version 0.1, 11.04.2012<br />
*/
public function escapeValue($value) {
public function escapeValue(string $value) {
$quoted = $this->dbConn->quote($value);

return substr($quoted, 1, strlen($quoted) - 2);
Expand Down
6 changes: 3 additions & 3 deletions core/database/SQLiteHandler.php
Expand Up @@ -69,7 +69,7 @@ public function __construct() {
* @version
* Version 0.1, 10.02.2008<br />
*/
public function executeTextStatement($statement, $logStatement = false) {
public function executeTextStatement(string $statement, bool $logStatement = false) {

if ($logStatement == true) {
$this->dbLog->logEntry($this->dbLogTarget, '[SQLiteHandler::executeTextStatement()] Current statement: ' . $statement, LogEntry::SEVERITY_DEBUG);
Expand Down Expand Up @@ -106,7 +106,7 @@ public function executeTextStatement($statement, $logStatement = false) {
* Version 0.1, 24.02.2008<br />
* Version 0.2, 21.06.2008 (Replaced APPS__ENVIRONMENT with a value from the Registry)<br />
*/
public function executeStatement($namespace, $statementName, array $params = [], $logStatement = false) {
public function executeStatement(string $namespace, string $statementName, array $params = [], bool $logStatement = false) {

$statement = $this->getPreparedStatement($namespace, $statementName, $params);

Expand Down Expand Up @@ -162,7 +162,7 @@ public function fetchData($resultCursor, $type = self::ASSOC_FETCH_MODE) {
* @version
* Version 0.1, 23.02.2008<br />
*/
public function escapeValue($value) {
public function escapeValue(string $value) {
return sqlite_escape_string($value);
}

Expand Down

0 comments on commit 8730d3c

Please sign in to comment.