Skip to content

Commit e58e323

Browse files
committed
Support unsigned int32/64 types for columns. UInt64 is stored as a Blob type
1 parent e191c7d commit e58e323

File tree

4 files changed

+63
-3
lines changed

4 files changed

+63
-3
lines changed

Diff for: Sources/SQLite/Core/Value.swift

+35
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,27 @@ extension Int64: Number, Value {
7373

7474
}
7575

76+
extension UInt64: Number, Value {
77+
78+
public static let declaredDatatype = Blob.declaredDatatype
79+
80+
public static func fromDatatypeValue(_ datatypeValue: Blob) -> UInt64 {
81+
guard datatypeValue.bytes.count >= MemoryLayout<UInt64>.size else { return 0 }
82+
let bigEndianUInt64 = datatypeValue.bytes.withUnsafeBytes({ $0.load(as: UInt64.self )})
83+
return UInt64(bigEndian: bigEndianUInt64)
84+
}
85+
86+
public var datatypeValue: Blob {
87+
var bytes: [UInt8] = []
88+
withUnsafeBytes(of: self) { pointer in
89+
// little endian by default on iOS/macOS, so reverse to get bigEndian
90+
bytes.append(contentsOf: pointer.reversed())
91+
}
92+
return Blob(bytes: bytes)
93+
}
94+
95+
}
96+
7697
extension String: Binding, Value {
7798

7899
public static let declaredDatatype = "TEXT"
@@ -130,3 +151,17 @@ extension Int: Number, Value {
130151
}
131152

132153
}
154+
155+
extension UInt32: Number, Value {
156+
157+
public static var declaredDatatype = Int64.declaredDatatype
158+
159+
public static func fromDatatypeValue(_ datatypeValue: Int64) -> UInt32 {
160+
UInt32(datatypeValue)
161+
}
162+
163+
public var datatypeValue: Int64 {
164+
Int64(self)
165+
}
166+
167+
}

Diff for: Tests/SQLiteTests/SchemaTests.swift

+8
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ class SchemaTests: XCTestCase {
2727
"\"doubleOptional\" REAL, " +
2828
"\"int64\" INTEGER NOT NULL, " +
2929
"\"int64Optional\" INTEGER, " +
30+
"\"uint32\" INTEGER NOT NULL, " +
31+
"\"uint32Optional\" INTEGER, " +
32+
"\"uint64\" BLOB NOT NULL, " +
33+
"\"uint64Optional\" BLOB, " +
3034
"\"string\" TEXT NOT NULL, " +
3135
"\"stringOptional\" TEXT" +
3236
")",
@@ -37,6 +41,10 @@ class SchemaTests: XCTestCase {
3741
t.column(doubleOptional)
3842
t.column(int64)
3943
t.column(int64Optional)
44+
t.column(uint32)
45+
t.column(uint32Optional)
46+
t.column(uint64)
47+
t.column(uint64Optional)
4048
t.column(string)
4149
t.column(stringOptional)
4250
}

Diff for: Tests/SQLiteTests/SelectTests.swift

+14-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ class SelectTests: SQLiteTestCase {
1414
CREATE TABLE users_name (
1515
id INTEGER,
1616
user_id INTEGER REFERENCES users(id),
17-
name TEXT
17+
name TEXT,
18+
step_count BLOB,
19+
stair_count INTEGER
1820
)
1921
"""
2022
)
@@ -27,18 +29,27 @@ class SelectTests: SQLiteTestCase {
2729
let name = Expression<String>("name")
2830
let id = Expression<Int64>("id")
2931
let userID = Expression<Int64>("user_id")
32+
let stepCount = Expression<UInt64>("step_count")
33+
let stairCount = Expression<UInt32>("stair_count")
3034
let email = Expression<String>("email")
35+
// use UInt64.max - 1 to test Endianness - it should store/load as big endian
36+
let reallyBigNumber = UInt64.max - 1
37+
let prettyBigNumber = UInt32.max - 1
3138

3239
try! insertUser("Joey")
3340
try! db.run(usersData.insert(
3441
id <- 1,
3542
userID <- 1,
36-
name <- "Joey"
43+
name <- "Joey",
44+
stepCount <- reallyBigNumber,
45+
stairCount <- prettyBigNumber
3746
))
3847

39-
try! db.prepare(users.select(name, email).join(usersData, on: userID == users[id])).forEach {
48+
try! db.prepare(users.select(name, email, stepCount, stairCount).join(usersData, on: userID == users[id])).forEach {
4049
XCTAssertEqual($0[name], "Joey")
4150
XCTAssertEqual($0[email], "[email protected]")
51+
XCTAssertEqual($0[stepCount], reallyBigNumber)
52+
XCTAssertEqual($0[stairCount], prettyBigNumber)
4253
}
4354
}
4455
}

Diff for: Tests/SQLiteTests/TestHelpers.swift

+6
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ let intOptional = Expression<Int?>("intOptional")
9292
let int64 = Expression<Int64>("int64")
9393
let int64Optional = Expression<Int64?>("int64Optional")
9494

95+
let uint32 = Expression<Int>("uint32")
96+
let uint32Optional = Expression<Int?>("uint32Optional")
97+
98+
let uint64 = Expression<UInt64>("uint64")
99+
let uint64Optional = Expression<UInt64?>("uint64Optional")
100+
95101
let string = Expression<String>("string")
96102
let stringOptional = Expression<String?>("stringOptional")
97103

0 commit comments

Comments
 (0)