Skip to content
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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Sources/SQLite/Core/Blob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,23 @@ extension Blob: Equatable {
public func ==(lhs: Blob, rhs: Blob) -> Bool {
lhs.bytes == rhs.bytes
}

extension Blob: Comparable {
public static func < (lhs: Blob, rhs: Blob) -> Bool {
// put most sig digit at the end
let lBytes: [UInt8] = lhs.bytes.reversed()
let rBytes: [UInt8] = rhs.bytes.reversed()

for byteIndex in stride(from: max(lhs.bytes.count, rhs.bytes.count) - 1, to: 0, by: -1) {
Copy link
Collaborator

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 to bytes.count to avoid having to do reversed in the first place?

let lVal = byteIndex < lBytes.count ? lBytes[byteIndex] : 0
let rVal = byteIndex < rBytes.count ? rBytes[byteIndex] : 0
if lVal < rVal {
return true
} else if lVal > rVal {
return false
}
}
return true // lhs.bytes == rhs.bytes
}

}
4 changes: 4 additions & 0 deletions Sources/SQLite/Core/Statement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ public final class Statement {
sqlite3_bind_double(handle, Int32(idx), value)
} else if let value = value as? Int64 {
sqlite3_bind_int64(handle, Int32(idx), value)
} else if let value = value as? UInt64 {
sqlite3_bind_blob(handle, Int32(idx), value.datatypeValue.bytes, Int32(value.datatypeValue.bytes.count), SQLITE_TRANSIENT)
} else if let value = value as? UInt32 {
sqlite3_bind_int64(handle, Int32(idx), value.datatypeValue)
} else if let value = value as? String {
sqlite3_bind_text(handle, Int32(idx), value, -1, SQLITE_TRANSIENT)
} else if let value = value as? Int {
Expand Down
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about Linux? Will this work everywhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator

Choose a reason for hiding this comment

The 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"
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)
}

}
2 changes: 2 additions & 0 deletions Tests/SQLiteTests/AggregateFunctionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class AggregateFunctionsTests: XCTestCase {
func test_max_wrapsComparableExpressionsWithMaxFunction() {
assertSQL("max(\"int\")", int.max)
assertSQL("max(\"intOptional\")", intOptional.max)
assertSQL("max(\"uint64\")", uint64.max)
assertSQL("max(\"uint64Optional\")", uint64Optional.max)
assertSQL("max(\"double\")", double.max)
assertSQL("max(\"doubleOptional\")", doubleOptional.max)
assertSQL("max(\"string\")", string.max)
Expand Down
13 changes: 13 additions & 0 deletions Tests/SQLiteTests/FoundationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,17 @@ class FoundationTests: XCTestCase {
let uuid = UUID.fromDatatypeValue(string)
XCTAssertEqual(UUID(uuidString: "4ABE10C9-FF12-4CD4-90C1-4B429001BAD3"), uuid)
}

func testCompareBlob() {
let data1 = Data([1, 2, 3])
let data2 = Data([1, 3, 3])
let data3 = Data([4, 3])
let blob1 = data1.datatypeValue
let blob2 = data2.datatypeValue
let blob3 = data3.datatypeValue
XCTAssert(blob1 < blob2)
XCTAssert(blob2 > blob1)
XCTAssert(blob1 > blob3)
XCTAssert(blob2 > blob3)
}
}
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
23 changes: 19 additions & 4 deletions Tests/SQLiteTests/SelectTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
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<UInt32>("uint32")
let uint32Optional = Expression<UInt32?>("uint32Optional")

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

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

Expand Down