Skip to content

Commit

Permalink
Fix date/time handling when string values are not the same (fixes #143)
Browse files Browse the repository at this point in the history
This fixes the issue when applying a diff with date/time values,
but the representation of date/time values as strings is slightly
different even though they refer to the same date/time.

Fixes both the case with update and the case with delete.
Insert does not need fixing as there is no comparison with previous values.
  • Loading branch information
wonder-sk authored and tomasMizera committed Oct 13, 2021
1 parent 1ad4b78 commit 69637a8
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 0 deletions.
12 changes: 12 additions & 0 deletions geodiff/src/drivers/sqlitedriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,12 @@ static std::string sqlForUpdate( const std::string &tableName, const TableSchema
sql += " AND ";
if ( tbl.columns[i].isPrimaryKey )
sql += sqlitePrintf( " \"%w\" = ?%d ", tbl.columns[i].name.c_str(), i * 3 + 1 );
else if ( tbl.columns[i].type.baseType == TableColumnType::DATETIME )
{
// compare date/time values using datetime() because they may have
// multiple equivalent string representations (see #143)
sql += sqlitePrintf( " ( ?%d = 0 OR datetime(\"%w\") IS datetime(?%d) ) ", i * 3 + 2, tbl.columns[i].name.c_str(), i * 3 + 1 );
}
else
sql += sqlitePrintf( " ( ?%d = 0 OR \"%w\" IS ?%d ) ", i * 3 + 2, tbl.columns[i].name.c_str(), i * 3 + 1 );
}
Expand All @@ -652,6 +658,12 @@ static std::string sqlForDelete( const std::string &tableName, const TableSchema
sql += " AND ";
if ( tbl.columns[i].isPrimaryKey )
sql += sqlitePrintf( "\"%w\" = ?", tbl.columns[i].name.c_str() );
else if ( tbl.columns[i].type.baseType == TableColumnType::DATETIME )
{
// compare date/time values using datetime() because they may have
// multiple equivalent string representations (see #143)
sql += sqlitePrintf( "datetime(\"%w\") IS datetime(?)", tbl.columns[i].name.c_str() );
}
else
sql += sqlitePrintf( "\"%w\" IS ?", tbl.columns[i].name.c_str() );
}
Expand Down
27 changes: 27 additions & 0 deletions geodiff/tests/test_driver_sqlite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,33 @@ TEST( SqliteDriverTest, create_changeset_datetime )
);
}


TEST( SqliteDriverTest, apply_changeset_datetime )
{
// check that the datetime handling is robust - a single datetime may have
// multiple representations, e.g. '2021-03-16T00:00:00' and '2021-03-16T00:00:00Z'
// represent the same datetime, so things should work fine even if database
// contains '2021-03-16T00:00:00' but changeset refers to '2021-03-16T00:00:00Z'

// datetime1-3a.diff refers to date/time value in deleted row as '2021-04-01T15:00:00Z'
// while datetime1.gpkg has the value stored as '2021-04-01 15:00:00'
testApplyChangeset( "test_apply_changeset_datetime_delete",
pathjoin( testdir(), "datetime", "datetime1.gpkg" ),
pathjoin( testdir(), "datetime", "datetime1-3a.diff" ),
pathjoin( testdir(), "datetime", "datetime3.gpkg" )
);

// datetime1a.gpkg has the same content as datetime1, but date/time values
// are represented as '2021-04-01T15:00:00Z' instead of '2021-04-01 15:00:00'
// (which is used in datetime1.gpkg and in datetime1-2.diff)
testApplyChangeset( "test_apply_changeset_datetime_update",
pathjoin( testdir(), "datetime", "datetime1a.gpkg" ),
pathjoin( testdir(), "datetime", "datetime1-2.diff" ),
pathjoin( testdir(), "datetime", "datetime2.gpkg" )
);
}


TEST( SqliteDriverTest, apply_with_gpkg_contents )
{
// In geodiff >= 1.0 we ignore gpkg_* metadata tables. However older geodiff
Expand Down
Binary file added geodiff/tests/testdata/datetime/datetime1-3a.diff
Binary file not shown.
Binary file not shown.
Binary file added geodiff/tests/testdata/datetime/datetime3.gpkg
Binary file not shown.

0 comments on commit 69637a8

Please sign in to comment.