|
1
|
<?php |
|
2
|
|
|
3
|
namespace mini; |
|
4
|
|
|
5
|
use mini\Database\Session; |
|
6
|
use mini\Database\VirtualDatabase; |
|
7
|
|
|
8
|
/** |
|
9
|
* Virtual Database Feature - Global Helper Function |
|
10
|
* |
|
11
|
* Provides vdb() helper for accessing the VirtualDatabase via Session. |
|
12
|
*/ |
|
13
|
|
|
14
|
// Register VirtualDatabase engine - singleton that holds registered tables |
|
15
|
Mini::$mini->addService(VirtualDatabase::class, Lifetime::Singleton, fn() => Mini::$mini->loadServiceConfig(VirtualDatabase::class)); |
|
16
|
|
|
17
|
// Register Session - scoped per request/fiber for temp table isolation |
|
18
|
Mini::$mini->addService(Session::class, Lifetime::Scoped, fn() => Mini::$mini->get(VirtualDatabase::class)->session()); |
|
19
|
|
|
20
|
/** |
|
21
|
* Get a VirtualDatabase session for the current request scope |
|
22
|
* |
|
23
|
* Returns a Session that wraps the VirtualDatabase. Each request/fiber gets |
|
24
|
* its own Session with isolated temporary tables. Use getEngine() to access |
|
25
|
* the underlying VirtualDatabase for table registration. |
|
26
|
* |
|
27
|
* Usage: |
|
28
|
* ```php |
|
29
|
* // Standard querying |
|
30
|
* vdb()->query("SELECT * FROM countries WHERE continent = ?", ['Europe']); |
|
31
|
* vdb()->queryOne("SELECT * FROM users WHERE id = ?", [123]); |
|
32
|
* |
|
33
|
* // Temporary tables (isolated per request/fiber) |
|
34
|
* vdb()->exec("CREATE TEMPORARY TABLE tmp AS SELECT * FROM users WHERE active = 1"); |
|
35
|
* vdb()->query("SELECT * FROM tmp"); |
|
36
|
* |
|
37
|
* // Access underlying engine for table registration |
|
38
|
* vdb()->getEngine()->registerTable('data', new ArrayTable(...$columns)); |
|
39
|
* ``` |
|
40
|
* |
|
41
|
* @return Session The session-scoped database interface |
|
42
|
*/ |
|
43
|
function vdb(): Session |
|
44
|
{ |
|
45
|
return Mini::$mini->get(Session::class); |
|
46
|
} |
|
47
|
|