mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-28 04:24:20 +00:00
Fix failing tests for key/id encoding strategies
This commit is contained in:
1 parent
4cc3768344
commit
8076e7c725
3 files changed
+92
-64
No files matched your search
@@ -1,3 +1,18 @@
|
||||
import Foundation
|
||||
|
||||
// MARK: - TypedRequest
|
||||
|
||||
/// The protocol for all requests that know how database rows should
|
||||
/// be interpreted.
|
||||
public protocol TypedRequest {
|
||||
/// The type that can decode database rows.
|
||||
///
|
||||
/// In the request below, it is Book:
|
||||
///
|
||||
/// let request = Book.all()
|
||||
associatedtype RowDecoder
|
||||
}
|
||||
|
||||
// MARK: - SelectionRequest
|
||||
|
||||
/// The protocol for all requests that can refine their selection.
|
||||
@@ -221,7 +236,7 @@ public protocol TableRequest {
|
||||
func aliased(_ alias: TableAlias) -> Self
|
||||
}
|
||||
|
||||
extension TableRequest where Self: FilteredRequest {
|
||||
extension TableRequest where Self: FilteredRequest, Self: TypedRequest {
|
||||
|
||||
/// Creates a request filtered by primary key.
|
||||
///
|
||||
@@ -246,7 +261,38 @@ extension TableRequest where Self: FilteredRequest {
|
||||
-> Self
|
||||
where Sequence.Element: DatabaseValueConvertible
|
||||
{
|
||||
let keys = Array(keys)
|
||||
// In order to encode keys in the database, we perform a runtime check
|
||||
// for EncodableRecord, and look for a customized encoding strategy.
|
||||
// Such dynamic dispatch is unusual in GRDB, but static dispatch
|
||||
// (customizing TableRequest where RowDecoder: EncodableRecord) would
|
||||
// make it impractical to define `filter(id:)`, `fetchOne(_:key:)`,
|
||||
// `deleteAll(_:ids:)` etc.
|
||||
if let recordType = RowDecoder.self as? EncodableRecord.Type {
|
||||
if Sequence.Element.self == Date.self {
|
||||
let strategy = recordType.databaseDateEncodingStrategy
|
||||
let keys = keys.compactMap { strategy.encode($0 as! Date)?.databaseValue }
|
||||
return filter(rawKeys: keys)
|
||||
} else if Sequence.Element.self == UUID.self {
|
||||
let strategy = recordType.databaseUUIDEncodingStrategy
|
||||
let keys = keys.map { strategy.encode($0 as! UUID).databaseValue }
|
||||
return filter(rawKeys: keys)
|
||||
}
|
||||
}
|
||||
|
||||
return filter(rawKeys: keys)
|
||||
}
|
||||
|
||||
/// Creates a request filtered by primary key.
|
||||
///
|
||||
/// // SELECT * FROM player WHERE ... id IN (1, 2, 3)
|
||||
/// let request = try Player...filter(encodedKeys: [1, 2, 3])
|
||||
///
|
||||
/// - parameter keys: A collection of primary keys
|
||||
func filter<Sequence: Swift.Sequence>(rawKeys: Sequence)
|
||||
-> Self
|
||||
where Sequence.Element: DatabaseValueConvertible
|
||||
{
|
||||
let keys = Array(rawKeys)
|
||||
if keys.isEmpty {
|
||||
return none()
|
||||
}
|
||||
@@ -332,7 +378,7 @@ extension TableRequest where Self: FilteredRequest {
|
||||
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6, *)
|
||||
extension TableRequest
|
||||
where Self: FilteredRequest,
|
||||
Self: JoinableRequest, // for RowDecoder
|
||||
Self: TypedRequest,
|
||||
RowDecoder: Identifiable,
|
||||
RowDecoder.ID: DatabaseValueConvertible
|
||||
{
|
||||
@@ -363,7 +409,7 @@ where Self: FilteredRequest,
|
||||
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6, *)
|
||||
extension TableRequest
|
||||
where Self: FilteredRequest,
|
||||
Self: JoinableRequest, // for RowDecoder
|
||||
Self: TypedRequest,
|
||||
RowDecoder: Identifiable,
|
||||
RowDecoder.ID: _OptionalProtocol,
|
||||
RowDecoder.ID.Wrapped: DatabaseValueConvertible
|
||||
@@ -620,21 +666,7 @@ public protocol _JoinableRequest {
|
||||
}
|
||||
|
||||
/// The protocol for all requests that can be associated.
|
||||
public protocol JoinableRequest: _JoinableRequest {
|
||||
/// The record type that can be associated to.
|
||||
///
|
||||
/// In the request below, it is Book:
|
||||
///
|
||||
/// let request = Book.all()
|
||||
///
|
||||
/// In the `belongsTo` association below, it is Author:
|
||||
///
|
||||
/// struct Book: TableRecord {
|
||||
/// // BelongsToAssociation<Book, Author>
|
||||
/// static let author = belongsTo(Author.self)
|
||||
/// }
|
||||
associatedtype RowDecoder
|
||||
}
|
||||
public protocol JoinableRequest: TypedRequest, _JoinableRequest { }
|
||||
|
||||
extension JoinableRequest {
|
||||
/// Creates a request that prefetches an association.
|
||||
|
||||
@@ -226,48 +226,3 @@ extension ColumnEncoder: SingleValueEncodingContainer {
|
||||
try recordEncoder.encode(value, forKey: key)
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 10.12, watchOS 3.0, tvOS 10.0, *)
|
||||
private var iso8601Formatter: ISO8601DateFormatter = {
|
||||
let formatter = ISO8601DateFormatter()
|
||||
formatter.formatOptions = .withInternetDateTime
|
||||
return formatter
|
||||
}()
|
||||
|
||||
extension DatabaseDateEncodingStrategy {
|
||||
fileprivate func encode(_ date: Date) -> DatabaseValueConvertible? {
|
||||
switch self {
|
||||
case .deferredToDate:
|
||||
return date.databaseValue
|
||||
case .timeIntervalSinceReferenceDate:
|
||||
return date.timeIntervalSinceReferenceDate
|
||||
case .timeIntervalSince1970:
|
||||
return date.timeIntervalSince1970
|
||||
case .millisecondsSince1970:
|
||||
return Int64(floor(1000.0 * date.timeIntervalSince1970))
|
||||
case .secondsSince1970:
|
||||
return Int64(floor(date.timeIntervalSince1970))
|
||||
case .iso8601:
|
||||
if #available(macOS 10.12, watchOS 3.0, tvOS 10.0, *) {
|
||||
return iso8601Formatter.string(from: date)
|
||||
} else {
|
||||
fatalError("ISO8601DateFormatter is unavailable on this platform.")
|
||||
}
|
||||
case .formatted(let formatter):
|
||||
return formatter.string(from: date)
|
||||
case .custom(let format):
|
||||
return format(date)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension DatabaseUUIDEncodingStrategy {
|
||||
fileprivate func encode(_ uuid: UUID) -> DatabaseValueConvertible? {
|
||||
switch self {
|
||||
case .deferredToUUID:
|
||||
return uuid.databaseValue
|
||||
case .string:
|
||||
return uuid.uuidString
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -374,6 +374,38 @@ public enum DatabaseDateEncodingStrategy {
|
||||
|
||||
/// Encodes the result of the user-provided function
|
||||
case custom((Date) -> DatabaseValueConvertible?)
|
||||
|
||||
@available(macOS 10.12, watchOS 3.0, tvOS 10.0, *)
|
||||
private static let iso8601Formatter: ISO8601DateFormatter = {
|
||||
let formatter = ISO8601DateFormatter()
|
||||
formatter.formatOptions = .withInternetDateTime
|
||||
return formatter
|
||||
}()
|
||||
|
||||
func encode(_ date: Date) -> DatabaseValueConvertible? {
|
||||
switch self {
|
||||
case .deferredToDate:
|
||||
return date.databaseValue
|
||||
case .timeIntervalSinceReferenceDate:
|
||||
return date.timeIntervalSinceReferenceDate
|
||||
case .timeIntervalSince1970:
|
||||
return date.timeIntervalSince1970
|
||||
case .millisecondsSince1970:
|
||||
return Int64(floor(1000.0 * date.timeIntervalSince1970))
|
||||
case .secondsSince1970:
|
||||
return Int64(floor(date.timeIntervalSince1970))
|
||||
case .iso8601:
|
||||
if #available(macOS 10.12, watchOS 3.0, tvOS 10.0, *) {
|
||||
return Self.iso8601Formatter.string(from: date)
|
||||
} else {
|
||||
fatalError("ISO8601DateFormatter is unavailable on this platform.")
|
||||
}
|
||||
case .formatted(let formatter):
|
||||
return formatter.string(from: date)
|
||||
case .custom(let format):
|
||||
return format(date)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - DatabaseUUIDEncodingStrategy
|
||||
@@ -397,6 +429,15 @@ public enum DatabaseUUIDEncodingStrategy {
|
||||
|
||||
/// Encodes UUIDs as strings such as "E621E1F8-C36C-495A-93FC-0C247A3E6E5F"
|
||||
case string
|
||||
|
||||
func encode(_ uuid: UUID) -> DatabaseValueConvertible {
|
||||
switch self {
|
||||
case .deferredToUUID:
|
||||
return uuid.databaseValue
|
||||
case .string:
|
||||
return uuid.uuidString
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - DatabaseColumnEncodingStrategy
|
||||
|
||||
Reference in new issue
Block a user