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

Conversation

robertmryan
Copy link
Collaborator

  • 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, as discussed in #73.

So, for example, if you want to delineate between NULL and a zero length BLOB, in 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

* 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
Copy link
Collaborator Author

FWIW, I have also removed the testVersionNumber test, as that is testing a deprecated method. In Xcode 12.4, at least, calling deprecated methods is now an error, rather than just a warning.

@ccgus ccgus merged commit c183a1f into ccgus:master Mar 25, 2021
@ccgus
Copy link
Owner

ccgus commented Mar 25, 2021

Danke!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants