Skip to content

Commit 94d095d

Browse files
committed
Merge branch 'refs/heads/development'
2 parents d3e1893 + a2e5dc6 commit 94d095d

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

guide/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ Below is the default class configuration. Normally, only the ``username``, the
8787
'charset' => 'utf8mb4',
8888
'collation' => 'utf8mb4_general_ci',
8989
'timezone' => '+00:00',
90+
'init_queries' => true,
9091
'ssl' => [
9192
'enabled' => false,
9293
'verify' => true,

src/Database.php

Lines changed: 22 additions & 5 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
}
@@ -128,6 +132,7 @@ protected function log(string $message, LogLevel $level = LogLevel::ERROR) : voi
128132
'charset' => 'string',
129133
'collation' => 'string',
130134
'timezone' => 'string',
135+
'init_queries' => 'bool',
131136
'ssl' => 'array',
132137
'failover' => 'array',
133138
'options' => 'array',
@@ -147,6 +152,7 @@ protected function makeConfig(array $config) : array
147152
'charset' => 'utf8mb4',
148153
'collation' => 'utf8mb4_general_ci',
149154
'timezone' => '+00:00',
155+
'init_queries' => true,
150156
'ssl' => [
151157
'enabled' => false,
152158
'verify' => true,
@@ -246,8 +252,10 @@ protected function connect(
246252
);
247253
return $this->connect($config);
248254
}
249-
$this->setCollations($config['charset'], $config['collation']);
250-
$this->setTimezone($config['timezone']);
255+
if ($config['init_queries']) {
256+
$this->setCollations($config['charset'], $config['collation']);
257+
$this->setTimezone($config['timezone']);
258+
}
251259
return $this;
252260
}
253261

@@ -256,13 +264,21 @@ protected function setCollations(string $charset, string $collation) : bool
256264
$this->mysqli->set_charset($charset);
257265
$charset = $this->quote($charset);
258266
$collation = $this->quote($collation);
259-
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);
260272
}
261273

262274
protected function setTimezone(string $timezone) : bool
263275
{
264276
$timezone = $this->quote($timezone);
265-
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);
266282
}
267283

268284
/**
@@ -338,6 +354,7 @@ public function reconnect() : static
338354
'charset' => 'string',
339355
'collation' => 'string',
340356
'timezone' => 'string',
357+
'init_queries' => 'bool',
341358
'ssl' => 'array',
342359
'failover' => 'array',
343360
'options' => 'array',

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)