Skip to content

Commit

Permalink
moved the extension to Swift.Data to follow the extension to Swift.St…
Browse files Browse the repository at this point in the history
…ring in the StandardLibrary.swift,

added case .blob(let data): return String(data: data, encoding: .utf8) to String.fromDatabaseValue
fixed tests
  • Loading branch information
Vlad Alexa committed Aug 14, 2018
1 parent d81475a commit 76f5adf
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 86 deletions.
8 changes: 0 additions & 8 deletions GRDB.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,6 @@
56873BF21F2CB400004D24B4 /* Fixits-1.2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56873BEB1F2CB400004D24B4 /* Fixits-1.2.swift */; };
5690C32A1D23E6D800E59934 /* FoundationDateComponentsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5690C3251D23E6D800E59934 /* FoundationDateComponentsTests.swift */; };
5690C33B1D23E7D200E59934 /* FoundationDateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5690C3361D23E7D200E59934 /* FoundationDateTests.swift */; };
5690C3401D23E82A00E59934 /* Data.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5690C33F1D23E82A00E59934 /* Data.swift */; };
5690C3431D23E82A00E59934 /* Data.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5690C33F1D23E82A00E59934 /* Data.swift */; };
569178491CED9B6000E179EA /* DatabaseQueueTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 569178451CED9B6000E179EA /* DatabaseQueueTests.swift */; };
569531091C9067DC00CF1A2B /* DatabaseQueueCrashTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 569530FB1C9067CC00CF1A2B /* DatabaseQueueCrashTests.swift */; };
5695310A1C9067DC00CF1A2B /* DatabaseValueConvertibleCrashTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 569530FC1C9067CC00CF1A2B /* DatabaseValueConvertibleCrashTests.swift */; };
Expand Down Expand Up @@ -673,7 +671,6 @@
56F3E7631E67F8C100BF0F01 /* Fixits-0.101.1.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56F3E7621E67F8C100BF0F01 /* Fixits-0.101.1.swift */; };
56F3E7661E67F8C100BF0F01 /* Fixits-0.101.1.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56F3E7621E67F8C100BF0F01 /* Fixits-0.101.1.swift */; };
56F3E7691E67F8C100BF0F01 /* Fixits-0.101.1.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56F3E7621E67F8C100BF0F01 /* Fixits-0.101.1.swift */; };
56F5ABD91D814330001F60CB /* Data.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5690C33F1D23E82A00E59934 /* Data.swift */; };
56F5ABDA1D814330001F60CB /* NSData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5657AAB81D107001006283EF /* NSData.swift */; };
56F5ABDC1D814330001F60CB /* URL.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5657AB0E1D10899D006283EF /* URL.swift */; };
56F5ABDD1D814330001F60CB /* UUID.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A8C22F1D1914540096E9D4 /* UUID.swift */; };
Expand Down Expand Up @@ -956,7 +953,6 @@
56873BEB1F2CB400004D24B4 /* Fixits-1.2.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Fixits-1.2.swift"; sourceTree = "<group>"; };
5690C3251D23E6D800E59934 /* FoundationDateComponentsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FoundationDateComponentsTests.swift; sourceTree = "<group>"; };
5690C3361D23E7D200E59934 /* FoundationDateTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FoundationDateTests.swift; sourceTree = "<group>"; };
5690C33F1D23E82A00E59934 /* Data.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Data.swift; sourceTree = "<group>"; };
569178451CED9B6000E179EA /* DatabaseQueueTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseQueueTests.swift; sourceTree = "<group>"; };
569530FB1C9067CC00CF1A2B /* DatabaseQueueCrashTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseQueueCrashTests.swift; sourceTree = "<group>"; };
569530FC1C9067CC00CF1A2B /* DatabaseValueConvertibleCrashTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseValueConvertibleCrashTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1217,7 +1213,6 @@
5605F14A1C672E4000235C62 /* Foundation */ = {
isa = PBXGroup;
children = (
5690C33F1D23E82A00E59934 /* Data.swift */,
5605F14C1C672E4000235C62 /* DatabaseDateComponents.swift */,
5674A7021F307FCD0095F066 /* DatabaseValueConvertible+ReferenceConvertible.swift */,
5605F14F1C672E4000235C62 /* Date.swift */,
Expand Down Expand Up @@ -2383,7 +2378,6 @@
565490C61D5AE236005622CB /* Statement.swift in Sources */,
56CEB5071EAA2F4D00BFAF62 /* FTS4.swift in Sources */,
56CEB4F71EAA2EFA00BFAF62 /* FetchableRecord.swift in Sources */,
56F5ABD91D814330001F60CB /* Data.swift in Sources */,
565490D41D5AE252005622CB /* (null) in Sources */,
566475D21D981D5E00FF74B8 /* SQLFunctions.swift in Sources */,
565490B61D5AE236005622CB /* Configuration.swift in Sources */,
Expand Down Expand Up @@ -2494,7 +2488,6 @@
5659F4931EA8D964004A4992 /* ReadWriteBox.swift in Sources */,
5605F1941C6B1A8700235C62 /* QueryInterfaceQuery.swift in Sources */,
56B7F43B1BEB42D500E39BBF /* Migration.swift in Sources */,
5690C3431D23E82A00E59934 /* Data.swift in Sources */,
5674A7001F307F600095F066 /* FetchableRecord+Decodable.swift in Sources */,
5674A6E91F307F0E0095F066 /* DatabaseValueConvertible+Decodable.swift in Sources */,
566B91161FA4C3F50012D5B0 /* DatabaseCollation.swift in Sources */,
Expand Down Expand Up @@ -2935,7 +2928,6 @@
5605F1931C6B1A8700235C62 /* QueryInterfaceQuery.swift in Sources */,
566B912B1FA4D0CC0012D5B0 /* StatementAuthorizer.swift in Sources */,
56A2388B1B9C75030082EB20 /* Statement.swift in Sources */,
5690C3401D23E82A00E59934 /* Data.swift in Sources */,
5659F4881EA8D94E004A4992 /* Utils.swift in Sources */,
56FC98781D969DEF00E3C842 /* SQLExpression+QueryInterface.swift in Sources */,
56A238931B9C750B0082EB20 /* DatabaseMigrator.swift in Sources */,
Expand Down
43 changes: 0 additions & 43 deletions GRDB/Core/Support/Foundation/Data.swift

This file was deleted.

36 changes: 32 additions & 4 deletions GRDB/Core/Support/StandardLibrary/StandardLibrary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import SQLite3
#endif

import Foundation

// MARK: - Value Types

/// Bool adopts DatabaseValueConvertible and StatementColumnConvertible.
Expand Down Expand Up @@ -456,10 +458,36 @@ extension String: DatabaseValueConvertible, StatementColumnConvertible {
/// Returns a String initialized from *dbValue*, if possible.
public static func fromDatabaseValue(_ dbValue: DatabaseValue) -> String? {
switch dbValue.storage {
case .string(let string):
return string
default:
return nil
case .blob(let data): return String(data: data, encoding: .utf8)
case .string(let string): return string
default: return nil
}
}
}

/// Data is convertible to and from DatabaseValue.
extension Data : DatabaseValueConvertible, StatementColumnConvertible {
public init(sqliteStatement: SQLiteStatement, index: Int32) {
if let bytes = sqlite3_column_blob(sqliteStatement, Int32(index)) {
let count = Int(sqlite3_column_bytes(sqliteStatement, Int32(index)))
self.init(bytes: bytes, count: count) // copy bytes
} else {
self.init()
}
}

/// Returns a value that can be stored in the database.
public var databaseValue: DatabaseValue {
return DatabaseValue(storage: .blob(self))
}

/// Returns a Data initialized from *dbValue*, if it contains
/// a Blob.
public static func fromDatabaseValue(_ dbValue: DatabaseValue) -> Data? {
switch dbValue.storage {
case .blob(let data): return data
case .string(let string): return string.data(using: .utf8)
default: return nil
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion GRDB/Record/FetchableRecord+Decodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private struct RowKeyedDecodingContainer<Key: CodingKey>: KeyedDecodingContainer
// because we're decoding a database value here (string, int, double, data, null)
// if we find a nested Decodable type then pass the string to JSON decoder
let value = try T(from: RowDecoder(row: row, codingPath: codingPath + [key]))
if let _ = value as? Codable {
if let _ = value as? Encodable {
// Support for nested ( Codable )
return try JSONDecoder().decode(type.self, from: row.dataNoCopy(named: key.stringValue)!)
} else {
Expand Down
6 changes: 0 additions & 6 deletions GRDBCipher.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -500,8 +500,6 @@
5690C3391D23E7D200E59934 /* FoundationDateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5690C3361D23E7D200E59934 /* FoundationDateTests.swift */; };
5690C33C1D23E7D200E59934 /* FoundationDateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5690C3361D23E7D200E59934 /* FoundationDateTests.swift */; };
5690C33D1D23E7D200E59934 /* FoundationDateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5690C3361D23E7D200E59934 /* FoundationDateTests.swift */; };
5690C3411D23E82A00E59934 /* Data.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5690C33F1D23E82A00E59934 /* Data.swift */; };
5690C3441D23E82A00E59934 /* Data.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5690C33F1D23E82A00E59934 /* Data.swift */; };
569178471CED9B6000E179EA /* DatabaseQueueTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 569178451CED9B6000E179EA /* DatabaseQueueTests.swift */; };
569178481CED9B6000E179EA /* DatabaseQueueTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 569178451CED9B6000E179EA /* DatabaseQueueTests.swift */; };
5691784A1CED9B6000E179EA /* DatabaseQueueTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 569178451CED9B6000E179EA /* DatabaseQueueTests.swift */; };
Expand Down Expand Up @@ -1057,7 +1055,6 @@
568E1CB81CB03847008D97A6 /* GRDBCipher.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = GRDBCipher.h; path = SQLCipher/GRDBCipher.h; sourceTree = "<group>"; };
5690C3251D23E6D800E59934 /* FoundationDateComponentsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FoundationDateComponentsTests.swift; sourceTree = "<group>"; };
5690C3361D23E7D200E59934 /* FoundationDateTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FoundationDateTests.swift; sourceTree = "<group>"; };
5690C33F1D23E82A00E59934 /* Data.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Data.swift; sourceTree = "<group>"; };
569178451CED9B6000E179EA /* DatabaseQueueTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseQueueTests.swift; sourceTree = "<group>"; };
5695311E1C907A8C00CF1A2B /* DatabaseSchemaCache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseSchemaCache.swift; sourceTree = "<group>"; };
569531231C90878D00CF1A2B /* DatabaseQueueSchemaCacheTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseQueueSchemaCacheTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1264,7 +1261,6 @@
5605F14A1C672E4000235C62 /* Foundation */ = {
isa = PBXGroup;
children = (
5690C33F1D23E82A00E59934 /* Data.swift */,
5605F14C1C672E4000235C62 /* DatabaseDateComponents.swift */,
5674A7021F307FCD0095F066 /* DatabaseValueConvertible+ReferenceConvertible.swift */,
5605F14F1C672E4000235C62 /* Date.swift */,
Expand Down Expand Up @@ -2206,7 +2202,6 @@
566B912C1FA4D0CC0012D5B0 /* StatementAuthorizer.swift in Sources */,
560FC5351CB003810014AA8E /* Statement.swift in Sources */,
5653EB9320961FC000F46237 /* AssociationQuery.swift in Sources */,
5690C3411D23E82A00E59934 /* Data.swift in Sources */,
5659F4891EA8D94E004A4992 /* Utils.swift in Sources */,
560FC5361CB003810014AA8E /* DatabaseMigrator.swift in Sources */,
560FC5371CB003810014AA8E /* DatabaseSchemaCache.swift in Sources */,
Expand Down Expand Up @@ -2641,7 +2636,6 @@
566B912F1FA4D0CC0012D5B0 /* StatementAuthorizer.swift in Sources */,
56AFCA0A1CB1A8BB00F48B96 /* Migration.swift in Sources */,
5653EB9420961FC000F46237 /* AssociationQuery.swift in Sources */,
5690C3441D23E82A00E59934 /* Data.swift in Sources */,
5659F48C1EA8D94E004A4992 /* Utils.swift in Sources */,
56AFCA0B1CB1A8BB00F48B96 /* Row.swift in Sources */,
56AFCA0C1CB1A8BB00F48B96 /* DatabaseSchemaCache.swift in Sources */,
Expand Down
6 changes: 0 additions & 6 deletions GRDBCustom.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,6 @@
5690C32D1D23E6D800E59934 /* FoundationDateComponentsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5690C3251D23E6D800E59934 /* FoundationDateComponentsTests.swift */; };
5690C33A1D23E7D200E59934 /* FoundationDateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5690C3361D23E7D200E59934 /* FoundationDateTests.swift */; };
5690C33E1D23E7D200E59934 /* FoundationDateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5690C3361D23E7D200E59934 /* FoundationDateTests.swift */; };
5690C3421D23E82A00E59934 /* Data.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5690C33F1D23E82A00E59934 /* Data.swift */; };
5690C3451D23E82A00E59934 /* Data.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5690C33F1D23E82A00E59934 /* Data.swift */; };
5698AC061D9B9FCF0056AF8C /* QueryInterfaceExtensibilityTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5698AC021D9B9FCF0056AF8C /* QueryInterfaceExtensibilityTests.swift */; };
5698AC0A1D9B9FCF0056AF8C /* QueryInterfaceExtensibilityTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5698AC021D9B9FCF0056AF8C /* QueryInterfaceExtensibilityTests.swift */; };
5698AC391D9E5A590056AF8C /* FTS3Pattern.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5698AC361D9E5A590056AF8C /* FTS3Pattern.swift */; };
Expand Down Expand Up @@ -721,7 +719,6 @@
56873BEB1F2CB400004D24B4 /* Fixits-1.2.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Fixits-1.2.swift"; sourceTree = "<group>"; };
5690C3251D23E6D800E59934 /* FoundationDateComponentsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FoundationDateComponentsTests.swift; sourceTree = "<group>"; };
5690C3361D23E7D200E59934 /* FoundationDateTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FoundationDateTests.swift; sourceTree = "<group>"; };
5690C33F1D23E82A00E59934 /* Data.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Data.swift; sourceTree = "<group>"; };
569178451CED9B6000E179EA /* DatabaseQueueTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseQueueTests.swift; sourceTree = "<group>"; };
5695311E1C907A8C00CF1A2B /* DatabaseSchemaCache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseSchemaCache.swift; sourceTree = "<group>"; };
569531231C90878D00CF1A2B /* DatabaseQueueSchemaCacheTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseQueueSchemaCacheTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -923,7 +920,6 @@
5605F14A1C672E4000235C62 /* Foundation */ = {
isa = PBXGroup;
children = (
5690C33F1D23E82A00E59934 /* Data.swift */,
5605F14C1C672E4000235C62 /* DatabaseDateComponents.swift */,
5674A7021F307FCD0095F066 /* DatabaseValueConvertible+ReferenceConvertible.swift */,
5605F14F1C672E4000235C62 /* Date.swift */,
Expand Down Expand Up @@ -1881,7 +1877,6 @@
F3BA80321CFB28A4003DC1BA /* FetchedRecordsController.swift in Sources */,
5659F48D1EA8D94E004A4992 /* Utils.swift in Sources */,
F3BA80231CFB288C003DC1BA /* NSString.swift in Sources */,
5690C3451D23E82A00E59934 /* Data.swift in Sources */,
56FC987D1D969DEF00E3C842 /* SQLExpression+QueryInterface.swift in Sources */,
F3BA80111CFB2876003DC1BA /* DatabaseSchemaCache.swift in Sources */,
F3BA80281CFB2891003DC1BA /* StandardLibrary.swift in Sources */,
Expand Down Expand Up @@ -2154,7 +2149,6 @@
F3BA808E1CFB2E7A003DC1BA /* FetchedRecordsController.swift in Sources */,
5659F48A1EA8D94E004A4992 /* Utils.swift in Sources */,
F3BA807F1CFB2E61003DC1BA /* NSString.swift in Sources */,
5690C3421D23E82A00E59934 /* Data.swift in Sources */,
56FC987A1D969DEF00E3C842 /* SQLExpression+QueryInterface.swift in Sources */,
F3BA806D1CFB2E55003DC1BA /* DatabaseSchemaCache.swift in Sources */,
F3BA80841CFB2E67003DC1BA /* StandardLibrary.swift in Sources */,
Expand Down
Loading

0 comments on commit 76f5adf

Please sign in to comment.