-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Feature/unsigned int #1091
base: master
Are you sure you want to change the base?
Feature/unsigned int #1091
Changes from all commits
e58e323
17d4b90
69bac35
14734e0
9a75697
6537109
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about Linux? Will this work everywhere? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah you're right, this would likely fail on non-macOS. i'll look for the right way to get defined bit order regardless of system. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the simplest way here would be withUnsafeBytes(of: bigEndian) { pointer in
bytes.append(contentsOf: pointer)
} however, why is it stored as big endian by default? given that it implies a conversion in most cases. |
||
bytes.append(contentsOf: pointer.reversed()) | ||
} | ||
return Blob(bytes: bytes) | ||
} | ||
|
||
} | ||
|
||
extension String: Binding, Value { | ||
|
||
public static let declaredDatatype = "TEXT" | ||
|
@@ -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) | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,31 +14,46 @@ 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 | ||
) | ||
""" | ||
) | ||
} | ||
|
||
func test_select_columns_from_multiple_tables() { | ||
func test_select_columns_from_multiple_tables() throws { | ||
let usersData = Table("users_name") | ||
let users = Table("users") | ||
|
||
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) | ||
} | ||
|
||
// ensure we can bind UInt64 and UInt32 | ||
_ = try db.run("SELECT * FROM \"users_name\" WHERE step_count = ? AND stair_count = ?", | ||
reallyBigNumber, prettyBigNumber) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wouldn't it be easier to change the iteration from
0
tobytes.count
to avoid having to doreversed
in the first place?