mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
Support for Foundation.Decimal
This commit is contained in:
1 parent
e355af3584
commit
d9083a7bf5
7 files changed
+244
-24
No files matched your search
@@ -528,6 +528,13 @@
|
||||
5682D723239582AA004B58C4 /* DatabaseSuspensionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5682D71A239582AA004B58C4 /* DatabaseSuspensionTests.swift */; };
|
||||
56848973242DE36F002F9702 /* ValueObservationScheduler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56848972242DE36F002F9702 /* ValueObservationScheduler.swift */; };
|
||||
568735A21CEDE16C009B9116 /* Betty.jpeg in Resources */ = {isa = PBXBuildFile; fileRef = 5687359E1CEDE16C009B9116 /* Betty.jpeg */; };
|
||||
56894F752606576600268F4D /* FoundationDecimalTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56894F742606576600268F4D /* FoundationDecimalTests.swift */; };
|
||||
56894F762606576700268F4D /* FoundationDecimalTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56894F742606576600268F4D /* FoundationDecimalTests.swift */; };
|
||||
56894F772606576700268F4D /* FoundationDecimalTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56894F742606576600268F4D /* FoundationDecimalTests.swift */; };
|
||||
56894FB72606589700268F4D /* Decimal.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56894F94260657D600268F4D /* Decimal.swift */; };
|
||||
56894FC82606589B00268F4D /* Decimal.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56894F94260657D600268F4D /* Decimal.swift */; };
|
||||
56894FD12606589C00268F4D /* Decimal.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56894F94260657D600268F4D /* Decimal.swift */; };
|
||||
56894FD22606589D00268F4D /* Decimal.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56894F94260657D600268F4D /* Decimal.swift */; };
|
||||
568D131F2207213E00674B58 /* SQLQueryGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568D13182207213E00674B58 /* SQLQueryGenerator.swift */; };
|
||||
568D13202207213E00674B58 /* SQLQueryGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568D13182207213E00674B58 /* SQLQueryGenerator.swift */; };
|
||||
568D13212207213F00674B58 /* SQLQueryGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568D13182207213E00674B58 /* SQLQueryGenerator.swift */; };
|
||||
@@ -1436,6 +1443,8 @@
|
||||
5682D71A239582AA004B58C4 /* DatabaseSuspensionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseSuspensionTests.swift; sourceTree = "<group>"; };
|
||||
56848972242DE36F002F9702 /* ValueObservationScheduler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ValueObservationScheduler.swift; sourceTree = "<group>"; };
|
||||
5687359E1CEDE16C009B9116 /* Betty.jpeg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = Betty.jpeg; sourceTree = "<group>"; };
|
||||
56894F742606576600268F4D /* FoundationDecimalTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FoundationDecimalTests.swift; sourceTree = "<group>"; };
|
||||
56894F94260657D600268F4D /* Decimal.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Decimal.swift; sourceTree = "<group>"; };
|
||||
568D13182207213E00674B58 /* SQLQueryGenerator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLQueryGenerator.swift; sourceTree = "<group>"; };
|
||||
568ECA8925D7013000B71526 /* SQLSelection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SQLSelection.swift; sourceTree = "<group>"; };
|
||||
568ECA9E25D7E53E00B71526 /* SQLOrdering.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLOrdering.swift; sourceTree = "<group>"; };
|
||||
@@ -1689,13 +1698,14 @@
|
||||
5605F14C1C672E4000235C62 /* DatabaseDateComponents.swift */,
|
||||
5674A7021F307FCD0095F066 /* DatabaseValueConvertible+ReferenceConvertible.swift */,
|
||||
5605F14F1C672E4000235C62 /* Date.swift */,
|
||||
56894F94260657D600268F4D /* Decimal.swift */,
|
||||
5657AAB81D107001006283EF /* NSData.swift */,
|
||||
5605F1501C672E4000235C62 /* NSNull.swift */,
|
||||
5605F1511C672E4000235C62 /* NSNumber.swift */,
|
||||
5605F1521C672E4000235C62 /* NSString.swift */,
|
||||
C96C0F242084A442006B2981 /* SQLiteDateParser.swift */,
|
||||
5657AB0E1D10899D006283EF /* URL.swift */,
|
||||
56A8C22F1D1914540096E9D4 /* UUID.swift */,
|
||||
C96C0F242084A442006B2981 /* SQLiteDateParser.swift */,
|
||||
);
|
||||
path = Foundation;
|
||||
sourceTree = "<group>";
|
||||
@@ -2363,6 +2373,7 @@
|
||||
5657AB2F1D108BA9006283EF /* FoundationDataTests.swift */,
|
||||
5690C3251D23E6D800E59934 /* FoundationDateComponentsTests.swift */,
|
||||
5690C3361D23E7D200E59934 /* FoundationDateTests.swift */,
|
||||
56894F742606576600268F4D /* FoundationDecimalTests.swift */,
|
||||
5657AB301D108BA9006283EF /* FoundationNSDataTests.swift */,
|
||||
56F0B98E1B6001C600A2F135 /* FoundationNSDateTests.swift */,
|
||||
561667001D08A49900ADD404 /* FoundationNSDecimalNumberTests.swift */,
|
||||
@@ -2866,6 +2877,7 @@
|
||||
56F5ABDA1D814330001F60CB /* NSData.swift in Sources */,
|
||||
564F9C341F07611900877A00 /* DatabaseFunction.swift in Sources */,
|
||||
5659F4961EA8D964004A4992 /* ReadWriteBox.swift in Sources */,
|
||||
56894FD12606589C00268F4D /* Decimal.swift in Sources */,
|
||||
56172950223533F40006E219 /* EncodableRecord.swift in Sources */,
|
||||
56AE64142229A53700AD1B0B /* HasOneThroughAssociation.swift in Sources */,
|
||||
5613ED3721A95A5C00DC7A68 /* Map.swift in Sources */,
|
||||
@@ -3085,6 +3097,7 @@
|
||||
56D91AAA2205F2F100770D8D /* DatabasePromise.swift in Sources */,
|
||||
566BE71F2342542F00A8254B /* LockedBox.swift in Sources */,
|
||||
5698AC7B1DA37DCB0056AF8C /* VirtualTableModule.swift in Sources */,
|
||||
56894FC82606589B00268F4D /* Decimal.swift in Sources */,
|
||||
569D6DDF220EF9E100A058A9 /* SQLInterpolation.swift in Sources */,
|
||||
56A238821B9C75030082EB20 /* DatabaseError.swift in Sources */,
|
||||
5653EB0D20944C7C00F46237 /* HasManyAssociation.swift in Sources */,
|
||||
@@ -3137,6 +3150,7 @@
|
||||
563EF45421631E21007DAACD /* QueryInterfacePromiseTests.swift in Sources */,
|
||||
56A238501B9C74A90082EB20 /* RecordMinimalPrimaryKeyRowIDTests.swift in Sources */,
|
||||
564448871EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift in Sources */,
|
||||
56894F762606576700268F4D /* FoundationDecimalTests.swift in Sources */,
|
||||
56A238461B9C74A90082EB20 /* RowFromDictionaryTests.swift in Sources */,
|
||||
569C1EB51CF07DDD0042627B /* SchedulingWatchdogTests.swift in Sources */,
|
||||
562EA82A1F17B2AC00FA528C /* CompilationProtocolTests.swift in Sources */,
|
||||
@@ -3364,6 +3378,7 @@
|
||||
563EF45321631E21007DAACD /* QueryInterfacePromiseTests.swift in Sources */,
|
||||
56D4966D1D81309E008276D7 /* RecordMinimalPrimaryKeySingleTests.swift in Sources */,
|
||||
564448831EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift in Sources */,
|
||||
56894F752606576600268F4D /* FoundationDecimalTests.swift in Sources */,
|
||||
56D496811D813131008276D7 /* TransactionObserverSavepointsTests.swift in Sources */,
|
||||
56D496AB1D8132CA008276D7 /* DatabasePoolFunctionTests.swift in Sources */,
|
||||
562EA8261F17B2AC00FA528C /* CompilationProtocolTests.swift in Sources */,
|
||||
@@ -3669,6 +3684,7 @@
|
||||
AAA4DCCE230F1E0600C74B15 /* DatabasePromise.swift in Sources */,
|
||||
566BE7212342542F00A8254B /* LockedBox.swift in Sources */,
|
||||
AAA4DCCF230F1E0600C74B15 /* VirtualTableModule.swift in Sources */,
|
||||
56894FD22606589D00268F4D /* Decimal.swift in Sources */,
|
||||
AAA4DCD0230F1E0600C74B15 /* SQLInterpolation.swift in Sources */,
|
||||
AAA4DCD1230F1E0600C74B15 /* DatabaseError.swift in Sources */,
|
||||
AAA4DCD2230F1E0600C74B15 /* HasManyAssociation.swift in Sources */,
|
||||
@@ -3721,6 +3737,7 @@
|
||||
AAA4DD14230F262000C74B15 /* QueryInterfacePromiseTests.swift in Sources */,
|
||||
AAA4DD15230F262000C74B15 /* RecordMinimalPrimaryKeyRowIDTests.swift in Sources */,
|
||||
AAA4DD16230F262000C74B15 /* DatabaseAfterNextTransactionCommitTests.swift in Sources */,
|
||||
56894F772606576700268F4D /* FoundationDecimalTests.swift in Sources */,
|
||||
AAA4DD17230F262000C74B15 /* RowFromDictionaryTests.swift in Sources */,
|
||||
AAA4DD18230F262000C74B15 /* SchedulingWatchdogTests.swift in Sources */,
|
||||
AAA4DD19230F262000C74B15 /* CompilationProtocolTests.swift in Sources */,
|
||||
@@ -4026,6 +4043,7 @@
|
||||
568D131F2207213E00674B58 /* SQLQueryGenerator.swift in Sources */,
|
||||
5605F15D1C672E4000235C62 /* DatabaseDateComponents.swift in Sources */,
|
||||
567ECE4F2222E431009245CA /* GRDB-5.0.swift in Sources */,
|
||||
56894FB72606589700268F4D /* Decimal.swift in Sources */,
|
||||
56B964B91DA51D0A0002DA19 /* FTS5Pattern.swift in Sources */,
|
||||
56FBFEDA2210731A00945324 /* SQLRequest.swift in Sources */,
|
||||
563363C01C942C04000BE133 /* DatabaseReader.swift in Sources */,
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
#if !os(Linux)
|
||||
import Foundation
|
||||
|
||||
/// Decimal adopts DatabaseValueConvertible
|
||||
extension Decimal: DatabaseValueConvertible {
|
||||
public var databaseValue: DatabaseValue {
|
||||
NSDecimalNumber(decimal: self)
|
||||
.description(withLocale: Locale(identifier: "en_US_POSIX"))
|
||||
.databaseValue
|
||||
}
|
||||
|
||||
public static func fromDatabaseValue(_ dbValue: DatabaseValue) -> Self? {
|
||||
switch dbValue.storage {
|
||||
case .int64(let int64):
|
||||
return self.init(int64)
|
||||
case .double(let double):
|
||||
return self.init(double)
|
||||
case let .string(string):
|
||||
return self.init(string: string)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Decimal adopts StatementColumnConvertible
|
||||
extension Decimal: StatementColumnConvertible {
|
||||
public init?(sqliteStatement: SQLiteStatement, index: Int32) {
|
||||
switch sqlite3_column_type(sqliteStatement, index) {
|
||||
case SQLITE_INTEGER:
|
||||
self.init(sqlite3_column_int64(sqliteStatement, index))
|
||||
case SQLITE_FLOAT:
|
||||
self.init(sqlite3_column_double(sqliteStatement, index))
|
||||
case SQLITE_TEXT:
|
||||
self.init(string: String(cString: sqlite3_column_text(sqliteStatement, index)!))
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -72,16 +72,9 @@ extension NSNumber: DatabaseValueConvertible {
|
||||
case .double(let double):
|
||||
return self.init(value: double)
|
||||
case let .string(string):
|
||||
if let t = self as? NSDecimalNumber.Type {
|
||||
// NumberFormatter can't exactly decode "18446744073709551615"
|
||||
// NSDecimalNumber(string:) does.
|
||||
return (t.init(string: string) as! Self)
|
||||
} else {
|
||||
let formatter = NumberFormatter()
|
||||
formatter.numberStyle = .decimal
|
||||
formatter.locale = Locale(identifier: "en_US_POSIX")
|
||||
return formatter.number(from: string) as? Self
|
||||
}
|
||||
let number = NSDecimalNumber(string: string, locale: Locale(identifier: "en_US_POSIX"))
|
||||
if number == NSDecimalNumber.notANumber { return nil }
|
||||
return number as? Self
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -354,6 +354,10 @@
|
||||
567F45AF1F888B2600030B59 /* TruncateOptimizationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 567F45A71F888B2600030B59 /* TruncateOptimizationTests.swift */; };
|
||||
568068341EBBA26100EFB8AA /* SQLRequestTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568068301EBBA26100EFB8AA /* SQLRequestTests.swift */; };
|
||||
568068381EBBA26100EFB8AA /* SQLRequestTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568068301EBBA26100EFB8AA /* SQLRequestTests.swift */; };
|
||||
56894FE3260658A400268F4D /* Decimal.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56894FA0260657F600268F4D /* Decimal.swift */; };
|
||||
56894FE8260658A500268F4D /* Decimal.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56894FA0260657F600268F4D /* Decimal.swift */; };
|
||||
56894FF2260658E600268F4D /* FoundationDecimalTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56894FF1260658E600268F4D /* FoundationDecimalTests.swift */; };
|
||||
56894FF3260658E600268F4D /* FoundationDecimalTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56894FF1260658E600268F4D /* FoundationDecimalTests.swift */; };
|
||||
568ECB0D25D904CA00B71526 /* SQLSelection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568ECB0C25D904CA00B71526 /* SQLSelection.swift */; };
|
||||
568ECB0E25D904CA00B71526 /* SQLSelection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568ECB0C25D904CA00B71526 /* SQLSelection.swift */; };
|
||||
568ECB3525D91CEF00B71526 /* SQLSubquery.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568ECB3425D91CEE00B71526 /* SQLSubquery.swift */; };
|
||||
@@ -964,6 +968,8 @@
|
||||
567F45A71F888B2600030B59 /* TruncateOptimizationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TruncateOptimizationTests.swift; sourceTree = "<group>"; };
|
||||
568068301EBBA26100EFB8AA /* SQLRequestTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLRequestTests.swift; sourceTree = "<group>"; };
|
||||
5687359E1CEDE16C009B9116 /* Betty.jpeg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = Betty.jpeg; sourceTree = "<group>"; };
|
||||
56894FA0260657F600268F4D /* Decimal.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Decimal.swift; sourceTree = "<group>"; };
|
||||
56894FF1260658E600268F4D /* FoundationDecimalTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FoundationDecimalTests.swift; sourceTree = "<group>"; };
|
||||
568ECB0C25D904CA00B71526 /* SQLSelection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SQLSelection.swift; sourceTree = "<group>"; };
|
||||
568ECB3425D91CEE00B71526 /* SQLSubquery.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLSubquery.swift; sourceTree = "<group>"; };
|
||||
5690C3251D23E6D800E59934 /* FoundationDateComponentsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FoundationDateComponentsTests.swift; sourceTree = "<group>"; };
|
||||
@@ -1179,6 +1185,7 @@
|
||||
5605F14C1C672E4000235C62 /* DatabaseDateComponents.swift */,
|
||||
5674A7021F307FCD0095F066 /* DatabaseValueConvertible+ReferenceConvertible.swift */,
|
||||
5605F14F1C672E4000235C62 /* Date.swift */,
|
||||
56894FA0260657F600268F4D /* Decimal.swift */,
|
||||
5657AAB81D107001006283EF /* NSData.swift */,
|
||||
5605F1501C672E4000235C62 /* NSNull.swift */,
|
||||
5605F1511C672E4000235C62 /* NSNumber.swift */,
|
||||
@@ -1838,6 +1845,7 @@
|
||||
5657AB2F1D108BA9006283EF /* FoundationDataTests.swift */,
|
||||
5690C3251D23E6D800E59934 /* FoundationDateComponentsTests.swift */,
|
||||
5690C3361D23E7D200E59934 /* FoundationDateTests.swift */,
|
||||
56894FF1260658E600268F4D /* FoundationDecimalTests.swift */,
|
||||
5657AB301D108BA9006283EF /* FoundationNSDataTests.swift */,
|
||||
56F0B98E1B6001C600A2F135 /* FoundationNSDateTests.swift */,
|
||||
561667001D08A49900ADD404 /* FoundationNSDecimalNumberTests.swift */,
|
||||
@@ -2337,6 +2345,7 @@
|
||||
5611626C2576BF9D007AAF99 /* JoinAssociation.swift in Sources */,
|
||||
5656A8A82295BF44001FF3FF /* DatabasePromise.swift in Sources */,
|
||||
5656A86E2295BD56001FF3FF /* HasManyThroughAssociation.swift in Sources */,
|
||||
56894FE8260658A500268F4D /* Decimal.swift in Sources */,
|
||||
566BE7162342541F00A8254B /* LockedBox.swift in Sources */,
|
||||
F3BA80111CFB2876003DC1BA /* DatabaseSchemaCache.swift in Sources */,
|
||||
F3BA80281CFB2891003DC1BA /* StandardLibrary.swift in Sources */,
|
||||
@@ -2585,6 +2594,7 @@
|
||||
F3BA80501CFB2B59003DC1BA /* DatabasePoolSchemaCacheTests.swift in Sources */,
|
||||
F3BA80BB1CFB2FD1003DC1BA /* DatabasePoolConcurrencyTests.swift in Sources */,
|
||||
F3BA81141CFB305E003DC1BA /* QueryInterfaceRequestTests.swift in Sources */,
|
||||
56894FF3260658E600268F4D /* FoundationDecimalTests.swift in Sources */,
|
||||
5653EB7720961FB200F46237 /* AssociationHasOneSQLDerivationTests.swift in Sources */,
|
||||
F3BA81271CFB3063003DC1BA /* RecordEditedTests.swift in Sources */,
|
||||
56FF455D1D2CDA5200F21EF9 /* RecordUniqueIndexTests.swift in Sources */,
|
||||
@@ -2694,6 +2704,7 @@
|
||||
5611626B2576BF9D007AAF99 /* JoinAssociation.swift in Sources */,
|
||||
5656A8A72295BF44001FF3FF /* DatabasePromise.swift in Sources */,
|
||||
5656A86D2295BD56001FF3FF /* HasManyThroughAssociation.swift in Sources */,
|
||||
56894FE3260658A400268F4D /* Decimal.swift in Sources */,
|
||||
566BE7152342541F00A8254B /* LockedBox.swift in Sources */,
|
||||
F3BA806D1CFB2E55003DC1BA /* DatabaseSchemaCache.swift in Sources */,
|
||||
F3BA80841CFB2E67003DC1BA /* StandardLibrary.swift in Sources */,
|
||||
@@ -2942,6 +2953,7 @@
|
||||
F3BA811A1CFB305F003DC1BA /* Record+QueryInterfaceRequestTests.swift in Sources */,
|
||||
F3BA81111CFB3057003DC1BA /* StatementArguments+FoundationTests.swift in Sources */,
|
||||
F3BA81371CFB3064003DC1BA /* RecordInitializersTests.swift in Sources */,
|
||||
56894FF2260658E600268F4D /* FoundationDecimalTests.swift in Sources */,
|
||||
5653EB7620961FB200F46237 /* AssociationHasOneSQLDerivationTests.swift in Sources */,
|
||||
F3BA80C91CFB2FD8003DC1BA /* DatabaseQueueBackupTests.swift in Sources */,
|
||||
56AF746E1D41FB9C005E9FF3 /* DatabaseValueConvertibleEscapingTests.swift in Sources */,
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
import XCTest
|
||||
import GRDB
|
||||
|
||||
class FoundationDecimalTests: GRDBTestCase {
|
||||
|
||||
func testDecimalIsEncodedAsString() throws {
|
||||
XCTAssertEqual(Decimal(string: "0")!.databaseValue, "0".databaseValue)
|
||||
XCTAssertEqual(Decimal(string: "0.5")!.databaseValue, "0.5".databaseValue)
|
||||
XCTAssertEqual(Decimal(string: "1")!.databaseValue, "1".databaseValue)
|
||||
XCTAssertEqual(Decimal(string: "-1")!.databaseValue, "-1".databaseValue)
|
||||
XCTAssertEqual(Decimal(string: "9223372036854775807")!.databaseValue, "9223372036854775807".databaseValue)
|
||||
XCTAssertEqual(Decimal(string: "9223372036854775806")!.databaseValue, "9223372036854775806".databaseValue)
|
||||
XCTAssertEqual(Decimal(string: "-9223372036854775807")!.databaseValue, "-9223372036854775807".databaseValue)
|
||||
XCTAssertEqual(Decimal(string: "-9223372036854775808")!.databaseValue, "-9223372036854775808".databaseValue)
|
||||
XCTAssertEqual(Decimal(string: "18446744073709551615")!.databaseValue, "18446744073709551615".databaseValue)
|
||||
}
|
||||
|
||||
func testDecimalDatabaseRoundTrip() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
func roundTrip(_ value: Decimal) throws -> Bool {
|
||||
guard let back = try dbQueue.inDatabase({ try Decimal.fetchOne($0, sql: "SELECT ?", arguments: [value]) }) else {
|
||||
XCTFail()
|
||||
return false
|
||||
}
|
||||
return back == value
|
||||
}
|
||||
|
||||
XCTAssertTrue(try roundTrip(Decimal(Int32.min + 1)))
|
||||
XCTAssertTrue(try roundTrip(Decimal(Double(10000000.01))))
|
||||
}
|
||||
|
||||
func testDecimalDatabaseValueRoundTrip() {
|
||||
func roundTrip(_ value: Decimal) -> Bool
|
||||
{
|
||||
let dbValue = value.databaseValue
|
||||
guard let back = Decimal.fromDatabaseValue(dbValue) else
|
||||
{
|
||||
XCTFail("Failed to convert from DatabaseValue to Decimal")
|
||||
return false
|
||||
}
|
||||
return back.isEqual(to: value)
|
||||
}
|
||||
|
||||
XCTAssertTrue(roundTrip(Decimal(Int32.min + 1)))
|
||||
XCTAssertTrue(roundTrip(Decimal(Double(10000000.01))))
|
||||
}
|
||||
|
||||
func testDecimalFromDatabaseValueFailure() {
|
||||
let databaseValue_Null = DatabaseValue.null
|
||||
let databaseValue_String = "foo".databaseValue
|
||||
let databaseValue_Blob = "bar".data(using: .utf8)!.databaseValue
|
||||
XCTAssertNil(Decimal.fromDatabaseValue(databaseValue_Null))
|
||||
XCTAssertNil(Decimal.fromDatabaseValue(databaseValue_String))
|
||||
XCTAssertNil(Decimal.fromDatabaseValue(databaseValue_Blob))
|
||||
}
|
||||
|
||||
func testDecimalDecodingFromInt64() throws {
|
||||
func test(_ value: Int64, isDecodedAs number: Decimal) throws {
|
||||
XCTAssertEqual(Decimal(value), number)
|
||||
|
||||
let decodedFromDatabaseValue = Decimal.fromDatabaseValue(value.databaseValue)
|
||||
XCTAssertEqual(decodedFromDatabaseValue, number)
|
||||
|
||||
let decodedFromDatabase = try DatabaseQueue().read { db in
|
||||
try Decimal.fetchOne(db, sql: "SELECT ?", arguments: [value])
|
||||
}
|
||||
XCTAssertEqual(decodedFromDatabase, number)
|
||||
}
|
||||
try test(0, isDecodedAs: Decimal(string: "0")!)
|
||||
try test(1, isDecodedAs: Decimal(string: "1")!)
|
||||
try test(-1, isDecodedAs: Decimal(string: "-1")!)
|
||||
try test(9223372036854775807, isDecodedAs: Decimal(string: "9223372036854775807")!)
|
||||
try test(9223372036854775806, isDecodedAs: Decimal(string: "9223372036854775806")!)
|
||||
try test(-9223372036854775807, isDecodedAs: Decimal(string: "-9223372036854775807")!)
|
||||
try test(-9223372036854775808, isDecodedAs: Decimal(string: "-9223372036854775808")!)
|
||||
}
|
||||
|
||||
func testDecimalDecodingFromDouble() throws {
|
||||
func test(_ value: Double, isDecodedAs number: Decimal) throws {
|
||||
XCTAssertEqual(Decimal(value), number)
|
||||
|
||||
let decodedFromDatabaseValue = Decimal.fromDatabaseValue(value.databaseValue)
|
||||
XCTAssertEqual(decodedFromDatabaseValue, number)
|
||||
|
||||
let decodedFromDatabase = try DatabaseQueue().read { db in
|
||||
try Decimal.fetchOne(db, sql: "SELECT ?", arguments: [value])
|
||||
}
|
||||
XCTAssertEqual(decodedFromDatabase, number)
|
||||
}
|
||||
try test(0, isDecodedAs: Decimal(string: "0")!)
|
||||
try test(0.25, isDecodedAs: Decimal(string: "0.25")!)
|
||||
try test(1, isDecodedAs: Decimal(string: "1")!)
|
||||
try test(-1, isDecodedAs: Decimal(string: "-1")!)
|
||||
}
|
||||
|
||||
func testDecimalDecodingFromText() throws {
|
||||
func test(_ value: String, isDecodedAs number: Decimal) throws {
|
||||
XCTAssertEqual(Decimal(string: value), number)
|
||||
|
||||
let decodedFromDatabaseValue = Decimal.fromDatabaseValue(value.databaseValue)
|
||||
XCTAssertEqual(decodedFromDatabaseValue, number)
|
||||
|
||||
let decodedFromDatabase = try DatabaseQueue().read { db in
|
||||
try Decimal.fetchOne(db, sql: "SELECT ?", arguments: [value])
|
||||
}
|
||||
XCTAssertEqual(decodedFromDatabase, number)
|
||||
}
|
||||
try test("0", isDecodedAs: Decimal(string: "0")!)
|
||||
try test("0.25", isDecodedAs: Decimal(string: "0.25")!)
|
||||
try test("1", isDecodedAs: Decimal(string: "1")!)
|
||||
try test("-1", isDecodedAs: Decimal(string: "-1")!)
|
||||
try test("9223372036854775807", isDecodedAs: Decimal(string: "9223372036854775807")!)
|
||||
try test("9223372036854775806", isDecodedAs: Decimal(string: "9223372036854775806")!)
|
||||
try test("-9223372036854775807", isDecodedAs: Decimal(string: "-9223372036854775807")!)
|
||||
try test("-9223372036854775808", isDecodedAs: Decimal(string: "-9223372036854775808")!)
|
||||
try test("18446744073709551615", isDecodedAs: Decimal(string: "18446744073709551615")!)
|
||||
}
|
||||
}
|
||||
@@ -49,6 +49,45 @@ class FoundationNSDecimalNumberTests: GRDBTestCase {
|
||||
XCTAssertEqual(storage(NSDecimalNumber(string: "-9223372036854775809")), .double)
|
||||
}
|
||||
|
||||
func testNSDecimalNumberDatabaseRoundTrip() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
func roundTrip(_ value: NSDecimalNumber) throws -> Bool {
|
||||
guard let back = try dbQueue.inDatabase({ try NSDecimalNumber.fetchOne($0, sql: "SELECT ?", arguments: [value]) }) else {
|
||||
XCTFail()
|
||||
return false
|
||||
}
|
||||
return back == value
|
||||
}
|
||||
|
||||
XCTAssertTrue(try roundTrip(NSDecimalNumber(value: Int32.min + 1)))
|
||||
XCTAssertTrue(try roundTrip(NSDecimalNumber(value: Double(10000000.01))))
|
||||
}
|
||||
|
||||
func testNSDecimalNumberDatabaseValueRoundTrip() {
|
||||
func roundTrip(_ value: NSDecimalNumber) -> Bool
|
||||
{
|
||||
let dbValue = value.databaseValue
|
||||
guard let back = NSDecimalNumber.fromDatabaseValue(dbValue) else
|
||||
{
|
||||
XCTFail("Failed to convert from DatabaseValue to NSDecimalNumber")
|
||||
return false
|
||||
}
|
||||
return back.isEqual(to: value)
|
||||
}
|
||||
|
||||
XCTAssertTrue(roundTrip(NSDecimalNumber(value: Int32.min + 1)))
|
||||
XCTAssertTrue(roundTrip(NSDecimalNumber(value: Double(10000000.01))))
|
||||
}
|
||||
|
||||
func testNSDecimalNumberFromDatabaseValueFailure() {
|
||||
let databaseValue_Null = DatabaseValue.null
|
||||
let databaseValue_String = "foo".databaseValue
|
||||
let databaseValue_Blob = "bar".data(using: .utf8)!.databaseValue
|
||||
XCTAssertNil(NSDecimalNumber.fromDatabaseValue(databaseValue_Null))
|
||||
XCTAssertNil(NSDecimalNumber.fromDatabaseValue(databaseValue_String))
|
||||
XCTAssertNil(NSDecimalNumber.fromDatabaseValue(databaseValue_Blob))
|
||||
}
|
||||
|
||||
func testNSDecimalNumberDecodingFromInt64() throws {
|
||||
func test(_ value: Int64, isDecodedAs number: NSDecimalNumber) throws {
|
||||
XCTAssertEqual(NSDecimalNumber(value: value), number)
|
||||
|
||||
@@ -107,7 +107,6 @@ class FoundationNSNumberTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
func testNSNumberDatabaseValueRoundTrip() {
|
||||
|
||||
func roundTrip(_ value: NSNumber) -> Bool
|
||||
{
|
||||
let dbValue = value.databaseValue
|
||||
@@ -133,23 +132,23 @@ class FoundationNSNumberTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
func testNSNumberDecodingFromText() throws {
|
||||
func test(_ value: String, isDecodedAs number: NSNumber) throws {
|
||||
let decodedFromDatabaseValue = NSNumber.fromDatabaseValue(value.databaseValue)
|
||||
func test(_ value: String, isDecodedAs number: NSDecimalNumber) throws {
|
||||
let decodedFromDatabaseValue = NSNumber.fromDatabaseValue(value.databaseValue) as? NSDecimalNumber
|
||||
XCTAssertEqual(decodedFromDatabaseValue, number)
|
||||
|
||||
let decodedFromDatabase = try DatabaseQueue().read { db in
|
||||
try NSNumber.fetchOne(db, sql: "SELECT ?", arguments: [value])
|
||||
try NSNumber.fetchOne(db, sql: "SELECT ?", arguments: [value]) as? NSDecimalNumber
|
||||
}
|
||||
XCTAssertEqual(decodedFromDatabase, number)
|
||||
}
|
||||
try test("0", isDecodedAs: NSNumber(value: 0))
|
||||
try test("0.25", isDecodedAs: NSNumber(value: 0.25))
|
||||
try test("1", isDecodedAs: NSNumber(value: 1))
|
||||
try test("-1", isDecodedAs: NSNumber(value: -1))
|
||||
try test("9223372036854775807", isDecodedAs: NSNumber(value: 9223372036854775807))
|
||||
try test("9223372036854775806", isDecodedAs: NSNumber(value: 9223372036854775806))
|
||||
try test("-9223372036854775807", isDecodedAs: NSNumber(value: -9223372036854775807))
|
||||
try test("-9223372036854775808", isDecodedAs: NSNumber(value: -9223372036854775808))
|
||||
try test("18446744073709551615", isDecodedAs: NSNumber(value: UInt64(18446744073709551615)))
|
||||
try test("0", isDecodedAs: NSDecimalNumber(value: 0))
|
||||
try test("0.25", isDecodedAs: NSDecimalNumber(value: 0.25))
|
||||
try test("1", isDecodedAs: NSDecimalNumber(value: 1))
|
||||
try test("-1", isDecodedAs: NSDecimalNumber(value: -1))
|
||||
try test("9223372036854775807", isDecodedAs: NSDecimalNumber(value: 9223372036854775807))
|
||||
try test("9223372036854775806", isDecodedAs: NSDecimalNumber(value: 9223372036854775806))
|
||||
try test("-9223372036854775807", isDecodedAs: NSDecimalNumber(value: -9223372036854775807))
|
||||
try test("-9223372036854775808", isDecodedAs: NSDecimalNumber(value: -9223372036854775808))
|
||||
try test("18446744073709551615", isDecodedAs: NSDecimalNumber(value: UInt64(18446744073709551615)))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user