mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
Fetch sequences of optional or non-optional values.
This commit is contained in:
1 parent
8c508ef9a9
commit
ff9b2c8bda
4 files changed
+207
-206
No files matched your search
@@ -32,124 +32,11 @@ public protocol DatabaseValueConvertible {
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Fetching optional DatabaseValueConvertible
|
||||
|
||||
/**
|
||||
Types that adopt DatabaseValueConvertible can be initialized from database
|
||||
values.
|
||||
|
||||
The protocol comes with built-in methods that allow to fetch sequences, arrays,
|
||||
or single instances:
|
||||
|
||||
String.fetch(db, "SELECT name FROM ...", arguments:...) // DatabaseSequence<String>
|
||||
String.fetchAll(db, "SELECT name FROM ...", arguments:...) // [String]
|
||||
|
||||
let statement = db.selectStatement("SELECT name FROM ...")
|
||||
String.fetch(statement, arguments:...) // DatabaseSequence<String>
|
||||
String.fetchAll(statement, arguments:...) // [String]
|
||||
|
||||
DatabaseValueConvertible is adopted by Bool, Int, String, etc.
|
||||
*/
|
||||
extension Optional where Wrapped: DatabaseValueConvertible {
|
||||
|
||||
// MARK: - Fetching From SelectStatement
|
||||
|
||||
/**
|
||||
Fetches a sequence of optional values.
|
||||
|
||||
let statement = db.selectStatement("SELECT name FROM ...")
|
||||
let names = Optional<String>.fetch(statement) // DatabaseSequence<String?>
|
||||
|
||||
The returned sequence can be consumed several times, but it may yield
|
||||
different results, should database changes have occurred between two
|
||||
generations:
|
||||
|
||||
let names = Optional<String>.fetch(statement)
|
||||
Array(names) // Arthur, Barbara
|
||||
db.execute("DELETE ...")
|
||||
Array(names) // Arthur
|
||||
|
||||
If the database is modified while the sequence is iterating, the remaining
|
||||
elements are undefined.
|
||||
|
||||
- parameter statement: The statement to run.
|
||||
- parameter arguments: Optional statement arguments.
|
||||
- returns: A sequence of optional values.
|
||||
*/
|
||||
public static func fetch(statement: SelectStatement, arguments: StatementArguments? = nil) -> DatabaseSequence<Wrapped?> {
|
||||
let sqliteStatement = statement.sqliteStatement
|
||||
return statement.fetch(arguments: arguments) {
|
||||
Wrapped.fromDatabaseValue(DatabaseValue(sqliteStatement: sqliteStatement, index: 0))
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
Fetches an array of optional values.
|
||||
|
||||
let statement = db.selectStatement("SELECT name FROM ...")
|
||||
let names = Optional<String>.fetchAll(statement) // [String?]
|
||||
|
||||
- parameter statement: The statement to run.
|
||||
- parameter arguments: Optional statement arguments.
|
||||
- returns: An array of optional values.
|
||||
*/
|
||||
public static func fetchAll(statement: SelectStatement, arguments: StatementArguments? = nil) -> [Wrapped?] {
|
||||
return Array(fetch(statement, arguments: arguments))
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Fetching From Database
|
||||
|
||||
/**
|
||||
Fetches a sequence of optional values.
|
||||
|
||||
let names = Optional<String>.fetch(db, "SELECT name FROM ...") // DatabaseSequence<String?>
|
||||
|
||||
The returned sequence can be consumed several times, but it may yield
|
||||
different results, should database changes have occurred between two
|
||||
generations:
|
||||
|
||||
let names = Optional<String>.fetch(db, "SELECT name FROM ...")
|
||||
Array(names) // Arthur, Barbara
|
||||
db.execute("DELETE ...")
|
||||
Array(names) // Arthur
|
||||
|
||||
If the database is modified while the sequence is iterating, the remaining
|
||||
elements are undefined.
|
||||
|
||||
- parameter db: A Database.
|
||||
- parameter sql: An SQL query.
|
||||
- parameter arguments: Optional statement arguments.
|
||||
- returns: A sequence of optional values.
|
||||
*/
|
||||
public static func fetch(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> DatabaseSequence<Wrapped?> {
|
||||
return fetch(db.selectStatement(sql), arguments: arguments)
|
||||
}
|
||||
|
||||
/**
|
||||
Fetches an array of optional values.
|
||||
|
||||
let names = String.fetchAll(db, "SELECT name FROM ...") // [String?]
|
||||
|
||||
- parameter db: A Database.
|
||||
- parameter sql: An SQL query.
|
||||
- parameter arguments: Optional statement arguments.
|
||||
- returns: An array of optional values.
|
||||
*/
|
||||
public static func fetchAll(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> [Wrapped?] {
|
||||
return fetchAll(db.selectStatement(sql), arguments: arguments)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Fetching non null DatabaseValueConvertible
|
||||
|
||||
/**
|
||||
Types that adopt DatabaseValueConvertible can be initialized from database
|
||||
values.
|
||||
|
||||
The protocol comes with built-in methods that allow to fetch sequences, arrays,
|
||||
or single instances:
|
||||
DatabaseValueConvertible comes with built-in methods that allow to fetch
|
||||
sequences, arrays, or single instances:
|
||||
|
||||
String.fetch(db, "SELECT name FROM ...", arguments:...) // DatabaseSequence<String>
|
||||
String.fetchAll(db, "SELECT name FROM ...", arguments:...) // [String]
|
||||
@@ -295,143 +182,108 @@ public extension DatabaseValueConvertible {
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Fetching DatabaseValueConvertible + SQLiteStatementConvertible
|
||||
// MARK: - Fetching optional DatabaseValueConvertible
|
||||
|
||||
public extension DatabaseValueConvertible where Self: SQLiteStatementConvertible {
|
||||
/**
|
||||
Swift's Optional comes with built-in methods that allow to fetch sequences and
|
||||
arrays of optional DatabaseValueConvertible:
|
||||
|
||||
Optional<String>.fetch(db, "SELECT name FROM ...", arguments:...) // DatabaseSequence<String?>
|
||||
Optional<String>.fetchAll(db, "SELECT name FROM ...", arguments:...) // [String?]
|
||||
|
||||
let statement = db.selectStatement("SELECT name FROM ...")
|
||||
Optional<String>.fetch(statement, arguments:...) // DatabaseSequence<String?>
|
||||
Optional<String>.fetchAll(statement, arguments:...) // [String?]
|
||||
|
||||
DatabaseValueConvertible is adopted by Bool, Int, String, etc.
|
||||
*/
|
||||
public extension Optional where Wrapped: DatabaseValueConvertible {
|
||||
|
||||
// MARK: - Fetching From SelectStatement
|
||||
|
||||
/**
|
||||
This method is an optimized specialization of
|
||||
DatabaseValueConvertible.fetch(_,arguments:) for types that adopt both
|
||||
DatabaseValueConvertible and SQLiteStatementConvertible protocols.
|
||||
Fetches a sequence of optional values.
|
||||
|
||||
let statement = db.selectStatement("SELECT name FROM ...")
|
||||
let names = String.fetch(statement) // DatabaseSequence<String>
|
||||
let names = Optional<String>.fetch(statement) // DatabaseSequence<String?>
|
||||
|
||||
See the documentation of DatabaseValueConvertible.fetch(_,arguments:) for
|
||||
more information.
|
||||
The returned sequence can be consumed several times, but it may yield
|
||||
different results, should database changes have occurred between two
|
||||
generations:
|
||||
|
||||
let names = Optional<String>.fetch(statement)
|
||||
Array(names) // Arthur, Barbara
|
||||
db.execute("DELETE ...")
|
||||
Array(names) // Arthur
|
||||
|
||||
If the database is modified while the sequence is iterating, the remaining
|
||||
elements are undefined.
|
||||
|
||||
- parameter statement: The statement to run.
|
||||
- parameter arguments: Optional statement arguments.
|
||||
- returns: A sequence of non null values.
|
||||
- returns: A sequence of optional values.
|
||||
*/
|
||||
public static func fetch(statement: SelectStatement, arguments: StatementArguments? = nil) -> DatabaseSequence<Self> {
|
||||
public static func fetch(statement: SelectStatement, arguments: StatementArguments? = nil) -> DatabaseSequence<Wrapped?> {
|
||||
let sqliteStatement = statement.sqliteStatement
|
||||
return statement.fetch(arguments: arguments) {
|
||||
let sqliteStatement = statement.sqliteStatement
|
||||
if sqlite3_column_type(sqliteStatement, 0) == SQLITE_NULL {
|
||||
if let arguments = statement.arguments {
|
||||
fatalError("Found NULL \(Self.self) while iterating `\(statement.sql)` with arguments \(arguments).")
|
||||
} else {
|
||||
fatalError("Found NULL \(Self.self) while iterating `\(statement.sql)`.")
|
||||
}
|
||||
} else {
|
||||
return Self.init(sqliteStatement: sqliteStatement, index: 0)
|
||||
}
|
||||
Wrapped.fromDatabaseValue(DatabaseValue(sqliteStatement: sqliteStatement, index: 0))
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
This method is an optimized specialization of
|
||||
DatabaseValueConvertible.fetchAll(_,arguments:) for types that adopt both
|
||||
DatabaseValueConvertible and SQLiteStatementConvertible protocols.
|
||||
Fetches an array of optional values.
|
||||
|
||||
let statement = db.selectStatement("SELECT name FROM ...")
|
||||
let names = String.fetchAll(statement) // [String]
|
||||
|
||||
See the documentation of DatabaseValueConvertible.fetchAll(_,arguments:) for
|
||||
more information.
|
||||
let names = Optional<String>.fetchAll(statement) // [String?]
|
||||
|
||||
- parameter statement: The statement to run.
|
||||
- parameter arguments: Optional statement arguments.
|
||||
- returns: An array of non null values.
|
||||
- returns: An array of optional values.
|
||||
*/
|
||||
public static func fetchAll(statement: SelectStatement, arguments: StatementArguments? = nil) -> [Self] {
|
||||
public static func fetchAll(statement: SelectStatement, arguments: StatementArguments? = nil) -> [Wrapped?] {
|
||||
return Array(fetch(statement, arguments: arguments))
|
||||
}
|
||||
|
||||
/**
|
||||
This method is an optimized specialization of
|
||||
DatabaseValueConvertible.fetchOne(_,arguments:) for types that adopt both
|
||||
DatabaseValueConvertible and SQLiteStatementConvertible protocols.
|
||||
|
||||
let statement = db.selectStatement("SELECT name FROM ...")
|
||||
let name = String.fetchOne(statement) // String?
|
||||
|
||||
See the documentation of DatabaseValueConvertible.fetchOne(_,arguments:) for
|
||||
more information.
|
||||
|
||||
- parameter statement: The statement to run.
|
||||
- parameter arguments: Optional statement arguments.
|
||||
- returns: An optional value.
|
||||
*/
|
||||
public static func fetchOne(statement: SelectStatement, arguments: StatementArguments? = nil) -> Self? {
|
||||
let optionals = statement.fetch(arguments: arguments) {
|
||||
Self.fromDatabaseValue(DatabaseValue(sqliteStatement: statement.sqliteStatement, index: 0))
|
||||
}
|
||||
guard let value = optionals.generate().next() else {
|
||||
return nil
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Fetching From Database
|
||||
|
||||
/**
|
||||
This method is an optimized specialization of
|
||||
DatabaseValueConvertible.fetch(_,sql:,arguments:) for types that adopt both
|
||||
DatabaseValueConvertible and SQLiteStatementConvertible protocols.
|
||||
Fetches a sequence of optional values.
|
||||
|
||||
let names = String.fetch(db, "SELECT name FROM ...") // DatabaseSequence<String>
|
||||
let names = Optional<String>.fetch(db, "SELECT name FROM ...") // DatabaseSequence<String?>
|
||||
|
||||
See the documentation of DatabaseValueConvertible.fetch(_,sql:,arguments:)
|
||||
for more information.
|
||||
The returned sequence can be consumed several times, but it may yield
|
||||
different results, should database changes have occurred between two
|
||||
generations:
|
||||
|
||||
let names = Optional<String>.fetch(db, "SELECT name FROM ...")
|
||||
Array(names) // Arthur, Barbara
|
||||
db.execute("DELETE ...")
|
||||
Array(names) // Arthur
|
||||
|
||||
If the database is modified while the sequence is iterating, the remaining
|
||||
elements are undefined.
|
||||
|
||||
- parameter db: A Database.
|
||||
- parameter sql: An SQL query.
|
||||
- parameter arguments: Optional statement arguments.
|
||||
- returns: A sequence of non null values.
|
||||
- returns: A sequence of optional values.
|
||||
*/
|
||||
public static func fetch(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> DatabaseSequence<Self> {
|
||||
public static func fetch(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> DatabaseSequence<Wrapped?> {
|
||||
return fetch(db.selectStatement(sql), arguments: arguments)
|
||||
}
|
||||
|
||||
/**
|
||||
This method is an optimized specialization of
|
||||
DatabaseValueConvertible.fetchAll(_,sql:,arguments:) for types that adopt
|
||||
both DatabaseValueConvertible and SQLiteStatementConvertible protocols.
|
||||
Fetches an array of optional values.
|
||||
|
||||
let names = String.fetchAll(db, "SELECT name FROM ...") // [String?]
|
||||
|
||||
See the documentation of DatabaseValueConvertible.fetchAll(_,sql:,arguments:)
|
||||
for more information.
|
||||
|
||||
- parameter db: A Database.
|
||||
- parameter sql: An SQL query.
|
||||
- parameter arguments: Optional statement arguments.
|
||||
- returns: An array of non null values.
|
||||
- returns: An array of optional values.
|
||||
*/
|
||||
public static func fetchAll(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> [Self] {
|
||||
public static func fetchAll(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> [Wrapped?] {
|
||||
return fetchAll(db.selectStatement(sql), arguments: arguments)
|
||||
}
|
||||
|
||||
/**
|
||||
This method is an optimized specialization of
|
||||
DatabaseValueConvertible.fetchOne(_,sql:,arguments:) for types that adopt
|
||||
both DatabaseValueConvertible and SQLiteStatementConvertible protocols.
|
||||
|
||||
let name = String.fetchOne(db, "SELECT name FROM ...") // String?
|
||||
|
||||
See the documentation of DatabaseValueConvertible.fetchOne(_,sql:,arguments:)
|
||||
for more information.
|
||||
|
||||
- parameter db: A Database.
|
||||
- parameter sql: An SQL query.
|
||||
- parameter arguments: Optional statement arguments.
|
||||
- returns: An optional value.
|
||||
*/
|
||||
public static func fetchOne(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> Self? {
|
||||
return fetchOne(db.selectStatement(sql), arguments: arguments)
|
||||
}
|
||||
}
|
||||
@@ -25,3 +25,151 @@ public protocol SQLiteStatementConvertible {
|
||||
*/
|
||||
init(sqliteStatement: SQLiteStatement, index: Int32)
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Fetching non null SQLiteStatementConvertible
|
||||
|
||||
/**
|
||||
Types that adopt both DatabaseValueConvertible and SQLiteStatementConvertible
|
||||
can be efficiently initialized from database values.
|
||||
|
||||
See DatabaseValueConvertible for more information.
|
||||
*/
|
||||
public extension DatabaseValueConvertible where Self: SQLiteStatementConvertible {
|
||||
|
||||
// MARK: - Fetching From SelectStatement
|
||||
|
||||
/**
|
||||
This method is an optimized specialization of
|
||||
DatabaseValueConvertible.fetch(_,arguments:) for types that adopt both
|
||||
DatabaseValueConvertible and SQLiteStatementConvertible protocols.
|
||||
|
||||
let statement = db.selectStatement("SELECT name FROM ...")
|
||||
let names = String.fetch(statement) // DatabaseSequence<String>
|
||||
|
||||
See the documentation of DatabaseValueConvertible.fetch(_,arguments:) for
|
||||
more information.
|
||||
|
||||
- parameter statement: The statement to run.
|
||||
- parameter arguments: Optional statement arguments.
|
||||
- returns: A sequence of non null values.
|
||||
*/
|
||||
public static func fetch(statement: SelectStatement, arguments: StatementArguments? = nil) -> DatabaseSequence<Self> {
|
||||
return statement.fetch(arguments: arguments) {
|
||||
let sqliteStatement = statement.sqliteStatement
|
||||
if sqlite3_column_type(sqliteStatement, 0) == SQLITE_NULL {
|
||||
if let arguments = statement.arguments {
|
||||
fatalError("Found NULL \(Self.self) while iterating `\(statement.sql)` with arguments \(arguments).")
|
||||
} else {
|
||||
fatalError("Found NULL \(Self.self) while iterating `\(statement.sql)`.")
|
||||
}
|
||||
} else {
|
||||
return Self.init(sqliteStatement: sqliteStatement, index: 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
This method is an optimized specialization of
|
||||
DatabaseValueConvertible.fetchAll(_,arguments:) for types that adopt both
|
||||
DatabaseValueConvertible and SQLiteStatementConvertible protocols.
|
||||
|
||||
let statement = db.selectStatement("SELECT name FROM ...")
|
||||
let names = String.fetchAll(statement) // [String]
|
||||
|
||||
See the documentation of DatabaseValueConvertible.fetchAll(_,arguments:) for
|
||||
more information.
|
||||
|
||||
- parameter statement: The statement to run.
|
||||
- parameter arguments: Optional statement arguments.
|
||||
- returns: An array of non null values.
|
||||
*/
|
||||
public static func fetchAll(statement: SelectStatement, arguments: StatementArguments? = nil) -> [Self] {
|
||||
return Array(fetch(statement, arguments: arguments))
|
||||
}
|
||||
|
||||
/**
|
||||
This method is an optimized specialization of
|
||||
DatabaseValueConvertible.fetchOne(_,arguments:) for types that adopt both
|
||||
DatabaseValueConvertible and SQLiteStatementConvertible protocols.
|
||||
|
||||
let statement = db.selectStatement("SELECT name FROM ...")
|
||||
let name = String.fetchOne(statement) // String?
|
||||
|
||||
See the documentation of DatabaseValueConvertible.fetchOne(_,arguments:) for
|
||||
more information.
|
||||
|
||||
- parameter statement: The statement to run.
|
||||
- parameter arguments: Optional statement arguments.
|
||||
- returns: An optional value.
|
||||
*/
|
||||
public static func fetchOne(statement: SelectStatement, arguments: StatementArguments? = nil) -> Self? {
|
||||
let optionals = statement.fetch(arguments: arguments) {
|
||||
Self.fromDatabaseValue(DatabaseValue(sqliteStatement: statement.sqliteStatement, index: 0))
|
||||
}
|
||||
guard let value = optionals.generate().next() else {
|
||||
return nil
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Fetching From Database
|
||||
|
||||
/**
|
||||
This method is an optimized specialization of
|
||||
DatabaseValueConvertible.fetch(_,sql:,arguments:) for types that adopt both
|
||||
DatabaseValueConvertible and SQLiteStatementConvertible protocols.
|
||||
|
||||
let names = String.fetch(db, "SELECT name FROM ...") // DatabaseSequence<String>
|
||||
|
||||
See the documentation of DatabaseValueConvertible.fetch(_,sql:,arguments:)
|
||||
for more information.
|
||||
|
||||
- parameter db: A Database.
|
||||
- parameter sql: An SQL query.
|
||||
- parameter arguments: Optional statement arguments.
|
||||
- returns: A sequence of non null values.
|
||||
*/
|
||||
public static func fetch(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> DatabaseSequence<Self> {
|
||||
return fetch(db.selectStatement(sql), arguments: arguments)
|
||||
}
|
||||
|
||||
/**
|
||||
This method is an optimized specialization of
|
||||
DatabaseValueConvertible.fetchAll(_,sql:,arguments:) for types that adopt
|
||||
both DatabaseValueConvertible and SQLiteStatementConvertible protocols.
|
||||
|
||||
let names = String.fetchAll(db, "SELECT name FROM ...") // [String?]
|
||||
|
||||
See the documentation of DatabaseValueConvertible.fetchAll(_,sql:,arguments:)
|
||||
for more information.
|
||||
|
||||
- parameter db: A Database.
|
||||
- parameter sql: An SQL query.
|
||||
- parameter arguments: Optional statement arguments.
|
||||
- returns: An array of non null values.
|
||||
*/
|
||||
public static func fetchAll(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> [Self] {
|
||||
return fetchAll(db.selectStatement(sql), arguments: arguments)
|
||||
}
|
||||
|
||||
/**
|
||||
This method is an optimized specialization of
|
||||
DatabaseValueConvertible.fetchOne(_,sql:,arguments:) for types that adopt
|
||||
both DatabaseValueConvertible and SQLiteStatementConvertible protocols.
|
||||
|
||||
let name = String.fetchOne(db, "SELECT name FROM ...") // String?
|
||||
|
||||
See the documentation of DatabaseValueConvertible.fetchOne(_,sql:,arguments:)
|
||||
for more information.
|
||||
|
||||
- parameter db: A Database.
|
||||
- parameter sql: An SQL query.
|
||||
- parameter arguments: Optional statement arguments.
|
||||
- returns: An optional value.
|
||||
*/
|
||||
public static func fetchOne(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> Self? {
|
||||
return fetchOne(db.selectStatement(sql), arguments: arguments)
|
||||
}
|
||||
}
|
||||
@@ -338,9 +338,9 @@ for (columnName, databaseValue) in row { ... } // ("a", 1), ("a", 2)
|
||||
|
||||
#### Value Queries
|
||||
|
||||
Instead of rows, you can directly fetch **values**, extracted from the first column of the resulting rows.
|
||||
Instead of rows, you can directly fetch **values**, extracted from the first column.
|
||||
|
||||
Like rows, values can be fetched as **sequences**, **arrays**, or **single** value:
|
||||
Fetch **sequences** of values, **arrays**, or a **single** value:
|
||||
|
||||
```swift
|
||||
dbQueue.inDatabase { db in
|
||||
|
||||
@@ -15,3 +15,4 @@ Not sure:
|
||||
Require changes in the Swift language:
|
||||
|
||||
- [ ] Turn DatabaseIntRepresentable and DatabaseStringRepresentable into SQLiteStatementConvertible when Swift allows for it.
|
||||
- [ ] Specific and optimized Optional<SQLiteStatementConvertible>.fetch... methods when rdar://22852669 is fixed.
|
||||
Reference in new issue
Block a user