Fix crash when trying to fetch -1 as a UInt from the database

Here's a simplified version of the code we currently have:

      let sql = """
        SELECT COUNT(*) - 1
        FROM my_table
        WHERE id >= :current_id
      """
      return try UInt.fetchOne(db, sql: sql)

This causes GRDB to throw an error when the result of the query is `-1`
because `-1` can't be converted to an unsigned integer. This caused a
crash.

I believe the `- 1` subtraction exists to filter out the current
interaction, so I updated the SQL to effectively be:

    SELECT COUNT(*)
    FROM my_table
    WHERE id > :current_id

This should prevent us from ever selecting `-1`, and therefore prevent
this crash.
This commit is contained in:
Evan Hahn 2022-09-14 11:49:56 -05:00 committed by Evan Hahn
parent f19eb02565
commit 76f83eaa46

View File

@ -1293,10 +1293,10 @@ public class GRDBInteractionFinder: NSObject, InteractionFinderAdapter {
}
let distanceSQL = """
SELECT count(*) - 1
SELECT COUNT(*)
FROM \(InteractionRecord.databaseTableName)
WHERE \(interactionColumn: .threadUniqueId) = ?
AND \(interactionColumn: .id) >= ?
AND \(interactionColumn: .id) > ?
\(Self.filterStoryRepliesClause(for: storyReplyQueryMode))
\(excludePlaceholders ? filterPlaceholdersClause : "")
"""