-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconnection.php
190 lines (159 loc) · 4.99 KB
/
connection.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
class Database
{
public const HOST = "localhost";
public const USERNAME = "root";
public const PASSWORD = "";
public const NAME = "quickfix";
}
abstract class ObjectPool
{
private int $expirationTime;
private array $locked, $unlocked;
private array $objects;
public function __construct()
{
$this->expirationTime = 30000; // 30 seconds
$this->locked = array();
$this->unlocked = array();
$this->objects = array();
}
abstract protected function create(): Object;
abstract public function validate(Object $o): bool;
abstract public function expire(Object $o): void;
public function checkOut(): Object
{
$now = time();
if (count($this->unlocked) > 0) {
foreach ($this->unlocked as $objId => $time) {
$o = $this->objects[$objId];
if (($now - $time) > $this->expirationTime) {
unset($this->unlocked[$objId]);
unset($this->objects[$objId]);
$this->expire($o);
$o = null;
}
else {
if ($this->validate($o)) {
unset($this->unlocked[$objId]);
$this->locked[$objId] = time();
return $o;
}
else {
unset($this->unlocked[$objId]);
unset($this->objects[$objId]);
$this->expire($o);
$o = null;
}
}
}
}
$o = $this->create();
$id = spl_object_hash($o);
$this->objects[$id] = $o;
$this->locked[$id] = time();
return $o;
}
public function checkIn(Object $o): void
{
$id = array_search($o, $this->objects);
unset($this->locked[$id]);
$this->unlocked[$id] = time();
}
}
class Connection{
private mysqli $connection;
public function __construct(String $host, String $dbusername, String $dbpassword, String $dbname)
{
$this->connection = new mysqli($host, $dbusername, $dbpassword, $dbname);
if ($this->connection->connect_errno) {
echo "Failed to connect to MySQL: " . $this->connection -> connect_error;
exit();
}
}
public function close(): void
{
$this->connection->close();
}
public function is_valid(): bool
{
if ($this->connection->connect_errno) {
return false;
}
return true;
}
public function query(String $query): mysqli_result|bool
{
return $this->connection->query($query);
}
public function real_escape_string($string){
return $this->connection->real_escape_string($string);
}
public function insert_id(){
return $this->connection->insert_id;
}
}
class ConnectionPool extends ObjectPool
{
private String $host;
private String $dbusername;
private String $dbpassword;
private String $dbname;
private static ConnectionPool $connectionPool;
private function __construct(String $host, String $dbusername, String $dbpassword, String $dbname)
{
parent::__construct();
$this->host = $host;
$this->dbusername = $dbusername;
$this->dbpassword = $dbpassword;
$this->dbname = $dbname;
}
public static function getInstance(): ConnectionPool
{
if (!isset(self::$connectionPool) || is_null(self::$connectionPool)) {
self::$connectionPool = new ConnectionPool(Database::HOST, Database::USERNAME, Database::PASSWORD, Database::NAME);
}
return self::$connectionPool;
}
protected function create(): Connection
{
return new Connection($this->host, $this->dbusername, $this->dbpassword, $this->dbname);
}
public function validate(Object $conn): bool
{
return $conn->is_valid();
}
public function expire(Object $conn): void
{
$conn->close();
}
public function getConnection(): Connection
{
return parent::checkOut();
}
public function releaseConnection(Connection $conn): void
{
parent::checkIn($conn);
}
}
class QueryHandler{
public static $insert_id;
public static function query(String $query): mysqli_result|bool
{
$connectionPool = ConnectionPool::getInstance();
$connection = $connectionPool->getConnection();
$result = $connection->query($query);
QueryHandler::$insert_id = $connection->insert_id();
$connectionPool->releaseConnection($connection);
return $result;
}
public static function real_escape_string($string)
{
$connectionPool = ConnectionPool::getInstance();
$connection = $connectionPool->getConnection();
$string = $connection->real_escape_string($string);
$connectionPool->releaseConnection($connection);
return $string;
}
}
?>