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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
|
<?php
declare(strict_types=1);
namespace mystic\forum;
use DateTime;
use DateTimeImmutable;
use mystic\forum\attributes\Column;
use mystic\forum\attributes\NotNull;
use mystic\forum\attributes\PrimaryKey;
use mystic\forum\attributes\References;
use mystic\forum\attributes\Table;
use mystic\forum\exceptions\DatabaseConnectionException;
use mystic\forum\orm\Entity;
use mystic\forum\utils\ArrayUtils;
use mystic\forum\utils\StringUtils;
use PgSql\Connection;
use ReflectionClass;
use ReflectionNamedType;
use ReflectionProperty;
class Database {
private Connection $connection;
private const PRIMARY_KEY = 0b0000_0001;
private const NOT_NULL = 0b0000_0010;
private const REFERENCES = 0b0000_0100;
public function __construct(string $connectionString) {
try {
$conn = \pg_connect($connectionString);
if ($conn !== false)
$this->connection = $conn;
} catch (\ErrorException $ex) {
throw new DatabaseConnectionException($ex->getMessage(), $ex->getCode(), $ex);
}
}
public static function getConnectionString(string $host, string $user, string $password, string $dbname, int $port = 5432): string {
return self::buildConnectionString([
"host" => $host,
"dbname" => $dbname,
"user" => $user,
"password" => $password,
"port" => strval($port),
]);
}
public static function buildConnectionString(array $params): string {
$items = [];
foreach ($params as $key => $value) {
$items []= $key . "='" . self::escapeConnectionStringValue($value) . "'";
}
return implode(" ", $items);
}
private static function escapeConnectionStringValue(string $value): string {
return str_replace([
'\\',
"'",
], [
'\\\\',
"\\'",
], $value);
}
private static function getTableName(string $entityClassName): string {
$reflClass = new ReflectionClass($entityClassName);
$attrs = $reflClass->getAttributes(Table::class);
foreach ($attrs as $attr)
return $attr->newInstance()->tableName;
return "public." . StringUtils::camelToSnake($reflClass->getShortName());
}
private static function getColumnName(ReflectionProperty &$prop): string {
$attrs = $prop->getAttributes(Column::class);
foreach ($attrs as $attr) {
$name = $attr->newInstance()->columnName;
if ($name !== null)
return $name;
}
return StringUtils::camelToSnake($prop->getName());
}
private static function getColumnType(ReflectionProperty &$prop): string {
$attrs = $prop->getAttributes(Column::class);
foreach ($attrs as $attr) {
$type = $attr->newInstance()->columnType;
if ($type !== null)
return $type;
}
$type = $prop->getType();
if (!($type instanceof ReflectionNamedType))
throw new \RuntimeException("Union types or intersection types cannot be converted to a "
. "database type. Please specify one manually using the #[Column] attribute!");
$typeName = $type->getName();
if (!$type->isBuiltin() && $typeName !== \DateTime::class && $typeName !== \DateTimeImmutable::class)
throw new \RuntimeException("User-defined types cannot be converted to a "
. "database type. Please specify one manually using the #[Column] attribute!");
switch ($typeName) {
case \DateTime::class:
case \DateTimeImmutable::class:
return "timestamp with time zone";
case "true":
case "false":
case "bool":
return "boolean";
case "float":
return "double precision";
case "int":
return "bigint";
case "string":
case "null":
return "text";
case "object":
case "array":
case "iterable":
case "callable":
case "mixed":
case "never":
case "void":
default:
throw new \RuntimeException("The type \"$typeName\" cannot be stored in the database");
}
}
private static function getColumnDefinitions(string $entityClassName): array {
$statements = [];
// TODO Foreign keys
$rflEntity = new \ReflectionClass($entityClassName);
$cols = self::getColumns($rflEntity);
foreach ($cols as $colName => $colInfo) {
$rflProp = new \ReflectionProperty($entityClassName, $colInfo["propertyName"]);
$colType = self::getColumnType($rflProp);
$statement = "$colName $colType";
if (($colInfo["flags"] & self::NOT_NULL) !== 0) {
$statement .= " NOT NULL";
}
if (($colInfo["flags"] & self::PRIMARY_KEY) !== 0) {
$statement .= " PRIMARY KEY";
}
if (($colInfo["flags"] & self::REFERENCES) !== 0) {
$statement .= " REFERENCES " . $colInfo["reference"];
}
$statements []= $statement;
}
return $statements;
}
private static function getColumns(ReflectionClass &$rflEntity): array {
return ArrayUtils::assocFromPairs(array_map(function(ReflectionProperty $prop): array {
$flags = 0;
$attrs = $prop->getAttributes();
$reference = "";
foreach ($attrs as $attr) {
$attrName = $attr->getName();
if ($attrName === PrimaryKey::class) {
$flags |= self::PRIMARY_KEY;
} elseif ($attrName === NotNull::class) {
$flags |= self::NOT_NULL;
} elseif ($attrName === References::class) {
$flags |= self::REFERENCES;
$reference = $attr->newInstance()->__toString();
}
}
return [self::getColumnName($prop), [
"propertyName" => $prop->getName(),
"flags" => $flags,
"reference" => $reference,
]];
}, $rflEntity->getProperties(ReflectionProperty::IS_PUBLIC)));
}
private static function getPrimaryKeyColumn(array &$cols): ?string {
foreach ($cols as $col => ["flags" => $flags])
if (($flags & self::PRIMARY_KEY) !== 0)
return $col;
return null;
}
private static function stringifyValue(mixed $value): ?string {
if (is_null($value))
return null;
elseif (is_bool($value))
return $value ? "true" : "false";
elseif (is_scalar($value))
return strval($value);
elseif (is_a($value, \DateTimeInterface::class))
return $value->format("c");
else
throw new \RuntimeException("Don't know how to stringify " . is_object($value) ? get_class($value) : gettype($value));
}
private static function assignValue(Entity &$entity, array $colProps, ?string $value): void {
$propName = $colProps["propertyName"];
$prop = new \ReflectionProperty($entity, $propName);
$type = $prop->getType();
$typedValue = null;
if (!($type instanceof ReflectionNamedType))
throw new \RuntimeException("Union types or intersection types cannot be converted to from a database type.");
$typeName = $type->getName();
if (!$type->isBuiltin() && $typeName !== \DateTime::class && $typeName !== \DateTimeImmutable::class)
throw new \RuntimeException("User-defined types cannot be converted to from a database type.");
switch ($typeName) {
case \DateTime::class:
$typedValue = new \DateTime($value);
break;
case \DateTimeImmutable::class:
$typedValue = new \DateTimeImmutable($value);
break;
case "true":
case "false":
case "bool":
$typedValue = in_array(strtolower($value), ["true","yes","on","1"]) ? true : (in_array(strtolower($value), ["false","no","off","0"]) ? false : null);
break;
case "float":
$typedValue = floatval($value);
break;
case "int":
$typedValue = intval($value);
break;
case "string":
case "null":
$typedValue = strval($value);
break;
case "object":
case "array":
case "iterable":
case "callable":
case "mixed":
case "never":
case "void":
default:
throw new \RuntimeException("The type \"$typeName\" cannot be restored from the database");
}
$entity->{$propName} = $typedValue;
}
private static function getColumnValues(Entity &$entity, array &$cols): array {
$values = [];
foreach ($cols as $colInfo) {
$values []= self::stringifyValue($entity->{$colInfo["propertyName"]} ?? null);
}
return $values;
}
public function insert(Entity &$entity): void {
$entityClassName = get_class($entity);
$tableName = self::getTableName($entityClassName);
$reflClass = new ReflectionClass($entityClassName);
$cols = self::getColumns($reflClass);
$query = "INSERT INTO $tableName VALUES (" . implode(",", ArrayUtils::fill(fn($i) => "$" . ($i + 1), count($cols))) . ");";
$result = \pg_query_params($this->connection, $query, self::getColumnValues($entity, $cols));
if ($result === false)
throw new \RuntimeException("Insert failed: " . \pg_last_error($this->connection));
\pg_free_result($result);
}
public function fetch(Entity &$entity): bool {
$entityClassName = get_class($entity);
$tableName = self::getTableName($entityClassName);
$reflClass = new ReflectionClass($entityClassName);
$cols = self::getColumns($reflClass);
$primaryCol = self::getPrimaryKeyColumn($cols);
if ($primaryCol === null)
throw new \RuntimeException("Fetching an entity requires a primary key column to be specified");
$query = "SELECT * FROM $tableName WHERE $primaryCol = \$1 LIMIT 1;";
$result = \pg_query_params($this->connection, $query, [ $entity->{$cols[$primaryCol]["propertyName"]} ]);
if ($result === false)
throw new \RuntimeException("Fetch failed: " . \pg_last_error($this->connection));
$row = \pg_fetch_assoc($result);
\pg_free_result($result);
if ($row === false)
return false;
foreach ($cols as $colName => $colProps)
self::assignValue($entity, $colProps, $row[$colName]);
return true;
}
public function delete(Entity &$entity): bool {
$entityClassName = get_class($entity);
$tableName = self::getTableName($entityClassName);
$reflClass = new ReflectionClass($entityClassName);
$cols = self::getColumns($reflClass);
$primaryCol = self::getPrimaryKeyColumn($cols);
if ($primaryCol === null)
throw new \RuntimeException("Deleting an entity requires a primary key column to be specified");
$query = "DELETE FROM $tableName WHERE $primaryCol = \$1;";
$result = \pg_query_params($this->connection, $query, [ $entity->{$cols[$primaryCol]["propertyName"]} ]);
if ($result === false)
throw new \RuntimeException("Deletion failed: " . \pg_last_error($this->connection));
$num_affected_rows = \pg_affected_rows($result);
\pg_free_result($result);
return $num_affected_rows >= 1;
}
public function update(Entity &$entity): bool {
$tableName = self::getTableName(get_class($entity));
$reflClass = new ReflectionClass($entity);
$cols = self::getColumns($reflClass);
$primaryCol = self::getPrimaryKeyColumn($cols);
if ($primaryCol === null)
throw new \RuntimeException("Updating an entity requires a primary key column to be specified");
$filteredCols = array_filter(ArrayUtils::asPairs($cols), function($pair) {
return ($pair[1]["flags"] & self::PRIMARY_KEY) === 0;
});
$colDef = implode(", ", array_map(function(array $pair): string {
return implode("=", [ $pair[0], '$' . $pair[1] ]);
}, $filteredCols));
$query = "UPDATE $tableName SET $colDef WHERE $primaryCol = \$1;";
$result = \pg_query_params($this->connection, $query, array_map(fn($c) => $entity->{$c["propertyName"]}, $filteredCols));
if ($result === false)
throw new \RuntimeException("Update failed: " . \pg_last_error($this->connection));
$num_affected_rows = \pg_affected_rows($result);
\pg_free_result($result);
return $num_affected_rows >= 1;
}
public function ensureTable(string $entityClassName): void {
$tableName = self::getTableName($entityClassName);
$colDefs = self::getColumnDefinitions($entityClassName);
$query = "CREATE TABLE IF NOT EXISTS $tableName ();";
foreach ($colDefs as $colDef)
$query .= "ALTER TABLE $tableName ADD COLUMN IF NOT EXISTS $colDef;";
$result = \pg_query($this->connection, $query);
if ($result === false)
throw new \RuntimeException("Table creation failed: " . \pg_last_error($this->connection));
\pg_free_result($result);
}
}
|