Skip to content

Commit

Permalink
Merge branch 'refs/heads/development'
Browse files Browse the repository at this point in the history
  • Loading branch information
natanfelles committed Aug 4, 2024
2 parents d3e1893 + a2e5dc6 commit 94d095d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
1 change: 1 addition & 0 deletions guide/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ Below is the default class configuration. Normally, only the ``username``, the
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_general_ci',
'timezone' => '+00:00',
'init_queries' => true,
'ssl' => [
'enabled' => false,
'verify' => true,
Expand Down
27 changes: 22 additions & 5 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,12 @@ public function __construct(
string $schema = null,
string $host = 'localhost',
int $port = 3306,
Logger $logger = null
Logger $logger = null,
DatabaseCollector $collector = null
) {
if ($collector) {
$this->setDebugCollector($collector);
}
$this->logger = $logger;
$this->connect($username, $password, $schema, $host, $port);
}
Expand Down Expand Up @@ -128,6 +132,7 @@ protected function log(string $message, LogLevel $level = LogLevel::ERROR) : voi
'charset' => 'string',
'collation' => 'string',
'timezone' => 'string',
'init_queries' => 'bool',
'ssl' => 'array',
'failover' => 'array',
'options' => 'array',
Expand All @@ -147,6 +152,7 @@ protected function makeConfig(array $config) : array
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_general_ci',
'timezone' => '+00:00',
'init_queries' => true,
'ssl' => [
'enabled' => false,
'verify' => true,
Expand Down Expand Up @@ -246,8 +252,10 @@ protected function connect(
);
return $this->connect($config);
}
$this->setCollations($config['charset'], $config['collation']);
$this->setTimezone($config['timezone']);
if ($config['init_queries']) {
$this->setCollations($config['charset'], $config['collation']);
$this->setTimezone($config['timezone']);
}
return $this;
}

Expand All @@ -256,13 +264,21 @@ protected function setCollations(string $charset, string $collation) : bool
$this->mysqli->set_charset($charset);
$charset = $this->quote($charset);
$collation = $this->quote($collation);
return $this->mysqli->real_query("SET NAMES {$charset} COLLATE {$collation}");
$statement = "SET NAMES {$charset} COLLATE {$collation}";
$this->lastQuery = $statement;
return isset($this->debugCollector)
? $this->addToDebug(fn () => $this->mysqli->real_query($statement))
: $this->mysqli->real_query($statement);
}

protected function setTimezone(string $timezone) : bool
{
$timezone = $this->quote($timezone);
return $this->mysqli->real_query("SET time_zone = {$timezone}");
$statement = "SET time_zone = {$timezone}";
$this->lastQuery = $statement;
return isset($this->debugCollector)
? $this->addToDebug(fn () => $this->mysqli->real_query($statement))
: $this->mysqli->real_query($statement);
}

/**
Expand Down Expand Up @@ -338,6 +354,7 @@ public function reconnect() : static
'charset' => 'string',
'collation' => 'string',
'timezone' => 'string',
'init_queries' => 'bool',
'ssl' => 'array',
'failover' => 'array',
'options' => 'array',
Expand Down
19 changes: 19 additions & 0 deletions tests/DatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Tests\Database;

use Framework\Database\Database;
use Framework\Database\Debug\DatabaseCollector;
use Framework\Database\Definition\AlterSchema;
use Framework\Database\Definition\AlterTable;
use Framework\Database\Definition\CreateSchema;
Expand Down Expand Up @@ -115,6 +116,24 @@ public function testConnectionFailWithLogger() : void
}
}

public function testConnectionWithDebugCollector() : void
{
$collector = new DatabaseCollector();
self::assertCount(0, $collector->getData());
$config = [
'username' => \getenv('DB_USERNAME'),
'password' => \getenv('DB_PASSWORD'),
'host' => \getenv('DB_HOST'),
'port' => \getenv('DB_PORT'),
];
$database = new Database($config, collector: $collector);
self::assertCount(2, $collector->getData());
self::assertSame(
"SET time_zone = '+00:00'",
$database->getLastQuery()
);
}

protected function mariadbHasSSL() : bool
{
$version = static::$database->getConnection()->get_server_info();
Expand Down

0 comments on commit 94d095d

Please sign in to comment.