Skip to content

Commit

Permalink
MySQL time value type binary decoding now properly increments offset (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
bennnjamin authored Aug 10, 2022
1 parent f96012f commit 3a2c329
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/MysqlDataType.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public function decodeBinary(string $string, int &$offset = 0, int $flags = 0):
default:
throw new SqlException("Unexpected string length for date in binary protocol: " . ($length - 1));
}

$offset += $length;
return \str_pad((string) $year, 2, "0", \STR_PAD_LEFT)
. "-" . \str_pad((string) $month, 2, "0", \STR_PAD_LEFT)
. "-" . \str_pad((string) $day, 2, "0", \STR_PAD_LEFT)
Expand Down Expand Up @@ -192,7 +192,7 @@ public function decodeBinary(string $string, int &$offset = 0, int $flags = 0):
default:
throw new SqlException("Unexpected string length for time in binary protocol: " . ($length - 1));
}

$offset += $length;
return ($negative ? "" : "-") . \str_pad((string) $day, 2, "0", \STR_PAD_LEFT)
. "d " . \str_pad((string) $hour, 2, "0", \STR_PAD_LEFT)
. ":" . \str_pad((string) $minute, 2, "0", \STR_PAD_LEFT)
Expand Down
8 changes: 4 additions & 4 deletions test/LinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public function testPrepared()
$stmt = $db->prepare("SELECT * FROM main WHERE a = ? OR b = ?");
$result = $stmt->execute([1, 8]);
$this->assertInstanceOf(MysqlResult::class, $result);
$this->assertSame(3, $result->getColumnCount());
$this->assertSame(4, $result->getColumnCount());
$got = [];
foreach ($result as $row) {
$got[] = \array_values($row);
Expand All @@ -155,7 +155,7 @@ public function testPrepared()
$stmt = $db->prepare("SELECT * FROM main WHERE a = :a OR b = ?");
$result = $stmt->execute(["a" => 2, 5]);
$this->assertInstanceOf(MysqlResult::class, $result);
$this->assertSame(3, $result->getColumnCount());
$this->assertSame(4, $result->getColumnCount());
$got = [];
foreach ($result as $row) {
$got[] = \array_values($row);
Expand Down Expand Up @@ -236,9 +236,9 @@ public function testExecute()
$got[] = \array_values($row);
}
$this->assertCount(2, $got);
$this->assertSame([[2, 2, 3], [4, 4, 5]], $got);
$this->assertSame([[2, 2, 3, '1970-01-01 00:00:00.00000'], [4, 4, 5, '1970-01-01 00:00:00.00000']], $got);

$result = $db->execute("INSERT INTO main (a, b) VALUES (:a, :b)", ["a" => 10, "b" => 11]);
$result = $db->execute("INSERT INTO main (a, b) VALUES (:a, :b)", ["a" => 10, "b" => 11, "c" => '1970-01-01 00:00:00']);
$this->assertInstanceOf(MysqlResult::class, $result);
$this->assertGreaterThan(5, $result->getLastInsertId());

Expand Down
5 changes: 3 additions & 2 deletions test/initialize.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ function initialize(\mysqli $db): void
{
$db->query("CREATE DATABASE test");

$db->query("CREATE TABLE test.main (id INT NOT NULL AUTO_INCREMENT, a INT, b INT, PRIMARY KEY (id))");
$db->query("INSERT INTO test.main (a, b) VALUES (1, 2), (2, 3), (3, 4), (4, 5), (5, 6)");
$db->query("CREATE TABLE test.main (id INT NOT NULL AUTO_INCREMENT, a INT, b INT, c DATETIME, PRIMARY KEY (id))");
$epoch = date('1970-01-01 00:00:00');
$db->query("INSERT INTO test.main (a, b, c) VALUES (1, 2, '$epoch'), (2, 3, '$epoch'), (3, 4, '$epoch'), (4, 5, '$epoch'), (5, 6, '$epoch')");

$db->query("CREATE TABLE test.json (a JSON)");
$db->query("INSERT INTO test.json VALUES ('{\"key\": \"value\"}')");
Expand Down

0 comments on commit 3a2c329

Please sign in to comment.