forked from tmanternach/WebSysLog
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdbhelper.php
104 lines (85 loc) · 2.41 KB
/
dbhelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
/*
Simple Database Object which holds all data required for connecting to a database.
- Such Object automatically opens a connection if it's needed
- When this Object is deconstructed, an open mysql-connection will be automatically closed!
*/
class DBObject {
private $hostname;
private $username;
private $password;
private $con = NULL;
private $database;
public function getDBName(){
return $this->database;
}
public function getUserName(){
return $this->username;
}
public function getDBCon(){
if(!isset($this->con)){
$this->connect();
}
return $this->con;
}
private function connect(){
$con = mysql_connect(
$this->hostname,
$this->username,
$this->password
);
if(!$con){
die('Could not connect: ' . mysql_error()
. debug_print_backtrace());
} else {
$this->con = $con;
set_time_limit(300);
mysql_select_db($this->database, $con);
}
}
function __construct($hostname, $username, $password, $database){
$this->hostname = $hostname;
$this->username = $username;
$this->password = $password;
$this->database = $database;
}
function __destruct(){
if(isset($this->con)){
mysql_close($this->con);
}
}
}
class TableObject {
private $DBObject;
private $TableName;
private $ColumnTranslations;
public function __construct($DBObject, $TableName, $ColumnTranslations){
if(!is_object($DBObject) || !(get_class($DBObject) == 'DBObject')){
die("First argument isn't a DBObject!". debug_print_backtrace());
}
if(isset($ColumnTranslations) && !is_array($ColumnTranslations)){
die("ColumnTranslation isn't an array!" . debug_print_backtrace());
}
$this->DBObject = $DBObject;
$this->TableName = $TableName;
if(isset($ColumnTranslations)){
$this->ColumnTranslations = $ColumnTranslations;
} else {
$this->ColumnTranslations = NULL;
}
//var_dump($this->ColumnTranslations);
}
public function tableName(){
return $this->DBObject->getDBName() . '.' . $this->TableName;
}
public function long_columnName($columnIdentifier){
return $this->tableName() . '.' . $this->columnName($columnIdentifier);
}
public function columnName($columnIdentifier){
if(array_key_exists($columnIdentifier, $this->ColumnTranslations)){
return $this->ColumnTranslations[$columnIdentifier];
}
return $columnIdentifier;
}
}
?>