|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Lagdo\DbAdmin\Db; |
|
4
|
|
|
|
|
5
|
|
|
use Lagdo\DbAdmin\Driver\Driver; |
|
6
|
|
|
use Lagdo\DbAdmin\Driver\DriverInterface; |
|
7
|
|
|
use Closure; |
|
8
|
|
|
|
|
9
|
|
|
use function Jaxon\jaxon; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Add callbacks to the driver features. |
|
13
|
|
|
*/ |
|
14
|
|
|
class AppDriver extends Driver |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var array |
|
18
|
|
|
*/ |
|
19
|
|
|
private array $callbacks = []; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param Driver $driver |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct(protected Driver $driver) |
|
25
|
|
|
{ |
|
26
|
|
|
// "Clone" the driver instance. |
|
27
|
|
|
$this->utils = $driver->utils; |
|
28
|
|
|
$this->config = $driver->config; |
|
29
|
|
|
$this->mainConnection = $driver->mainConnection; |
|
30
|
|
|
$this->connection = $driver->connection; |
|
31
|
|
|
|
|
32
|
|
|
$this->server = $driver->server; |
|
33
|
|
|
$this->database = $driver->database; |
|
34
|
|
|
$this->table = $driver->table; |
|
35
|
|
|
$this->query = $driver->query; |
|
36
|
|
|
$this->grammar = $driver->grammar; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @inheritDoc |
|
41
|
|
|
*/ |
|
42
|
|
|
public function name() |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->driver->name(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return void |
|
49
|
|
|
*/ |
|
50
|
|
|
protected function beforeConnection() |
|
51
|
|
|
{ |
|
52
|
|
|
$this->driver->beforeConnection(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return void |
|
57
|
|
|
*/ |
|
58
|
|
|
protected function configConnection() |
|
59
|
|
|
{ |
|
60
|
|
|
$this->driver->configConnection(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return void |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function openedConnection() |
|
67
|
|
|
{ |
|
68
|
|
|
$this->driver->openedConnection(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @inheritDoc |
|
73
|
|
|
*/ |
|
74
|
|
|
public function createConnection(array $options) |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->driver->createConnection($options); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param string $query |
|
81
|
|
|
* |
|
82
|
|
|
* @return void |
|
83
|
|
|
*/ |
|
84
|
|
|
private function callCallbacks(string $query): void |
|
85
|
|
|
{ |
|
86
|
|
|
foreach ($this->callbacks as $callback) { |
|
87
|
|
|
$callback($query); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @inheritDoc |
|
93
|
|
|
*/ |
|
94
|
|
|
public function addQueryCallback(Closure $callback): void |
|
95
|
|
|
{ |
|
96
|
|
|
$this->callbacks[] = $callback; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @inheritDoc |
|
101
|
|
|
*/ |
|
102
|
|
|
public function multiQuery(string $query) |
|
103
|
|
|
{ |
|
104
|
|
|
$result = $this->driver->multiQuery($query); |
|
105
|
|
|
// Call the query callbacks. |
|
106
|
|
|
$this->callCallbacks($query); |
|
107
|
|
|
return $result; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @inheritDoc |
|
112
|
|
|
*/ |
|
113
|
|
|
public function result(string $query, int $field = -1) |
|
114
|
|
|
{ |
|
115
|
|
|
$result = $this->driver->result($query, $field); |
|
116
|
|
|
// Call the query callbacks. |
|
117
|
|
|
$this->callCallbacks($query); |
|
118
|
|
|
return $result; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @inheritDoc |
|
123
|
|
|
*/ |
|
124
|
|
|
public function execute(string $query) |
|
125
|
|
|
{ |
|
126
|
|
|
$result = $this->driver->execute($query); |
|
127
|
|
|
// Call the query callbacks. |
|
128
|
|
|
$this->callCallbacks($query); |
|
129
|
|
|
return $result; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param array $options |
|
134
|
|
|
* |
|
135
|
|
|
* @return DriverInterface|null |
|
136
|
|
|
*/ |
|
137
|
|
|
public static function createDriver(array $options): ?DriverInterface |
|
138
|
|
|
{ |
|
139
|
|
|
$drivers = Driver::drivers(); |
|
140
|
|
|
$driver = $options['driver']; |
|
141
|
|
|
$closure = $drivers[$driver] ?? null; |
|
142
|
|
|
return !$closure ? null : $closure(jaxon()->di(), $options); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|