DatabaseValueConvertible

public protocol DatabaseValueConvertible : SQLExpressible

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.makeSelectStatement(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

    Returns a value that can be stored in the database.

    Declaration

    Swift

    var databaseValue: DatabaseValue { get }
  • fromDatabaseValue(_:) Default implementation

    Returns a value initialized from dbValue, if possible.

    Default Implementation

    Returns a value initialized from dbValue, if possible.

    Declaration

    Swift

    static func fromDatabaseValue(_ dbValue: DatabaseValue) -> `Self`?
  • Returns a cursor over values fetched from a prepared statement.

    let statement = try db.makeSelectStatement(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 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 -> DatabaseValueCursor<Self>

    Parameters

    statement

    The statement to run.

    arguments

    Optional statement arguments.

    adapter

    Optional RowAdapter

    Return Value

    A cursor over fetched values.

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

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

    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.

  • 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.makeSelectStatement(sql: "SELECT name FROM ...")
    let name = try String.fetchOne(statement)   // String?
    

    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 value.

  • 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 dispath 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 -> DatabaseValueCursor<Self>

    Parameters

    db

    A database connection.

    sql

    An SQL query.

    arguments

    Statement arguments.

    adapter

    Optional RowAdapter

    Return Value

    A cursor over fetched values.

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

    Declaration

    Swift

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

    Parameters

    db

    A database connection.

    sql

    An SQL query.

    arguments

    Statement arguments.

    adapter

    Optional RowAdapter

    Return Value

    An array.

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

    Declaration

    Swift

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

    Parameters

    db

    A database connection.

    sql

    An SQL query.

    arguments

    Statement arguments.

    adapter

    Optional RowAdapter

    Return Value

    An optional value.

  • 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 dispath queue.

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public static func fetchCursor<R>(_ db: Database, _ request: R) throws -> DatabaseValueCursor<Self> where R : FetchRequest

    Parameters

    db

    A database connection.

    request

    A FetchRequest.

    Return Value

    A cursor over fetched values.

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

    Declaration

    Swift

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

    Parameters

    db

    A database connection.

    request

    A FetchRequest.

    Return Value

    An array.

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

    Declaration

    Swift

    public static func fetchOne<R>(_ db: Database, _ request: R) throws -> Self? where R : FetchRequest

    Parameters

    db

    A database connection.

    request

    A FetchRequest.

    Return Value

    An optional value.

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

    let statement = try db.makeSelectStatement(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 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 -> FastDatabaseValueCursor<Self>

    Parameters

    statement

    The statement to run.

    arguments

    Optional statement arguments.

    adapter

    Optional RowAdapter

    Return Value

    A cursor over fetched values.

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

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

    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 values.

  • Returns a single value fetched from a prepared statement.

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

    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 value.

  • 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 dispath 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 -> FastDatabaseValueCursor<Self>

    Parameters

    db

    A database connection.

    sql

    An SQL query.

    arguments

    Statement arguments.

    adapter

    Optional RowAdapter

    Return Value

    A cursor over fetched values.

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

    Declaration

    Swift

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

    Parameters

    db

    A database connection.

    sql

    An SQL query.

    arguments

    Statement arguments.

    adapter

    Optional RowAdapter

    Return Value

    An array of values.

  • Returns a single value fetched from an SQL query.

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

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

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

    Parameters

    db

    A database connection.

    sql

    An SQL query.

    arguments

    Statement arguments.

    adapter

    Optional RowAdapter

    Return Value

    An optional value.

  • 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 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 dispath queue.

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

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

    Parameters

    db

    A database connection.

    request

    A FetchRequest.

    Return Value

    A cursor over fetched values.

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

    Declaration

    Swift

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

    Parameters

    db

    A database connection.

    request

    A FetchRequest.

    Return Value

    An array of values.

  • fetchOne(_:_:) Extension method

    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?
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public static func fetchOne<R>(_ db: Database, _ request: R) throws -> Self? where R : FetchRequest

    Parameters

    db

    A database connection.

    request

    A FetchRequest.

    Return Value

    An optional value.