1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lagdo\DbAdmin\Db; |
4
|
|
|
|
5
|
|
|
use Lagdo\DbAdmin\Driver\Driver; |
6
|
|
|
use Closure; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Add callbacks to the driver features. |
10
|
|
|
*/ |
11
|
|
|
class CallbackDriver extends Driver |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
private array $callbacks = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param Driver $driver |
20
|
|
|
*/ |
21
|
|
|
public function __construct(private Driver $driver) |
22
|
|
|
{ |
23
|
|
|
// "Clone" the driver instance. |
24
|
|
|
$this->utils = $driver->utils; |
25
|
|
|
$this->config = $driver->config; |
26
|
|
|
$this->mainConnection = $driver->mainConnection; |
27
|
|
|
$this->connection = $driver->connection; |
28
|
|
|
|
29
|
|
|
$this->server = $driver->server; |
30
|
|
|
$this->database = $driver->database; |
31
|
|
|
$this->table = $driver->table; |
32
|
|
|
$this->query = $driver->query; |
33
|
|
|
$this->grammar = $driver->grammar; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @inheritDoc |
38
|
|
|
*/ |
39
|
|
|
public function name() |
40
|
|
|
{ |
41
|
|
|
return $this->driver->name(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @inheritDoc |
46
|
|
|
*/ |
47
|
|
|
public function createConnection(array $options) |
48
|
|
|
{ |
49
|
|
|
$this->connection = $this->driver->createConnection($options); |
50
|
|
|
return $this->connection; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $query |
55
|
|
|
* |
56
|
|
|
* @return void |
57
|
|
|
*/ |
58
|
|
|
private function callCallbacks(string $query): void |
59
|
|
|
{ |
60
|
|
|
foreach ($this->callbacks as $callback) { |
61
|
|
|
$callback($query); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @inheritDoc |
67
|
|
|
*/ |
68
|
|
|
public function addQueryCallback(Closure $callback): void |
69
|
|
|
{ |
70
|
|
|
$this->callbacks[] = $callback; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @inheritDoc |
75
|
|
|
*/ |
76
|
|
|
public function multiQuery(string $query) |
77
|
|
|
{ |
78
|
|
|
$result = $this->driver->multiQuery($query); |
79
|
|
|
// Call the query callbacks. |
80
|
|
|
$this->callCallbacks($query); |
81
|
|
|
return $result; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @inheritDoc |
86
|
|
|
*/ |
87
|
|
|
public function result(string $query, int $field = -1) |
88
|
|
|
{ |
89
|
|
|
$result = $this->driver->result($query, $field); |
90
|
|
|
// Call the query callbacks. |
91
|
|
|
$this->callCallbacks($query); |
92
|
|
|
return $result; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @inheritDoc |
97
|
|
|
*/ |
98
|
|
|
public function execute(string $query) |
99
|
|
|
{ |
100
|
|
|
$result = $this->driver->execute($query); |
101
|
|
|
// Call the query callbacks. |
102
|
|
|
$this->callCallbacks($query); |
103
|
|
|
return $result; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|