Commit c857f2e4 authored by Administrator's avatar Administrator

Update 1 files via Son of Anton

parent fb8477ef
...@@ -12,15 +12,8 @@ class Database ...@@ -12,15 +12,8 @@ class Database
private array $afterDeleteHooks = []; private array $afterDeleteHooks = [];
private array $tableCache = []; private array $tableCache = [];
public function __construct(array $config) public function __construct(string $host = '127.0.0.1', int $port = 3306, string $dbName = '', string $user = 'root', string $pass = '', string $charset = 'utf8mb4')
{ {
$host = $config['host'] ?? '127.0.0.1';
$port = $config['port'] ?? '3306';
$dbName = $config['name'] ?? '';
$user = $config['user'] ?? 'root';
$pass = $config['pass'] ?? '';
$charset = $config['charset'] ?? 'utf8mb4';
$dsn = "mysql:host={$host};port={$port};dbname={$dbName};charset={$charset}"; $dsn = "mysql:host={$host};port={$port};dbname={$dbName};charset={$charset}";
$this->pdo = new \PDO($dsn, $user, $pass, [ $this->pdo = new \PDO($dsn, $user, $pass, [
...@@ -47,6 +40,11 @@ class Database ...@@ -47,6 +40,11 @@ class Database
return $stmt; return $stmt;
} }
public function raw(string $sql): void
{
$this->pdo->exec($sql);
}
public function select(string $sql, array $params = []): array public function select(string $sql, array $params = []): array
{ {
return $this->query($sql, $params)->fetchAll(); return $this->query($sql, $params)->fetchAll();
...@@ -101,13 +99,11 @@ class Database ...@@ -101,13 +99,11 @@ class Database
if (!empty($this->afterUpdateHooks)) { if (!empty($this->afterUpdateHooks)) {
try { try {
// Try to extract entity ID from WHERE clause
$entityId = $this->extractIdFromWhere($where, $whereParams); $entityId = $this->extractIdFromWhere($where, $whereParams);
if ($entityId !== null) { if ($entityId !== null) {
$oldRecord = $this->selectOne("SELECT * FROM `{$table}` WHERE `id` = ?", [$entityId]); $oldRecord = $this->selectOne("SELECT * FROM `{$table}` WHERE `id` = ?", [$entityId]);
} else { } else {
// Fallback: fetch first matching record
$oldRecord = $this->selectOne("SELECT * FROM `{$table}` WHERE {$where} LIMIT 1", $whereParams); $oldRecord = $this->selectOne("SELECT * FROM `{$table}` WHERE {$where} LIMIT 1", $whereParams);
if ($oldRecord && isset($oldRecord['id'])) { if ($oldRecord && isset($oldRecord['id'])) {
$entityId = (int) $oldRecord['id']; $entityId = (int) $oldRecord['id'];
...@@ -145,7 +141,6 @@ class Database ...@@ -145,7 +141,6 @@ class Database
if ($entityId !== null) { if ($entityId !== null) {
$newRecord = $this->selectOne("SELECT * FROM `{$table}` WHERE `id` = ?", [$entityId]); $newRecord = $this->selectOne("SELECT * FROM `{$table}` WHERE `id` = ?", [$entityId]);
} elseif ($oldRecord) { } elseif ($oldRecord) {
// Construct approximate new record from old + changes
$newRecord = array_merge($oldRecord, $data); $newRecord = array_merge($oldRecord, $data);
} }
} catch (\Throwable $e) { } catch (\Throwable $e) {
...@@ -169,7 +164,6 @@ class Database ...@@ -169,7 +164,6 @@ class Database
public function delete(string $table, string $where, array $whereParams = []): int public function delete(string $table, string $where, array $whereParams = []): int
{ {
// Fetch record before deleting
$oldRecord = null; $oldRecord = null;
$entityId = null; $entityId = null;
...@@ -254,37 +248,21 @@ class Database ...@@ -254,37 +248,21 @@ class Database
// HOOK REGISTRATION // HOOK REGISTRATION
// ═══════════════════════════════════════════ // ═══════════════════════════════════════════
/**
* Hook: called after a successful INSERT.
* Callback: function(string $table, array $data, ?int $insertId)
*/
public function onAfterInsert(callable $callback): void public function onAfterInsert(callable $callback): void
{ {
$this->afterInsertHooks[] = $callback; $this->afterInsertHooks[] = $callback;
} }
/**
* Hook: called before an UPDATE executes.
* Callback: function(string $table, array $newData, ?int $entityId, ?array $oldRecord)
*/
public function onBeforeUpdate(callable $callback): void public function onBeforeUpdate(callable $callback): void
{ {
$this->beforeUpdateHooks[] = $callback; $this->beforeUpdateHooks[] = $callback;
} }
/**
* Hook: called after a successful UPDATE.
* Callback: function(string $table, array $changedData, ?int $entityId, ?array $oldRecord, ?array $newRecord)
*/
public function onAfterUpdate(callable $callback): void public function onAfterUpdate(callable $callback): void
{ {
$this->afterUpdateHooks[] = $callback; $this->afterUpdateHooks[] = $callback;
} }
/**
* Hook: called after a successful DELETE.
* Callback: function(string $table, array $oldRecord, ?int $entityId)
*/
public function onAfterDelete(callable $callback): void public function onAfterDelete(callable $callback): void
{ {
$this->afterDeleteHooks[] = $callback; $this->afterDeleteHooks[] = $callback;
...@@ -294,12 +272,8 @@ class Database ...@@ -294,12 +272,8 @@ class Database
// INTERNAL HELPERS // INTERNAL HELPERS
// ═══════════════════════════════════════════ // ═══════════════════════════════════════════
/**
* Try to extract the entity ID from a WHERE clause like `id` = ? or id = ?
*/
private function extractIdFromWhere(string $where, array $params): ?int private function extractIdFromWhere(string $where, array $params): ?int
{ {
// Match patterns: `id` = ?, id = ?, `id`=?
if (preg_match('/^`?id`?\s*=\s*\?/', trim($where)) && !empty($params)) { if (preg_match('/^`?id`?\s*=\s*\?/', trim($where)) && !empty($params)) {
$val = $params[0]; $val = $params[0];
if (is_numeric($val)) { if (is_numeric($val)) {
...@@ -309,9 +283,6 @@ class Database ...@@ -309,9 +283,6 @@ class Database
return null; return null;
} }
/**
* Get last insert ID
*/
public function lastInsertId(): int public function lastInsertId(): int
{ {
return (int) $this->pdo->lastInsertId(); return (int) $this->pdo->lastInsertId();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment