|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Lagdo\DbAdmin\Db\Service\Audit; |
|
4
|
|
|
|
|
5
|
|
|
use Lagdo\DbAdmin\Driver\Db\AbstractConnection; |
|
6
|
|
|
use Lagdo\DbAdmin\Driver\Db\StatementInterface; |
|
7
|
|
|
use Lagdo\DbAdmin\Driver\DriverInterface; |
|
8
|
|
|
use Lagdo\Facades\Logger; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Connection to the audit database |
|
12
|
|
|
*/ |
|
13
|
|
|
class ConnectionProxy |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var AbstractConnection|null |
|
17
|
|
|
*/ |
|
18
|
|
|
private AbstractConnection|null $connection = null; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The constructor |
|
22
|
|
|
* |
|
23
|
|
|
* @param DriverInterface $driver |
|
24
|
|
|
* @param array $database |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct(private DriverInterface $driver, array $database) |
|
27
|
|
|
{ |
|
28
|
|
|
$connection = $driver->createConnection($database); |
|
29
|
|
|
if ($connection->open($database['name'], $database['schema'] ?? '')) { |
|
30
|
|
|
$this->connection = $connection; |
|
31
|
|
|
} |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return string |
|
36
|
|
|
*/ |
|
37
|
|
|
public function jush(): string |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->driver->jush(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @return bool |
|
44
|
|
|
*/ |
|
45
|
|
|
public function connected(): bool |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->connection !== null; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param string $message |
|
52
|
|
|
* |
|
53
|
|
|
* @return void |
|
54
|
|
|
*/ |
|
55
|
|
|
public function logWarning(string $message): void |
|
56
|
|
|
{ |
|
57
|
|
|
Logger::warning($message, [ |
|
58
|
|
|
'message' => $this->connection?->error() ?? 'Unable to connect to the audit database.', |
|
59
|
|
|
]); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param string $query |
|
64
|
|
|
* @param array|null $values |
|
65
|
|
|
* |
|
66
|
|
|
* @return bool|StatementInterface |
|
67
|
|
|
*/ |
|
68
|
|
|
public function executeQuery(string $query, array|null $values = null): bool|StatementInterface |
|
69
|
|
|
{ |
|
70
|
|
|
if ($this->connection === null) { |
|
71
|
|
|
return false; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if ($values === null) { |
|
75
|
|
|
return $this->connection->query($query); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$st = $this->connection->prepareStatement($query); |
|
79
|
|
|
return $this->connection->executeStatement($st, $values) ?? false; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|