Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add typeForColumn functions #818

Merged
merged 1 commit into from
Mar 25, 2021
Merged

Add typeForColumn functions #818

merged 1 commit into from
Mar 25, 2021

Commits on Mar 25, 2021

  1. Add typeForColumn functions

    * Add `typeForColumn` and `typeForColumnIndex` methods and tests
    * Update warnings in `dataForColumn` and `dataForColumnIndex` to describe SQLite behavior re zero-length blobs
    * Bump version to 2.7.8
    
    This should permit accurate handling of zero-length BLOBs, e.g., in Swift:
    
    ```swift
    let fileURL = URL(fileURLWithPath: NSTemporaryDirectory())
        .appendingPathComponent("test.sqlite")
    
    do {
        let db = FMDatabase(url: fileURL)
        db.open()
        try db.executeUpdate("CREATE TABLE test (data BLOB)", values: nil)
        let data = "foo".data(using: .utf8)!
        let sql = "INSERT INTO test (data) VALUES (?)"
        try db.executeUpdate(sql, values: [data])
        try db.executeUpdate(sql, values: [Data()])
        try db.executeUpdate(sql, values: [NSNull()])
    
        let rs = try db.executeQuery("SELECT * FROM test", values: nil)
        while rs.next() {
            let type = rs.type(forColumnIndex: 0)
            switch type {
            case .blob:
                if let data = rs.data(forColumnIndex: 0) {
                    print("blob with \(data.count) bytes")
                } else {
                    print("blob but no data")
                }
    
            case .null:
                print("Null")
    
            default:
                print("type is \(type)")
            }
        }
    } catch {
        print(error)
    }
    ```
    
    That will produce:
    
    ```
    blob with 3 bytes
    blob but no data
    Null
    ```
    robertmryan committed Mar 25, 2021
    Configuration menu
    Copy the full SHA
    c183a1f View commit details
    Browse the repository at this point in the history