mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-29 04:24:21 +00:00
Cleanup
This commit is contained in:
1 parent
c233fec9b9
commit
90fc23205c
1 file changed
+7
-11
@@ -1,23 +1,19 @@
|
||||
import Foundation
|
||||
|
||||
private let integerRoundingBehavior = NSDecimalNumberHandler(roundingMode: .RoundPlain, scale: 0, raiseOnExactness: false, raiseOnOverflow: false, raiseOnUnderflow: false, raiseOnDivideByZero: false)
|
||||
private let int64maxDecimal = NSDecimalNumber(mantissa: UInt64(Int64.max), exponent: 0, isNegative: false)
|
||||
private let int64minDecimal = NSDecimalNumber(mantissa: UInt64(Int64.max) + 1, exponent: 0, isNegative: true) // Int64.min = -(Int64.max + 1) because of two's complement
|
||||
|
||||
/// NSNumber adopts DatabaseValueConvertible
|
||||
extension NSNumber: DatabaseValueConvertible {
|
||||
|
||||
/// Returns a value that can be stored in the database.
|
||||
public var databaseValue: DatabaseValue {
|
||||
if let decimal = self as? NSDecimalNumber {
|
||||
// Don't lose precision: store integers that fits in Int64 as Int64
|
||||
let rounded = decimal.decimalNumberByRoundingAccordingToBehavior(integerRoundingBehavior)
|
||||
if (rounded == decimal)
|
||||
&& (decimal.compare(int64maxDecimal) != .OrderedDescending) // decimal <= Int64.max
|
||||
&& (decimal.compare(int64minDecimal) != .OrderedAscending) // decimal >= Int64.min
|
||||
{
|
||||
return longLongValue.databaseValue
|
||||
}
|
||||
// Don't lose precision: store integers that fits in Int64 as Int64
|
||||
if let decimal = self as? NSDecimalNumber
|
||||
where decimal == decimal.decimalNumberByRoundingAccordingToBehavior(integerRoundingBehavior) // integer
|
||||
&& decimal.compare(NSDecimalNumber(longLong: Int64.max)) != .OrderedDescending // decimal <= Int64.max
|
||||
&& decimal.compare(NSDecimalNumber(longLong: Int64.min)) != .OrderedAscending // decimal >= Int64.min
|
||||
{
|
||||
return longLongValue.databaseValue
|
||||
}
|
||||
|
||||
switch String.fromCString(objCType)! {
|
||||
|
||||
Reference in new issue
Block a user