Skip to content

Commit 2cb76cd

Browse files
authored
Fix Datetime2 microseconds formatting (#141)
* Add tests for Datetime2 parsing * Fix the microseconds parsing for Datetime2
1 parent ffe7cc9 commit 2cb76cd

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

src/MySQLReplication/Event/RowEvent/RowEvent.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,11 @@ protected function getDatetime2(ColumnDTO $columnDTO): ?string
707707
$year . '-' . $month . '-' . $day . ' ' . $hour . ':' . $minute . ':' . $second
708708
);
709709
if ($formattedDate) {
710-
return $formattedDate . $fsp;
710+
if ($fsp > 0) {
711+
return vsprintf('%s.%06u', [$formattedDate, $fsp]);
712+
} else {
713+
return $formattedDate;
714+
}
711715
}
712716

713717
return null;

tests/Integration/TypesTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,46 @@ public function testShouldReturnNullOnZeroDateDateTime(): void
381381
self::assertNull($event->values[0]['test']);
382382
}
383383

384+
public function testShouldBeDateTime2(): void
385+
{
386+
$create_query = 'CREATE TABLE test (test DATETIME(6));';
387+
$insert_query = 'INSERT INTO test VALUES("1984-12-03 12:33:07.023450")';
388+
389+
$event = $this->createAndInsertValue($create_query, $insert_query);
390+
391+
self::assertEquals('1984-12-03 12:33:07.023450', $event->values[0]['test']);
392+
}
393+
394+
public function testShouldBeZeroDateTime2(): void
395+
{
396+
$create_query = 'CREATE TABLE test (id INTEGER, test DATETIME(6) NOT NULL DEFAULT 0);';
397+
$insert_query = 'INSERT INTO test (id) VALUES(1)';
398+
399+
$event = $this->createAndInsertValue($create_query, $insert_query);
400+
401+
self::assertNull($event->values[0]['test']);
402+
}
403+
404+
public function testShouldBeBrokenDateTime2(): void
405+
{
406+
$create_query = 'CREATE TABLE test (test DATETIME(6) NOT NULL);';
407+
$insert_query = 'INSERT INTO test VALUES("2013-00-00 00:00:00")';
408+
409+
$event = $this->createAndInsertValue($create_query, $insert_query);
410+
411+
self::assertNull($event->values[0]['test']);
412+
}
413+
414+
public function testShouldReturnNullOnZeroDateDateTime2(): void
415+
{
416+
$create_query = 'CREATE TABLE test (test DATETIME(6) NOT NULL);';
417+
$insert_query = 'INSERT INTO test VALUES("0000-00-00 00:00:00")';
418+
419+
$event = $this->createAndInsertValue($create_query, $insert_query);
420+
421+
self::assertNull($event->values[0]['test']);
422+
}
423+
384424
public function testShouldBeYear(): void
385425
{
386426
$create_query = 'CREATE TABLE test (test YEAR(4), test2 YEAR, test3 YEAR)';

0 commit comments

Comments
 (0)