-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdbWrapper.class.php
More file actions
170 lines (125 loc) · 5.04 KB
/
dbWrapper.class.php
File metadata and controls
170 lines (125 loc) · 5.04 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
class dbWrapper {
private $connection;
////////////////////////////////////////////////////////////////////////////
public function __construct($DBName, $DBUser, $DBPassword, $DBHost = "localhost", $DBPort = "3306", $DBCharset = "utf8") {
// Establish connnection
try {
$this->connection = new PDO( 'mysql:host=' . $DBHost .';dbname=' . $DBName . ';port=' . $DBPort . ';charset=' . $DBCharset, $DBUser, $DBPassword);
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if (version_compare(PHP_VERSION, '5.3.6') <= 0) {
// Fallback for PHP Versions which ignore the charset-parameter in the connections dsn
$this->connection->exec("SET NAMES " . $DBCharset);
}
} catch (PDOException $exception) {
throw new Exception('Could not connect to database!');
return FALSE;
}
}
////////////////////////////////////////////////////////////////////////////
// Returns a PDO statement where you have to bind the parameters and execute it yourself
public function prepare($sql) {
return new DBStatement($sql, $this->connection);
}
////////////////////////////////////////////////////////////////////////////
public function query($sql, $parameters = false){
// Prevent the script from using SELECT on this method
$sql = str_replace("\t"," ",$sql);
$teil = explode(" ",$sql);
$teil = trim($teil[0]);
// DEBUG
if ((strtolower($teil) == "select" OR strtolower($teil) == "(select" OR strtolower($teil) == "show")){
throw new Exception("The Database Interface doesn't support select queries on the function 'query'. Use getArray instead.");
}
// Perform the query
$dbObject = $this->prepare($sql);
$statement = $dbObject->execute($parameters);
return $dbObject;
} // end of function dbquery()
////////////////////////////////////////////////////////////////////////////
public function getList($sql, $parameters = null){
// Prevent the script from using the method for other cases than 'select' or 'show'
$sql = str_replace("\t"," ",$sql);
$teil = explode(" ",$sql);
$teil = trim($teil[0]);
if (!(strtolower($teil) == "select" OR strtolower($teil) == "(select" OR strtolower($teil) == "show")){
throw new Exception("ERROR: The function 'getList' may not be used for other orders than 'select' or 'show' ");
}
$dbObject = $this->prepare($sql);
$dbObject->execute($parameters);
return $dbObject->getArray();
}
////////////////////////////////////////////////////////////////////////////
public function getRow($sql, $parameters = null){
// Prevent the script from using the method for other cases than 'select' or 'show'
$sql = str_replace("\t"," ",$sql);
$teil = explode(" ",$sql);
$teil = trim($teil[0]);
if (!(strtolower($teil) == "select" OR strtolower($teil) == "(select" OR strtolower($teil) == "show")){
throw new Exception("ERROR: The function 'getRow' may not be used for other orders than 'select' or 'show' ");
}
$dbObject = $this->prepare($sql);
$dbObject->execute($parameters);
return $dbObject->getRow();
}
////////////////////////////////////////////////////////////////////////////
// Get the raw PDO DB Connection
function getConnection () {
return $this->connection;
}
// Get the id of the last inserted dataset
public function getLastInsertId() {
return $this->connection->lastInsertId();
}
} // end of class
class DBStatement {
private $connection;
private $statement;
private $sql;
// Prepare the statement
public function __construct($sql, $connection) {
try {
$this->connection = $connection;
$this->statement = $this->connection->prepare($sql);
$this->sql = $sql;
} catch (PDOException $exception) {
throw new Exception("<br>MySQL-Error-No: " . $exception->getCode() . "<br>MySQL-Error: " . $exception->getMessage() . "<br><br>Performed SQL:<br>".nl2br($this->sql));
}
}
// Bind parameters and execute it
public function execute($parameters = null, $setting2parameter = null) {
// Bind parameters
if ($parameters)
foreach ($parameters as $key => $value) {
if ($setting2parameter[$key]){
$this->statement->bindValue($key, $value, $setting2parameter[$key]);
} else {
$this->statement->bindValue($key, $value);
}
}
// Perform queries
try {
$this->statement->execute();
} catch (PDOException $exception) {
throw new Exception("<br>MySQL-Error-No: " . $exception->getCode() . "<br>MySQL-Error: " . $exception->getMessage() . "<br><br>Performed SQL:<br>".nl2br($this->sql));
}
return $this->statement;
}
// Returns the whole array
public function getArray($fetchType = PDO::FETCH_ASSOC) {
return $this->statement->fetchAll($fetchType);
}
// Returns just one row. Should be used if you only need the one (the first) row.
public function getRow($fetchType = PDO::FETCH_ASSOC) {
return $this->statement->fetch($fetchType);
}
// Get the raw PDO::STATEMENT
public function getDBStatement() {
return $this->statement;
}
// Get the id of the last inserted dataset
public function getLastInsertId() {
return $this->connection->lastInsertId();
}
}
?>