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)
-
fetchCursor(_:)
Extension methodA 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 methodAn 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 methodThe 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 methodReturns 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 methodReturns 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 methodReturns 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 methodReturns 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 methodReturns 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 methodReturns 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.
-
fetchCursor(_:arguments:adapter:)
Extension methodA 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.
-
fetchAll(_:arguments:adapter:)
Extension methodReturns 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.
-
fetchOne(_:arguments:adapter:)
Extension methodReturns 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 methodReturns 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 methodReturns 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.Parameters
db
A database connection.
-
fetchOne(_:_:)
Extension methodReturns 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.Parameters
db
A database connection.
-
fetchCursor(_:_:arguments:adapter:)
Extension methodReturns 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.
-
fetchAll(_:_:arguments:adapter:)
Extension methodReturns 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.
-
fetchOne(_:_:arguments:adapter:)
Extension methodReturns 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.