Skip to content

Commit

Permalink
Add support for sql_mode and fix set charset (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruifil committed Dec 19, 2023
1 parent 9b590a0 commit b4ab0d6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/Internal/ConnectionProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ public function connect(?Cancellation $cancellation = null): void
$future = $future->finally(static fn () => $cancellation?->unsubscribe($id));
}

// if a charset is specified, we need to set before any query
if ($this->config->getCharset() !== MysqlConfig::DEFAULT_CHARSET
|| $this->config->getCollation() !== MysqlConfig::DEFAULT_COLLATE
) {
Expand All @@ -232,6 +233,13 @@ public function connect(?Cancellation $cancellation = null): void
});
}

if ($this->config->getSqlMode()) {
$future = $future->map(function (): void {
$sqlMode = $this->config->getSqlMode();
$this->query("SET SESSION sql_mode='$sqlMode'")->await();
});
}

$future->await();
}

Expand Down
24 changes: 22 additions & 2 deletions src/MysqlConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
final class MysqlConfig extends SqlConfig
{
public const DEFAULT_PORT = 3306;
public const BIN_CHARSET = 45; // utf8mb4_general_ci
public const BIN_CHARSET = 255; // utf8mb4_0900_ai_ci

public const KEY_MAP = [
...parent::KEY_MAP,
Expand All @@ -19,7 +19,7 @@ final class MysqlConfig extends SqlConfig
];

public const DEFAULT_CHARSET = "utf8mb4";
public const DEFAULT_COLLATE = "utf8mb4_general_ci";
public const DEFAULT_COLLATE = "utf8mb4_0900_ai_ci";

private bool $useCompression;

Expand All @@ -34,6 +34,8 @@ final class MysqlConfig extends SqlConfig
/* @var string private key to use for sha256_password auth method */
private string $key;

private ?string $sqlMode;

public static function fromString(string $connectionString, ConnectContext $context = null): self
{
$parts = self::parseConnectionString($connectionString, self::KEY_MAP);
Expand Down Expand Up @@ -84,6 +86,7 @@ public function __construct(
?ConnectContext $context = null,
string $charset = self::DEFAULT_CHARSET,
string $collate = self::DEFAULT_COLLATE,
?string $sqlMode = null,
bool $useCompression = false,
string $key = '',
bool $useLocalInfile = false
Expand All @@ -93,6 +96,7 @@ public function __construct(
$this->context = $context ?? (new ConnectContext);
$this->charset = $charset;
$this->collate = $collate;
$this->sqlMode = $sqlMode;
$this->useCompression = $useCompression;
$this->key = $key;
$this->useLocalInfile = $useLocalInfile;
Expand Down Expand Up @@ -173,6 +177,22 @@ public function withCharset(string $charset, string $collate): self
return $new;
}

public function getSqlMode(): ?string
{
return $this->sqlMode;
}

/**
* Set the Server SQL Modes at connection time.
* @see https://dev.mysql.com/doc/refman/8.0/en/sql-mode.html
*/
public function withSqlMode(?string $sqlMode): self
{
$new = clone $this;
$new->sqlMode = $sqlMode;
return $new;
}

public function getKey(): string
{
return $this->key;
Expand Down
5 changes: 3 additions & 2 deletions test/LinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Amp\Future;
use Amp\Mysql\MysqlColumnDefinition;
use Amp\Mysql\MysqlConfig;
use Amp\Mysql\MysqlDataType;
use Amp\Mysql\MysqlLink;
use Amp\Mysql\MysqlResult;
Expand Down Expand Up @@ -216,8 +217,8 @@ public function testPrepared(): void

$this->assertEquals([
new MysqlColumnDefinition(...\array_merge($base, ["type" => MysqlDataType::LongLong, "flags" => 128])),
new MysqlColumnDefinition(...\array_merge($base, ["type" => MysqlDataType::Datetime, "length" => 104, "decimals" => 6, "charset" => 45])),
new MysqlColumnDefinition(...\array_merge($base, ["type" => MysqlDataType::VarString, "length" => 65532, "decimals" => 31, "charset" => 45])),
new MysqlColumnDefinition(...\array_merge($base, ["type" => MysqlDataType::Datetime, "length" => 104, "decimals" => 6, "charset" => MysqlConfig::BIN_CHARSET])),
new MysqlColumnDefinition(...\array_merge($base, ["type" => MysqlDataType::VarString, "length" => 65532, "decimals" => 31, "charset" => MysqlConfig::BIN_CHARSET])),
], $stmt->getParameterDefinitions());

$stmt->bind("data", 'd');
Expand Down

0 comments on commit b4ab0d6

Please sign in to comment.