---
source: boltffi_bindgen/src/render/swift/templates.rs
expression: "render_class(&cls, \"boltffi\")"
---
public final class Database {
    let handle: OpaquePointer

    init(handle: OpaquePointer) {
        self.handle = handle
    }

    public init(path: String) {
        var path = path
        let ptr = path.withUTF8 { pathBuf -> OpaquePointer? in boltffi_database_open(pathBuf.baseAddress!, UInt(pathBuf.count)) }!
        self.handle = ptr
    }

    deinit {
        boltffi_database_free(handle)
    }

    public func query(sql: String) -> String {
        var sql = sql
        return sql.withUTF8 { sqlBuf in
            let buf = boltffi_database_query(handle, sqlBuf.baseAddress!, UInt(sqlBuf.count))
            defer { boltffi_free_buf(buf) }
            return boltffiDecodeOwnedBuf(buf.ptr, Int(buf.len)) { reader in reader.readString() }
        }
    }

}
