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 AppDriver 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
|
|
|
return $this->driver->createConnection($options); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $query |
54
|
|
|
* |
55
|
|
|
* @return void |
56
|
|
|
*/ |
57
|
|
|
private function callCallbacks(string $query): void |
58
|
|
|
{ |
59
|
|
|
foreach ($this->callbacks as $callback) { |
60
|
|
|
$callback($query); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @inheritDoc |
66
|
|
|
*/ |
67
|
|
|
public function addQueryCallback(Closure $callback): void |
68
|
|
|
{ |
69
|
|
|
$this->callbacks[] = $callback; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @inheritDoc |
74
|
|
|
*/ |
75
|
|
|
public function multiQuery(string $query) |
76
|
|
|
{ |
77
|
|
|
$result = $this->driver->multiQuery($query); |
78
|
|
|
// Call the query callbacks. |
79
|
|
|
$this->callCallbacks($query); |
80
|
|
|
return $result; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @inheritDoc |
85
|
|
|
*/ |
86
|
|
|
public function result(string $query, int $field = -1) |
87
|
|
|
{ |
88
|
|
|
$result = $this->driver->result($query, $field); |
89
|
|
|
// Call the query callbacks. |
90
|
|
|
$this->callCallbacks($query); |
91
|
|
|
return $result; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @inheritDoc |
96
|
|
|
*/ |
97
|
|
|
public function execute(string $query) |
98
|
|
|
{ |
99
|
|
|
$result = $this->driver->execute($query); |
100
|
|
|
// Call the query callbacks. |
101
|
|
|
$this->callCallbacks($query); |
102
|
|
|
return $result; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|