RowConvertible

public protocol RowConvertible

Types that adopt RowConvertible can be initialized from a database Row.

let row = try Row.fetchOne(db, "SELECT ...")!
let person = Person(row)

The protocol comes with built-in methods that allow to fetch cursors, arrays, or single records:

try Person.fetchCursor(db, "SELECT ...", arguments:...) // DatabaseCursor<Person>
try Person.fetchAll(db, "SELECT ...", arguments:...)    // [Person]
try Person.fetchOne(db, "SELECT ...", arguments:...)    // Person?

let statement = try db.makeSelectStatement("SELECT ...")
try Person.fetchCursor(statement, arguments:...) // DatabaseCursor<Person>
try Person.fetchAll(statement, arguments:...)    // [Person]
try Person.fetchOne(statement, arguments:...)    // Person?

RowConvertible is adopted by Record.

  • Initializes a record from row.

    For performance reasons, the row argument may be reused during the iteration of a fetch query. If you want to keep the row for later use, make sure to store a copy: self.row = row.copy().

    Declaration

    Swift

    init(row: Row)
  • awakeFromFetch(row:) Default implementation

    Do not call this method directly.

    This method is called in an arbitrary dispatch queue, after a record has been fetched from the database.

    Types that adopt RowConvertible have an opportunity to complete their initialization.

    Default Implementation

    Default implementation, which does nothing.

    Declaration

    Swift

    mutating func awakeFromFetch(row: Row)
  • A cursor over records fetched from a prepared statement.

    let statement = try db.makeSelectStatement("SELECT * FROM persons")
    let persons = try Person.fetchCursor(statement) // DatabaseCursor<Person>
    while let person = try persons.next() { // Person
        ...
    }
    

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

    The cursor must be iterated in a protected dispath queue.

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public static func fetchCursor(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> DatabaseCursor<Self>

    Parameters

    statement

    The statement to run.

    arguments

    Optional statement arguments.

    adapter

    Optional RowAdapter

    Return Value

    A cursor over fetched records.

  • Returns an array of records fetched from a prepared statement.

    let statement = try db.makeSelectStatement("SELECT * FROM persons")
    let persons = try Person.fetchAll(statement) // [Person]
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

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

    Parameters

    statement

    The statement to run.

    arguments

    Optional statement arguments.

    adapter

    Optional RowAdapter

    Return Value

    An array of records.

  • Returns a single record fetched from a prepared statement.

    let statement = try db.makeSelectStatement("SELECT * FROM persons")
    let person = try Person.fetchOne(statement) // Person?
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public static func fetchOne(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> Self?

    Parameters

    statement

    The statement to run.

    arguments

    Optional statement arguments.

    adapter

    Optional RowAdapter

    Return Value

    An optional record.

  • fetchCursor(_:_:) Extension method

    Returns a cursor over records fetched from a fetch request.

    let nameColumn = Column("firstName")
    let request = Person.order(nameColumn)
    let identities = try Identity.fetchCursor(db, request) // DatabaseCursor<Identity>
    while let identity = try identities.next() { // Identity
        ...
    }
    

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

    The cursor must be iterated in a protected dispath queue.

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public static func fetchCursor(_ db: Database, _ request: Request) throws -> DatabaseCursor<Self>

    Parameters

    db

    A database connection.

    request

    A fetch request.

    Return Value

    A cursor over fetched records.

  • fetchAll(_:_:) Extension method

    Returns an array of records fetched from a fetch request.

    let nameColumn = Column("name")
    let request = Person.order(nameColumn)
    let identities = try Identity.fetchAll(db, request) // [Identity]
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public static func fetchAll(_ db: Database, _ request: Request) throws -> [Self]

    Parameters

    db

    A database connection.

  • fetchOne(_:_:) Extension method

    Returns a single record fetched from a fetch request.

    let nameColumn = Column("name")
    let request = Person.order(nameColumn)
    let identity = try Identity.fetchOne(db, request) // Identity?
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public static func fetchOne(_ db: Database, _ request: Request) throws -> Self?

    Parameters

    db

    A database connection.

  • Returns a cursor over records fetched from an SQL query.

    let persons = try Person.fetchCursor(db, "SELECT * FROM persons") // DatabaseCursor<Person>
    while let person = try persons.next() { // Person
        ...
    }
    

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

    The cursor must be iterated in a protected dispath queue.

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

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

    Parameters

    db

    A database connection.

    sql

    An SQL query.

    arguments

    Optional statement arguments.

    adapter

    Optional RowAdapter

    Return Value

    A cursor over fetched records.

  • Returns an array of records fetched from an SQL query.

    let persons = try Person.fetchAll(db, "SELECT * FROM persons") // [Person]
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

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

    Parameters

    db

    A database connection.

    sql

    An SQL query.

    arguments

    Optional statement arguments.

    adapter

    Optional RowAdapter

    Return Value

    An array of records.

  • Returns a single record fetched from an SQL query.

    let person = try Person.fetchOne(db, "SELECT * FROM persons") // Person?
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public static func fetchOne(_ db: Database, _ sql: String, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> Self?

    Parameters

    db

    A database connection.

    sql

    An SQL query.

    arguments

    Optional statement arguments.

    adapter

    Optional RowAdapter

    Return Value

    An optional record.

  • fetchCursor(_:) Extension method

    A cursor over all records fetched from the database.

    let persons = try Person.fetchCursor(db) // DatabaseCursor<Person>
    while let person = try persons.next() {  // Person
        ...
    }
    

    Records are iterated in the natural ordering of the table.

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

    The cursor must be iterated in a protected dispath queue.

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public static func fetchCursor(_ db: Database) throws -> DatabaseCursor<Self>

    Parameters

    db

    A database connection.

    Return Value

    A cursor over fetched records.

  • fetchAll(_:) Extension method

    An array of all records fetched from the database.

    let persons = try Person.fetchAll(db) // [Person]
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

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

    Parameters

    db

    A database connection.

  • fetchOne(_:) Extension method

    The first found record.

    let person = try Person.fetchOne(db) // Person?
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

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

    Parameters

    db

    A database connection.

  • fetchCursor(_:keys:) Extension method

    Returns a cursor over records, given their primary keys.

    let persons = try Person.fetchCursor(db, keys: [1, 2, 3]) // DatabaseCursor<Person>
    while let person = try persons.next() {
        ...
    }
    

    Records are iterated in unspecified order.

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public static func fetchCursor<Sequence: Swift.Sequence>(_ db: Database, keys: Sequence) throws -> DatabaseCursor<Self>? where Sequence.Iterator.Element: DatabaseValueConvertible

    Parameters

    db

    A database connection.

    keys

    A sequence of primary keys.

    Return Value

    A cursor over fetched records.

  • fetchAll(_:keys:) Extension method

    Returns an array of records, given their primary keys.

    let persons = try Person.fetchAll(db, keys: [1, 2, 3]) // [Person]
    

    The order of records in the returned array is undefined.

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public static func fetchAll<Sequence: Swift.Sequence>(_ db: Database, keys: Sequence) throws -> [Self] where Sequence.Iterator.Element: DatabaseValueConvertible

    Parameters

    db

    A database connection.

    keys

    A sequence of primary keys.

    Return Value

    An array of records.

  • fetchOne(_:key:) Extension method

    Returns a single record given its primary key.

    let person = try Person.fetchOne(db, key: 123) // Person?
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public static func fetchOne<PrimaryKeyType: DatabaseValueConvertible>(_ db: Database, key: PrimaryKeyType?) throws -> Self?

    Parameters

    db

    A database connection.

    key

    A primary key value.

    Return Value

    An optional record.

  • fetchCursor(_:keys:) Extension method

    Returns a cursor over records identified by the provided unique keys (primary key or any key with a unique index on it).

    let persons = try Person.fetchCursor(db, keys: [["email": "a@example.com"], ["email": "b@example.com"]]) // DatabaseCursor<Person>
    while let person = try persons.next() { // Person
        ...
    }
    

    Records are iterated in unspecified order.

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public static func fetchCursor(_ db: Database, keys: [[String: DatabaseValueConvertible?]]) throws -> DatabaseCursor<Self>?

    Parameters

    db

    A database connection.

    keys

    An array of key dictionaries.

    Return Value

    A cursor over fetched records.

  • fetchAll(_:keys:) Extension method

    Returns an array of records identified by the provided unique keys (primary key or any key with a unique index on it).

    let persons = try Person.fetchAll(db, keys: [["email": "a@example.com"], ["email": "b@example.com"]]) // [Person]
    

    The order of records in the returned array is undefined.

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public static func fetchAll(_ db: Database, keys: [[String: DatabaseValueConvertible?]]) throws -> [Self]

    Parameters

    db

    A database connection.

    keys

    An array of key dictionaries.

    Return Value

    An array of records.

  • fetchOne(_:key:) Extension method

    Returns a single record identified by a unique key (the primary key or any key with a unique index on it).

    let person = try Person.fetchOne(db, key: ["name": Arthur"]) // Person?
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public static func fetchOne(_ db: Database, key: [String: DatabaseValueConvertible?]) throws -> Self?

    Parameters

    db

    A database connection.

    key

    A dictionary of values.

    Return Value

    An optional record.