Skip to content

Commit a2e5dc6

Browse files
committed
Allow collect initial queries
1 parent 7522d16 commit a2e5dc6

2 files changed

Lines changed: 34 additions & 3 deletions

File tree

src/Database.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,12 @@ public function __construct(
9393
string $schema = null,
9494
string $host = 'localhost',
9595
int $port = 3306,
96-
Logger $logger = null
96+
Logger $logger = null,
97+
DatabaseCollector $collector = null
9798
) {
99+
if ($collector) {
100+
$this->setDebugCollector($collector);
101+
}
98102
$this->logger = $logger;
99103
$this->connect($username, $password, $schema, $host, $port);
100104
}
@@ -260,13 +264,21 @@ protected function setCollations(string $charset, string $collation) : bool
260264
$this->mysqli->set_charset($charset);
261265
$charset = $this->quote($charset);
262266
$collation = $this->quote($collation);
263-
return $this->mysqli->real_query("SET NAMES {$charset} COLLATE {$collation}");
267+
$statement = "SET NAMES {$charset} COLLATE {$collation}";
268+
$this->lastQuery = $statement;
269+
return isset($this->debugCollector)
270+
? $this->addToDebug(fn () => $this->mysqli->real_query($statement))
271+
: $this->mysqli->real_query($statement);
264272
}
265273

266274
protected function setTimezone(string $timezone) : bool
267275
{
268276
$timezone = $this->quote($timezone);
269-
return $this->mysqli->real_query("SET time_zone = {$timezone}");
277+
$statement = "SET time_zone = {$timezone}";
278+
$this->lastQuery = $statement;
279+
return isset($this->debugCollector)
280+
? $this->addToDebug(fn () => $this->mysqli->real_query($statement))
281+
: $this->mysqli->real_query($statement);
270282
}
271283

272284
/**

tests/DatabaseTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Tests\Database;
1111

1212
use Framework\Database\Database;
13+
use Framework\Database\Debug\DatabaseCollector;
1314
use Framework\Database\Definition\AlterSchema;
1415
use Framework\Database\Definition\AlterTable;
1516
use Framework\Database\Definition\CreateSchema;
@@ -115,6 +116,24 @@ public function testConnectionFailWithLogger() : void
115116
}
116117
}
117118

119+
public function testConnectionWithDebugCollector() : void
120+
{
121+
$collector = new DatabaseCollector();
122+
self::assertCount(0, $collector->getData());
123+
$config = [
124+
'username' => \getenv('DB_USERNAME'),
125+
'password' => \getenv('DB_PASSWORD'),
126+
'host' => \getenv('DB_HOST'),
127+
'port' => \getenv('DB_PORT'),
128+
];
129+
$database = new Database($config, collector: $collector);
130+
self::assertCount(2, $collector->getData());
131+
self::assertSame(
132+
"SET time_zone = '+00:00'",
133+
$database->getLastQuery()
134+
);
135+
}
136+
118137
protected function mariadbHasSSL() : bool
119138
{
120139
$version = static::$database->getConnection()->get_server_info();

0 commit comments

Comments
 (0)