diff --git a/GRDB/Core/Cursor.swift b/GRDB/Core/Cursor.swift index d843c25a6..51ef3b303 100644 --- a/GRDB/Core/Cursor.swift +++ b/GRDB/Core/Cursor.swift @@ -16,6 +16,7 @@ extension Array { /// /// let cursor = try String.fetchCursor(db, sql: "SELECT 'foo' UNION ALL SELECT 'bar'") /// let strings = try Array(cursor) // ["foo", "bar"] + @inlinable public init(_ cursor: C) throws where C.Element == Element { self.init() while let element = try cursor.next() { diff --git a/GRDB/Core/DatabaseValueConvertible.swift b/GRDB/Core/DatabaseValueConvertible.swift index 5b2cafb57..ad66f5863 100644 --- a/GRDB/Core/DatabaseValueConvertible.swift +++ b/GRDB/Core/DatabaseValueConvertible.swift @@ -48,27 +48,28 @@ extension DatabaseValueConvertible { /// } /// } public final class DatabaseValueCursor : Cursor { - private let statement: SelectStatement + @usableFromInline let _statement: SelectStatement @usableFromInline let _sqliteStatement: SQLiteStatement @usableFromInline let _columnIndex: Int32 @usableFromInline var _done = false + @inlinable init(statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws { - self.statement = statement - self._sqliteStatement = statement.sqliteStatement + _statement = statement + _sqliteStatement = statement.sqliteStatement if let adapter = adapter { // adapter may redefine the index of the leftmost column - self._columnIndex = try Int32(adapter.baseColumnIndex(atIndex: 0, layout: statement)) + _columnIndex = try Int32(adapter.baseColumnIndex(atIndex: 0, layout: statement)) } else { - self._columnIndex = 0 + _columnIndex = 0 } - statement.reset(withArguments: arguments) + _statement.reset(withArguments: arguments) } deinit { // Statement reset fails when sqlite3_step has previously failed. // Just ignore reset error. - try? statement.reset() + try? _statement.reset() } /// :nodoc: @@ -86,19 +87,9 @@ public final class DatabaseValueCursor : Cursor case SQLITE_ROW: return Value.decode(from: _sqliteStatement, atUncheckedIndex: _columnIndex) case let code: - try handleErrorCode(code) + try _statement.didFail(withResultCode: code) } } - - @usableFromInline - func handleErrorCode(_ code: Int32) throws -> Never { - statement.database.selectStatementDidFail(statement) - throw DatabaseError( - resultCode: code, - message: statement.database.lastErrorMessage, - sql: statement.sql, - arguments: statement.arguments) - } } /// A cursor of optional database values extracted from a single column. @@ -111,27 +102,28 @@ public final class DatabaseValueCursor : Cursor /// } /// } public final class NullableDatabaseValueCursor : Cursor { - private let statement: SelectStatement + @usableFromInline let _statement: SelectStatement @usableFromInline let _sqliteStatement: SQLiteStatement @usableFromInline let _columnIndex: Int32 @usableFromInline var _done = false + @inlinable init(statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws { - self.statement = statement - self._sqliteStatement = statement.sqliteStatement + _statement = statement + _sqliteStatement = statement.sqliteStatement if let adapter = adapter { // adapter may redefine the index of the leftmost column - self._columnIndex = try Int32(adapter.baseColumnIndex(atIndex: 0, layout: statement)) + _columnIndex = try Int32(adapter.baseColumnIndex(atIndex: 0, layout: statement)) } else { - self._columnIndex = 0 + _columnIndex = 0 } - statement.reset(withArguments: arguments) + _statement.reset(withArguments: arguments) } deinit { // Statement reset fails when sqlite3_step has previously failed. // Just ignore reset error. - try? statement.reset() + try? _statement.reset() } /// :nodoc: @@ -149,19 +141,9 @@ public final class NullableDatabaseValueCursor case SQLITE_ROW: return Value.decodeIfPresent(from: _sqliteStatement, atUncheckedIndex: _columnIndex) case let code: - try handleErrorCode(code) + try _statement.didFail(withResultCode: code) } } - - @usableFromInline - func handleErrorCode(_ code: Int32) throws -> Never { - statement.database.selectStatementDidFail(statement) - throw DatabaseError( - resultCode: code, - message: statement.database.lastErrorMessage, - sql: statement.sql, - arguments: statement.arguments) - } } /// DatabaseValueConvertible comes with built-in methods that allow to fetch @@ -200,6 +182,7 @@ extension DatabaseValueConvertible { /// - adapter: Optional RowAdapter /// - returns: A cursor over fetched values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchCursor(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> DatabaseValueCursor { return try DatabaseValueCursor(statement: statement, arguments: arguments, adapter: adapter) } @@ -215,6 +198,7 @@ extension DatabaseValueConvertible { /// - adapter: Optional RowAdapter /// - returns: An array. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchAll(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> [Self] { return try Array(fetchCursor(statement, arguments: arguments, adapter: adapter)) } @@ -233,6 +217,7 @@ extension DatabaseValueConvertible { /// - adapter: Optional RowAdapter /// - returns: An optional value. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchOne(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> Self? { // fetchOne returns nil if there is no row, or if there is a row with a null value let cursor = try NullableDatabaseValueCursor(statement: statement, arguments: arguments, adapter: adapter) @@ -263,6 +248,7 @@ extension DatabaseValueConvertible { /// - adapter: Optional RowAdapter /// - returns: A cursor over fetched values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchCursor(_ db: Database, sql: String, arguments: StatementArguments = StatementArguments(), adapter: RowAdapter? = nil) throws -> DatabaseValueCursor { return try fetchCursor(db, SQLRequest(sql: sql, arguments: arguments, adapter: adapter)) } @@ -278,6 +264,7 @@ extension DatabaseValueConvertible { /// - adapter: Optional RowAdapter /// - returns: An array. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchAll(_ db: Database, sql: String, arguments: StatementArguments = StatementArguments(), adapter: RowAdapter? = nil) throws -> [Self] { return try fetchAll(db, SQLRequest(sql: sql, arguments: arguments, adapter: adapter)) } @@ -296,6 +283,7 @@ extension DatabaseValueConvertible { /// - adapter: Optional RowAdapter /// - returns: An optional value. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchOne(_ db: Database, sql: String, arguments: StatementArguments = StatementArguments(), adapter: RowAdapter? = nil) throws -> Self? { return try fetchOne(db, SQLRequest(sql: sql, arguments: arguments, adapter: adapter)) } @@ -323,6 +311,7 @@ extension DatabaseValueConvertible { /// - request: A FetchRequest. /// - returns: A cursor over fetched values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchCursor(_ db: Database, _ request: R) throws -> DatabaseValueCursor { let (statement, adapter) = try request.prepare(db) return try fetchCursor(statement, adapter: adapter) @@ -338,6 +327,7 @@ extension DatabaseValueConvertible { /// - request: A FetchRequest. /// - returns: An array. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchAll(_ db: Database, _ request: R) throws -> [Self] { let (statement, adapter) = try request.prepare(db) return try fetchAll(statement, adapter: adapter) @@ -356,6 +346,7 @@ extension DatabaseValueConvertible { /// - request: A FetchRequest. /// - returns: An optional value. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchOne(_ db: Database, _ request: R) throws -> Self? { let (statement, adapter) = try request.prepare(db) return try fetchOne(statement, adapter: adapter) @@ -382,6 +373,7 @@ extension FetchRequest where RowDecoder: DatabaseValueConvertible { /// - parameter db: A database connection. /// - returns: A cursor over fetched values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public func fetchCursor(_ db: Database) throws -> DatabaseValueCursor { return try RowDecoder.fetchCursor(db, self) } @@ -394,6 +386,7 @@ extension FetchRequest where RowDecoder: DatabaseValueConvertible { /// - parameter db: A database connection. /// - returns: An array of values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public func fetchAll(_ db: Database) throws -> [RowDecoder] { return try RowDecoder.fetchAll(db, self) } @@ -409,6 +402,7 @@ extension FetchRequest where RowDecoder: DatabaseValueConvertible { /// - parameter db: A database connection. /// - returns: An optional value. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public func fetchOne(_ db: Database) throws -> RowDecoder? { return try RowDecoder.fetchOne(db, self) } @@ -448,6 +442,7 @@ extension Optional where Wrapped: DatabaseValueConvertible { /// - adapter: Optional RowAdapter /// - returns: A cursor over fetched optional values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchCursor(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> NullableDatabaseValueCursor { return try NullableDatabaseValueCursor(statement: statement, arguments: arguments, adapter: adapter) } @@ -463,6 +458,7 @@ extension Optional where Wrapped: DatabaseValueConvertible { /// - adapter: Optional RowAdapter /// - returns: An array of optional values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchAll(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> [Wrapped?] { return try Array(fetchCursor(statement, arguments: arguments, adapter: adapter)) } @@ -491,6 +487,7 @@ extension Optional where Wrapped: DatabaseValueConvertible { /// - adapter: Optional RowAdapter /// - returns: A cursor over fetched optional values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchCursor(_ db: Database, sql: String, arguments: StatementArguments = StatementArguments(), adapter: RowAdapter? = nil) throws -> NullableDatabaseValueCursor { return try fetchCursor(db, SQLRequest(sql: sql, arguments: arguments, adapter: adapter)) } @@ -506,6 +503,7 @@ extension Optional where Wrapped: DatabaseValueConvertible { /// - adapter: Optional RowAdapter /// - returns: An array of optional values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchAll(_ db: Database, sql: String, arguments: StatementArguments = StatementArguments(), adapter: RowAdapter? = nil) throws -> [Wrapped?] { return try fetchAll(db, SQLRequest(sql: sql, arguments: arguments, adapter: adapter)) } @@ -533,6 +531,7 @@ extension Optional where Wrapped: DatabaseValueConvertible { /// - request: A FetchRequest. /// - returns: A cursor over fetched optional values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchCursor(_ db: Database, _ request: R) throws -> NullableDatabaseValueCursor { let (statement, adapter) = try request.prepare(db) return try fetchCursor(statement, adapter: adapter) @@ -548,6 +547,7 @@ extension Optional where Wrapped: DatabaseValueConvertible { /// - request: A FetchRequest. /// - returns: An array of optional values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchAll(_ db: Database, _ request: R) throws -> [Wrapped?] { let (statement, adapter) = try request.prepare(db) return try fetchAll(statement, adapter: adapter) @@ -574,6 +574,7 @@ extension FetchRequest where RowDecoder: _OptionalProtocol, RowDecoder._Wrapped: /// - parameter db: A database connection. /// - returns: A cursor over fetched values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public func fetchCursor(_ db: Database) throws -> NullableDatabaseValueCursor { return try Optional.fetchCursor(db, self) } @@ -586,6 +587,7 @@ extension FetchRequest where RowDecoder: _OptionalProtocol, RowDecoder._Wrapped: /// - parameter db: A database connection. /// - returns: An array of values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public func fetchAll(_ db: Database) throws -> [RowDecoder._Wrapped?] { return try Optional.fetchAll(db, self) } diff --git a/GRDB/Core/Row.swift b/GRDB/Core/Row.swift index 454cdf7bc..eb33c8608 100644 --- a/GRDB/Core/Row.swift +++ b/GRDB/Core/Row.swift @@ -86,6 +86,7 @@ public final class Row : Equatable, Hashable, RandomAccessCollection, Expressibl /// The row is implemented on top of StatementRowImpl, which grants *direct* /// access to the SQLite statement. Iteration of the statement does modify /// the row. + @usableFromInline init(statement: SelectStatement) { let statementRef = Unmanaged.passRetained(statement) // released in deinit self.statementRef = statementRef @@ -617,6 +618,7 @@ public final class RowCursor : Cursor { @usableFromInline let _row: Row // Reused for performance @usableFromInline var _done = false + @inlinable init(statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws { self.statement = statement self._row = try Row(statement: statement).adapted(with: adapter, layout: statement) @@ -645,15 +647,9 @@ public final class RowCursor : Cursor { case SQLITE_ROW: return _row case let code: - try handleErrorCode(code) + try statement.didFail(withResultCode: code) } } - - @usableFromInline - func handleErrorCode(_ code: Int32) throws -> Never { - statement.database.selectStatementDidFail(statement) - throw DatabaseError(resultCode: code, message: statement.database.lastErrorMessage, sql: statement.sql, arguments: statement.arguments) - } } extension Row { @@ -689,6 +685,7 @@ extension Row { /// - adapter: Optional RowAdapter /// - returns: A cursor over fetched rows. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchCursor(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> RowCursor { return try RowCursor(statement: statement, arguments: arguments, adapter: adapter) } @@ -704,6 +701,7 @@ extension Row { /// - adapter: Optional RowAdapter /// - returns: An array of rows. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchAll(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> [Row] { // The cursor reuses a single mutable row. Return immutable copies. return try Array(fetchCursor(statement, arguments: arguments, adapter: adapter).map { $0.copy() }) @@ -720,6 +718,7 @@ extension Row { /// - adapter: Optional RowAdapter /// - returns: An optional row. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchOne(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> Row? { let cursor = try fetchCursor(statement, arguments: arguments, adapter: adapter) // Keep cursor alive until we can copy the fetched row @@ -761,6 +760,7 @@ extension Row { /// - adapter: Optional RowAdapter /// - returns: A cursor over fetched rows. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchCursor(_ db: Database, sql: String, arguments: StatementArguments = StatementArguments(), adapter: RowAdapter? = nil) throws -> RowCursor { return try fetchCursor(db, SQLRequest(sql: sql, arguments: arguments, adapter: adapter)) } @@ -780,6 +780,7 @@ extension Row { /// - adapter: Optional RowAdapter /// - returns: An array of rows. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchAll(_ db: Database, sql: String, arguments: StatementArguments = StatementArguments(), adapter: RowAdapter? = nil) throws -> [Row] { return try fetchAll(db, SQLRequest(sql: sql, arguments: arguments, adapter: adapter)) } @@ -799,6 +800,7 @@ extension Row { /// - adapter: Optional RowAdapter /// - returns: An optional row. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchOne(_ db: Database, sql: String, arguments: StatementArguments = StatementArguments(), adapter: RowAdapter? = nil) throws -> Row? { return try fetchOne(db, SQLRequest(sql: sql, arguments: arguments, adapter: adapter)) } @@ -835,6 +837,7 @@ extension Row { /// - request: A FetchRequest. /// - returns: A cursor over fetched rows. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchCursor(_ db: Database, _ request: R) throws -> RowCursor { let (statement, adapter) = try request.prepare(db) return try fetchCursor(statement, adapter: adapter) @@ -850,6 +853,7 @@ extension Row { /// - 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: R) throws -> [Row] { let (statement, adapter) = try request.prepare(db) return try fetchAll(statement, adapter: adapter) @@ -865,6 +869,7 @@ extension Row { /// - 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: R) throws -> Row? { let (statement, adapter) = try request.prepare(db) return try fetchOne(statement, adapter: adapter) @@ -900,6 +905,7 @@ extension FetchRequest where RowDecoder: Row { /// - parameter db: A database connection. /// - returns: A cursor over fetched rows. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public func fetchCursor(_ db: Database) throws -> RowCursor { return try Row.fetchCursor(db, self) } @@ -912,6 +918,7 @@ extension FetchRequest where RowDecoder: Row { /// - 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) } @@ -924,6 +931,7 @@ extension FetchRequest where RowDecoder: Row { /// - parameter db: A database connection. /// - returns: A,n optional rows. /// - 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/GRDB/Core/RowAdapter.swift b/GRDB/Core/RowAdapter.swift index 905666176..fe80eeb2d 100644 --- a/GRDB/Core/RowAdapter.swift +++ b/GRDB/Core/RowAdapter.swift @@ -246,6 +246,7 @@ extension RowAdapter { } extension RowAdapter { + @usableFromInline func baseColumnIndex(atIndex index: Int, layout: RowLayout) throws -> Int { return try layoutedAdapter(from: layout).mapping.baseColumnIndex(atMappingIndex: index) } @@ -460,6 +461,7 @@ extension Row { } /// Returns self if adapter is nil + @usableFromInline func adapted(with adapter: RowAdapter?, layout: RowLayout) throws -> Row { guard let adapter = adapter else { return self diff --git a/GRDB/Core/Statement.swift b/GRDB/Core/Statement.swift index 1ad33ca82..2d9d71a23 100644 --- a/GRDB/Core/Statement.swift +++ b/GRDB/Core/Statement.swift @@ -356,10 +356,23 @@ public final class SelectStatement : Statement { } /// Utility function for cursors + @usableFromInline func reset(withArguments arguments: StatementArguments? = nil) { prepare(withArguments: arguments) try! reset() } + + /// Utility function for cursors + @usableFromInline + func didFail(withResultCode resultCode: Int32) throws -> Never { + database.selectStatementDidFail(self) + throw DatabaseError( + resultCode: resultCode, + message: database.lastErrorMessage, + sql: sql, + arguments: arguments) + + } } /// A cursor that iterates a database statement without producing any value. @@ -373,21 +386,22 @@ public final class SelectStatement : Statement { /// try cursor.next() /// } final class StatementCursor: Cursor { - public let statement: SelectStatement + @usableFromInline let _statement: SelectStatement @usableFromInline let _sqliteStatement: SQLiteStatement @usableFromInline var _done = false - // Use SelectStatement.cursor() instead - fileprivate init(statement: SelectStatement, arguments: StatementArguments? = nil) { - self.statement = statement - self._sqliteStatement = statement.sqliteStatement - statement.reset(withArguments: arguments) + // Use SelectStatement.makeCursor() instead + @inlinable + init(statement: SelectStatement, arguments: StatementArguments? = nil) { + _statement = statement + _sqliteStatement = statement.sqliteStatement + _statement.reset(withArguments: arguments) } deinit { // Statement reset fails when sqlite3_step has previously failed. // Just ignore reset error. - try? statement.reset() + try? _statement.reset() } /// :nodoc: @@ -405,15 +419,9 @@ final class StatementCursor: Cursor { case SQLITE_ROW: return .some(()) case let code: - try handleErrorCode(code) + try _statement.didFail(withResultCode: code) } } - - @usableFromInline - func handleErrorCode(_ code: Int32) throws -> Never { - statement.database.selectStatementDidFail(statement) - throw DatabaseError(resultCode: code, message: statement.database.lastErrorMessage, sql: statement.sql, arguments: statement.arguments) - } } diff --git a/GRDB/Core/StatementColumnConvertible.swift b/GRDB/Core/StatementColumnConvertible.swift index 9ebda18fa..2ee10eae0 100644 --- a/GRDB/Core/StatementColumnConvertible.swift +++ b/GRDB/Core/StatementColumnConvertible.swift @@ -60,27 +60,28 @@ public protocol StatementColumnConvertible { /// } /// } public final class FastDatabaseValueCursor : Cursor { - private let statement: SelectStatement + @usableFromInline let _statement: SelectStatement @usableFromInline let _columnIndex: Int32 @usableFromInline let _sqliteStatement: SQLiteStatement @usableFromInline var _done = false + @inlinable init(statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws { - self.statement = statement - self._sqliteStatement = statement.sqliteStatement + _statement = statement + _sqliteStatement = statement.sqliteStatement if let adapter = adapter { // adapter may redefine the index of the leftmost column - self._columnIndex = try Int32(adapter.baseColumnIndex(atIndex: 0, layout: statement)) + _columnIndex = try Int32(adapter.baseColumnIndex(atIndex: 0, layout: statement)) } else { - self._columnIndex = 0 + _columnIndex = 0 } - statement.reset(withArguments: arguments) + _statement.reset(withArguments: arguments) } deinit { // Statement reset fails when sqlite3_step has previously failed. // Just ignore reset error. - try? statement.reset() + try? _statement.reset() } /// :nodoc: @@ -98,15 +99,9 @@ public final class FastDatabaseValueCursor Never { - statement.database.selectStatementDidFail(statement) - throw DatabaseError(resultCode: code, message: statement.database.lastErrorMessage, sql: statement.sql, arguments: statement.arguments) - } } /// A cursor of optional database values extracted from a single column. @@ -119,27 +114,28 @@ public final class FastDatabaseValueCursor : Cursor { - private let statement: SelectStatement + @usableFromInline let _statement: SelectStatement @usableFromInline let _columnIndex: Int32 @usableFromInline let _sqliteStatement: SQLiteStatement @usableFromInline var _done = false + @inlinable init(statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws { - self.statement = statement - self._sqliteStatement = statement.sqliteStatement + _statement = statement + _sqliteStatement = statement.sqliteStatement if let adapter = adapter { // adapter may redefine the index of the leftmost column - self._columnIndex = try Int32(adapter.baseColumnIndex(atIndex: 0, layout: statement)) + _columnIndex = try Int32(adapter.baseColumnIndex(atIndex: 0, layout: statement)) } else { - self._columnIndex = 0 + _columnIndex = 0 } - statement.reset(withArguments: arguments) + _statement.reset(withArguments: arguments) } deinit { // Statement reset fails when sqlite3_step has previously failed. // Just ignore reset error. - try? statement.reset() + try? _statement.reset() } /// :nodoc: @@ -157,15 +153,9 @@ public final class FastNullableDatabaseValueCursor Never { - statement.database.selectStatementDidFail(statement) - throw DatabaseError(resultCode: code, message: statement.database.lastErrorMessage, sql: statement.sql, arguments: statement.arguments) - } } /// Types that adopt both DatabaseValueConvertible and @@ -197,6 +187,7 @@ extension DatabaseValueConvertible where Self: StatementColumnConvertible { /// - adapter: Optional RowAdapter /// - returns: A cursor over fetched values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchCursor(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> FastDatabaseValueCursor { return try FastDatabaseValueCursor(statement: statement, arguments: arguments, adapter: adapter) } @@ -212,6 +203,7 @@ extension DatabaseValueConvertible where Self: StatementColumnConvertible { /// - adapter: Optional RowAdapter /// - returns: An array of values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchAll(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> [Self] { return try Array(fetchCursor(statement, arguments: arguments, adapter: adapter)) } @@ -227,6 +219,7 @@ extension DatabaseValueConvertible where Self: StatementColumnConvertible { /// - adapter: Optional RowAdapter /// - returns: An optional value. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchOne(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> Self? { // fetchOne returns nil if there is no row, or if there is a row with a null value let cursor = try FastNullableDatabaseValueCursor(statement: statement, arguments: arguments, adapter: adapter) @@ -257,6 +250,7 @@ extension DatabaseValueConvertible where Self: StatementColumnConvertible { /// - adapter: Optional RowAdapter /// - returns: A cursor over fetched values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchCursor(_ db: Database, sql: String, arguments: StatementArguments = StatementArguments(), adapter: RowAdapter? = nil) throws -> FastDatabaseValueCursor { return try fetchCursor(db, SQLRequest(sql: sql, arguments: arguments, adapter: adapter)) } @@ -272,6 +266,7 @@ extension DatabaseValueConvertible where Self: StatementColumnConvertible { /// - adapter: Optional RowAdapter /// - returns: An array of values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchAll(_ db: Database, sql: String, arguments: StatementArguments = StatementArguments(), adapter: RowAdapter? = nil) throws -> [Self] { return try fetchAll(db, SQLRequest(sql: sql, arguments: arguments, adapter: adapter)) } @@ -287,6 +282,7 @@ extension DatabaseValueConvertible where Self: StatementColumnConvertible { /// - adapter: Optional RowAdapter /// - returns: An optional value. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchOne(_ db: Database, sql: String, arguments: StatementArguments = StatementArguments(), adapter: RowAdapter? = nil) throws -> Self? { return try fetchOne(db, SQLRequest(sql: sql, arguments: arguments, adapter: adapter)) } @@ -314,6 +310,7 @@ extension DatabaseValueConvertible where Self: StatementColumnConvertible { /// - request: A FetchRequest. /// - returns: A cursor over fetched values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchCursor(_ db: Database, _ request: R) throws -> FastDatabaseValueCursor { let (statement, adapter) = try request.prepare(db) return try fetchCursor(statement, adapter: adapter) @@ -329,6 +326,7 @@ extension DatabaseValueConvertible where Self: StatementColumnConvertible { /// - request: A FetchRequest. /// - returns: An array of values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchAll(_ db: Database, _ request: R) throws -> [Self] { let (statement, adapter) = try request.prepare(db) return try fetchAll(statement, adapter: adapter) @@ -344,6 +342,7 @@ extension DatabaseValueConvertible where Self: StatementColumnConvertible { /// - request: A FetchRequest. /// - returns: An optional value. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchOne(_ db: Database, _ request: R) throws -> Self? { let (statement, adapter) = try request.prepare(db) return try fetchOne(statement, adapter: adapter) @@ -370,6 +369,7 @@ extension FetchRequest where RowDecoder: DatabaseValueConvertible & StatementCol /// - parameter db: A database connection. /// - returns: A cursor over fetched values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public func fetchCursor(_ db: Database) throws -> FastDatabaseValueCursor { return try RowDecoder.fetchCursor(db, self) } @@ -382,6 +382,7 @@ extension FetchRequest where RowDecoder: DatabaseValueConvertible & StatementCol /// - parameter db: A database connection. /// - returns: An array of values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public func fetchAll(_ db: Database) throws -> [RowDecoder] { return try RowDecoder.fetchAll(db, self) } @@ -397,6 +398,7 @@ extension FetchRequest where RowDecoder: DatabaseValueConvertible & StatementCol /// - parameter db: A database connection. /// - returns: An optional value. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public func fetchOne(_ db: Database) throws -> RowDecoder? { return try RowDecoder.fetchOne(db, self) } @@ -436,6 +438,7 @@ extension Optional where Wrapped: DatabaseValueConvertible & StatementColumnConv /// - adapter: Optional RowAdapter /// - returns: A cursor over fetched optional values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchCursor(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> FastNullableDatabaseValueCursor { return try FastNullableDatabaseValueCursor(statement: statement, arguments: arguments, adapter: adapter) } @@ -451,6 +454,7 @@ extension Optional where Wrapped: DatabaseValueConvertible & StatementColumnConv /// - adapter: Optional RowAdapter /// - returns: An array of optional values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchAll(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> [Wrapped?] { return try Array(fetchCursor(statement, arguments: arguments, adapter: adapter)) } @@ -479,6 +483,7 @@ extension Optional where Wrapped: DatabaseValueConvertible & StatementColumnConv /// - adapter: Optional RowAdapter /// - returns: A cursor over fetched optional values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchCursor(_ db: Database, sql: String, arguments: StatementArguments = StatementArguments(), adapter: RowAdapter? = nil) throws -> FastNullableDatabaseValueCursor { return try fetchCursor(db, SQLRequest(sql: sql, arguments: arguments, adapter: adapter)) } @@ -494,6 +499,7 @@ extension Optional where Wrapped: DatabaseValueConvertible & StatementColumnConv /// - adapter: Optional RowAdapter /// - returns: An array of optional values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchAll(_ db: Database, sql: String, arguments: StatementArguments = StatementArguments(), adapter: RowAdapter? = nil) throws -> [Wrapped?] { return try fetchAll(db, SQLRequest(sql: sql, arguments: arguments, adapter: adapter)) } @@ -521,6 +527,7 @@ extension Optional where Wrapped: DatabaseValueConvertible & StatementColumnConv /// - request: A FetchRequest. /// - returns: A cursor over fetched optional values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchCursor(_ db: Database, _ request: R) throws -> FastNullableDatabaseValueCursor { let (statement, adapter) = try request.prepare(db) return try fetchCursor(statement, adapter: adapter) @@ -536,6 +543,7 @@ extension Optional where Wrapped: DatabaseValueConvertible & StatementColumnConv /// - request: A FetchRequest. /// - returns: An array of optional values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchAll(_ db: Database, _ request: R) throws -> [Wrapped?] { let (statement, adapter) = try request.prepare(db) return try fetchAll(statement, adapter: adapter) @@ -562,6 +570,7 @@ extension FetchRequest where RowDecoder: _OptionalProtocol, RowDecoder._Wrapped: /// - parameter db: A database connection. /// - returns: A cursor over fetched values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public func fetchCursor(_ db: Database) throws -> FastNullableDatabaseValueCursor { return try Optional.fetchCursor(db, self) } @@ -574,6 +583,7 @@ extension FetchRequest where RowDecoder: _OptionalProtocol, RowDecoder._Wrapped: /// - parameter db: A database connection. /// - returns: An array of values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public func fetchAll(_ db: Database) throws -> [RowDecoder._Wrapped?] { return try Optional.fetchAll(db, self) } diff --git a/GRDB/Record/FetchableRecord+TableRecord.swift b/GRDB/Record/FetchableRecord+TableRecord.swift index 937736486..91fad6b2c 100644 --- a/GRDB/Record/FetchableRecord+TableRecord.swift +++ b/GRDB/Record/FetchableRecord+TableRecord.swift @@ -24,6 +24,7 @@ extension FetchableRecord where Self: TableRecord { /// - parameter db: A database connection. /// - returns: A cursor over fetched records. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchCursor(_ db: Database) throws -> RecordCursor { return try all().fetchCursor(db) } @@ -39,6 +40,7 @@ extension FetchableRecord where Self: TableRecord { /// /// - parameter db: A database connection. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchAll(_ db: Database) throws -> [Self] { return try all().fetchAll(db) } @@ -54,6 +56,7 @@ extension FetchableRecord where Self: TableRecord { /// /// - parameter db: A database connection. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchOne(_ db: Database) throws -> Self? { return try all().fetchOne(db) } @@ -77,6 +80,7 @@ extension FetchableRecord where Self: TableRecord { /// - keys: A sequence of primary keys. /// - returns: A cursor over fetched records. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchCursor(_ db: Database, keys: Sequence) throws -> RecordCursor where Sequence.Element: DatabaseValueConvertible { return try filter(keys: keys).fetchCursor(db) } @@ -92,6 +96,7 @@ extension FetchableRecord where Self: TableRecord { /// - keys: A sequence of primary keys. /// - returns: An array of records. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchAll(_ db: Database, keys: Sequence) throws -> [Self] where Sequence.Element: DatabaseValueConvertible { let keys = Array(keys) if keys.isEmpty { @@ -110,6 +115,7 @@ extension FetchableRecord where Self: TableRecord { /// - key: A primary key value. /// - returns: An optional record. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchOne(_ db: Database, key: PrimaryKeyType?) throws -> Self? { guard let key = key else { // Avoid hitting the database @@ -138,6 +144,7 @@ extension FetchableRecord where Self: TableRecord { /// - keys: An array of key dictionaries. /// - returns: A cursor over fetched records. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchCursor(_ db: Database, keys: [[String: DatabaseValueConvertible?]]) throws -> RecordCursor { return try filter(keys: keys).fetchCursor(db) } @@ -154,6 +161,7 @@ extension FetchableRecord where Self: TableRecord { /// - keys: An array of key dictionaries. /// - returns: An array of records. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchAll(_ db: Database, keys: [[String: DatabaseValueConvertible?]]) throws -> [Self] { if keys.isEmpty { // Avoid hitting the database @@ -172,6 +180,7 @@ extension FetchableRecord where Self: TableRecord { /// - key: A dictionary of values. /// - returns: An optional record. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchOne(_ db: Database, key: [String: DatabaseValueConvertible?]?) throws -> Self? { guard let key = key else { // Avoid hitting the database diff --git a/GRDB/Record/FetchableRecord.swift b/GRDB/Record/FetchableRecord.swift index 397e1d8be..a01e63bf3 100644 --- a/GRDB/Record/FetchableRecord.swift +++ b/GRDB/Record/FetchableRecord.swift @@ -158,6 +158,7 @@ extension FetchableRecord { /// - adapter: Optional RowAdapter /// - returns: A cursor over fetched records. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchCursor(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> RecordCursor { return try RecordCursor(statement: statement, arguments: arguments, adapter: adapter) } @@ -173,6 +174,7 @@ extension FetchableRecord { /// - adapter: Optional RowAdapter /// - returns: An array of records. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchAll(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> [Self] { return try Array(fetchCursor(statement, arguments: arguments, adapter: adapter)) } @@ -188,6 +190,7 @@ extension FetchableRecord { /// - adapter: Optional RowAdapter /// - returns: An optional record. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchOne(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> Self? { return try fetchCursor(statement, arguments: arguments, adapter: adapter).next() } @@ -216,6 +219,7 @@ extension FetchableRecord { /// - adapter: Optional RowAdapter /// - returns: A cursor over fetched records. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchCursor(_ db: Database, sql: String, arguments: StatementArguments = StatementArguments(), adapter: RowAdapter? = nil) throws -> RecordCursor { return try fetchCursor(db, SQLRequest(sql: sql, arguments: arguments, adapter: adapter)) } @@ -231,6 +235,7 @@ extension FetchableRecord { /// - adapter: Optional RowAdapter /// - returns: An array of records. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchAll(_ db: Database, sql: String, arguments: StatementArguments = StatementArguments(), adapter: RowAdapter? = nil) throws -> [Self] { return try fetchAll(db, SQLRequest(sql: sql, arguments: arguments, adapter: adapter)) } @@ -246,6 +251,7 @@ extension FetchableRecord { /// - adapter: Optional RowAdapter /// - returns: An optional record. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchOne(_ db: Database, sql: String, arguments: StatementArguments = StatementArguments(), adapter: RowAdapter? = nil) throws -> Self? { return try fetchOne(db, SQLRequest(sql: sql, arguments: arguments, adapter: adapter)) } @@ -273,6 +279,7 @@ extension FetchableRecord { /// - sql: a FetchRequest. /// - returns: A cursor over fetched records. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public static func fetchCursor(_ db: Database, _ request: R) throws -> RecordCursor { let (statement, adapter) = try request.prepare(db) return try fetchCursor(statement, adapter: adapter) @@ -288,6 +295,7 @@ extension FetchableRecord { /// - 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: R) throws -> [Self] { let (statement, adapter) = try request.prepare(db) return try fetchAll(statement, adapter: adapter) @@ -303,6 +311,7 @@ extension FetchableRecord { /// - 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: R) throws -> Self? { let (statement, adapter) = try request.prepare(db) return try fetchOne(statement, adapter: adapter) @@ -331,6 +340,7 @@ extension FetchRequest where RowDecoder: FetchableRecord { /// - parameter db: A database connection. /// - returns: A cursor over fetched records. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + @inlinable public func fetchCursor(_ db: Database) throws -> RecordCursor { return try RowDecoder.fetchCursor(db, self) } @@ -343,6 +353,7 @@ extension FetchRequest where RowDecoder: FetchableRecord { /// - 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) } @@ -355,6 +366,7 @@ extension FetchRequest where RowDecoder: FetchableRecord { /// - 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) } @@ -369,22 +381,23 @@ extension FetchRequest where RowDecoder: FetchableRecord { /// let players: RecordCursor = try Player.fetchCursor(db, sql: "SELECT * FROM player") /// } public final class RecordCursor : Cursor { - private let statement: SelectStatement + @usableFromInline let _statement: SelectStatement @usableFromInline let _row: Row // Reused for performance @usableFromInline let _sqliteStatement: SQLiteStatement @usableFromInline var _done = false + @inlinable init(statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws { - self.statement = statement - self._row = try Row(statement: statement).adapted(with: adapter, layout: statement) - self._sqliteStatement = statement.sqliteStatement - statement.reset(withArguments: arguments) + _statement = statement + _row = try Row(statement: statement).adapted(with: adapter, layout: statement) + _sqliteStatement = statement.sqliteStatement + _statement.reset(withArguments: arguments) } deinit { // Statement reset fails when sqlite3_step has previously failed. // Just ignore reset error. - try? statement.reset() + try? _statement.reset() } /// :nodoc: @@ -402,15 +415,9 @@ public final class RecordCursor : Cursor { case SQLITE_ROW: return Record(row: _row) case let code: - try handleErrorCode(code) + try _statement.didFail(withResultCode: code) } } - - @usableFromInline - func handleErrorCode(_ code: Int32) throws -> Never { - statement.database.selectStatementDidFail(statement) - throw DatabaseError(resultCode: code, message: statement.database.lastErrorMessage, sql: statement.sql, arguments: statement.arguments) - } } // MARK: - DatabaseDateDecodingStrategy diff --git a/Tests/GRDBTests/SelectStatementTests.swift b/Tests/GRDBTests/SelectStatementTests.swift index cf9178021..bc0bd854c 100644 --- a/Tests/GRDBTests/SelectStatementTests.swift +++ b/Tests/GRDBTests/SelectStatementTests.swift @@ -40,7 +40,7 @@ class SelectStatementTests : GRDBTestCase { let cursor = statement.makeCursor() // Check that StatementCursor gives access to the raw SQLite API - XCTAssertEqual(String(cString: sqlite3_column_name(cursor.statement.sqliteStatement, 0)), "firstName") + XCTAssertEqual(String(cString: sqlite3_column_name(cursor._statement.sqliteStatement, 0)), "firstName") XCTAssertFalse(try cursor.next() == nil) XCTAssertFalse(try cursor.next() == nil) @@ -55,7 +55,7 @@ class SelectStatementTests : GRDBTestCase { dbQueue.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError }) try dbQueue.inDatabase { db in func test(_ cursor: StatementCursor) throws { - let sql = cursor.statement.sql + let sql = cursor._statement.sql do { _ = try cursor.next() XCTFail()