@@ -28,7 +28,7 @@ import Data.Fixed (Micro, showFixed)
2828import Data.Functor.Contravariant ((>$<) )
2929import Data.Int (Int64 )
3030import Data.Pool (Pool )
31- import Data.Scientific (Scientific (.. ), scientific , toBoundedInteger )
31+ import Data.Scientific (Scientific (.. ), coefficient , scientific , toBoundedInteger )
3232import Data.Text (Text )
3333import qualified Data.Text as Text
3434import Data.WideWord (Word128 (.. ))
@@ -89,7 +89,7 @@ instance ToJSON Ada where
8989 -- `Number` results in it becoming `7.3112484749601107e10` while the old explorer is returning `73112484749.601107`
9090 toEncoding (Ada ada) =
9191 unsafeToEncoding $
92- Builder. string8 $ -- convert ByteString to Aeson's
92+ Builder. string8 $ -- convert ByteString to Aeson's -- convert ByteString to Aeson's
9393 showFixed True ada -- convert String to ByteString using Latin1 encoding
9494 -- convert Micro to String chopping off trailing zeros
9595
@@ -176,11 +176,31 @@ newtype DbWord64 = DbWord64 {unDbWord64 :: Word64}
176176 deriving (Eq , Generic , Num )
177177 deriving (Read , Show ) via (Quiet DbWord64 )
178178
179+ -- Helper to replicate the original Persistent fromPersistValue behavior for DbWord64
180+ -- This matches the PersistRational case: fromIntegral $ numerator r
181+ scientificToWord64 :: Scientific -> Word64
182+ scientificToWord64 s = case toBoundedInteger @ Word64 s of
183+ Just w64 -> w64
184+ Nothing -> fromIntegral $ coefficient s -- Fallback to coefficient for out-of-bounds values
185+
186+ -- Value encoder for DbWord64 using numeric (matches word64type domain)
187+ dbWord64ValueEncoder :: HsqlE. Value DbWord64
188+ dbWord64ValueEncoder = (\ x -> scientific (toInteger $ unDbWord64 x) 0 ) >$< HsqlE. numeric
189+
190+ -- Non-nullable encoder for DbWord64 parameters
191+ dbWord64Encoder :: HsqlE. Params DbWord64
192+ dbWord64Encoder = HsqlE. param $ HsqlE. nonNullable dbWord64ValueEncoder
193+
194+ -- Non-nullable decoder for DbWord64
195+ dbWord64Decoder :: HsqlD. Row DbWord64
196+ dbWord64Decoder = HsqlD. column (HsqlD. nonNullable (DbWord64 . scientificToWord64 <$> HsqlD. numeric))
197+
198+ -- Nullable encoder for DbWord64 parameters
179199maybeDbWord64Encoder :: HsqlE. Params (Maybe DbWord64 )
180- maybeDbWord64Encoder = HsqlE. param $ HsqlE. nullable $ fromIntegral . unDbWord64 >$< HsqlE. int8
200+ maybeDbWord64Encoder = HsqlE. param $ HsqlE. nullable dbWord64ValueEncoder
181201
182202maybeDbWord64Decoder :: HsqlD. Row (Maybe DbWord64 )
183- maybeDbWord64Decoder = HsqlD. column (HsqlD. nullable (DbWord64 . fromIntegral <$> HsqlD. int8 ))
203+ maybeDbWord64Decoder = HsqlD. column (HsqlD. nullable (DbWord64 . scientificToWord64 <$> HsqlD. numeric ))
184204
185205--------------------------------------------------------------------------------
186206-- The following must be in alphabetic order.
0 commit comments