Files
GRDB.swift/DESIGN.md
T
2015-09-07 22:02:01 +02:00

3.2 KiB

The Design of GRDB.swift

More than caveats or defects, there are a few glitches, or surprises in the GRDB.swift API. We try to explain them here. The interested readers can take them as Swift challenges!

  • Why is RowModel a class, when protocols are all the rage?

    Easy: Swift protocols don't provide super, and super is what turns RowModel into the flexible base class you need when you implement your specific needs:

    // A RowModel that makes sure its UUID is set before insertion:
    class A : RowModel {
        var UUID: NSString?
        override func insert(db: Database) throws {
            if UUID == nil {
                UUID = NSUUID().UUIDString
            }
            try super.insert()
        }
    }
    
    // A RowModel that validates itself before saving:
    class B : RowModel {
        override func insert(db: Database) throws {
            try validate()
            try super.insert(db)
        }
        override func update(db: Database) throws {
            try validate()
            try super.update(db)
        }
        func validate() throws {
            ...
        }
    }
    

    A second reason: the databaseEdited flag, which is true when a RowModel has unsaved changes, is easily provided by the base class RowModel. RowModel can manage its internal state accross method calls. Protocols could not provide this service without extra complexity.

    Yet, don't miss the RowConvertible and DatabaseMapping protocols: they provide fetching from custom SQL queries and fetching by primary key for free.

  • Why are DatabaseQueue.inTransaction() and DatabaseQueue.inDatabase() not reentrant?

    I, the library author, do not like database accesses to be hidden behind innocent-looking methods.

    For example, assuming a global dbQueue, let's compare:

    class Person {
        // Hidden database access:
        class func personsSortedByName() -> [Person] {
            return dbQueue.inDatabase { db in
                Person.fetchAll(db, "SELECT * FROM persons ORDER BY name")
            }
        }
        // Exposed database access:
        class func fetchPersonsSortedByName(db: Database) -> [Person] {
            return Person.fetchAll(db, "SELECT * FROM persons ORDER BY name")
        }
    }
    

    The first hides the database access, and will crash as soon as it is used inside a database block:

    dbQueue.inDatabase { db in
        // fatal error: DatabaseQueue.inDatabase(_:) was called reentrantly
        // on the same queue, which would lead to a deadlock.
        let persons = Person.personsSortedByName()
        ...
    }
    

    Sooner or later, it will have to be refactored into the second version:

    dbQueue.inDatabase { db in
        let persons = Person.personsSortedByName(db)
        ...
    }
    
  • Why must we provide query arguments in an Array, when Swift provides variadic method parameters?

    TODO