diff --git a/GRDB.xcodeproj/project.pbxproj b/GRDB.xcodeproj/project.pbxproj index fa5ed150e..f92ec9e27 100755 --- a/GRDB.xcodeproj/project.pbxproj +++ b/GRDB.xcodeproj/project.pbxproj @@ -27,6 +27,12 @@ 5605F1881C69111300235C62 /* DatabaseRegionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5605F1861C69111300235C62 /* DatabaseRegionTests.swift */; }; 5605F18D1C6B1A8700235C62 /* SQLCollatedExpression.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5605F1891C6B1A8700235C62 /* SQLCollatedExpression.swift */; }; 5605F18E1C6B1A8700235C62 /* SQLCollatedExpression.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5605F1891C6B1A8700235C62 /* SQLCollatedExpression.swift */; }; + 5607148C227617690091BB10 /* FetchableRecord+QueryInterface.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5607148B227617690091BB10 /* FetchableRecord+QueryInterface.swift */; }; + 5607148D227617690091BB10 /* FetchableRecord+QueryInterface.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5607148B227617690091BB10 /* FetchableRecord+QueryInterface.swift */; }; + 5607148E227617690091BB10 /* FetchableRecord+QueryInterface.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5607148B227617690091BB10 /* FetchableRecord+QueryInterface.swift */; }; + 560714962276177C0091BB10 /* Row+QueryInterface.swift in Sources */ = {isa = PBXBuildFile; fileRef = 560714952276177C0091BB10 /* Row+QueryInterface.swift */; }; + 560714972276177C0091BB10 /* Row+QueryInterface.swift in Sources */ = {isa = PBXBuildFile; fileRef = 560714952276177C0091BB10 /* Row+QueryInterface.swift */; }; + 560714982276177C0091BB10 /* Row+QueryInterface.swift in Sources */ = {isa = PBXBuildFile; fileRef = 560714952276177C0091BB10 /* Row+QueryInterface.swift */; }; 56071A411DB54ED000CA6E47 /* FetchedRecordsControllerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56B15D0A1CD4C35100A24C8B /* FetchedRecordsControllerTests.swift */; }; 5607EFD41BB827FD00605DE3 /* TransactionObserverTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5607EFD21BB8254800605DE3 /* TransactionObserverTests.swift */; }; 560A37A41C8F625000949E71 /* DatabasePool.swift in Sources */ = {isa = PBXBuildFile; fileRef = 560A37A31C8F625000949E71 /* DatabasePool.swift */; }; @@ -924,6 +930,8 @@ 5605F1581C672E4000235C62 /* StandardLibrary.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StandardLibrary.swift; sourceTree = ""; }; 5605F1861C69111300235C62 /* DatabaseRegionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseRegionTests.swift; sourceTree = ""; }; 5605F1891C6B1A8700235C62 /* SQLCollatedExpression.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLCollatedExpression.swift; sourceTree = ""; }; + 5607148B227617690091BB10 /* FetchableRecord+QueryInterface.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "FetchableRecord+QueryInterface.swift"; sourceTree = ""; }; + 560714952276177C0091BB10 /* Row+QueryInterface.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Row+QueryInterface.swift"; sourceTree = ""; }; 5607EFD21BB8254800605DE3 /* TransactionObserverTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TransactionObserverTests.swift; sourceTree = ""; }; 560A37A31C8F625000949E71 /* DatabasePool.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabasePool.swift; sourceTree = ""; }; 560A37A61C8FF6E500949E71 /* SerializedDatabase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SerializedDatabase.swift; sourceTree = ""; }; @@ -1546,11 +1554,13 @@ 5653EAF720944BC900F46237 /* Association */, 56CEB5401EAA359A00BFAF62 /* Column.swift */, 56D91AA82205F2F000770D8D /* DatabasePromise.swift */, + 5607148B227617690091BB10 /* FetchableRecord+QueryInterface.swift */, 56CEB5101EAA324B00BFAF62 /* FTS3+QueryInterfaceRequest.swift */, 56CEB5181EAA328900BFAF62 /* FTS5+QueryInterface.swift */, 56300B6F1C53F592005A543B /* QueryInterfaceRequest.swift */, 5653EB242094A14400F46237 /* QueryInterfaceRequest+Association.swift */, 5616AAF0207CD45E00AC3664 /* RequestProtocols.swift */, + 560714952276177C0091BB10 /* Row+QueryInterface.swift */, 5605F1891C6B1A8700235C62 /* SQLCollatedExpression.swift */, 566475991D97D8A000FF74B8 /* SQLCollection.swift */, 56CEB5411EAA359A00BFAF62 /* SQLExpressible.swift */, @@ -2318,6 +2328,7 @@ developmentRegion = English; hasScannedForEncodings = 0; knownRegions = ( + English, en, Base, ); @@ -2556,9 +2567,11 @@ 5613ED4A21A95C1200DC7A68 /* ValueObservation+Row.swift in Sources */, 565490D51D5AE252005622CB /* DatabaseValueConvertible+RawRepresentable.swift in Sources */, 565490C51D5AE236005622CB /* SerializedDatabase.swift in Sources */, + 560714982276177C0091BB10 /* Row+QueryInterface.swift in Sources */, 5698AC7E1DA37DCB0056AF8C /* VirtualTableModule.swift in Sources */, 564CE52221B3129A00652B19 /* ValueObservation+MapReducer.swift in Sources */, 5653EB1A20944C7C00F46237 /* ForeignKey.swift in Sources */, + 5607148E227617690091BB10 /* FetchableRecord+QueryInterface.swift in Sources */, 5698AC3D1D9E5A590056AF8C /* FTS3Pattern.swift in Sources */, 563EF45D2163309F007DAACD /* Inflections.swift in Sources */, 569D6DE0220EF9E100A058A9 /* SQLInterpolation.swift in Sources */, @@ -2708,6 +2721,7 @@ 564CE52121B3129A00652B19 /* ValueObservation+MapReducer.swift in Sources */, 5653EC132098738B00F46237 /* SQLGenerationContext.swift in Sources */, 5657AB121D10899D006283EF /* URL.swift in Sources */, + 560714972276177C0091BB10 /* Row+QueryInterface.swift in Sources */, 5616AAF2207CD45E00AC3664 /* RequestProtocols.swift in Sources */, 5659F48B1EA8D94E004A4992 /* Utils.swift in Sources */, 56A2387C1B9C75030082EB20 /* Configuration.swift in Sources */, @@ -2777,6 +2791,7 @@ 564F9C311F07611600877A00 /* DatabaseFunction.swift in Sources */, 564CE59E21B7A8B500652B19 /* ValueObservation+DistinctUntilChanged.swift in Sources */, 56B964A01DA51B4C0002DA19 /* FTS5.swift in Sources */, + 5607148D227617690091BB10 /* FetchableRecord+QueryInterface.swift in Sources */, 5674A6F01F307F0E0095F066 /* DatabaseValueConvertible+Encodable.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -3214,6 +3229,7 @@ 5698AD181DAAD17A0056AF8C /* FTS5Tokenizer.swift in Sources */, 56B964B11DA51D010002DA19 /* FTS5TokenizerDescriptor.swift in Sources */, 560A37A71C8FF6E500949E71 /* SerializedDatabase.swift in Sources */, + 560714962276177C0091BB10 /* Row+QueryInterface.swift in Sources */, 5653EB252094A14400F46237 /* QueryInterfaceRequest+Association.swift in Sources */, 5605F1691C672E4000235C62 /* NSString.swift in Sources */, 560D92401C672C3E00F4F92B /* DatabaseValueConvertible.swift in Sources */, @@ -3283,6 +3299,7 @@ 5657AAB91D107001006283EF /* NSData.swift in Sources */, 560D92421C672C3E00F4F92B /* StatementColumnConvertible.swift in Sources */, 56CEB5531EAA359A00BFAF62 /* SQLExpression.swift in Sources */, + 5607148C227617690091BB10 /* FetchableRecord+QueryInterface.swift in Sources */, 56B9649D1DA51B4C0002DA19 /* FTS5.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/GRDB/Core/Row.swift b/GRDB/Core/Row.swift index 369de8ce7..27b45cff6 100644 --- a/GRDB/Core/Row.swift +++ b/GRDB/Core/Row.swift @@ -931,7 +931,7 @@ extension FetchRequest where RowDecoder: Row { /// let row = try request.fetchOne(db) /// /// - parameter db: A database connection. - /// - returns: A,n optional rows. + /// - returns: An optional row. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. @inlinable public func fetchOne(_ db: Database) throws -> Row? { diff --git a/GRDB/QueryInterface/FetchableRecord+QueryInterface.swift b/GRDB/QueryInterface/FetchableRecord+QueryInterface.swift new file mode 100644 index 000000000..049a0d0d8 --- /dev/null +++ b/GRDB/QueryInterface/FetchableRecord+QueryInterface.swift @@ -0,0 +1,66 @@ +extension FetchableRecord { + + // MARK: Fetching From QueryInterfaceRequest + + /// Returns an array of records fetched from a query interface request. + /// + /// let request = try Player.all() + /// let players = try Player.fetchAll(db, request) // [Player] + /// + /// - parameters: + /// - db: A database connection. + /// - sql: a FetchRequest. + /// - returns: An array of records. + /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable + public static func fetchAll(_ db: Database, _ request: QueryInterfaceRequest) throws -> [Self] { + // TODO: prefetch if needed + let (statement, adapter) = try request.prepare(db, forSingleResult: false) + return try fetchAll(statement, adapter: adapter) + } + + /// Returns a single record fetched from a query interface request. + /// + /// let request = try Player.filter(key: 1) + /// let player = try Player.fetchOne(db, request) // Player? + /// + /// - parameters: + /// - db: A database connection. + /// - sql: a FetchRequest. + /// - returns: An optional record. + /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable + public static func fetchOne(_ db: Database, _ request: QueryInterfaceRequest) throws -> Self? { + // TODO: prefetch if needed + let (statement, adapter) = try request.prepare(db, forSingleResult: true) + return try fetchOne(statement, adapter: adapter) + } +} + +extension QueryInterfaceRequest where RowDecoder: FetchableRecord { + /// An array of fetched records. + /// + /// let request: QueryInterfaceRequest = ... + /// let players = try request.fetchAll(db) // [Player] + /// + /// - parameter db: A database connection. + /// - returns: An array of records. + /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable + public func fetchAll(_ db: Database) throws -> [RowDecoder] { + return try RowDecoder.fetchAll(db, self) + } + + /// The first fetched record. + /// + /// let request: QueryInterfaceRequest = ... + /// let player = try request.fetchOne(db) // Player? + /// + /// - parameter db: A database connection. + /// - returns: An optional record. + /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable + public func fetchOne(_ db: Database) throws -> RowDecoder? { + return try RowDecoder.fetchOne(db, self) + } +} diff --git a/GRDB/QueryInterface/Row+QueryInterface.swift b/GRDB/QueryInterface/Row+QueryInterface.swift new file mode 100644 index 000000000..30914f355 --- /dev/null +++ b/GRDB/QueryInterface/Row+QueryInterface.swift @@ -0,0 +1,69 @@ +extension Row { + + // MARK: - Fetching From QueryInterfaceRequest + + /// Returns an array of rows fetched from a fetch request. + /// + /// let request = Player.all() + /// let rows = try Row.fetchAll(db, request) + /// + /// - parameters: + /// - db: A database connection. + /// - request: A FetchRequest. + /// - returns: An array of rows. + /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable + public static func fetchAll(_ db: Database, _ request: QueryInterfaceRequest) throws -> [Row] { + // TODO: prefetch if needed + let (statement, adapter) = try request.prepare(db, forSingleResult: false) + return try fetchAll(statement, adapter: adapter) + } + + /// Returns a single row fetched from a fetch request. + /// + /// let request = Player.filter(key: 1) + /// let row = try Row.fetchOne(db, request) + /// + /// - parameters: + /// - db: A database connection. + /// - request: A FetchRequest. + /// - returns: An optional row. + /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable + public static func fetchOne(_ db: Database, _ request: QueryInterfaceRequest) throws -> Row? { + // TODO: prefetch if needed + let (statement, adapter) = try request.prepare(db, forSingleResult: true) + return try fetchOne(statement, adapter: adapter) + } +} + +extension QueryInterfaceRequest where RowDecoder: Row { + + // MARK: Fetching Rows + + /// An array of fetched rows. + /// + /// let request: QueryInterfaceRequest = ... + /// let rows = try request.fetchAll(db) + /// + /// - parameter db: A database connection. + /// - returns: An array of fetched rows. + /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable + public func fetchAll(_ db: Database) throws -> [Row] { + return try Row.fetchAll(db, self) + } + + /// The first fetched row. + /// + /// let request: QueryInterfaceRequest = ... + /// let row = try request.fetchOne(db) + /// + /// - parameter db: A database connection. + /// - returns: An optional row. + /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable + public func fetchOne(_ db: Database) throws -> Row? { + return try Row.fetchOne(db, self) + } +} diff --git a/GRDBCustom.xcodeproj/project.pbxproj b/GRDBCustom.xcodeproj/project.pbxproj index 1414ee207..d95023846 100755 --- a/GRDBCustom.xcodeproj/project.pbxproj +++ b/GRDBCustom.xcodeproj/project.pbxproj @@ -7,6 +7,10 @@ objects = { /* Begin PBXBuildFile section */ + 5607149B227618660091BB10 /* Row+QueryInterface.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56071499227618660091BB10 /* Row+QueryInterface.swift */; }; + 5607149C227618660091BB10 /* Row+QueryInterface.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56071499227618660091BB10 /* Row+QueryInterface.swift */; }; + 5607149E227618800091BB10 /* FetchableRecord+QueryInterface.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5607149D227618800091BB10 /* FetchableRecord+QueryInterface.swift */; }; + 5607149F227618800091BB10 /* FetchableRecord+QueryInterface.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5607149D227618800091BB10 /* FetchableRecord+QueryInterface.swift */; }; 56071A501DB54ED300CA6E47 /* FetchedRecordsControllerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56B15D0A1CD4C35100A24C8B /* FetchedRecordsControllerTests.swift */; }; 5613ED6121A95E6100DC7A68 /* ValueObservation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5613ED5921A95E6100DC7A68 /* ValueObservation.swift */; }; 5613ED6221A95E6100DC7A68 /* ValueObservation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5613ED5921A95E6100DC7A68 /* ValueObservation.swift */; }; @@ -682,6 +686,8 @@ 5605F1861C69111300235C62 /* DatabaseRegionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseRegionTests.swift; sourceTree = ""; }; 5605F1891C6B1A8700235C62 /* SQLCollatedExpression.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLCollatedExpression.swift; sourceTree = ""; }; 5605F18C1C6B1A8700235C62 /* SQLSelectQuery.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLSelectQuery.swift; sourceTree = ""; }; + 56071499227618660091BB10 /* Row+QueryInterface.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Row+QueryInterface.swift"; sourceTree = ""; }; + 5607149D227618800091BB10 /* FetchableRecord+QueryInterface.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "FetchableRecord+QueryInterface.swift"; sourceTree = ""; }; 5607EFD21BB8254800605DE3 /* TransactionObserverTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TransactionObserverTests.swift; sourceTree = ""; }; 560A37A31C8F625000949E71 /* DatabasePool.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabasePool.swift; sourceTree = ""; }; 560A37A61C8FF6E500949E71 /* SerializedDatabase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SerializedDatabase.swift; sourceTree = ""; }; @@ -1250,11 +1256,13 @@ 5653EB3620961F6000F46237 /* Association */, 56CEB5401EAA359A00BFAF62 /* Column.swift */, 56D91AA52205F2CF00770D8D /* DatabasePromise.swift */, + 5607149D227618800091BB10 /* FetchableRecord+QueryInterface.swift */, 56CEB5101EAA324B00BFAF62 /* FTS3+QueryInterfaceRequest.swift */, 56CEB5181EAA328900BFAF62 /* FTS5+QueryInterface.swift */, 56300B6F1C53F592005A543B /* QueryInterfaceRequest.swift */, 5653EB5320961F7D00F46237 /* QueryInterfaceRequest+Association.swift */, 5616AAF4207CD59300AC3664 /* RequestProtocols.swift */, + 56071499227618660091BB10 /* Row+QueryInterface.swift */, 5605F1891C6B1A8700235C62 /* SQLCollatedExpression.swift */, 566475991D97D8A000FF74B8 /* SQLCollection.swift */, 56CEB5411EAA359A00BFAF62 /* SQLExpressible.swift */, @@ -2099,8 +2107,10 @@ 5653EB4820961F6100F46237 /* HasManyAssociation.swift in Sources */, F3BA80301CFB289F003DC1BA /* DatabaseMigrator.swift in Sources */, 56E9FAD62210538400C703A8 /* SQLLiteral.swift in Sources */, + 5607149F227618800091BB10 /* FetchableRecord+QueryInterface.swift in Sources */, 5613ED6221A95E6100DC7A68 /* ValueObservation.swift in Sources */, F3BA80361CFB28A4003DC1BA /* TableRecord.swift in Sources */, + 5607149C227618660091BB10 /* Row+QueryInterface.swift in Sources */, 567071F5208A00BE006AD95A /* SQLiteDateParser.swift in Sources */, F3BA80271CFB2891003DC1BA /* DatabaseValueConvertible+RawRepresentable.swift in Sources */, 5613ED6421A95E6100DC7A68 /* ValueObservation+Combine.swift in Sources */, @@ -2416,8 +2426,10 @@ 5653EB4720961F6100F46237 /* HasManyAssociation.swift in Sources */, F3BA808C1CFB2E75003DC1BA /* DatabaseMigrator.swift in Sources */, 56E9FAD52210538400C703A8 /* SQLLiteral.swift in Sources */, + 5607149E227618800091BB10 /* FetchableRecord+QueryInterface.swift in Sources */, 5613ED6121A95E6100DC7A68 /* ValueObservation.swift in Sources */, F3BA80921CFB2E7A003DC1BA /* TableRecord.swift in Sources */, + 5607149B227618660091BB10 /* Row+QueryInterface.swift in Sources */, 567071F4208A00BE006AD95A /* SQLiteDateParser.swift in Sources */, F3BA80831CFB2E67003DC1BA /* DatabaseValueConvertible+RawRepresentable.swift in Sources */, 5613ED6321A95E6100DC7A68 /* ValueObservation+Combine.swift in Sources */,