FetchRequest

public protocol FetchRequest : DatabaseRegionConvertible, SQLSubqueryable

The protocol for all requests that fetch database rows, and tell how those rows should be interpreted.

struct Player: FetchableRecord { ... }
let request: ... // Some FetchRequest that fetches Player
try request.fetchCursor(db) // Cursor of Player
try request.fetchAll(db)    // [Player]
try request.fetchSet(db)    // Set<Player>
try request.fetchOne(db)    // Player?
try request.fetchCount(db)  // Int
  • The type that tells how fetched database rows should be interpreted.

    Declaration

    Swift

    associatedtype RowDecoder
  • Returns a PreparedRequest that is ready to be executed.

    Declaration

    Swift

    func makePreparedRequest(_ db: Database, forSingleResult singleResult: Bool) throws -> PreparedRequest

    Parameters

    db

    A database connection.

    singleResult

    A hint that a single result row will be consumed. Implementations can optionally use it to optimize the prepared statement, for example by adding a LIMIT 1 SQL clause.

    // Calls makePreparedRequest(db, forSingleResult: true) try request.fetchOne(db)

    // Calls makePreparedRequest(db, forSingleResult: false) try request.fetchAll(db)

    Return Value

    A prepared request.

  • Returns the number of rows fetched by the request.

    Declaration

    Swift

    func fetchCount(_ db: Database) throws -> Int

    Parameters

    db

    A database connection.

FetchRequest

AdaptedFetchRequest

Fetching Values

  • fetchCursor(_:) Extension method

    A cursor over fetched values.

    let request: ... // Some FetchRequest that fetches String
    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.

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public func fetchCursor(_ db: Database) throws -> DatabaseValueCursor<RowDecoder>

    Parameters

    db

    A database connection.

    Return Value

    A cursor over fetched values.

  • fetchAll(_:) Extension method

    An array of fetched values.

    let request: ... // Some FetchRequest that fetches String
    let strings = try request.fetchAll(db) // [String]
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public func fetchAll(_ db: Database) throws -> [RowDecoder]

    Parameters

    db

    A database connection.

    Return Value

    An array of values.

  • fetchOne(_:) Extension method

    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 that fetches String
    let string = try request.fetchOne(db) // String?
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public func fetchOne(_ db: Database) throws -> RowDecoder?

    Parameters

    db

    A database connection.

    Return Value

    An optional value.

Available where RowDecoder: DatabaseValueConvertible & Hashable

  • fetchSet(_:) Extension method

    A set of fetched values.

    let request: ... // Some FetchRequest that fetches String
    let strings = try request.fetchSet(db) // Set<String>
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public func fetchSet(_ db: Database) throws -> Set<RowDecoder>

    Parameters

    db

    A database connection.

    Return Value

    A set of values.

Fetching Optional values

  • fetchCursor(_:) Extension method

    A cursor over fetched optional values.

    let request: ... // Some FetchRequest that fetches Optional<String>
    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.

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public func fetchCursor(_ db: Database) throws -> NullableDatabaseValueCursor<RowDecoder.Wrapped>

    Parameters

    db

    A database connection.

    Return Value

    A cursor over fetched values.

  • fetchAll(_:) Extension method

    An array of fetched optional values.

    let request: ... // Some FetchRequest that fetches Optional<String>
    let strings = try request.fetchAll(db) // [String?]
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public func fetchAll(_ db: Database) throws -> [RowDecoder.Wrapped?]

    Parameters

    db

    A database connection.

    Return Value

    An array of values.

  • fetchOne(_:) Extension method

    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 that fetches String?
    let string = try request.fetchOne(db) // String?
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public func fetchOne(_ db: Database) throws -> RowDecoder.Wrapped?

    Parameters

    db

    A database connection.

    Return Value

    An optional value.

Available where RowDecoder: _OptionalProtocol, RowDecoder.Wrapped: DatabaseValueConvertible & Hashable

  • fetchSet(_:) Extension method

    A set of fetched optional values.

    let request: ... // Some FetchRequest that fetches Optional<String>
    let strings = try request.fetchSet(db) // Set<String?>
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public func fetchSet(_ db: Database) throws -> Set<RowDecoder.Wrapped?>

    Parameters

    db

    A database connection.

    Return Value

    A set of values.

Fetching Rows

  • fetchCursor(_:) Extension method

    A cursor over fetched rows.

    let request: ... // Some FetchRequest that fetches Row
    let rows = try request.fetchCursor(db) // RowCursor
    while let row = try rows.next() {  // Row
        let id: Int64 = row[0]
        let name: String = row[1]
    }
    

    Fetched rows are reused during the cursor iteration: don’t turn a row cursor into an array with Array(rows) or rows.filter { ... } since you would not get the distinct rows you expect. Use Row.fetchAll(...) instead.

    For the same reason, make sure you make a copy whenever you extract a row for later use: row.copy().

    If the database is modified during the cursor iteration, the remaining elements are undefined.

    The cursor must be iterated in a protected dispatch queue.

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public func fetchCursor(_ db: Database) throws -> RowCursor

    Parameters

    db

    A database connection.

    Return Value

    A cursor over fetched rows.

  • fetchAll(_:) Extension method

    An array of fetched rows.

    let request: ... // Some FetchRequest that fetches Row
    let rows = try request.fetchAll(db)
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public func fetchAll(_ db: Database) throws -> [Row]

    Parameters

    db

    A database connection.

    Return Value

    An array of fetched rows.

  • fetchSet(_:) Extension method

    A set of fetched rows.

    let request: ... // Some FetchRequest that fetches Row
    let rows = try request.fetchSet(db)
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public func fetchSet(_ db: Database) throws -> Set<Row>

    Parameters

    db

    A database connection.

    Return Value

    A set of fetched rows.

  • fetchOne(_:) Extension method

    The first fetched row.

    let request: ... // Some FetchRequest that fetches Row
    let row = try request.fetchOne(db)
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public func fetchOne(_ db: Database) throws -> Row?

    Parameters

    db

    A database connection.

    Return Value

    An optional row.

