/// The `StatementColumnConvertible` protocol grants access to the low-level C /// interface that extracts values from query results: /// . It can bring performance /// improvements. /// /// To use it, have a value type adopt both `StatementColumnConvertible` and /// `DatabaseValueConvertible`. GRDB will then automatically apply the /// optimization whenever direct access to SQLite is possible: /// /// let rows = Row.fetchCursor(db, sql: "SELECT ...") /// while let row = try rows.next() { /// let int: Int = row[0] // there /// } /// let ints = Int.fetchAll(db, sql: "SELECT ...") // there /// struct Player { /// init(row: Row) { /// name = row["name"] // there /// score = row["score"] // there /// } /// } public protocol StatementColumnConvertible { /// Creates a value from a raw SQLite statement pointer, if possible. /// /// This method can be called with a NULL database value. /// /// - warning: Do not customize the default implementation. /// /// - parameters: /// - sqliteStatement: A pointer to an SQLite statement. /// - index: The column index. /// - returns: A decoded value, or, if decoding is impossible, nil. static func fromStatement( _ sqliteStatement: SQLiteStatement, atUncheckedIndex index: CInt) -> Self? /// Creates a value from a raw SQLite statement pointer, if possible. /// /// For example, here is the how Int64 adopts StatementColumnConvertible: /// /// extension Int64: StatementColumnConvertible { /// init?(sqliteStatement: SQLiteStatement, index: CInt) { /// self = sqlite3_column_int64(sqliteStatement, index) /// } /// } /// /// See for more information. /// /// - precondition: This initializer must not be called with NULL database /// values. /// - parameters: /// - sqliteStatement: A pointer to an SQLite statement. /// - index: The column index. /// - returns: A decoded value, or, if decoding is impossible, nil. init?(sqliteStatement: SQLiteStatement, index: CInt) } extension StatementColumnConvertible { // `Optional` overrides this default behavior. /// Default implementation fails on decoding NULL. @inline(__always) @inlinable public static func fromStatement(_ sqliteStatement: SQLiteStatement, atUncheckedIndex index: CInt) -> Self? { if sqlite3_column_type(sqliteStatement, index) == SQLITE_NULL { return nil } return self.init(sqliteStatement: sqliteStatement, index: index) } } // MARK: - Conversions extension DatabaseValueConvertible where Self: StatementColumnConvertible { @usableFromInline /* private */ static func _valueMismatch( fromStatement sqliteStatement: SQLiteStatement, atUncheckedIndex index: CInt, context: @autoclosure () -> RowDecodingContext) throws -> Never { throw RowDecodingError.valueMismatch( Self.self, sqliteStatement: sqliteStatement, index: index, context: context()) } @inline(__always) @inlinable static func fastDecode( fromRow row: Row, atUncheckedIndex index: Int) throws -> Self { if let sqliteStatement = row.sqliteStatement { return try fastDecode( fromStatement: sqliteStatement, atUncheckedIndex: CInt(index), context: RowDecodingContext(row: row, key: .columnIndex(index))) } // Support for fast decoding from adapted rows return try row.fastDecode(Self.self, atUncheckedIndex: index) } @inline(__always) @inlinable static func fastDecode( fromStatement sqliteStatement: SQLiteStatement, atUncheckedIndex index: CInt, context: @autoclosure () -> RowDecodingContext) throws -> Self { if let value = fromStatement(sqliteStatement, atUncheckedIndex: index) { return value } else { try _valueMismatch(fromStatement: sqliteStatement, atUncheckedIndex: index, context: context()) } } // Support for Decodable @inline(__always) @inlinable static func fastDecodeIfPresent( fromRow row: Row, atUncheckedIndex index: Int) throws -> Self? { try Optional.fastDecode(fromRow: row, atUncheckedIndex: index) } } // MARK: - Cursors /// A cursor of database values extracted from a single column. /// For example: /// /// try dbQueue.read { db in /// let names: ColumnCursor = try String.fetchCursor(db, sql: "SELECT name FROM player") /// while let name = names.next() { // String /// print(name) /// } /// } public final class FastDatabaseValueCursor: DatabaseCursor where Value: DatabaseValueConvertible & StatementColumnConvertible { public typealias Element = Value /// :nodoc: public let _statement: Statement /// :nodoc: public var _isDone = false @usableFromInline let columnIndex: CInt init(statement: Statement, arguments: StatementArguments? = nil, adapter: (any RowAdapter)? = nil) throws { self._statement = statement if let adapter = adapter { // adapter may redefine the index of the leftmost column columnIndex = try CInt(adapter.baseColumnIndex(atIndex: 0, layout: statement)) } else { columnIndex = 0 } // Assume cursor is created for immediate iteration: reset and set arguments try statement.reset(withArguments: arguments) } deinit { // Statement reset fails when sqlite3_step has previously failed. // Just ignore reset error. try? _statement.reset() } /// :nodoc: @inlinable public func _element(sqliteStatement: SQLiteStatement) throws -> Value { try Value.fastDecode( fromStatement: sqliteStatement, atUncheckedIndex: columnIndex, context: RowDecodingContext(statement: _statement, index: Int(columnIndex))) } } /// Types that adopt both DatabaseValueConvertible and /// StatementColumnConvertible can be efficiently initialized from /// database values. /// /// See DatabaseValueConvertible for more information. extension DatabaseValueConvertible where Self: StatementColumnConvertible { // MARK: Fetching From Prepared Statement /// Returns a cursor over values fetched from a prepared statement. /// /// let statement = try db.makeStatement(sql: "SELECT name FROM ...") /// let names = try String.fetchCursor(statement) // Cursor of String /// while let name = try names.next() { // String /// ... /// } /// /// If the database is modified during the cursor iteration, the remaining /// elements are undefined. /// /// The cursor must be iterated in a protected dispatch queue. /// /// - parameters: /// - statement: The statement to run. /// - arguments: Optional statement arguments. /// - adapter: Optional RowAdapter /// - returns: A cursor over fetched values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. public static func fetchCursor( _ statement: Statement, arguments: StatementArguments? = nil, adapter: (any RowAdapter)? = nil) throws -> FastDatabaseValueCursor { try FastDatabaseValueCursor(statement: statement, arguments: arguments, adapter: adapter) } /// Returns an array of values fetched from a prepared statement. /// /// let statement = try db.makeStatement(sql: "SELECT name FROM ...") /// let names = try String.fetchAll(statement) // [String] /// /// - parameters: /// - statement: The statement to run. /// - arguments: Optional statement arguments. /// - adapter: Optional RowAdapter /// - returns: An array of values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. public static func fetchAll( _ statement: Statement, arguments: StatementArguments? = nil, adapter: (any RowAdapter)? = nil) throws -> [Self] { try Array(fetchCursor(statement, arguments: arguments, adapter: adapter)) } /// Returns a single value fetched from a prepared statement. /// /// let statement = try db.makeStatement(sql: "SELECT name FROM ...") /// let name = try String.fetchOne(statement) // String? /// /// - parameters: /// - statement: The statement to run. /// - arguments: Optional statement arguments. /// - adapter: Optional RowAdapter /// - returns: An optional value. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. public static func fetchOne( _ statement: Statement, arguments: StatementArguments? = nil, adapter: (any RowAdapter)? = nil) throws -> Self? { // fetchOne handles both a missing row, and one row with a NULL value. let cursor = try FastDatabaseValueCursor( statement: statement, arguments: arguments, adapter: adapter) return try cursor.next() ?? nil } } extension DatabaseValueConvertible where Self: StatementColumnConvertible & Hashable { /// Returns a set of values fetched from a prepared statement. /// /// let statement = try db.makeStatement(sql: "SELECT name FROM ...") /// let names = try String.fetchSet(statement) // Set /// /// - parameters: /// - statement: The statement to run. /// - arguments: Optional statement arguments. /// - adapter: Optional RowAdapter /// - returns: A set of values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. public static func fetchSet( _ statement: Statement, arguments: StatementArguments? = nil, adapter: (any RowAdapter)? = nil) throws -> Set { try Set(fetchCursor(statement, arguments: arguments, adapter: adapter)) } } extension DatabaseValueConvertible where Self: StatementColumnConvertible { // MARK: Fetching From SQL /// Returns a cursor over values fetched from an SQL query. /// /// let names = try String.fetchCursor(db, sql: "SELECT name FROM ...") // Cursor of String /// while let name = try names.next() { // String /// ... /// } /// /// If the database is modified during the cursor iteration, the remaining /// elements are undefined. /// /// The cursor must be iterated in a protected dispatch queue. /// /// - parameters: /// - db: A database connection. /// - sql: An SQL query. /// - arguments: Statement arguments. /// - adapter: Optional RowAdapter /// - returns: A cursor over fetched values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. public static func fetchCursor( _ db: Database, sql: String, arguments: StatementArguments = StatementArguments(), adapter: (any RowAdapter)? = nil) throws -> FastDatabaseValueCursor { try fetchCursor(db, SQLRequest(sql: sql, arguments: arguments, adapter: adapter)) } /// Returns an array of values fetched from an SQL query. /// /// let names = try String.fetchAll(db, sql: "SELECT name FROM ...") // [String] /// /// - parameters: /// - db: A database connection. /// - sql: An SQL query. /// - arguments: Statement arguments. /// - adapter: Optional RowAdapter /// - returns: An array of values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. public static func fetchAll( _ db: Database, sql: String, arguments: StatementArguments = StatementArguments(), adapter: (any RowAdapter)? = nil) throws -> [Self] { try fetchAll(db, SQLRequest(sql: sql, arguments: arguments, adapter: adapter)) } /// Returns a single value fetched from an SQL query. /// /// let name = try String.fetchOne(db, sql: "SELECT name FROM ...") // String? /// /// - parameters: /// - db: A database connection. /// - sql: An SQL query. /// - arguments: Statement arguments. /// - adapter: Optional RowAdapter /// - returns: An optional value. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. public static func fetchOne( _ db: Database, sql: String, arguments: StatementArguments = StatementArguments(), adapter: (any RowAdapter)? = nil) throws -> Self? { try fetchOne(db, SQLRequest(sql: sql, arguments: arguments, adapter: adapter)) } } extension DatabaseValueConvertible where Self: StatementColumnConvertible & Hashable { /// Returns a set of values fetched from an SQL query. /// /// let names = try String.fetchSet(db, sql: "SELECT name FROM ...") // Set /// /// - parameters: /// - db: A database connection. /// - sql: An SQL query. /// - arguments: Statement arguments. /// - adapter: Optional RowAdapter /// - returns: A set of values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. public static func fetchSet( _ db: Database, sql: String, arguments: StatementArguments = StatementArguments(), adapter: (any RowAdapter)? = nil) throws -> Set { try fetchSet(db, SQLRequest(sql: sql, arguments: arguments, adapter: adapter)) } } extension DatabaseValueConvertible where Self: StatementColumnConvertible { // MARK: Fetching From FetchRequest /// Returns a cursor over values fetched from a fetch request. /// /// let request = Player.select(Column("name")) /// let names = try String.fetchCursor(db, request) // Cursor of String /// while let name = try names.next() { // String /// ... /// } /// /// If the database is modified during the cursor iteration, the remaining /// elements are undefined. /// /// The cursor must be iterated in a protected dispatch queue. /// /// - parameters: /// - db: A database connection. /// - request: A FetchRequest. /// - returns: A cursor over fetched values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. public static func fetchCursor(_ db: Database, _ request: R) throws -> FastDatabaseValueCursor { let request = try request.makePreparedRequest(db, forSingleResult: false) return try fetchCursor(request.statement, adapter: request.adapter) } /// Returns an array of values fetched from a fetch request. /// /// let request = Player.select(Column("name")) /// let names = try String.fetchAll(db, request) // [String] /// /// - parameters: /// - db: A database connection. /// - request: A FetchRequest. /// - returns: An array of values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. public static func fetchAll(_ db: Database, _ request: R) throws -> [Self] { let request = try request.makePreparedRequest(db, forSingleResult: false) return try fetchAll(request.statement, adapter: request.adapter) } /// Returns a single value fetched from a fetch request. /// /// let request = Player.filter(key: 1).select(Column("name")) /// let name = try String.fetchOne(db, request) // String? /// /// - parameters: /// - db: A database connection. /// - request: A FetchRequest. /// - returns: An optional value. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. public static func fetchOne(_ db: Database, _ request: R) throws -> Self? { let request = try request.makePreparedRequest(db, forSingleResult: true) return try fetchOne(request.statement, adapter: request.adapter) } } extension DatabaseValueConvertible where Self: StatementColumnConvertible & Hashable { /// Returns a set of values fetched from a fetch request. /// /// let request = Player.select(Column("name")) /// let names = try String.fetchSet(db, request) // Set /// /// - parameters: /// - db: A database connection. /// - request: A FetchRequest. /// - returns: A set of values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. public static func fetchSet(_ db: Database, _ request: R) throws -> Set { let request = try request.makePreparedRequest(db, forSingleResult: false) return try fetchSet(request.statement, adapter: request.adapter) } } extension FetchRequest where RowDecoder: DatabaseValueConvertible & StatementColumnConvertible { // MARK: Fetching Values /// A cursor over fetched values. /// /// let request: some FetchRequest = ... /// let strings = try request.fetchCursor(db) // Cursor of String /// while let string = try strings.next() { // String /// ... /// } /// /// If the database is modified during the cursor iteration, the remaining /// elements are undefined. /// /// The cursor must be iterated in a protected dispatch queue. /// /// - parameter db: A database connection. /// - returns: A cursor over fetched values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. public func fetchCursor(_ db: Database) throws -> FastDatabaseValueCursor { try RowDecoder.fetchCursor(db, self) } /// An array of fetched values. /// /// let request: some FetchRequest = ... /// let strings = try request.fetchAll(db) // [String] /// /// - parameter db: A database connection. /// - returns: An array of values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. public func fetchAll(_ db: Database) throws -> [RowDecoder] { try RowDecoder.fetchAll(db, self) } /// The first fetched value. /// /// The result is nil if the request returns no row, or if no value can be /// extracted from the first row. /// /// let request: some FetchRequest = ... /// let string = try request.fetchOne(db) // String? /// /// - parameter db: A database connection. /// - returns: An optional value. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. public func fetchOne(_ db: Database) throws -> RowDecoder? { try RowDecoder.fetchOne(db, self) } } extension FetchRequest where RowDecoder: DatabaseValueConvertible & StatementColumnConvertible & Hashable { /// A set of fetched values. /// /// let request: some FetchRequest = ... /// let strings = try request.fetchSet(db) // Set /// /// - parameter db: A database connection. /// - returns: An array of values. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. public func fetchSet(_ db: Database) throws -> Set { try RowDecoder.fetchSet(db, self) } }