163 lines
3.4 KiB
PHP
163 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace database;
|
|
|
|
include_once "../../global.inc.php";
|
|
include_once PATH_CONFIG . "/config.db.php";
|
|
|
|
/**
|
|
* The SQLManager manage the SQL-Connection for an API
|
|
* @author stubbfel
|
|
* @since 20.06.2013
|
|
*/
|
|
abstract class SqlManager {
|
|
|
|
/**
|
|
* Variable for address of the server
|
|
* @var string
|
|
*/
|
|
private $serverAddress;
|
|
|
|
/**
|
|
* Variable for name of the databaseuser
|
|
* @var string
|
|
*/
|
|
private $userName;
|
|
|
|
/**
|
|
* Variable for name of the database
|
|
* @var string
|
|
*/
|
|
private $dbName;
|
|
|
|
/**
|
|
* Variable for password of the user
|
|
* @var string
|
|
*/
|
|
private $userPw;
|
|
|
|
/**
|
|
* Variable for link of the connection to the server
|
|
* @var connection
|
|
*/
|
|
private $link;
|
|
|
|
/**
|
|
* String for an and-operrator
|
|
* @var string
|
|
*/
|
|
protected static $andTerm = " and ";
|
|
|
|
/**
|
|
* String for an or-operrator
|
|
* @var string
|
|
*/
|
|
protected static $orTerm = " or ";
|
|
|
|
/**
|
|
* String for quotes in a query
|
|
* @var string
|
|
*/
|
|
protected static $quoteTerm = "\"";
|
|
|
|
/**
|
|
* String for open Bracket in a query
|
|
* @var string
|
|
*/
|
|
protected static $openBracket = "(";
|
|
|
|
/**
|
|
* String for close Bracket in a query
|
|
* @var string
|
|
*/
|
|
protected static $closeBracket = ")";
|
|
|
|
/**
|
|
* Default-Constructor
|
|
*/
|
|
public function __construct() {
|
|
$this->serverAddress = \config\DBConfig::$sqlServer;
|
|
$this->dbName = \config\DBConfig::$sqlDBName;
|
|
$this->userName = \config\DBConfig::$sqlDBUser;
|
|
$this->userPw = \config\DBConfig::$sqlDBUserPW
|
|
|
|
;
|
|
}
|
|
|
|
/**
|
|
* Default-DEConstructor
|
|
*/
|
|
public function __destruct() {
|
|
|
|
// close connection
|
|
$this->closeConnection();
|
|
|
|
// delete connection parameter
|
|
unset($this->serverAddress);
|
|
unset($this->dbName);
|
|
unset($this->userName);
|
|
unset($this->userPW
|
|
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Method setup the connection to the Database
|
|
*/
|
|
public function connect() {
|
|
$this->link = mysql_connect($this->serverAddress , $this->userName , $this->userPw);
|
|
if (!$this->link) {
|
|
exit("No Connection: " . mysql_error());
|
|
}
|
|
$selected = mysql_select_db($this->dbName , $this->link);
|
|
if (!$selected) {
|
|
exit("No DB: " . mysql_error());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Method close the connection
|
|
*/
|
|
public function closeConnection() {
|
|
if (
|
|
|
|
$this->link) {
|
|
mysql_close($this->link);
|
|
unset($this->link);
|
|
$this->link = null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Method send a query to the Datebase and return the result
|
|
* @param string $query
|
|
|
|
|
|
* @return result[num][assoc]
|
|
*/
|
|
protected function query($query) {
|
|
|
|
// send error
|
|
$mysqlResult = mysql_query($query, $this->link );
|
|
if (!$mysqlResult) {
|
|
// echo $query;
|
|
exit("Query error: " . mysql_error());
|
|
}
|
|
|
|
// fetch result
|
|
$rowNums = mysql_num_rows($mysqlResult);
|
|
$result = array();
|
|
for ($i = 0; $i < $rowNums; $i++) {
|
|
$row = mysql_fetch_assoc($mysqlResult);
|
|
$result[$i] = $row;
|
|
}
|
|
|
|
// call gc
|
|
mysql_free_result($mysqlResult);
|
|
|
|
return $result;
|
|
}
|
|
}
|
|
|
|
?>
|