-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathMysqli.php
326 lines (285 loc) · 8.37 KB
/
Mysqli.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<?php /** @noinspection PhpComposerExtensionStubsInspection */
namespace Aternos\Model\Driver\Mysqli;
use Aternos\Model\{Driver\Driver,
Driver\Features\CRUDAbleInterface,
Driver\Features\CRUDQueryableInterface,
ModelInterface,
Query\DeleteQuery,
Query\Generator\SQL,
Query\Query,
Query\QueryResult,
Query\UpdateQuery
};
use Exception;
use mysqli_result;
/**
* Class Mysqli
*
* Inherit this class, overwrite the connect function
* and/or the protected connection specific properties
* and register the new class in the driver factory
* for other credentials or connect specifics
*
* @package Aternos\Model\Driver
*/
class Mysqli extends Driver implements CRUDAbleInterface, CRUDQueryableInterface
{
public const ID = "mysqli";
protected string $id = self::ID;
/**
* Host address
*
* @var string|null
*/
protected ?string $host = null;
/**
* Host port
*
* @var int|null
*/
protected ?int $port = null;
/**
* Authentication username
*
* @var string|null
*/
protected ?string $username = null;
/**
* Authentication password
*
* @var string|null
*/
protected ?string $password = null;
/**
* Socket path or pipe
*
* @var string|null
*/
protected ?string $socket = null;
/**
* Database name
*
* @var string
*/
protected string $database = "data";
/**
* @var \mysqli|null
*/
protected ?\mysqli $connection = null;
/**
* Mysqli constructor.
*
* @param string|null $host
* @param int|null $port
* @param string|null $username
* @param string|null $password
* @param string|null $socket
* @param string|null $database
*/
public function __construct(?string $host = null, ?int $port = null, ?string $username = null, ?string $password = null, ?string $socket = null, ?string $database = null)
{
$this->host = $host ?? $this->host;
$this->port = $port ?? $this->port;
$this->username = $username ?? $this->username;
$this->password = $password ?? $this->password;
$this->socket = $socket ?? $this->socket;
$this->database = $database ?? $this->database;
}
/**
* Connect to database
*
* @throws Exception
*/
protected function connect(): void
{
if (!$this->connection || !@mysqli_ping($this->connection)) {
$this->connection = mysqli_connect($this->host, $this->username, $this->password, $this->database, $this->port, $this->socket);
if (!$this->connection) {
throw new Exception("Could not connect to Mysqli database. Error: " . mysqli_error($this->connection));
}
}
}
/**
* Execute a mysql query
*
* @param string $query
* @return bool|mysqli_result
* @throws Exception
*/
protected function rawQuery(string $query): mysqli_result|bool
{
$this->connect();
$result = mysqli_query($this->connection, $query);
if (mysqli_error($this->connection)) {
throw new Exception("MySQLi Error #" . mysqli_errno($this->connection) . ": " . mysqli_error($this->connection));
}
return $result;
}
/**
* Save the model
*
* @param ModelInterface $model
* @return bool
* @throws Exception
*/
public function save(ModelInterface $model): bool
{
$this->connect();
$table = $model::getName();
$modelValues = get_object_vars($model);
$columns = [];
$values = [];
foreach ($modelValues as $key => $value) {
$columns[] = "`" . $key . "`";
if (is_int($value) || is_float($value)) {
$values[] = $value;
} else if (is_null($value)) {
$values[] = "NULL";
} else {
$values[] = "'" . mysqli_real_escape_string($this->connection, $value) . "'";
}
}
$updates = [];
foreach ($modelValues as $column => $modelValue) {
if (is_int($modelValue) || is_float($modelValue)) {
$updates[] = "`" . $column . "`=" . $modelValue;
} else if (is_null($modelValue)) {
$updates[] = "`" . $column . "`=NULL";
} else {
$updates[] = "`" . $column . "`='" . mysqli_real_escape_string($this->connection, $modelValue) . "'";
}
}
$query = "INSERT INTO `" . $table . "` (" . implode(",", $columns) . ") VALUES (" . implode(",", $values) . ") ON DUPLICATE KEY UPDATE " . implode(",", $updates);
$this->rawQuery($query);
return true;
}
/**
* Get the model
*
* @param class-string<ModelInterface> $modelClass
* @param mixed $id
* @param ModelInterface|null $model
* @return ModelInterface|null
* @throws Exception
*/
public function get(string $modelClass, mixed $id, ?ModelInterface $model = null): ?ModelInterface
{
$this->connect();
$table = $modelClass::getName();
$escapedId = mysqli_real_escape_string($this->connection, $id);
$query = "SELECT * FROM `" . $table . "` WHERE `" . $modelClass::getIdField() . "` = '" . $escapedId . "'";
$result = $this->rawQuery($query);
if (!$result || mysqli_num_rows($result) === 0) {
return null;
}
$row = mysqli_fetch_assoc($result);
if ($model) {
return $model->applyData($row);
}
return $modelClass::getModelFromData($row);
}
/**
* Delete the model
*
* @param ModelInterface $model
* @return bool
* @throws Exception
*/
public function delete(ModelInterface $model): bool
{
$this->connect();
$table = $model::getName();
$id = mysqli_real_escape_string($this->connection, $model->getId());
$query = "DELETE FROM `" . $table . "` WHERE `" . $model->getIdField() . "` = '" . $id . "'";
$this->rawQuery($query);
return true;
}
/**
* Execute a SELECT, UPDATE or DELETE query
*
* @param Query $query
* @return QueryResult
* @throws Exception
*/
public function query(Query $query): QueryResult
{
$this->connect();
$generator = new SQL(function ($value) {
return mysqli_real_escape_string($this->connection, $value);
});
$queryString = $generator->generate($query);
$rawQueryResult = $this->rawQuery($queryString);
$result = new QueryResult((bool)$rawQueryResult);
$result->setQueryString($queryString);
if ($query instanceof UpdateQuery || $query instanceof DeleteQuery) {
$result->setAffectedRows(mysqli_affected_rows($this->connection));
return $result;
}
if (is_bool($rawQueryResult)) {
return $result;
}
while ($row = mysqli_fetch_assoc($rawQueryResult)) {
/** @var class-string<ModelInterface> $modelClass */
$modelClass = $query->modelClassName;
$model = $modelClass::getModelFromData($row);
if ($model) {
$result->add($model);
}
}
return $result;
}
/**
* @param string|null $host
* @return Mysqli
*/
public function setHost(?string $host): Mysqli
{
$this->host = $host;
return $this;
}
/**
* @param int|null $port
* @return Mysqli
*/
public function setPort(?int $port): Mysqli
{
$this->port = $port;
return $this;
}
/**
* @param string|null $username
* @return Mysqli
*/
public function setUsername(?string $username): Mysqli
{
$this->username = $username;
return $this;
}
/**
* @param string|null $password
* @return Mysqli
*/
public function setPassword(?string $password): Mysqli
{
$this->password = $password;
return $this;
}
/**
* @param string|null $socket
* @return Mysqli
*/
public function setSocket(?string $socket): Mysqli
{
$this->socket = $socket;
return $this;
}
/**
* @param string $database
* @return Mysqli
*/
public function setDatabase(string $database): Mysqli
{
$this->database = $database;
return $this;
}
}