Optional
extension Optional where Wrapped: DatabaseValueConvertible
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.makeSelectStatement(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.
-
Returns a cursor over optional values fetched from a prepared statement.
let statement = try db.makeSelectStatement(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 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 -> 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.makeSelectStatement(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: SelectStatement, 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.
-
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 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 -> 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.
-
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 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 -> 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.
-
Returns a cursor over optional values fetched from a prepared statement.
let statement = try db.makeSelectStatement(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 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 -> 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.makeSelectStatement(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: SelectStatement, 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.
-
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 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 -> 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 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.
-
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 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 -> 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.