mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-22 04:00:39 +00:00
Merge pull request #1885 from groue/dev/string-with-nul-character
Add support for Strings that contain the NUL character
This commit is contained in:
11 files changed
+463
-18
No files matched your search
@@ -105,8 +105,10 @@ public final class DatabaseFunction: Identifiable, Sendable {
|
||||
sqlite3_result_int64(context, int64)
|
||||
case .double(let double):
|
||||
sqlite3_result_double(context, double)
|
||||
case .string(let string):
|
||||
sqlite3_result_text(context, string, -1, SQLITE_TRANSIENT)
|
||||
case .string(var string):
|
||||
string.withSQLiteText {
|
||||
sqlite3_result_text(context, $0, $1, SQLITE_TRANSIENT)
|
||||
}
|
||||
case .blob(let data):
|
||||
data.withUnsafeBytes {
|
||||
sqlite3_result_blob(context, $0.baseAddress, CInt($0.count), SQLITE_TRANSIENT)
|
||||
@@ -447,8 +449,10 @@ public final class DatabaseFunction: Identifiable, Sendable {
|
||||
sqlite3_result_int64(sqliteContext, int64)
|
||||
case .double(let double):
|
||||
sqlite3_result_double(sqliteContext, double)
|
||||
case .string(let string):
|
||||
sqlite3_result_text(sqliteContext, string, -1, SQLITE_TRANSIENT)
|
||||
case .string(var string):
|
||||
string.withSQLiteText {
|
||||
sqlite3_result_text(sqliteContext, $0, $1, SQLITE_TRANSIENT)
|
||||
}
|
||||
case .blob(let data):
|
||||
data.withUnsafeBytes {
|
||||
sqlite3_result_blob(sqliteContext, $0.baseAddress, CInt($0.count), SQLITE_TRANSIENT)
|
||||
|
||||
@@ -147,7 +147,10 @@ public struct DatabaseValue: Hashable {
|
||||
case SQLITE_FLOAT:
|
||||
storage = .double(sqlite3_value_double(sqliteValue))
|
||||
case SQLITE_TEXT:
|
||||
storage = .string(String(cString: sqlite3_value_text(sqliteValue)!))
|
||||
let text = sqlite3_value_text(sqliteValue)
|
||||
let byteCount = Int(sqlite3_value_bytes(sqliteValue))
|
||||
let string = String(decoding: UnsafeBufferPointer(start: text, count: byteCount), as: UTF8.self)
|
||||
storage = .string(string)
|
||||
case SQLITE_BLOB:
|
||||
if let bytes = sqlite3_value_blob(sqliteValue) {
|
||||
let count = Int(sqlite3_value_bytes(sqliteValue))
|
||||
@@ -171,7 +174,8 @@ public struct DatabaseValue: Hashable {
|
||||
case SQLITE_FLOAT:
|
||||
storage = .double(sqlite3_column_double(sqliteStatement, index))
|
||||
case SQLITE_TEXT:
|
||||
storage = .string(String(cString: sqlite3_column_text(sqliteStatement, index)))
|
||||
let string = String(sqliteStatement: sqliteStatement, index: index)
|
||||
storage = .string(string)
|
||||
case SQLITE_BLOB:
|
||||
if let bytes = sqlite3_column_blob(sqliteStatement, index) {
|
||||
let count = Int(sqlite3_column_bytes(sqliteStatement, index))
|
||||
@@ -223,7 +227,7 @@ extension DatabaseValue: StatementBinding {
|
||||
let code = double.bind(to: sqliteStatement, at: index)
|
||||
try checkBindingSuccess(code: code, sqliteStatement: sqliteStatement)
|
||||
return try body()
|
||||
case .string(let string):
|
||||
case .string(var string):
|
||||
return try string.withBinding(to: sqliteStatement, at: index, do: body)
|
||||
case .blob(let data):
|
||||
return try data.withBinding(to: sqliteStatement, at: index, do: body)
|
||||
|
||||
@@ -56,9 +56,8 @@ extension Decimal: StatementColumnConvertible {
|
||||
case SQLITE_FLOAT:
|
||||
self.init(sqlite3_column_double(sqliteStatement, index))
|
||||
case SQLITE_TEXT:
|
||||
self.init(
|
||||
string: String(cString: sqlite3_column_text(sqliteStatement, index)!),
|
||||
locale: _posixLocale)
|
||||
let string = String(sqliteStatement: sqliteStatement, index: index)
|
||||
self.init(string: string, locale: _posixLocale)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -89,7 +89,7 @@ extension UUID: StatementColumnConvertible {
|
||||
public init?(sqliteStatement: SQLiteStatement, index: CInt) {
|
||||
switch sqlite3_column_type(sqliteStatement, index) {
|
||||
case SQLITE_TEXT:
|
||||
let string = String(cString: sqlite3_column_text(sqliteStatement, index)!)
|
||||
let string = String(sqliteStatement: sqliteStatement, index: index)
|
||||
guard let uuid = UUID(uuidString: string) else {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -592,7 +592,9 @@ extension String: DatabaseValueConvertible, StatementColumnConvertible {
|
||||
/// - sqliteStatement: A pointer to an SQLite statement.
|
||||
/// - index: The column index.
|
||||
public init(sqliteStatement: SQLiteStatement, index: CInt) {
|
||||
self = String(cString: sqlite3_column_text(sqliteStatement, index)!)
|
||||
let text = sqlite3_column_text(sqliteStatement, index)
|
||||
let byteCount = Int(sqlite3_column_bytes(sqliteStatement, index))
|
||||
self = String(decoding: UnsafeBufferPointer(start: text, count: byteCount), as: UTF8.self)
|
||||
}
|
||||
|
||||
/// Returns a TEXT database value.
|
||||
@@ -622,23 +624,57 @@ extension String: DatabaseValueConvertible, StatementColumnConvertible {
|
||||
}
|
||||
|
||||
public func bind(to sqliteStatement: SQLiteStatement, at index: CInt) -> CInt {
|
||||
sqlite3_bind_text(sqliteStatement, index, self, -1, SQLITE_TRANSIENT)
|
||||
var string = self
|
||||
return string.withSQLiteText {
|
||||
sqlite3_bind_text(sqliteStatement, index, $0, $1, SQLITE_TRANSIENT)
|
||||
}
|
||||
}
|
||||
|
||||
/// Calls the given closure after binding a statement argument.
|
||||
///
|
||||
/// The binding is valid only during the execution of this method.
|
||||
///
|
||||
/// This method is mutating because the string is made contiguous
|
||||
/// if necessary.
|
||||
///
|
||||
/// - parameter sqliteStatement: An SQLite statement.
|
||||
/// - parameter index: 1-based index to statement arguments.
|
||||
/// - parameter body: The closure to execute when argument is bound.
|
||||
func withBinding<T>(to sqliteStatement: SQLiteStatement, at index: CInt, do body: () throws -> T) throws -> T {
|
||||
try withCString {
|
||||
let code = sqlite3_bind_text(sqliteStatement, index, $0, -1, nil /* SQLITE_STATIC */)
|
||||
mutating func withBinding<T>(
|
||||
to sqliteStatement: SQLiteStatement,
|
||||
at index: CInt,
|
||||
do body: () throws -> T
|
||||
) throws -> T {
|
||||
try withSQLiteText {
|
||||
let code = sqlite3_bind_text(sqliteStatement, index, $0, $1, nil /* SQLITE_STATIC */)
|
||||
try checkBindingSuccess(code: code, sqliteStatement: sqliteStatement)
|
||||
return try body()
|
||||
}
|
||||
}
|
||||
|
||||
/// Runs body over the content of this string as an UTF8 string. This
|
||||
/// method exists because it deals with the ASCII NUL (\0) characters,
|
||||
/// unlike C strings.
|
||||
///
|
||||
/// This method is mutating because the string is made contiguous
|
||||
/// if necessary.
|
||||
mutating func withSQLiteText<T>(
|
||||
_ body: (UnsafePointer<CChar>, CInt) throws -> T
|
||||
) rethrows -> T {
|
||||
try withUTF8 { utf8 in
|
||||
guard let baseAddress = utf8.baseAddress else {
|
||||
// Empty string: yield a non-nil pointer to the NUL string
|
||||
// terminator, so that functions such as sqlite3_bind_text()
|
||||
// unambiguously interpret it as the empty string.
|
||||
return try withUnsafePointer(to: 0 as CChar) {
|
||||
try body($0, 0)
|
||||
}
|
||||
}
|
||||
return try baseAddress.withMemoryRebound(to: CChar.self, capacity: utf8.count) {
|
||||
try body($0, CInt(utf8.count))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -89,7 +89,20 @@ class DatabaseFunctionTests: GRDBTestCase {
|
||||
XCTAssertEqual(try String.fetchOne(db, sql: "SELECT f()")!, "foo")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func testFunctionReturningStringWithNULCharacter() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let fn = DatabaseFunction("f", argumentCount: 0) { dbValues in
|
||||
// SQLite supports strings that contain a NUL character:
|
||||
// https://sqlite.org/nulinstr.html
|
||||
return "foo\u{0}bar"
|
||||
}
|
||||
try dbQueue.inDatabase { db in
|
||||
db.add(function: fn)
|
||||
XCTAssertEqual(try String.fetchOne(db, sql: "SELECT f()")!, "foo\u{0}bar")
|
||||
}
|
||||
}
|
||||
|
||||
func testFunctionReturningData() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let fn = DatabaseFunction("f", argumentCount: 0) { dbValues in
|
||||
@@ -167,7 +180,22 @@ class DatabaseFunctionTests: GRDBTestCase {
|
||||
XCTAssertEqual(try String.fetchOne(db, sql: "SELECT f('foo')")!, "foo")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func testFunctionArgumentStringWithNULCharacter() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let fn = DatabaseFunction("f", argumentCount: 1) { (dbValues: [DatabaseValue]) in
|
||||
return String.fromDatabaseValue(dbValues[0])
|
||||
}
|
||||
try dbQueue.inDatabase { db in
|
||||
db.add(function: fn)
|
||||
// A NUL character can not be written in an SQL string literal:
|
||||
// the string is passed as a statement argument.
|
||||
XCTAssertEqual(
|
||||
try String.fetchOne(db, sql: "SELECT f(?)", arguments: ["foo\u{0}bar"])!,
|
||||
"foo\u{0}bar")
|
||||
}
|
||||
}
|
||||
|
||||
func testFunctionArgumentBlob() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let fn = DatabaseFunction("f", argumentCount: 1) { (dbValues: [DatabaseValue]) in
|
||||
|
||||
@@ -28,6 +28,8 @@ private let emojiData = emojiString.data(using: .utf8)
|
||||
private let nonUTF8Data = Data([0x80])
|
||||
private let invalidString = "\u{FFFD}" // decoded from nonUTF8Data
|
||||
private let jpegData = try! Data(contentsOf: testBundle.url(forResource: "Betty", withExtension: "jpeg")!)
|
||||
private let nulString = "foo\u{0}bar" // https://sqlite.org/nulinstr.html
|
||||
private let nulStringData = nulString.data(using: .utf8)
|
||||
|
||||
class DatabaseValueConversionTests : GRDBTestCase {
|
||||
|
||||
@@ -253,6 +255,22 @@ class DatabaseValueConversionTests : GRDBTestCase {
|
||||
return .rollback
|
||||
}
|
||||
|
||||
// nulString is turned to Text
|
||||
|
||||
try dbQueue.inTransaction { db in
|
||||
try db.execute(sql: "INSERT INTO `values` (textAffinity) VALUES (?)", arguments: [nulString])
|
||||
let sql = "SELECT textAffinity FROM `values`"
|
||||
XCTAssertEqual(try DatabaseValue.fetchOne(db, sql: sql)!.storageClass, .text)
|
||||
try assertDecoding(db, sql, Bool.self, expectedSQLiteConversion: false, expectedDatabaseValueConversion: nil)
|
||||
try assertDecoding(db, sql, Int.self, expectedSQLiteConversion:0, expectedDatabaseValueConversion: nil)
|
||||
try assertDecoding(db, sql, Int32.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
|
||||
try assertDecoding(db, sql, Int64.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
|
||||
try assertDecoding(db, sql, Double.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
|
||||
try assertDecoding(db, sql, String.self, expectedSQLiteConversion: nulString, expectedDatabaseValueConversion: nulString)
|
||||
try assertDecoding(db, sql, Data.self, expectedSQLiteConversion: nulStringData, expectedDatabaseValueConversion: nulStringData)
|
||||
return .rollback
|
||||
}
|
||||
|
||||
// emojiData is turned to Blob
|
||||
|
||||
try dbQueue.inTransaction { db in
|
||||
@@ -269,6 +287,22 @@ class DatabaseValueConversionTests : GRDBTestCase {
|
||||
return .rollback
|
||||
}
|
||||
|
||||
// nulStringData is turned to Blob
|
||||
|
||||
try dbQueue.inTransaction { db in
|
||||
try db.execute(sql: "INSERT INTO `values` (textAffinity) VALUES (?)", arguments: [nulStringData])
|
||||
let sql = "SELECT textAffinity FROM `values`"
|
||||
XCTAssertEqual(try DatabaseValue.fetchOne(db, sql: sql)!.storageClass, .blob)
|
||||
try assertDecoding(db, sql, Bool.self, expectedSQLiteConversion: false, expectedDatabaseValueConversion: nil)
|
||||
try assertDecoding(db, sql, Int.self, expectedSQLiteConversion:0, expectedDatabaseValueConversion: nil)
|
||||
try assertDecoding(db, sql, Int32.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
|
||||
try assertDecoding(db, sql, Int64.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
|
||||
try assertDecoding(db, sql, Double.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
|
||||
try assertDecoding(db, sql, String.self, expectedSQLiteConversion: nulString, expectedDatabaseValueConversion: nulString)
|
||||
try assertDecoding(db, sql, Data.self, expectedSQLiteConversion: nulStringData, expectedDatabaseValueConversion: nulStringData)
|
||||
return .rollback
|
||||
}
|
||||
|
||||
// nonUTF8Data is turned to Blob
|
||||
|
||||
try dbQueue.inTransaction { db in
|
||||
@@ -512,6 +546,22 @@ class DatabaseValueConversionTests : GRDBTestCase {
|
||||
return .rollback
|
||||
}
|
||||
|
||||
// nulString is turned to Text
|
||||
|
||||
try dbQueue.inTransaction { db in
|
||||
try db.execute(sql: "INSERT INTO `values` (realAffinity) VALUES (?)", arguments: [nulString])
|
||||
let sql = "SELECT realAffinity FROM `values`"
|
||||
XCTAssertEqual(try DatabaseValue.fetchOne(db, sql: sql)!.storageClass, .text)
|
||||
try assertDecoding(db, sql, Bool.self, expectedSQLiteConversion: false, expectedDatabaseValueConversion: nil)
|
||||
try assertDecoding(db, sql, Int.self, expectedSQLiteConversion:0, expectedDatabaseValueConversion: nil)
|
||||
try assertDecoding(db, sql, Int32.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
|
||||
try assertDecoding(db, sql, Int64.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
|
||||
try assertDecoding(db, sql, Double.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
|
||||
try assertDecoding(db, sql, String.self, expectedSQLiteConversion: nulString, expectedDatabaseValueConversion: nulString)
|
||||
try assertDecoding(db, sql, Data.self, expectedSQLiteConversion: nulStringData, expectedDatabaseValueConversion: nulStringData)
|
||||
return .rollback
|
||||
}
|
||||
|
||||
// emojiData is turned to Blob
|
||||
|
||||
try dbQueue.inTransaction { db in
|
||||
@@ -528,6 +578,22 @@ class DatabaseValueConversionTests : GRDBTestCase {
|
||||
return .rollback
|
||||
}
|
||||
|
||||
// nulStringData is turned to Blob
|
||||
|
||||
try dbQueue.inTransaction { db in
|
||||
try db.execute(sql: "INSERT INTO `values` (realAffinity) VALUES (?)", arguments: [nulStringData])
|
||||
let sql = "SELECT realAffinity FROM `values`"
|
||||
XCTAssertEqual(try DatabaseValue.fetchOne(db, sql: sql)!.storageClass, .blob)
|
||||
try assertDecoding(db, sql, Bool.self, expectedSQLiteConversion: false, expectedDatabaseValueConversion: nil)
|
||||
try assertDecoding(db, sql, Int.self, expectedSQLiteConversion:0, expectedDatabaseValueConversion: nil)
|
||||
try assertDecoding(db, sql, Int32.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
|
||||
try assertDecoding(db, sql, Int64.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
|
||||
try assertDecoding(db, sql, Double.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
|
||||
try assertDecoding(db, sql, String.self, expectedSQLiteConversion: nulString, expectedDatabaseValueConversion: nulString)
|
||||
try assertDecoding(db, sql, Data.self, expectedSQLiteConversion: nulStringData, expectedDatabaseValueConversion: nulStringData)
|
||||
return .rollback
|
||||
}
|
||||
|
||||
// nonUTF8Data is turned to Blob
|
||||
|
||||
try dbQueue.inTransaction { db in
|
||||
@@ -699,6 +765,22 @@ class DatabaseValueConversionTests : GRDBTestCase {
|
||||
return .rollback
|
||||
}
|
||||
|
||||
// nulString is turned to Text
|
||||
|
||||
try dbQueue.inTransaction { db in
|
||||
try db.execute(sql: "INSERT INTO `values` (noneAffinity) VALUES (?)", arguments: [nulString])
|
||||
let sql = "SELECT noneAffinity FROM `values`"
|
||||
XCTAssertEqual(try DatabaseValue.fetchOne(db, sql: sql)!.storageClass, .text)
|
||||
try assertDecoding(db, sql, Bool.self, expectedSQLiteConversion: false, expectedDatabaseValueConversion: nil)
|
||||
try assertDecoding(db, sql, Int.self, expectedSQLiteConversion:0, expectedDatabaseValueConversion: nil)
|
||||
try assertDecoding(db, sql, Int32.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
|
||||
try assertDecoding(db, sql, Int64.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
|
||||
try assertDecoding(db, sql, Double.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
|
||||
try assertDecoding(db, sql, String.self, expectedSQLiteConversion: nulString, expectedDatabaseValueConversion: nulString)
|
||||
try assertDecoding(db, sql, Data.self, expectedSQLiteConversion: nulStringData, expectedDatabaseValueConversion: nulStringData)
|
||||
return .rollback
|
||||
}
|
||||
|
||||
// emojiData is turned to Blob
|
||||
|
||||
try dbQueue.inTransaction { db in
|
||||
@@ -715,6 +797,22 @@ class DatabaseValueConversionTests : GRDBTestCase {
|
||||
return .rollback
|
||||
}
|
||||
|
||||
// nullStringData is turned to Blob
|
||||
|
||||
try dbQueue.inTransaction { db in
|
||||
try db.execute(sql: "INSERT INTO `values` (noneAffinity) VALUES (?)", arguments: [nulStringData])
|
||||
let sql = "SELECT noneAffinity FROM `values`"
|
||||
XCTAssertEqual(try DatabaseValue.fetchOne(db, sql: sql)!.storageClass, .blob)
|
||||
try assertDecoding(db, sql, Bool.self, expectedSQLiteConversion: false, expectedDatabaseValueConversion: nil)
|
||||
try assertDecoding(db, sql, Int.self, expectedSQLiteConversion:0, expectedDatabaseValueConversion: nil)
|
||||
try assertDecoding(db, sql, Int32.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
|
||||
try assertDecoding(db, sql, Int64.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
|
||||
try assertDecoding(db, sql, Double.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
|
||||
try assertDecoding(db, sql, String.self, expectedSQLiteConversion: nulString, expectedDatabaseValueConversion: nulString)
|
||||
try assertDecoding(db, sql, Data.self, expectedSQLiteConversion: nulStringData, expectedDatabaseValueConversion: nulStringData)
|
||||
return .rollback
|
||||
}
|
||||
|
||||
// nonUTF8Data is turned to Blob
|
||||
|
||||
try dbQueue.inTransaction { db in
|
||||
@@ -932,6 +1030,22 @@ class DatabaseValueConversionTests : GRDBTestCase {
|
||||
return .rollback
|
||||
}
|
||||
|
||||
// nulString is turned to Text
|
||||
|
||||
try dbQueue.inTransaction { db in
|
||||
try db.execute(sql: "INSERT INTO `values` (\(columnName)) VALUES (?)", arguments: [nulString])
|
||||
let sql = "SELECT \(columnName) FROM `values`"
|
||||
XCTAssertEqual(try DatabaseValue.fetchOne(db, sql: sql)!.storageClass, .text)
|
||||
try assertDecoding(db, sql, Bool.self, expectedSQLiteConversion: false, expectedDatabaseValueConversion: nil)
|
||||
try assertDecoding(db, sql, Int.self, expectedSQLiteConversion:0, expectedDatabaseValueConversion: nil)
|
||||
try assertDecoding(db, sql, Int32.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
|
||||
try assertDecoding(db, sql, Int64.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
|
||||
try assertDecoding(db, sql, Double.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
|
||||
try assertDecoding(db, sql, String.self, expectedSQLiteConversion: nulString, expectedDatabaseValueConversion: nulString)
|
||||
try assertDecoding(db, sql, Data.self, expectedSQLiteConversion: nulStringData, expectedDatabaseValueConversion: nulStringData)
|
||||
return .rollback
|
||||
}
|
||||
|
||||
// emojiData is turned to Blob
|
||||
|
||||
try dbQueue.inTransaction { db in
|
||||
@@ -948,6 +1062,22 @@ class DatabaseValueConversionTests : GRDBTestCase {
|
||||
return .rollback
|
||||
}
|
||||
|
||||
// nulStringData is turned to Blob
|
||||
|
||||
try dbQueue.inTransaction { db in
|
||||
try db.execute(sql: "INSERT INTO `values` (\(columnName)) VALUES (?)", arguments: [nulStringData])
|
||||
let sql = "SELECT \(columnName) FROM `values`"
|
||||
XCTAssertEqual(try DatabaseValue.fetchOne(db, sql: sql)!.storageClass, .blob)
|
||||
try assertDecoding(db, sql, Bool.self, expectedSQLiteConversion: false, expectedDatabaseValueConversion: nil)
|
||||
try assertDecoding(db, sql, Int.self, expectedSQLiteConversion:0, expectedDatabaseValueConversion: nil)
|
||||
try assertDecoding(db, sql, Int32.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
|
||||
try assertDecoding(db, sql, Int64.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
|
||||
try assertDecoding(db, sql, Double.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
|
||||
try assertDecoding(db, sql, String.self, expectedSQLiteConversion: nulString, expectedDatabaseValueConversion: nulString)
|
||||
try assertDecoding(db, sql, Data.self, expectedSQLiteConversion: nulStringData, expectedDatabaseValueConversion: nulStringData)
|
||||
return .rollback
|
||||
}
|
||||
|
||||
// nonUTF8Data is turned to Blob
|
||||
|
||||
try dbQueue.inTransaction { db in
|
||||
|
||||
@@ -485,6 +485,7 @@ class UpdateStatementTests : GRDBTestCase {
|
||||
}
|
||||
|
||||
try test(value: "Foo")
|
||||
try test(value: "foo\u{0}bar") // https://sqlite.org/nulinstr.html
|
||||
try test(value: "")
|
||||
try test(value: "Hello".data(using: .utf8)!)
|
||||
try test(value: Data())
|
||||
@@ -519,6 +520,7 @@ class UpdateStatementTests : GRDBTestCase {
|
||||
}
|
||||
|
||||
try test(value: "Foo")
|
||||
try test(value: "foo\u{0}bar") // https://sqlite.org/nulinstr.html
|
||||
try test(value: "")
|
||||
try test(value: "Hello".data(using: .utf8)!)
|
||||
try test(value: Data())
|
||||
@@ -560,6 +562,7 @@ class UpdateStatementTests : GRDBTestCase {
|
||||
}
|
||||
|
||||
try test(value: "Foo")
|
||||
try test(value: "foo\u{0}bar") // https://sqlite.org/nulinstr.html
|
||||
try test(value: "")
|
||||
try test(value: "Hello".data(using: .utf8)!)
|
||||
try test(value: Data())
|
||||
@@ -596,6 +599,7 @@ class UpdateStatementTests : GRDBTestCase {
|
||||
}
|
||||
|
||||
try test(value: "Foo")
|
||||
try test(value: "foo\u{0}bar") // https://sqlite.org/nulinstr.html
|
||||
try test(value: "")
|
||||
try test(value: "Hello".data(using: .utf8)!)
|
||||
try test(value: Data())
|
||||
|
||||
@@ -0,0 +1,172 @@
|
||||
import XCTest
|
||||
import GRDB
|
||||
|
||||
/// Here we test the decoding of string columns. The other fetch tests in
|
||||
/// this target use an all-INT model, and never decode a string.
|
||||
class FetchStringTests: XCTestCase {
|
||||
static let shortString = "foo"
|
||||
static let longString = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus consectetur felis eget nibh aliquet ullamcorper. Nam sodales, tellus a cursus tincidunt, arcu purus suscipit elit, nec congue erat ipsum a purus."
|
||||
|
||||
static let expectedRowCount = 200_000
|
||||
static let personRowCount = 100_000
|
||||
static let recordFetchesPerIteration = 5
|
||||
static let fetchesPerIteration = 10
|
||||
|
||||
private struct Person: Codable, FetchableRecord, PersistableRecord {
|
||||
static let databaseTableName = "person"
|
||||
|
||||
var firstName: String
|
||||
var lastName: String
|
||||
var email: String
|
||||
var address: String
|
||||
var age: Int
|
||||
var height: Double
|
||||
}
|
||||
|
||||
func test_shortString_fetch_performance() throws {
|
||||
try measureFetch(of: String.self, from: "short")
|
||||
}
|
||||
|
||||
func test_longString_fetch_performance() throws {
|
||||
try measureFetch(of: String.self, from: "long")
|
||||
}
|
||||
|
||||
func test_shortString_databaseValue_performance() throws {
|
||||
try measureFetch(of: DatabaseValue.self, from: "short")
|
||||
}
|
||||
|
||||
func test_longString_databaseValue_performance() throws {
|
||||
try measureFetch(of: DatabaseValue.self, from: "long")
|
||||
}
|
||||
|
||||
func test_shortString_as_argument() throws {
|
||||
let dbQueue = try DatabaseQueue()
|
||||
try dbQueue.write { db in
|
||||
let statement = try db.makeStatement(sql: "SELECT ? WHERE 0")
|
||||
measure {
|
||||
for _ in 0..<100_000 {
|
||||
try! statement.execute(arguments: [FetchStringTests.shortString])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_longString_as_argument() throws {
|
||||
let dbQueue = try DatabaseQueue()
|
||||
try dbQueue.write { db in
|
||||
let statement = try db.makeStatement(sql: "SELECT ? WHERE 0")
|
||||
measure {
|
||||
for _ in 0..<100_000 {
|
||||
try! statement.execute(arguments: [FetchStringTests.longString])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_record_fetch_performance() throws {
|
||||
let dbQueue = try makePersonDatabase()
|
||||
try dbQueue.read { db in
|
||||
measure {
|
||||
for _ in 0..<Self.recordFetchesPerIteration {
|
||||
let people = try! Person.fetchCursor(db)
|
||||
var count = 0
|
||||
while try! people.next() != nil {
|
||||
count += 1
|
||||
}
|
||||
XCTAssertEqual(count, Self.personRowCount)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func measureFetch<Value: DatabaseValueConvertible & StatementColumnConvertible>(
|
||||
of type: Value.Type,
|
||||
from column: String) throws
|
||||
{
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.read { db in
|
||||
measure {
|
||||
for _ in 0..<Self.fetchesPerIteration {
|
||||
let values = try! Value.fetchCursor(db, sql: "SELECT \(column) FROM item")
|
||||
var count = 0
|
||||
while try! values.next() != nil {
|
||||
count += 1
|
||||
}
|
||||
XCTAssertEqual(count, Self.expectedRowCount)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func measureFetch<Value: DatabaseValueConvertible>(
|
||||
of type: Value.Type,
|
||||
from column: String) throws
|
||||
{
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.read { db in
|
||||
measure {
|
||||
for _ in 0..<Self.fetchesPerIteration {
|
||||
let values = try! Value.fetchCursor(db, sql: "SELECT \(column) FROM item")
|
||||
var count = 0
|
||||
while try! values.next() != nil {
|
||||
count += 1
|
||||
}
|
||||
XCTAssertEqual(count, Self.expectedRowCount)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func makePersonDatabase() throws -> DatabaseQueue {
|
||||
let url = URL(fileURLWithPath: NSTemporaryDirectory())
|
||||
.appendingPathComponent("GRDBPerformancePeople.sqlite")
|
||||
let dbQueue = try DatabaseQueue(path: url.path)
|
||||
try dbQueue.write { db in
|
||||
if try db.tableExists("person"),
|
||||
try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM person") == Self.personRowCount
|
||||
{
|
||||
return
|
||||
}
|
||||
try db.execute(sql: "DROP TABLE IF EXISTS person")
|
||||
try db.create(table: "person") { table in
|
||||
table.column("firstName", .text)
|
||||
table.column("lastName", .text)
|
||||
table.column("email", .text)
|
||||
table.column("address", .text)
|
||||
table.column("age", .integer)
|
||||
table.column("height", .double)
|
||||
}
|
||||
let statement = try db.makeStatement(sql: """
|
||||
INSERT INTO person (firstName, lastName, email, address, age, height)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
""")
|
||||
for index in 0..<Self.personRowCount {
|
||||
try statement.execute(arguments: [
|
||||
"Arthur", "Dubois", "arthur.dubois@example.com",
|
||||
"12 Rue de la Paix, 75002 Paris", 20 + index % 60, 1.60 + Double(index % 40) / 100,
|
||||
])
|
||||
}
|
||||
}
|
||||
return dbQueue
|
||||
}
|
||||
|
||||
private func makeDatabaseQueue() throws -> DatabaseQueue {
|
||||
let url = URL(fileURLWithPath: NSTemporaryDirectory())
|
||||
.appendingPathComponent("GRDBPerformanceStrings.sqlite")
|
||||
let dbQueue = try DatabaseQueue(path: url.path)
|
||||
try dbQueue.write { db in
|
||||
if try db.tableExists("item"),
|
||||
try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM item") == Self.expectedRowCount
|
||||
{
|
||||
return
|
||||
}
|
||||
try db.execute(sql: "DROP TABLE IF EXISTS item")
|
||||
try db.execute(sql: "CREATE TABLE item (short TEXT, long TEXT)")
|
||||
let statement = try db.makeStatement(sql: "INSERT INTO item (short, long) VALUES (?, ?)")
|
||||
for _ in 0..<Self.expectedRowCount {
|
||||
try statement.execute(arguments: [FetchStringTests.shortString, FetchStringTests.longString])
|
||||
}
|
||||
}
|
||||
return dbQueue
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
import XCTest
|
||||
import GRDB
|
||||
|
||||
/// Here we test the return of string values from a custom SQL function.
|
||||
class FunctionResultTests: XCTestCase {
|
||||
|
||||
private let rowCount = 100_000
|
||||
private let queriesPerIteration = 20
|
||||
|
||||
var dbDirectoryPath: String!
|
||||
var dbQueue: DatabaseQueue!
|
||||
|
||||
static let echo = DatabaseFunction("echo", argumentCount: 1, pure: true) { dbValues in
|
||||
String.fromDatabaseValue(dbValues[0])
|
||||
}
|
||||
|
||||
override func setUpWithError() throws {
|
||||
let dbDirectoryName = "FunctionResultTests-\(ProcessInfo.processInfo.globallyUniqueString)"
|
||||
dbDirectoryPath = (NSTemporaryDirectory() as NSString).appendingPathComponent(dbDirectoryName)
|
||||
try FileManager.default.createDirectory(atPath: dbDirectoryPath, withIntermediateDirectories: true)
|
||||
let dbPath = (dbDirectoryPath as NSString).appendingPathComponent("db.sqlite")
|
||||
|
||||
var configuration = Configuration()
|
||||
configuration.prepareDatabase { db in
|
||||
db.add(function: Self.echo)
|
||||
}
|
||||
dbQueue = try DatabaseQueue(path: dbPath, configuration: configuration)
|
||||
}
|
||||
|
||||
override func tearDownWithError() throws {
|
||||
dbQueue = nil
|
||||
try FileManager.default.removeItem(atPath: dbDirectoryPath)
|
||||
}
|
||||
|
||||
func test_shortString_functionResult_performance() throws {
|
||||
try measureFunctionResults(over: ArgumentsTests.shortString)
|
||||
}
|
||||
|
||||
func test_longString_functionResult_performance() throws {
|
||||
try measureFunctionResults(over: ArgumentsTests.longString)
|
||||
}
|
||||
|
||||
private func measureFunctionResults(over string: String) throws {
|
||||
try dbQueue.write { db in
|
||||
try db.execute(sql: "CREATE TABLE t(a TEXT)")
|
||||
let statement = try db.makeStatement(sql: "INSERT INTO t(a) VALUES (?)")
|
||||
for _ in 0..<rowCount {
|
||||
try statement.execute(arguments: [string])
|
||||
}
|
||||
}
|
||||
|
||||
measure {
|
||||
try! dbQueue.read { db in
|
||||
for _ in 0..<queriesPerIteration {
|
||||
_ = try Int.fetchOne(db, sql: "SELECT COUNT(echo(a)) FROM t")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -31,6 +31,8 @@
|
||||
5690AFDC212058CB001530EA /* FetchRecordDecodableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5690AFDA212058CB001530EA /* FetchRecordDecodableTests.swift */; };
|
||||
56A3FEAA27A85F4800B0292E /* RealmSwift in Frameworks */ = {isa = PBXBuildFile; productRef = 56A3FEA927A85F4800B0292E /* RealmSwift */; };
|
||||
56A6F4AC29BBC8E200E22662 /* ArgumentsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A6F4AB29BBC8E200E22662 /* ArgumentsTests.swift */; };
|
||||
AA1F0001000000000000E001 /* FunctionResultTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA1F0002000000000000E002 /* FunctionResultTests.swift */; };
|
||||
AA1F0003000000000000E003 /* FetchStringTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA1F0004000000000000E004 /* FetchStringTests.swift */; };
|
||||
56B6D0E52618BF78003CC455 /* FetchRecordOptimizedTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56B6D0E42618BF78003CC455 /* FetchRecordOptimizedTests.swift */; };
|
||||
56B6D0E62618BF78003CC455 /* FetchRecordOptimizedTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56B6D0E42618BF78003CC455 /* FetchRecordOptimizedTests.swift */; };
|
||||
56B6D0EA2618C00C003CC455 /* InsertRecordOptimizedTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56B6D0E92618C00C003CC455 /* InsertRecordOptimizedTests.swift */; };
|
||||
@@ -100,6 +102,8 @@
|
||||
5690AFD72120589A001530EA /* InsertRecordEncodableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InsertRecordEncodableTests.swift; sourceTree = "<group>"; };
|
||||
5690AFDA212058CB001530EA /* FetchRecordDecodableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FetchRecordDecodableTests.swift; sourceTree = "<group>"; };
|
||||
56A6F4AB29BBC8E200E22662 /* ArgumentsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArgumentsTests.swift; sourceTree = "<group>"; };
|
||||
AA1F0002000000000000E002 /* FunctionResultTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FunctionResultTests.swift; sourceTree = "<group>"; };
|
||||
AA1F0004000000000000E004 /* FetchStringTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FetchStringTests.swift; sourceTree = "<group>"; };
|
||||
56B6D0E42618BF78003CC455 /* FetchRecordOptimizedTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FetchRecordOptimizedTests.swift; sourceTree = "<group>"; };
|
||||
56B6D0E92618C00C003CC455 /* InsertRecordOptimizedTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InsertRecordOptimizedTests.swift; sourceTree = "<group>"; };
|
||||
56B7DEC83063F1B30018462B /* StatementPreparationPerformanceTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatementPreparationPerformanceTests.swift; sourceTree = "<group>"; };
|
||||
@@ -165,6 +169,8 @@
|
||||
56BB862D1BA98933001F9168 /* GRDBPerformanceComparisonTests-Bridging.h */,
|
||||
56DE7B2D1C42B23B00861EB8 /* PerformanceModel.xcdatamodeld */,
|
||||
56A6F4AB29BBC8E200E22662 /* ArgumentsTests.swift */,
|
||||
AA1F0002000000000000E002 /* FunctionResultTests.swift */,
|
||||
AA1F0004000000000000E004 /* FetchStringTests.swift */,
|
||||
567071FA208A509C006AD95A /* DateParsingTests.swift */,
|
||||
56DE7B271C41302500861EB8 /* FetchNamedValuesTests.swift */,
|
||||
56DE7B291C4130AF00861EB8 /* FetchPositionalValuesTests.swift */,
|
||||
@@ -352,6 +358,8 @@
|
||||
5690AFD82120589A001530EA /* InsertRecordEncodableTests.swift in Sources */,
|
||||
56B6D0EA2618C00C003CC455 /* InsertRecordOptimizedTests.swift in Sources */,
|
||||
56A6F4AC29BBC8E200E22662 /* ArgumentsTests.swift in Sources */,
|
||||
AA1F0001000000000000E001 /* FunctionResultTests.swift in Sources */,
|
||||
AA1F0003000000000000E003 /* FetchStringTests.swift in Sources */,
|
||||
5690AFDB212058CB001530EA /* FetchRecordDecodableTests.swift in Sources */,
|
||||
56D507831F6D7B2E00AE1C5B /* InsertRecordStructTests.swift in Sources */,
|
||||
56DE7B241C412F7E00861EB8 /* InsertPositionalValuesTests.swift in Sources */,
|
||||
|
||||
Reference in new issue
Block a user