DatabaseValueConvertible
public protocol DatabaseValueConvertible : SQLExpressible, StatementBinding
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.
-
Returns a value that can be stored in the database.
Declaration
Swift
var databaseValue: DatabaseValue { get }
-
Returns a value initialized from
dbValue
, if possible.Declaration
Swift
static func fromDatabaseValue(_ dbValue: DatabaseValue) -> Self?
-
sqlExpression
Extension methodDeclaration
Swift
public var sqlExpression: SQLExpression { get }
-
bind(to:
Extension methodat: ) Declaration
Swift
public func bind(to sqliteStatement: SQLiteStatement, at index: CInt) -> CInt
-
fetchCursor(_:
Extension methodarguments: adapter: ) 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.Declaration
Swift
public static func fetchCursor( _ statement: Statement, 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.
-
fetchAll(_:
Extension methodarguments: adapter: ) 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.Declaration
Swift
public static func fetchAll( _ statement: Statement, 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.
-
fetchOne(_:
Extension methodarguments: adapter: ) 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.Declaration
Swift
public static func fetchOne( _ statement: Statement, 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.
-
fetchCursor(_:
Extension methodsql: arguments: adapter: ) 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.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.
-
fetchAll(_:
Extension methodsql: arguments: adapter: ) 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.
-
fetchOne(_:
Extension methodsql: arguments: adapter: ) 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 dispatch 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.
-
fetchSet(_:
Extension methodarguments: adapter: ) 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.Declaration
Swift
public static func fetchSet( _ statement: Statement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> Set<Self>
Parameters
statement
The statement to run.
arguments
Optional statement arguments.
adapter
Optional RowAdapter
Return Value
A set.
-
fetchSet(_:
Extension methodsql: arguments: adapter: ) 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.Declaration
Swift
public static func fetchSet( _ db: Database, sql: String, arguments: StatementArguments = StatementArguments(), adapter: RowAdapter? = nil) throws -> Set<Self>
Parameters
db
A database connection.
sql
An SQL query.
arguments
Statement arguments.
adapter
Optional RowAdapter
Return Value
A set.
-
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.Declaration
Swift
public static func fetchSet<R>(_ db: Database, _ request: R) throws -> Set<Self> where R : FetchRequest
Parameters
db
A database connection.
request
A FetchRequest.
Return Value
A set.
-
fetchCursor(_:
Extension methodarguments: adapter: ) 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.Declaration
Swift
public static func fetchCursor( _ statement: Statement, 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.
-
fetchAll(_:
Extension methodarguments: adapter: ) 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.Declaration
Swift
public static func fetchAll( _ statement: Statement, 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.
-
fetchOne(_:
Extension methodarguments: adapter: ) Returns a single value fetched from a prepared statement.
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.Declaration
Swift
public static func fetchOne( _ statement: Statement, 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.
-
fetchSet(_:
Extension methodarguments: adapter: ) 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.Declaration
Swift
public static func fetchSet( _ statement: Statement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> Set<Self>
Parameters
statement
The statement to run.
arguments
Optional statement arguments.
adapter
Optional RowAdapter
Return Value
A set of values.
-
fetchCursor(_:
Extension methodsql: arguments: adapter: ) 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 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 -> FastDatabaseValueCursor<Self>
Parameters
db
A database connection.
sql
An SQL query.
arguments
Statement arguments.
adapter
Optional RowAdapter
Return Value
A cursor over fetched values.
-
fetchAll(_:
Extension methodsql: arguments: adapter: ) 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.
-
fetchOne(_:
Extension methodsql: arguments: adapter: ) 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.
-
fetchSet(_:
Extension methodsql: arguments: adapter: ) 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.Declaration
Swift
public static func fetchSet( _ db: Database, sql: String, arguments: StatementArguments = StatementArguments(), adapter: RowAdapter? = nil) throws -> Set<Self>
Parameters
db
A database connection.
sql
An SQL query.
arguments
Statement arguments.
adapter
Optional RowAdapter
Return Value
A set of values.
-
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 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 -> 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.
-
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.Declaration
Swift
public static func fetchSet<R>(_ db: Database, _ request: R) throws -> Set<Self> where R : FetchRequest
Parameters
db
A database connection.
request
A FetchRequest.
Return Value
A set of values.
-
databaseValue
Default implementationDefault Implementation
Returns a value that can be stored in the database.
Declaration
Swift
public var databaseValue: DatabaseValue { get }
-
fromDatabaseValue(_:
Default implementation) Default Implementation
Returns a value initialized from dbValue, if possible.
Declaration
Swift
public static func fromDatabaseValue(_ dbValue: DatabaseValue) -> Self?