DatabaseValueConvertible

Types that adopt DatabaseValueConvertible can be initialized from database values.

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

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

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

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

  • databaseValue Default implementation

    Returns a value that can be stored in the database.

    Default Implementation

  • fromDatabaseValue(_:) Default implementation

    Returns a value initialized from dbValue, if possible.

    Default Implementation

  • fromMissingColumn() Default implementation

    Creates a value from a missing column, if possible.

    Warning

    Do not customize the default implementation.

    Default Implementation

    Default implementation fails to decode a value from a missing column.

  • sqlExpression Extension method
  • bind(to:at:) Extension method

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.

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.
  • 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]
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.
  • Returns a single value fetched from a prepared statement.

    The result is nil if the query returns no row, or if no value can be extracted from the first row.

    let statement = try db.makeStatement(sql: "SELECT name FROM ...")
    let name = try String.fetchOne(statement)   // String?
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

Cursors

  • 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<String>
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

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 name.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.
  • Returns an array of 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.
  • Returns a single value fetched from an SQL query.

    The result is nil if the query returns no row, or if no value can be extracted from the first row.

    let name = try String.fetchOne(db, sql: "SELECT name FROM ...") // String?
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

Cursors

  • Returns a set of 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.

Fetching From FetchRequest

  • fetchCursor(_:_:) Extension method

    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 name.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.
  • fetchAll(_:_:) Extension method

    Returns an array of values fetched from a fetch request.

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

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.
  • fetchOne(_:_:) Extension method

    Returns a single value fetched from a fetch request.

    The result is nil if the query returns no row, or if no value can be extracted from the first row.

    let request = Player.filter(key: 1).select(Column("name"))
    let name = try String.fetchOne(db, request) // String?
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

Cursors

  • fetchSet(_:_:) Extension method

    Returns a set of values fetched from a fetch request.

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

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.