Skip to content

Commit

Permalink
Merge pull request #10 from adriencanterot/fix/unicode
Browse files Browse the repository at this point in the history
Fix unicode string length not being interpreted right by SQLite
  • Loading branch information
tanner0101 authored Oct 17, 2016
2 parents f23d43b + fbc30d8 commit 49288db
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Sources/SQLite/SQLite.swift
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ extension SQLite {
}

public func bind(_ value: String) throws {
let strlen = Int32(value.characters.count)
let strlen = Int32(value.utf8CString.count)

This comment has been minimized.

Copy link
@sroebert

sroebert Oct 19, 2016

Member

This change actually messes up quite a lot for me. I cannot properly read data using DB Browser for SQLite anymore, text is shown as blobs. Also filtering on ID's does not work correctly anymore. I'm not exactly sure what should be changed, but this does not seem like the correct solution.

This comment has been minimized.

Copy link
@tanner0101

tanner0101 Oct 24, 2016

Author Member

Are you sure this is related? This is only changing the count of the length of the string. Additionally, all of the tests continued to pass with this addition.

This comment has been minimized.

Copy link
@tanner0101

tanner0101 Oct 24, 2016

Author Member

Just saw the PR.

if sqlite3_bind_text(pointer, nextBindPosition, value, strlen, SQLITE_TRANSIENT) != SQLITE_OK {
throw SQLiteError.bind(database.errorMessage)
}
Expand All @@ -231,7 +231,7 @@ extension SQLite {
try bind(value ? 1 : 0)
}

//binds a null value, useful for inserting new rows without having to put an id

public func null() throws {
if sqlite3_bind_null(pointer, nextBindPosition) != SQLITE_OK {
throw SQLiteError.bind(database.errorMessage)
Expand Down
70 changes: 65 additions & 5 deletions Tests/SQLiteTests/SQLite3Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,71 @@ import XCTest
@testable import SQLite

class SQLite3Tests: XCTestCase {
static let allTests = [
("testReality", testReality)
]
static let allTests = [("testTables", testTables),
("testUnicode", testUnicode)]

func testReality() {
XCTAssert(2 + 2 == 4, "Something is seriously wrong.")
var database:SQLite!

override func setUp() {
self.database = SQLite.makeTestConnection()
}

func testTables() {
do {
try _ = database.execute("DROP TABLE IF EXISTS foo")
try _ = database.execute("CREATE TABLE foo (bar INT(4), baz VARCHAR(16))")
try _ = database.execute("INSERT INTO foo VALUES (42, 'Life')")
try _ = database.execute("INSERT INTO foo VALUES (1337, 'Elite')")
try _ = database.execute("INSERT INTO foo VALUES (9, NULL)")

if let resultBar = try database.execute("SELECT * FROM foo WHERE bar = 42").first {
XCTAssertEqual(resultBar.data["bar"], "42")
XCTAssertEqual(resultBar.data["baz"], "Life")
} else {
XCTFail("Could not get bar result")
}


if let resultBaz = try database.execute("SELECT * FROM foo where baz = 'Elite'").first {
XCTAssertEqual(resultBaz.data["bar"], "1337")
XCTAssertEqual(resultBaz.data["baz"], "Elite")
} else {
XCTFail("Could not get baz result")
}

if let resultBaz = try database.execute("SELECT * FROM foo where bar = 9").first {
XCTAssertEqual(resultBaz.data["bar"], "9")
XCTAssertEqual(resultBaz.data["baz"], nil)
} else {
XCTFail("Could not get null result")
}
} catch {
XCTFail("Testing tables failed: \(error)")
}
}

func testUnicode() {

do {

/**
This string includes characters from most Unicode categories
such as Latin, Latin-Extended-A/B, Cyrrilic, Greek etc.
*/
let unicode = "®¿ÐØ×ĞƋƢǂNJǕǮȐȘȢȱȵẀˍΔῴЖ♆"
try _ = database.execute("DROP TABLE IF EXISTS `foo`")
try _ = database.execute("CREATE TABLE `foo` (bar TEXT)")
try _ = database.execute("INSERT INTO `foo` VALUES(?)") { statement in
try statement.bind(unicode)
}

if let results = try database.execute("SELECT * FROM `foo`").first {
XCTAssertEqual(results.data["bar"], unicode)
}
} catch {
XCTFail(error.localizedDescription)
}

}

}
15 changes: 15 additions & 0 deletions Tests/SQLiteTests/Utilities.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import XCTest
@testable import SQLite

extension SQLite {
static func makeTestConnection() -> SQLite? {
do {
let sqlite = try SQLite(path:"test_database.sqlite")
return sqlite

} catch {
XCTFail()
}
return nil
}
}

0 comments on commit 49288db

Please sign in to comment.