Fetching Values

  • fetchCursor(_:) Extension method

    A cursor over fetched values.

    let request: ... // Some FetchRequest that fetches String
    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.

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public func fetchCursor(_ db: Database) throws -> FastDatabaseValueCursor<RowDecoder>

    Parameters

    db

    A database connection.

    Return Value

    A cursor over fetched values.

  • fetchAll(_:) Extension method

    An array of fetched values.

    let request: ... // Some FetchRequest that fetches String
    let strings = try request.fetchAll(db) // [String]
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public func fetchAll(_ db: Database) throws -> [RowDecoder]

    Parameters

    db

    A database connection.

    Return Value

    An array of values.

  • fetchOne(_:) Extension method

    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 that fetches String
    let string = try request.fetchOne(db) // String?
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public func fetchOne(_ db: Database) throws -> RowDecoder?

    Parameters

    db

    A database connection.

    Return Value

    An optional value.

  • fetchSet(_:) Extension method

    A set of fetched values.

    let request: ... // Some FetchRequest that fetches String
    let strings = try request.fetchSet(db) // Set<String>
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public func fetchSet(_ db: Database) throws -> Set<RowDecoder>

    Parameters

    db

    A database connection.

    Return Value

    An array of values.

Fetching Optional values

  • fetchCursor(_:) Extension method

    A cursor over fetched optional values.

    let request: ... // Some FetchRequest that fetches Optional<String>
    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.

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public func fetchCursor(_ db: Database) throws -> FastNullableDatabaseValueCursor<RowDecoder.Wrapped>

    Parameters

    db

    A database connection.

    Return Value

    A cursor over fetched values.

  • fetchAll(_:) Extension method

    An array of fetched optional values.

    let request: ... // Some FetchRequest that fetches Optional<String>
    let strings = try request.fetchAll(db) // [String?]
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public func fetchAll(_ db: Database) throws -> [RowDecoder.Wrapped?]

    Parameters

    db

    A database connection.

    Return Value

    An array of values.

  • fetchOne(_:) Extension method

    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 that fetches String?
    let string = try request.fetchOne(db) // String?
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public func fetchOne(_ db: Database) throws -> RowDecoder.Wrapped?

    Parameters

    db

    A database connection.

    Return Value

    An optional value.

Available where RowDecoder: _OptionalProtocol, RowDecoder.Wrapped: DatabaseValueConvertible & StatementColumnConvertible & Hashable

  • fetchSet(_:) Extension method

    A set of fetched optional values.

    let request: ... // Some FetchRequest that fetches Optional<String>
    let strings = try request.fetchSet(db) // Set<String?>
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public func fetchSet(_ db: Database) throws -> Set<RowDecoder.Wrapped?>

    Parameters

    db

    A database connection.

    Return Value

    A set of values.

Fetching Records

  • fetchCursor(_:) Extension method

    A cursor over fetched records.

    let request: ... // Some FetchRequest that fetches Player
    let players = try request.fetchCursor(db) // Cursor of Player
    while let player = try players.next() {   // Player
        ...
    }
    

    If the database is modified during the cursor iteration, the remaining elements are undefined.

    The cursor must be iterated in a protected dispatch queue.

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public func fetchCursor(_ db: Database) throws -> RecordCursor<RowDecoder>

    Parameters

    db

    A database connection.

    Return Value

    A cursor over fetched records.

  • fetchAll(_:) Extension method

    An array of fetched records.

    let request: ... // Some FetchRequest that fetches Player
    let players = try request.fetchAll(db) // [Player]
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public func fetchAll(_ db: Database) throws -> [RowDecoder]

    Parameters

    db

    A database connection.

    Return Value

    An array of records.

  • fetchOne(_:) Extension method

    The first fetched record.

    let request: ... // Some FetchRequest that fetches Player
    let player = try request.fetchOne(db) // Player?
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public func fetchOne(_ db: Database) throws -> RowDecoder?

    Parameters

    db

    A database connection.

    Return Value

    An optional record.

Available where RowDecoder: FetchableRecord & Hashable

  • fetchSet(_:) Extension method

    A set of fetched records.

    let request: ... // Some FetchRequest that fetches Player
    let players = try request.fetchSet(db) // Set<Player>
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public func fetchSet(_ db: Database) throws -> Set<RowDecoder>

    Parameters

    db

    A database connection.

    Return Value

    A set of records.