diff --git a/DemoApps/GRDBDemoiOS/GRDBDemoiOS/PlayersViewController.swift b/DemoApps/GRDBDemoiOS/GRDBDemoiOS/PlayersViewController.swift index a47810a6d..bd0258d82 100644 --- a/DemoApps/GRDBDemoiOS/GRDBDemoiOS/PlayersViewController.swift +++ b/DemoApps/GRDBDemoiOS/GRDBDemoiOS/PlayersViewController.swift @@ -69,7 +69,7 @@ class PlayersViewController: UITableViewController { private func configureTitle() { // Track changes in the number of players playerCountObserver = try! ValueObservation - .trackingCount(withUniquing: playersRequest) + .trackingCount(playersRequest) .start(in: dbQueue) { [unowned self] count in switch count { case 0: self.navigationItem.title = "No Player" diff --git a/GRDB.xcodeproj/project.pbxproj b/GRDB.xcodeproj/project.pbxproj index 00806ec7d..b42142903 100755 --- a/GRDB.xcodeproj/project.pbxproj +++ b/GRDB.xcodeproj/project.pbxproj @@ -137,8 +137,6 @@ 563B071621862C4700B38F35 /* ValueObservationRecordTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 563B071421862C4600B38F35 /* ValueObservationRecordTests.swift */; }; 563B071821862F4C00B38F35 /* ValueObservationDatabaseValueConvertibleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 563B071721862F4C00B38F35 /* ValueObservationDatabaseValueConvertibleTests.swift */; }; 563B071921862F4C00B38F35 /* ValueObservationDatabaseValueConvertibleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 563B071721862F4C00B38F35 /* ValueObservationDatabaseValueConvertibleTests.swift */; }; - 563B0723218634CC00B38F35 /* ValueObservationStatementColumnConvertibleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 563B0722218634CC00B38F35 /* ValueObservationStatementColumnConvertibleTests.swift */; }; - 563B0724218634CC00B38F35 /* ValueObservationStatementColumnConvertibleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 563B0722218634CC00B38F35 /* ValueObservationStatementColumnConvertibleTests.swift */; }; 563EF415215F87EB007DAACD /* OrderedDictionary.swift in Sources */ = {isa = PBXBuildFile; fileRef = 563EF414215F87EB007DAACD /* OrderedDictionary.swift */; }; 563EF416215F87EB007DAACD /* OrderedDictionary.swift in Sources */ = {isa = PBXBuildFile; fileRef = 563EF414215F87EB007DAACD /* OrderedDictionary.swift */; }; 563EF417215F87EB007DAACD /* OrderedDictionary.swift in Sources */ = {isa = PBXBuildFile; fileRef = 563EF414215F87EB007DAACD /* OrderedDictionary.swift */; }; @@ -922,7 +920,6 @@ 563B0704218627F700B38F35 /* ValueObservationRowTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ValueObservationRowTests.swift; sourceTree = ""; }; 563B071421862C4600B38F35 /* ValueObservationRecordTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ValueObservationRecordTests.swift; sourceTree = ""; }; 563B071721862F4C00B38F35 /* ValueObservationDatabaseValueConvertibleTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ValueObservationDatabaseValueConvertibleTests.swift; sourceTree = ""; }; - 563B0722218634CC00B38F35 /* ValueObservationStatementColumnConvertibleTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ValueObservationStatementColumnConvertibleTests.swift; sourceTree = ""; }; 563EF414215F87EB007DAACD /* OrderedDictionary.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OrderedDictionary.swift; sourceTree = ""; }; 563EF42C2161180D007DAACD /* AssociationAggregate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AssociationAggregate.swift; sourceTree = ""; }; 563EF43E216131D1007DAACD /* AssociationAggregateTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AssociationAggregateTests.swift; sourceTree = ""; }; @@ -1496,7 +1493,6 @@ 563B06BC2185CCD300B38F35 /* ValueObservationReducerTests.swift */, 563B0704218627F700B38F35 /* ValueObservationRowTests.swift */, 563B06C12185D29F00B38F35 /* ValueObservationSchedulingTests.swift */, - 563B0722218634CC00B38F35 /* ValueObservationStatementColumnConvertibleTests.swift */, ); name = ValueObservation; sourceTree = ""; @@ -2799,7 +2795,6 @@ 562393641DEE06D300A6B01F /* CursorTests.swift in Sources */, 563B06C62185D29F00B38F35 /* ValueObservationSchedulingTests.swift in Sources */, 5657AB5A1D108BA9006283EF /* FoundationNSStringTests.swift in Sources */, - 563B0724218634CC00B38F35 /* ValueObservationStatementColumnConvertibleTests.swift in Sources */, 56300B6C1C53D3E8005A543B /* TableRecord+QueryInterfaceRequestTests.swift in Sources */, 5674A7231F30A8DF0095F066 /* MutablePersistableRecordEncodableTests.swift in Sources */, 56F0B9921B6001C600A2F135 /* FoundationNSDateTests.swift in Sources */, @@ -2976,7 +2971,6 @@ 5653EAD620944B4F00F46237 /* AssociationChainRowScopesTests.swift in Sources */, 563B06C52185D29F00B38F35 /* ValueObservationSchedulingTests.swift in Sources */, 5698AC801DA380A20056AF8C /* VirtualTableModuleTests.swift in Sources */, - 563B0723218634CC00B38F35 /* ValueObservationStatementColumnConvertibleTests.swift in Sources */, 56D496821D813131008276D7 /* TransactionObserverTests.swift in Sources */, 56D496941D81317B008276D7 /* SchedulingWatchdogTests.swift in Sources */, 5674A7201F30A8DF0095F066 /* MutablePersistableRecordEncodableTests.swift in Sources */, diff --git a/GRDB/Core/ValueObservation.swift b/GRDB/Core/ValueObservation.swift index e30e0077a..9c3c8f462 100644 --- a/GRDB/Core/ValueObservation.swift +++ b/GRDB/Core/ValueObservation.swift @@ -197,7 +197,7 @@ public enum ValueReducers { } } - public struct UniqueRecords: ValueReducer { + public struct Records: ValueReducer { let _fetch: (Database) throws -> [Row] var rows: [Row]? @@ -214,6 +214,7 @@ public enum ValueReducers { /// :nodoc: public mutating func value(_ newRows: [Row]) -> [Record]? { if let rows = rows, rows == newRows { + // Don't notify consecutive identical rows return nil } self.rows = newRows @@ -221,7 +222,7 @@ public enum ValueReducers { } } - public struct UniqueRecord: ValueReducer { + public struct Record: ValueReducer { let _fetch: (Database) throws -> Row? var row: Row?? @@ -238,6 +239,7 @@ public enum ValueReducers { /// :nodoc: public mutating func value(_ newRow: Row?) -> Record?? { if let row = row, row == newRow { + // Don't notify consecutive identical row return nil } self.row = newRow @@ -245,7 +247,7 @@ public enum ValueReducers { } } - public struct UniqueValues: ValueReducer { + public struct Values: ValueReducer { let _fetch: (Database) throws -> [DatabaseValue] var dbValues: [DatabaseValue]? @@ -262,6 +264,7 @@ public enum ValueReducers { /// :nodoc: public mutating func value(_ newDbValues: [DatabaseValue]) -> [T]? { if let dbValues = dbValues, dbValues == newDbValues { + // Don't notify consecutive identical dbValues return nil } self.dbValues = newDbValues @@ -269,9 +272,10 @@ public enum ValueReducers { } } - public struct UniqueValue: ValueReducer { + public struct Value: ValueReducer { let _fetch: (Database) throws -> DatabaseValue? var dbValue: DatabaseValue?? + var previousValueWasNil = false /// TODO public init(_ fetch: @escaping (Database) throws -> DatabaseValue?) { @@ -286,14 +290,34 @@ public enum ValueReducers { /// :nodoc: public mutating func value(_ newDbValue: DatabaseValue?) -> T?? { if let dbValue = dbValue, dbValue == newDbValue { + // Don't notify consecutive identical dbValue return nil } self.dbValue = newDbValue - return .some(newDbValue.map { T.decode(from: $0, conversionContext: nil) }) + guard let dbValue = newDbValue else { + if previousValueWasNil { + return nil + } else { + previousValueWasNil = true + return .some(nil) + } + } + let value = T.decodeIfPresent(from: dbValue, conversionContext: nil) + if let value = value { + previousValueWasNil = false + return .some(value) + } else { + if previousValueWasNil { + return nil + } else { + previousValueWasNil = true + return .some(nil) + } + } } } - public struct UniqueOptionalValues: ValueReducer { + public struct OptionalValues: ValueReducer { let _fetch: (Database) throws -> [DatabaseValue] var dbValues: [DatabaseValue]? @@ -310,6 +334,7 @@ public enum ValueReducers { /// :nodoc: public mutating func value(_ newDbValues: [DatabaseValue]) -> [T?]? { if let dbValues = dbValues, dbValues == newDbValues { + // Don't notify consecutive identical dbValues return nil } self.dbValues = newDbValues @@ -613,38 +638,6 @@ extension ValueObservation where Reducer == Void { /// - parameter request: the observed request. /// - returns: a ValueObservation. public static func trackingCount(_ request: Request) - -> ValueObservation> - { - return ValueObservation.tracking(request, fetch: request.fetchCount) - } - - /// Creates a ValueObservation which observes *request*, and notifies a - /// fresh count whenever the request is impacted by a database transaction. - /// - /// For example: - /// - /// let request = Player.all() - /// let observation = ValueObservation.trackingCount(withUniquing: request) - /// - /// let observer = try observation.start(in: dbQueue) { count: Int in - /// print("number of players has changed") - /// } - /// - /// The returned observation has the default configuration: - /// - /// - When started with the `start(_:onError:onChage:)` method, a fresh - /// value is immediately notified on the dispatch queue which starts - /// the observation. - /// - Upon subsequent database changes, fresh values are notified on the - /// main queue. - /// - The observation lasts until the observer returned by - /// `start` is deallocated. - /// - /// See ValueObservation for more information. - /// - /// - parameter request: the observed request. - /// - returns: a ValueObservation. - public static func trackingCount(withUniquing request: Request) -> ValueObservation> { return ValueObservation.tracking(withUniquing: request, fetch: request.fetchCount) @@ -681,39 +674,6 @@ extension ValueObservation where Reducer == Void { /// - parameter request: the observed request. /// - returns: a ValueObservation. public static func trackingAll(_ request: Request) - -> ValueObservation> - where Request.RowDecoder == Row - { - return ValueObservation.tracking(request, fetch: request.fetchAll) - } - - /// Creates a ValueObservation which observes *request*, and notifies - /// fresh rows whenever the request is impacted by a database transaction. - /// - /// For example: - /// - /// let request = SQLRequest("SELECT * FROM player") - /// let observation = ValueObservation.trackingAll(withUniquing: request) - /// - /// let observer = try observation.start(in: dbQueue) { rows: [Row] in - /// print("players have changed") - /// } - /// - /// The returned observation has the default configuration: - /// - /// - When started with the `start(_:onError:onChage:)` method, a fresh - /// value is immediately notified on the dispatch queue which starts - /// the observation. - /// - Upon subsequent database changes, fresh values are notified on the - /// main queue. - /// - The observation lasts until the observer returned by - /// `start` is deallocated. - /// - /// See ValueObservation for more information. - /// - /// - parameter request: the observed request. - /// - returns: a ValueObservation. - public static func trackingAll(withUniquing request: Request) -> ValueObservation> where Request.RowDecoder == Row { @@ -747,39 +707,6 @@ extension ValueObservation where Reducer == Void { /// - parameter request: the observed request. /// - returns: a new ValueObservation. public static func trackingOne(_ request: Request) - -> ValueObservation> - where Request.RowDecoder == Row - { - return ValueObservation.tracking(request, fetch: request.fetchOne) - } - - /// Creates a ValueObservation which observes *request*, and notifies a - /// fresh row whenever the request is impacted by a database transaction. - /// - /// For example: - /// - /// let request = SQLRequest("SELECT * FROM player WHERE id = ?", arguments: [1]) - /// let observation = ValueObservation.trackingOne(withUniquing: request) - /// - /// let observer = try observation.start(in: dbQueue) { row: Row? in - /// print("players have changed") - /// } - /// - /// The returned observation has the default configuration: - /// - /// - When started with the `start(_:onError:onChage:)` method, a fresh - /// value is immediately notified on the dispatch queue which starts - /// the observation. - /// - Upon subsequent database changes, fresh values are notified on the - /// main queue. - /// - The observation lasts until the observer returned by - /// `start` is deallocated. - /// - /// See ValueObservation for more information. - /// - /// - parameter request: the observed request. - /// - returns: a new ValueObservation. - public static func trackingOne(withUniquing request: Request) -> ValueObservation> where Request.RowDecoder == Row { @@ -818,46 +745,12 @@ extension ValueObservation where Reducer == Void { /// - parameter request: the observed request. /// - returns: a ValueObservation. public static func trackingAll(_ request: Request) - -> ValueObservation> + -> ValueObservation> where Request.RowDecoder: FetchableRecord { - return ValueObservation.tracking(request, fetch: request.fetchAll) - } - - /// Creates a ValueObservation which observes *request*, and notifies - /// fresh records whenever the request is impacted by a - /// database transaction. - /// - /// For example: - /// - /// let request = Player.all() - /// let observation = ValueObservation.trackingAll(withUniquing: request) - /// - /// let observer = try observation.start(in: dbQueue) { players: [Player] in - /// print("players have changed") - /// } - /// - /// The returned observation has the default configuration: - /// - /// - When started with the `start(_:onError:onChage:)` method, a fresh - /// value is immediately notified on the dispatch queue which starts - /// the observation. - /// - Upon subsequent database changes, fresh values are notified on the - /// main queue. - /// - The observation lasts until the observer returned by - /// `start` is deallocated. - /// - /// See ValueObservation for more information. - /// - /// - parameter request: the observed request. - /// - returns: a ValueObservation. - public static func trackingAll(withUniquing request: Request) - -> ValueObservation> - where Request.RowDecoder: FetchableRecord - { - return ValueObservation>.tracking( + return ValueObservation>.tracking( request, - reducer: ValueReducers.UniqueRecords { try Row.fetchAll($0, request) }) + reducer: ValueReducers.Records { try Row.fetchAll($0, request) }) } /// Creates a ValueObservation which observes *request*, and notifies a @@ -887,45 +780,12 @@ extension ValueObservation where Reducer == Void { /// - parameter request: the observed request. /// - returns: a ValueObservation. public static func trackingOne(_ request: Request) -> - ValueObservation> + ValueObservation> where Request.RowDecoder: FetchableRecord { - return ValueObservation.tracking(request, fetch: request.fetchOne) - } - - /// Creates a ValueObservation which observes *request*, and notifies a - /// fresh record whenever the request is impacted by a database transaction. - /// - /// For example: - /// - /// let request = Player.filter(key: 1) - /// let observation = ValueObservation.trackingOne(withUniquing: request) - /// - /// let observer = try observation.start(in: dbQueue) { player: Player? in - /// print("player has changed") - /// } - /// - /// The returned observation has the default configuration: - /// - /// - When started with the `start(_:onError:onChage:)` method, a fresh - /// value is immediately notified on the dispatch queue which starts - /// the observation. - /// - Upon subsequent database changes, fresh values are notified on the - /// main queue. - /// - The observation lasts until the observer returned by - /// `start` is deallocated. - /// - /// See ValueObservation for more information. - /// - /// - parameter request: the observed request. - /// - returns: a ValueObservation. - public static func trackingOne(withUniquing request: Request) -> - ValueObservation> - where Request.RowDecoder: FetchableRecord - { - return ValueObservation>.tracking( + return ValueObservation>.tracking( request, - reducer: ValueReducers.UniqueRecord { try Row.fetchOne($0, request) }) + reducer: ValueReducers.Record { try Row.fetchOne($0, request) }) } } @@ -960,46 +820,12 @@ extension ValueObservation where Reducer == Void { /// - parameter request: the observed request. /// - returns: a ValueObservation. public static func trackingAll(_ request: Request) - -> ValueObservation> + -> ValueObservation> where Request.RowDecoder: DatabaseValueConvertible { - return ValueObservation.tracking(request, fetch: request.fetchAll) - } - - /// Creates a ValueObservation which observes *request*, and notifies - /// fresh values whenever the request is impacted by a - /// database transaction. - /// - /// For example: - /// - /// let request = Player.select(Column("name"), as: String.self) - /// let observation = ValueObservation.trackingAll(withUniquing: request) - /// - /// let observer = try observation.start(in: dbQueue) { names: [String] in - /// print("player name have changed") - /// } - /// - /// The returned observation has the default configuration: - /// - /// - When started with the `start(_:onError:onChage:)` method, a fresh - /// value is immediately notified on the dispatch queue which starts - /// the observation. - /// - Upon subsequent database changes, fresh values are notified on the - /// main queue. - /// - The observation lasts until the observer returned by - /// `start` is deallocated. - /// - /// See ValueObservation for more information. - /// - /// - parameter request: the observed request. - /// - returns: a ValueObservation. - public static func trackingAll(withUniquing request: Request) - -> ValueObservation> - where Request.RowDecoder: DatabaseValueConvertible - { - return ValueObservation>.tracking( + return ValueObservation>.tracking( request, - reducer: ValueReducers.UniqueValues { try DatabaseValue.fetchAll($0, request) }) + reducer: ValueReducers.Values { try DatabaseValue.fetchAll($0, request) }) } /// Creates a ValueObservation which observes *request*, and notifies a @@ -1029,116 +855,12 @@ extension ValueObservation where Reducer == Void { /// - parameter request: the observed request. /// - returns: a ValueObservation. public static func trackingOne(_ request: Request) - -> ValueObservation> + -> ValueObservation> where Request.RowDecoder: DatabaseValueConvertible { - return ValueObservation.tracking(request, fetch: request.fetchOne) - } - - /// Creates a ValueObservation which observes *request*, and notifies a - /// fresh value whenever the request is impacted by a database transaction. - /// - /// For example: - /// - /// let request = Player.select(max(Column("score")), as: Int.self) - /// let observation = ValueObservation.trackingOne(withUniquing: request) - /// - /// let observer = try observation.start(in: dbQueue) { maxScore: Int? in - /// print("maximum score has changed") - /// } - /// - /// The returned observation has the default configuration: - /// - /// - When started with the `start(_:onError:onChage:)` method, a fresh - /// value is immediately notified on the dispatch queue which starts - /// the observation. - /// - Upon subsequent database changes, fresh values are notified on the - /// main queue. - /// - The observation lasts until the observer returned by - /// `start` is deallocated. - /// - /// See ValueObservation for more information. - /// - /// - parameter request: the observed request. - /// - returns: a ValueObservation. - public static func trackingOne(withUniquing request: Request) - -> ValueObservation> - where Request.RowDecoder: DatabaseValueConvertible - { - return ValueObservation>.tracking( + return ValueObservation>.tracking( request, - reducer: ValueReducers.UniqueValue { try DatabaseValue.fetchOne($0, request) }) - } -} - -// MARK: - DatabaseValueConvertible & StatementColumnConvertible Observation - -extension ValueObservation where Reducer == Void { - /// Creates a ValueObservation which observes *request*, and notifies - /// fresh values whenever the request is impacted by a - /// database transaction. - /// - /// For example: - /// - /// let request = Player.select(Column("name"), as: String.self) - /// let observation = ValueObservation.trackingAll(request) - /// - /// let observer = try observation.start(in: dbQueue) { names: [String] in - /// print("player name have changed") - /// } - /// - /// The returned observation has the default configuration: - /// - /// - When started with the `start(_:onError:onChage:)` method, a fresh - /// value is immediately notified on the dispatch queue which starts - /// the observation. - /// - Upon subsequent database changes, fresh values are notified on the - /// main queue. - /// - The observation lasts until the observer returned by - /// `start` is deallocated. - /// - /// See ValueObservation for more information. - /// - /// - parameter request: the observed request. - /// - returns: a ValueObservation. - public static func trackingAll(_ request: Request) - -> ValueObservation> - where Request.RowDecoder: DatabaseValueConvertible & StatementColumnConvertible - { - return ValueObservation.tracking(request, fetch: request.fetchAll) - } - - /// Creates a ValueObservation which observes *request*, and notifies a - /// fresh value whenever the request is impacted by a database transaction. - /// - /// For example: - /// - /// let request = Player.select(max(Column("score")), as: Int.self) - /// let observation = ValueObservation.trackingOne(request) - /// - /// let observer = try observation.start(in: dbQueue) { maxScore: Int? in - /// print("maximum score has changed") - /// } - /// - /// The returned observation has the default configuration: - /// - /// - When started with the `start(_:onError:onChage:)` method, a fresh - /// value is immediately notified on the dispatch queue which starts - /// the observation. - /// - Upon subsequent database changes, fresh values are notified on the - /// main queue. - /// - The observation lasts until the observer returned by - /// `start` is deallocated. - /// - /// See ValueObservation for more information. - /// - /// - parameter request: the observed request. - /// - returns: a ValueObservation. - public static func trackingOne(_ request: Request) - -> ValueObservation> - where Request.RowDecoder: DatabaseValueConvertible & StatementColumnConvertible - { - return ValueObservation.tracking(request, fetch: request.fetchOne) + reducer: ValueReducers.Value { try DatabaseValue.fetchOne($0, request) }) } } @@ -1173,86 +895,12 @@ extension ValueObservation where Reducer == Void { /// - parameter request: the observed request. /// - returns: a ValueObservation. public static func trackingAll(_ request: Request) - -> ValueObservation> + -> ValueObservation> where Request.RowDecoder: _OptionalProtocol, Request.RowDecoder._Wrapped: DatabaseValueConvertible { - return ValueObservation.tracking(request, fetch: request.fetchAll) - } - - /// Creates a ValueObservation which observes *request*, and notifies - /// fresh values whenever the request is impacted by a - /// database transaction. - /// - /// For example: - /// - /// let request = Player.select(Column("name"), as: String.self) - /// let observation = ValueObservation.trackingAll(withUniquing: request) - /// - /// let observer = try observation.start(in: dbQueue) { names: [String] in - /// print("player name have changed") - /// } - /// - /// The returned observation has the default configuration: - /// - /// - When started with the `start(_:onError:onChage:)` method, a fresh - /// value is immediately notified on the dispatch queue which starts - /// the observation. - /// - Upon subsequent database changes, fresh values are notified on the - /// main queue. - /// - The observation lasts until the observer returned by - /// `start` is deallocated. - /// - /// See ValueObservation for more information. - /// - /// - parameter request: the observed request. - /// - returns: a ValueObservation. - public static func trackingAll(withUniquing request: Request) - -> ValueObservation> - where Request.RowDecoder: _OptionalProtocol, - Request.RowDecoder._Wrapped: DatabaseValueConvertible - { - return ValueObservation>.tracking( + return ValueObservation>.tracking( request, - reducer: ValueReducers.UniqueOptionalValues { try DatabaseValue.fetchAll($0, request) }) - } -} - -// MARK: - Optional DatabaseValueConvertible & StatementColumnConvertible Observation - -extension ValueObservation where Reducer == Void { - /// Creates a ValueObservation which observes *request*, and notifies - /// fresh values whenever the request is impacted by a - /// database transaction. - /// - /// For example: - /// - /// let request = Player.select(Column("name"), as: String.self) - /// let observation = ValueObservation.trackingAll(request) - /// - /// let observer = try observation.start(in: dbQueue) { names: [String] in - /// print("player name have changed") - /// } - /// - /// The returned observation has the default configuration: - /// - /// - When started with the `start(_:onError:onChage:)` method, a fresh - /// value is immediately notified on the dispatch queue which starts - /// the observation. - /// - Upon subsequent database changes, fresh values are notified on the - /// main queue. - /// - The observation lasts until the observer returned by - /// `start` is deallocated. - /// - /// See ValueObservation for more information. - /// - /// - parameter request: the observed request. - /// - returns: a ValueObservation. - public static func trackingAll(_ request: Request) - -> ValueObservation> - where Request.RowDecoder: _OptionalProtocol, - Request.RowDecoder._Wrapped: DatabaseValueConvertible & StatementColumnConvertible - { - return ValueObservation.tracking(request, fetch: request.fetchAll) + reducer: ValueReducers.OptionalValues { try DatabaseValue.fetchAll($0, request) }) } } diff --git a/GRDBCipher.xcodeproj/project.pbxproj b/GRDBCipher.xcodeproj/project.pbxproj index 9f4650dab..f6541248d 100755 --- a/GRDBCipher.xcodeproj/project.pbxproj +++ b/GRDBCipher.xcodeproj/project.pbxproj @@ -218,10 +218,6 @@ 563B071F21862F5D00B38F35 /* ValueObservationDatabaseValueConvertibleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 563B071D21862F5D00B38F35 /* ValueObservationDatabaseValueConvertibleTests.swift */; }; 563B072021862F5D00B38F35 /* ValueObservationDatabaseValueConvertibleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 563B071D21862F5D00B38F35 /* ValueObservationDatabaseValueConvertibleTests.swift */; }; 563B072121862F5D00B38F35 /* ValueObservationDatabaseValueConvertibleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 563B071D21862F5D00B38F35 /* ValueObservationDatabaseValueConvertibleTests.swift */; }; - 563B0729218634E200B38F35 /* ValueObservationStatementColumnConvertibleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 563B0728218634E100B38F35 /* ValueObservationStatementColumnConvertibleTests.swift */; }; - 563B072A218634E200B38F35 /* ValueObservationStatementColumnConvertibleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 563B0728218634E100B38F35 /* ValueObservationStatementColumnConvertibleTests.swift */; }; - 563B072B218634E200B38F35 /* ValueObservationStatementColumnConvertibleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 563B0728218634E100B38F35 /* ValueObservationStatementColumnConvertibleTests.swift */; }; - 563B072C218634E200B38F35 /* ValueObservationStatementColumnConvertibleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 563B0728218634E100B38F35 /* ValueObservationStatementColumnConvertibleTests.swift */; }; 563EF424215F8A88007DAACD /* OrderedDictionary.swift in Sources */ = {isa = PBXBuildFile; fileRef = 563EF422215F8A88007DAACD /* OrderedDictionary.swift */; }; 563EF425215F8A88007DAACD /* OrderedDictionary.swift in Sources */ = {isa = PBXBuildFile; fileRef = 563EF422215F8A88007DAACD /* OrderedDictionary.swift */; }; 563EF43C2161183E007DAACD /* AssociationAggregate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 563EF43A2161183E007DAACD /* AssociationAggregate.swift */; }; @@ -1040,7 +1036,6 @@ 563B07072186280500B38F35 /* ValueObservationRowTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ValueObservationRowTests.swift; sourceTree = ""; }; 563B070C21862C3500B38F35 /* ValueObservationRecordTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ValueObservationRecordTests.swift; sourceTree = ""; }; 563B071D21862F5D00B38F35 /* ValueObservationDatabaseValueConvertibleTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ValueObservationDatabaseValueConvertibleTests.swift; sourceTree = ""; }; - 563B0728218634E100B38F35 /* ValueObservationStatementColumnConvertibleTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ValueObservationStatementColumnConvertibleTests.swift; sourceTree = ""; }; 563EF422215F8A88007DAACD /* OrderedDictionary.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OrderedDictionary.swift; sourceTree = ""; }; 563EF43A2161183E007DAACD /* AssociationAggregate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationAggregate.swift; sourceTree = ""; }; 563EF44421613212007DAACD /* AssociationAggregateTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationAggregateTests.swift; sourceTree = ""; }; @@ -1584,7 +1579,6 @@ 563B06DF2185E07200B38F35 /* ValueObservationReducerTests.swift */, 563B07072186280500B38F35 /* ValueObservationRowTests.swift */, 563B06E02185E07200B38F35 /* ValueObservationSchedulingTests.swift */, - 563B0728218634E100B38F35 /* ValueObservationStatementColumnConvertibleTests.swift */, ); name = ValueObservation; sourceTree = ""; @@ -2509,7 +2503,6 @@ 560FC59E1CB00B880014AA8E /* FoundationNSDateTests.swift in Sources */, 568068321EBBA26100EFB8AA /* SQLRequestTests.swift in Sources */, 560FC59F1CB00B880014AA8E /* DatabaseTests.swift in Sources */, - 563B0729218634E200B38F35 /* ValueObservationStatementColumnConvertibleTests.swift in Sources */, 560FC5A01CB00B880014AA8E /* QueryInterfaceRequestTests.swift in Sources */, 5698ACCF1DA8C2620056AF8C /* RecordPrimaryKeyHiddenRowIDTests.swift in Sources */, 560FC5A21CB00B880014AA8E /* DatabaseFunctionTests.swift in Sources */, @@ -2686,7 +2679,6 @@ 5671565B1CB16729007DC145 /* TableRecord+QueryInterfaceRequestTests.swift in Sources */, 568068331EBBA26100EFB8AA /* SQLRequestTests.swift in Sources */, 5671565C1CB16729007DC145 /* FoundationNSDateTests.swift in Sources */, - 563B072A218634E200B38F35 /* ValueObservationStatementColumnConvertibleTests.swift in Sources */, 5671565D1CB16729007DC145 /* DatabaseTests.swift in Sources */, 5671565E1CB16729007DC145 /* QueryInterfaceRequestTests.swift in Sources */, 5698ACD01DA8C2620056AF8C /* RecordPrimaryKeyHiddenRowIDTests.swift in Sources */, @@ -2979,7 +2971,6 @@ 56AFCA6B1CB1AA9900F48B96 /* DatabaseTimestampTests.swift in Sources */, 568068361EBBA26100EFB8AA /* SQLRequestTests.swift in Sources */, 56AFCA6C1CB1AA9900F48B96 /* StatementArguments+FoundationTests.swift in Sources */, - 563B072B218634E200B38F35 /* ValueObservationStatementColumnConvertibleTests.swift in Sources */, 562393651DEE06D300A6B01F /* CursorTests.swift in Sources */, 5657AB5B1D108BA9006283EF /* FoundationNSStringTests.swift in Sources */, 56AFCA6D1CB1AA9900F48B96 /* TableRecord+QueryInterfaceRequestTests.swift in Sources */, @@ -3156,7 +3147,6 @@ 56AFCAC41CB1ABC800F48B96 /* DatabaseTimestampTests.swift in Sources */, 568068371EBBA26100EFB8AA /* SQLRequestTests.swift in Sources */, 56AFCAC51CB1ABC800F48B96 /* StatementArguments+FoundationTests.swift in Sources */, - 563B072C218634E200B38F35 /* ValueObservationStatementColumnConvertibleTests.swift in Sources */, 562393661DEE06D300A6B01F /* CursorTests.swift in Sources */, 56AFCAC61CB1ABC800F48B96 /* TableRecord+QueryInterfaceRequestTests.swift in Sources */, 56AFCAC71CB1ABC800F48B96 /* FoundationNSDateTests.swift in Sources */, diff --git a/GRDBCustom.xcodeproj/project.pbxproj b/GRDBCustom.xcodeproj/project.pbxproj index 3d164e482..7b4462c5f 100755 --- a/GRDBCustom.xcodeproj/project.pbxproj +++ b/GRDBCustom.xcodeproj/project.pbxproj @@ -63,8 +63,6 @@ 563B071321862C3E00B38F35 /* ValueObservationRecordTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 563B071121862C3E00B38F35 /* ValueObservationRecordTests.swift */; }; 563B071B21862F5600B38F35 /* ValueObservationDatabaseValueConvertibleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 563B071A21862F5600B38F35 /* ValueObservationDatabaseValueConvertibleTests.swift */; }; 563B071C21862F5600B38F35 /* ValueObservationDatabaseValueConvertibleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 563B071A21862F5600B38F35 /* ValueObservationDatabaseValueConvertibleTests.swift */; }; - 563B0726218634D800B38F35 /* ValueObservationStatementColumnConvertibleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 563B0725218634D700B38F35 /* ValueObservationStatementColumnConvertibleTests.swift */; }; - 563B0727218634D800B38F35 /* ValueObservationStatementColumnConvertibleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 563B0725218634D700B38F35 /* ValueObservationStatementColumnConvertibleTests.swift */; }; 563EF420215F8A76007DAACD /* OrderedDictionary.swift in Sources */ = {isa = PBXBuildFile; fileRef = 563EF41E215F8A76007DAACD /* OrderedDictionary.swift */; }; 563EF421215F8A76007DAACD /* OrderedDictionary.swift in Sources */ = {isa = PBXBuildFile; fileRef = 563EF41E215F8A76007DAACD /* OrderedDictionary.swift */; }; 563EF4382161182C007DAACD /* AssociationAggregate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 563EF4362161182C007DAACD /* AssociationAggregate.swift */; }; @@ -681,7 +679,6 @@ 563B0701218627DE00B38F35 /* ValueObservationRowTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ValueObservationRowTests.swift; sourceTree = ""; }; 563B071121862C3E00B38F35 /* ValueObservationRecordTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ValueObservationRecordTests.swift; sourceTree = ""; }; 563B071A21862F5600B38F35 /* ValueObservationDatabaseValueConvertibleTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ValueObservationDatabaseValueConvertibleTests.swift; sourceTree = ""; }; - 563B0725218634D700B38F35 /* ValueObservationStatementColumnConvertibleTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ValueObservationStatementColumnConvertibleTests.swift; sourceTree = ""; }; 563EF41E215F8A76007DAACD /* OrderedDictionary.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OrderedDictionary.swift; sourceTree = ""; }; 563EF4362161182C007DAACD /* AssociationAggregate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationAggregate.swift; sourceTree = ""; }; 563EF441216131F5007DAACD /* AssociationAggregateTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationAggregateTests.swift; sourceTree = ""; }; @@ -1198,7 +1195,6 @@ 563B06CF2185E04600B38F35 /* ValueObservationReducerTests.swift */, 563B0701218627DE00B38F35 /* ValueObservationRowTests.swift */, 563B06D02185E04600B38F35 /* ValueObservationSchedulingTests.swift */, - 563B0725218634D700B38F35 /* ValueObservationStatementColumnConvertibleTests.swift */, ); name = ValueObservation; sourceTree = ""; @@ -2152,7 +2148,6 @@ F3BA810B1CFB3056003DC1BA /* Row+FoundationTests.swift in Sources */, 5698AC901DA389380056AF8C /* FTS3TableBuilderTests.swift in Sources */, 568068381EBBA26100EFB8AA /* SQLRequestTests.swift in Sources */, - 563B0727218634D800B38F35 /* ValueObservationStatementColumnConvertibleTests.swift in Sources */, F3BA80DB1CFB300E003DC1BA /* DatabaseValueConversionTests.swift in Sources */, 56741EAF1E66A8B3003E422D /* FetchRequestTests.swift in Sources */, 5657AB5D1D108BA9006283EF /* FoundationNSStringTests.swift in Sources */, @@ -2445,7 +2440,6 @@ F3BA81131CFB305B003DC1BA /* DatabaseMigratorTests.swift in Sources */, F3BA81381CFB3064003DC1BA /* RecordSubClassTests.swift in Sources */, F3BA81351CFB3064003DC1BA /* RecordEditedTests.swift in Sources */, - 563B0726218634D800B38F35 /* ValueObservationStatementColumnConvertibleTests.swift in Sources */, 568068341EBBA26100EFB8AA /* SQLRequestTests.swift in Sources */, 5657AB691D108BA9006283EF /* FoundationURLTests.swift in Sources */, 56741EAB1E66A8B3003E422D /* FetchRequestTests.swift in Sources */, diff --git a/Tests/GRDBTests/ValueObservationCountTests.swift b/Tests/GRDBTests/ValueObservationCountTests.swift index 3519d11e1..70a9f7eca 100644 --- a/Tests/GRDBTests/ValueObservationCountTests.swift +++ b/Tests/GRDBTests/ValueObservationCountTests.swift @@ -20,7 +20,7 @@ class ValueObservationCountTests: GRDBTestCase { var counts: [Int] = [] let notificationExpectation = expectation(description: "notification") notificationExpectation.assertForOverFulfill = true - notificationExpectation.expectedFulfillmentCount = 4 + notificationExpectation.expectedFulfillmentCount = 5 struct T: TableRecord { } var observation = ValueObservation.trackingCount(T.all()) @@ -30,48 +30,20 @@ class ValueObservationCountTests: GRDBTestCase { notificationExpectation.fulfill() } - try dbQueue.write { - try $0.execute("INSERT INTO t DEFAULT VALUES") - } - try dbQueue.write { - try $0.execute("UPDATE t SET id = id") - } - try dbQueue.write { - try $0.execute("INSERT INTO t DEFAULT VALUES") + try dbQueue.inDatabase { db in + try db.execute("INSERT INTO t DEFAULT VALUES") // +1 + try db.execute("UPDATE t SET id = id") // = + try db.execute("INSERT INTO t DEFAULT VALUES") // +1 + try db.inTransaction { // +1 + try db.execute("INSERT INTO t DEFAULT VALUES") + try db.execute("INSERT INTO t DEFAULT VALUES") + try db.execute("DELETE FROM t WHERE id = 1") + return .commit + } + try db.execute("DELETE FROM t WHERE id = 2") // -1 } waitForExpectations(timeout: 1, handler: nil) - XCTAssertEqual(counts, [0, 1, 1, 2]) - } - - func testCountWithUniquing() throws { - let dbQueue = try makeDatabaseQueue() - try dbQueue.write { try $0.execute("CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT)") } - - var counts: [Int] = [] - let notificationExpectation = expectation(description: "notification") - notificationExpectation.assertForOverFulfill = true - notificationExpectation.expectedFulfillmentCount = 3 - - struct T: TableRecord { } - var observation = ValueObservation.trackingCount(withUniquing: T.all()) - observation.extent = .databaseLifetime - _ = try observation.start(in: dbQueue) { count in - counts.append(count) - notificationExpectation.fulfill() - } - - try dbQueue.write { - try $0.execute("INSERT INTO t DEFAULT VALUES") - } - try dbQueue.write { - try $0.execute("UPDATE t SET id = id") - } - try dbQueue.write { - try $0.execute("INSERT INTO t DEFAULT VALUES") - } - - waitForExpectations(timeout: 1, handler: nil) - XCTAssertEqual(counts, [0, 1, 2]) + XCTAssertEqual(counts, [0, 1, 2, 3, 2]) } } diff --git a/Tests/GRDBTests/ValueObservationDatabaseValueConvertibleTests.swift b/Tests/GRDBTests/ValueObservationDatabaseValueConvertibleTests.swift index 70392ddbb..1f5e11562 100644 --- a/Tests/GRDBTests/ValueObservationDatabaseValueConvertibleTests.swift +++ b/Tests/GRDBTests/ValueObservationDatabaseValueConvertibleTests.swift @@ -44,55 +44,24 @@ class ValueObservationDatabaseValueConvertibleTests: GRDBTestCase { notificationExpectation.fulfill() } - try dbQueue.write { - try $0.execute("INSERT INTO t (id, name) VALUES (1, 'foo')") - } - try dbQueue.write { - try $0.execute("UPDATE t SET name = 'foo' WHERE id = 1") - } - try dbQueue.write { - try $0.execute("INSERT INTO t (id, name) VALUES (2, 'bar')") + try dbQueue.inDatabase { db in + try db.execute("INSERT INTO t (id, name) VALUES (1, 'foo')") // +1 + try db.execute("UPDATE t SET name = 'foo' WHERE id = 1") // = + try db.inTransaction { // +1 + try db.execute("INSERT INTO t (id, name) VALUES (2, 'bar')") + try db.execute("INSERT INTO t (id, name) VALUES (3, 'baz')") + try db.execute("DELETE FROM t WHERE id = 3") + return .commit + } + try db.execute("DELETE FROM t WHERE id = 1") // -1 } waitForExpectations(timeout: 1, handler: nil) XCTAssertEqual(results.map { $0.map { $0.rawValue }}, [ [], ["foo"], - ["foo"], - ["foo", "bar"]]) - } - - func testAllWithUniquing() throws { - let dbQueue = try makeDatabaseQueue() - try dbQueue.write { try $0.execute("CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)") } - - var results: [[Name]] = [] - let notificationExpectation = expectation(description: "notification") - notificationExpectation.assertForOverFulfill = true - notificationExpectation.expectedFulfillmentCount = 3 - - var observation = ValueObservation.trackingAll(withUniquing: SQLRequest("SELECT name FROM t ORDER BY id")) - observation.extent = .databaseLifetime - _ = try observation.start(in: dbQueue) { names in - results.append(names) - notificationExpectation.fulfill() - } - - try dbQueue.write { - try $0.execute("INSERT INTO t (id, name) VALUES (1, 'foo')") - } - try dbQueue.write { - try $0.execute("UPDATE t SET name = 'foo' WHERE id = 1") - } - try dbQueue.write { - try $0.execute("INSERT INTO t (id, name) VALUES (2, 'bar')") - } - - waitForExpectations(timeout: 1, handler: nil) - XCTAssertEqual(results.map { $0.map { $0.rawValue }}, [ - [], - ["foo"], - ["foo", "bar"]]) + ["foo", "bar"], + ["bar"]]) } func testOne() throws { @@ -102,7 +71,7 @@ class ValueObservationDatabaseValueConvertibleTests: GRDBTestCase { var results: [Name?] = [] let notificationExpectation = expectation(description: "notification") notificationExpectation.assertForOverFulfill = true - notificationExpectation.expectedFulfillmentCount = 5 + notificationExpectation.expectedFulfillmentCount = 7 var observation = ValueObservation.trackingOne(SQLRequest("SELECT name FROM t ORDER BY id DESC")) observation.extent = .databaseLifetime @@ -111,63 +80,32 @@ class ValueObservationDatabaseValueConvertibleTests: GRDBTestCase { notificationExpectation.fulfill() } - try dbQueue.write { - try $0.execute("INSERT INTO t (id, name) VALUES (1, 'foo')") - } - try dbQueue.write { - try $0.execute("UPDATE t SET name = 'foo' WHERE id = 1") - } - try dbQueue.write { - try $0.execute("INSERT INTO t (id, name) VALUES (2, 'bar')") - } - try dbQueue.write { - try $0.execute("DELETE FROM t") + try dbQueue.inDatabase { db in + try db.execute("INSERT INTO t (id, name) VALUES (1, 'foo')") + try db.execute("UPDATE t SET name = 'foo' WHERE id = 1") + try db.inTransaction { + try db.execute("INSERT INTO t (id, name) VALUES (2, 'bar')") + try db.execute("INSERT INTO t (id, name) VALUES (3, 'baz')") + try db.execute("DELETE FROM t WHERE id = 3") + return .commit + } + try db.execute("DELETE FROM t") + try db.execute("INSERT INTO t (id, name) VALUES (1, 'baz')") + try db.execute("UPDATE t SET name = NULL") + try db.execute("DELETE FROM t") + try db.execute("INSERT INTO t (id, name) VALUES (1, NULL)") + try db.execute("UPDATE t SET name = 'qux'") } waitForExpectations(timeout: 1, handler: nil) XCTAssertEqual(results.map { $0.map { $0.rawValue }}, [ nil, "foo", - "foo", "bar", - nil]) - } - - func testOneWithUniquing() throws { - let dbQueue = try makeDatabaseQueue() - try dbQueue.write { try $0.execute("CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)") } - - var results: [Name?] = [] - let notificationExpectation = expectation(description: "notification") - notificationExpectation.assertForOverFulfill = true - notificationExpectation.expectedFulfillmentCount = 4 - - var observation = ValueObservation.trackingOne(withUniquing: SQLRequest("SELECT name FROM t ORDER BY id DESC")) - observation.extent = .databaseLifetime - _ = try observation.start(in: dbQueue) { name in - results.append(name) - notificationExpectation.fulfill() - } - - try dbQueue.write { - try $0.execute("INSERT INTO t (id, name) VALUES (1, 'foo')") - } - try dbQueue.write { - try $0.execute("UPDATE t SET name = 'foo' WHERE id = 1") - } - try dbQueue.write { - try $0.execute("INSERT INTO t (id, name) VALUES (2, 'bar')") - } - try dbQueue.write { - try $0.execute("DELETE FROM t") - } - - waitForExpectations(timeout: 1, handler: nil) - XCTAssertEqual(results.map { $0.map { $0.rawValue }}, [ nil, - "foo", - "bar", - nil]) + "baz", + nil, + "qux"]) } func testAllOptional() throws { @@ -186,54 +124,23 @@ class ValueObservationDatabaseValueConvertibleTests: GRDBTestCase { notificationExpectation.fulfill() } - try dbQueue.write { - try $0.execute("INSERT INTO t (id, name) VALUES (1, 'foo')") - } - try dbQueue.write { - try $0.execute("UPDATE t SET name = 'foo' WHERE id = 1") - } - try dbQueue.write { - try $0.execute("INSERT INTO t (id, name) VALUES (2, NULL)") + try dbQueue.inDatabase { db in + try db.execute("INSERT INTO t (id, name) VALUES (1, 'foo')") // +1 + try db.execute("UPDATE t SET name = 'foo' WHERE id = 1") // = + try db.inTransaction { // +1 + try db.execute("INSERT INTO t (id, name) VALUES (2, NULL)") + try db.execute("INSERT INTO t (id, name) VALUES (3, 'baz')") + try db.execute("DELETE FROM t WHERE id = 3") + return .commit + } + try db.execute("DELETE FROM t WHERE id = 1") // -1 } waitForExpectations(timeout: 1, handler: nil) XCTAssertEqual(results.map { $0.map { $0?.rawValue }}, [ [], ["foo"], - ["foo"], - ["foo", nil]]) - } - - func testAllOptionalWithUniquing() throws { - let dbQueue = try makeDatabaseQueue() - try dbQueue.write { try $0.execute("CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)") } - - var results: [[Name?]] = [] - let notificationExpectation = expectation(description: "notification") - notificationExpectation.assertForOverFulfill = true - notificationExpectation.expectedFulfillmentCount = 3 - - var observation = ValueObservation.trackingAll(withUniquing: SQLRequest("SELECT name FROM t ORDER BY id")) - observation.extent = .databaseLifetime - _ = try observation.start(in: dbQueue) { names in - results.append(names) - notificationExpectation.fulfill() - } - - try dbQueue.write { - try $0.execute("INSERT INTO t (id, name) VALUES (1, 'foo')") - } - try dbQueue.write { - try $0.execute("UPDATE t SET name = 'foo' WHERE id = 1") - } - try dbQueue.write { - try $0.execute("INSERT INTO t (id, name) VALUES (2, NULL)") - } - - waitForExpectations(timeout: 1, handler: nil) - XCTAssertEqual(results.map { $0.map { $0?.rawValue }}, [ - [], - ["foo"], - ["foo", nil]]) + ["foo", nil], + [nil]]) } } diff --git a/Tests/GRDBTests/ValueObservationRecordTests.swift b/Tests/GRDBTests/ValueObservationRecordTests.swift index 36fc96fb5..d145b85ee 100644 --- a/Tests/GRDBTests/ValueObservationRecordTests.swift +++ b/Tests/GRDBTests/ValueObservationRecordTests.swift @@ -43,55 +43,24 @@ class ValueObservationRecordTests: GRDBTestCase { notificationExpectation.fulfill() } - try dbQueue.write { - try $0.execute("INSERT INTO t (id, name) VALUES (1, 'foo')") - } - try dbQueue.write { - try $0.execute("UPDATE t SET name = 'foo' WHERE id = 1") - } - try dbQueue.write { - try $0.execute("INSERT INTO t (id, name) VALUES (2, 'bar')") + try dbQueue.inDatabase { db in + try db.execute("INSERT INTO t (id, name) VALUES (1, 'foo')") // +1 + try db.execute("UPDATE t SET name = 'foo' WHERE id = 1") // = + try db.inTransaction { // +1 + try db.execute("INSERT INTO t (id, name) VALUES (2, 'bar')") + try db.execute("INSERT INTO t (id, name) VALUES (3, 'baz')") + try db.execute("DELETE FROM t WHERE id = 3") + return .commit + } + try db.execute("DELETE FROM t WHERE id = 1") // -1 } waitForExpectations(timeout: 1, handler: nil) XCTAssertEqual(results.map { $0.map { $0.row }}, [ [], [["id":1, "name":"foo"]], - [["id":1, "name":"foo"]], - [["id":1, "name":"foo"], ["id":2, "name":"bar"]]]) - } - - func testAllWithUniquing() throws { - let dbQueue = try makeDatabaseQueue() - try dbQueue.write { try $0.execute("CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)") } - - var results: [[Player]] = [] - let notificationExpectation = expectation(description: "notification") - notificationExpectation.assertForOverFulfill = true - notificationExpectation.expectedFulfillmentCount = 3 - - var observation = ValueObservation.trackingAll(withUniquing: SQLRequest("SELECT * FROM t ORDER BY id")) - observation.extent = .databaseLifetime - _ = try observation.start(in: dbQueue) { players in - results.append(players) - notificationExpectation.fulfill() - } - - try dbQueue.write { - try $0.execute("INSERT INTO t (id, name) VALUES (1, 'foo')") - } - try dbQueue.write { - try $0.execute("UPDATE t SET name = 'foo' WHERE id = 1") - } - try dbQueue.write { - try $0.execute("INSERT INTO t (id, name) VALUES (2, 'bar')") - } - - waitForExpectations(timeout: 1, handler: nil) - XCTAssertEqual(results.map { $0.map { $0.row }}, [ - [], - [["id":1, "name":"foo"]], - [["id":1, "name":"foo"], ["id":2, "name":"bar"]]]) + [["id":1, "name":"foo"], ["id":2, "name":"bar"]], + [["id":2, "name":"bar"]]]) } func testOne() throws { @@ -101,7 +70,7 @@ class ValueObservationRecordTests: GRDBTestCase { var results: [Player?] = [] let notificationExpectation = expectation(description: "notification") notificationExpectation.assertForOverFulfill = true - notificationExpectation.expectedFulfillmentCount = 5 + notificationExpectation.expectedFulfillmentCount = 4 var observation = ValueObservation.trackingOne(SQLRequest("SELECT * FROM t ORDER BY id DESC")) observation.extent = .databaseLifetime @@ -110,57 +79,18 @@ class ValueObservationRecordTests: GRDBTestCase { notificationExpectation.fulfill() } - try dbQueue.write { - try $0.execute("INSERT INTO t (id, name) VALUES (1, 'foo')") - } - try dbQueue.write { - try $0.execute("UPDATE t SET name = 'foo' WHERE id = 1") - } - try dbQueue.write { - try $0.execute("INSERT INTO t (id, name) VALUES (2, 'bar')") - } - try dbQueue.write { - try $0.execute("DELETE FROM t") + try dbQueue.inDatabase { db in + try db.execute("INSERT INTO t (id, name) VALUES (1, 'foo')") // +1 + try db.execute("UPDATE t SET name = 'foo' WHERE id = 1") // = + try db.inTransaction { // +1 + try db.execute("INSERT INTO t (id, name) VALUES (2, 'bar')") + try db.execute("INSERT INTO t (id, name) VALUES (3, 'baz')") + try db.execute("DELETE FROM t WHERE id = 3") + return .commit + } + try db.execute("DELETE FROM t") // -1 } - waitForExpectations(timeout: 1, handler: nil) - XCTAssertEqual(results.map { $0.map { $0.row }}, [ - nil, - ["id":1, "name":"foo"], - ["id":1, "name":"foo"], - ["id":2, "name":"bar"], - nil]) - } - - func testOneWithUniquing() throws { - let dbQueue = try makeDatabaseQueue() - try dbQueue.write { try $0.execute("CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)") } - - var results: [Player?] = [] - let notificationExpectation = expectation(description: "notification") - notificationExpectation.assertForOverFulfill = true - notificationExpectation.expectedFulfillmentCount = 4 - - var observation = ValueObservation.trackingOne(withUniquing: SQLRequest("SELECT * FROM t ORDER BY id DESC")) - observation.extent = .databaseLifetime - _ = try observation.start(in: dbQueue) { player in - results.append(player) - notificationExpectation.fulfill() - } - - try dbQueue.write { - try $0.execute("INSERT INTO t (id, name) VALUES (1, 'foo')") - } - try dbQueue.write { - try $0.execute("UPDATE t SET name = 'foo' WHERE id = 1") - } - try dbQueue.write { - try $0.execute("INSERT INTO t (id, name) VALUES (2, 'bar')") - } - try dbQueue.write { - try $0.execute("DELETE FROM t") - } - waitForExpectations(timeout: 1, handler: nil) XCTAssertEqual(results.map { $0.map { $0.row }}, [ nil, diff --git a/Tests/GRDBTests/ValueObservationReducerTests.swift b/Tests/GRDBTests/ValueObservationReducerTests.swift index 59ecac53e..393da3e7c 100644 --- a/Tests/GRDBTests/ValueObservationReducerTests.swift +++ b/Tests/GRDBTests/ValueObservationReducerTests.swift @@ -279,7 +279,7 @@ class ValueObservationReducerTests: GRDBTestCase { var counts: [String] = [] let notificationExpectation = expectation(description: "notification") notificationExpectation.assertForOverFulfill = true - notificationExpectation.expectedFulfillmentCount = 4 + notificationExpectation.expectedFulfillmentCount = 3 struct T: TableRecord { } var observation = ValueObservation @@ -293,11 +293,10 @@ class ValueObservationReducerTests: GRDBTestCase { try dbQueue.inDatabase { try $0.execute("INSERT INTO t DEFAULT VALUES") - try $0.execute("UPDATE t SET id = id") try $0.execute("INSERT INTO t DEFAULT VALUES") } waitForExpectations(timeout: 1, handler: nil) - XCTAssertEqual(counts, ["0", "1", "1", "2"]) + XCTAssertEqual(counts, ["0", "1", "2"]) } } diff --git a/Tests/GRDBTests/ValueObservationRowTests.swift b/Tests/GRDBTests/ValueObservationRowTests.swift index c5a55c9ae..1b6656ebf 100644 --- a/Tests/GRDBTests/ValueObservationRowTests.swift +++ b/Tests/GRDBTests/ValueObservationRowTests.swift @@ -29,55 +29,24 @@ class ValueObservationRowTests: GRDBTestCase { notificationExpectation.fulfill() } - try dbQueue.write { - try $0.execute("INSERT INTO t (id, name) VALUES (1, 'foo')") - } - try dbQueue.write { - try $0.execute("UPDATE t SET name = 'foo' WHERE id = 1") - } - try dbQueue.write { - try $0.execute("INSERT INTO t (id, name) VALUES (2, 'bar')") + try dbQueue.inDatabase { db in + try db.execute("INSERT INTO t (id, name) VALUES (1, 'foo')") // +1 + try db.execute("UPDATE t SET name = 'foo' WHERE id = 1") // = + try db.inTransaction { // +1 + try db.execute("INSERT INTO t (id, name) VALUES (2, 'bar')") + try db.execute("INSERT INTO t (id, name) VALUES (3, 'baz')") + try db.execute("DELETE FROM t WHERE id = 3") + return .commit + } + try db.execute("DELETE FROM t WHERE id = 1") // -1 } waitForExpectations(timeout: 1, handler: nil) XCTAssertEqual(results, [ [], [["id":1, "name":"foo"]], - [["id":1, "name":"foo"]], - [["id":1, "name":"foo"], ["id":2, "name":"bar"]]]) - } - - func testAllWithUniquing() throws { - let dbQueue = try makeDatabaseQueue() - try dbQueue.write { try $0.execute("CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)") } - - var results: [[Row]] = [] - let notificationExpectation = expectation(description: "notification") - notificationExpectation.assertForOverFulfill = true - notificationExpectation.expectedFulfillmentCount = 3 - - var observation = ValueObservation.trackingAll(withUniquing: SQLRequest("SELECT * FROM t ORDER BY id")) - observation.extent = .databaseLifetime - _ = try observation.start(in: dbQueue) { rows in - results.append(rows) - notificationExpectation.fulfill() - } - - try dbQueue.write { - try $0.execute("INSERT INTO t (id, name) VALUES (1, 'foo')") - } - try dbQueue.write { - try $0.execute("UPDATE t SET name = 'foo' WHERE id = 1") - } - try dbQueue.write { - try $0.execute("INSERT INTO t (id, name) VALUES (2, 'bar')") - } - - waitForExpectations(timeout: 1, handler: nil) - XCTAssertEqual(results, [ - [], - [["id":1, "name":"foo"]], - [["id":1, "name":"foo"], ["id":2, "name":"bar"]]]) + [["id":1, "name":"foo"], ["id":2, "name":"bar"]], + [["id":2, "name":"bar"]]]) } func testOne() throws { @@ -87,7 +56,7 @@ class ValueObservationRowTests: GRDBTestCase { var results: [Row?] = [] let notificationExpectation = expectation(description: "notification") notificationExpectation.assertForOverFulfill = true - notificationExpectation.expectedFulfillmentCount = 5 + notificationExpectation.expectedFulfillmentCount = 4 var observation = ValueObservation.trackingOne(SQLRequest("SELECT * FROM t ORDER BY id DESC")) observation.extent = .databaseLifetime @@ -96,57 +65,18 @@ class ValueObservationRowTests: GRDBTestCase { notificationExpectation.fulfill() } - try dbQueue.write { - try $0.execute("INSERT INTO t (id, name) VALUES (1, 'foo')") - } - try dbQueue.write { - try $0.execute("UPDATE t SET name = 'foo' WHERE id = 1") - } - try dbQueue.write { - try $0.execute("INSERT INTO t (id, name) VALUES (2, 'bar')") - } - try dbQueue.write { - try $0.execute("DELETE FROM t") + try dbQueue.inDatabase { db in + try db.execute("INSERT INTO t (id, name) VALUES (1, 'foo')") // +1 + try db.execute("UPDATE t SET name = 'foo' WHERE id = 1") // = + try db.inTransaction { // +1 + try db.execute("INSERT INTO t (id, name) VALUES (2, 'bar')") + try db.execute("INSERT INTO t (id, name) VALUES (3, 'baz')") + try db.execute("DELETE FROM t WHERE id = 3") + return .commit + } + try db.execute("DELETE FROM t") // -1 } - waitForExpectations(timeout: 1, handler: nil) - XCTAssertEqual(results, [ - nil, - ["id":1, "name":"foo"], - ["id":1, "name":"foo"], - ["id":2, "name":"bar"], - nil]) - } - - func testOneWithUniquing() throws { - let dbQueue = try makeDatabaseQueue() - try dbQueue.write { try $0.execute("CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)") } - - var results: [Row?] = [] - let notificationExpectation = expectation(description: "notification") - notificationExpectation.assertForOverFulfill = true - notificationExpectation.expectedFulfillmentCount = 4 - - var observation = ValueObservation.trackingOne(withUniquing: SQLRequest("SELECT * FROM t ORDER BY id DESC")) - observation.extent = .databaseLifetime - _ = try observation.start(in: dbQueue) { row in - results.append(row) - notificationExpectation.fulfill() - } - - try dbQueue.write { - try $0.execute("INSERT INTO t (id, name) VALUES (1, 'foo')") - } - try dbQueue.write { - try $0.execute("UPDATE t SET name = 'foo' WHERE id = 1") - } - try dbQueue.write { - try $0.execute("INSERT INTO t (id, name) VALUES (2, 'bar')") - } - try dbQueue.write { - try $0.execute("DELETE FROM t") - } - waitForExpectations(timeout: 1, handler: nil) XCTAssertEqual(results, [ nil, diff --git a/Tests/GRDBTests/ValueObservationStatementColumnConvertibleTests.swift b/Tests/GRDBTests/ValueObservationStatementColumnConvertibleTests.swift deleted file mode 100644 index fc5bdc299..000000000 --- a/Tests/GRDBTests/ValueObservationStatementColumnConvertibleTests.swift +++ /dev/null @@ -1,148 +0,0 @@ -import XCTest -#if GRDBCIPHER - import GRDBCipher -#elseif GRDBCUSTOMSQLITE - import GRDBCustomSQLite -#else - #if SWIFT_PACKAGE - import CSQLite - #else - import SQLite3 - #endif - import GRDB -#endif - -private struct Name: DatabaseValueConvertible { - var rawValue: String - var fast: Bool - - var databaseValue: DatabaseValue { - return rawValue.databaseValue - } - - static func fromDatabaseValue(_ dbValue: DatabaseValue) -> Name? { - guard let rawValue = String.fromDatabaseValue(dbValue) else { - return nil - } - return Name(rawValue: rawValue, fast: false) - } -} - -extension Name: StatementColumnConvertible { - init(sqliteStatement: SQLiteStatement, index: Int32) { - self.init(rawValue: String(sqliteStatement: sqliteStatement, index: index), fast: true) - } -} - -class ValueObservationStatementColumnConvertibleTests: GRDBTestCase { - func testAll() throws { - let dbQueue = try makeDatabaseQueue() - try dbQueue.write { try $0.execute("CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)") } - - var results: [[Name]] = [] - let notificationExpectation = expectation(description: "notification") - notificationExpectation.assertForOverFulfill = true - notificationExpectation.expectedFulfillmentCount = 4 - - var observation = ValueObservation.trackingAll(SQLRequest("SELECT name FROM t ORDER BY id")) - observation.extent = .databaseLifetime - _ = try observation.start(in: dbQueue) { names in - XCTAssert(names.allSatisfy { $0.fast }) - results.append(names) - notificationExpectation.fulfill() - } - - try dbQueue.write { - try $0.execute("INSERT INTO t (id, name) VALUES (1, 'foo')") - } - try dbQueue.write { - try $0.execute("UPDATE t SET name = 'foo' WHERE id = 1") - } - try dbQueue.write { - try $0.execute("INSERT INTO t (id, name) VALUES (2, 'bar')") - } - - waitForExpectations(timeout: 1, handler: nil) - XCTAssertEqual(results.map { $0.map { $0.rawValue }}, [ - [], - ["foo"], - ["foo"], - ["foo", "bar"]]) - } - - func testOne() throws { - let dbQueue = try makeDatabaseQueue() - try dbQueue.write { try $0.execute("CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)") } - - var results: [Name?] = [] - let notificationExpectation = expectation(description: "notification") - notificationExpectation.assertForOverFulfill = true - notificationExpectation.expectedFulfillmentCount = 5 - - var observation = ValueObservation.trackingOne(SQLRequest("SELECT name FROM t ORDER BY id DESC")) - observation.extent = .databaseLifetime - _ = try observation.start(in: dbQueue) { name in - if let name = name { - XCTAssert(name.fast) - } - results.append(name) - notificationExpectation.fulfill() - } - - try dbQueue.write { - try $0.execute("INSERT INTO t (id, name) VALUES (1, 'foo')") - } - try dbQueue.write { - try $0.execute("UPDATE t SET name = 'foo' WHERE id = 1") - } - try dbQueue.write { - try $0.execute("INSERT INTO t (id, name) VALUES (2, 'bar')") - } - try dbQueue.write { - try $0.execute("DELETE FROM t") - } - - waitForExpectations(timeout: 1, handler: nil) - XCTAssertEqual(results.map { $0.map { $0.rawValue }}, [ - nil, - "foo", - "foo", - "bar", - nil]) - } - - func testAllOptional() throws { - let dbQueue = try makeDatabaseQueue() - try dbQueue.write { try $0.execute("CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)") } - - var results: [[Name?]] = [] - let notificationExpectation = expectation(description: "notification") - notificationExpectation.assertForOverFulfill = true - notificationExpectation.expectedFulfillmentCount = 4 - - var observation = ValueObservation.trackingAll(SQLRequest("SELECT name FROM t ORDER BY id")) - observation.extent = .databaseLifetime - _ = try observation.start(in: dbQueue) { names in - XCTAssert(names.allSatisfy { $0.map { $0.fast } ?? true }) - results.append(names) - notificationExpectation.fulfill() - } - - try dbQueue.write { - try $0.execute("INSERT INTO t (id, name) VALUES (1, 'foo')") - } - try dbQueue.write { - try $0.execute("UPDATE t SET name = 'foo' WHERE id = 1") - } - try dbQueue.write { - try $0.execute("INSERT INTO t (id, name) VALUES (2, NULL)") - } - - waitForExpectations(timeout: 1, handler: nil) - XCTAssertEqual(results.map { $0.map { $0?.rawValue }}, [ - [], - ["foo"], - ["foo"], - ["foo", nil]]) - } -}