Skip to content

Commit

Permalink
Support unsigned int32/64 types for columns. UInt64 is stored as a Bl…
Browse files Browse the repository at this point in the history
…ob type
  • Loading branch information
adamwulf committed Oct 20, 2021
1 parent 3d530f1 commit e625cdf
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 3 deletions.
35 changes: 35 additions & 0 deletions Sources/SQLite/Core/Value.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,27 @@ extension Int64: Number, Value {

}

extension UInt64: Number, Value {

public static let declaredDatatype = Blob.declaredDatatype

public static func fromDatatypeValue(_ datatypeValue: Blob) -> UInt64 {
guard datatypeValue.bytes.count >= MemoryLayout<UInt64>.size else { return 0 }
let bigEndianUInt64 = datatypeValue.bytes.withUnsafeBytes({ $0.load(as: UInt64.self )})
return UInt64(bigEndian: bigEndianUInt64)
}

public var datatypeValue: Blob {
var bytes: [UInt8] = []
withUnsafeBytes(of: self) { pointer in
// little endian by default on iOS/macOS, so reverse to get bigEndian
bytes.append(contentsOf: pointer.reversed())
}
return Blob(bytes: bytes)
}

}

extension String: Binding, Value {

public static let declaredDatatype = "TEXT"
Expand Down Expand Up @@ -130,3 +151,17 @@ extension Int: Number, Value {
}

}

extension UInt32: Number, Value {

public static var declaredDatatype = Int64.declaredDatatype

public static func fromDatatypeValue(_ datatypeValue: Int64) -> UInt32 {
UInt32(datatypeValue)
}

public var datatypeValue: Int64 {
Int64(self)
}

}
8 changes: 8 additions & 0 deletions Tests/SQLiteTests/SchemaTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class SchemaTests: XCTestCase {
"\"doubleOptional\" REAL, " +
"\"int64\" INTEGER NOT NULL, " +
"\"int64Optional\" INTEGER, " +
"\"uint32\" INTEGER NOT NULL, " +
"\"uint32Optional\" INTEGER, " +
"\"uint64\" BLOB NOT NULL, " +
"\"uint64Optional\" BLOB, " +
"\"string\" TEXT NOT NULL, " +
"\"stringOptional\" TEXT" +
")",
Expand All @@ -37,6 +41,10 @@ class SchemaTests: XCTestCase {
t.column(doubleOptional)
t.column(int64)
t.column(int64Optional)
t.column(uint32)
t.column(uint32Optional)
t.column(uint64)
t.column(uint64Optional)
t.column(string)
t.column(stringOptional)
}
Expand Down
17 changes: 14 additions & 3 deletions Tests/SQLiteTests/SelectTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ class SelectTests: SQLiteTestCase {
CREATE TABLE users_name (
id INTEGER,
user_id INTEGER REFERENCES users(id),
name TEXT
name TEXT,
step_count BLOB,
stair_count INTEGER
)
"""
)
Expand All @@ -27,18 +29,27 @@ class SelectTests: SQLiteTestCase {
let name = Expression<String>("name")
let id = Expression<Int64>("id")
let userID = Expression<Int64>("user_id")
let stepCount = Expression<UInt64>("step_count")
let stairCount = Expression<UInt32>("stair_count")
let email = Expression<String>("email")
// use UInt64.max - 1 to test Endianness - it should store/load as big endian
let reallyBigNumber = UInt64.max - 1
let prettyBigNumber = UInt32.max - 1

try! insertUser("Joey")
try! db.run(usersData.insert(
id <- 1,
userID <- 1,
name <- "Joey"
name <- "Joey",
stepCount <- reallyBigNumber,
stairCount <- prettyBigNumber
))

try! db.prepare(users.select(name, email).join(usersData, on: userID == users[id])).forEach {
try! db.prepare(users.select(name, email, stepCount, stairCount).join(usersData, on: userID == users[id])).forEach {
XCTAssertEqual($0[name], "Joey")
XCTAssertEqual($0[email], "[email protected]")
XCTAssertEqual($0[stepCount], reallyBigNumber)
XCTAssertEqual($0[stairCount], prettyBigNumber)
}
}
}
6 changes: 6 additions & 0 deletions Tests/SQLiteTests/TestHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ let intOptional = Expression<Int?>("intOptional")
let int64 = Expression<Int64>("int64")
let int64Optional = Expression<Int64?>("int64Optional")

let uint32 = Expression<Int>("uint32")
let uint32Optional = Expression<Int?>("uint32Optional")

let uint64 = Expression<UInt64>("uint64")
let uint64Optional = Expression<UInt64?>("uint64Optional")

let string = Expression<String>("string")
let stringOptional = Expression<String?>("stringOptional")

Expand Down

0 comments on commit e625cdf

Please sign in to comment.