mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-28 04:24:20 +00:00
Cleanup
This commit is contained in:
1 parent
c34ac0f17b
commit
c8023adff1
7 files changed
+21
-22
No files matched your search
@@ -511,7 +511,7 @@ extension DatabasePool : DatabaseWriter {
|
||||
do {
|
||||
try db.beginTransaction(.deferred)
|
||||
assert(db.isInsideTransaction)
|
||||
try db.makeSelectStatement("SELECT rootpage FROM sqlite_master").fetchCursor().next() // doesn't work with cached statement
|
||||
try db.makeSelectStatement("SELECT rootpage FROM sqlite_master").cursor().next() // doesn't work with cached statement
|
||||
} catch {
|
||||
readError = error
|
||||
semaphore.signal() // Release the writer queue and rethrow error
|
||||
|
||||
@@ -128,14 +128,13 @@ extension DatabaseValueConvertible {
|
||||
public static func fetchCursor(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> DatabaseCursor<Self> {
|
||||
// Reuse a single mutable row for performance
|
||||
let row = try Row(statement: statement).adapted(with: adapter, layout: statement)
|
||||
return statement.fetchCursor(arguments: arguments) { () -> Self in
|
||||
return statement.cursor(arguments: arguments, next: {
|
||||
let dbv: DatabaseValue = row.value(atIndex: 0)
|
||||
if let value = Self.fromDatabaseValue(dbv) {
|
||||
return value
|
||||
} else {
|
||||
guard let value = Self.fromDatabaseValue(dbv) else {
|
||||
throw DatabaseError(resultCode: .SQLITE_ERROR, message: "could not convert database value \(dbv) to \(Self.self)", sql: statement.sql, arguments: arguments)
|
||||
}
|
||||
}
|
||||
return value
|
||||
})
|
||||
}
|
||||
|
||||
/// Returns an array of values fetched from a prepared statement.
|
||||
@@ -170,7 +169,7 @@ extension DatabaseValueConvertible {
|
||||
public static func fetchOne(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> Self? {
|
||||
// Reuse a single mutable row for performance
|
||||
let row = try Row(statement: statement).adapted(with: adapter, layout: statement)
|
||||
let cursor = statement.fetchCursor(arguments: arguments) { () -> Self? in
|
||||
let cursor: DatabaseCursor<Self?> = statement.cursor(arguments: arguments, next: {
|
||||
let dbv: DatabaseValue = row.value(atIndex: 0)
|
||||
if let value = Self.fromDatabaseValue(dbv) {
|
||||
return value
|
||||
@@ -179,7 +178,7 @@ extension DatabaseValueConvertible {
|
||||
} else {
|
||||
throw DatabaseError(resultCode: .SQLITE_ERROR, message: "could not convert database value \(dbv) to \(Self.self)", sql: statement.sql, arguments: arguments)
|
||||
}
|
||||
}
|
||||
})
|
||||
return try cursor.next() ?? nil
|
||||
}
|
||||
}
|
||||
@@ -341,7 +340,7 @@ extension Optional where Wrapped: DatabaseValueConvertible {
|
||||
public static func fetchCursor(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> DatabaseCursor<Wrapped?> {
|
||||
// Reuse a single mutable row for performance
|
||||
let row = try Row(statement: statement).adapted(with: adapter, layout: statement)
|
||||
return statement.fetchCursor(arguments: arguments) { () -> Wrapped? in
|
||||
return statement.cursor(arguments: arguments, next: {
|
||||
let dbv: DatabaseValue = row.value(atIndex: 0)
|
||||
if let value = Wrapped.fromDatabaseValue(dbv) {
|
||||
return value
|
||||
@@ -350,7 +349,7 @@ extension Optional where Wrapped: DatabaseValueConvertible {
|
||||
} else {
|
||||
throw DatabaseError(resultCode: .SQLITE_ERROR, message: "could not convert database value \(dbv) to \(Wrapped.self)", sql: statement.sql, arguments: arguments)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Returns an array of optional values fetched from a prepared statement.
|
||||
|
||||
+2
-2
@@ -527,7 +527,7 @@ extension Row {
|
||||
public static func fetchCursor(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> DatabaseCursor<Row> {
|
||||
// Reuse a single mutable row for performance
|
||||
let row = try Row(statement: statement).adapted(with: adapter, layout: statement)
|
||||
return statement.fetchCursor(arguments: arguments) { row }
|
||||
return statement.cursor(arguments: arguments, next: { row })
|
||||
}
|
||||
|
||||
/// Returns an array of rows fetched from a prepared statement.
|
||||
@@ -559,7 +559,7 @@ extension Row {
|
||||
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
|
||||
public static func fetchOne(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> Row? {
|
||||
// The cursor reuses a single mutable row. Return an immutable copy.
|
||||
return try fetchCursor(statement, arguments: arguments, adapter: adapter).next().flatMap { $0.copy() }
|
||||
return try fetchCursor(statement, arguments: arguments, adapter: adapter).next().map { $0.copy() }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -240,7 +240,7 @@ public final class SelectStatement : Statement {
|
||||
}
|
||||
|
||||
/// Creates a DatabaseCursor
|
||||
func fetchCursor<Element>(arguments: StatementArguments? = nil, element: @escaping () throws -> Element) -> DatabaseCursor<Element> {
|
||||
func cursor<Element>(arguments: StatementArguments? = nil, next element: @escaping () throws -> Element) -> DatabaseCursor<Element> {
|
||||
// Check that cursor is built on a valid queue.
|
||||
SchedulingWatchdog.preconditionValidQueue(database, "Database was not used on the correct thread.")
|
||||
|
||||
@@ -253,8 +253,8 @@ public final class SelectStatement : Statement {
|
||||
}
|
||||
|
||||
/// Creates a cursor whose results are ignored
|
||||
func fetchCursor(arguments: StatementArguments? = nil) -> DatabaseCursor<Void> {
|
||||
return fetchCursor(arguments: arguments) { }
|
||||
func cursor(arguments: StatementArguments? = nil) -> DatabaseCursor<Void> {
|
||||
return cursor(arguments: arguments) { }
|
||||
}
|
||||
|
||||
/// Information about the table and columns read by a SelectStatement
|
||||
|
||||
@@ -61,12 +61,12 @@ extension DatabaseValueConvertible where Self: StatementColumnConvertible {
|
||||
// We'll read from leftmost column at index 0, unless adapter mangles columns
|
||||
let columnIndex = try Int32(adapter?.baseColumnIndex(atIndex: 0, layout: statement) ?? 0)
|
||||
let sqliteStatement = statement.sqliteStatement
|
||||
return statement.fetchCursor(arguments: arguments) {
|
||||
return statement.cursor(arguments: arguments, next: {
|
||||
if sqlite3_column_type(sqliteStatement, columnIndex) == SQLITE_NULL {
|
||||
throw DatabaseError(resultCode: .SQLITE_ERROR, message: "could not convert database value NULL to \(Self.self)", sql: statement.sql, arguments: arguments)
|
||||
}
|
||||
return Self.init(sqliteStatement: sqliteStatement, index: columnIndex)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Returns an array of values fetched from a prepared statement.
|
||||
@@ -99,12 +99,12 @@ extension DatabaseValueConvertible where Self: StatementColumnConvertible {
|
||||
// We'll read from leftmost column at index 0, unless adapter mangles columns
|
||||
let columnIndex = try Int32(adapter?.baseColumnIndex(atIndex: 0, layout: statement) ?? 0)
|
||||
let sqliteStatement = statement.sqliteStatement
|
||||
let cursor = statement.fetchCursor(arguments: arguments) { () -> Self? in
|
||||
let cursor: DatabaseCursor<Self?> = statement.cursor(arguments: arguments, next: {
|
||||
if sqlite3_column_type(sqliteStatement, columnIndex) == SQLITE_NULL {
|
||||
return nil
|
||||
}
|
||||
return Self.init(sqliteStatement: sqliteStatement, index: columnIndex)
|
||||
}
|
||||
})
|
||||
return try cursor.next() ?? nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@
|
||||
}
|
||||
}
|
||||
try db.makeSelectStatement("SELECT * FROM documents WHERE documents MATCH ?")
|
||||
.fetchCursor(arguments: [rawPattern])
|
||||
.cursor(arguments: [rawPattern])
|
||||
.next() // error on next() for invalid patterns
|
||||
}
|
||||
} catch let error as DatabaseError {
|
||||
|
||||
@@ -67,11 +67,11 @@ extension RowConvertible {
|
||||
// It is the record's responsibility to copy the row if needed.
|
||||
// See Record.awakeFromFetch(), for example.
|
||||
let row = try Row(statement: statement).adapted(with: adapter, layout: statement)
|
||||
return statement.fetchCursor(arguments: arguments) {
|
||||
return statement.cursor(arguments: arguments, next: {
|
||||
var record = self.init(row: row)
|
||||
record.awakeFromFetch(row: row)
|
||||
return record
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Returns an array of records fetched from a prepared statement.
|
||||
|
||||
Reference in new issue
Block a user