{-# OPTIONS_GHC -Wno-orphans #-}
module ClickHaskell.Primitive.TUInt where

-- Internal
import ClickHaskell.Primitive.Serialization

-- GHC included
import Data.ByteString.Builder (byteString, word8, word16LE, word32LE, word64LE)
import Data.ByteString.Char8 as BS8 (pack)
import Data.Word (Word8, Word16, Word32, Word64)

-- External
import Data.Binary.Get (getWord8, getWord16le, getWord32le, getWord64le)
import Data.WideWord


-- * UInt8

{- | ClickHouse UInt8 column type -}
type UInt8 = Word8

instance IsChType UInt8 where
  chTypeName :: String
chTypeName = String
"UInt8"
  defaultValueOfTypeName :: UInt8
defaultValueOfTypeName = UInt8
0

instance Serializable UInt8 where
  serialize :: ProtocolRevision -> UInt8 -> Builder
serialize ProtocolRevision
_ = UInt8 -> Builder
word8
  deserialize :: ProtocolRevision -> Get UInt8
deserialize ProtocolRevision
_ = Get UInt8
getWord8
  {-# INLINE deserialize #-}

instance ToQueryPart UInt8 where
  toQueryPart :: UInt8 -> Builder
toQueryPart = StrictByteString -> Builder
byteString (StrictByteString -> Builder)
-> (UInt8 -> StrictByteString) -> UInt8 -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> StrictByteString
BS8.pack (String -> StrictByteString)
-> (UInt8 -> String) -> UInt8 -> StrictByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UInt8 -> String
forall a. Show a => a -> String
show


-- * UInt16


{- | ClickHouse UInt16 column type -}
type UInt16 = Word16

instance IsChType UInt16 where
  chTypeName :: String
chTypeName = String
"UInt16"
  defaultValueOfTypeName :: UInt16
defaultValueOfTypeName = UInt16
0

instance Serializable UInt16 where
  serialize :: ProtocolRevision -> UInt16 -> Builder
serialize ProtocolRevision
_ = UInt16 -> Builder
word16LE
  deserialize :: ProtocolRevision -> Get UInt16
deserialize ProtocolRevision
_ = Get UInt16
getWord16le
  {-# INLINE deserialize #-}

instance ToQueryPart UInt16 where
  toQueryPart :: UInt16 -> Builder
toQueryPart = StrictByteString -> Builder
byteString (StrictByteString -> Builder)
-> (UInt16 -> StrictByteString) -> UInt16 -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> StrictByteString
BS8.pack (String -> StrictByteString)
-> (UInt16 -> String) -> UInt16 -> StrictByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UInt16 -> String
forall a. Show a => a -> String
show


-- * UInt32

{- | ClickHouse UInt32 column type -}
type UInt32 = Word32

instance IsChType UInt32 where
  chTypeName :: String
chTypeName = String
"UInt32"
  defaultValueOfTypeName :: UInt32
defaultValueOfTypeName = UInt32
0

instance Serializable UInt32 where
  serialize :: ProtocolRevision -> UInt32 -> Builder
serialize ProtocolRevision
_ = UInt32 -> Builder
word32LE
  deserialize :: ProtocolRevision -> Get UInt32
deserialize ProtocolRevision
_ = Get UInt32
getWord32le
  {-# INLINE deserialize #-}

instance ToQueryPart UInt32 where
  toQueryPart :: UInt32 -> Builder
toQueryPart = StrictByteString -> Builder
byteString (StrictByteString -> Builder)
-> (UInt32 -> StrictByteString) -> UInt32 -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> StrictByteString
BS8.pack (String -> StrictByteString)
-> (UInt32 -> String) -> UInt32 -> StrictByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UInt32 -> String
forall a. Show a => a -> String
show


-- * UInt64

{- | ClickHouse UInt64 column type -}
type UInt64 = Word64

instance IsChType UInt64 where
  chTypeName :: String
chTypeName = String
"UInt64"
  defaultValueOfTypeName :: UInt64
defaultValueOfTypeName = UInt64
0

instance Serializable UInt64 where
  serialize :: ProtocolRevision -> UInt64 -> Builder
serialize ProtocolRevision
_ = UInt64 -> Builder
word64LE
  deserialize :: ProtocolRevision -> Get UInt64
deserialize ProtocolRevision
_ = Get UInt64
getWord64le
  {-# INLINE deserialize #-}

instance ToQueryPart UInt64 where
  toQueryPart :: UInt64 -> Builder
toQueryPart = StrictByteString -> Builder
byteString (StrictByteString -> Builder)
-> (UInt64 -> StrictByteString) -> UInt64 -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> StrictByteString
BS8.pack (String -> StrictByteString)
-> (UInt64 -> String) -> UInt64 -> StrictByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UInt64 -> String
forall a. Show a => a -> String
show


-- * UInt128

{- | ClickHouse UInt128 column type -}
type UInt128 = Word128

instance IsChType UInt128 where
  chTypeName :: String
chTypeName = String
"UInt128"
  defaultValueOfTypeName :: UInt128
defaultValueOfTypeName = UInt128
0

instance Serializable UInt128 where
  serialize :: ProtocolRevision -> UInt128 -> Builder
serialize ProtocolRevision
_ = (\(Word128 UInt64
hi UInt64
lo) -> UInt64 -> Builder
word64LE UInt64
lo Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> UInt64 -> Builder
word64LE UInt64
hi)
  deserialize :: ProtocolRevision -> Get UInt128
deserialize ProtocolRevision
_ = do
    low <- Get UInt64
getWord64le
    high <- getWord64le
    pure $ Word128 high low
  {-# INLINE deserialize #-}

instance ToQueryPart UInt128 where
  toQueryPart :: UInt128 -> Builder
toQueryPart UInt128
w128 = Builder
"'" Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> (StrictByteString -> Builder
byteString (StrictByteString -> Builder)
-> (UInt128 -> StrictByteString) -> UInt128 -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> StrictByteString
BS8.pack (String -> StrictByteString)
-> (UInt128 -> String) -> UInt128 -> StrictByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UInt128 -> String
forall a. Show a => a -> String
show) UInt128
w128 Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
"'"


-- * UInt256

{- | ClickHouse UInt128 column type -}
type UInt256 = Word256

instance IsChType UInt256 where
  chTypeName :: String
chTypeName = String
"UInt256"
  defaultValueOfTypeName :: UInt256
defaultValueOfTypeName = UInt256
0

instance Serializable UInt256 where
  serialize :: ProtocolRevision -> UInt256 -> Builder
serialize ProtocolRevision
_ = (\(Word256 UInt64
high UInt64
mid1 UInt64
mid0 UInt64
low) -> UInt64 -> Builder
word64LE UInt64
low Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> UInt64 -> Builder
word64LE UInt64
mid0 Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> UInt64 -> Builder
word64LE UInt64
mid1 Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> UInt64 -> Builder
word64LE UInt64
high)
  deserialize :: ProtocolRevision -> Get UInt256
deserialize ProtocolRevision
_ = do
    low <- Get UInt64
getWord64le
    mid0 <- getWord64le
    mid1 <- getWord64le
    high <- getWord64le
    pure $ Word256 high mid1 mid0 low
  {-# INLINE deserialize #-}

instance ToQueryPart UInt256 where
  toQueryPart :: UInt256 -> Builder
toQueryPart UInt256
w256 = Builder
"'" Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> (StrictByteString -> Builder
byteString (StrictByteString -> Builder)
-> (UInt256 -> StrictByteString) -> UInt256 -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> StrictByteString
BS8.pack (String -> StrictByteString)
-> (UInt256 -> String) -> UInt256 -> StrictByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UInt256 -> String
forall a. Show a => a -> String
show) UInt256
w256 Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
"'"