Optional

extension Optional where Wrapped: DatabaseValueConvertible
extension Optional where Wrapped: DatabaseValueConvertible & Hashable
extension Optional where Wrapped: DatabaseValueConvertible & StatementColumnConvertible
extension Optional where Wrapped: DatabaseValueConvertible & StatementColumnConvertible & Hashable

Swift’s Optional comes with built-in methods that allow to fetch cursors and arrays of optional DatabaseValueConvertible:

try Optional<String>.fetchCursor(db, sql: "SELECT name FROM ...", arguments:...) // Cursor of String?
try Optional<String>.fetchAll(db, sql: "SELECT name FROM ...", arguments:...)    // [String?]

let statement = try db.makeStatement(sql: "SELECT name FROM ...")
try Optional<String>.fetchCursor(statement, arguments:...) // Cursor of String?
try Optional<String>.fetchAll(statement, arguments:...)    // [String?]

DatabaseValueConvertible is adopted by Bool, Int, String, etc.

Fetching From Prepared Statement

  • Returns a cursor over optional values fetched from a prepared statement.

    let statement = try db.makeStatement(sql: "SELECT name FROM ...")
    let names = try Optional<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.

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public static func fetchCursor(
        _ statement: Statement,
        arguments: StatementArguments? = nil,
        adapter: RowAdapter? = nil)
    throws -> NullableDatabaseValueCursor<Wrapped>

    Parameters

    statement

    The statement to run.

    arguments

    Optional statement arguments.

    adapter

    Optional RowAdapter

    Return Value

    A cursor over fetched optional values.

  • Returns an array of optional values fetched from a prepared statement.

    let statement = try db.makeStatement(sql: "SELECT name FROM ...")
    let names = try Optional<String>.fetchAll(statement)  // [String?]
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public static func fetchAll(
        _ statement: Statement,
        arguments: StatementArguments? = nil,
        adapter: RowAdapter? = nil)
    throws -> [Wrapped?]

    Parameters

    statement

    The statement to run.

    arguments

    Optional statement arguments.

    adapter

    Optional RowAdapter

    Return Value

    An array of optional values.

Available where Wrapped: DatabaseValueConvertible & Hashable

  • Returns a set of optional values fetched from a prepared statement.

    let statement = try db.makeStatement(sql: "SELECT name FROM ...")
    let names = try Optional<String>.fetchSet(statement)  // Set<String?>
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public static func fetchSet(
        _ statement: Statement,
        arguments: StatementArguments? = nil,
        adapter: RowAdapter? = nil)
    throws -> Set<Wrapped?>

    Parameters

    statement

    The statement to run.

    arguments

    Optional statement arguments.

    adapter

    Optional RowAdapter

    Return Value

    A set of optional values.

Fetching From SQL

  • Returns a cursor over optional values fetched from an SQL query.

    let names = try Optional<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.

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public static func fetchCursor(
        _ db: Database,
        sql: String,
        arguments: StatementArguments = StatementArguments(),
        adapter: RowAdapter? = nil)
    throws -> NullableDatabaseValueCursor<Wrapped>

    Parameters

    db

    A database connection.

    sql

    An SQL query.

    arguments

    Statement arguments.

    adapter

    Optional RowAdapter

    Return Value

    A cursor over fetched optional values.

  • Returns an array of optional values fetched from an SQL query.

    let names = try String.fetchAll(db, sql: "SELECT name FROM ...") // [String?]
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public static func fetchAll(
        _ db: Database,
        sql: String,
        arguments: StatementArguments = StatementArguments(),
        adapter: RowAdapter? = nil)
    throws -> [Wrapped?]

    Parameters

    db

    A database connection.

    sql

    An SQL query.

    arguments

    Statement arguments.

    adapter

    Optional RowAdapter

    Return Value

    An array of optional values.

Available where Wrapped: DatabaseValueConvertible & Hashable

  • Returns a set of optional values fetched from an SQL query.

    let names = try String.fetchSet(db, sql: "SELECT name FROM ...") // Set<String?>
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public static func fetchSet(
        _ db: Database,
        sql: String,
        arguments: StatementArguments = StatementArguments(),
        adapter: RowAdapter? = nil)
    throws -> Set<Wrapped?>

    Parameters

    db

    A database connection.

    sql

    An SQL query.

    arguments

    Statement arguments.

    adapter

    Optional RowAdapter

    Return Value

    A set of optional values.

Fetching From FetchRequest

  • Returns a cursor over optional values fetched from a fetch request.

    let request = Player.select(Column("name"))
    let names = try Optional<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.

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public static func fetchCursor<R: FetchRequest>(_ db: Database, _ request: R)
    throws -> NullableDatabaseValueCursor<Wrapped>

    Parameters

    db

    A database connection.

    request

    A FetchRequest.

    Return Value

    A cursor over fetched optional values.

  • Returns an array of optional values fetched from a fetch request.

    let request = Player.select(Column("name"))
    let names = try Optional<String>.fetchAll(db, request) // [String?]
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public static func fetchAll<R>(_ db: Database, _ request: R) throws -> [Wrapped?] where R : FetchRequest

    Parameters

    db

    A database connection.

    request

    A FetchRequest.

    Return Value

    An array of optional values.

