Skip to content

Commit

Permalink
better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jkrukowski committed Aug 21, 2024
1 parent 51b4325 commit f88ae89
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let package = Package(
platforms: [
.iOS(.v13),
.watchOS(.v6),
.tvOS(.v13)
.tvOS(.v13),
],
products: [
.executable(
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ The [Swift Package Manager](https://www.swift.org/documentation/package-manager/

```swift
dependencies: [
.package(url: "https://github.com/jkrukowski/SQLiteVec", from: "0.0.4")
.package(url: "https://github.com/jkrukowski/SQLiteVec", from: "0.0.5")
]
```

Expand Down Expand Up @@ -51,20 +51,20 @@ try await db.execute("CREATE VIRTUAL TABLE vec_items USING vec0(embedding float[
for row in data {
try await db.execute(
"""
INSERT INTO vec_items(rowid, embedding)
INSERT INTO vec_items(rowid, embedding)
VALUES (?, ?)
""",
""",
params: [row.index, row.vector]
)
}

// query the embeddings
let result = try await db.query(
"""
SELECT rowid, distance
FROM vec_items
WHERE embedding MATCH ?
ORDER BY distance
SELECT rowid, distance
FROM vec_items
WHERE embedding MATCH ?
ORDER BY distance
LIMIT 3
""",
params: [query]
Expand Down
5 changes: 3 additions & 2 deletions Sources/SQLiteVec/Database.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public actor Database {
private func prepare(_ sql: String, params: [Any]) throws -> OpaquePointer {
var stmt: OpaquePointer?
try SQLiteVecError.check(
sqlite3_prepare_v2(_handle, sql, -1, &stmt, nil)
sqlite3_prepare_v2(_handle, sql, -1, &stmt, nil),
_handle
)
let paramCount = Int(sqlite3_bind_parameter_count(stmt))
guard paramCount == params.count else {
Expand All @@ -78,7 +79,7 @@ public actor Database {
default:
result = sqlite3_bind_null(stmt, Int32(index + 1))
}
try SQLiteVecError.check(result)
try SQLiteVecError.check(result, _handle)
}
return stmt!
}
Expand Down
34 changes: 31 additions & 3 deletions Sources/SQLiteVec/Models.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,48 @@ import Foundation

struct SQLiteVecError: Error {
let code: Int32
let message: String?

init(code: Int32) {
init(code: Int32, message: String?) {
self.code = code
self.message = message
}
}

extension SQLiteVecError: CustomStringConvertible {
var description: String {
if let message {
"Error \(code): \(message)"
} else {
"Error \(code)"
}
}
}

extension SQLiteVecError: CustomDebugStringConvertible {
var debugDescription: String {
description
}
}

extension SQLiteVecError {
static let successCodes: Set = [SQLITE_OK, SQLITE_ROW, SQLITE_DONE]

static func check(_ code: Int32) throws {
static func check(_ code: Int32, _ handle: OpaquePointer? = nil) throws {
if successCodes.contains(code) {
return
}
throw SQLiteVecError(code: code)
let message: String? =
if let handle {
String(cString: sqlite3_errmsg(handle))
} else {
nil
}
throw SQLiteVecError(code: code, message: message)
}

static func message(_ handle: OpaquePointer) -> String {
String(cString: sqlite3_errmsg(handle))
}
}

Expand Down
37 changes: 32 additions & 5 deletions Tests/SQLiteVecTests/DatabaseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,33 @@ final class DatabaseTests: XCTestCase {
XCTAssertEqual(result[0]["name"] as? String, "Jane")
}

func testSubQuery() async throws {
let db = try Database(.inMemory)
try await db.execute("""
CREATE TABLE users (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL UNIQUE
)
"""
)
try await db.execute("INSERT INTO users(id, name) VALUES (?, ?)", params: [1, "John"])
try await db.execute("INSERT INTO users(id, name) VALUES (?, ?)", params: [2, "Jane"])
try await db.execute("INSERT INTO users(id, name) VALUES (?, ?)", params: [3, "Jim"])

let result = try await db.query(
"""
SELECT * FROM (
SELECT * FROM users
WHERE name LIKE 'J%'
) as sub WHERE sub.id = ?
""",
params: [2]
)
XCTAssertEqual(result.count, 1)
XCTAssertEqual(result[0]["id"] as? Int, 2)
XCTAssertEqual(result[0]["name"] as? String, "Jane")
}

func testQuantizeBinary() async throws {
let db = try Database(.inMemory)
let result = try await db.query(
Expand Down Expand Up @@ -103,18 +130,18 @@ final class DatabaseTests: XCTestCase {
for row in data {
try await db.execute(
"""
INSERT INTO vec_items(rowid, embedding)
INSERT INTO vec_items(rowid, embedding)
VALUES (?, ?)
""",
params: [row.index, row.vector]
)
}
let result = try await db.query(
"""
SELECT rowid, distance
FROM vec_items
WHERE embedding MATCH ?
ORDER BY distance
SELECT rowid, distance
FROM vec_items
WHERE embedding MATCH ?
ORDER BY distance
LIMIT 3
""",
params: [query]
Expand Down

0 comments on commit f88ae89

Please sign in to comment.