diff --git a/GRDB/DatabaseQueue.swift b/GRDB/DatabaseQueue.swift index 50f7b4506..ec6c425cf 100644 --- a/GRDB/DatabaseQueue.swift +++ b/GRDB/DatabaseQueue.swift @@ -65,6 +65,34 @@ public final class DatabaseQueue { // MARK: - Database access + // IMPLEMENTATON NOTE + // + // When one sees code like: + // + // try dbQueue.inDatabase { db in + // try db.execute("INSERT ...") + // } + // + // One can wonder why there is no direct DatabaseQueue.execute() methods: + // + // // Look, Ma! No block! + // try dbQueue.execute("INSERT ...") + // + // Well, this is on purpose. Let's imagine what would happen if such method + // would exist: + // + // try dbQueue.execute("INSERT ...") + // let id = dbQueue.lastInsertedRowID + // + // Boom. The user feels like he can load the last inserted row ID, and he + // has just introduced a bug: between the two lines of code, some other + // statements may have been executed in other threads: the last inserted row + // ID may well be irrelevant. + // + // So until lastInsertedRowID is only accessible through the result of the + // statement execution, we don't provide any dbQueue.execute() function. And + // for consistency, no dbQueue.fetchXXX() method :-) + /** Executes a throwing block in the database queue. diff --git a/TODO.md b/TODO.md index ab230f37b..e1e082843 100644 --- a/TODO.md +++ b/TODO.md @@ -3,6 +3,5 @@ - [ ] db.fetch(RowModel.type, primaryKeys:[...]) // several primary keys - [ ] Investigate INSERT OR REPLACE - [ ] Add bindings to SQLiteError, so that one known the value that could not be updated/inserted. -- [ ] Document why execute and fetch methods will never be added to DatabaseQueue, and one always has to execute a block. It is because code like `dbQueue.execute("INSERT..."); let id = dbQueue.lastInsertedRowID` would be *unsafe*. - [ ] Pierlo complains that databaseDictionary returns a dictionary of values when updateFromDatabaseRow eats a Row. See if we can do something about that. - [ ] db.fetch(RowModel.type, primaryKey: Bindings) in unsafe because there are chances that array bindings don't match the order of primary key columns. Rethink fetching by primary key for RowModels with primary keys that span accross multiple columns.