Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/Internal/ConnectionProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -483,13 +483,20 @@ public function execute(int $stmtId, string $query, array $params, array $prebou
$paramType = $params[$paramId]->getType();

if (isset($prebound[$paramId])) {
$types[] = MysqlDataType::encodeInt16(MysqlDataType::VarString->value);
$preboundType = match ($paramType) {
MysqlDataType::TinyBlob,
MysqlDataType::Blob,
MysqlDataType::MediumBlob,
MysqlDataType::LongBlob => MysqlDataType::LongBlob,
default => MysqlDataType::VarString,
};
$types[] = MysqlDataType::encodeInt16($preboundType->value);
continue;
}

$encodedValue = match ($paramType) {
MysqlDataType::Json => MysqlEncodedValue::fromJson($param),
default => MysqlEncodedValue::fromValue($param),
default => MysqlEncodedValue::fromValue($param, $paramType),
};

$types[] = MysqlDataType::encodeInt16($encodedValue->getType()->value);
Expand Down
26 changes: 22 additions & 4 deletions src/Internal/MysqlEncodedValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
/** @internal */
final class MysqlEncodedValue
{
public static function fromValue(mixed $param): self
public static function fromValue(mixed $param, ?MysqlDataType $targetType = null): self
{
switch (\get_debug_type($param)) {
case "string":
return new self(MysqlDataType::LongBlob, MysqlDataType::encodeInt(\strlen($param)) . $param);
return new self(self::stringTypeFor($targetType), MysqlDataType::encodeInt(\strlen($param)) . $param);

case "int":
if ($param >= -(1 << 7) && $param < (1 << 7)) {
Expand Down Expand Up @@ -40,17 +40,35 @@ public static function fromValue(mixed $param): self

default:
if ($param instanceof \BackedEnum) {
return self::fromValue($param->value);
return self::fromValue($param->value, $targetType);
}

if ($param instanceof \Stringable) {
return self::fromValue((string) $param);
return self::fromValue((string) $param, $targetType);
}

throw new \TypeError("Unexpected type for query parameter: " . \get_debug_type($param));
}
}

/**
* Picks the wire type for a PHP string parameter. Blob-family targets keep
* the binary (charset 63) interpretation of LongBlob so raw bytes are not
* transcoded against the connection charset; everything else uses VarString
* so MariaDB's native UUID column type and similar string-typed columns
* parse the value correctly.
*/
private static function stringTypeFor(?MysqlDataType $targetType): MysqlDataType
{
return match ($targetType) {
MysqlDataType::TinyBlob,
MysqlDataType::Blob,
MysqlDataType::MediumBlob,
MysqlDataType::LongBlob => MysqlDataType::LongBlob,
default => MysqlDataType::VarString,
};
}

Comment thread
trowski marked this conversation as resolved.
Resolved
public static function fromJson(?string $json): self
{
if ($json === null) {
Expand Down
88 changes: 88 additions & 0 deletions test/MysqlEncodedValueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php declare(strict_types=1);

namespace Amp\Mysql\Test;

use Amp\Mysql\Internal\MysqlEncodedValue;
use Amp\Mysql\MysqlDataType;
use PHPUnit\Framework\TestCase;

class MysqlEncodedValueTest extends TestCase
{
/**
* @return iterable<string, array{MysqlDataType|null, MysqlDataType}>
*/
public function provideStringTargets(): iterable
{
yield 'no target (legacy default)' => [null, MysqlDataType::VarString];
yield 'varchar' => [MysqlDataType::Varchar, MysqlDataType::VarString];
yield 'string' => [MysqlDataType::String, MysqlDataType::VarString];
yield 'var_string' => [MysqlDataType::VarString, MysqlDataType::VarString];
yield 'tiny_blob' => [MysqlDataType::TinyBlob, MysqlDataType::LongBlob];
yield 'blob' => [MysqlDataType::Blob, MysqlDataType::LongBlob];
yield 'medium_blob' => [MysqlDataType::MediumBlob, MysqlDataType::LongBlob];
yield 'long_blob' => [MysqlDataType::LongBlob, MysqlDataType::LongBlob];
yield 'bit' => [MysqlDataType::Bit, MysqlDataType::VarString];
yield 'enum' => [MysqlDataType::Enum, MysqlDataType::VarString];
yield 'set' => [MysqlDataType::Set, MysqlDataType::VarString];
yield 'geometry' => [MysqlDataType::Geometry, MysqlDataType::VarString];
}

/**
* @dataProvider provideStringTargets
*/
public function testStringPicksTypeBasedOnTarget(?MysqlDataType $target, MysqlDataType $expected): void
{
$encoded = MysqlEncodedValue::fromValue('hello', $target);

self::assertSame($expected, $encoded->getType());
}

public function testBinaryPayloadIntoBlobStaysLongBlob(): void
{
$encoded = MysqlEncodedValue::fromValue(\random_bytes(64), MysqlDataType::Blob);

self::assertSame(MysqlDataType::LongBlob, $encoded->getType());
}

public function testStringableIntoBlobStaysLongBlob(): void
{
$stringable = new class implements \Stringable {
public function __toString(): string
{
return 'cast me';
}
};

$encoded = MysqlEncodedValue::fromValue($stringable, MysqlDataType::LongBlob);

self::assertSame(MysqlDataType::LongBlob, $encoded->getType());
}

public function testBackedEnumIntoStringTargetUsesVarString(): void
{
$enum = StringTargetEnum::Foo;

$encoded = MysqlEncodedValue::fromValue($enum, MysqlDataType::Varchar);

self::assertSame(MysqlDataType::VarString, $encoded->getType());
}

public function testIntegerEncodingIgnoresTarget(): void
{
$encoded = MysqlEncodedValue::fromValue(1, MysqlDataType::LongBlob);

self::assertSame(MysqlDataType::Tiny, $encoded->getType());
}

public function testNullEncodingIgnoresTarget(): void
{
$encoded = MysqlEncodedValue::fromValue(null, MysqlDataType::LongBlob);

self::assertSame(MysqlDataType::Null, $encoded->getType());
}
}

enum StringTargetEnum: string
{
case Foo = 'foo';
}