Skip to content

Attach and detach #30

Closed
Closed
@macu

Description

@macu

Reading the documentation for this project I thought, "This is what Swift should be." I think it merits attach and detach methods (returning Bool or NSError). Some of the quirks to consider...

Working with FMDB, db.executeUpdate("ATTACH ? AS ?", withArgumentsInArray: [attachFilepath, attachAs]) is always returning true in my environment, perhaps everywhere. To determine success, I have to verify that the attached database is actually listed in the database_list pragma:

// Verify that the database is listed in the database_list pragma.
// http://www.sqlite.org/pragma.html#pragma_database_list
if let r = db.executeQuery("pragma database_list", withArgumentsInArray: []) {
    while r.next() {
        if let dbName = r.stringForColumnIndex(1) {
            if dbName == attachAs {
                return true
            }
        }
    }
}
return false

But even this wasn't sufficient. A zero-byte or corrupt database file could be attached, and would be listed in the database_list pragma, so I then test whether a query can be made against the expected tables:

/// Returns whether a query over the given table and columns may produce results.
func testQuery(#table: String, columns: [String]? = nil) -> Bool {
    let cols = (columns == nil) ? "*" : ",".join(columns!)
    let query = "SELECT \(cols) FROM \(table) LIMIT 1"
    // Result set will be nil if the query is invalid.
    return self.executeQuery(query, withArgumentsInArray: []) != nil
}

There may be a better way to determine the success of attach. If someone decides to implement attach and detach, I hope these notes will be useful. 👍

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions