Skip to content

Commit

Permalink
Better fix for #1357
Browse files Browse the repository at this point in the history
Previous fix would not correctly address #1357 when invalid double-quoted identifiers are interpreted as string literals (Configuration.acceptsDoubleQuotedStringLiterals = true, or SQLCipher 3)
  • Loading branch information
groue committed Apr 1, 2023
1 parent 82f300e commit b81e858
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
15 changes: 7 additions & 8 deletions GRDB/QueryInterface/SQL/SQLRelation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -612,18 +612,17 @@ extension SQLRelation {
guard !isDistinct else {
return try fetchTrivialCount(db)
}


// <https://github.com/groue/GRDB.swift/issues/1357>
guard selection.allSatisfy(\.isTriviallyCountable) else {
return try fetchTrivialCount(db)
}

// SELECT expr1, expr2, ... FROM tableName ...
// ->
// SELECT COUNT(*) FROM tableName ...
let countRelation = unordered().select(.countAll)
do {
return try QueryInterfaceRequest(relation: countRelation).fetchOne(db)!
} catch {
// <https://github.com/groue/GRDB.swift/issues/1357>
// TODO: can we inspect the request and avoid catching an error?
return try fetchTrivialCount(db)
}
return try QueryInterfaceRequest(relation: countRelation).fetchOne(db)!
}
}

Expand Down
12 changes: 12 additions & 0 deletions GRDB/QueryInterface/SQL/SQLSelection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,18 @@ extension SQLSelection {
return .literal(sqlLiteral.qualified(with: alias))
}
}

/// Supports SQLRelation.fetchCount.
///
/// See <https://github.com/groue/GRDB.swift/issues/1357>
var isTriviallyCountable: Bool {
switch impl {
case .aliasedExpression, .literal:
return false
case .allColumns, .qualifiedAllColumns, .expression:
return true
}
}
}

extension [SQLSelection] {
Expand Down

0 comments on commit b81e858

Please sign in to comment.