mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-29 04:24:21 +00:00
Remove support for text encodings
This commit is contained in:
1 parent
1f14cc35dd
commit
5ba06a74a0
6 files changed
-161
No files matched your search
@@ -20,8 +20,6 @@ Release Notes
|
||||
Person.filter(Column("email").like("%@example.com")).fetchAll(db)
|
||||
```
|
||||
|
||||
- `Configuration.textEncoding` let you specify the [text encoding](https://www.sqlite.org/pragma.html#pragma_encoding) of a database.
|
||||
|
||||
**Breaking Changes**
|
||||
|
||||
- The SQLForeignKeyAction, SQLColumnType, SQLConflictResolution, and SQLCollation types have been renamed Database.ForeignKeyAction, Database.ColumnType, Database.ConflictResolution, and Database.CollationName.
|
||||
|
||||
@@ -406,14 +406,6 @@
|
||||
566475D71D981D5E00FF74B8 /* SQLOperators.swift in Sources */ = {isa = PBXBuildFile; fileRef = 566475CB1D981D5E00FF74B8 /* SQLOperators.swift */; };
|
||||
566475D81D981D5E00FF74B8 /* SQLOperators.swift in Sources */ = {isa = PBXBuildFile; fileRef = 566475CB1D981D5E00FF74B8 /* SQLOperators.swift */; };
|
||||
566475D91D981D5E00FF74B8 /* SQLOperators.swift in Sources */ = {isa = PBXBuildFile; fileRef = 566475CB1D981D5E00FF74B8 /* SQLOperators.swift */; };
|
||||
566475DB1D98319F00FF74B8 /* TextEncodingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 566475DA1D98319F00FF74B8 /* TextEncodingTests.swift */; };
|
||||
566475DC1D98319F00FF74B8 /* TextEncodingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 566475DA1D98319F00FF74B8 /* TextEncodingTests.swift */; };
|
||||
566475DD1D98319F00FF74B8 /* TextEncodingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 566475DA1D98319F00FF74B8 /* TextEncodingTests.swift */; };
|
||||
566475DE1D98319F00FF74B8 /* TextEncodingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 566475DA1D98319F00FF74B8 /* TextEncodingTests.swift */; };
|
||||
566475DF1D98319F00FF74B8 /* TextEncodingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 566475DA1D98319F00FF74B8 /* TextEncodingTests.swift */; };
|
||||
566475E01D98319F00FF74B8 /* TextEncodingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 566475DA1D98319F00FF74B8 /* TextEncodingTests.swift */; };
|
||||
566475E11D98319F00FF74B8 /* TextEncodingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 566475DA1D98319F00FF74B8 /* TextEncodingTests.swift */; };
|
||||
566475E21D98319F00FF74B8 /* TextEncodingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 566475DA1D98319F00FF74B8 /* TextEncodingTests.swift */; };
|
||||
566AD8B21D5318F4002EC1A8 /* TableDefinition.swift in Sources */ = {isa = PBXBuildFile; fileRef = 566AD8B11D5318F4002EC1A8 /* TableDefinition.swift */; };
|
||||
566AD8B31D5318F4002EC1A8 /* TableDefinition.swift in Sources */ = {isa = PBXBuildFile; fileRef = 566AD8B11D5318F4002EC1A8 /* TableDefinition.swift */; };
|
||||
566AD8B41D5318F4002EC1A8 /* TableDefinition.swift in Sources */ = {isa = PBXBuildFile; fileRef = 566AD8B11D5318F4002EC1A8 /* TableDefinition.swift */; };
|
||||
@@ -1558,7 +1550,6 @@
|
||||
566475C11D981C5D00FF74B8 /* SQLSelectQuery.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLSelectQuery.swift; sourceTree = "<group>"; };
|
||||
566475CA1D981D5E00FF74B8 /* SQLFunctions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLFunctions.swift; sourceTree = "<group>"; };
|
||||
566475CB1D981D5E00FF74B8 /* SQLOperators.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLOperators.swift; sourceTree = "<group>"; };
|
||||
566475DA1D98319F00FF74B8 /* TextEncodingTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TextEncodingTests.swift; sourceTree = "<group>"; };
|
||||
566AD8B11D5318F4002EC1A8 /* TableDefinition.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TableDefinition.swift; sourceTree = "<group>"; };
|
||||
566AD8C51D531BE4002EC1A8 /* SQLTableBuilderTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLTableBuilderTests.swift; sourceTree = "<group>"; };
|
||||
567156151CB142AA007DC145 /* ReadOnlyDatabaseTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ReadOnlyDatabaseTests.swift; sourceTree = "<group>"; };
|
||||
@@ -2170,7 +2161,6 @@
|
||||
567A80521D41350C00C7DCEC /* IndexInfoTests.swift */,
|
||||
56A238141B9C74A90082EB20 /* InMemoryDatabaseTests.swift */,
|
||||
567156151CB142AA007DC145 /* ReadOnlyDatabaseTests.swift */,
|
||||
566475DA1D98319F00FF74B8 /* TextEncodingTests.swift */,
|
||||
);
|
||||
path = Database;
|
||||
sourceTree = "<group>";
|
||||
@@ -3490,7 +3480,6 @@
|
||||
560FC59C1CB00B880014AA8E /* StatementArguments+FoundationTests.swift in Sources */,
|
||||
5657AB671D108BA9006283EF /* URLTests.swift in Sources */,
|
||||
560FC59D1CB00B880014AA8E /* TableMapping+QueryInterfaceRequestTests.swift in Sources */,
|
||||
566475DC1D98319F00FF74B8 /* TextEncodingTests.swift in Sources */,
|
||||
5634B0741CF22BEF005360B9 /* FetchRequestTests.swift in Sources */,
|
||||
560FC59E1CB00B880014AA8E /* NSDateTests.swift in Sources */,
|
||||
560FC59F1CB00B880014AA8E /* DatabaseTests.swift in Sources */,
|
||||
@@ -3678,7 +3667,6 @@
|
||||
567156591CB16729007DC145 /* DatabaseTimestampTests.swift in Sources */,
|
||||
5671565A1CB16729007DC145 /* StatementArguments+FoundationTests.swift in Sources */,
|
||||
5657AB581D108BA9006283EF /* NSStringTests.swift in Sources */,
|
||||
566475DD1D98319F00FF74B8 /* TextEncodingTests.swift in Sources */,
|
||||
5671565B1CB16729007DC145 /* TableMapping+QueryInterfaceRequestTests.swift in Sources */,
|
||||
5671565C1CB16729007DC145 /* NSDateTests.swift in Sources */,
|
||||
5671565D1CB16729007DC145 /* DatabaseTests.swift in Sources */,
|
||||
@@ -3821,7 +3809,6 @@
|
||||
56AFCA581CB1AA9900F48B96 /* Row+FoundationTests.swift in Sources */,
|
||||
569178411CED8E0C00E179EA /* FetchedRecordsControllerTests.swift in Sources */,
|
||||
56AFCA5A1CB1AA9900F48B96 /* RecordSubClassTests.swift in Sources */,
|
||||
566475E01D98319F00FF74B8 /* TextEncodingTests.swift in Sources */,
|
||||
56A4CDB51D4234B200B1A9B9 /* SQLExpressionLiteralTests.swift in Sources */,
|
||||
56AFCA5B1CB1AA9900F48B96 /* DatabaseValueTests.swift in Sources */,
|
||||
56AFCA5C1CB1AA9900F48B96 /* ReadOnlyDatabaseTests.swift in Sources */,
|
||||
@@ -3927,7 +3914,6 @@
|
||||
56AFCAAF1CB1ABC800F48B96 /* DatabasePoolConcurrencyTests.swift in Sources */,
|
||||
56AFCAB01CB1ABC800F48B96 /* TransactionObserverTests.swift in Sources */,
|
||||
56AFCAB11CB1ABC800F48B96 /* Row+FoundationTests.swift in Sources */,
|
||||
566475E11D98319F00FF74B8 /* TextEncodingTests.swift in Sources */,
|
||||
56A4CDB61D4234B200B1A9B9 /* SQLExpressionLiteralTests.swift in Sources */,
|
||||
5657AB4C1D108BA9006283EF /* NSNullTests.swift in Sources */,
|
||||
56AFCAB31CB1ABC800F48B96 /* RecordSubClassTests.swift in Sources */,
|
||||
@@ -4121,7 +4107,6 @@
|
||||
56A238B71B9CA2590082EB20 /* DatabaseTimestampTests.swift in Sources */,
|
||||
565D5D751BBC70AE00DC9BD4 /* StatementArguments+FoundationTests.swift in Sources */,
|
||||
5657AB5A1D108BA9006283EF /* NSStringTests.swift in Sources */,
|
||||
566475DF1D98319F00FF74B8 /* TextEncodingTests.swift in Sources */,
|
||||
56300B6C1C53D3E8005A543B /* TableMapping+QueryInterfaceRequestTests.swift in Sources */,
|
||||
56F0B9921B6001C600A2F135 /* NSDateTests.swift in Sources */,
|
||||
56A238381B9C74A90082EB20 /* DatabaseTests.swift in Sources */,
|
||||
@@ -4192,7 +4177,6 @@
|
||||
56D496711D81309E008276D7 /* PrimaryKeySingleTests.swift in Sources */,
|
||||
56D496611D81304E008276D7 /* Row+FoundationTests.swift in Sources */,
|
||||
56D496C41D813767008276D7 /* DatabaseQueueTests.swift in Sources */,
|
||||
566475DB1D98319F00FF74B8 /* TextEncodingTests.swift in Sources */,
|
||||
56D496851D813147008276D7 /* StatementArgumentsTests.swift in Sources */,
|
||||
56D496B81D813465008276D7 /* DataMemoryTests.swift in Sources */,
|
||||
56D496541D812F5B008276D7 /* SQLExpressionLiteralTests.swift in Sources */,
|
||||
@@ -4449,7 +4433,6 @@
|
||||
F3BA80DB1CFB300E003DC1BA /* DatabaseValueConversionTests.swift in Sources */,
|
||||
5657AB5D1D108BA9006283EF /* NSStringTests.swift in Sources */,
|
||||
F3BA81241CFB3063003DC1BA /* PrimaryKeySingleWithReplaceConflictResolutionTests.swift in Sources */,
|
||||
566475E21D98319F00FF74B8 /* TextEncodingTests.swift in Sources */,
|
||||
F3BA80E81CFB3016003DC1BA /* DictionaryRowTests.swift in Sources */,
|
||||
F3BA80B41CFB2FC9003DC1BA /* ReadOnlyDatabaseTests.swift in Sources */,
|
||||
F3BA81011CFB3032003DC1BA /* CGFloatTests.swift in Sources */,
|
||||
@@ -4586,7 +4569,6 @@
|
||||
F3BA80F21CFB301A003DC1BA /* SavepointTests.swift in Sources */,
|
||||
F3BA80B81CFB2FCD003DC1BA /* DatabaseErrorTests.swift in Sources */,
|
||||
F3BA80FA1CFB3021003DC1BA /* SelectStatementTests.swift in Sources */,
|
||||
566475DE1D98319F00FF74B8 /* TextEncodingTests.swift in Sources */,
|
||||
F3BA81191CFB305F003DC1BA /* QueryInterfaceRequestTests.swift in Sources */,
|
||||
F3BA812F1CFB3064003DC1BA /* PrimaryKeyNoneTests.swift in Sources */,
|
||||
5690C33A1D23E7D200E59934 /* DateTests.swift in Sources */,
|
||||
|
||||
@@ -38,13 +38,6 @@ public struct Configuration {
|
||||
/// Default: nil
|
||||
public var trace: TraceFunction?
|
||||
|
||||
/// The text encoding used by the main database
|
||||
///
|
||||
/// Default: UTF-8
|
||||
///
|
||||
/// See https://www.sqlite.org/pragma.html#pragma_encoding
|
||||
public var textEncoding: Database.TextEncoding = .utf8
|
||||
|
||||
|
||||
// MARK: - Encryption
|
||||
|
||||
|
||||
@@ -179,37 +179,6 @@ public final class Database {
|
||||
public static let datetime = ColumnType("DATETIME")
|
||||
}
|
||||
|
||||
/// A database text encoding
|
||||
///
|
||||
/// See https://www.sqlite.org/pragma.html#pragma_encoding
|
||||
public struct TextEncoding : RawRepresentable, Hashable {
|
||||
public let rawValue: String
|
||||
|
||||
public init(rawValue: String) {
|
||||
self.rawValue = rawValue
|
||||
}
|
||||
|
||||
public init(_ rawValue: String) {
|
||||
self.rawValue = rawValue
|
||||
}
|
||||
|
||||
public var hashValue: Int {
|
||||
return rawValue.hashValue
|
||||
}
|
||||
|
||||
/// The `UTF-8` database encoding
|
||||
public static let utf8 = TextEncoding("UTF-8")
|
||||
|
||||
/// The `UTF-16` database encoding
|
||||
public static let utf16 = TextEncoding("UTF-16")
|
||||
|
||||
/// The `UTF-16le` database encoding
|
||||
public static let utf16le = TextEncoding("UTF-16le")
|
||||
|
||||
/// The `UTF-16be` database encoding
|
||||
public static let utf16be = TextEncoding("UTF-16be")
|
||||
}
|
||||
|
||||
|
||||
/// A foreign key action.
|
||||
///
|
||||
@@ -341,7 +310,6 @@ public final class Database {
|
||||
throw DatabaseError(code: code, message: String(cString: sqlite3_errmsg(sqliteConnection)))
|
||||
}
|
||||
|
||||
let textEncoding: TextEncoding
|
||||
do {
|
||||
#if SQLITE_HAS_CODEC
|
||||
if let passphrase = configuration.passphrase {
|
||||
@@ -361,50 +329,11 @@ public final class Database {
|
||||
throw DatabaseError(code: code, message: String(cString: sqlite3_errmsg(sqliteConnection)))
|
||||
}
|
||||
}
|
||||
|
||||
// Set database encoding, if not set yet
|
||||
do {
|
||||
// TODO: this would be much easier if we would disable SchedulingWatchDog
|
||||
let code = sqlite3_exec(sqliteConnection, "PRAGMA encoding = '\(configuration.textEncoding.rawValue)'", nil, nil, nil)
|
||||
guard code == SQLITE_OK else {
|
||||
throw DatabaseError(code: code, message: String(cString: sqlite3_errmsg(sqliteConnection)))
|
||||
}
|
||||
}
|
||||
|
||||
// Read database encoding, and update configuration
|
||||
do {
|
||||
// TODO: this would be much easier if we would disable SchedulingWatchDog
|
||||
var statement: SQLiteStatement? = nil
|
||||
let code = sqlite3_prepare_v2(sqliteConnection, "PRAGMA encoding", -1, &statement, nil)
|
||||
guard code == SQLITE_OK else {
|
||||
throw DatabaseError(code: code, message: String(cString: sqlite3_errmsg(sqliteConnection)))
|
||||
}
|
||||
defer {
|
||||
sqlite3_finalize(statement)
|
||||
}
|
||||
switch sqlite3_step(statement) {
|
||||
case SQLITE_DONE:
|
||||
// Weird. Don't crash but log
|
||||
NSLog("GRDB could not read text encoding of database at path: %@", NSString(string: path))
|
||||
textEncoding = configuration.textEncoding
|
||||
case SQLITE_ROW:
|
||||
textEncoding = TextEncoding(String(cString: sqlite3_column_text(statement, 0)!))
|
||||
case let code:
|
||||
throw DatabaseError(code: code, message: String(cString: sqlite3_errmsg(sqliteConnection)))
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
closeConnection(sqliteConnection!)
|
||||
throw error
|
||||
}
|
||||
|
||||
// Store real textEncoding in configuration
|
||||
if textEncoding != configuration.textEncoding {
|
||||
NSLog("GRDB warning: text encoding of database at path %@ is %@, not %@", NSString(string: path), NSString(string: textEncoding.rawValue), NSString(string: configuration.textEncoding.rawValue))
|
||||
}
|
||||
var configuration = configuration
|
||||
configuration.textEncoding = textEncoding
|
||||
|
||||
self.configuration = configuration
|
||||
self.schemaCache = schemaCache
|
||||
self.sqliteConnection = sqliteConnection!
|
||||
|
||||
@@ -339,7 +339,6 @@ var config = Configuration()
|
||||
config.readonly = true
|
||||
config.foreignKeysEnabled = true // Default is already true
|
||||
config.trace = { print($0) } // Prints all SQL statements
|
||||
config.textEncoding = .utf8 // Default is already UTF-8
|
||||
config.fileAttributes = [FileAttributeKey.protectionKey.rawValue: ...] // Configure database protection
|
||||
|
||||
let dbQueue = try DatabaseQueue(
|
||||
@@ -419,7 +418,6 @@ var config = Configuration()
|
||||
config.readonly = true
|
||||
config.foreignKeysEnabled = true // Default is already true
|
||||
config.trace = { print($0) } // Prints all SQL statements
|
||||
config.textEncoding = .utf8 // Default is already UTF-8
|
||||
config.fileAttributes = [FileAttributeKey.protectionKey.rawValue: ...] // Configure database protection
|
||||
config.maximumReaderCount = 10 // The default is 5
|
||||
|
||||
|
||||
@@ -1,61 +0,0 @@
|
||||
import XCTest
|
||||
#if USING_SQLCIPHER
|
||||
import GRDBCipher
|
||||
#elseif USING_CUSTOMSQLITE
|
||||
import GRDBCustomSQLite
|
||||
#else
|
||||
import GRDB
|
||||
#endif
|
||||
|
||||
extension Database.TextEncoding : DatabaseValueConvertible { }
|
||||
|
||||
class TextEncodingTests : GRDBTestCase {
|
||||
|
||||
func testDefaultEncoding() {
|
||||
assertNoError {
|
||||
dbConfiguration = Configuration()
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
dbQueue.inDatabase { db in
|
||||
let encoding = Database.TextEncoding.fetchOne(db, "PRAGMA encoding")
|
||||
XCTAssertEqual(encoding, .utf8)
|
||||
XCTAssertEqual(db.configuration.textEncoding, .utf8)
|
||||
}
|
||||
XCTAssertEqual(dbQueue.configuration.textEncoding, .utf8)
|
||||
}
|
||||
}
|
||||
|
||||
func testCustomEncoding() {
|
||||
assertNoError {
|
||||
dbConfiguration.textEncoding = .utf16le
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
dbQueue.inDatabase { db in
|
||||
let encoding = Database.TextEncoding.fetchOne(db, "PRAGMA encoding")
|
||||
XCTAssertEqual(encoding, .utf16le)
|
||||
XCTAssertEqual(db.configuration.textEncoding, .utf16le)
|
||||
}
|
||||
XCTAssertEqual(dbQueue.configuration.textEncoding, .utf16le)
|
||||
}
|
||||
}
|
||||
|
||||
func testDatabaseKnowsItsEncoding() {
|
||||
assertNoError {
|
||||
let dbQueue = try makeDatabaseQueue(filename: "db.sqlite")
|
||||
// Something has to be written so that the encoding is actually locked
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.create(table: "foo") { t in
|
||||
t.column("id", .integer)
|
||||
}
|
||||
}
|
||||
}
|
||||
assertNoError {
|
||||
dbConfiguration.textEncoding = .utf16le
|
||||
let dbQueue = try makeDatabaseQueue(filename: "db.sqlite")
|
||||
dbQueue.inDatabase { db in
|
||||
let encoding = Database.TextEncoding.fetchOne(db, "PRAGMA encoding")
|
||||
XCTAssertEqual(encoding, .utf8)
|
||||
XCTAssertEqual(db.configuration.textEncoding, .utf8)
|
||||
}
|
||||
XCTAssertEqual(dbQueue.configuration.textEncoding, .utf8)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user