Available where Wrapped: DatabaseValueConvertible & Hashable

  • Returns a set of optional values fetched from a fetch request.

    let request = Player.select(Column("name"))
    let names = try Optional<String>.fetchSet(db, request) // Set<String?>
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public static func fetchSet<R>(_ db: Database, _ request: R) throws -> Set<Wrapped?> where R : FetchRequest

    Parameters

    db

    A database connection.

    request

    A FetchRequest.

    Return Value

    A set of optional values.

Fetching From Prepared Statement

  • Returns a cursor over optional values fetched from a prepared statement.

    let statement = try db.makeStatement(sql: "SELECT name FROM ...")
    let names = try Optional<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.

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public static func fetchCursor(
        _ statement: Statement,
        arguments: StatementArguments? = nil,
        adapter: RowAdapter? = nil)
    throws -> FastNullableDatabaseValueCursor<Wrapped>

    Parameters

    statement

    The statement to run.

    arguments

    Optional statement arguments.

    adapter

    Optional RowAdapter

    Return Value

    A cursor over fetched optional values.

  • Returns an array of optional values fetched from a prepared statement.

    let statement = try db.makeStatement(sql: "SELECT name FROM ...")
    let names = try Optional<String>.fetchAll(statement)  // [String?]
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public static func fetchAll(
        _ statement: Statement,
        arguments: StatementArguments? = nil,
        adapter: RowAdapter? = nil)
    throws -> [Wrapped?]

    Parameters

    statement

    The statement to run.

    arguments

    Optional statement arguments.

    adapter

    Optional RowAdapter

    Return Value

    An array of optional values.

Available where Wrapped: DatabaseValueConvertible & StatementColumnConvertible & Hashable

  • Returns a set of optional values fetched from a prepared statement.

    let statement = try db.makeStatement(sql: "SELECT name FROM ...")
    let names = try Optional<String>.fetchSet(statement)  // Set<String?>
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public static func fetchSet(
        _ statement: Statement,
        arguments: StatementArguments? = nil,
        adapter: RowAdapter? = nil)
    throws -> Set<Wrapped?>

    Parameters

    statement

    The statement to run.

    arguments

    Optional statement arguments.

    adapter

    Optional RowAdapter

    Return Value

    A set of optional values.

Fetching From SQL

  • Returns a cursor over optional values fetched from an SQL query.

    let names = try Optional<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.

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public static func fetchCursor(
        _ db: Database,
        sql: String,
        arguments: StatementArguments = StatementArguments(),
        adapter: RowAdapter? = nil)
    throws -> FastNullableDatabaseValueCursor<Wrapped>

    Parameters

    db

    A database connection.

    sql

    An SQL query.

    arguments

    Statement arguments.

    adapter

    Optional RowAdapter

    Return Value

    A cursor over fetched optional values.

  • Returns an array of optional values fetched from an SQL query.

    let names = try Optional<String>.fetchAll(db, sql: "SELECT name FROM ...") // [String?]
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public static func fetchAll(
        _ db: Database,
        sql: String,
        arguments: StatementArguments = StatementArguments(),
        adapter: RowAdapter? = nil)
    throws -> [Wrapped?]

    Parameters

    db

    A database connection.

    sql

    An SQL query.

    arguments

    Statement arguments.

    adapter

    Optional RowAdapter

    Return Value

    An array of optional values.

Available where Wrapped: DatabaseValueConvertible & StatementColumnConvertible & Hashable

  • Returns a set of optional values fetched from an SQL query.

    let names = try Optional<String>.fetchSet(db, sql: "SELECT name FROM ...") // Set<String?>
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public static func fetchSet(
        _ db: Database,
        sql: String,
        arguments: StatementArguments = StatementArguments(),
        adapter: RowAdapter? = nil)
    throws -> Set<Wrapped?>

    Parameters

    db

    A database connection.

    sql

    An SQL query.

    arguments

    Statement arguments.

    adapter

    Optional RowAdapter

    Return Value

    A set of optional values.

Fetching From FetchRequest

  • Returns a cursor over optional values fetched from a fetch request.

    let request = Player.select(Column("name"))
    let names = try Optional<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.

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public static func fetchCursor<R: FetchRequest>(_ db: Database, _ request: R)
    throws -> FastNullableDatabaseValueCursor<Wrapped>

    Parameters

    db

    A database connection.

    request

    A FetchRequest.

    Return Value

    A cursor over fetched optional values.

  • Returns an array of optional values fetched from a fetch request.

    let request = Player.select(Column("name"))
    let names = try Optional<String>.fetchAll(db, request) // [String?]
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public static func fetchAll<R>(_ db: Database, _ request: R) throws -> [Wrapped?] where R : FetchRequest

    Parameters

    db

    A database connection.

    request

    A FetchRequest.

    Return Value

    An array of optional values.

Available where Wrapped: DatabaseValueConvertible & StatementColumnConvertible & Hashable

  • Returns a set of optional values fetched from a fetch request.

    let request = Player.select(Column("name"))
    let names = try Optional<String>.fetchSet(db, request) // Set<String?>
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public static func fetchSet<R>(_ db: Database, _ request: R) throws -> Set<Wrapped?> where R : FetchRequest

    Parameters

    db

    A database connection.

    request

    A FetchRequest.

    Return Value

    A set of optional values.