-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery-profiling.php
More file actions
41 lines (34 loc) · 1.3 KB
/
query-profiling.php
File metadata and controls
41 lines (34 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
use Neuron\Database\DatabaseManager;
use Neuron\Database\Drivers\MysqlDatabaseDriver;
use Neuron\Extensibility\ExtensionStore;
use Neuron\Extensibility\InstantiatorInterface;
use Neuron\Database\Events\QueryExecutedEvent;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterface;
/** @var LoggerInterface $logger */
/** @var InstantiatorInterface $instantiator */
/** @var EventDispatcherInterface $eventDispatcher */
$extensions = new ExtensionStore($instantiator, $logger, $eventDispatcher);
$database = new DatabaseManager($logger, $extensions, $eventDispatcher);
// Connect to your database
$database->connect('_default_', MysqlDatabaseDriver::class, [
'database' => 'my_database',
'host' => 'localhost',
'username' => 'user',
'password' => 'password',
]);
// Enable profiling
$database->enableProfiling();
// Listen to query executed events for profiling output
$eventDispatcher->listen(QueryExecutedEvent::class, function (QueryExecutedEvent $event) {
echo sprintf(
"[%s] Query executed: %s | Duration: %.4f seconds\n",
strtoupper($event->action),
$event->query,
$event->duration
);
});
// Execute queries
$database->fetchAll('SELECT * FROM users LIMIT 10');
$database->execute('UPDATE users SET last_login = NOW() WHERE id = ?', [1]);