Skip to content

Commit a74151e

Browse files
authored
feat: MyPdo log query given and executing time (milisecond) (#288)
1 parent fa8b764 commit a74151e

File tree

2 files changed

+96
-2
lines changed

2 files changed

+96
-2
lines changed

src/System/Database/MyPDO.php

+47-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@ class MyPDO
1818
*/
1919
protected $configs;
2020

21+
/**
22+
* Query prepare statment;.
23+
*/
24+
protected string $query;
25+
26+
/**
27+
* Log query when execute and fatching.
28+
* - query.
29+
*
30+
* @var array<int, array<string, mixed>>
31+
*/
32+
protected $logs = [];
33+
2134
/**
2235
* @param array<string, string> $configs
2336
*/
@@ -89,7 +102,7 @@ public function configs()
89102
*/
90103
public function query(string $query): self
91104
{
92-
$this->stmt = $this->dbh->prepare($query);
105+
$this->stmt = $this->dbh->prepare($this->query = $query);
93106

94107
return $this;
95108
}
@@ -135,7 +148,13 @@ public function bind($param, $value, $type = null): self
135148
*/
136149
public function execute()
137150
{
138-
return $this->stmt->execute();
151+
$start = microtime(true);
152+
$execute = $this->stmt->execute();
153+
$elapsed = round((microtime(true) - $start) * 1000, 2);
154+
155+
$this->addLog($this->query, $elapsed);
156+
157+
return $execute;
139158
}
140159

141160
/**
@@ -234,4 +253,30 @@ public function cancelTransaction(): bool
234253
{
235254
return $this->dbh->rollBack();
236255
}
256+
257+
protected function addLog(string $query, float $elapsed_time): void
258+
{
259+
$this->logs[] = [
260+
'query' => $query,
261+
'time' => $elapsed_time,
262+
];
263+
}
264+
265+
/**
266+
* Flush logs query.
267+
*/
268+
public function flushLogs(): void
269+
{
270+
$this->logs = [];
271+
}
272+
273+
/**
274+
* Get logs query.
275+
*
276+
* @return array<int, array<string, mixed>>
277+
*/
278+
public function getLogs(): array
279+
{
280+
return $this->logs;
281+
}
237282
}

tests/DataBase/MyPdoTest.php

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace System\Test\Database;
6+
7+
final class MyPdoTest extends BaseConnection
8+
{
9+
protected function setUp(): void
10+
{
11+
$this->createConnection();
12+
$this->createUserSchema();
13+
$this->createUser([
14+
[
15+
'user' => 'taylor',
16+
'password' => password_hash('password', PASSWORD_DEFAULT),
17+
'stat' => 100,
18+
],
19+
]);
20+
}
21+
22+
protected function tearDown(): void
23+
{
24+
$this->dropConnection();
25+
}
26+
27+
/**
28+
* @test
29+
*
30+
* @group database
31+
*/
32+
public function itCanLogExcutionConnention()
33+
{
34+
$this->pdo->flushLogs();
35+
$this->pdo->query('select * from users where user = :user')->bind('user', 'taylor')->resultset();
36+
$this->pdo->query('select * from users where user = :user')->bind('user', 'taylor')->single();
37+
$this->pdo->query('delete from users where user = :user')->bind('user', 'taylor')->execute();
38+
39+
$logs = [
40+
'select * from users where user = :user',
41+
'select * from users where user = :user',
42+
'delete from users where user = :user',
43+
];
44+
45+
foreach ($this->pdo->getLogs() as $key => $log) {
46+
$this->assertEquals($log['query'], $logs[$key]);
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)