{-# LANGUAGE
    AllowAmbiguousTypes
  , ConstraintKinds
  , DataKinds
  , DefaultSignatures
  , DeriveAnyClass
  , DeriveGeneric
  , DerivingStrategies
  , DuplicateRecordFields
  , GADTs
  , GeneralizedNewtypeDeriving
  , FlexibleContexts
  , FlexibleInstances
  , LambdaCase
  , MultiParamTypeClasses
  , NamedFieldPuns
  , NoFieldSelectors
  , NumericUnderscores
  , OverloadedStrings
  , RecordWildCards
  , TupleSections
  , TypeApplications
  , TypeFamilies
  , TypeOperators
  , ScopedTypeVariables
  , StandaloneDeriving
  , UndecidableInstances
#-}

{-# OPTIONS_GHC
  -Wno-orphans
#-}

module ClickHaskell
  (
  -- * Connection
    ChCredential(..), defaultCredentials
  , Connection(..), openNativeConnection

  -- * Reading and writing
  , Table
  , Columns, Column, KnownColumn(..), DeserializableColumn(..)

  -- * Reading
  , ReadableFrom(..)
  , select
  , selectFrom
  , selectFromView, View, parameter, Parameter, Parameters, viewParameters
  , generateRandom
  -- ** Internal
  , handleSelect

  -- * Errors
  , ClientError(..)
  , ConnectionError(..)
  , UserError(..)
  , InternalError(..)

  -- * Writing
  , WritableInto(..)
  , insertInto

  -- * Ping database connection
  , ping

  -- * ClickHouse types
  , IsChType(ToChTypeName, chTypeName, defaultValueOfTypeName)
  , ToChType(toChType)
  , FromChType(fromChType)
  , ToQueryPart(toQueryPart)

  , DateTime(..)

  , module Data.Int
  , UInt8, UInt16, UInt32, UInt64, UInt128
  , Nullable
  , LowCardinality, IsLowCardinalitySupported
  -- Deprecated Ch prefixed types. Use above one
  , ChDate, ChDateTime
  , ChUInt8, ChUInt16, ChUInt32, ChUInt64, ChUInt128
  , ChInt8, ChInt16, ChInt32, ChInt64, ChInt128

  , ChString(..)
  , ChUUID(..)

  , ChArray(..)

  , UVarInt(..)
  , module Data.WideWord
  ) where

-- Internal
import Paths_ClickHaskell (version)

-- GHC included
import Control.Concurrent (MVar, newMVar, putMVar, takeMVar)
import Control.DeepSeq (NFData)
import Control.Exception (Exception, SomeException, bracketOnError, catch, finally, mask, onException, throw, throwIO)
import Control.Monad (forM, replicateM, void, when, (<$!>), (<=<))
import Data.Binary.Get
import Data.Binary.Get.Internal (readN)
import Data.Bits (Bits (setBit, unsafeShiftL, unsafeShiftR, (.&.), (.|.)))
import Data.ByteString as BS (StrictByteString, length, take, toStrict)
import Data.ByteString.Builder
import Data.ByteString.Builder as BS (Builder, byteString)
import Data.ByteString.Char8 as BS8 (concatMap, length, pack, replicate, singleton)
import Data.Coerce (coerce)
import Data.IORef (IORef, atomicModifyIORef, atomicWriteIORef, newIORef, readIORef)
import Data.Int (Int16, Int32, Int64, Int8)
import Data.Kind (Constraint, Type)
import Data.List (uncons)
import Data.Maybe (listToMaybe)
import Data.String (IsString (..))
import Data.Text (Text)
import Data.Text.Encoding as Text (encodeUtf8)
import Data.Time (UTCTime, ZonedTime, zonedTimeToUTC)
import Data.Time.Clock.POSIX (posixSecondsToUTCTime, utcTimeToPOSIXSeconds)
import Data.Type.Bool (If)
import Data.Type.Equality (type (==))
import Data.Typeable (Proxy (..))
import Data.Version (Version (..))
import Data.Word (Word16, Word32, Word64, Word8)
import GHC.Generics (C1, D1, Generic (..), K1 (K1, unK1), M1 (M1, unM1), Meta (MetaSel), Rec0, S1, type (:*:) (..))
import GHC.Stack (HasCallStack, callStack, prettyCallStack)
import GHC.TypeLits (AppendSymbol, ErrorMessage (..), KnownNat, KnownSymbol, Nat, Symbol, TypeError, natVal, symbolVal)
import System.Environment (lookupEnv)
import System.Timeout (timeout)

-- External
import Data.WideWord (Int128 (..), Word128(..))
import Network.Socket as Sock
import Network.Socket.ByteString (recv)
import Network.Socket.ByteString.Lazy (sendAll)

-- * Connection

data ChCredential = MkChCredential
  { ChCredential -> Text
chLogin    :: Text
  , ChCredential -> Text
chPass     :: Text
  , ChCredential -> Text
chDatabase :: Text
  , ChCredential -> String
chHost     :: HostName
  , ChCredential -> String
chPort     :: ServiceName
  }

defaultCredentials :: ChCredential
defaultCredentials :: ChCredential
defaultCredentials = MkChCredential
  { $sel:chLogin:MkChCredential :: Text
chLogin    = Text
"default"
  , $sel:chPass:MkChCredential :: Text
chPass     = Text
""
  , $sel:chHost:MkChCredential :: String
chHost     = String
"localhost"
  , $sel:chDatabase:MkChCredential :: Text
chDatabase = Text
"default"
  , $sel:chPort:MkChCredential :: String
chPort     = String
"9000"
  }

data Connection where MkConnection :: (MVar ConnectionState) -> Connection

withConnection :: HasCallStack => Connection -> (ConnectionState -> IO a) -> IO a
withConnection :: forall a.
HasCallStack =>
Connection -> (ConnectionState -> IO a) -> IO a
withConnection (MkConnection MVar ConnectionState
connStateMVar) ConnectionState -> IO a
f =
  ((forall a. IO a -> IO a) -> IO a) -> IO a
forall b. ((forall a. IO a -> IO a) -> IO b) -> IO b
mask (((forall a. IO a -> IO a) -> IO a) -> IO a)
-> ((forall a. IO a -> IO a) -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$ \forall a. IO a -> IO a
restore -> do
    ConnectionState
connState <- MVar ConnectionState -> IO ConnectionState
forall a. MVar a -> IO a
takeMVar MVar ConnectionState
connStateMVar
    a
b <- IO a -> IO () -> IO a
forall a b. IO a -> IO b -> IO a
onException
      (IO a -> IO a
forall a. IO a -> IO a
restore (ConnectionState -> IO a
f ConnectionState
connState))
      (MVar ConnectionState -> ConnectionState -> IO ()
forall a. MVar a -> a -> IO ()
putMVar MVar ConnectionState
connStateMVar (ConnectionState -> IO ()) -> IO ConnectionState -> IO ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< ConnectionState -> IO ConnectionState
reopenConnection ConnectionState
connState)
    MVar ConnectionState -> ConnectionState -> IO ()
forall a. MVar a -> a -> IO ()
putMVar MVar ConnectionState
connStateMVar ConnectionState
connState
    a -> IO a
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return a
b

data ConnectionState = MkConnectionState
  { ConnectionState -> Socket
sock     :: Socket
  , ConnectionState -> ChString
user     :: ChString
  , ConnectionState -> ChString
hostname :: ChString
  , ConnectionState -> ChString
os_user  :: ChString
  , ConnectionState -> Buffer
buffer   :: Buffer
  , ConnectionState -> ProtocolRevision
revision :: ProtocolRevision
  , ConnectionState -> ChCredential
creds    :: ChCredential
  }

data ConnectionError = NoAdressResolved | EstablishTimeout
  deriving (Int -> ConnectionError -> ShowS
[ConnectionError] -> ShowS
ConnectionError -> String
(Int -> ConnectionError -> ShowS)
-> (ConnectionError -> String)
-> ([ConnectionError] -> ShowS)
-> Show ConnectionError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ConnectionError -> ShowS
showsPrec :: Int -> ConnectionError -> ShowS
$cshow :: ConnectionError -> String
show :: ConnectionError -> String
$cshowList :: [ConnectionError] -> ShowS
showList :: [ConnectionError] -> ShowS
Show, Show ConnectionError
Typeable ConnectionError
(Typeable ConnectionError, Show ConnectionError) =>
(ConnectionError -> SomeException)
-> (SomeException -> Maybe ConnectionError)
-> (ConnectionError -> String)
-> Exception ConnectionError
SomeException -> Maybe ConnectionError
ConnectionError -> String
ConnectionError -> SomeException
forall e.
(Typeable e, Show e) =>
(e -> SomeException)
-> (SomeException -> Maybe e) -> (e -> String) -> Exception e
$ctoException :: ConnectionError -> SomeException
toException :: ConnectionError -> SomeException
$cfromException :: SomeException -> Maybe ConnectionError
fromException :: SomeException -> Maybe ConnectionError
$cdisplayException :: ConnectionError -> String
displayException :: ConnectionError -> String
Exception)

writeToConnection :: Serializable packet => ConnectionState -> packet -> IO ()
writeToConnection :: forall packet.
Serializable packet =>
ConnectionState -> packet -> IO ()
writeToConnection MkConnectionState{Socket
$sel:sock:MkConnectionState :: ConnectionState -> Socket
sock :: Socket
sock, ProtocolRevision
$sel:revision:MkConnectionState :: ConnectionState -> ProtocolRevision
revision :: ProtocolRevision
revision} packet
packet =
  (Socket -> ByteString -> IO ()
sendAll Socket
sock (ByteString -> IO ()) -> (packet -> ByteString) -> packet -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> ByteString
toLazyByteString (Builder -> ByteString)
-> (packet -> Builder) -> packet -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ProtocolRevision -> packet -> Builder
forall chType.
Serializable chType =>
ProtocolRevision -> chType -> Builder
serialize ProtocolRevision
revision) packet
packet

writeToConnectionEncode :: ConnectionState -> (ProtocolRevision -> Builder) -> IO ()
writeToConnectionEncode :: ConnectionState -> (ProtocolRevision -> Builder) -> IO ()
writeToConnectionEncode MkConnectionState{Socket
$sel:sock:MkConnectionState :: ConnectionState -> Socket
sock :: Socket
sock, ProtocolRevision
$sel:revision:MkConnectionState :: ConnectionState -> ProtocolRevision
revision :: ProtocolRevision
revision} ProtocolRevision -> Builder
serializer =
  (Socket -> ByteString -> IO ()
sendAll Socket
sock (ByteString -> IO ())
-> (Builder -> ByteString) -> Builder -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> ByteString
toLazyByteString) (ProtocolRevision -> Builder
serializer ProtocolRevision
revision)

openNativeConnection :: HasCallStack => ChCredential -> IO Connection
openNativeConnection :: HasCallStack => ChCredential -> IO Connection
openNativeConnection ChCredential
creds = (MVar ConnectionState -> Connection)
-> IO (MVar ConnectionState) -> IO Connection
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap MVar ConnectionState -> Connection
MkConnection (IO (MVar ConnectionState) -> IO Connection)
-> (ConnectionState -> IO (MVar ConnectionState))
-> ConnectionState
-> IO Connection
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ConnectionState -> IO (MVar ConnectionState)
forall a. a -> IO (MVar a)
newMVar (ConnectionState -> IO Connection)
-> IO ConnectionState -> IO Connection
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< ChCredential -> IO ConnectionState
createConnectionState ChCredential
creds

reopenConnection :: ConnectionState -> IO ConnectionState
reopenConnection :: ConnectionState -> IO ConnectionState
reopenConnection MkConnectionState{Socket
ProtocolRevision
ChString
Buffer
ChCredential
$sel:sock:MkConnectionState :: ConnectionState -> Socket
$sel:user:MkConnectionState :: ConnectionState -> ChString
$sel:hostname:MkConnectionState :: ConnectionState -> ChString
$sel:os_user:MkConnectionState :: ConnectionState -> ChString
$sel:buffer:MkConnectionState :: ConnectionState -> Buffer
$sel:revision:MkConnectionState :: ConnectionState -> ProtocolRevision
$sel:creds:MkConnectionState :: ConnectionState -> ChCredential
sock :: Socket
user :: ChString
hostname :: ChString
os_user :: ChString
buffer :: Buffer
revision :: ProtocolRevision
creds :: ChCredential
..} = do
  Buffer -> IO ()
flushBuffer Buffer
buffer
  Socket -> IO ()
close Socket
sock
  ChCredential -> IO ConnectionState
createConnectionState ChCredential
creds

createConnectionState :: ChCredential -> IO ConnectionState
createConnectionState :: ChCredential -> IO ConnectionState
createConnectionState creds :: ChCredential
creds@MkChCredential{String
$sel:chHost:MkChCredential :: ChCredential -> String
chHost :: String
chHost, String
$sel:chPort:MkChCredential :: ChCredential -> String
chPort :: String
chPort, Text
$sel:chLogin:MkChCredential :: ChCredential -> Text
chLogin :: Text
chLogin, Text
$sel:chPass:MkChCredential :: ChCredential -> Text
chPass :: Text
chPass, Text
$sel:chDatabase:MkChCredential :: ChCredential -> Text
chDatabase :: Text
chDatabase} = do
  ChString
hostname <- ChString -> (String -> ChString) -> Maybe String -> ChString
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ChString
"" String -> ChString
forall chType inputType.
ToChType chType inputType =>
inputType -> chType
toChType (Maybe String -> ChString) -> IO (Maybe String) -> IO ChString
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> IO (Maybe String)
lookupEnv String
"HOSTNAME"
  ChString
os_user <- ChString -> (String -> ChString) -> Maybe String -> ChString
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ChString
"" String -> ChString
forall chType inputType.
ToChType chType inputType =>
inputType -> chType
toChType (Maybe String -> ChString) -> IO (Maybe String) -> IO ChString
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> IO (Maybe String)
lookupEnv String
"USER"
  AddrInfo{Family
addrFamily :: Family
addrFamily :: AddrInfo -> Family
addrFamily, SocketType
addrSocketType :: SocketType
addrSocketType :: AddrInfo -> SocketType
addrSocketType, ProtocolNumber
addrProtocol :: ProtocolNumber
addrProtocol :: AddrInfo -> ProtocolNumber
addrProtocol, SockAddr
addrAddress :: SockAddr
addrAddress :: AddrInfo -> SockAddr
addrAddress}
    <- IO AddrInfo
-> (AddrInfo -> IO AddrInfo) -> Maybe AddrInfo -> IO AddrInfo
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (ConnectionError -> IO AddrInfo
forall e a. Exception e => e -> IO a
throwIO ConnectionError
NoAdressResolved) AddrInfo -> IO AddrInfo
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe AddrInfo -> IO AddrInfo)
-> ([AddrInfo] -> Maybe AddrInfo) -> [AddrInfo] -> IO AddrInfo
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [AddrInfo] -> Maybe AddrInfo
forall a. [a] -> Maybe a
listToMaybe
    ([AddrInfo] -> IO AddrInfo) -> IO [AddrInfo] -> IO AddrInfo
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Maybe AddrInfo -> Maybe String -> Maybe String -> IO [AddrInfo]
getAddrInfo
      (AddrInfo -> Maybe AddrInfo
forall a. a -> Maybe a
Just AddrInfo
defaultHints{addrFlags = [AI_ADDRCONFIG], addrSocketType = Stream})
      (String -> Maybe String
forall a. a -> Maybe a
Just String
chHost)
      (String -> Maybe String
forall a. a -> Maybe a
Just String
chPort)
  Socket
sock <- IO Socket -> (Socket -> IO Socket) -> Maybe Socket -> IO Socket
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (ConnectionError -> IO Socket
forall e a. Exception e => e -> IO a
throwIO ConnectionError
EstablishTimeout) Socket -> IO Socket
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
    (Maybe Socket -> IO Socket) -> IO (Maybe Socket) -> IO Socket
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Int -> IO Socket -> IO (Maybe Socket)
forall a. Int -> IO a -> IO (Maybe a)
timeout Int
3_000_000 (
      IO Socket
-> (Socket -> IO ()) -> (Socket -> IO Socket) -> IO Socket
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracketOnError
        (Family -> SocketType -> ProtocolNumber -> IO Socket
socket Family
addrFamily SocketType
addrSocketType ProtocolNumber
addrProtocol)
        (\Socket
sock ->
          forall e a. Exception e => IO a -> (e -> IO a) -> IO a
catch @SomeException
            (IO () -> IO () -> IO ()
forall a b. IO a -> IO b -> IO a
finally
              (Socket -> ShutdownCmd -> IO ()
shutdown Socket
sock ShutdownCmd
ShutdownBoth)
              (Socket -> IO ()
close Socket
sock)
            )
            (IO () -> SomeException -> IO ()
forall a b. a -> b -> a
const (IO () -> SomeException -> IO ())
-> IO () -> SomeException -> IO ()
forall a b. (a -> b) -> a -> b
$ () -> IO ()
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ())
        )
        (\Socket
sock -> do
           Socket -> SocketOption -> Int -> IO ()
setSocketOption Socket
sock SocketOption
NoDelay Int
1
           Socket -> SocketOption -> Int -> IO ()
setSocketOption Socket
sock SocketOption
Sock.KeepAlive Int
1
           Socket -> SockAddr -> IO ()
connect Socket
sock SockAddr
addrAddress
           Socket -> IO Socket
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Socket
sock
        )
      )

  (Socket -> ByteString -> IO ()
sendAll Socket
sock (ByteString -> IO ())
-> (HelloPacket -> ByteString) -> HelloPacket -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> ByteString
toLazyByteString (Builder -> ByteString)
-> (HelloPacket -> Builder) -> HelloPacket -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ProtocolRevision -> HelloPacket -> Builder
forall chType.
Serializable chType =>
ProtocolRevision -> chType -> Builder
serialize ProtocolRevision
latestSupportedRevision)
    (HelloParameters -> HelloPacket
mkHelloPacket MkHelloParameters{Text
chLogin :: Text
chPass :: Text
chDatabase :: Text
$sel:chDatabase:MkHelloParameters :: Text
$sel:chLogin:MkHelloParameters :: Text
$sel:chPass:MkHelloParameters :: Text
..})

  Buffer
buffer <- Int -> Socket -> IO Buffer
initBuffer Int
4096 Socket
sock
  ServerPacketType
serverPacketType <- Buffer -> Get ServerPacketType -> IO ServerPacketType
forall packet. Buffer -> Get packet -> IO packet
rawBufferizedRead Buffer
buffer (ProtocolRevision -> Get ServerPacketType
forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize ProtocolRevision
latestSupportedRevision)
  case ServerPacketType
serverPacketType of
    HelloResponse MkHelloResponse{ProtocolRevision
server_revision :: ProtocolRevision
$sel:server_revision:MkHelloResponse :: HelloResponse -> ProtocolRevision
server_revision} -> do
      let revision :: ProtocolRevision
revision = ProtocolRevision -> ProtocolRevision -> ProtocolRevision
forall a. Ord a => a -> a -> a
min ProtocolRevision
server_revision ProtocolRevision
latestSupportedRevision
          conn :: ConnectionState
conn = MkConnectionState{$sel:user:MkConnectionState :: ChString
user = Text -> ChString
forall chType inputType.
ToChType chType inputType =>
inputType -> chType
toChType Text
chLogin, Socket
ProtocolRevision
ChString
Buffer
ChCredential
$sel:sock:MkConnectionState :: Socket
$sel:hostname:MkConnectionState :: ChString
$sel:os_user:MkConnectionState :: ChString
$sel:buffer:MkConnectionState :: Buffer
$sel:revision:MkConnectionState :: ProtocolRevision
$sel:creds:MkConnectionState :: ChCredential
creds :: ChCredential
hostname :: ChString
os_user :: ChString
sock :: Socket
buffer :: Buffer
revision :: ProtocolRevision
..}
      ConnectionState -> Addendum -> IO ()
forall packet.
Serializable packet =>
ConnectionState -> packet -> IO ()
writeToConnection ConnectionState
conn Addendum
mkAddendum
      ConnectionState -> IO ConnectionState
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ConnectionState
conn
    Exception ExceptionPacket
exception -> ClientError -> IO ConnectionState
forall e a. Exception e => e -> IO a
throwIO (HasCallStack => UserError -> ClientError
UserError -> ClientError
UserError (UserError -> ClientError) -> UserError -> ClientError
forall a b. (a -> b) -> a -> b
$ ExceptionPacket -> UserError
DatabaseException ExceptionPacket
exception)
    ServerPacketType
otherPacket         -> ClientError -> IO ConnectionState
forall e a. Exception e => e -> IO a
throwIO (HasCallStack => InternalError -> ClientError
InternalError -> ClientError
InternalError (InternalError -> ClientError) -> InternalError -> ClientError
forall a b. (a -> b) -> a -> b
$ ServerPacketType -> InternalError
UnexpectedPacketType ServerPacketType
otherPacket)




-- * Ping

ping :: HasCallStack => Connection -> IO ()
ping :: HasCallStack => Connection -> IO ()
ping Connection
conn = do
  Connection -> (ConnectionState -> IO ()) -> IO ()
forall a.
HasCallStack =>
Connection -> (ConnectionState -> IO a) -> IO a
withConnection Connection
conn ((ConnectionState -> IO ()) -> IO ())
-> (ConnectionState -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \connState :: ConnectionState
connState@MkConnectionState{ProtocolRevision
$sel:revision:MkConnectionState :: ConnectionState -> ProtocolRevision
revision :: ProtocolRevision
revision, Buffer
$sel:buffer:MkConnectionState :: ConnectionState -> Buffer
buffer :: Buffer
buffer} -> do
    ConnectionState -> PingPacket -> IO ()
forall packet.
Serializable packet =>
ConnectionState -> packet -> IO ()
writeToConnection ConnectionState
connState PingPacket
mkPingPacket
    ServerPacketType
responsePacket <- Buffer -> Get ServerPacketType -> IO ServerPacketType
forall packet. Buffer -> Get packet -> IO packet
rawBufferizedRead Buffer
buffer (ProtocolRevision -> Get ServerPacketType
forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize ProtocolRevision
revision)
    case ServerPacketType
responsePacket of
      ServerPacketType
Pong                -> () -> IO ()
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
      Exception ExceptionPacket
exception -> ClientError -> IO ()
forall e a. Exception e => e -> IO a
throwIO (HasCallStack => UserError -> ClientError
UserError -> ClientError
UserError (UserError -> ClientError) -> UserError -> ClientError
forall a b. (a -> b) -> a -> b
$ ExceptionPacket -> UserError
DatabaseException ExceptionPacket
exception)
      ServerPacketType
otherPacket         -> ClientError -> IO ()
forall e a. Exception e => e -> IO a
throwIO (HasCallStack => InternalError -> ClientError
InternalError -> ClientError
InternalError (InternalError -> ClientError) -> InternalError -> ClientError
forall a b. (a -> b) -> a -> b
$ ServerPacketType -> InternalError
UnexpectedPacketType ServerPacketType
otherPacket)




-- * Reading

select ::
  forall columns record result
  .
  (ReadableFrom (Columns columns) record)
  =>
  Connection -> ChString -> ([record] -> IO result) -> IO [result]
select :: forall (columns :: [*]) record result.
ReadableFrom (Columns columns) record =>
Connection -> ChString -> ([record] -> IO result) -> IO [result]
select Connection
conn ChString
query [record] -> IO result
f = do
  Connection -> (ConnectionState -> IO [result]) -> IO [result]
forall a.
HasCallStack =>
Connection -> (ConnectionState -> IO a) -> IO a
withConnection Connection
conn ((ConnectionState -> IO [result]) -> IO [result])
-> (ConnectionState -> IO [result]) -> IO [result]
forall a b. (a -> b) -> a -> b
$ \ConnectionState
connState -> do
    ConnectionState -> QueryPacket -> IO ()
forall packet.
Serializable packet =>
ConnectionState -> packet -> IO ()
writeToConnection ConnectionState
connState (ConnectionState -> ChString -> QueryPacket
mkQueryPacket ConnectionState
connState ChString
query)
    ConnectionState -> DataPacket -> IO ()
forall packet.
Serializable packet =>
ConnectionState -> packet -> IO ()
writeToConnection ConnectionState
connState (ChString -> UVarInt -> UVarInt -> DataPacket
mkDataPacket ChString
"" UVarInt
0 UVarInt
0)
    forall hasColumns record result.
ReadableFrom hasColumns record =>
ConnectionState -> ([record] -> IO result) -> IO [result]
handleSelect @(Columns columns) ConnectionState
connState (\[record]
x -> result -> result
forall a. a -> a
id (result -> result) -> IO result -> IO result
forall (m :: * -> *) a b. Monad m => (a -> b) -> m a -> m b
<$!> [record] -> IO result
f [record]
x)

data Table (name :: Symbol) (columns :: [Type])

selectFrom ::
  forall table record name columns a
  .
  ( table ~ Table name columns
  , KnownSymbol name
  , ReadableFrom table record
  )
  =>
  Connection -> ([record] -> IO a) -> IO [a]
selectFrom :: forall table record (name :: Symbol) (columns :: [*]) a.
(table ~ Table name columns, KnownSymbol name,
 ReadableFrom table record) =>
Connection -> ([record] -> IO a) -> IO [a]
selectFrom Connection
conn [record] -> IO a
f = do
  Connection -> (ConnectionState -> IO [a]) -> IO [a]
forall a.
HasCallStack =>
Connection -> (ConnectionState -> IO a) -> IO a
withConnection Connection
conn ((ConnectionState -> IO [a]) -> IO [a])
-> (ConnectionState -> IO [a]) -> IO [a]
forall a b. (a -> b) -> a -> b
$ \ConnectionState
connState -> do
    ConnectionState -> QueryPacket -> IO ()
forall packet.
Serializable packet =>
ConnectionState -> packet -> IO ()
writeToConnection ConnectionState
connState (ConnectionState -> ChString -> QueryPacket
mkQueryPacket ConnectionState
connState (Builder -> ChString
forall chType inputType.
ToChType chType inputType =>
inputType -> chType
toChType Builder
query))
    ConnectionState -> DataPacket -> IO ()
forall packet.
Serializable packet =>
ConnectionState -> packet -> IO ()
writeToConnection ConnectionState
connState (ChString -> UVarInt -> UVarInt -> DataPacket
mkDataPacket ChString
"" UVarInt
0 UVarInt
0)
    forall hasColumns record result.
ReadableFrom hasColumns record =>
ConnectionState -> ([record] -> IO result) -> IO [result]
handleSelect @table ConnectionState
connState (\[record]
x -> a -> a
forall a. a -> a
id (a -> a) -> IO a -> IO a
forall (m :: * -> *) a b. Monad m => (a -> b) -> m a -> m b
<$!> [record] -> IO a
f [record]
x)
  where
    query :: Builder
query =
      Builder
"SELECT " Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> forall hasColumns record. ReadableFrom hasColumns record => Builder
readingColumns @table @record Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
      Builder
" FROM " Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> (StrictByteString -> Builder
byteString (StrictByteString -> Builder)
-> (String -> StrictByteString) -> String -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> StrictByteString
BS8.pack) (Proxy name -> String
forall (n :: Symbol) (proxy :: Symbol -> *).
KnownSymbol n =>
proxy n -> String
symbolVal (Proxy name -> String) -> Proxy name -> String
forall a b. (a -> b) -> a -> b
$ forall {k} (t :: k). Proxy t
forall (t :: Symbol). Proxy t
Proxy @name)

data View (name :: Symbol) (columns :: [Type]) (parameters :: [Type])

selectFromView ::
  forall view record result name columns parameters passedParameters
  .
  ( ReadableFrom view record
  , KnownSymbol name
  , view ~ View name columns parameters
  , CheckParameters parameters passedParameters
  )
  => Connection -> (Parameters '[] -> Parameters passedParameters) -> ([record] -> IO result) -> IO [result]
selectFromView :: forall view record result (name :: Symbol) (columns :: [*])
       (parameters :: [*]) (passedParameters :: [*]).
(ReadableFrom view record, KnownSymbol name,
 view ~ View name columns parameters,
 CheckParameters parameters passedParameters) =>
Connection
-> (Parameters '[] -> Parameters passedParameters)
-> ([record] -> IO result)
-> IO [result]
selectFromView Connection
conn Parameters '[] -> Parameters passedParameters
interpreter [record] -> IO result
f = do
  Connection -> (ConnectionState -> IO [result]) -> IO [result]
forall a.
HasCallStack =>
Connection -> (ConnectionState -> IO a) -> IO a
withConnection Connection
conn ((ConnectionState -> IO [result]) -> IO [result])
-> (ConnectionState -> IO [result]) -> IO [result]
forall a b. (a -> b) -> a -> b
$ \ConnectionState
connState -> do
    ConnectionState -> QueryPacket -> IO ()
forall packet.
Serializable packet =>
ConnectionState -> packet -> IO ()
writeToConnection ConnectionState
connState (ConnectionState -> ChString -> QueryPacket
mkQueryPacket ConnectionState
connState (Builder -> ChString
forall chType inputType.
ToChType chType inputType =>
inputType -> chType
toChType Builder
query))
    ConnectionState -> DataPacket -> IO ()
forall packet.
Serializable packet =>
ConnectionState -> packet -> IO ()
writeToConnection ConnectionState
connState (ChString -> UVarInt -> UVarInt -> DataPacket
mkDataPacket ChString
"" UVarInt
0 UVarInt
0)
    forall hasColumns record result.
ReadableFrom hasColumns record =>
ConnectionState -> ([record] -> IO result) -> IO [result]
handleSelect @view ConnectionState
connState (\[record]
x -> result -> result
forall a. a -> a
id (result -> result) -> IO result -> IO result
forall (m :: * -> *) a b. Monad m => (a -> b) -> m a -> m b
<$!> [record] -> IO result
f [record]
x)
    where
    query :: Builder
query =
      Builder
"SELECT " Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> forall hasColumns record. ReadableFrom hasColumns record => Builder
readingColumns @view @record Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
      Builder
" FROM " Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> (StrictByteString -> Builder
byteString (StrictByteString -> Builder)
-> (Proxy name -> StrictByteString) -> Proxy name -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> StrictByteString
BS8.pack (String -> StrictByteString)
-> (Proxy name -> String) -> Proxy name -> StrictByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (n :: Symbol) (proxy :: Symbol -> *).
KnownSymbol n =>
proxy n -> String
symbolVal @name) Proxy name
forall {k} (t :: k). Proxy t
Proxy Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> (Parameters '[] -> Parameters passedParameters) -> Builder
forall (passedParameters :: [*]).
(Parameters '[] -> Parameters passedParameters) -> Builder
viewParameters Parameters '[] -> Parameters passedParameters
interpreter

generateRandom ::
  forall columns record result
  .
  (ReadableFrom (Columns columns) record)
  =>
  Connection -> (UInt64, UInt64, UInt64) -> UInt64 -> ([record] -> IO result) -> IO [result]
generateRandom :: forall (columns :: [*]) record result.
ReadableFrom (Columns columns) record =>
Connection
-> (UInt64, UInt64, UInt64)
-> UInt64
-> ([record] -> IO result)
-> IO [result]
generateRandom Connection
conn (UInt64
randomSeed, UInt64
maxStrLen, UInt64
maxArrayLen) UInt64
limit [record] -> IO result
f = do
  Connection -> (ConnectionState -> IO [result]) -> IO [result]
forall a.
HasCallStack =>
Connection -> (ConnectionState -> IO a) -> IO a
withConnection Connection
conn ((ConnectionState -> IO [result]) -> IO [result])
-> (ConnectionState -> IO [result]) -> IO [result]
forall a b. (a -> b) -> a -> b
$ \ConnectionState
connState -> do
    ConnectionState -> QueryPacket -> IO ()
forall packet.
Serializable packet =>
ConnectionState -> packet -> IO ()
writeToConnection ConnectionState
connState (ConnectionState -> ChString -> QueryPacket
mkQueryPacket ConnectionState
connState ChString
query)
    ConnectionState -> DataPacket -> IO ()
forall packet.
Serializable packet =>
ConnectionState -> packet -> IO ()
writeToConnection ConnectionState
connState (ChString -> UVarInt -> UVarInt -> DataPacket
mkDataPacket ChString
"" UVarInt
0 UVarInt
0)
    forall hasColumns record result.
ReadableFrom hasColumns record =>
ConnectionState -> ([record] -> IO result) -> IO [result]
handleSelect @(Columns columns) ConnectionState
connState (\[record]
x -> result -> result
forall a. a -> a
id (result -> result) -> IO result -> IO result
forall (m :: * -> *) a b. Monad m => (a -> b) -> m a -> m b
<$!> [record] -> IO result
f [record]
x)
  where
  query :: ChString
query =
    let columns :: Builder
columns = forall hasColumns record. ReadableFrom hasColumns record => Builder
readingColumnsAndTypes @(Columns columns) @record
    in Builder -> ChString
forall chType inputType.
ToChType chType inputType =>
inputType -> chType
toChType (Builder -> ChString) -> Builder -> ChString
forall a b. (a -> b) -> a -> b
$
      Builder
"SELECT * FROM generateRandom(" Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
          Builder
"'" Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
columns Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
"' ," Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
            UInt64 -> Builder
forall chType. ToQueryPart chType => chType -> Builder
toQueryPart UInt64
randomSeed Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
"," Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
            UInt64 -> Builder
forall chType. ToQueryPart chType => chType -> Builder
toQueryPart UInt64
maxStrLen Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
"," Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
            UInt64 -> Builder
forall chType. ToQueryPart chType => chType -> Builder
toQueryPart UInt64
maxArrayLen Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
        Builder
")" Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
      Builder
" LIMIT " Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> UInt64 -> Builder
forall chType. ToQueryPart chType => chType -> Builder
toQueryPart UInt64
limit Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
";"

-- *** Internal

handleSelect ::
  forall hasColumns record result
  .
  ReadableFrom hasColumns record
  =>
  ConnectionState -> ([record] -> IO result) -> IO [result]
handleSelect :: forall hasColumns record result.
ReadableFrom hasColumns record =>
ConnectionState -> ([record] -> IO result) -> IO [result]
handleSelect MkConnectionState{Socket
ProtocolRevision
ChString
Buffer
ChCredential
$sel:sock:MkConnectionState :: ConnectionState -> Socket
$sel:user:MkConnectionState :: ConnectionState -> ChString
$sel:hostname:MkConnectionState :: ConnectionState -> ChString
$sel:os_user:MkConnectionState :: ConnectionState -> ChString
$sel:buffer:MkConnectionState :: ConnectionState -> Buffer
$sel:revision:MkConnectionState :: ConnectionState -> ProtocolRevision
$sel:creds:MkConnectionState :: ConnectionState -> ChCredential
sock :: Socket
user :: ChString
hostname :: ChString
os_user :: ChString
buffer :: Buffer
revision :: ProtocolRevision
creds :: ChCredential
..} [record] -> IO result
f = [result] -> IO [result]
loop []
  where
  loop :: [result] -> IO [result]
loop [result]
acc = Buffer -> Get ServerPacketType -> IO ServerPacketType
forall packet. Buffer -> Get packet -> IO packet
rawBufferizedRead Buffer
buffer (ProtocolRevision -> Get ServerPacketType
forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize ProtocolRevision
revision) IO ServerPacketType
-> (ServerPacketType -> IO [result]) -> IO [result]
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
    \ServerPacketType
packet -> case ServerPacketType
packet of
      DataResponse MkDataPacket{$sel:columns_count:MkDataPacket :: DataPacket -> UVarInt
columns_count = UVarInt
0, $sel:rows_count:MkDataPacket :: DataPacket -> UVarInt
rows_count = UVarInt
0} -> [result] -> IO [result]
loop [result]
acc
      DataResponse MkDataPacket{UVarInt
$sel:rows_count:MkDataPacket :: DataPacket -> UVarInt
rows_count :: UVarInt
rows_count} -> do
        result
result <- [record] -> IO result
f ([record] -> IO result) -> IO [record] -> IO result
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Buffer -> Get [record] -> IO [record]
forall packet. Buffer -> Get packet -> IO packet
rawBufferizedRead Buffer
buffer (forall hasColumns record.
ReadableFrom hasColumns record =>
ProtocolRevision -> UVarInt -> Get [record]
deserializeColumns @hasColumns ProtocolRevision
revision UVarInt
rows_count)
        [result] -> IO [result]
loop (result
result result -> [result] -> [result]
forall a. a -> [a] -> [a]
: [result]
acc)
      Progress    ProgressPacket
_       -> [result] -> IO [result]
loop [result]
acc
      ProfileInfo ProfileInfo
_       -> [result] -> IO [result]
loop [result]
acc
      ServerPacketType
EndOfStream         -> [result] -> IO [result]
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [result]
acc
      Exception ExceptionPacket
exception -> ClientError -> IO [result]
forall e a. Exception e => e -> IO a
throwIO (HasCallStack => UserError -> ClientError
UserError -> ClientError
UserError (UserError -> ClientError) -> UserError -> ClientError
forall a b. (a -> b) -> a -> b
$ ExceptionPacket -> UserError
DatabaseException ExceptionPacket
exception)
      ServerPacketType
otherPacket         -> ClientError -> IO [result]
forall e a. Exception e => e -> IO a
throwIO (HasCallStack => InternalError -> ClientError
InternalError -> ClientError
InternalError (InternalError -> ClientError) -> InternalError -> ClientError
forall a b. (a -> b) -> a -> b
$ ServerPacketType -> InternalError
UnexpectedPacketType ServerPacketType
otherPacket)




-- * Writing

insertInto ::
  forall table record name columns
  .
  ( table ~ Table name columns
  , WritableInto table record
  , KnownSymbol name
  )
  => Connection -> [record] -> IO ()
insertInto :: forall table record (name :: Symbol) (columns :: [*]).
(table ~ Table name columns, WritableInto table record,
 KnownSymbol name) =>
Connection -> [record] -> IO ()
insertInto Connection
conn [record]
columnsData = do
  Connection -> (ConnectionState -> IO ()) -> IO ()
forall a.
HasCallStack =>
Connection -> (ConnectionState -> IO a) -> IO a
withConnection Connection
conn ((ConnectionState -> IO ()) -> IO ())
-> (ConnectionState -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ConnectionState
connState -> do
    ConnectionState -> QueryPacket -> IO ()
forall packet.
Serializable packet =>
ConnectionState -> packet -> IO ()
writeToConnection ConnectionState
connState (ConnectionState -> ChString -> QueryPacket
mkQueryPacket ConnectionState
connState ChString
query)
    ConnectionState -> DataPacket -> IO ()
forall packet.
Serializable packet =>
ConnectionState -> packet -> IO ()
writeToConnection ConnectionState
connState (ChString -> UVarInt -> UVarInt -> DataPacket
mkDataPacket ChString
"" UVarInt
0 UVarInt
0)
    forall columns record.
WritableInto columns record =>
ConnectionState -> [record] -> IO ()
handleInsertResult @table ConnectionState
connState [record]
columnsData
  where
  query :: ChString
query = Builder -> ChString
forall chType inputType.
ToChType chType inputType =>
inputType -> chType
toChType (Builder -> ChString) -> Builder -> ChString
forall a b. (a -> b) -> a -> b
$
    Builder
"INSERT INTO " Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> (StrictByteString -> Builder
byteString (StrictByteString -> Builder)
-> (Proxy name -> StrictByteString) -> Proxy name -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> StrictByteString
BS8.pack (String -> StrictByteString)
-> (Proxy name -> String) -> Proxy name -> StrictByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (n :: Symbol) (proxy :: Symbol -> *).
KnownSymbol n =>
proxy n -> String
symbolVal @name (Proxy name -> Builder) -> Proxy name -> Builder
forall a b. (a -> b) -> a -> b
$ Proxy name
forall {k} (t :: k). Proxy t
Proxy)
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
" (" Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> forall columns record. WritableInto columns record => Builder
writingColumns @table @record Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
") VALUES"
    

handleInsertResult :: forall columns record . WritableInto columns record => ConnectionState -> [record] -> IO ()
handleInsertResult :: forall columns record.
WritableInto columns record =>
ConnectionState -> [record] -> IO ()
handleInsertResult conn :: ConnectionState
conn@MkConnectionState{Socket
ProtocolRevision
ChString
Buffer
ChCredential
$sel:sock:MkConnectionState :: ConnectionState -> Socket
$sel:user:MkConnectionState :: ConnectionState -> ChString
$sel:hostname:MkConnectionState :: ConnectionState -> ChString
$sel:os_user:MkConnectionState :: ConnectionState -> ChString
$sel:buffer:MkConnectionState :: ConnectionState -> Buffer
$sel:revision:MkConnectionState :: ConnectionState -> ProtocolRevision
$sel:creds:MkConnectionState :: ConnectionState -> ChCredential
sock :: Socket
user :: ChString
hostname :: ChString
os_user :: ChString
buffer :: Buffer
revision :: ProtocolRevision
creds :: ChCredential
..} [record]
records = do
  ServerPacketType
firstPacket <- Buffer -> Get ServerPacketType -> IO ServerPacketType
forall packet. Buffer -> Get packet -> IO packet
rawBufferizedRead Buffer
buffer (ProtocolRevision -> Get ServerPacketType
forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize ProtocolRevision
revision)
  case ServerPacketType
firstPacket of
    TableColumns      TableColumns
_ -> forall columns record.
WritableInto columns record =>
ConnectionState -> [record] -> IO ()
handleInsertResult @columns ConnectionState
conn [record]
records
    DataResponse MkDataPacket{} -> do
      ()
_emptyDataPacket <- Buffer -> Get () -> IO ()
forall packet. Buffer -> Get packet -> IO packet
rawBufferizedRead Buffer
buffer (forall columns record.
WritableInto columns record =>
ProtocolRevision -> Get ()
deserializeInsertHeader @columns @record ProtocolRevision
revision)
      ConnectionState -> DataPacket -> IO ()
forall packet.
Serializable packet =>
ConnectionState -> packet -> IO ()
writeToConnection ConnectionState
conn (ChString -> UVarInt -> UVarInt -> DataPacket
mkDataPacket ChString
"" (forall columns record. WritableInto columns record => UVarInt
columnsCount @columns @record) (Int -> UVarInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> UVarInt) -> Int -> UVarInt
forall a b. (a -> b) -> a -> b
$ [record] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
Prelude.length [record]
records))
      ConnectionState -> (ProtocolRevision -> Builder) -> IO ()
writeToConnectionEncode ConnectionState
conn (forall columns record.
WritableInto columns record =>
[record] -> ProtocolRevision -> Builder
serializeRecords @columns [record]
records)
      ConnectionState -> DataPacket -> IO ()
forall packet.
Serializable packet =>
ConnectionState -> packet -> IO ()
writeToConnection ConnectionState
conn (ChString -> UVarInt -> UVarInt -> DataPacket
mkDataPacket ChString
"" UVarInt
0 UVarInt
0)
      forall columns record.
WritableInto columns record =>
ConnectionState -> [record] -> IO ()
handleInsertResult @columns @record ConnectionState
conn []
    ServerPacketType
EndOfStream         -> () -> IO ()
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
    Exception ExceptionPacket
exception -> ClientError -> IO ()
forall e a. Exception e => e -> IO a
throwIO (HasCallStack => UserError -> ClientError
UserError -> ClientError
UserError (UserError -> ClientError) -> UserError -> ClientError
forall a b. (a -> b) -> a -> b
$ ExceptionPacket -> UserError
DatabaseException ExceptionPacket
exception)
    ServerPacketType
otherPacket         -> ClientError -> IO ()
forall e a. Exception e => e -> IO a
throwIO (HasCallStack => InternalError -> ClientError
InternalError -> ClientError
InternalError (InternalError -> ClientError) -> InternalError -> ClientError
forall a b. (a -> b) -> a -> b
$ ServerPacketType -> InternalError
UnexpectedPacketType ServerPacketType
otherPacket)




-- * Bufferization

data Buffer = MkBuffer
  { Buffer -> Int
bufferSize :: Int
  , Buffer -> Socket
bufferSocket :: Socket
  , Buffer -> IORef StrictByteString
buff :: IORef StrictByteString
  }

initBuffer :: Int -> Socket -> IO Buffer
initBuffer :: Int -> Socket -> IO Buffer
initBuffer Int
size Socket
sock = Int -> Socket -> IORef StrictByteString -> Buffer
MkBuffer Int
size Socket
sock (IORef StrictByteString -> Buffer)
-> IO (IORef StrictByteString) -> IO Buffer
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> StrictByteString -> IO (IORef StrictByteString)
forall a. a -> IO (IORef a)
newIORef StrictByteString
""

flushBuffer :: Buffer -> IO ()
flushBuffer :: Buffer -> IO ()
flushBuffer MkBuffer{IORef StrictByteString
$sel:buff:MkBuffer :: Buffer -> IORef StrictByteString
buff :: IORef StrictByteString
buff} = IORef StrictByteString -> StrictByteString -> IO ()
forall a. IORef a -> a -> IO ()
atomicWriteIORef IORef StrictByteString
buff StrictByteString
""

readBuffer ::  Buffer -> IO StrictByteString
readBuffer :: Buffer -> IO StrictByteString
readBuffer buffer :: Buffer
buffer@MkBuffer{Int
IORef StrictByteString
Socket
$sel:bufferSize:MkBuffer :: Buffer -> Int
$sel:bufferSocket:MkBuffer :: Buffer -> Socket
$sel:buff:MkBuffer :: Buffer -> IORef StrictByteString
bufferSize :: Int
bufferSocket :: Socket
buff :: IORef StrictByteString
..} =
  IORef StrictByteString -> IO StrictByteString
forall a. IORef a -> IO a
readIORef IORef StrictByteString
buff
    IO StrictByteString
-> (StrictByteString -> IO StrictByteString) -> IO StrictByteString
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (\StrictByteString
currentBuffer ->
      case StrictByteString -> Int
BS.length StrictByteString
currentBuffer of
        Int
0 -> Socket -> Int -> IO StrictByteString
recv Socket
bufferSocket Int
bufferSize
        Int
_ -> Buffer -> IO ()
flushBuffer Buffer
buffer IO () -> IO StrictByteString -> IO StrictByteString
forall a b. IO a -> IO b -> IO b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> StrictByteString -> IO StrictByteString
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure StrictByteString
currentBuffer
    )

writeToBuffer :: Buffer -> StrictByteString -> IO ()
writeToBuffer :: Buffer -> StrictByteString -> IO ()
writeToBuffer MkBuffer{Int
IORef StrictByteString
Socket
$sel:bufferSize:MkBuffer :: Buffer -> Int
$sel:bufferSocket:MkBuffer :: Buffer -> Socket
$sel:buff:MkBuffer :: Buffer -> IORef StrictByteString
bufferSize :: Int
bufferSocket :: Socket
buff :: IORef StrictByteString
..} StrictByteString
val = IO StrictByteString -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IORef StrictByteString
-> (StrictByteString -> (StrictByteString, StrictByteString))
-> IO StrictByteString
forall a b. IORef a -> (a -> (a, b)) -> IO b
atomicModifyIORef IORef StrictByteString
buff (StrictByteString
val,))

rawBufferizedRead :: Buffer -> Get packet -> IO packet
rawBufferizedRead :: forall packet. Buffer -> Get packet -> IO packet
rawBufferizedRead Buffer
buffer Get packet
parser = Buffer -> Decoder packet -> IO packet
forall packet. Buffer -> Decoder packet -> IO packet
runBufferReader Buffer
buffer (Get packet -> Decoder packet
forall a. Get a -> Decoder a
runGetIncremental Get packet
parser)

runBufferReader :: Buffer -> Decoder packet -> IO packet
runBufferReader :: forall packet. Buffer -> Decoder packet -> IO packet
runBufferReader Buffer
buffer = \case
  (Partial Maybe StrictByteString -> Decoder packet
decoder) -> Buffer -> IO StrictByteString
readBuffer Buffer
buffer IO StrictByteString -> (StrictByteString -> IO packet) -> IO packet
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Buffer -> Decoder packet -> IO packet
forall packet. Buffer -> Decoder packet -> IO packet
runBufferReader Buffer
buffer (Decoder packet -> IO packet)
-> (StrictByteString -> Decoder packet)
-> StrictByteString
-> IO packet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe StrictByteString -> Decoder packet
decoder (Maybe StrictByteString -> Decoder packet)
-> (StrictByteString -> Maybe StrictByteString)
-> StrictByteString
-> Decoder packet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StrictByteString -> Maybe StrictByteString
forall a. a -> Maybe a
Just
  (Done StrictByteString
leftover Int64
_consumed packet
packet) -> packet
packet packet -> IO () -> IO packet
forall a b. a -> IO b -> IO a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Buffer -> StrictByteString -> IO ()
writeToBuffer Buffer
buffer StrictByteString
leftover
  (Fail StrictByteString
_leftover Int64
_consumed String
msg) -> ClientError -> IO packet
forall e a. Exception e => e -> IO a
throwIO (HasCallStack => InternalError -> ClientError
InternalError -> ClientError
InternalError (InternalError -> ClientError) -> InternalError -> ClientError
forall a b. (a -> b) -> a -> b
$ String -> InternalError
DeserializationError String
msg)




-- * Errors handling

data ClientError where
  UserError :: HasCallStack => UserError -> ClientError
  InternalError :: HasCallStack => InternalError -> ClientError

instance Show ClientError where
  show :: ClientError -> String
show (UserError UserError
err)         = String
"UserError " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> UserError -> String
forall a. Show a => a -> String
show UserError
err String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"\n" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> CallStack -> String
prettyCallStack CallStack
HasCallStack => CallStack
callStack
  show (InternalError InternalError
err)     = String
"InternalError " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> InternalError -> String
forall a. Show a => a -> String
show InternalError
err String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"\n" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> CallStack -> String
prettyCallStack CallStack
HasCallStack => CallStack
callStack

deriving anyclass instance Exception ClientError

{- |
  You shouldn't see this exceptions. Please report a bug if it appears
-}
data InternalError
  = UnexpectedPacketType ServerPacketType
  | DeserializationError String
  deriving (Int -> InternalError -> ShowS
[InternalError] -> ShowS
InternalError -> String
(Int -> InternalError -> ShowS)
-> (InternalError -> String)
-> ([InternalError] -> ShowS)
-> Show InternalError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> InternalError -> ShowS
showsPrec :: Int -> InternalError -> ShowS
$cshow :: InternalError -> String
show :: InternalError -> String
$cshowList :: [InternalError] -> ShowS
showList :: [InternalError] -> ShowS
Show, Show InternalError
Typeable InternalError
(Typeable InternalError, Show InternalError) =>
(InternalError -> SomeException)
-> (SomeException -> Maybe InternalError)
-> (InternalError -> String)
-> Exception InternalError
SomeException -> Maybe InternalError
InternalError -> String
InternalError -> SomeException
forall e.
(Typeable e, Show e) =>
(e -> SomeException)
-> (SomeException -> Maybe e) -> (e -> String) -> Exception e
$ctoException :: InternalError -> SomeException
toException :: InternalError -> SomeException
$cfromException :: SomeException -> Maybe InternalError
fromException :: SomeException -> Maybe InternalError
$cdisplayException :: InternalError -> String
displayException :: InternalError -> String
Exception)

data UserError
  = UnmatchedType String
  | UnmatchedColumn String
  | DatabaseException ExceptionPacket
  deriving (Int -> UserError -> ShowS
[UserError] -> ShowS
UserError -> String
(Int -> UserError -> ShowS)
-> (UserError -> String)
-> ([UserError] -> ShowS)
-> Show UserError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> UserError -> ShowS
showsPrec :: Int -> UserError -> ShowS
$cshow :: UserError -> String
show :: UserError -> String
$cshowList :: [UserError] -> ShowS
showList :: [UserError] -> ShowS
Show, Show UserError
Typeable UserError
(Typeable UserError, Show UserError) =>
(UserError -> SomeException)
-> (SomeException -> Maybe UserError)
-> (UserError -> String)
-> Exception UserError
SomeException -> Maybe UserError
UserError -> String
UserError -> SomeException
forall e.
(Typeable e, Show e) =>
(e -> SomeException)
-> (SomeException -> Maybe e) -> (e -> String) -> Exception e
$ctoException :: UserError -> SomeException
toException :: UserError -> SomeException
$cfromException :: SomeException -> Maybe UserError
fromException :: SomeException -> Maybe UserError
$cdisplayException :: UserError -> String
displayException :: UserError -> String
Exception)




-- * Client packets

data ClientPacketType
  = Hello | Query | Data | Cancel | Ping | TablesStatusRequest
  | KeepAlive | Scalar | IgnoredPartUUIDs | ReadTaskResponse
  | MergeTreeReadTaskResponse | SSHChallengeRequest | SSHChallengeResponse
  deriving (Int -> ClientPacketType
ClientPacketType -> Int
ClientPacketType -> [ClientPacketType]
ClientPacketType -> ClientPacketType
ClientPacketType -> ClientPacketType -> [ClientPacketType]
ClientPacketType
-> ClientPacketType -> ClientPacketType -> [ClientPacketType]
(ClientPacketType -> ClientPacketType)
-> (ClientPacketType -> ClientPacketType)
-> (Int -> ClientPacketType)
-> (ClientPacketType -> Int)
-> (ClientPacketType -> [ClientPacketType])
-> (ClientPacketType -> ClientPacketType -> [ClientPacketType])
-> (ClientPacketType -> ClientPacketType -> [ClientPacketType])
-> (ClientPacketType
    -> ClientPacketType -> ClientPacketType -> [ClientPacketType])
-> Enum ClientPacketType
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
$csucc :: ClientPacketType -> ClientPacketType
succ :: ClientPacketType -> ClientPacketType
$cpred :: ClientPacketType -> ClientPacketType
pred :: ClientPacketType -> ClientPacketType
$ctoEnum :: Int -> ClientPacketType
toEnum :: Int -> ClientPacketType
$cfromEnum :: ClientPacketType -> Int
fromEnum :: ClientPacketType -> Int
$cenumFrom :: ClientPacketType -> [ClientPacketType]
enumFrom :: ClientPacketType -> [ClientPacketType]
$cenumFromThen :: ClientPacketType -> ClientPacketType -> [ClientPacketType]
enumFromThen :: ClientPacketType -> ClientPacketType -> [ClientPacketType]
$cenumFromTo :: ClientPacketType -> ClientPacketType -> [ClientPacketType]
enumFromTo :: ClientPacketType -> ClientPacketType -> [ClientPacketType]
$cenumFromThenTo :: ClientPacketType
-> ClientPacketType -> ClientPacketType -> [ClientPacketType]
enumFromThenTo :: ClientPacketType
-> ClientPacketType -> ClientPacketType -> [ClientPacketType]
Enum, Int -> ClientPacketType -> ShowS
[ClientPacketType] -> ShowS
ClientPacketType -> String
(Int -> ClientPacketType -> ShowS)
-> (ClientPacketType -> String)
-> ([ClientPacketType] -> ShowS)
-> Show ClientPacketType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ClientPacketType -> ShowS
showsPrec :: Int -> ClientPacketType -> ShowS
$cshow :: ClientPacketType -> String
show :: ClientPacketType -> String
$cshowList :: [ClientPacketType] -> ShowS
showList :: [ClientPacketType] -> ShowS
Show)

type family PacketTypeNumber (packetType :: ClientPacketType)
  where
  PacketTypeNumber Hello = 0
  PacketTypeNumber Query = 1
  PacketTypeNumber Data = 2
  PacketTypeNumber Cancel = 3
  PacketTypeNumber Ping = 4
  PacketTypeNumber TablesStatusRequest = 5
  PacketTypeNumber ClickHaskell.KeepAlive = 6
  PacketTypeNumber Scalar = 7
  PacketTypeNumber IgnoredPartUUIDs = 8
  PacketTypeNumber ReadTaskResponse = 9
  PacketTypeNumber MergeTreeReadTaskResponse = 10
  PacketTypeNumber SSHChallengeRequest = 11
  PacketTypeNumber SSHChallengeResponse = 12

data Packet (packetType :: ClientPacketType) = MkPacket
instance KnownNat (PacketTypeNumber packetType) => Show (Packet (packetType :: ClientPacketType)) where
  show :: Packet packetType -> String
show Packet packetType
_ = ClientPacketType -> String
forall a. Show a => a -> String
show (ClientPacketType -> String)
-> (UVarInt -> ClientPacketType) -> UVarInt -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Enum a => Int -> a
toEnum @ClientPacketType (Int -> ClientPacketType)
-> (UVarInt -> Int) -> UVarInt -> ClientPacketType
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UVarInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (UVarInt -> String) -> UVarInt -> String
forall a b. (a -> b) -> a -> b
$ forall (packetType :: ClientPacketType).
KnownNat (PacketTypeNumber packetType) =>
UVarInt
packetNumVal @packetType

packetNumVal :: forall packetType . KnownNat (PacketTypeNumber packetType) => UVarInt
packetNumVal :: forall (packetType :: ClientPacketType).
KnownNat (PacketTypeNumber packetType) =>
UVarInt
packetNumVal = Integer -> UVarInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Integer -> UVarInt)
-> (Proxy (PacketTypeNumber packetType) -> Integer)
-> Proxy (PacketTypeNumber packetType)
-> UVarInt
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Proxy (PacketTypeNumber packetType) -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (Proxy (PacketTypeNumber packetType) -> UVarInt)
-> Proxy (PacketTypeNumber packetType) -> UVarInt
forall a b. (a -> b) -> a -> b
$ forall (t :: Nat). Proxy t
forall {k} (t :: k). Proxy t
Proxy @(PacketTypeNumber packetType)

instance
  KnownNat (PacketTypeNumber packetType)
  =>
  Serializable (Packet (packetType :: ClientPacketType)) where
  serialize :: ProtocolRevision -> Packet packetType -> Builder
serialize ProtocolRevision
rev Packet packetType
_ = forall chType.
Serializable chType =>
ProtocolRevision -> chType -> Builder
serialize @UVarInt ProtocolRevision
rev (forall (packetType :: ClientPacketType).
KnownNat (PacketTypeNumber packetType) =>
UVarInt
packetNumVal @packetType)

instance Deserializable (Packet (packetType :: ClientPacketType)) where
  deserialize :: ProtocolRevision -> Get (Packet packetType)
deserialize ProtocolRevision
_rev = Packet packetType -> Get (Packet packetType)
forall a. a -> Get a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall (packetType :: ClientPacketType). Packet packetType
MkPacket @packetType)


-- ** Hello

data HelloParameters = MkHelloParameters
  { HelloParameters -> Text
chDatabase :: Text
  , HelloParameters -> Text
chLogin :: Text
  , HelloParameters -> Text
chPass :: Text
  }

mkHelloPacket :: HelloParameters -> HelloPacket
mkHelloPacket :: HelloParameters -> HelloPacket
mkHelloPacket MkHelloParameters{Text
$sel:chDatabase:MkHelloParameters :: HelloParameters -> Text
chDatabase :: Text
chDatabase, Text
$sel:chLogin:MkHelloParameters :: HelloParameters -> Text
chLogin :: Text
chLogin, Text
$sel:chPass:MkHelloParameters :: HelloParameters -> Text
chPass :: Text
chPass} =
  MkHelloPacket
    { $sel:packet_type:MkHelloPacket :: Packet 'Hello
packet_type          = Packet 'Hello
forall (packetType :: ClientPacketType). Packet packetType
MkPacket
    , ChString
$sel:client_name:MkHelloPacket :: ChString
client_name :: ChString
client_name, UVarInt
$sel:client_version_major:MkHelloPacket :: UVarInt
client_version_major :: UVarInt
client_version_major, UVarInt
$sel:client_version_minor:MkHelloPacket :: UVarInt
client_version_minor :: UVarInt
client_version_minor
    , $sel:tcp_protocol_version:MkHelloPacket :: ProtocolRevision
tcp_protocol_version = ProtocolRevision
latestSupportedRevision
    , $sel:default_database:MkHelloPacket :: ChString
default_database     = Text -> ChString
forall chType inputType.
ToChType chType inputType =>
inputType -> chType
toChType Text
chDatabase
    , $sel:user:MkHelloPacket :: ChString
user                 = Text -> ChString
forall chType inputType.
ToChType chType inputType =>
inputType -> chType
toChType Text
chLogin
    , $sel:password:MkHelloPacket :: ChString
password             = Text -> ChString
forall chType inputType.
ToChType chType inputType =>
inputType -> chType
toChType Text
chPass
    }

data HelloPacket = MkHelloPacket
  { HelloPacket -> Packet 'Hello
packet_type          :: Packet Hello
  , HelloPacket -> ChString
client_name          :: ChString
  , HelloPacket -> UVarInt
client_version_major :: UVarInt
  , HelloPacket -> UVarInt
client_version_minor :: UVarInt
  , HelloPacket -> ProtocolRevision
tcp_protocol_version :: ProtocolRevision
  , HelloPacket -> ChString
default_database     :: ChString
  , HelloPacket -> ChString
user                 :: ChString
  , HelloPacket -> ChString
password             :: ChString
  }
  deriving ((forall x. HelloPacket -> Rep HelloPacket x)
-> (forall x. Rep HelloPacket x -> HelloPacket)
-> Generic HelloPacket
forall x. Rep HelloPacket x -> HelloPacket
forall x. HelloPacket -> Rep HelloPacket x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. HelloPacket -> Rep HelloPacket x
from :: forall x. HelloPacket -> Rep HelloPacket x
$cto :: forall x. Rep HelloPacket x -> HelloPacket
to :: forall x. Rep HelloPacket x -> HelloPacket
Generic, ProtocolRevision -> HelloPacket -> Builder
(ProtocolRevision -> HelloPacket -> Builder)
-> Serializable HelloPacket
forall chType.
(ProtocolRevision -> chType -> Builder) -> Serializable chType
$cserialize :: ProtocolRevision -> HelloPacket -> Builder
serialize :: ProtocolRevision -> HelloPacket -> Builder
Serializable)


data Addendum = MkAddendum{Addendum
-> SinceRevision ChString DBMS_MIN_PROTOCOL_VERSION_WITH_QUOTA_KEY
quota_key :: ChString `SinceRevision` DBMS_MIN_PROTOCOL_VERSION_WITH_QUOTA_KEY}
  deriving ((forall x. Addendum -> Rep Addendum x)
-> (forall x. Rep Addendum x -> Addendum) -> Generic Addendum
forall x. Rep Addendum x -> Addendum
forall x. Addendum -> Rep Addendum x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Addendum -> Rep Addendum x
from :: forall x. Addendum -> Rep Addendum x
$cto :: forall x. Rep Addendum x -> Addendum
to :: forall x. Rep Addendum x -> Addendum
Generic, ProtocolRevision -> Addendum -> Builder
(ProtocolRevision -> Addendum -> Builder) -> Serializable Addendum
forall chType.
(ProtocolRevision -> chType -> Builder) -> Serializable chType
$cserialize :: ProtocolRevision -> Addendum -> Builder
serialize :: ProtocolRevision -> Addendum -> Builder
Serializable)
mkAddendum :: Addendum
mkAddendum :: Addendum
mkAddendum = MkAddendum{$sel:quota_key:MkAddendum :: SinceRevision ChString DBMS_MIN_PROTOCOL_VERSION_WITH_QUOTA_KEY
quota_key = ChString
-> SinceRevision ChString DBMS_MIN_PROTOCOL_VERSION_WITH_QUOTA_KEY
forall a (revisionNumber :: Nat).
a -> SinceRevision a revisionNumber
MkSinceRevision ChString
""}


-- ** Ping

data PingPacket = MkPingPacket{PingPacket -> Packet 'Ping
packet_type :: Packet Ping}
  deriving ((forall x. PingPacket -> Rep PingPacket x)
-> (forall x. Rep PingPacket x -> PingPacket) -> Generic PingPacket
forall x. Rep PingPacket x -> PingPacket
forall x. PingPacket -> Rep PingPacket x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. PingPacket -> Rep PingPacket x
from :: forall x. PingPacket -> Rep PingPacket x
$cto :: forall x. Rep PingPacket x -> PingPacket
to :: forall x. Rep PingPacket x -> PingPacket
Generic, ProtocolRevision -> PingPacket -> Builder
(ProtocolRevision -> PingPacket -> Builder)
-> Serializable PingPacket
forall chType.
(ProtocolRevision -> chType -> Builder) -> Serializable chType
$cserialize :: ProtocolRevision -> PingPacket -> Builder
serialize :: ProtocolRevision -> PingPacket -> Builder
Serializable)
mkPingPacket :: PingPacket
mkPingPacket :: PingPacket
mkPingPacket = MkPingPacket{$sel:packet_type:MkPingPacket :: Packet 'Ping
packet_type = Packet 'Ping
forall (packetType :: ClientPacketType). Packet packetType
MkPacket}


-- ** Query

mkQueryPacket :: ConnectionState -> ChString -> QueryPacket
mkQueryPacket :: ConnectionState -> ChString -> QueryPacket
mkQueryPacket MkConnectionState{Socket
ProtocolRevision
ChString
Buffer
ChCredential
$sel:sock:MkConnectionState :: ConnectionState -> Socket
$sel:user:MkConnectionState :: ConnectionState -> ChString
$sel:hostname:MkConnectionState :: ConnectionState -> ChString
$sel:os_user:MkConnectionState :: ConnectionState -> ChString
$sel:buffer:MkConnectionState :: ConnectionState -> Buffer
$sel:revision:MkConnectionState :: ConnectionState -> ProtocolRevision
$sel:creds:MkConnectionState :: ConnectionState -> ChCredential
sock :: Socket
user :: ChString
hostname :: ChString
os_user :: ChString
buffer :: Buffer
revision :: ProtocolRevision
creds :: ChCredential
..} ChString
query = MkQueryPacket
  { $sel:query_packet:MkQueryPacket :: Packet 'Query
query_packet = Packet 'Query
forall (packetType :: ClientPacketType). Packet packetType
MkPacket
  , $sel:query_id:MkQueryPacket :: ChString
query_id = ChString
""
  , $sel:client_info:MkQueryPacket :: SinceRevision ClientInfo DBMS_MIN_REVISION_WITH_CLIENT_INFO
client_info                    = ClientInfo
-> SinceRevision ClientInfo DBMS_MIN_REVISION_WITH_CLIENT_INFO
forall a (revisionNumber :: Nat).
a -> SinceRevision a revisionNumber
MkSinceRevision MkClientInfo
    { $sel:query_kind:MkClientInfo :: QueryKind
query_kind                   = QueryKind
InitialQuery
    , $sel:initial_user:MkClientInfo :: ChString
initial_user                 = ChString
user
    , $sel:initial_query_id:MkClientInfo :: ChString
initial_query_id             = ChString
""
    , $sel:initial_adress:MkClientInfo :: ChString
initial_adress               = ChString
"0.0.0.0:0"
    , $sel:initial_time:MkClientInfo :: SinceRevision
  Int64 DBMS_MIN_PROTOCOL_VERSION_WITH_INITIAL_QUERY_START_TIME
initial_time                 = Int64
-> SinceRevision
     Int64 DBMS_MIN_PROTOCOL_VERSION_WITH_INITIAL_QUERY_START_TIME
forall a (revisionNumber :: Nat).
a -> SinceRevision a revisionNumber
MkSinceRevision Int64
0
    , $sel:interface_type:MkClientInfo :: UInt8
interface_type               = UInt8
1 -- [tcp - 1, http - 2]
    , ChString
os_user :: ChString
$sel:os_user:MkClientInfo :: ChString
os_user, ChString
hostname :: ChString
$sel:hostname:MkClientInfo :: ChString
hostname
    , ChString
client_name :: ChString
$sel:client_name:MkClientInfo :: ChString
client_name, UVarInt
client_version_major :: UVarInt
$sel:client_version_major:MkClientInfo :: UVarInt
client_version_major, UVarInt
client_version_minor :: UVarInt
$sel:client_version_minor:MkClientInfo :: UVarInt
client_version_minor
    , $sel:client_revision:MkClientInfo :: ProtocolRevision
client_revision              = ProtocolRevision
revision
    , $sel:quota_key:MkClientInfo :: SinceRevision
  ChString DBMS_MIN_REVISION_WITH_QUOTA_KEY_IN_CLIENT_INFO
quota_key                    = ChString
-> SinceRevision
     ChString DBMS_MIN_REVISION_WITH_QUOTA_KEY_IN_CLIENT_INFO
forall a (revisionNumber :: Nat).
a -> SinceRevision a revisionNumber
MkSinceRevision ChString
""
    , $sel:distrubuted_depth:MkClientInfo :: SinceRevision UVarInt DBMS_TCP_PROTOCOL_VERSION
distrubuted_depth            = UVarInt -> SinceRevision UVarInt DBMS_TCP_PROTOCOL_VERSION
forall a (revisionNumber :: Nat).
a -> SinceRevision a revisionNumber
MkSinceRevision UVarInt
0
    , SinceRevision UVarInt DBMS_MIN_REVISION_WITH_VERSION_PATCH
$sel:client_version_patch:MkClientInfo :: SinceRevision UVarInt DBMS_MIN_REVISION_WITH_VERSION_PATCH
client_version_patch :: SinceRevision UVarInt DBMS_MIN_REVISION_WITH_VERSION_PATCH
client_version_patch
    , $sel:open_telemetry:MkClientInfo :: SinceRevision UInt8 DBMS_MIN_REVISION_WITH_OPENTELEMETRY
open_telemetry               = UInt8 -> SinceRevision UInt8 DBMS_MIN_REVISION_WITH_OPENTELEMETRY
forall a (revisionNumber :: Nat).
a -> SinceRevision a revisionNumber
MkSinceRevision UInt8
0
    , $sel:collaborate_with_initiator:MkClientInfo :: SinceRevision UVarInt DBMS_MIN_REVISION_WITH_PARALLEL_REPLICAS
collaborate_with_initiator   = UVarInt
-> SinceRevision UVarInt DBMS_MIN_REVISION_WITH_PARALLEL_REPLICAS
forall a (revisionNumber :: Nat).
a -> SinceRevision a revisionNumber
MkSinceRevision UVarInt
0
    , $sel:count_participating_replicas:MkClientInfo :: SinceRevision UVarInt DBMS_MIN_REVISION_WITH_PARALLEL_REPLICAS
count_participating_replicas = UVarInt
-> SinceRevision UVarInt DBMS_MIN_REVISION_WITH_PARALLEL_REPLICAS
forall a (revisionNumber :: Nat).
a -> SinceRevision a revisionNumber
MkSinceRevision UVarInt
0
    , $sel:number_of_current_replica:MkClientInfo :: SinceRevision UVarInt DBMS_MIN_REVISION_WITH_PARALLEL_REPLICAS
number_of_current_replica    = UVarInt
-> SinceRevision UVarInt DBMS_MIN_REVISION_WITH_PARALLEL_REPLICAS
forall a (revisionNumber :: Nat).
a -> SinceRevision a revisionNumber
MkSinceRevision UVarInt
0
    }
  , $sel:settings:MkQueryPacket :: DbSettings
settings           = DbSettings
MkDbSettings
  , $sel:interserver_secret:MkQueryPacket :: SinceRevision ChString DBMS_MIN_REVISION_WITH_INTERSERVER_SECRET
interserver_secret = ChString
-> SinceRevision ChString DBMS_MIN_REVISION_WITH_INTERSERVER_SECRET
forall a (revisionNumber :: Nat).
a -> SinceRevision a revisionNumber
MkSinceRevision ChString
""
  , $sel:query_stage:MkQueryPacket :: QueryStage
query_stage        = QueryStage
Complete
  , $sel:compression:MkQueryPacket :: UVarInt
compression        = UVarInt
0
  , $sel:query:MkQueryPacket :: ChString
query              = ChString
query
  , $sel:parameters:MkQueryPacket :: SinceRevision
  QueryParameters DBMS_MIN_PROTOCOL_VERSION_WITH_PARAMETERS
parameters         = QueryParameters
-> SinceRevision
     QueryParameters DBMS_MIN_PROTOCOL_VERSION_WITH_PARAMETERS
forall a (revisionNumber :: Nat).
a -> SinceRevision a revisionNumber
MkSinceRevision QueryParameters
MkQueryParameters
  }

data QueryPacket = MkQueryPacket
  { QueryPacket -> Packet 'Query
query_packet       :: Packet Query
  , QueryPacket -> ChString
query_id           :: ChString
  , QueryPacket
-> SinceRevision ClientInfo DBMS_MIN_REVISION_WITH_CLIENT_INFO
client_info        :: ClientInfo `SinceRevision` DBMS_MIN_REVISION_WITH_CLIENT_INFO
  , QueryPacket -> DbSettings
settings           :: DbSettings
  , QueryPacket
-> SinceRevision ChString DBMS_MIN_REVISION_WITH_INTERSERVER_SECRET
interserver_secret :: ChString `SinceRevision` DBMS_MIN_REVISION_WITH_INTERSERVER_SECRET
  , QueryPacket -> QueryStage
query_stage        :: QueryStage
  , QueryPacket -> UVarInt
compression        :: UVarInt
  , QueryPacket -> ChString
query              :: ChString
  , QueryPacket
-> SinceRevision
     QueryParameters DBMS_MIN_PROTOCOL_VERSION_WITH_PARAMETERS
parameters         :: QueryParameters `SinceRevision` DBMS_MIN_PROTOCOL_VERSION_WITH_PARAMETERS
  }
  deriving ((forall x. QueryPacket -> Rep QueryPacket x)
-> (forall x. Rep QueryPacket x -> QueryPacket)
-> Generic QueryPacket
forall x. Rep QueryPacket x -> QueryPacket
forall x. QueryPacket -> Rep QueryPacket x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. QueryPacket -> Rep QueryPacket x
from :: forall x. QueryPacket -> Rep QueryPacket x
$cto :: forall x. Rep QueryPacket x -> QueryPacket
to :: forall x. Rep QueryPacket x -> QueryPacket
Generic, ProtocolRevision -> QueryPacket -> Builder
(ProtocolRevision -> QueryPacket -> Builder)
-> Serializable QueryPacket
forall chType.
(ProtocolRevision -> chType -> Builder) -> Serializable chType
$cserialize :: ProtocolRevision -> QueryPacket -> Builder
serialize :: ProtocolRevision -> QueryPacket -> Builder
Serializable)

data DbSettings = MkDbSettings
instance Serializable DbSettings where serialize :: ProtocolRevision -> DbSettings -> Builder
serialize ProtocolRevision
rev DbSettings
_ = forall chType.
Serializable chType =>
ProtocolRevision -> chType -> Builder
serialize @ChString ProtocolRevision
rev ChString
""

data QueryParameters = MkQueryParameters
instance Serializable QueryParameters where serialize :: ProtocolRevision -> QueryParameters -> Builder
serialize ProtocolRevision
rev QueryParameters
_ = forall chType.
Serializable chType =>
ProtocolRevision -> chType -> Builder
serialize @ChString ProtocolRevision
rev ChString
""

data QueryStage
  = FetchColumns
  | WithMergeableState
  | Complete
  | WithMergeableStateAfterAggregation
  | WithMergeableStateAfterAggregationAndLimit
  deriving (Int -> QueryStage
QueryStage -> Int
QueryStage -> [QueryStage]
QueryStage -> QueryStage
QueryStage -> QueryStage -> [QueryStage]
QueryStage -> QueryStage -> QueryStage -> [QueryStage]
(QueryStage -> QueryStage)
-> (QueryStage -> QueryStage)
-> (Int -> QueryStage)
-> (QueryStage -> Int)
-> (QueryStage -> [QueryStage])
-> (QueryStage -> QueryStage -> [QueryStage])
-> (QueryStage -> QueryStage -> [QueryStage])
-> (QueryStage -> QueryStage -> QueryStage -> [QueryStage])
-> Enum QueryStage
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
$csucc :: QueryStage -> QueryStage
succ :: QueryStage -> QueryStage
$cpred :: QueryStage -> QueryStage
pred :: QueryStage -> QueryStage
$ctoEnum :: Int -> QueryStage
toEnum :: Int -> QueryStage
$cfromEnum :: QueryStage -> Int
fromEnum :: QueryStage -> Int
$cenumFrom :: QueryStage -> [QueryStage]
enumFrom :: QueryStage -> [QueryStage]
$cenumFromThen :: QueryStage -> QueryStage -> [QueryStage]
enumFromThen :: QueryStage -> QueryStage -> [QueryStage]
$cenumFromTo :: QueryStage -> QueryStage -> [QueryStage]
enumFromTo :: QueryStage -> QueryStage -> [QueryStage]
$cenumFromThenTo :: QueryStage -> QueryStage -> QueryStage -> [QueryStage]
enumFromThenTo :: QueryStage -> QueryStage -> QueryStage -> [QueryStage]
Enum)

instance Serializable QueryStage where
  serialize :: ProtocolRevision -> QueryStage -> Builder
serialize ProtocolRevision
rev = forall chType.
Serializable chType =>
ProtocolRevision -> chType -> Builder
serialize @UVarInt ProtocolRevision
rev (UVarInt -> Builder)
-> (QueryStage -> UVarInt) -> QueryStage -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. QueryStage -> UVarInt
queryStageCode

queryStageCode :: QueryStage -> UVarInt
queryStageCode :: QueryStage -> UVarInt
queryStageCode = \case
  QueryStage
FetchColumns -> UVarInt
0
  QueryStage
WithMergeableState -> UVarInt
1
  QueryStage
Complete -> UVarInt
2
  QueryStage
WithMergeableStateAfterAggregation -> UVarInt
3
  QueryStage
WithMergeableStateAfterAggregationAndLimit -> UVarInt
4

data Flags = IMPORTANT | CUSTOM | OBSOLETE
_flagCode :: Flags -> UInt64
_flagCode :: Flags -> UInt64
_flagCode Flags
IMPORTANT = UInt64
0x01
_flagCode Flags
CUSTOM    = UInt64
0x02
_flagCode Flags
OBSOLETE  = UInt64
0x04

data ClientInfo = MkClientInfo
  { ClientInfo -> QueryKind
query_kind                   :: QueryKind
  , ClientInfo -> ChString
initial_user                 :: ChString
  , ClientInfo -> ChString
initial_query_id             :: ChString
  , ClientInfo -> ChString
initial_adress               :: ChString
  , ClientInfo
-> SinceRevision
     Int64 DBMS_MIN_PROTOCOL_VERSION_WITH_INITIAL_QUERY_START_TIME
initial_time                 :: Int64 `SinceRevision` DBMS_MIN_PROTOCOL_VERSION_WITH_INITIAL_QUERY_START_TIME
  , ClientInfo -> UInt8
interface_type               :: UInt8
  , ClientInfo -> ChString
os_user                      :: ChString
  , ClientInfo -> ChString
hostname                     :: ChString
  , ClientInfo -> ChString
client_name                  :: ChString
  , ClientInfo -> UVarInt
client_version_major         :: UVarInt
  , ClientInfo -> UVarInt
client_version_minor         :: UVarInt
  , ClientInfo -> ProtocolRevision
client_revision              :: ProtocolRevision
  , ClientInfo
-> SinceRevision
     ChString DBMS_MIN_REVISION_WITH_QUOTA_KEY_IN_CLIENT_INFO
quota_key                    :: ChString `SinceRevision` DBMS_MIN_REVISION_WITH_QUOTA_KEY_IN_CLIENT_INFO
  , ClientInfo -> SinceRevision UVarInt DBMS_TCP_PROTOCOL_VERSION
distrubuted_depth            :: UVarInt `SinceRevision` DBMS_MIN_PROTOCOL_VERSION_WITH_DISTRIBUTED_DEPTH
  , ClientInfo
-> SinceRevision UVarInt DBMS_MIN_REVISION_WITH_VERSION_PATCH
client_version_patch         :: UVarInt `SinceRevision` DBMS_MIN_REVISION_WITH_VERSION_PATCH
  , ClientInfo
-> SinceRevision UInt8 DBMS_MIN_REVISION_WITH_OPENTELEMETRY
open_telemetry               :: UInt8 `SinceRevision` DBMS_MIN_REVISION_WITH_OPENTELEMETRY
  , ClientInfo
-> SinceRevision UVarInt DBMS_MIN_REVISION_WITH_PARALLEL_REPLICAS
collaborate_with_initiator   :: UVarInt `SinceRevision` DBMS_MIN_REVISION_WITH_PARALLEL_REPLICAS
  , ClientInfo
-> SinceRevision UVarInt DBMS_MIN_REVISION_WITH_PARALLEL_REPLICAS
count_participating_replicas :: UVarInt `SinceRevision` DBMS_MIN_REVISION_WITH_PARALLEL_REPLICAS
  , ClientInfo
-> SinceRevision UVarInt DBMS_MIN_REVISION_WITH_PARALLEL_REPLICAS
number_of_current_replica    :: UVarInt `SinceRevision` DBMS_MIN_REVISION_WITH_PARALLEL_REPLICAS
  }
  deriving ((forall x. ClientInfo -> Rep ClientInfo x)
-> (forall x. Rep ClientInfo x -> ClientInfo) -> Generic ClientInfo
forall x. Rep ClientInfo x -> ClientInfo
forall x. ClientInfo -> Rep ClientInfo x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ClientInfo -> Rep ClientInfo x
from :: forall x. ClientInfo -> Rep ClientInfo x
$cto :: forall x. Rep ClientInfo x -> ClientInfo
to :: forall x. Rep ClientInfo x -> ClientInfo
Generic, ProtocolRevision -> ClientInfo -> Builder
(ProtocolRevision -> ClientInfo -> Builder)
-> Serializable ClientInfo
forall chType.
(ProtocolRevision -> chType -> Builder) -> Serializable chType
$cserialize :: ProtocolRevision -> ClientInfo -> Builder
serialize :: ProtocolRevision -> ClientInfo -> Builder
Serializable)

data QueryKind = NoQuery | InitialQuery | SecondaryQuery
  deriving (Int -> QueryKind
QueryKind -> Int
QueryKind -> [QueryKind]
QueryKind -> QueryKind
QueryKind -> QueryKind -> [QueryKind]
QueryKind -> QueryKind -> QueryKind -> [QueryKind]
(QueryKind -> QueryKind)
-> (QueryKind -> QueryKind)
-> (Int -> QueryKind)
-> (QueryKind -> Int)
-> (QueryKind -> [QueryKind])
-> (QueryKind -> QueryKind -> [QueryKind])
-> (QueryKind -> QueryKind -> [QueryKind])
-> (QueryKind -> QueryKind -> QueryKind -> [QueryKind])
-> Enum QueryKind
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
$csucc :: QueryKind -> QueryKind
succ :: QueryKind -> QueryKind
$cpred :: QueryKind -> QueryKind
pred :: QueryKind -> QueryKind
$ctoEnum :: Int -> QueryKind
toEnum :: Int -> QueryKind
$cfromEnum :: QueryKind -> Int
fromEnum :: QueryKind -> Int
$cenumFrom :: QueryKind -> [QueryKind]
enumFrom :: QueryKind -> [QueryKind]
$cenumFromThen :: QueryKind -> QueryKind -> [QueryKind]
enumFromThen :: QueryKind -> QueryKind -> [QueryKind]
$cenumFromTo :: QueryKind -> QueryKind -> [QueryKind]
enumFromTo :: QueryKind -> QueryKind -> [QueryKind]
$cenumFromThenTo :: QueryKind -> QueryKind -> QueryKind -> [QueryKind]
enumFromThenTo :: QueryKind -> QueryKind -> QueryKind -> [QueryKind]
Enum)

instance Serializable QueryKind where
  serialize :: ProtocolRevision -> QueryKind -> Builder
serialize ProtocolRevision
rev = forall chType.
Serializable chType =>
ProtocolRevision -> chType -> Builder
serialize @UInt8 ProtocolRevision
rev (UInt8 -> Builder) -> (QueryKind -> UInt8) -> QueryKind -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. QueryKind -> UInt8
queryKindToEnum

queryKindToEnum :: QueryKind -> UInt8
queryKindToEnum :: QueryKind -> UInt8
queryKindToEnum = \case QueryKind
NoQuery -> UInt8
1; QueryKind
InitialQuery -> UInt8
2; QueryKind
SecondaryQuery -> UInt8
3

-- ** Data

mkDataPacket :: ChString -> UVarInt -> UVarInt -> DataPacket
mkDataPacket :: ChString -> UVarInt -> UVarInt -> DataPacket
mkDataPacket ChString
table_name UVarInt
columns_count UVarInt
rows_count =
  MkDataPacket
    { $sel:packet_type:MkDataPacket :: Packet 'Data
packet_type   = Packet 'Data
forall (packetType :: ClientPacketType). Packet packetType
MkPacket
    , ChString
table_name :: ChString
$sel:table_name:MkDataPacket :: ChString
table_name
    , $sel:block_info:MkDataPacket :: BlockInfo
block_info    = MkBlockInfo
      { $sel:field_num1:MkBlockInfo :: UVarInt
field_num1   = UVarInt
1, $sel:is_overflows:MkBlockInfo :: UInt8
is_overflows = UInt8
0
      , $sel:field_num2:MkBlockInfo :: UVarInt
field_num2   = UVarInt
2, $sel:bucket_num:MkBlockInfo :: Int32
bucket_num   = -Int32
1
      , $sel:eof:MkBlockInfo :: UVarInt
eof          = UVarInt
0
      }
    , UVarInt
$sel:columns_count:MkDataPacket :: UVarInt
columns_count :: UVarInt
columns_count
    , UVarInt
$sel:rows_count:MkDataPacket :: UVarInt
rows_count :: UVarInt
rows_count
    }

data DataPacket = MkDataPacket
  { DataPacket -> Packet 'Data
packet_type   :: Packet Data
  , DataPacket -> ChString
table_name    :: ChString
  , DataPacket -> BlockInfo
block_info    :: BlockInfo
  , DataPacket -> UVarInt
columns_count :: UVarInt
  , DataPacket -> UVarInt
rows_count    :: UVarInt
  }
  deriving ((forall x. DataPacket -> Rep DataPacket x)
-> (forall x. Rep DataPacket x -> DataPacket) -> Generic DataPacket
forall x. Rep DataPacket x -> DataPacket
forall x. DataPacket -> Rep DataPacket x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. DataPacket -> Rep DataPacket x
from :: forall x. DataPacket -> Rep DataPacket x
$cto :: forall x. Rep DataPacket x -> DataPacket
to :: forall x. Rep DataPacket x -> DataPacket
Generic, ProtocolRevision -> DataPacket -> Builder
(ProtocolRevision -> DataPacket -> Builder)
-> Serializable DataPacket
forall chType.
(ProtocolRevision -> chType -> Builder) -> Serializable chType
$cserialize :: ProtocolRevision -> DataPacket -> Builder
serialize :: ProtocolRevision -> DataPacket -> Builder
Serializable, ProtocolRevision -> Get DataPacket
(ProtocolRevision -> Get DataPacket) -> Deserializable DataPacket
forall chType.
(ProtocolRevision -> Get chType) -> Deserializable chType
$cdeserialize :: ProtocolRevision -> Get DataPacket
deserialize :: ProtocolRevision -> Get DataPacket
Deserializable, Int -> DataPacket -> ShowS
[DataPacket] -> ShowS
DataPacket -> String
(Int -> DataPacket -> ShowS)
-> (DataPacket -> String)
-> ([DataPacket] -> ShowS)
-> Show DataPacket
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DataPacket -> ShowS
showsPrec :: Int -> DataPacket -> ShowS
$cshow :: DataPacket -> String
show :: DataPacket -> String
$cshowList :: [DataPacket] -> ShowS
showList :: [DataPacket] -> ShowS
Show)

data BlockInfo = MkBlockInfo
  { BlockInfo -> UVarInt
field_num1   :: UVarInt, BlockInfo -> UInt8
is_overflows :: UInt8
  , BlockInfo -> UVarInt
field_num2   :: UVarInt, BlockInfo -> Int32
bucket_num   :: Int32
  , BlockInfo -> UVarInt
eof          :: UVarInt
  }
  deriving ((forall x. BlockInfo -> Rep BlockInfo x)
-> (forall x. Rep BlockInfo x -> BlockInfo) -> Generic BlockInfo
forall x. Rep BlockInfo x -> BlockInfo
forall x. BlockInfo -> Rep BlockInfo x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. BlockInfo -> Rep BlockInfo x
from :: forall x. BlockInfo -> Rep BlockInfo x
$cto :: forall x. Rep BlockInfo x -> BlockInfo
to :: forall x. Rep BlockInfo x -> BlockInfo
Generic, ProtocolRevision -> BlockInfo -> Builder
(ProtocolRevision -> BlockInfo -> Builder)
-> Serializable BlockInfo
forall chType.
(ProtocolRevision -> chType -> Builder) -> Serializable chType
$cserialize :: ProtocolRevision -> BlockInfo -> Builder
serialize :: ProtocolRevision -> BlockInfo -> Builder
Serializable, ProtocolRevision -> Get BlockInfo
(ProtocolRevision -> Get BlockInfo) -> Deserializable BlockInfo
forall chType.
(ProtocolRevision -> Get chType) -> Deserializable chType
$cdeserialize :: ProtocolRevision -> Get BlockInfo
deserialize :: ProtocolRevision -> Get BlockInfo
Deserializable, Int -> BlockInfo -> ShowS
[BlockInfo] -> ShowS
BlockInfo -> String
(Int -> BlockInfo -> ShowS)
-> (BlockInfo -> String)
-> ([BlockInfo] -> ShowS)
-> Show BlockInfo
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> BlockInfo -> ShowS
showsPrec :: Int -> BlockInfo -> ShowS
$cshow :: BlockInfo -> String
show :: BlockInfo -> String
$cshowList :: [BlockInfo] -> ShowS
showList :: [BlockInfo] -> ShowS
Show)




-- * Server packets

data ServerPacketType where
  HelloResponse :: HelloResponse -> ServerPacketType
  DataResponse :: DataPacket -> ServerPacketType
  Exception :: ExceptionPacket -> ServerPacketType
  Progress :: ProgressPacket -> ServerPacketType
  Pong :: ServerPacketType
  EndOfStream :: ServerPacketType
  ProfileInfo :: ProfileInfo -> ServerPacketType
  Totals :: ServerPacketType
  Extremes :: ServerPacketType
  TablesStatusResponse :: ServerPacketType
  Log :: ServerPacketType
  TableColumns :: TableColumns -> ServerPacketType
  UUIDs :: ServerPacketType
  ReadTaskRequest :: ServerPacketType
  ProfileEvents :: ServerPacketType
  UnknownPacket :: UVarInt -> ServerPacketType

instance Deserializable ServerPacketType where
  deserialize :: ProtocolRevision -> Get ServerPacketType
deserialize ProtocolRevision
rev = do
    UVarInt
packetNum <- forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize @UVarInt ProtocolRevision
rev
    case UVarInt
packetNum of
      UVarInt
0  -> HelloResponse -> ServerPacketType
HelloResponse (HelloResponse -> ServerPacketType)
-> Get HelloResponse -> Get ServerPacketType
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ProtocolRevision -> Get HelloResponse
forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize ProtocolRevision
rev
      UVarInt
1  -> DataPacket -> ServerPacketType
DataResponse (DataPacket -> ServerPacketType)
-> Get DataPacket -> Get ServerPacketType
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ProtocolRevision -> Get DataPacket
forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize ProtocolRevision
rev
      UVarInt
2  -> ExceptionPacket -> ServerPacketType
Exception (ExceptionPacket -> ServerPacketType)
-> Get ExceptionPacket -> Get ServerPacketType
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ProtocolRevision -> Get ExceptionPacket
forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize ProtocolRevision
rev
      UVarInt
3  -> ProgressPacket -> ServerPacketType
Progress (ProgressPacket -> ServerPacketType)
-> Get ProgressPacket -> Get ServerPacketType
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ProtocolRevision -> Get ProgressPacket
forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize ProtocolRevision
rev
      UVarInt
4  -> ServerPacketType -> Get ServerPacketType
forall a. a -> Get a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ServerPacketType
Pong
      UVarInt
5  -> ServerPacketType -> Get ServerPacketType
forall a. a -> Get a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ServerPacketType
EndOfStream
      UVarInt
6  -> ProfileInfo -> ServerPacketType
ProfileInfo (ProfileInfo -> ServerPacketType)
-> Get ProfileInfo -> Get ServerPacketType
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ProtocolRevision -> Get ProfileInfo
forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize ProtocolRevision
rev
      UVarInt
7  -> ServerPacketType -> Get ServerPacketType
forall a. a -> Get a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ServerPacketType
Totals
      UVarInt
8  -> ServerPacketType -> Get ServerPacketType
forall a. a -> Get a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ServerPacketType
Extremes
      UVarInt
9  -> ServerPacketType -> Get ServerPacketType
forall a. a -> Get a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ServerPacketType
TablesStatusResponse
      UVarInt
10 -> ServerPacketType -> Get ServerPacketType
forall a. a -> Get a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ServerPacketType
Log
      UVarInt
11 -> TableColumns -> ServerPacketType
TableColumns (TableColumns -> ServerPacketType)
-> Get TableColumns -> Get ServerPacketType
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ProtocolRevision -> Get TableColumns
forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize ProtocolRevision
rev
      UVarInt
12 -> ServerPacketType -> Get ServerPacketType
forall a. a -> Get a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ServerPacketType
UUIDs
      UVarInt
13 -> ServerPacketType -> Get ServerPacketType
forall a. a -> Get a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ServerPacketType
ReadTaskRequest
      UVarInt
14 -> ServerPacketType -> Get ServerPacketType
forall a. a -> Get a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ServerPacketType
ProfileEvents
      UVarInt
_  -> ServerPacketType -> Get ServerPacketType
forall a. a -> Get a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ServerPacketType -> Get ServerPacketType)
-> ServerPacketType -> Get ServerPacketType
forall a b. (a -> b) -> a -> b
$ UVarInt -> ServerPacketType
UnknownPacket UVarInt
packetNum

instance Show ServerPacketType where
  show :: ServerPacketType -> String
show (HelloResponse HelloResponse
hello) = String
"HelloResponse " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> HelloResponse -> String
forall a. Show a => a -> String
show HelloResponse
hello
  show (DataResponse DataPacket
dataPacket) = String
"DataResponse " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> DataPacket -> String
forall a. Show a => a -> String
show DataPacket
dataPacket
  show (Exception ExceptionPacket
exception) = String
"Exception " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> ExceptionPacket -> String
forall a. Show a => a -> String
show ExceptionPacket
exception
  show (Progress ProgressPacket
progress) = String
"Progress " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> ProgressPacket -> String
forall a. Show a => a -> String
show ProgressPacket
progress
  show ServerPacketType
Pong = String
"Pong"
  show ServerPacketType
EndOfStream = String
"EndOfStream"
  show (ProfileInfo ProfileInfo
profileInfo) = String
"ProfileInfo " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> ProfileInfo -> String
forall a. Show a => a -> String
show ProfileInfo
profileInfo
  show ServerPacketType
Totals = String
"Totals"
  show ServerPacketType
Extremes = String
"Extremes"
  show ServerPacketType
TablesStatusResponse = String
"TablesStatusResponse"
  show ServerPacketType
Log = String
"Log"
  show (TableColumns TableColumns
tabelColumnsPacket) = String
"TableColumns " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> TableColumns -> String
forall a. Show a => a -> String
show TableColumns
tabelColumnsPacket
  show ServerPacketType
UUIDs = String
"UUIDs"
  show ServerPacketType
ReadTaskRequest = String
"ReadTaskRequest"
  show ServerPacketType
ProfileEvents = String
"ProfileEvents"
  show (UnknownPacket UVarInt
packetNum) = String
"UnknownPacket: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> UVarInt -> String
forall a. Show a => a -> String
show UVarInt
packetNum

-- ** HelloResponse

{-
  https://github.com/ClickHouse/ClickHouse/blob/eb4a74d7412a1fcf52727cd8b00b365d6b9ed86c/src/Client/Connection.cpp#L520
-}
data HelloResponse = MkHelloResponse
  { HelloResponse -> ChString
server_name                    :: ChString
  , HelloResponse -> UVarInt
server_version_major           :: UVarInt
  , HelloResponse -> UVarInt
server_version_minor           :: UVarInt
  , HelloResponse -> ProtocolRevision
server_revision                :: ProtocolRevision
  , HelloResponse
-> SinceRevision
     UVarInt DBMS_MIN_REVISION_WITH_VERSIONED_PARALLEL_REPLICAS_PROTOCOL
server_parallel_replicas_proto :: UVarInt  `SinceRevision` DBMS_MIN_REVISION_WITH_VERSIONED_PARALLEL_REPLICAS_PROTOCOL
  , HelloResponse
-> SinceRevision ChString DBMS_MIN_REVISION_WITH_SERVER_TIMEZONE
server_timezone                :: ChString `SinceRevision` DBMS_MIN_REVISION_WITH_SERVER_TIMEZONE
  , HelloResponse
-> SinceRevision
     ChString DBMS_MIN_REVISION_WITH_SERVER_DISPLAY_NAME
server_display_name            :: ChString `SinceRevision` DBMS_MIN_REVISION_WITH_SERVER_DISPLAY_NAME
  , HelloResponse
-> SinceRevision UVarInt DBMS_MIN_REVISION_WITH_VERSION_PATCH
server_version_patch           :: UVarInt  `SinceRevision` DBMS_MIN_REVISION_WITH_VERSION_PATCH
  , HelloResponse
-> SinceRevision
     ChString DBMS_MIN_PROTOCOL_VERSION_WITH_CHUNKED_PACKETS
proto_send_chunked_srv         :: ChString `SinceRevision` DBMS_MIN_PROTOCOL_VERSION_WITH_CHUNKED_PACKETS
  , HelloResponse
-> SinceRevision
     ChString DBMS_MIN_PROTOCOL_VERSION_WITH_CHUNKED_PACKETS
proto_recv_chunked_srv         :: ChString `SinceRevision` DBMS_MIN_PROTOCOL_VERSION_WITH_CHUNKED_PACKETS
  , HelloResponse
-> SinceRevision
     [PasswordComplexityRules]
     DBMS_MIN_PROTOCOL_VERSION_WITH_PASSWORD_COMPLEXITY_RULES
password_complexity_rules      :: [PasswordComplexityRules] `SinceRevision` DBMS_MIN_PROTOCOL_VERSION_WITH_PASSWORD_COMPLEXITY_RULES
  , HelloResponse
-> SinceRevision
     UInt64 DBMS_MIN_REVISION_WITH_INTERSERVER_SECRET_V2
read_nonce                     :: UInt64 `SinceRevision` DBMS_MIN_REVISION_WITH_INTERSERVER_SECRET_V2
  }
  deriving ((forall x. HelloResponse -> Rep HelloResponse x)
-> (forall x. Rep HelloResponse x -> HelloResponse)
-> Generic HelloResponse
forall x. Rep HelloResponse x -> HelloResponse
forall x. HelloResponse -> Rep HelloResponse x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. HelloResponse -> Rep HelloResponse x
from :: forall x. HelloResponse -> Rep HelloResponse x
$cto :: forall x. Rep HelloResponse x -> HelloResponse
to :: forall x. Rep HelloResponse x -> HelloResponse
Generic, Int -> HelloResponse -> ShowS
[HelloResponse] -> ShowS
HelloResponse -> String
(Int -> HelloResponse -> ShowS)
-> (HelloResponse -> String)
-> ([HelloResponse] -> ShowS)
-> Show HelloResponse
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> HelloResponse -> ShowS
showsPrec :: Int -> HelloResponse -> ShowS
$cshow :: HelloResponse -> String
show :: HelloResponse -> String
$cshowList :: [HelloResponse] -> ShowS
showList :: [HelloResponse] -> ShowS
Show)

instance Deserializable HelloResponse where
  deserialize :: ProtocolRevision -> Get HelloResponse
deserialize ProtocolRevision
revision = do
    ChString
server_name                    <- ProtocolRevision -> Get ChString
forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize ProtocolRevision
revision
    UVarInt
server_version_major           <- ProtocolRevision -> Get UVarInt
forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize ProtocolRevision
revision
    UVarInt
server_version_minor           <- ProtocolRevision -> Get UVarInt
forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize ProtocolRevision
revision
    ProtocolRevision
server_revision                <- ProtocolRevision -> Get ProtocolRevision
forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize ProtocolRevision
revision
    -- Override current protocol revision for backward compatibility
    let chosenRevision :: ProtocolRevision
chosenRevision = ProtocolRevision -> ProtocolRevision -> ProtocolRevision
forall a. Ord a => a -> a -> a
min ProtocolRevision
server_revision ProtocolRevision
revision
    SinceRevision
  UVarInt DBMS_MIN_REVISION_WITH_VERSIONED_PARALLEL_REPLICAS_PROTOCOL
server_parallel_replicas_proto <- ProtocolRevision
-> Get
     (SinceRevision
        UVarInt
        DBMS_MIN_REVISION_WITH_VERSIONED_PARALLEL_REPLICAS_PROTOCOL)
forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize ProtocolRevision
chosenRevision
    SinceRevision ChString DBMS_MIN_REVISION_WITH_SERVER_TIMEZONE
server_timezone                <- ProtocolRevision
-> Get
     (SinceRevision ChString DBMS_MIN_REVISION_WITH_SERVER_TIMEZONE)
forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize ProtocolRevision
chosenRevision
    SinceRevision ChString DBMS_MIN_REVISION_WITH_SERVER_DISPLAY_NAME
server_display_name            <- ProtocolRevision
-> Get
     (SinceRevision ChString DBMS_MIN_REVISION_WITH_SERVER_DISPLAY_NAME)
forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize ProtocolRevision
chosenRevision
    SinceRevision UVarInt DBMS_MIN_REVISION_WITH_VERSION_PATCH
server_version_patch           <- ProtocolRevision
-> Get (SinceRevision UVarInt DBMS_MIN_REVISION_WITH_VERSION_PATCH)
forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize ProtocolRevision
chosenRevision
    SinceRevision
  ChString DBMS_MIN_PROTOCOL_VERSION_WITH_CHUNKED_PACKETS
proto_send_chunked_srv         <- ProtocolRevision
-> Get
     (SinceRevision
        ChString DBMS_MIN_PROTOCOL_VERSION_WITH_CHUNKED_PACKETS)
forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize ProtocolRevision
chosenRevision
    SinceRevision
  ChString DBMS_MIN_PROTOCOL_VERSION_WITH_CHUNKED_PACKETS
proto_recv_chunked_srv         <- ProtocolRevision
-> Get
     (SinceRevision
        ChString DBMS_MIN_PROTOCOL_VERSION_WITH_CHUNKED_PACKETS)
forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize ProtocolRevision
chosenRevision
    SinceRevision
  [PasswordComplexityRules]
  DBMS_MIN_PROTOCOL_VERSION_WITH_PASSWORD_COMPLEXITY_RULES
password_complexity_rules      <- ProtocolRevision
-> Get
     (SinceRevision
        [PasswordComplexityRules]
        DBMS_MIN_PROTOCOL_VERSION_WITH_PASSWORD_COMPLEXITY_RULES)
forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize ProtocolRevision
chosenRevision
    SinceRevision UInt64 DBMS_MIN_REVISION_WITH_INTERSERVER_SECRET_V2
read_nonce                     <- ProtocolRevision
-> Get
     (SinceRevision UInt64 DBMS_MIN_REVISION_WITH_INTERSERVER_SECRET_V2)
forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize ProtocolRevision
chosenRevision
    HelloResponse -> Get HelloResponse
forall a. a -> Get a
forall (f :: * -> *) a. Applicative f => a -> f a
pure MkHelloResponse{SinceRevision
  [PasswordComplexityRules]
  DBMS_MIN_PROTOCOL_VERSION_WITH_PASSWORD_COMPLEXITY_RULES
SinceRevision UInt64 DBMS_MIN_REVISION_WITH_INTERSERVER_SECRET_V2
SinceRevision UVarInt DBMS_MIN_REVISION_WITH_VERSION_PATCH
SinceRevision
  UVarInt DBMS_MIN_REVISION_WITH_VERSIONED_PARALLEL_REPLICAS_PROTOCOL
SinceRevision ChString DBMS_MIN_REVISION_WITH_SERVER_TIMEZONE
SinceRevision ChString DBMS_MIN_REVISION_WITH_SERVER_DISPLAY_NAME
SinceRevision
  ChString DBMS_MIN_PROTOCOL_VERSION_WITH_CHUNKED_PACKETS
ProtocolRevision
UVarInt
ChString
$sel:server_revision:MkHelloResponse :: ProtocolRevision
$sel:server_name:MkHelloResponse :: ChString
$sel:server_version_major:MkHelloResponse :: UVarInt
$sel:server_version_minor:MkHelloResponse :: UVarInt
$sel:server_parallel_replicas_proto:MkHelloResponse :: SinceRevision
  UVarInt DBMS_MIN_REVISION_WITH_VERSIONED_PARALLEL_REPLICAS_PROTOCOL
$sel:server_timezone:MkHelloResponse :: SinceRevision ChString DBMS_MIN_REVISION_WITH_SERVER_TIMEZONE
$sel:server_display_name:MkHelloResponse :: SinceRevision ChString DBMS_MIN_REVISION_WITH_SERVER_DISPLAY_NAME
$sel:server_version_patch:MkHelloResponse :: SinceRevision UVarInt DBMS_MIN_REVISION_WITH_VERSION_PATCH
$sel:proto_send_chunked_srv:MkHelloResponse :: SinceRevision
  ChString DBMS_MIN_PROTOCOL_VERSION_WITH_CHUNKED_PACKETS
$sel:proto_recv_chunked_srv:MkHelloResponse :: SinceRevision
  ChString DBMS_MIN_PROTOCOL_VERSION_WITH_CHUNKED_PACKETS
$sel:password_complexity_rules:MkHelloResponse :: SinceRevision
  [PasswordComplexityRules]
  DBMS_MIN_PROTOCOL_VERSION_WITH_PASSWORD_COMPLEXITY_RULES
$sel:read_nonce:MkHelloResponse :: SinceRevision UInt64 DBMS_MIN_REVISION_WITH_INTERSERVER_SECRET_V2
server_name :: ChString
server_version_major :: UVarInt
server_version_minor :: UVarInt
server_revision :: ProtocolRevision
server_parallel_replicas_proto :: SinceRevision
  UVarInt DBMS_MIN_REVISION_WITH_VERSIONED_PARALLEL_REPLICAS_PROTOCOL
server_timezone :: SinceRevision ChString DBMS_MIN_REVISION_WITH_SERVER_TIMEZONE
server_display_name :: SinceRevision ChString DBMS_MIN_REVISION_WITH_SERVER_DISPLAY_NAME
server_version_patch :: SinceRevision UVarInt DBMS_MIN_REVISION_WITH_VERSION_PATCH
proto_send_chunked_srv :: SinceRevision
  ChString DBMS_MIN_PROTOCOL_VERSION_WITH_CHUNKED_PACKETS
proto_recv_chunked_srv :: SinceRevision
  ChString DBMS_MIN_PROTOCOL_VERSION_WITH_CHUNKED_PACKETS
password_complexity_rules :: SinceRevision
  [PasswordComplexityRules]
  DBMS_MIN_PROTOCOL_VERSION_WITH_PASSWORD_COMPLEXITY_RULES
read_nonce :: SinceRevision UInt64 DBMS_MIN_REVISION_WITH_INTERSERVER_SECRET_V2
..}

data PasswordComplexityRules = MkPasswordComplexityRules
  { PasswordComplexityRules -> ChString
original_pattern  :: ChString
  , PasswordComplexityRules -> ChString
exception_message :: ChString
  }
  deriving ((forall x.
 PasswordComplexityRules -> Rep PasswordComplexityRules x)
-> (forall x.
    Rep PasswordComplexityRules x -> PasswordComplexityRules)
-> Generic PasswordComplexityRules
forall x. Rep PasswordComplexityRules x -> PasswordComplexityRules
forall x. PasswordComplexityRules -> Rep PasswordComplexityRules x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. PasswordComplexityRules -> Rep PasswordComplexityRules x
from :: forall x. PasswordComplexityRules -> Rep PasswordComplexityRules x
$cto :: forall x. Rep PasswordComplexityRules x -> PasswordComplexityRules
to :: forall x. Rep PasswordComplexityRules x -> PasswordComplexityRules
Generic, ProtocolRevision -> Get PasswordComplexityRules
(ProtocolRevision -> Get PasswordComplexityRules)
-> Deserializable PasswordComplexityRules
forall chType.
(ProtocolRevision -> Get chType) -> Deserializable chType
$cdeserialize :: ProtocolRevision -> Get PasswordComplexityRules
deserialize :: ProtocolRevision -> Get PasswordComplexityRules
Deserializable, Int -> PasswordComplexityRules -> ShowS
[PasswordComplexityRules] -> ShowS
PasswordComplexityRules -> String
(Int -> PasswordComplexityRules -> ShowS)
-> (PasswordComplexityRules -> String)
-> ([PasswordComplexityRules] -> ShowS)
-> Show PasswordComplexityRules
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PasswordComplexityRules -> ShowS
showsPrec :: Int -> PasswordComplexityRules -> ShowS
$cshow :: PasswordComplexityRules -> String
show :: PasswordComplexityRules -> String
$cshowList :: [PasswordComplexityRules] -> ShowS
showList :: [PasswordComplexityRules] -> ShowS
Show)

instance Deserializable [PasswordComplexityRules] where
  deserialize :: ProtocolRevision -> Get [PasswordComplexityRules]
deserialize ProtocolRevision
rev = do
    UVarInt
len <- forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize @UVarInt ProtocolRevision
rev
    Int -> Get PasswordComplexityRules -> Get [PasswordComplexityRules]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM (UVarInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral UVarInt
len) (forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize @PasswordComplexityRules ProtocolRevision
rev)

-- ** Exception

data ExceptionPacket = MkExceptionPacket
  { ExceptionPacket -> Int32
code        :: Int32
  , ExceptionPacket -> ChString
name        :: ChString
  , ExceptionPacket -> ChString
message     :: ChString
  , ExceptionPacket -> ChString
stack_trace :: ChString
  , ExceptionPacket -> UInt8
nested      :: UInt8
  }
  deriving ((forall x. ExceptionPacket -> Rep ExceptionPacket x)
-> (forall x. Rep ExceptionPacket x -> ExceptionPacket)
-> Generic ExceptionPacket
forall x. Rep ExceptionPacket x -> ExceptionPacket
forall x. ExceptionPacket -> Rep ExceptionPacket x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ExceptionPacket -> Rep ExceptionPacket x
from :: forall x. ExceptionPacket -> Rep ExceptionPacket x
$cto :: forall x. Rep ExceptionPacket x -> ExceptionPacket
to :: forall x. Rep ExceptionPacket x -> ExceptionPacket
Generic, ProtocolRevision -> Get ExceptionPacket
(ProtocolRevision -> Get ExceptionPacket)
-> Deserializable ExceptionPacket
forall chType.
(ProtocolRevision -> Get chType) -> Deserializable chType
$cdeserialize :: ProtocolRevision -> Get ExceptionPacket
deserialize :: ProtocolRevision -> Get ExceptionPacket
Deserializable, Int -> ExceptionPacket -> ShowS
[ExceptionPacket] -> ShowS
ExceptionPacket -> String
(Int -> ExceptionPacket -> ShowS)
-> (ExceptionPacket -> String)
-> ([ExceptionPacket] -> ShowS)
-> Show ExceptionPacket
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ExceptionPacket -> ShowS
showsPrec :: Int -> ExceptionPacket -> ShowS
$cshow :: ExceptionPacket -> String
show :: ExceptionPacket -> String
$cshowList :: [ExceptionPacket] -> ShowS
showList :: [ExceptionPacket] -> ShowS
Show)

-- ** Progress

data ProgressPacket = MkProgressPacket
  { ProgressPacket -> UVarInt
rows        :: UVarInt
  , ProgressPacket -> UVarInt
bytes       :: UVarInt
  , ProgressPacket -> UVarInt
total_rows  :: UVarInt
  , ProgressPacket
-> SinceRevision
     UVarInt DBMS_MIN_PROTOCOL_VERSION_WITH_TOTAL_BYTES_IN_PROGRESS
total_bytes :: UVarInt `SinceRevision` DBMS_MIN_PROTOCOL_VERSION_WITH_TOTAL_BYTES_IN_PROGRESS
  , ProgressPacket
-> SinceRevision
     UVarInt DBMS_MIN_PROTOCOL_VERSION_WITH_TOTAL_BYTES_IN_PROGRESS
wrote_rows  :: UVarInt `SinceRevision` DBMS_MIN_PROTOCOL_VERSION_WITH_TOTAL_BYTES_IN_PROGRESS
  , ProgressPacket
-> SinceRevision UVarInt DBMS_MIN_REVISION_WITH_CLIENT_WRITE_INFO
wrote_bytes :: UVarInt `SinceRevision` DBMS_MIN_REVISION_WITH_CLIENT_WRITE_INFO
  , ProgressPacket
-> SinceRevision UVarInt DBMS_MIN_REVISION_WITH_CLIENT_WRITE_INFO
elapsed_ns  :: UVarInt `SinceRevision` DBMS_MIN_REVISION_WITH_CLIENT_WRITE_INFO
  }
  deriving ((forall x. ProgressPacket -> Rep ProgressPacket x)
-> (forall x. Rep ProgressPacket x -> ProgressPacket)
-> Generic ProgressPacket
forall x. Rep ProgressPacket x -> ProgressPacket
forall x. ProgressPacket -> Rep ProgressPacket x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ProgressPacket -> Rep ProgressPacket x
from :: forall x. ProgressPacket -> Rep ProgressPacket x
$cto :: forall x. Rep ProgressPacket x -> ProgressPacket
to :: forall x. Rep ProgressPacket x -> ProgressPacket
Generic, ProtocolRevision -> Get ProgressPacket
(ProtocolRevision -> Get ProgressPacket)
-> Deserializable ProgressPacket
forall chType.
(ProtocolRevision -> Get chType) -> Deserializable chType
$cdeserialize :: ProtocolRevision -> Get ProgressPacket
deserialize :: ProtocolRevision -> Get ProgressPacket
Deserializable, Int -> ProgressPacket -> ShowS
[ProgressPacket] -> ShowS
ProgressPacket -> String
(Int -> ProgressPacket -> ShowS)
-> (ProgressPacket -> String)
-> ([ProgressPacket] -> ShowS)
-> Show ProgressPacket
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ProgressPacket -> ShowS
showsPrec :: Int -> ProgressPacket -> ShowS
$cshow :: ProgressPacket -> String
show :: ProgressPacket -> String
$cshowList :: [ProgressPacket] -> ShowS
showList :: [ProgressPacket] -> ShowS
Show)

-- ** ProfileInfo

data ProfileInfo = MkProfileInfo
  { ProfileInfo -> UVarInt
rows                         :: UVarInt
  , ProfileInfo -> UVarInt
blocks                       :: UVarInt
  , ProfileInfo -> UVarInt
bytes                        :: UVarInt
  , ProfileInfo -> UInt8
applied_limit                :: UInt8
  , ProfileInfo -> UVarInt
rows_before_limit            :: UVarInt
  , ProfileInfo -> UInt8
calculated_rows_before_limit :: UInt8
  , ProfileInfo
-> SinceRevision
     UInt8 DBMS_MIN_REVISION_WITH_ROWS_BEFORE_AGGREGATION
applied_aggregation          :: UInt8 `SinceRevision` DBMS_MIN_REVISION_WITH_ROWS_BEFORE_AGGREGATION
  , ProfileInfo
-> SinceRevision
     UVarInt DBMS_MIN_REVISION_WITH_ROWS_BEFORE_AGGREGATION
rows_before_aggregation      :: UVarInt `SinceRevision` DBMS_MIN_REVISION_WITH_ROWS_BEFORE_AGGREGATION
  }
  deriving ((forall x. ProfileInfo -> Rep ProfileInfo x)
-> (forall x. Rep ProfileInfo x -> ProfileInfo)
-> Generic ProfileInfo
forall x. Rep ProfileInfo x -> ProfileInfo
forall x. ProfileInfo -> Rep ProfileInfo x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ProfileInfo -> Rep ProfileInfo x
from :: forall x. ProfileInfo -> Rep ProfileInfo x
$cto :: forall x. Rep ProfileInfo x -> ProfileInfo
to :: forall x. Rep ProfileInfo x -> ProfileInfo
Generic, ProtocolRevision -> Get ProfileInfo
(ProtocolRevision -> Get ProfileInfo) -> Deserializable ProfileInfo
forall chType.
(ProtocolRevision -> Get chType) -> Deserializable chType
$cdeserialize :: ProtocolRevision -> Get ProfileInfo
deserialize :: ProtocolRevision -> Get ProfileInfo
Deserializable, Int -> ProfileInfo -> ShowS
[ProfileInfo] -> ShowS
ProfileInfo -> String
(Int -> ProfileInfo -> ShowS)
-> (ProfileInfo -> String)
-> ([ProfileInfo] -> ShowS)
-> Show ProfileInfo
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ProfileInfo -> ShowS
showsPrec :: Int -> ProfileInfo -> ShowS
$cshow :: ProfileInfo -> String
show :: ProfileInfo -> String
$cshowList :: [ProfileInfo] -> ShowS
showList :: [ProfileInfo] -> ShowS
Show)

-- ** TableColumns

data TableColumns = MkTableColumns
  { TableColumns -> ChString
table_name :: ChString
  , TableColumns -> ChString
table_columns :: ChString
  }
  deriving ((forall x. TableColumns -> Rep TableColumns x)
-> (forall x. Rep TableColumns x -> TableColumns)
-> Generic TableColumns
forall x. Rep TableColumns x -> TableColumns
forall x. TableColumns -> Rep TableColumns x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. TableColumns -> Rep TableColumns x
from :: forall x. TableColumns -> Rep TableColumns x
$cto :: forall x. Rep TableColumns x -> TableColumns
to :: forall x. Rep TableColumns x -> TableColumns
Generic, ProtocolRevision -> Get TableColumns
(ProtocolRevision -> Get TableColumns)
-> Deserializable TableColumns
forall chType.
(ProtocolRevision -> Get chType) -> Deserializable chType
$cdeserialize :: ProtocolRevision -> Get TableColumns
deserialize :: ProtocolRevision -> Get TableColumns
Deserializable, Int -> TableColumns -> ShowS
[TableColumns] -> ShowS
TableColumns -> String
(Int -> TableColumns -> ShowS)
-> (TableColumns -> String)
-> ([TableColumns] -> ShowS)
-> Show TableColumns
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TableColumns -> ShowS
showsPrec :: Int -> TableColumns -> ShowS
$cshow :: TableColumns -> String
show :: TableColumns -> String
$cshowList :: [TableColumns] -> ShowS
showList :: [TableColumns] -> ShowS
Show)








-- * Deserialization

-- ** Generic API

type GenericReadable record hasColumns =
  ( Generic record
  , GReadable (GetColumns hasColumns) (Rep record)
  )

class HasColumns hasColumns => ReadableFrom hasColumns record
  where
  default deserializeColumns :: GenericReadable record hasColumns => ProtocolRevision -> UVarInt -> Get [record]
  deserializeColumns :: ProtocolRevision -> UVarInt -> Get [record]
  deserializeColumns ProtocolRevision
rev UVarInt
size = do
    [Rep record Any]
list <- forall (columns :: [*]) (f :: * -> *) p.
GReadable columns f =>
ProtocolRevision -> UVarInt -> Get [f p]
gFromColumns @(GetColumns hasColumns) ProtocolRevision
rev UVarInt
size
    [record] -> Get [record]
forall a. a -> Get a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([record] -> Get [record]) -> [record] -> Get [record]
forall a b. (a -> b) -> a -> b
$ do
      Rep record Any
element <- [Rep record Any]
list
      case Rep record Any -> record
forall a x. Generic a => Rep a x -> a
forall x. Rep record x -> record
to Rep record Any
element of record
res -> record -> [record]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (record -> [record]) -> record -> [record]
forall a b. (a -> b) -> a -> b
$! record
res

  default readingColumns :: GenericReadable record hasColumns => Builder
  readingColumns :: Builder
  readingColumns
    = Builder
-> ((Builder, [Builder]) -> Builder)
-> Maybe (Builder, [Builder])
-> Builder
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Builder
"" ((Builder -> [Builder] -> Builder)
-> (Builder, [Builder]) -> Builder
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry ((Builder -> Builder -> Builder) -> Builder -> [Builder] -> Builder
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl (\Builder
a Builder
b -> Builder
a Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
", " Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
b))) (Maybe (Builder, [Builder]) -> Builder)
-> ([(Builder, Builder)] -> Maybe (Builder, [Builder]))
-> [(Builder, Builder)]
-> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Builder] -> Maybe (Builder, [Builder])
forall a. [a] -> Maybe (a, [a])
uncons
    ([Builder] -> Maybe (Builder, [Builder]))
-> ([(Builder, Builder)] -> [Builder])
-> [(Builder, Builder)]
-> Maybe (Builder, [Builder])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Builder, Builder) -> Builder)
-> [(Builder, Builder)] -> [Builder]
forall a b. (a -> b) -> [a] -> [b]
map (Builder, Builder) -> Builder
forall a b. (a, b) -> a
fst
    ([(Builder, Builder)] -> Builder)
-> [(Builder, Builder)] -> Builder
forall a b. (a -> b) -> a -> b
$ forall (columns :: [*]) (f :: * -> *).
GReadable columns f =>
[(Builder, Builder)]
gReadingColumns @(GetColumns hasColumns) @(Rep record)

  default readingColumnsAndTypes :: GenericReadable record hasColumns => Builder
  readingColumnsAndTypes :: Builder
  readingColumnsAndTypes
    = Builder
-> ((Builder, [Builder]) -> Builder)
-> Maybe (Builder, [Builder])
-> Builder
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Builder
"" ((Builder -> [Builder] -> Builder)
-> (Builder, [Builder]) -> Builder
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry ((Builder -> Builder -> Builder) -> Builder -> [Builder] -> Builder
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl (\Builder
a Builder
b -> Builder
a Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
", " Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
b))) (Maybe (Builder, [Builder]) -> Builder)
-> ([(Builder, Builder)] -> Maybe (Builder, [Builder]))
-> [(Builder, Builder)]
-> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Builder] -> Maybe (Builder, [Builder])
forall a. [a] -> Maybe (a, [a])
uncons
    ([Builder] -> Maybe (Builder, [Builder]))
-> ([(Builder, Builder)] -> [Builder])
-> [(Builder, Builder)]
-> Maybe (Builder, [Builder])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Builder, Builder) -> Builder)
-> [(Builder, Builder)] -> [Builder]
forall a b. (a -> b) -> [a] -> [b]
map (\(Builder
colName, Builder
colType) -> Builder
colName Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
" " Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
colType)
    ([(Builder, Builder)] -> Builder)
-> [(Builder, Builder)] -> Builder
forall a b. (a -> b) -> a -> b
$ forall (columns :: [*]) (f :: * -> *).
GReadable columns f =>
[(Builder, Builder)]
gReadingColumns @(GetColumns hasColumns) @(Rep record)


class GReadable (columns :: [Type]) f
  where
  gFromColumns :: ProtocolRevision -> UVarInt -> Get [f p]
  gReadingColumns :: [(Builder, Builder)]

instance
  GReadable columns f
  =>
  GReadable columns (D1 c (C1 c2 f))
  where
  {-# INLINE gFromColumns #-}
  gFromColumns :: forall p. ProtocolRevision -> UVarInt -> Get [D1 c (C1 c2 f) p]
gFromColumns ProtocolRevision
rev UVarInt
size = (f p -> D1 c (C1 c2 f) p) -> [f p] -> [D1 c (C1 c2 f) p]
forall a b. (a -> b) -> [a] -> [b]
map (C1 c2 f p -> D1 c (C1 c2 f) p
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (C1 c2 f p -> D1 c (C1 c2 f) p)
-> (f p -> C1 c2 f p) -> f p -> D1 c (C1 c2 f) p
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f p -> C1 c2 f p
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1) ([f p] -> [D1 c (C1 c2 f) p])
-> Get [f p] -> Get [D1 c (C1 c2 f) p]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (columns :: [*]) (f :: * -> *) p.
GReadable columns f =>
ProtocolRevision -> UVarInt -> Get [f p]
gFromColumns @columns ProtocolRevision
rev UVarInt
size
  gReadingColumns :: [(Builder, Builder)]
gReadingColumns = forall (columns :: [*]) (f :: * -> *).
GReadable columns f =>
[(Builder, Builder)]
gReadingColumns @columns @f

instance
  GReadable columns (left :*: (right1 :*: right2))
  =>
  GReadable columns ((left :*: right1) :*: right2)
  where
  {-# INLINE gFromColumns #-}
  gFromColumns :: forall p.
ProtocolRevision
-> UVarInt -> Get [(:*:) (left :*: right1) right2 p]
gFromColumns ProtocolRevision
rev UVarInt
size = do
    [(:*:) left (right1 :*: right2) p]
list <- forall (columns :: [*]) (f :: * -> *) p.
GReadable columns f =>
ProtocolRevision -> UVarInt -> Get [f p]
gFromColumns @columns ProtocolRevision
rev UVarInt
size
    [(:*:) (left :*: right1) right2 p]
-> Get [(:*:) (left :*: right1) right2 p]
forall a. a -> Get a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [(left p
l left p -> right1 p -> (:*:) left right1 p
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: right1 p
r1) (:*:) left right1 p -> right2 p -> (:*:) (left :*: right1) right2 p
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: right2 p
r2 | (left p
l :*: (right1 p
r1 :*: right2 p
r2)) <- [(:*:) left (right1 :*: right2) p]
list]
  gReadingColumns :: [(Builder, Builder)]
gReadingColumns = forall (columns :: [*]) (f :: * -> *).
GReadable columns f =>
[(Builder, Builder)]
gReadingColumns @columns @(left :*: (right1 :*: right2))


instance
  ( KnownColumn (Column name chType)
  , GReadable '[Column name chType] (S1 (MetaSel (Just name) a b f) rec)
  , GReadable restColumns right
  , '(Column name chType, restColumns) ~ TakeColumn name columns
  )
  =>
  GReadable columns (S1 (MetaSel (Just name) a b f) rec :*: right)
  where
  {-# INLINE gFromColumns #-}
  gFromColumns :: forall p.
ProtocolRevision
-> UVarInt
-> Get [(:*:) (S1 ('MetaSel ('Just name) a b f) rec) right p]
gFromColumns ProtocolRevision
rev UVarInt
size = do
    (S1 ('MetaSel ('Just name) a b f) rec p
 -> right p -> (:*:) (S1 ('MetaSel ('Just name) a b f) rec) right p)
-> [S1 ('MetaSel ('Just name) a b f) rec p]
-> [right p]
-> [(:*:) (S1 ('MetaSel ('Just name) a b f) rec) right p]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith S1 ('MetaSel ('Just name) a b f) rec p
-> right p -> (:*:) (S1 ('MetaSel ('Just name) a b f) rec) right p
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
(:*:)
      ([S1 ('MetaSel ('Just name) a b f) rec p]
 -> [right p]
 -> [(:*:) (S1 ('MetaSel ('Just name) a b f) rec) right p])
-> Get [S1 ('MetaSel ('Just name) a b f) rec p]
-> Get
     ([right p]
      -> [(:*:) (S1 ('MetaSel ('Just name) a b f) rec) right p])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (columns :: [*]) (f :: * -> *) p.
GReadable columns f =>
ProtocolRevision -> UVarInt -> Get [f p]
gFromColumns @'[Column name chType] ProtocolRevision
rev UVarInt
size
      Get
  ([right p]
   -> [(:*:) (S1 ('MetaSel ('Just name) a b f) rec) right p])
-> Get [right p]
-> Get [(:*:) (S1 ('MetaSel ('Just name) a b f) rec) right p]
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (columns :: [*]) (f :: * -> *) p.
GReadable columns f =>
ProtocolRevision -> UVarInt -> Get [f p]
gFromColumns @restColumns ProtocolRevision
rev UVarInt
size
  gReadingColumns :: [(Builder, Builder)]
gReadingColumns =
    (forall column. KnownColumn column => Builder
renderColumnName @(Column name chType), forall column. KnownColumn column => Builder
renderColumnType @(Column name chType))
    (Builder, Builder) -> [(Builder, Builder)] -> [(Builder, Builder)]
forall a. a -> [a] -> [a]
: forall (columns :: [*]) (f :: * -> *).
GReadable columns f =>
[(Builder, Builder)]
gReadingColumns @restColumns @right

instance
  ( KnownColumn (Column name chType)
  , DeserializableColumn (Column name chType)
  , FromChType chType inputType
  , '(Column name chType, restColumns) ~ TakeColumn name columns
  ) => GReadable columns ((S1 (MetaSel (Just name) a b f)) (Rec0 inputType))
  where
  {-# INLINE gFromColumns #-}
  gFromColumns :: forall p.
ProtocolRevision
-> UVarInt
-> Get [S1 ('MetaSel ('Just name) a b f) (Rec0 inputType) p]
gFromColumns ProtocolRevision
rev UVarInt
size =
    (chType -> S1 ('MetaSel ('Just name) a b f) (Rec0 inputType) p)
-> [chType]
-> [S1 ('MetaSel ('Just name) a b f) (Rec0 inputType) p]
forall a b. (a -> b) -> [a] -> [b]
map (Rec0 inputType p
-> S1 ('MetaSel ('Just name) a b f) (Rec0 inputType) p
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (Rec0 inputType p
 -> S1 ('MetaSel ('Just name) a b f) (Rec0 inputType) p)
-> (chType -> Rec0 inputType p)
-> chType
-> S1 ('MetaSel ('Just name) a b f) (Rec0 inputType) p
forall b c a. (b -> c) -> (a -> b) -> a -> c
. inputType -> Rec0 inputType p
forall k i c (p :: k). c -> K1 i c p
K1 (inputType -> Rec0 inputType p)
-> (chType -> inputType) -> chType -> Rec0 inputType p
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall chType outputType.
FromChType chType outputType =>
chType -> outputType
fromChType @chType) ([chType] -> [S1 ('MetaSel ('Just name) a b f) (Rec0 inputType) p])
-> (Column name chType -> [chType])
-> Column name chType
-> [S1 ('MetaSel ('Just name) a b f) (Rec0 inputType) p]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Column name chType -> [chType]
forall (name :: Symbol) chType. Column name chType -> [chType]
columnValues
      (Column name chType
 -> [S1 ('MetaSel ('Just name) a b f) (Rec0 inputType) p])
-> Get (Column name chType)
-> Get [S1 ('MetaSel ('Just name) a b f) (Rec0 inputType) p]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall column.
DeserializableColumn column =>
ProtocolRevision -> Bool -> UVarInt -> Get column
deserializeColumn @(Column name chType) ProtocolRevision
rev Bool
True UVarInt
size
  gReadingColumns :: [(Builder, Builder)]
gReadingColumns = (forall column. KnownColumn column => Builder
renderColumnName @(Column name chType), forall column. KnownColumn column => Builder
renderColumnType @(Column name chType)) (Builder, Builder) -> [(Builder, Builder)] -> [(Builder, Builder)]
forall a. a -> [a] -> [a]
: []




-- ** Column deserialization

{-# SPECIALIZE replicateM :: Int -> Get chType -> Get [chType] #-}

class DeserializableColumn column where
  deserializeColumn :: ProtocolRevision -> Bool -> UVarInt -> Get column

handleColumnHeader :: forall column . KnownColumn column => ProtocolRevision -> Bool -> Get ()
handleColumnHeader :: forall column.
KnownColumn column =>
ProtocolRevision -> Bool -> Get ()
handleColumnHeader ProtocolRevision
rev Bool
isCheckRequired = do
  let expectedColumnName :: ChString
expectedColumnName = Builder -> ChString
forall chType inputType.
ToChType chType inputType =>
inputType -> chType
toChType (forall column. KnownColumn column => Builder
renderColumnName @column)
  ChString
resultColumnName <- forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize @ChString ProtocolRevision
rev 
  Bool -> Get () -> Get ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool
isCheckRequired Bool -> Bool -> Bool
&& ChString
resultColumnName ChString -> ChString -> Bool
forall a. Eq a => a -> a -> Bool
/= ChString
expectedColumnName)
    (Get () -> Get ()) -> (String -> Get ()) -> String -> Get ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ClientError -> Get ()
forall a e. Exception e => e -> a
throw (ClientError -> Get ())
-> (String -> ClientError) -> String -> Get ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HasCallStack => UserError -> ClientError
UserError -> ClientError
UserError (UserError -> ClientError)
-> (String -> UserError) -> String -> ClientError
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> UserError
UnmatchedColumn
      (String -> Get ()) -> String -> Get ()
forall a b. (a -> b) -> a -> b
$ String
"Got column \"" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> ChString -> String
forall a. Show a => a -> String
show ChString
resultColumnName String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"\" but expected \"" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> ChString -> String
forall a. Show a => a -> String
show ChString
expectedColumnName String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"\""

  let expectedType :: ChString
expectedType = Builder -> ChString
forall chType inputType.
ToChType chType inputType =>
inputType -> chType
toChType (forall column. KnownColumn column => Builder
renderColumnType @column)
  ChString
resultType <- forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize @ChString ProtocolRevision
rev
  Bool -> Get () -> Get ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool
isCheckRequired Bool -> Bool -> Bool
&& ChString
resultType ChString -> ChString -> Bool
forall a. Eq a => a -> a -> Bool
/= ChString
expectedType)
    (Get () -> Get ()) -> (String -> Get ()) -> String -> Get ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ClientError -> Get ()
forall a e. Exception e => e -> a
throw (ClientError -> Get ())
-> (String -> ClientError) -> String -> Get ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HasCallStack => UserError -> ClientError
UserError -> ClientError
UserError (UserError -> ClientError)
-> (String -> UserError) -> String -> ClientError
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> UserError
UnmatchedType
      (String -> Get ()) -> String -> Get ()
forall a b. (a -> b) -> a -> b
$  String
"Column " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> ChString -> String
forall a. Show a => a -> String
show ChString
resultColumnName String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
" has type " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> ChString -> String
forall a. Show a => a -> String
show ChString
resultType String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
". But expected type is " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> ChString -> String
forall a. Show a => a -> String
show ChString
expectedType

instance
  ( KnownColumn (Column name chType)
  , Deserializable chType
  ) =>
  DeserializableColumn (Column name chType) where
  {-# INLINE deserializeColumn #-}
  deserializeColumn :: ProtocolRevision -> Bool -> UVarInt -> Get (Column name chType)
deserializeColumn ProtocolRevision
rev Bool
isCheckRequired UVarInt
rows = do
    forall column.
KnownColumn column =>
ProtocolRevision -> Bool -> Get ()
handleColumnHeader @(Column name chType) ProtocolRevision
rev Bool
isCheckRequired
    SinceRevision UInt8 DBMS_MIN_REVISION_WITH_CUSTOM_SERIALIZATION
_isCustom <- forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize @(UInt8 `SinceRevision` DBMS_MIN_REVISION_WITH_CUSTOM_SERIALIZATION) ProtocolRevision
rev
    [chType]
column <- Int -> Get chType -> Get [chType]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM (UVarInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral UVarInt
rows) (forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize @chType ProtocolRevision
rev)
    Column
  (GetColumnName (Column name chType))
  (GetColumnType (Column name chType))
-> Get
     (Column
        (GetColumnName (Column name chType))
        (GetColumnType (Column name chType)))
forall a. a -> Get a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Column
   (GetColumnName (Column name chType))
   (GetColumnType (Column name chType))
 -> Get
      (Column
         (GetColumnName (Column name chType))
         (GetColumnType (Column name chType))))
-> Column
     (GetColumnName (Column name chType))
     (GetColumnType (Column name chType))
-> Get
     (Column
        (GetColumnName (Column name chType))
        (GetColumnType (Column name chType)))
forall a b. (a -> b) -> a -> b
$ forall column.
KnownColumn column =>
[GetColumnType column]
-> Column (GetColumnName column) (GetColumnType column)
mkColumn @(Column name chType) [chType]
[GetColumnType (Column name chType)]
column

instance {-# OVERLAPPING #-}
  ( KnownColumn (Column name (Nullable chType))
  , Deserializable chType
  ) =>
  DeserializableColumn (Column name (Nullable chType)) where
  {-# INLINE deserializeColumn #-}
  deserializeColumn :: ProtocolRevision
-> Bool -> UVarInt -> Get (Column name (Nullable chType))
deserializeColumn ProtocolRevision
rev Bool
isCheckRequired UVarInt
rows = do
    forall column.
KnownColumn column =>
ProtocolRevision -> Bool -> Get ()
handleColumnHeader @(Column name (Nullable chType)) ProtocolRevision
rev Bool
isCheckRequired
    SinceRevision UInt8 DBMS_MIN_REVISION_WITH_CUSTOM_SERIALIZATION
_isCustom <- forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize @(UInt8 `SinceRevision` DBMS_MIN_REVISION_WITH_CUSTOM_SERIALIZATION) ProtocolRevision
rev
    [UInt8]
nulls <- Int -> Get UInt8 -> Get [UInt8]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM (UVarInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral UVarInt
rows) (forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize @UInt8 ProtocolRevision
rev)
    [Nullable chType]
nullable <-
      [UInt8]
-> (UInt8 -> Get (Nullable chType)) -> Get [Nullable chType]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM
        [UInt8]
nulls
        (\case
          UInt8
0 -> chType -> Nullable chType
forall a. a -> Maybe a
Just (chType -> Nullable chType) -> Get chType -> Get (Nullable chType)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize @chType ProtocolRevision
rev
          UInt8
_ -> (Nullable chType
forall a. Maybe a
Nothing Nullable chType -> Get chType -> Get (Nullable chType)
forall a b. a -> Get b -> Get a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize @chType ProtocolRevision
rev)
        )
    Column
  (GetColumnName (Column name (Nullable chType)))
  (GetColumnType (Column name (Nullable chType)))
-> Get
     (Column
        (GetColumnName (Column name (Nullable chType)))
        (GetColumnType (Column name (Nullable chType))))
forall a. a -> Get a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Column
   (GetColumnName (Column name (Nullable chType)))
   (GetColumnType (Column name (Nullable chType)))
 -> Get
      (Column
         (GetColumnName (Column name (Nullable chType)))
         (GetColumnType (Column name (Nullable chType)))))
-> Column
     (GetColumnName (Column name (Nullable chType)))
     (GetColumnType (Column name (Nullable chType)))
-> Get
     (Column
        (GetColumnName (Column name (Nullable chType)))
        (GetColumnType (Column name (Nullable chType))))
forall a b. (a -> b) -> a -> b
$ forall column.
KnownColumn column =>
[GetColumnType column]
-> Column (GetColumnName column) (GetColumnType column)
mkColumn @(Column name (Nullable chType)) [Nullable chType]
[GetColumnType (Column name (Nullable chType))]
nullable

instance {-# OVERLAPPING #-}
  ( KnownColumn (Column name (LowCardinality chType))
  , Deserializable chType
  , ToChType (LowCardinality chType) chType
  , IsLowCardinalitySupported chType
  , TypeError ('Text "LowCardinality deserialization still unsupported")
  ) =>
  DeserializableColumn (Column name (LowCardinality chType)) where
  {-# INLINE deserializeColumn #-}
  deserializeColumn :: ProtocolRevision
-> Bool -> UVarInt -> Get (Column name (LowCardinality chType))
deserializeColumn ProtocolRevision
rev Bool
isCheckRequired UVarInt
rows = do
    forall column.
KnownColumn column =>
ProtocolRevision -> Bool -> Get ()
handleColumnHeader @(Column name (LowCardinality chType)) ProtocolRevision
rev Bool
isCheckRequired
    SinceRevision UInt8 DBMS_MIN_REVISION_WITH_CUSTOM_SERIALIZATION
_isCustom <- forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize @(UInt8 `SinceRevision` DBMS_MIN_REVISION_WITH_CUSTOM_SERIALIZATION) ProtocolRevision
rev
    UInt64
_serializationType <- (UInt64 -> UInt64 -> UInt64
forall a. Bits a => a -> a -> a
.&. UInt64
0xf) (UInt64 -> UInt64) -> Get UInt64 -> Get UInt64
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize @UInt64 ProtocolRevision
rev
    Int64
_index_size <- forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize @Int64 ProtocolRevision
rev
    -- error $ "Trace | " <> show _serializationType <> " : " <> show _index_size
    [LowCardinality chType]
lc <- Int -> Get (LowCardinality chType) -> Get [LowCardinality chType]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM (UVarInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral UVarInt
rows) (chType -> LowCardinality chType
forall chType inputType.
ToChType chType inputType =>
inputType -> chType
toChType (chType -> LowCardinality chType)
-> Get chType -> Get (LowCardinality chType)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize @chType ProtocolRevision
rev)
    Column
  (GetColumnName (Column name (LowCardinality chType)))
  (GetColumnType (Column name (LowCardinality chType)))
-> Get
     (Column
        (GetColumnName (Column name (LowCardinality chType)))
        (GetColumnType (Column name (LowCardinality chType))))
forall a. a -> Get a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Column
   (GetColumnName (Column name (LowCardinality chType)))
   (GetColumnType (Column name (LowCardinality chType)))
 -> Get
      (Column
         (GetColumnName (Column name (LowCardinality chType)))
         (GetColumnType (Column name (LowCardinality chType)))))
-> Column
     (GetColumnName (Column name (LowCardinality chType)))
     (GetColumnType (Column name (LowCardinality chType)))
-> Get
     (Column
        (GetColumnName (Column name (LowCardinality chType)))
        (GetColumnType (Column name (LowCardinality chType))))
forall a b. (a -> b) -> a -> b
$ forall column.
KnownColumn column =>
[GetColumnType column]
-> Column (GetColumnName column) (GetColumnType column)
mkColumn @(Column name (LowCardinality chType)) [LowCardinality chType]
[GetColumnType (Column name (LowCardinality chType))]
lc

instance {-# OVERLAPPING #-}
  ( KnownColumn (Column name (ChArray chType))
  , Deserializable chType
  , TypeError ('Text "Arrays deserialization still unsupported")
  )
  => DeserializableColumn (Column name (ChArray chType)) where
  {-# INLINE deserializeColumn #-}
  deserializeColumn :: ProtocolRevision
-> Bool -> UVarInt -> Get (Column name (ChArray chType))
deserializeColumn ProtocolRevision
rev Bool
isCheckRequired UVarInt
_rows = do
    forall column.
KnownColumn column =>
ProtocolRevision -> Bool -> Get ()
handleColumnHeader @(Column name (ChArray chType)) ProtocolRevision
rev Bool
isCheckRequired
    SinceRevision UInt8 DBMS_MIN_REVISION_WITH_CUSTOM_SERIALIZATION
_isCustom <- forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize @(UInt8 `SinceRevision` DBMS_MIN_REVISION_WITH_CUSTOM_SERIALIZATION) ProtocolRevision
rev
    (UInt64
arraySize, [UInt64]
_offsets) <- ProtocolRevision -> Get (UInt64, [UInt64])
readOffsets ProtocolRevision
rev
    [chType]
_types <- Int -> Get chType -> Get [chType]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM (UInt64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral UInt64
arraySize) (forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize @chType ProtocolRevision
rev)
    Column
  (GetColumnName (Column name (ChArray chType)))
  (GetColumnType (Column name (ChArray chType)))
-> Get
     (Column
        (GetColumnName (Column name (ChArray chType)))
        (GetColumnType (Column name (ChArray chType))))
forall a. a -> Get a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Column
   (GetColumnName (Column name (ChArray chType)))
   (GetColumnType (Column name (ChArray chType)))
 -> Get
      (Column
         (GetColumnName (Column name (ChArray chType)))
         (GetColumnType (Column name (ChArray chType)))))
-> Column
     (GetColumnName (Column name (ChArray chType)))
     (GetColumnType (Column name (ChArray chType)))
-> Get
     (Column
        (GetColumnName (Column name (ChArray chType)))
        (GetColumnType (Column name (ChArray chType))))
forall a b. (a -> b) -> a -> b
$ forall column.
KnownColumn column =>
[GetColumnType column]
-> Column (GetColumnName column) (GetColumnType column)
mkColumn @(Column name (ChArray chType)) []
    where
    readOffsets :: ProtocolRevision -> Get (UInt64, [UInt64])
    readOffsets :: ProtocolRevision -> Get (UInt64, [UInt64])
readOffsets ProtocolRevision
revivion = do
      UInt64
size <- forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize @UInt64 ProtocolRevision
rev
      (UInt64
size, ) ([UInt64] -> (UInt64, [UInt64]))
-> Get [UInt64] -> Get (UInt64, [UInt64])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> UInt64 -> Get [UInt64]
go UInt64
size
      where
      go :: UInt64 -> Get [UInt64]
go UInt64
arraySize =
        do
        UInt64
nextOffset <- forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize @UInt64 ProtocolRevision
revivion
        if UInt64
arraySize UInt64 -> UInt64 -> Bool
forall a. Ord a => a -> a -> Bool
>= UInt64
nextOffset
          then [UInt64] -> Get [UInt64]
forall a. a -> Get a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [UInt64
nextOffset]
          else (UInt64
nextOffset UInt64 -> [UInt64] -> [UInt64]
forall a. a -> [a] -> [a]
:) ([UInt64] -> [UInt64]) -> Get [UInt64] -> Get [UInt64]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> UInt64 -> Get [UInt64]
go UInt64
arraySize


class
  Deserializable chType
  where
  {-# INLINE deserialize #-}
  default deserialize :: (Generic chType, GDeserializable (Rep chType)) => ProtocolRevision -> Get chType
  deserialize :: ProtocolRevision -> Get chType
  deserialize ProtocolRevision
rev = Rep chType Any -> chType
forall a x. Generic a => Rep a x -> a
forall x. Rep chType x -> chType
to (Rep chType Any -> chType) -> Get (Rep chType Any) -> Get chType
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ProtocolRevision -> Get (Rep chType Any)
forall p. ProtocolRevision -> Get (Rep chType p)
forall (f :: * -> *) p.
GDeserializable f =>
ProtocolRevision -> Get (f p)
gDeserialize ProtocolRevision
rev


-- ** Generics

class GDeserializable f
  where
  gDeserialize :: ProtocolRevision -> Get (f p)

instance
  GDeserializable f
  =>
  GDeserializable (D1 c (C1 c2 f))
  where
  {-# INLINE gDeserialize #-}
  gDeserialize :: forall p. ProtocolRevision -> Get (D1 c (C1 c2 f) p)
gDeserialize ProtocolRevision
rev = C1 c2 f p -> M1 D c (C1 c2 f) p
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (C1 c2 f p -> M1 D c (C1 c2 f) p)
-> (f p -> C1 c2 f p) -> f p -> M1 D c (C1 c2 f) p
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f p -> C1 c2 f p
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (f p -> M1 D c (C1 c2 f) p)
-> Get (f p) -> Get (M1 D c (C1 c2 f) p)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ProtocolRevision -> Get (f p)
forall p. ProtocolRevision -> Get (f p)
forall (f :: * -> *) p.
GDeserializable f =>
ProtocolRevision -> Get (f p)
gDeserialize ProtocolRevision
rev

instance
  GDeserializable (left :*: (right1 :*: right2))
  =>
  GDeserializable ((left :*: right1) :*: right2)
  where
  {-# INLINE gDeserialize #-}
  gDeserialize :: forall p.
ProtocolRevision -> Get ((:*:) (left :*: right1) right2 p)
gDeserialize ProtocolRevision
rev = (\(left p
l :*: (right1 p
r1 :*: right2 p
r2)) -> (left p
l left p -> right1 p -> (:*:) left right1 p
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: right1 p
r1) (:*:) left right1 p -> right2 p -> (:*:) (left :*: right1) right2 p
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: right2 p
r2) ((:*:) left (right1 :*: right2) p
 -> (:*:) (left :*: right1) right2 p)
-> Get ((:*:) left (right1 :*: right2) p)
-> Get ((:*:) (left :*: right1) right2 p)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ProtocolRevision -> Get ((:*:) left (right1 :*: right2) p)
forall p.
ProtocolRevision -> Get ((:*:) left (right1 :*: right2) p)
forall (f :: * -> *) p.
GDeserializable f =>
ProtocolRevision -> Get (f p)
gDeserialize ProtocolRevision
rev

instance
  (GDeserializable (S1 metaSel field), GDeserializable right)
  =>
  GDeserializable (S1 metaSel field :*: right)
  where
  {-# INLINE gDeserialize #-}
  gDeserialize :: forall p.
ProtocolRevision -> Get ((:*:) (S1 metaSel field) right p)
gDeserialize ProtocolRevision
rev = S1 metaSel field p -> right p -> (:*:) (S1 metaSel field) right p
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
(:*:) (S1 metaSel field p -> right p -> (:*:) (S1 metaSel field) right p)
-> Get (S1 metaSel field p)
-> Get (right p -> (:*:) (S1 metaSel field) right p)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ProtocolRevision -> Get (S1 metaSel field p)
forall p. ProtocolRevision -> Get (S1 metaSel field p)
forall (f :: * -> *) p.
GDeserializable f =>
ProtocolRevision -> Get (f p)
gDeserialize ProtocolRevision
rev Get (right p -> (:*:) (S1 metaSel field) right p)
-> Get (right p) -> Get ((:*:) (S1 metaSel field) right p)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ProtocolRevision -> Get (right p)
forall p. ProtocolRevision -> Get (right p)
forall (f :: * -> *) p.
GDeserializable f =>
ProtocolRevision -> Get (f p)
gDeserialize ProtocolRevision
rev

instance
  Deserializable chType
  =>
  GDeserializable (S1 (MetaSel (Just typeName) a b f) (Rec0 chType))
  where
  {-# INLINE gDeserialize #-}
  gDeserialize :: forall p.
ProtocolRevision
-> Get (S1 ('MetaSel ('Just typeName) a b f) (Rec0 chType) p)
gDeserialize ProtocolRevision
rev =  Rec0 chType p
-> M1 S ('MetaSel ('Just typeName) a b f) (Rec0 chType) p
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (Rec0 chType p
 -> M1 S ('MetaSel ('Just typeName) a b f) (Rec0 chType) p)
-> (chType -> Rec0 chType p)
-> chType
-> M1 S ('MetaSel ('Just typeName) a b f) (Rec0 chType) p
forall b c a. (b -> c) -> (a -> b) -> a -> c
. chType -> Rec0 chType p
forall k i c (p :: k). c -> K1 i c p
K1 (chType -> M1 S ('MetaSel ('Just typeName) a b f) (Rec0 chType) p)
-> Get chType
-> Get (M1 S ('MetaSel ('Just typeName) a b f) (Rec0 chType) p)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize @chType ProtocolRevision
rev


-- ** Database types

instance Deserializable Int8 where deserialize :: ProtocolRevision -> Get Int8
deserialize ProtocolRevision
_ = Int8 -> Int8
forall chType inputType.
ToChType chType inputType =>
inputType -> chType
toChType (Int8 -> Int8) -> Get Int8 -> Get Int8
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Int8
getInt8; {-# INLINE deserialize #-}
instance Deserializable Int16 where deserialize :: ProtocolRevision -> Get Int16
deserialize ProtocolRevision
_ = Int16 -> Int16
forall chType inputType.
ToChType chType inputType =>
inputType -> chType
toChType (Int16 -> Int16) -> Get Int16 -> Get Int16
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Int16
getInt16le; {-# INLINE deserialize #-}
instance Deserializable Int32 where deserialize :: ProtocolRevision -> Get Int32
deserialize ProtocolRevision
_ = Int32 -> Int32
forall chType inputType.
ToChType chType inputType =>
inputType -> chType
toChType (Int32 -> Int32) -> Get Int32 -> Get Int32
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Int32
getInt32le; {-# INLINE deserialize #-}
instance Deserializable Int64 where deserialize :: ProtocolRevision -> Get Int64
deserialize ProtocolRevision
_ = Int64 -> Int64
forall chType inputType.
ToChType chType inputType =>
inputType -> chType
toChType (Int64 -> Int64) -> Get Int64 -> Get Int64
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Int64
getInt64le; {-# INLINE deserialize #-}
instance Deserializable Int128 where deserialize :: ProtocolRevision -> Get Int128
deserialize ProtocolRevision
_ = Int128 -> Int128
forall chType inputType.
ToChType chType inputType =>
inputType -> chType
toChType (Int128 -> Int128) -> Get Int128 -> Get Int128
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((UInt64 -> UInt64 -> Int128) -> UInt64 -> UInt64 -> Int128
forall a b c. (a -> b -> c) -> b -> a -> c
flip UInt64 -> UInt64 -> Int128
Int128 (UInt64 -> UInt64 -> Int128)
-> Get UInt64 -> Get (UInt64 -> Int128)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get UInt64
getWord64le Get (UInt64 -> Int128) -> Get UInt64 -> Get Int128
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get UInt64
getWord64le); {-# INLINE deserialize #-}
instance Deserializable UInt8 where deserialize :: ProtocolRevision -> Get UInt8
deserialize ProtocolRevision
_ = UInt8 -> UInt8
forall chType inputType.
ToChType chType inputType =>
inputType -> chType
toChType (UInt8 -> UInt8) -> Get UInt8 -> Get UInt8
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get UInt8
getWord8; {-# INLINE deserialize #-}
instance Deserializable UInt16 where deserialize :: ProtocolRevision -> Get UInt16
deserialize ProtocolRevision
_ = UInt16 -> UInt16
forall chType inputType.
ToChType chType inputType =>
inputType -> chType
toChType (UInt16 -> UInt16) -> Get UInt16 -> Get UInt16
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get UInt16
getWord16le; {-# INLINE deserialize #-}
instance Deserializable UInt32 where deserialize :: ProtocolRevision -> Get UInt32
deserialize ProtocolRevision
_ = UInt32 -> UInt32
forall chType inputType.
ToChType chType inputType =>
inputType -> chType
toChType (UInt32 -> UInt32) -> Get UInt32 -> Get UInt32
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get UInt32
getWord32le; {-# INLINE deserialize #-}
instance Deserializable UInt64 where deserialize :: ProtocolRevision -> Get UInt64
deserialize ProtocolRevision
_ = UInt64 -> UInt64
forall chType inputType.
ToChType chType inputType =>
inputType -> chType
toChType (UInt64 -> UInt64) -> Get UInt64 -> Get UInt64
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get UInt64
getWord64le; {-# INLINE deserialize #-}
instance Deserializable UInt128 where deserialize :: ProtocolRevision -> Get UInt128
deserialize ProtocolRevision
_ = UInt128 -> UInt128
forall chType inputType.
ToChType chType inputType =>
inputType -> chType
toChType (UInt128 -> UInt128) -> Get UInt128 -> Get UInt128
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((UInt64 -> UInt64 -> UInt128) -> UInt64 -> UInt64 -> UInt128
forall a b c. (a -> b -> c) -> b -> a -> c
flip UInt64 -> UInt64 -> UInt128
Word128 (UInt64 -> UInt64 -> UInt128)
-> Get UInt64 -> Get (UInt64 -> UInt128)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get UInt64
getWord64le Get (UInt64 -> UInt128) -> Get UInt64 -> Get UInt128
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get UInt64
getWord64le); {-# INLINE deserialize #-}
instance Deserializable ChUUID where deserialize :: ProtocolRevision -> Get ChUUID
deserialize ProtocolRevision
_ = UInt128 -> ChUUID
MkChUUID (UInt128 -> ChUUID) -> Get UInt128 -> Get ChUUID
forall (m :: * -> *) a b. Monad m => (a -> b) -> m a -> m b
<$!> ((UInt64 -> UInt64 -> UInt128) -> UInt64 -> UInt64 -> UInt128
forall a b c. (a -> b -> c) -> b -> a -> c
flip UInt64 -> UInt64 -> UInt128
Word128 (UInt64 -> UInt64 -> UInt128)
-> Get UInt64 -> Get (UInt64 -> UInt128)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get UInt64
getWord64le Get (UInt64 -> UInt128) -> Get UInt64 -> Get UInt128
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get UInt64
getWord64le); {-# INLINE deserialize #-}
instance Deserializable ChString where deserialize :: ProtocolRevision -> Get ChString
deserialize = (\Int
n -> StrictByteString -> ChString
forall chType inputType.
ToChType chType inputType =>
inputType -> chType
toChType (StrictByteString -> ChString)
-> Get StrictByteString -> Get ChString
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int
-> (StrictByteString -> StrictByteString) -> Get StrictByteString
forall a. Int -> (StrictByteString -> a) -> Get a
readN Int
n (Int -> StrictByteString -> StrictByteString
BS.take Int
n)) (Int -> Get ChString)
-> (UVarInt -> Int) -> UVarInt -> Get ChString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UVarInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (UVarInt -> Get ChString)
-> (ProtocolRevision -> Get UVarInt)
-> ProtocolRevision
-> Get ChString
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize @UVarInt; {-# INLINE deserialize #-}
instance Deserializable Date where deserialize :: ProtocolRevision -> Get Date
deserialize ProtocolRevision
_ = UInt16 -> Date
forall chType inputType.
ToChType chType inputType =>
inputType -> chType
toChType (UInt16 -> Date) -> Get UInt16 -> Get Date
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get UInt16
getWord16le; {-# INLINE deserialize #-}
instance Deserializable (DateTime tz) where deserialize :: ProtocolRevision -> Get (DateTime tz)
deserialize ProtocolRevision
_ = UInt32 -> DateTime tz
forall chType inputType.
ToChType chType inputType =>
inputType -> chType
toChType (UInt32 -> DateTime tz) -> Get UInt32 -> Get (DateTime tz)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get UInt32
getWord32le; {-# INLINE deserialize #-}
instance Deserializable UVarInt where
  {-# INLINE deserialize #-}
  deserialize :: ProtocolRevision -> Get UVarInt
deserialize ProtocolRevision
_ = Int -> UVarInt -> Get UVarInt
forall {a}. (Bits a, Num a) => Int -> a -> Get a
go Int
0 (UVarInt
0 :: UVarInt)
    where
    go :: Int -> a -> Get a
go Int
i a
o | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
10 = do
      UInt8
byte <- Get UInt8
getWord8
      let o' :: a
o' = a
o a -> a -> a
forall a. Bits a => a -> a -> a
.|. ((UInt8 -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral UInt8
byte a -> a -> a
forall a. Bits a => a -> a -> a
.&. a
0x7f) a -> Int -> a
forall a. Bits a => a -> Int -> a
`unsafeShiftL` (Int
7 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
i))
      if UInt8
byte UInt8 -> UInt8 -> UInt8
forall a. Bits a => a -> a -> a
.&. UInt8
0x80 UInt8 -> UInt8 -> Bool
forall a. Eq a => a -> a -> Bool
== UInt8
0 then a -> Get a
forall a. a -> Get a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> Get a) -> a -> Get a
forall a b. (a -> b) -> a -> b
$! a
o' else Int -> a -> Get a
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (a -> Get a) -> a -> Get a
forall a b. (a -> b) -> a -> b
$! a
o'
    go Int
_ a
_ = String -> Get a
forall a. String -> Get a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"input exceeds varuint size"








-- * Columns

-- ** Columns extraction helper

class HasColumns hasColumns where type GetColumns hasColumns :: [Type]
instance HasColumns (Columns columns)          where type GetColumns (Columns columns) = columns
instance HasColumns (Table name columns)       where type GetColumns (Table _ columns) = columns
instance HasColumns (View name columns params) where type GetColumns (View _ columns _) = columns


-- ** Take column by name from list of columns

type family
  TakeColumn (name :: Symbol) (columns :: [Type]) :: (Type, [Type])
  where
  TakeColumn name columns = GoTakeColumn name columns '[]

type family
  GoTakeColumn name (columns :: [Type]) (acc :: [Type]) :: (Type, [Type])
  where
  GoTakeColumn name (Column name chType ': columns) acc = '(Column name chType, acc ++ columns)
  GoTakeColumn name (Column name1 chType ': columns) acc = (GoTakeColumn name columns (Column name1 chType ': acc))
  GoTakeColumn name '[]                 acc = TypeError
    (    'Text "There is no column \"" :<>: 'Text name :<>: 'Text "\" in table"
    :$$: 'Text "You can't use this field"
    )

type family
  (++) (list1 :: [Type]) (list2 :: [Type]) :: [Type]
  where
  (++) '[]            list = list
  (++) (head ': tail) list = tail ++ (head ': list)


data Columns (columns :: [Type]) where
  Empty :: Columns '[]
  AddColumn
    :: KnownColumn (Column name chType)
    => Column name chType
    -> Columns columns
    -> Columns (Column name chType ': columns)

{- |
Column declaration

For example:

@
type MyColumn = Column "myColumn" ChString
@
-}
data Column (name :: Symbol) (chType :: Type) where
  UInt8Column :: [UInt8] -> Column name UInt8
  UInt16Column :: [UInt16] -> Column name UInt16
  UInt32Column :: [UInt32] -> Column name UInt32
  UInt64Column :: [UInt64] -> Column name UInt64
  UInt128Column :: [UInt128] -> Column name UInt128
  Int8Column :: [Int8] -> Column name Int8
  Int16Column :: [Int16] -> Column name Int16
  Int32Column :: [Int32] -> Column name Int32
  Int64Column :: [Int64] -> Column name Int64
  Int128Column :: [Int128] -> Column name Int128
  DateColumn :: [Date] -> Column name Date
  DateTimeColumn :: [DateTime tz] -> Column name (DateTime tz)
  UUIDColumn :: [ChUUID] -> Column name ChUUID
  StringColumn :: [ChString] -> Column name ChString
  ArrayColumn :: [ChArray chType] -> Column name (ChArray chType)
  NullableColumn :: [Nullable chType] -> Column name (Nullable chType)
  LowCardinalityColumn :: IsLowCardinalitySupported chType => [chType] -> Column name (LowCardinality chType)

type family GetColumnName column :: Symbol
  where
  GetColumnName (Column name columnType) = name

type family GetColumnType column :: Type
  where
  GetColumnType (Column name columnType) = columnType

class
  ( IsChType (GetColumnType column)
  , KnownSymbol (GetColumnName column)
  ) =>
  KnownColumn column where
  renderColumnName :: Builder
  renderColumnName = (String -> Builder
stringUtf8 (String -> Builder)
-> (Proxy (GetColumnName column) -> String)
-> Proxy (GetColumnName column)
-> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (n :: Symbol) (proxy :: Symbol -> *).
KnownSymbol n =>
proxy n -> String
symbolVal @(GetColumnName column)) Proxy (GetColumnName column)
forall {k} (t :: k). Proxy t
Proxy

  renderColumnType :: Builder
  renderColumnType = forall chType. IsChType chType => Builder
chTypeName @(GetColumnType column)

  mkColumn :: [GetColumnType column] -> Column (GetColumnName column) (GetColumnType column)

{-# INLINE [0] columnValues #-}
columnValues :: Column name chType -> [chType]
columnValues :: forall (name :: Symbol) chType. Column name chType -> [chType]
columnValues Column name chType
column = case Column name chType
column of
  (UInt8Column [UInt8]
values) -> [chType]
[UInt8]
values
  (UInt16Column [UInt16]
values) -> [chType]
[UInt16]
values
  (UInt32Column [UInt32]
values) -> [chType]
[UInt32]
values
  (UInt64Column [UInt64]
values) -> [chType]
[UInt64]
values
  (UInt128Column [UInt128]
values) -> [chType]
[UInt128]
values
  (Int8Column [Int8]
values) -> [chType]
[Int8]
values
  (Int16Column [Int16]
values) -> [chType]
[Int16]
values
  (Int32Column [Int32]
values) -> [chType]
[Int32]
values
  (Int64Column [Int64]
values) -> [chType]
[Int64]
values
  (Int128Column [Int128]
values) -> [chType]
[Int128]
values
  (DateColumn [Date]
values) -> [chType]
[Date]
values
  (DateTimeColumn [DateTime tz]
values) -> [chType]
[DateTime tz]
values
  (UUIDColumn [ChUUID]
values) -> [chType]
[ChUUID]
values
  (StringColumn [ChString]
values) -> [chType]
[ChString]
values
  (ArrayColumn [ChArray chType]
arrayValues) -> [chType]
[ChArray chType]
arrayValues
  (NullableColumn [Maybe chType]
nullableValues) ->  [chType]
[Maybe chType]
nullableValues
  (LowCardinalityColumn [chType]
lowCardinalityValues) -> (chType -> chType) -> [chType] -> [chType]
forall a b. (a -> b) -> [a] -> [b]
map chType -> chType
forall chType outputType.
FromChType chType outputType =>
chType -> outputType
fromChType [chType]
lowCardinalityValues

instance KnownSymbol name => KnownColumn (Column name UInt8) where mkColumn :: [GetColumnType (Column name UInt8)]
-> Column
     (GetColumnName (Column name UInt8))
     (GetColumnType (Column name UInt8))
mkColumn = [UInt8] -> Column name UInt8
[GetColumnType (Column name UInt8)]
-> Column
     (GetColumnName (Column name UInt8))
     (GetColumnType (Column name UInt8))
forall (name :: Symbol). [UInt8] -> Column name UInt8
UInt8Column
instance KnownSymbol name => KnownColumn (Column name UInt16) where mkColumn :: [GetColumnType (Column name UInt16)]
-> Column
     (GetColumnName (Column name UInt16))
     (GetColumnType (Column name UInt16))
mkColumn = [UInt16] -> Column name UInt16
[GetColumnType (Column name UInt16)]
-> Column
     (GetColumnName (Column name UInt16))
     (GetColumnType (Column name UInt16))
forall (name :: Symbol). [UInt16] -> Column name UInt16
UInt16Column
instance KnownSymbol name => KnownColumn (Column name UInt32) where mkColumn :: [GetColumnType (Column name UInt32)]
-> Column
     (GetColumnName (Column name UInt32))
     (GetColumnType (Column name UInt32))
mkColumn = [UInt32] -> Column name UInt32
[GetColumnType (Column name UInt32)]
-> Column
     (GetColumnName (Column name UInt32))
     (GetColumnType (Column name UInt32))
forall (name :: Symbol). [UInt32] -> Column name UInt32
UInt32Column
instance KnownSymbol name => KnownColumn (Column name UInt64) where mkColumn :: [GetColumnType (Column name UInt64)]
-> Column
     (GetColumnName (Column name UInt64))
     (GetColumnType (Column name UInt64))
mkColumn = [UInt64] -> Column name UInt64
[GetColumnType (Column name UInt64)]
-> Column
     (GetColumnName (Column name UInt64))
     (GetColumnType (Column name UInt64))
forall (name :: Symbol). [UInt64] -> Column name UInt64
UInt64Column
instance KnownSymbol name => KnownColumn (Column name UInt128) where mkColumn :: [GetColumnType (Column name UInt128)]
-> Column
     (GetColumnName (Column name UInt128))
     (GetColumnType (Column name UInt128))
mkColumn = [UInt128] -> Column name UInt128
[GetColumnType (Column name UInt128)]
-> Column
     (GetColumnName (Column name UInt128))
     (GetColumnType (Column name UInt128))
forall (name :: Symbol). [UInt128] -> Column name UInt128
UInt128Column
instance KnownSymbol name => KnownColumn (Column name Int8)  where mkColumn :: [GetColumnType (Column name Int8)]
-> Column
     (GetColumnName (Column name Int8))
     (GetColumnType (Column name Int8))
mkColumn = [Int8] -> Column name Int8
[GetColumnType (Column name Int8)]
-> Column
     (GetColumnName (Column name Int8))
     (GetColumnType (Column name Int8))
forall (name :: Symbol). [Int8] -> Column name Int8
Int8Column
instance KnownSymbol name => KnownColumn (Column name Int16) where mkColumn :: [GetColumnType (Column name Int16)]
-> Column
     (GetColumnName (Column name Int16))
     (GetColumnType (Column name Int16))
mkColumn = [Int16] -> Column name Int16
[GetColumnType (Column name Int16)]
-> Column
     (GetColumnName (Column name Int16))
     (GetColumnType (Column name Int16))
forall (name :: Symbol). [Int16] -> Column name Int16
Int16Column
instance KnownSymbol name => KnownColumn (Column name Int32) where mkColumn :: [GetColumnType (Column name Int32)]
-> Column
     (GetColumnName (Column name Int32))
     (GetColumnType (Column name Int32))
mkColumn = [Int32] -> Column name Int32
[GetColumnType (Column name Int32)]
-> Column
     (GetColumnName (Column name Int32))
     (GetColumnType (Column name Int32))
forall (name :: Symbol). [Int32] -> Column name Int32
Int32Column
instance KnownSymbol name => KnownColumn (Column name Int64) where mkColumn :: [GetColumnType (Column name Int64)]
-> Column
     (GetColumnName (Column name Int64))
     (GetColumnType (Column name Int64))
mkColumn = [Int64] -> Column name Int64
[GetColumnType (Column name Int64)]
-> Column
     (GetColumnName (Column name Int64))
     (GetColumnType (Column name Int64))
forall (name :: Symbol). [Int64] -> Column name Int64
Int64Column
instance KnownSymbol name => KnownColumn (Column name Int128) where mkColumn :: [GetColumnType (Column name Int128)]
-> Column
     (GetColumnName (Column name Int128))
     (GetColumnType (Column name Int128))
mkColumn = [Int128] -> Column name Int128
[GetColumnType (Column name Int128)]
-> Column
     (GetColumnName (Column name Int128))
     (GetColumnType (Column name Int128))
forall (name :: Symbol). [Int128] -> Column name Int128
Int128Column
instance KnownSymbol name => KnownColumn (Column name Date) where mkColumn :: [GetColumnType (Column name Date)]
-> Column
     (GetColumnName (Column name Date))
     (GetColumnType (Column name Date))
mkColumn = [Date] -> Column name Date
[GetColumnType (Column name Date)]
-> Column
     (GetColumnName (Column name Date))
     (GetColumnType (Column name Date))
forall (name :: Symbol). [Date] -> Column name Date
DateColumn
instance
  ( KnownSymbol name
  , IsChType (DateTime tz)
  ) =>
  KnownColumn (Column name (DateTime tz)) where mkColumn :: [GetColumnType (Column name (DateTime tz))]
-> Column
     (GetColumnName (Column name (DateTime tz)))
     (GetColumnType (Column name (DateTime tz)))
mkColumn = [DateTime tz] -> Column name (DateTime tz)
[GetColumnType (Column name (DateTime tz))]
-> Column
     (GetColumnName (Column name (DateTime tz)))
     (GetColumnType (Column name (DateTime tz)))
forall (name :: Symbol) (name :: Symbol).
[DateTime name] -> Column name (DateTime name)
DateTimeColumn
instance KnownSymbol name => KnownColumn (Column name ChUUID) where mkColumn :: [GetColumnType (Column name ChUUID)]
-> Column
     (GetColumnName (Column name ChUUID))
     (GetColumnType (Column name ChUUID))
mkColumn = [ChUUID] -> Column name ChUUID
[GetColumnType (Column name ChUUID)]
-> Column
     (GetColumnName (Column name ChUUID))
     (GetColumnType (Column name ChUUID))
forall (name :: Symbol). [ChUUID] -> Column name ChUUID
UUIDColumn
instance
  ( KnownSymbol name
  , IsChType chType
  , IsChType (Nullable chType)
  ) =>
  KnownColumn (Column name (Nullable chType)) where mkColumn :: [GetColumnType (Column name (Nullable chType))]
-> Column
     (GetColumnName (Column name (Nullable chType)))
     (GetColumnType (Column name (Nullable chType)))
mkColumn = [Nullable chType] -> Column name (Nullable chType)
[GetColumnType (Column name (Nullable chType))]
-> Column
     (GetColumnName (Column name (Nullable chType)))
     (GetColumnType (Column name (Nullable chType)))
forall name (name :: Symbol).
[Nullable name] -> Column name (Nullable name)
NullableColumn
instance KnownSymbol name => KnownColumn (Column name ChString) where mkColumn :: [GetColumnType (Column name ChString)]
-> Column
     (GetColumnName (Column name ChString))
     (GetColumnType (Column name ChString))
mkColumn = [ChString] -> Column name ChString
[GetColumnType (Column name ChString)]
-> Column
     (GetColumnName (Column name ChString))
     (GetColumnType (Column name ChString))
forall (name :: Symbol). [ChString] -> Column name ChString
StringColumn
instance
  ( KnownSymbol name
  , IsChType (LowCardinality chType)
  , IsLowCardinalitySupported chType
  ) =>
  KnownColumn (Column name (LowCardinality chType)) where mkColumn :: [GetColumnType (Column name (LowCardinality chType))]
-> Column
     (GetColumnName (Column name (LowCardinality chType)))
     (GetColumnType (Column name (LowCardinality chType)))
mkColumn = [chType] -> Column name (LowCardinality chType)
forall name (name :: Symbol).
IsLowCardinalitySupported name =>
[name] -> Column name (LowCardinality name)
LowCardinalityColumn ([chType] -> Column name (LowCardinality chType))
-> ([LowCardinality chType] -> [chType])
-> [LowCardinality chType]
-> Column name (LowCardinality chType)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (LowCardinality chType -> chType)
-> [LowCardinality chType] -> [chType]
forall a b. (a -> b) -> [a] -> [b]
map LowCardinality chType -> chType
forall chType outputType.
FromChType chType outputType =>
chType -> outputType
fromChType
instance KnownSymbol name => KnownColumn (Column name (ChArray ChString)) where mkColumn :: [GetColumnType (Column name (ChArray ChString))]
-> Column
     (GetColumnName (Column name (ChArray ChString)))
     (GetColumnType (Column name (ChArray ChString)))
mkColumn = [ChArray ChString] -> Column name (ChArray ChString)
[GetColumnType (Column name (ChArray ChString))]
-> Column
     (GetColumnName (Column name (ChArray ChString)))
     (GetColumnType (Column name (ChArray ChString)))
forall name (name :: Symbol).
[ChArray name] -> Column name (ChArray name)
ArrayColumn


-- ** Columns

instance
  Serializable (Columns '[])
  where
  {-# INLINE serialize #-}
  serialize :: ProtocolRevision -> Columns '[] -> Builder
serialize ProtocolRevision
_rev Columns '[]
Empty = Builder
""

instance (Serializable (Columns columns), Serializable col)
  =>
  Serializable (Columns (col ': columns))
  where
  {-# INLINE serialize #-}
  serialize :: ProtocolRevision -> Columns (col : columns) -> Builder
serialize ProtocolRevision
rev (AddColumn Column name chType
col Columns columns
columns) = ProtocolRevision -> Column name chType -> Builder
forall chType.
Serializable chType =>
ProtocolRevision -> chType -> Builder
serialize ProtocolRevision
rev Column name chType
col Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> ProtocolRevision -> Columns columns -> Builder
forall chType.
Serializable chType =>
ProtocolRevision -> chType -> Builder
serialize ProtocolRevision
rev Columns columns
columns

instance (KnownColumn (Column name chType), Serializable chType)
  =>
  Serializable (Column name chType) where
  {-# INLINE serialize #-}
  serialize :: ProtocolRevision -> Column name chType -> Builder
serialize ProtocolRevision
rev Column name chType
column
    =  ProtocolRevision -> ChString -> Builder
forall chType.
Serializable chType =>
ProtocolRevision -> chType -> Builder
serialize ProtocolRevision
rev (forall chType inputType.
ToChType chType inputType =>
inputType -> chType
toChType @ChString (Builder -> ChString) -> Builder -> ChString
forall a b. (a -> b) -> a -> b
$ forall column. KnownColumn column => Builder
renderColumnName @(Column name chType))
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> ProtocolRevision -> ChString -> Builder
forall chType.
Serializable chType =>
ProtocolRevision -> chType -> Builder
serialize ProtocolRevision
rev (forall chType inputType.
ToChType chType inputType =>
inputType -> chType
toChType @ChString (Builder -> ChString) -> Builder -> ChString
forall a b. (a -> b) -> a -> b
$ forall column. KnownColumn column => Builder
renderColumnType @(Column name chType))
    -- serialization is not custom
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> forall (revision :: Nat) monoid.
(KnownNat revision, Monoid monoid) =>
ProtocolRevision -> monoid -> monoid
afterRevision @DBMS_MIN_REVISION_WITH_CUSTOM_SERIALIZATION ProtocolRevision
rev (forall chType.
Serializable chType =>
ProtocolRevision -> chType -> Builder
serialize @UInt8 ProtocolRevision
rev UInt8
0)
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> [Builder] -> Builder
forall a. Monoid a => [a] -> a
mconcat ((chType -> Builder) -> [chType] -> [Builder]
forall a b. (a -> b) -> [a] -> [b]
Prelude.map (forall chType.
Serializable chType =>
ProtocolRevision -> chType -> Builder
serialize @chType ProtocolRevision
rev) (Column name chType -> [chType]
forall (name :: Symbol) chType. Column name chType -> [chType]
columnValues Column name chType
column))

instance {-# OVERLAPPING #-}
  ( KnownColumn (Column name (Nullable chType))
  , IsChType chType
  , Serializable chType
  ) => Serializable (Column name (Nullable chType)) where
  {-# INLINE serialize #-}
  serialize :: ProtocolRevision -> Column name (Nullable chType) -> Builder
serialize ProtocolRevision
rev Column name (Nullable chType)
column
    =  ProtocolRevision -> ChString -> Builder
forall chType.
Serializable chType =>
ProtocolRevision -> chType -> Builder
serialize ProtocolRevision
rev (forall chType inputType.
ToChType chType inputType =>
inputType -> chType
toChType @ChString (Builder -> ChString) -> Builder -> ChString
forall a b. (a -> b) -> a -> b
$ forall column. KnownColumn column => Builder
renderColumnName @(Column name (Nullable chType)))
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> ProtocolRevision -> ChString -> Builder
forall chType.
Serializable chType =>
ProtocolRevision -> chType -> Builder
serialize ProtocolRevision
rev (forall chType inputType.
ToChType chType inputType =>
inputType -> chType
toChType @ChString (Builder -> ChString) -> Builder -> ChString
forall a b. (a -> b) -> a -> b
$ forall column. KnownColumn column => Builder
renderColumnType @(Column name (Nullable chType)))
    -- serialization is not custom
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> forall (revision :: Nat) monoid.
(KnownNat revision, Monoid monoid) =>
ProtocolRevision -> monoid -> monoid
afterRevision @DBMS_MIN_REVISION_WITH_CUSTOM_SERIALIZATION ProtocolRevision
rev (forall chType.
Serializable chType =>
ProtocolRevision -> chType -> Builder
serialize @UInt8 ProtocolRevision
rev UInt8
0)
    -- Nulls
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> [Builder] -> Builder
forall a. Monoid a => [a] -> a
mconcat ((Nullable chType -> Builder) -> [Nullable chType] -> [Builder]
forall a b. (a -> b) -> [a] -> [b]
Prelude.map (forall chType.
Serializable chType =>
ProtocolRevision -> chType -> Builder
serialize @UInt8 ProtocolRevision
rev (UInt8 -> Builder)
-> (Nullable chType -> UInt8) -> Nullable chType -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UInt8 -> (chType -> UInt8) -> Nullable chType -> UInt8
forall b a. b -> (a -> b) -> Maybe a -> b
maybe UInt8
1 (UInt8 -> chType -> UInt8
forall a b. a -> b -> a
const UInt8
0)) (Column name (Nullable chType) -> [Nullable chType]
forall (name :: Symbol) chType. Column name chType -> [chType]
columnValues Column name (Nullable chType)
column))
    -- Values
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> [Builder] -> Builder
forall a. Monoid a => [a] -> a
mconcat ((Nullable chType -> Builder) -> [Nullable chType] -> [Builder]
forall a b. (a -> b) -> [a] -> [b]
Prelude.map (forall chType.
Serializable chType =>
ProtocolRevision -> chType -> Builder
serialize @chType ProtocolRevision
rev (chType -> Builder)
-> (Nullable chType -> chType) -> Nullable chType -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. chType -> (chType -> chType) -> Nullable chType -> chType
forall b a. b -> (a -> b) -> Maybe a -> b
maybe chType
forall chType. IsChType chType => chType
defaultValueOfTypeName chType -> chType
forall a. a -> a
id) (Column name (Nullable chType) -> [Nullable chType]
forall (name :: Symbol) chType. Column name chType -> [chType]
columnValues Column name (Nullable chType)
column))

instance {-# OVERLAPPING #-}
  ( KnownColumn (Column name (Nullable chType))
  , IsChType chType
  , Serializable chType
  , TypeError ('Text "LowCardinality serialization still unsupported")
  ) => Serializable (Column name (LowCardinality chType)) where
  {-# INLINE serialize #-}
  serialize :: ProtocolRevision -> Column name (LowCardinality chType) -> Builder
serialize ProtocolRevision
rev (LowCardinalityColumn [chType]
column)
    =  ProtocolRevision -> ChString -> Builder
forall chType.
Serializable chType =>
ProtocolRevision -> chType -> Builder
serialize ProtocolRevision
rev (forall chType inputType.
ToChType chType inputType =>
inputType -> chType
toChType @ChString (Builder -> ChString) -> Builder -> ChString
forall a b. (a -> b) -> a -> b
$ forall column. KnownColumn column => Builder
renderColumnName @(Column name (Nullable chType)))
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> ProtocolRevision -> ChString -> Builder
forall chType.
Serializable chType =>
ProtocolRevision -> chType -> Builder
serialize ProtocolRevision
rev (forall chType inputType.
ToChType chType inputType =>
inputType -> chType
toChType @ChString (Builder -> ChString) -> Builder -> ChString
forall a b. (a -> b) -> a -> b
$ forall column. KnownColumn column => Builder
renderColumnType @(Column name (Nullable chType)))
    -- serialization is not custom
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> forall (revision :: Nat) monoid.
(KnownNat revision, Monoid monoid) =>
ProtocolRevision -> monoid -> monoid
afterRevision @DBMS_MIN_REVISION_WITH_CUSTOM_SERIALIZATION ProtocolRevision
rev (forall chType.
Serializable chType =>
ProtocolRevision -> chType -> Builder
serialize @UInt8 ProtocolRevision
rev UInt8
0)
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> [chType] -> Builder
forall a. HasCallStack => a
undefined [chType]
column








-- * Parameters

type family KnownParameter param
  where
  KnownParameter (Parameter name parType) = (KnownSymbol name, IsChType parType, ToQueryPart parType)

data Parameter (name :: Symbol) (chType :: Type) = MkParamater chType

data Parameters parameters where
  NoParameters :: Parameters '[]
  AddParameter
    :: KnownParameter (Parameter name chType)
    => Parameter name chType
    -> Parameters parameters
    -> Parameters (Parameter name chType ': parameters)

{- |
>>> viewParameters (parameter @"a3" @ChString ("a3Val" :: String) . parameter @"a2" @ChString ("a2Val" :: String))
"(a3='a3Val', a2='a2Val')"
-}
viewParameters :: (Parameters '[] -> Parameters passedParameters) -> Builder
viewParameters :: forall (passedParameters :: [*]).
(Parameters '[] -> Parameters passedParameters) -> Builder
viewParameters Parameters '[] -> Parameters passedParameters
interpreter = Builder
"(" Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Parameters passedParameters -> Builder
forall (params :: [*]). Parameters params -> Builder
renderParameters (Parameters '[] -> Parameters passedParameters
interpreter Parameters '[]
NoParameters) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
")"

renderParameters :: Parameters params -> Builder
renderParameters :: forall (params :: [*]). Parameters params -> Builder
renderParameters Parameters params
NoParameters                      = Builder
""
renderParameters (AddParameter Parameter name chType
param Parameters parameters
NoParameters) = Parameter name chType -> Builder
forall (name :: Symbol) chType.
KnownParameter (Parameter name chType) =>
Parameter name chType -> Builder
renderParameter Parameter name chType
param
renderParameters (AddParameter Parameter name chType
param Parameters parameters
moreParams)   = Parameter name chType -> Builder
forall (name :: Symbol) chType.
KnownParameter (Parameter name chType) =>
Parameter name chType -> Builder
renderParameter Parameter name chType
param Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
", " Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Parameters parameters -> Builder
forall (params :: [*]). Parameters params -> Builder
renderParameters Parameters parameters
moreParams


parameter
  :: forall name chType parameters userType
  . (ToChType chType userType, KnownParameter (Parameter name chType))
  => userType -> Parameters parameters -> Parameters (Parameter name chType ': parameters)
parameter :: forall (name :: Symbol) chType (parameters :: [*]) userType.
(ToChType chType userType,
 KnownParameter (Parameter name chType)) =>
userType
-> Parameters parameters
-> Parameters (Parameter name chType : parameters)
parameter userType
val = Parameter name chType
-> Parameters parameters
-> Parameters (Parameter name chType : parameters)
forall (name :: Symbol) chType (parameters :: [*]).
KnownParameter (Parameter name chType) =>
Parameter name chType
-> Parameters parameters
-> Parameters (Parameter name chType : parameters)
AddParameter (chType -> Parameter name chType
forall (name :: Symbol) chType. chType -> Parameter name chType
MkParamater (chType -> Parameter name chType)
-> chType -> Parameter name chType
forall a b. (a -> b) -> a -> b
$ userType -> chType
forall chType inputType.
ToChType chType inputType =>
inputType -> chType
toChType userType
val)

renderParameter :: forall name chType . KnownParameter (Parameter name chType) => Parameter name chType -> Builder
renderParameter :: forall (name :: Symbol) chType.
KnownParameter (Parameter name chType) =>
Parameter name chType -> Builder
renderParameter (MkParamater chType
chType) = (StrictByteString -> Builder
byteString (StrictByteString -> Builder)
-> (Proxy name -> StrictByteString) -> Proxy name -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> StrictByteString
BS8.pack (String -> StrictByteString)
-> (Proxy name -> String) -> Proxy name -> StrictByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (n :: Symbol) (proxy :: Symbol -> *).
KnownSymbol n =>
proxy n -> String
symbolVal @name) Proxy name
forall {k} (t :: k). Proxy t
Proxy Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
"=" Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> chType -> Builder
forall chType. ToQueryPart chType => chType -> Builder
toQueryPart chType
chType

type family CheckParameters (required :: [Type]) (passed :: [Type]) :: Constraint
  where
  CheckParameters required passed = GoCheckParameters required passed '[]

type family GoCheckParameters required passed acc :: Constraint
  where
  GoCheckParameters '[] '[] '[] = ()
  GoCheckParameters (Parameter name _ ': _) '[] '[] = TypeError ('Text "Missing parameter \"" :<>: 'Text name :<>: 'Text "\".")
  GoCheckParameters '[] (p ': _) _ = TypeError ('Text "More parameters passed than used in the view")
  GoCheckParameters '[] '[] (p ': _) = TypeError ('Text "More parameters passed than used in the view")
  GoCheckParameters (Parameter name1 _ ': ps) '[] (Parameter name2 _ ': ps') = TypeError ('Text "Missing  \"" :<>: 'Text name1 :<>: 'Text "\" in passed parameters")
  GoCheckParameters (p ': ps) '[] (p' ': ps') = GoCheckParameters (p ': ps) (p' ': ps') '[]
  GoCheckParameters (Parameter name1 _ ': ps) (Parameter name1 _ ': ps') acc = (GoCheckParameters ps ps' acc)
  GoCheckParameters (Parameter name1 chType1 ': ps) (Parameter name2 chType2 ': ps') acc
    = (GoCheckParameters (Parameter name1 chType1 ': ps) ps' (Parameter name2 chType2 ': acc))
    
class ToQueryPart chType where toQueryPart :: chType -> BS.Builder

instance ToQueryPart chType => ToQueryPart (Nullable chType)
  where
  toQueryPart :: Nullable chType -> Builder
toQueryPart = Builder -> (chType -> Builder) -> Nullable chType -> Builder
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Builder
"null" chType -> Builder
forall chType. ToQueryPart chType => chType -> Builder
toQueryPart

instance ToQueryPart chType => ToQueryPart (LowCardinality chType)
  where
  toQueryPart :: LowCardinality chType -> Builder
toQueryPart (MkLowCardinality chType
chType) = chType -> Builder
forall chType. ToQueryPart chType => chType -> Builder
toQueryPart chType
chType

instance ToQueryPart ChUUID where
  toQueryPart :: ChUUID -> Builder
toQueryPart (MkChUUID (Word128 UInt64
hi UInt64
lo)) = [Builder] -> Builder
forall a. Monoid a => [a] -> a
mconcat
    [Builder
"'", Int -> UInt64 -> Builder
p Int
3 UInt64
hi, Int -> UInt64 -> Builder
p Int
2 UInt64
hi, Builder
"-", Int -> UInt64 -> Builder
p Int
1 UInt64
hi, Builder
"-", Int -> UInt64 -> Builder
p Int
0 UInt64
hi, Builder
"-", Int -> UInt64 -> Builder
p Int
3 UInt64
lo, Builder
"-", Int -> UInt64 -> Builder
p Int
2 UInt64
lo, Int -> UInt64 -> Builder
p Int
1 UInt64
lo, Int -> UInt64 -> Builder
p Int
0 UInt64
lo, Builder
"'"]
    where
    p :: Int -> Word64 -> Builder
    p :: Int -> UInt64 -> Builder
p Int
shiftN UInt64
word = UInt16 -> Builder
word16HexFixed (UInt16 -> Builder) -> UInt16 -> Builder
forall a b. (a -> b) -> a -> b
$ UInt64 -> UInt16
forall a b. (Integral a, Num b) => a -> b
fromIntegral (UInt64
word UInt64 -> Int -> UInt64
forall a. Bits a => a -> Int -> a
`unsafeShiftR` (Int
shiftNInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
16))

instance ToQueryPart ChString where
  toQueryPart :: ChString -> Builder
toQueryPart (MkChString StrictByteString
string) =  Builder
"'" Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> StrictByteString -> Builder
escapeQuery StrictByteString
string Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
"'"
    where
    escapeQuery :: StrictByteString -> Builder
    escapeQuery :: StrictByteString -> Builder
escapeQuery -- [ClickHaskell.DbTypes.ToDo.1]: Optimize
      = StrictByteString -> Builder
BS.byteString (StrictByteString -> Builder)
-> (StrictByteString -> StrictByteString)
-> StrictByteString
-> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> StrictByteString) -> StrictByteString -> StrictByteString
BS8.concatMap (\case Char
'\'' -> StrictByteString
"\\\'"; Char
'\\' -> StrictByteString
"\\\\"; Char
sym -> Char -> StrictByteString
BS8.singleton Char
sym;)

instance ToQueryPart Int8 where toQueryPart :: Int8 -> Builder
toQueryPart = StrictByteString -> Builder
BS.byteString (StrictByteString -> Builder)
-> (Int8 -> StrictByteString) -> Int8 -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> StrictByteString
BS8.pack (String -> StrictByteString)
-> (Int8 -> String) -> Int8 -> StrictByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int8 -> String
forall a. Show a => a -> String
show
instance ToQueryPart Int16 where toQueryPart :: Int16 -> Builder
toQueryPart = StrictByteString -> Builder
BS.byteString (StrictByteString -> Builder)
-> (Int16 -> StrictByteString) -> Int16 -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> StrictByteString
BS8.pack (String -> StrictByteString)
-> (Int16 -> String) -> Int16 -> StrictByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int16 -> String
forall a. Show a => a -> String
show
instance ToQueryPart Int32 where toQueryPart :: Int32 -> Builder
toQueryPart = StrictByteString -> Builder
BS.byteString (StrictByteString -> Builder)
-> (Int32 -> StrictByteString) -> Int32 -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> StrictByteString
BS8.pack (String -> StrictByteString)
-> (Int32 -> String) -> Int32 -> StrictByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int32 -> String
forall a. Show a => a -> String
show
instance ToQueryPart Int64 where toQueryPart :: Int64 -> Builder
toQueryPart = StrictByteString -> Builder
BS.byteString (StrictByteString -> Builder)
-> (Int64 -> StrictByteString) -> Int64 -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> StrictByteString
BS8.pack (String -> StrictByteString)
-> (Int64 -> String) -> Int64 -> StrictByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int64 -> String
forall a. Show a => a -> String
show
instance ToQueryPart Int128 where toQueryPart :: Int128 -> Builder
toQueryPart = StrictByteString -> Builder
BS.byteString (StrictByteString -> Builder)
-> (Int128 -> StrictByteString) -> Int128 -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> StrictByteString
BS8.pack (String -> StrictByteString)
-> (Int128 -> String) -> Int128 -> StrictByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int128 -> String
forall a. Show a => a -> String
show
instance ToQueryPart UInt8 where toQueryPart :: UInt8 -> Builder
toQueryPart = StrictByteString -> Builder
BS.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
instance ToQueryPart UInt16 where toQueryPart :: UInt16 -> Builder
toQueryPart = StrictByteString -> Builder
BS.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
instance ToQueryPart UInt32 where toQueryPart :: UInt32 -> Builder
toQueryPart = StrictByteString -> Builder
BS.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
instance ToQueryPart UInt64 where toQueryPart :: UInt64 -> Builder
toQueryPart = StrictByteString -> Builder
BS.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
instance ToQueryPart UInt128 where toQueryPart :: UInt128 -> Builder
toQueryPart = StrictByteString -> Builder
BS.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
instance ToQueryPart (DateTime tz)
  where
  toQueryPart :: DateTime tz -> Builder
toQueryPart DateTime tz
chDateTime = let time :: StrictByteString
time = String -> StrictByteString
BS8.pack (String -> StrictByteString)
-> (DateTime tz -> String) -> DateTime tz -> StrictByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UInt32 -> String
forall a. Show a => a -> String
show (UInt32 -> String)
-> (DateTime tz -> UInt32) -> DateTime tz -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall chType outputType.
FromChType chType outputType =>
chType -> outputType
fromChType @(DateTime tz) @Word32 (DateTime tz -> StrictByteString)
-> DateTime tz -> StrictByteString
forall a b. (a -> b) -> a -> b
$ DateTime tz
chDateTime
    in StrictByteString -> Builder
BS.byteString (Int -> Char -> StrictByteString
BS8.replicate (Int
10 Int -> Int -> Int
forall a. Num a => a -> a -> a
- StrictByteString -> Int
BS8.length StrictByteString
time) Char
'0' StrictByteString -> StrictByteString -> StrictByteString
forall a. Semigroup a => a -> a -> a
<> StrictByteString
time)
instance (IsChType chType, ToQueryPart chType) => ToQueryPart (ChArray chType)
  where
  toQueryPart :: ChArray chType -> Builder
toQueryPart
    = (\Builder
x -> Builder
"[" Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
x Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
"]")
    (Builder -> Builder)
-> (ChArray chType -> Builder) -> ChArray chType -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Builder
-> ((Builder, [Builder]) -> Builder)
-> Maybe (Builder, [Builder])
-> Builder
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Builder
"" ((Builder -> [Builder] -> Builder)
-> (Builder, [Builder]) -> Builder
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry ((Builder -> Builder -> Builder) -> Builder -> [Builder] -> Builder
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\Builder
a Builder
b -> Builder
a Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
"," Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
b))) (Maybe (Builder, [Builder]) -> Builder)
-> ([chType] -> Maybe (Builder, [Builder])) -> [chType] -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Builder] -> Maybe (Builder, [Builder])
forall a. [a] -> Maybe (a, [a])
uncons
    ([Builder] -> Maybe (Builder, [Builder]))
-> ([chType] -> [Builder])
-> [chType]
-> Maybe (Builder, [Builder])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (chType -> Builder) -> [chType] -> [Builder]
forall a b. (a -> b) -> [a] -> [b]
map (forall chType. ToQueryPart chType => chType -> Builder
toQueryPart @chType)) ([chType] -> Builder)
-> (ChArray chType -> [chType]) -> ChArray chType -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall chType outputType.
FromChType chType outputType =>
chType -> outputType
fromChType @(ChArray chType) @[chType]








-- * Serialization

-- *** Generic API

type GenericWritable record columns =
  ( Generic record
  , GWritable columns (Rep record)
  )

class
  ( HasColumns (Columns (GetColumns columns))
  , Serializable (Columns (GetColumns columns))
  ) =>
  WritableInto columns record
  where
  default deserializeInsertHeader :: GenericWritable record (GetColumns columns) => ProtocolRevision -> Get ()
  deserializeInsertHeader :: ProtocolRevision -> Get ()
  deserializeInsertHeader ProtocolRevision
rev = forall (columns :: [*]) (f :: * -> *).
GWritable columns f =>
ProtocolRevision -> Get ()
gDeserializeInsertHeader @(GetColumns columns) @(Rep record) ProtocolRevision
rev

  default serializeRecords :: GenericWritable record (GetColumns columns) => [record] -> ProtocolRevision -> Builder
  serializeRecords :: [record] -> ProtocolRevision -> Builder
  serializeRecords [record]
records ProtocolRevision
rev = forall (columns :: [*]) (f :: * -> *) p.
GWritable columns f =>
ProtocolRevision -> [f p] -> Builder
gSerializeRecords @(GetColumns columns) ProtocolRevision
rev ((record -> Rep record Any) -> [record] -> [Rep record Any]
forall a b. (a -> b) -> [a] -> [b]
map record -> Rep record Any
forall x. record -> Rep record x
forall a x. Generic a => a -> Rep a x
from [record]
records)

  default writingColumns :: GenericWritable record (GetColumns columns) => Builder
  writingColumns :: Builder
  writingColumns = forall (columns :: [*]) (f :: * -> *).
GWritable columns f =>
Builder
gWritingColumns @(GetColumns columns) @(Rep record)

  default columnsCount :: GenericWritable record (GetColumns columns) => UVarInt
  columnsCount :: UVarInt
  columnsCount = forall (columns :: [*]) (f :: * -> *).
GWritable columns f =>
UVarInt
gColumnsCount @(GetColumns columns) @(Rep record)

class GWritable (columns :: [Type]) f
  where
  gDeserializeInsertHeader :: ProtocolRevision -> Get ()
  gSerializeRecords :: ProtocolRevision -> [f p] -> Builder
  gWritingColumns :: Builder
  gColumnsCount :: UVarInt

instance
  GWritable columns f
  =>
  GWritable columns (D1 c (C1 c2 f))
  where
  {-# INLINE gSerializeRecords #-}
  gSerializeRecords :: forall p. ProtocolRevision -> [D1 c (C1 c2 f) p] -> Builder
gSerializeRecords ProtocolRevision
rev = forall (columns :: [*]) (f :: * -> *) p.
GWritable columns f =>
ProtocolRevision -> [f p] -> Builder
gSerializeRecords @columns ProtocolRevision
rev ([f p] -> Builder)
-> ([D1 c (C1 c2 f) p] -> [f p]) -> [D1 c (C1 c2 f) p] -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (D1 c (C1 c2 f) p -> f p) -> [D1 c (C1 c2 f) p] -> [f p]
forall a b. (a -> b) -> [a] -> [b]
map (M1 C c2 f p -> f p
forall k i (c :: Meta) (f :: k -> *) (p :: k). M1 i c f p -> f p
unM1 (M1 C c2 f p -> f p)
-> (D1 c (C1 c2 f) p -> M1 C c2 f p) -> D1 c (C1 c2 f) p -> f p
forall b c a. (b -> c) -> (a -> b) -> a -> c
. D1 c (C1 c2 f) p -> M1 C c2 f p
forall k i (c :: Meta) (f :: k -> *) (p :: k). M1 i c f p -> f p
unM1)
  gDeserializeInsertHeader :: ProtocolRevision -> Get ()
gDeserializeInsertHeader ProtocolRevision
rev = forall (columns :: [*]) (f :: * -> *).
GWritable columns f =>
ProtocolRevision -> Get ()
gDeserializeInsertHeader @columns @f ProtocolRevision
rev
  gWritingColumns :: Builder
gWritingColumns = forall (columns :: [*]) (f :: * -> *).
GWritable columns f =>
Builder
gWritingColumns @columns @f
  gColumnsCount :: UVarInt
gColumnsCount = forall (columns :: [*]) (f :: * -> *).
GWritable columns f =>
UVarInt
gColumnsCount @columns @f

instance
  GWritable columns (left1 :*: (left2 :*: right))
  =>
  GWritable columns ((left1 :*: left2) :*: right)
  where
  {-# INLINE gSerializeRecords #-}
  gSerializeRecords :: forall p.
ProtocolRevision -> [(:*:) (left1 :*: left2) right p] -> Builder
gSerializeRecords ProtocolRevision
rev = forall (columns :: [*]) (f :: * -> *) p.
GWritable columns f =>
ProtocolRevision -> [f p] -> Builder
gSerializeRecords @columns ProtocolRevision
rev ([(:*:) left1 (left2 :*: right) p] -> Builder)
-> ([(:*:) (left1 :*: left2) right p]
    -> [(:*:) left1 (left2 :*: right) p])
-> [(:*:) (left1 :*: left2) right p]
-> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((:*:) (left1 :*: left2) right p
 -> (:*:) left1 (left2 :*: right) p)
-> [(:*:) (left1 :*: left2) right p]
-> [(:*:) left1 (left2 :*: right) p]
forall a b. (a -> b) -> [a] -> [b]
map (\((left1 p
l1 :*: left2 p
l2) :*: right p
r) -> left1 p
l1 left1 p -> (:*:) left2 right p -> (:*:) left1 (left2 :*: right) p
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: (left2 p
l2 left2 p -> right p -> (:*:) left2 right p
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: right p
r))
  gDeserializeInsertHeader :: ProtocolRevision -> Get ()
gDeserializeInsertHeader ProtocolRevision
rev = Get () -> Get ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Get () -> Get ()) -> Get () -> Get ()
forall a b. (a -> b) -> a -> b
$ forall (columns :: [*]) (f :: * -> *).
GWritable columns f =>
ProtocolRevision -> Get ()
gDeserializeInsertHeader @columns @(left1 :*: (left2 :*: right)) ProtocolRevision
rev
  gWritingColumns :: Builder
gWritingColumns = forall (columns :: [*]) (f :: * -> *).
GWritable columns f =>
Builder
gWritingColumns @columns @(left1 :*: (left2 :*: right))
  gColumnsCount :: UVarInt
gColumnsCount = forall (columns :: [*]) (f :: * -> *).
GWritable columns f =>
UVarInt
gColumnsCount @columns @(left1 :*: (left2 :*: right))

instance
  ( GWritable '[Column name chType] (S1 (MetaSel (Just name) a b f) rec)
  , GWritable restColumns right
  , '(Column name chType, restColumns)~ TakeColumn name columns
  )
  =>
  GWritable columns (S1 (MetaSel (Just name) a b f) rec :*: right)
  where
  {-# INLINE gSerializeRecords #-}
  gSerializeRecords :: forall p.
ProtocolRevision
-> [(:*:) (S1 ('MetaSel ('Just name) a b f) rec) right p]
-> Builder
gSerializeRecords ProtocolRevision
rev
    = (\([M1 S ('MetaSel ('Just name) a b f) rec p]
a, [right p]
b) -> forall (columns :: [*]) (f :: * -> *) p.
GWritable columns f =>
ProtocolRevision -> [f p] -> Builder
gSerializeRecords @'[Column name chType] ProtocolRevision
rev [M1 S ('MetaSel ('Just name) a b f) rec p]
a Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> forall (columns :: [*]) (f :: * -> *) p.
GWritable columns f =>
ProtocolRevision -> [f p] -> Builder
gSerializeRecords @restColumns ProtocolRevision
rev [right p]
b)
    (([M1 S ('MetaSel ('Just name) a b f) rec p], [right p])
 -> Builder)
-> ([(:*:) (S1 ('MetaSel ('Just name) a b f) rec) right p]
    -> ([M1 S ('MetaSel ('Just name) a b f) rec p], [right p]))
-> [(:*:) (S1 ('MetaSel ('Just name) a b f) rec) right p]
-> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(M1 S ('MetaSel ('Just name) a b f) rec p, right p)]
-> ([M1 S ('MetaSel ('Just name) a b f) rec p], [right p])
forall a b. [(a, b)] -> ([a], [b])
unzip ([(M1 S ('MetaSel ('Just name) a b f) rec p, right p)]
 -> ([M1 S ('MetaSel ('Just name) a b f) rec p], [right p]))
-> ([(:*:) (S1 ('MetaSel ('Just name) a b f) rec) right p]
    -> [(M1 S ('MetaSel ('Just name) a b f) rec p, right p)])
-> [(:*:) (S1 ('MetaSel ('Just name) a b f) rec) right p]
-> ([M1 S ('MetaSel ('Just name) a b f) rec p], [right p])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((:*:) (S1 ('MetaSel ('Just name) a b f) rec) right p
 -> (M1 S ('MetaSel ('Just name) a b f) rec p, right p))
-> [(:*:) (S1 ('MetaSel ('Just name) a b f) rec) right p]
-> [(M1 S ('MetaSel ('Just name) a b f) rec p, right p)]
forall a b. (a -> b) -> [a] -> [b]
map (\(M1 S ('MetaSel ('Just name) a b f) rec p
l :*: right p
r) -> (M1 S ('MetaSel ('Just name) a b f) rec p
l, right p
r))
  gDeserializeInsertHeader :: ProtocolRevision -> Get ()
gDeserializeInsertHeader ProtocolRevision
rev = do
    forall (columns :: [*]) (f :: * -> *).
GWritable columns f =>
ProtocolRevision -> Get ()
gDeserializeInsertHeader @'[Column name chType] @(S1 (MetaSel (Just name) a b f) rec) ProtocolRevision
rev
    forall (columns :: [*]) (f :: * -> *).
GWritable columns f =>
ProtocolRevision -> Get ()
gDeserializeInsertHeader @restColumns @right ProtocolRevision
rev
  gWritingColumns :: Builder
gWritingColumns =
    forall (columns :: [*]) (f :: * -> *).
GWritable columns f =>
Builder
gWritingColumns @'[Column name chType] @(S1 (MetaSel (Just name) a b f) rec)
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
", " Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> forall (columns :: [*]) (f :: * -> *).
GWritable columns f =>
Builder
gWritingColumns @restColumns @right
  gColumnsCount :: UVarInt
gColumnsCount = forall (columns :: [*]) (f :: * -> *).
GWritable columns f =>
UVarInt
gColumnsCount @'[Column name chType] @(S1 (MetaSel (Just name) a b f) rec) UVarInt -> UVarInt -> UVarInt
forall a. Num a => a -> a -> a
+ forall (columns :: [*]) (f :: * -> *).
GWritable columns f =>
UVarInt
gColumnsCount @restColumns @right

instance {-# OVERLAPPING #-}
  ( KnownColumn (Column name chType)
  , ToChType chType inputType
  , Serializable (Column name chType)
  , DeserializableColumn (Column name chType)
  , '(Column name chType, restColumns) ~ TakeColumn name columns
  ) =>
  GWritable columns (S1 (MetaSel (Just name) a b f) (Rec0 inputType))
  where
  {-# INLINE gSerializeRecords #-}
  gSerializeRecords :: forall p.
ProtocolRevision
-> [S1 ('MetaSel ('Just name) a b f) (Rec0 inputType) p] -> Builder
gSerializeRecords ProtocolRevision
rev = ProtocolRevision -> Column name chType -> Builder
forall chType.
Serializable chType =>
ProtocolRevision -> chType -> Builder
serialize ProtocolRevision
rev (Column name chType -> Builder)
-> ([S1 ('MetaSel ('Just name) a b f) (Rec0 inputType) p]
    -> Column name chType)
-> [S1 ('MetaSel ('Just name) a b f) (Rec0 inputType) p]
-> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall column.
KnownColumn column =>
[GetColumnType column]
-> Column (GetColumnName column) (GetColumnType column)
mkColumn @(Column name chType) ([chType] -> Column name chType)
-> ([S1 ('MetaSel ('Just name) a b f) (Rec0 inputType) p]
    -> [chType])
-> [S1 ('MetaSel ('Just name) a b f) (Rec0 inputType) p]
-> Column name chType
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (S1 ('MetaSel ('Just name) a b f) (Rec0 inputType) p -> chType)
-> [S1 ('MetaSel ('Just name) a b f) (Rec0 inputType) p]
-> [chType]
forall a b. (a -> b) -> [a] -> [b]
map (inputType -> chType
forall chType inputType.
ToChType chType inputType =>
inputType -> chType
toChType (inputType -> chType)
-> (S1 ('MetaSel ('Just name) a b f) (Rec0 inputType) p
    -> inputType)
-> S1 ('MetaSel ('Just name) a b f) (Rec0 inputType) p
-> chType
forall b c a. (b -> c) -> (a -> b) -> a -> c
. K1 R inputType p -> inputType
forall k i c (p :: k). K1 i c p -> c
unK1 (K1 R inputType p -> inputType)
-> (S1 ('MetaSel ('Just name) a b f) (Rec0 inputType) p
    -> K1 R inputType p)
-> S1 ('MetaSel ('Just name) a b f) (Rec0 inputType) p
-> inputType
forall b c a. (b -> c) -> (a -> b) -> a -> c
. S1 ('MetaSel ('Just name) a b f) (Rec0 inputType) p
-> K1 R inputType p
forall k i (c :: Meta) (f :: k -> *) (p :: k). M1 i c f p -> f p
unM1)
  gDeserializeInsertHeader :: ProtocolRevision -> Get ()
gDeserializeInsertHeader ProtocolRevision
rev = Get (Column name chType) -> Get ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Get (Column name chType) -> Get ())
-> Get (Column name chType) -> Get ()
forall a b. (a -> b) -> a -> b
$ forall column.
DeserializableColumn column =>
ProtocolRevision -> Bool -> UVarInt -> Get column
deserializeColumn @(Column name chType) ProtocolRevision
rev Bool
False UVarInt
0
  gWritingColumns :: Builder
gWritingColumns = forall column. KnownColumn column => Builder
renderColumnName @(Column name chType)
  gColumnsCount :: UVarInt
gColumnsCount = UVarInt
1

class Serializable chType
  where
  default serialize :: (Generic chType, GSerializable (Rep chType)) => ProtocolRevision -> chType -> Builder
  serialize :: ProtocolRevision -> chType -> Builder
  serialize ProtocolRevision
rev = ProtocolRevision -> Rep chType Any -> Builder
forall p. ProtocolRevision -> Rep chType p -> Builder
forall (f :: * -> *) p.
GSerializable f =>
ProtocolRevision -> f p -> Builder
gSerialize ProtocolRevision
rev (Rep chType Any -> Builder)
-> (chType -> Rep chType Any) -> chType -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. chType -> Rep chType Any
forall x. chType -> Rep chType x
forall a x. Generic a => a -> Rep a x
from


-- ** Database types
instance Serializable UVarInt where
  serialize :: ProtocolRevision -> UVarInt -> Builder
serialize ProtocolRevision
_ = UVarInt -> Builder
forall {t}. (Integral t, Bits t) => t -> Builder
go
    where
    go :: t -> Builder
go t
i
      | t
i t -> t -> Bool
forall a. Ord a => a -> a -> Bool
< t
0x80 = UInt8 -> Builder
word8 (t -> UInt8
forall a b. (Integral a, Num b) => a -> b
fromIntegral t
i)
      | Bool
otherwise = UInt8 -> Builder
word8 (UInt8 -> Int -> UInt8
forall a. Bits a => a -> Int -> a
setBit (t -> UInt8
forall a b. (Integral a, Num b) => a -> b
fromIntegral t
i) Int
7) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> t -> Builder
go (t
i t -> Int -> t
forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
7)

instance Serializable ChString where
  serialize :: ProtocolRevision -> ChString -> Builder
serialize ProtocolRevision
rev ChString
str
    =  (forall chType.
Serializable chType =>
ProtocolRevision -> chType -> Builder
serialize @UVarInt ProtocolRevision
rev (UVarInt -> Builder)
-> (ChString -> UVarInt) -> ChString -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> UVarInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> UVarInt) -> (ChString -> Int) -> ChString -> UVarInt
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StrictByteString -> Int
BS.length (StrictByteString -> Int)
-> (ChString -> StrictByteString) -> ChString -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ChString -> StrictByteString
forall chType outputType.
FromChType chType outputType =>
chType -> outputType
fromChType) ChString
str Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> ChString -> Builder
forall chType outputType.
FromChType chType outputType =>
chType -> outputType
fromChType ChString
str

instance Serializable ChUUID where serialize :: ProtocolRevision -> ChUUID -> Builder
serialize ProtocolRevision
_ = (\(UInt64
hi, UInt64
lo) -> UInt64 -> Builder
word64LE UInt64
lo Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> UInt64 -> Builder
word64LE UInt64
hi) ((UInt64, UInt64) -> Builder)
-> (ChUUID -> (UInt64, UInt64)) -> ChUUID -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ChUUID -> (UInt64, UInt64)
forall chType outputType.
FromChType chType outputType =>
chType -> outputType
fromChType
instance Serializable Int8 where serialize :: ProtocolRevision -> Int8 -> Builder
serialize ProtocolRevision
_ = Int8 -> Builder
int8 (Int8 -> Builder) -> (Int8 -> Int8) -> Int8 -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int8 -> Int8
forall chType outputType.
FromChType chType outputType =>
chType -> outputType
fromChType
instance Serializable Int16 where serialize :: ProtocolRevision -> Int16 -> Builder
serialize ProtocolRevision
_ = Int16 -> Builder
int16LE (Int16 -> Builder) -> (Int16 -> Int16) -> Int16 -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int16 -> Int16
forall chType outputType.
FromChType chType outputType =>
chType -> outputType
fromChType
instance Serializable Int32 where serialize :: ProtocolRevision -> Int32 -> Builder
serialize ProtocolRevision
_ = Int32 -> Builder
int32LE (Int32 -> Builder) -> (Int32 -> Int32) -> Int32 -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int32 -> Int32
forall chType outputType.
FromChType chType outputType =>
chType -> outputType
fromChType
instance Serializable Int64 where serialize :: ProtocolRevision -> Int64 -> Builder
serialize ProtocolRevision
_ = Int64 -> Builder
int64LE (Int64 -> Builder) -> (Int64 -> Int64) -> Int64 -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int64 -> Int64
forall chType outputType.
FromChType chType outputType =>
chType -> outputType
fromChType
instance Serializable Int128 where serialize :: ProtocolRevision -> Int128 -> Builder
serialize ProtocolRevision
_ = (\(Int128 UInt64
hi UInt64
lo) -> UInt64 -> Builder
word64LE UInt64
lo Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> UInt64 -> Builder
word64LE UInt64
hi) (Int128 -> Builder) -> (Int128 -> Int128) -> Int128 -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int128 -> Int128
forall chType outputType.
FromChType chType outputType =>
chType -> outputType
fromChType
instance Serializable UInt8 where serialize :: ProtocolRevision -> UInt8 -> Builder
serialize ProtocolRevision
_ = UInt8 -> Builder
word8 (UInt8 -> Builder) -> (UInt8 -> UInt8) -> UInt8 -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UInt8 -> UInt8
forall chType outputType.
FromChType chType outputType =>
chType -> outputType
fromChType
instance Serializable UInt16 where serialize :: ProtocolRevision -> UInt16 -> Builder
serialize ProtocolRevision
_ = UInt16 -> Builder
word16LE (UInt16 -> Builder) -> (UInt16 -> UInt16) -> UInt16 -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UInt16 -> UInt16
forall chType outputType.
FromChType chType outputType =>
chType -> outputType
fromChType
instance Serializable UInt32 where serialize :: ProtocolRevision -> UInt32 -> Builder
serialize ProtocolRevision
_ = UInt32 -> Builder
word32LE (UInt32 -> Builder) -> (UInt32 -> UInt32) -> UInt32 -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UInt32 -> UInt32
forall chType outputType.
FromChType chType outputType =>
chType -> outputType
fromChType
instance Serializable UInt64 where serialize :: ProtocolRevision -> UInt64 -> Builder
serialize ProtocolRevision
_ = UInt64 -> Builder
word64LE (UInt64 -> Builder) -> (UInt64 -> UInt64) -> UInt64 -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UInt64 -> UInt64
forall chType outputType.
FromChType chType outputType =>
chType -> outputType
fromChType
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) (UInt128 -> Builder) -> (UInt128 -> UInt128) -> UInt128 -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UInt128 -> UInt128
forall chType outputType.
FromChType chType outputType =>
chType -> outputType
fromChType
instance Serializable (DateTime tz) where serialize :: ProtocolRevision -> DateTime tz -> Builder
serialize ProtocolRevision
_ = UInt32 -> Builder
word32LE (UInt32 -> Builder)
-> (DateTime tz -> UInt32) -> DateTime tz -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DateTime tz -> UInt32
forall chType outputType.
FromChType chType outputType =>
chType -> outputType
fromChType
instance Serializable Date where serialize :: ProtocolRevision -> Date -> Builder
serialize ProtocolRevision
_ = UInt16 -> Builder
word16LE (UInt16 -> Builder) -> (Date -> UInt16) -> Date -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Date -> UInt16
forall chType outputType.
FromChType chType outputType =>
chType -> outputType
fromChType


-- ** Generics

class GSerializable f
  where
  gSerialize :: ProtocolRevision -> f p -> Builder

instance
  GSerializable f
  =>
  GSerializable (D1 c (C1 c2 f))
  where
  {-# INLINE gSerialize #-}
  gSerialize :: forall p. ProtocolRevision -> D1 c (C1 c2 f) p -> Builder
gSerialize ProtocolRevision
rev (M1 (M1 f p
re)) = ProtocolRevision -> f p -> Builder
forall p. ProtocolRevision -> f p -> Builder
forall (f :: * -> *) p.
GSerializable f =>
ProtocolRevision -> f p -> Builder
gSerialize ProtocolRevision
rev f p
re

instance
  GSerializable (left1 :*: (left2 :*: right))
  =>
  GSerializable ((left1 :*: left2) :*: right)
  where
  {-# INLINE gSerialize #-}
  gSerialize :: forall p.
ProtocolRevision -> (:*:) (left1 :*: left2) right p -> Builder
gSerialize ProtocolRevision
rev ((left1 p
l1 :*: left2 p
l2) :*: right p
r) = ProtocolRevision -> (:*:) left1 (left2 :*: right) p -> Builder
forall p.
ProtocolRevision -> (:*:) left1 (left2 :*: right) p -> Builder
forall (f :: * -> *) p.
GSerializable f =>
ProtocolRevision -> f p -> Builder
gSerialize ProtocolRevision
rev (left1 p
l1 left1 p -> (:*:) left2 right p -> (:*:) left1 (left2 :*: right) p
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: (left2 p
l2 left2 p -> right p -> (:*:) left2 right p
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: right p
r))

instance
  Serializable chType
  =>
  GSerializable (S1 (MetaSel (Just typeName) a b f) (Rec0 chType))
  where
  {-# INLINE gSerialize #-}
  gSerialize :: forall p.
ProtocolRevision
-> S1 ('MetaSel ('Just typeName) a b f) (Rec0 chType) p -> Builder
gSerialize ProtocolRevision
rev = ProtocolRevision -> chType -> Builder
forall chType.
Serializable chType =>
ProtocolRevision -> chType -> Builder
serialize ProtocolRevision
rev (chType -> Builder)
-> (S1 ('MetaSel ('Just typeName) a b f) (Rec0 chType) p -> chType)
-> S1 ('MetaSel ('Just typeName) a b f) (Rec0 chType) p
-> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. K1 R chType p -> chType
forall k i c (p :: k). K1 i c p -> c
unK1 (K1 R chType p -> chType)
-> (S1 ('MetaSel ('Just typeName) a b f) (Rec0 chType) p
    -> K1 R chType p)
-> S1 ('MetaSel ('Just typeName) a b f) (Rec0 chType) p
-> chType
forall b c a. (b -> c) -> (a -> b) -> a -> c
. S1 ('MetaSel ('Just typeName) a b f) (Rec0 chType) p
-> K1 R chType p
forall k i (c :: Meta) (f :: k -> *) (p :: k). M1 i c f p -> f p
unM1

instance
  (Serializable chType, GSerializable right)
  =>
  GSerializable (S1 (MetaSel (Just typeName) a b f) (Rec0 chType) :*: right)
  where
  {-# INLINE gSerialize #-}
  gSerialize :: forall p.
ProtocolRevision
-> (:*:)
     (S1 ('MetaSel ('Just typeName) a b f) (Rec0 chType)) right p
-> Builder
gSerialize ProtocolRevision
rev (S1 ('MetaSel ('Just typeName) a b f) (Rec0 chType) p
left :*: right p
right)
    = (ProtocolRevision -> chType -> Builder
forall chType.
Serializable chType =>
ProtocolRevision -> chType -> Builder
serialize ProtocolRevision
rev (chType -> Builder)
-> (S1 ('MetaSel ('Just typeName) a b f) (Rec0 chType) p -> chType)
-> S1 ('MetaSel ('Just typeName) a b f) (Rec0 chType) p
-> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. K1 R chType p -> chType
forall k i c (p :: k). K1 i c p -> c
unK1 (K1 R chType p -> chType)
-> (S1 ('MetaSel ('Just typeName) a b f) (Rec0 chType) p
    -> K1 R chType p)
-> S1 ('MetaSel ('Just typeName) a b f) (Rec0 chType) p
-> chType
forall b c a. (b -> c) -> (a -> b) -> a -> c
. S1 ('MetaSel ('Just typeName) a b f) (Rec0 chType) p
-> K1 R chType p
forall k i (c :: Meta) (f :: k -> *) (p :: k). M1 i c f p -> f p
unM1 (S1 ('MetaSel ('Just typeName) a b f) (Rec0 chType) p -> Builder)
-> S1 ('MetaSel ('Just typeName) a b f) (Rec0 chType) p -> Builder
forall a b. (a -> b) -> a -> b
$ S1 ('MetaSel ('Just typeName) a b f) (Rec0 chType) p
left) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> ProtocolRevision -> right p -> Builder
forall p. ProtocolRevision -> right p -> Builder
forall (f :: * -> *) p.
GSerializable f =>
ProtocolRevision -> f p -> Builder
gSerialize ProtocolRevision
rev right p
right








class
  KnownSymbol (ToChTypeName chType)
  =>
  IsChType chType
  where
  -- | Shows database original type name
  --
  -- @
  -- type ToChTypeName ChString = \"String\"
  -- type ToChTypeName (Nullable UInt32) = \"Nullable(UInt32)\"
  -- @
  type ToChTypeName chType :: Symbol

  chTypeName :: Builder
  chTypeName = StrictByteString -> Builder
byteString (StrictByteString -> Builder)
-> (Proxy (ToChTypeName chType) -> StrictByteString)
-> Proxy (ToChTypeName chType)
-> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> StrictByteString
BS8.pack (String -> StrictByteString)
-> (Proxy (ToChTypeName chType) -> String)
-> Proxy (ToChTypeName chType)
-> StrictByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (n :: Symbol) (proxy :: Symbol -> *).
KnownSymbol n =>
proxy n -> String
symbolVal @(ToChTypeName chType) (Proxy (ToChTypeName chType) -> Builder)
-> Proxy (ToChTypeName chType) -> Builder
forall a b. (a -> b) -> a -> b
$ Proxy (ToChTypeName chType)
forall {k} (t :: k). Proxy t
Proxy

  defaultValueOfTypeName :: chType

instance IsChType Int8 where
  type ToChTypeName Int8 = "Int8"
  defaultValueOfTypeName :: Int8
defaultValueOfTypeName = Int8
0

instance IsChType Int16 where
  type ToChTypeName Int16 = "Int16"
  defaultValueOfTypeName :: Int16
defaultValueOfTypeName = Int16
0

instance IsChType Int32 where
  type ToChTypeName Int32 = "Int32"
  defaultValueOfTypeName :: Int32
defaultValueOfTypeName = Int32
0

instance IsChType Int64 where
  type ToChTypeName Int64 = "Int64"
  defaultValueOfTypeName :: Int64
defaultValueOfTypeName = Int64
0

instance IsChType Int128 where
  type ToChTypeName Int128 = "Int128"
  defaultValueOfTypeName :: Int128
defaultValueOfTypeName = Int128
0
-- | ClickHouse UInt8 column type
type UInt8 = Word8
instance IsChType UInt8 where
  type ToChTypeName UInt8 = "UInt8"
  defaultValueOfTypeName :: UInt8
defaultValueOfTypeName = UInt8
0

-- | ClickHouse UInt16 column type
type UInt16 = Word16
instance IsChType UInt16 where
  type ToChTypeName UInt16 = "UInt16"
  defaultValueOfTypeName :: UInt16
defaultValueOfTypeName = UInt16
0

-- | ClickHouse UInt32 column type
type UInt32 = Word32
instance IsChType UInt32 where
  type ToChTypeName UInt32 = "UInt32"
  defaultValueOfTypeName :: UInt32
defaultValueOfTypeName = UInt32
0

-- | ClickHouse UInt64 column type
type UInt64 = Word64
instance IsChType UInt64 where
  type ToChTypeName UInt64 = "UInt64"
  defaultValueOfTypeName :: UInt64
defaultValueOfTypeName = UInt64
0

-- | ClickHouse UInt128 column type
type UInt128 = Word128
instance IsChType UInt128 where
  type ToChTypeName UInt128 = "UInt128"
  defaultValueOfTypeName :: UInt128
defaultValueOfTypeName = UInt128
0






class ToChType chType inputType    where toChType    :: inputType -> chType
class FromChType chType outputType where fromChType  :: chType -> outputType

instance {-# OVERLAPPABLE #-} (IsChType chType, chType ~ inputType) => ToChType chType inputType where toChType :: inputType -> chType
toChType = inputType -> chType
inputType -> inputType
forall a. a -> a
id
instance {-# OVERLAPPABLE #-} (IsChType chType, chType ~ inputType) => FromChType chType inputType where fromChType :: chType -> inputType
fromChType = chType -> chType
chType -> inputType
forall a. a -> a
id

instance ToChType Int64 Int where toChType :: Int -> Int64
toChType = Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral
instance ToChType UInt128 UInt64 where toChType :: UInt64 -> UInt128
toChType = UInt64 -> UInt128
forall a b. (Integral a, Num b) => a -> b
fromIntegral



type ChUInt8    = UInt8    ;{-# DEPRECATED ChUInt8    "Ch prefixed types are deprecated. Use UInt8 instead" #-}
type ChUInt16   = UInt16   ;{-# DEPRECATED ChUInt16   "Ch prefixed types are deprecated. Use UInt16 instead" #-}
type ChUInt32   = UInt32   ;{-# DEPRECATED ChUInt32   "Ch prefixed types are deprecated. Use UInt32 instead" #-}
type ChUInt64   = UInt64   ;{-# DEPRECATED ChUInt64   "Ch prefixed types are deprecated. Use UInt64 instead" #-}
type ChUInt128  = UInt128  ;{-# DEPRECATED ChUInt128  "Ch prefixed types are deprecated. Use UInt128 instead" #-}
type ChInt8     = Int8     ;{-# DEPRECATED ChInt8     "Ch prefixed types are deprecated. Use Int8 instead" #-}
type ChInt16    = Int16    ;{-# DEPRECATED ChInt16    "Ch prefixed types are deprecated. Use Int16 instead" #-}
type ChInt32    = Int32    ;{-# DEPRECATED ChInt32    "Ch prefixed types are deprecated. Use Int32 instead" #-}
type ChInt64    = Int64    ;{-# DEPRECATED ChInt64    "Ch prefixed types are deprecated. Use Int64 instead" #-}
type ChInt128   = Int128   ;{-# DEPRECATED ChInt128   "Ch prefixed types are deprecated. Use Int128 instead" #-}
type ChDateTime = DateTime ;{-# DEPRECATED ChDateTime "Ch prefixed types are deprecated. Use DateTime instead" #-}
type ChDate     = Date     ;{-# DEPRECATED ChDate     "Ch prefixed types are deprecated. Use Date instead" #-}



-- | ClickHouse Nullable(T) column type
-- (type synonym for Maybe)
type Nullable = Maybe

type NullableTypeName chType = "Nullable(" `AppendSymbol` ToChTypeName chType `AppendSymbol` ")"

instance
  ( IsChType chType
  , KnownSymbol ("Nullable(" `AppendSymbol` ToChTypeName chType `AppendSymbol` ")")
  )
  =>
  IsChType (Nullable chType)
  where
  type ToChTypeName (Nullable chType) = NullableTypeName chType
  defaultValueOfTypeName :: Nullable chType
defaultValueOfTypeName = Nullable chType
forall a. Maybe a
Nothing

instance
  ToChType inputType chType
  =>
  ToChType (Nullable inputType) (Nullable chType)
  where
  toChType :: Nullable chType -> Nullable inputType
toChType = (chType -> inputType) -> Nullable chType -> Nullable inputType
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall chType inputType.
ToChType chType inputType =>
inputType -> chType
toChType @inputType @chType)

instance
  FromChType chType inputType
  =>
  FromChType (Nullable chType) (Nullable inputType)
  where
  fromChType :: Nullable chType -> Nullable inputType
fromChType = (chType -> inputType) -> Nullable chType -> Nullable inputType
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall chType outputType.
FromChType chType outputType =>
chType -> outputType
fromChType @chType)




-- | ClickHouse LowCardinality(T) column type
newtype LowCardinality chType = MkLowCardinality chType
instance
  ( IsLowCardinalitySupported chType
  , KnownSymbol ("LowCardinality(" `AppendSymbol` ToChTypeName chType `AppendSymbol` ")")
  ) =>
  IsChType (LowCardinality chType)
  where
  type ToChTypeName (LowCardinality chType) = "LowCardinality(" `AppendSymbol` ToChTypeName chType `AppendSymbol` ")"
  defaultValueOfTypeName :: LowCardinality chType
defaultValueOfTypeName = chType -> LowCardinality chType
forall chType. chType -> LowCardinality chType
MkLowCardinality (chType -> LowCardinality chType)
-> chType -> LowCardinality chType
forall a b. (a -> b) -> a -> b
$ forall chType. IsChType chType => chType
defaultValueOfTypeName @chType

deriving newtype instance (Eq chType, IsLowCardinalitySupported chType) => Eq (LowCardinality chType)
deriving newtype instance (Show chType, IsLowCardinalitySupported chType) => Show (LowCardinality chType)
deriving newtype instance (NFData chType, IsLowCardinalitySupported chType) => NFData (LowCardinality chType)
deriving newtype instance IsString (LowCardinality ChString)

class IsChType chType => IsLowCardinalitySupported chType
instance IsLowCardinalitySupported ChString
instance
  ( IsLowCardinalitySupported chType
  , IsChType (Nullable chType)
  ) =>
  IsLowCardinalitySupported (Nullable chType)

instance {-# OVERLAPPABLE #-}
  ( IsChType chType
  , TypeError
    (    'Text "LowCardinality("  ':<>: 'ShowType chType  ':<>: 'Text ") is unsupported"
    ':$$: 'Text "Use one of these types:"
    ':$$: 'Text "  ChString"
    ':$$: 'Text "  DateTime"
    ':$$: 'Text "  Nullable(T)"
    )
  ) => IsLowCardinalitySupported chType

instance ToChType inputType chType => ToChType (LowCardinality inputType) chType where
  toChType :: chType -> LowCardinality inputType
toChType = inputType -> LowCardinality inputType
forall chType. chType -> LowCardinality chType
MkLowCardinality (inputType -> LowCardinality inputType)
-> (chType -> inputType) -> chType -> LowCardinality inputType
forall b c a. (b -> c) -> (a -> b) -> a -> c
. chType -> inputType
forall chType inputType.
ToChType chType inputType =>
inputType -> chType
toChType

instance FromChType chType (LowCardinality chType) where
  fromChType :: chType -> LowCardinality chType
fromChType = chType -> LowCardinality chType
forall chType. chType -> LowCardinality chType
MkLowCardinality

instance
  FromChType chType outputType
  =>
  FromChType (LowCardinality chType) outputType
  where
  fromChType :: LowCardinality chType -> outputType
fromChType (MkLowCardinality chType
value) = chType -> outputType
forall chType outputType.
FromChType chType outputType =>
chType -> outputType
fromChType chType
value





-- | ClickHouse UUID column type
newtype ChUUID = MkChUUID Word128
  deriving newtype ((forall x. ChUUID -> Rep ChUUID x)
-> (forall x. Rep ChUUID x -> ChUUID) -> Generic ChUUID
forall x. Rep ChUUID x -> ChUUID
forall x. ChUUID -> Rep ChUUID x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ChUUID -> Rep ChUUID x
from :: forall x. ChUUID -> Rep ChUUID x
$cto :: forall x. Rep ChUUID x -> ChUUID
to :: forall x. Rep ChUUID x -> ChUUID
Generic, Int -> ChUUID -> ShowS
[ChUUID] -> ShowS
ChUUID -> String
(Int -> ChUUID -> ShowS)
-> (ChUUID -> String) -> ([ChUUID] -> ShowS) -> Show ChUUID
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ChUUID -> ShowS
showsPrec :: Int -> ChUUID -> ShowS
$cshow :: ChUUID -> String
show :: ChUUID -> String
$cshowList :: [ChUUID] -> ShowS
showList :: [ChUUID] -> ShowS
Show, ChUUID -> ChUUID -> Bool
(ChUUID -> ChUUID -> Bool)
-> (ChUUID -> ChUUID -> Bool) -> Eq ChUUID
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ChUUID -> ChUUID -> Bool
== :: ChUUID -> ChUUID -> Bool
$c/= :: ChUUID -> ChUUID -> Bool
/= :: ChUUID -> ChUUID -> Bool
Eq, ChUUID -> ()
(ChUUID -> ()) -> NFData ChUUID
forall a. (a -> ()) -> NFData a
$crnf :: ChUUID -> ()
rnf :: ChUUID -> ()
NFData, ChUUID
ChUUID -> ChUUID -> Bounded ChUUID
forall a. a -> a -> Bounded a
$cminBound :: ChUUID
minBound :: ChUUID
$cmaxBound :: ChUUID
maxBound :: ChUUID
Bounded, Int -> ChUUID
ChUUID -> Int
ChUUID -> [ChUUID]
ChUUID -> ChUUID
ChUUID -> ChUUID -> [ChUUID]
ChUUID -> ChUUID -> ChUUID -> [ChUUID]
(ChUUID -> ChUUID)
-> (ChUUID -> ChUUID)
-> (Int -> ChUUID)
-> (ChUUID -> Int)
-> (ChUUID -> [ChUUID])
-> (ChUUID -> ChUUID -> [ChUUID])
-> (ChUUID -> ChUUID -> [ChUUID])
-> (ChUUID -> ChUUID -> ChUUID -> [ChUUID])
-> Enum ChUUID
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
$csucc :: ChUUID -> ChUUID
succ :: ChUUID -> ChUUID
$cpred :: ChUUID -> ChUUID
pred :: ChUUID -> ChUUID
$ctoEnum :: Int -> ChUUID
toEnum :: Int -> ChUUID
$cfromEnum :: ChUUID -> Int
fromEnum :: ChUUID -> Int
$cenumFrom :: ChUUID -> [ChUUID]
enumFrom :: ChUUID -> [ChUUID]
$cenumFromThen :: ChUUID -> ChUUID -> [ChUUID]
enumFromThen :: ChUUID -> ChUUID -> [ChUUID]
$cenumFromTo :: ChUUID -> ChUUID -> [ChUUID]
enumFromTo :: ChUUID -> ChUUID -> [ChUUID]
$cenumFromThenTo :: ChUUID -> ChUUID -> ChUUID -> [ChUUID]
enumFromThenTo :: ChUUID -> ChUUID -> ChUUID -> [ChUUID]
Enum)

instance IsChType ChUUID where
  type ToChTypeName ChUUID = "UUID"
  defaultValueOfTypeName :: ChUUID
defaultValueOfTypeName = UInt128 -> ChUUID
MkChUUID UInt128
0


instance ToChType ChUUID Word64 where toChType :: UInt64 -> ChUUID
toChType = UInt128 -> ChUUID
MkChUUID (UInt128 -> ChUUID) -> (UInt64 -> UInt128) -> UInt64 -> ChUUID
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (UInt64 -> UInt64 -> UInt128) -> UInt64 -> UInt64 -> UInt128
forall a b c. (a -> b -> c) -> b -> a -> c
flip UInt64 -> UInt64 -> UInt128
Word128 UInt64
0
instance ToChType ChUUID (Word64, Word64) where toChType :: (UInt64, UInt64) -> ChUUID
toChType = UInt128 -> ChUUID
MkChUUID (UInt128 -> ChUUID)
-> ((UInt64, UInt64) -> UInt128) -> (UInt64, UInt64) -> ChUUID
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (UInt64 -> UInt64 -> UInt128) -> (UInt64, UInt64) -> UInt128
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry ((UInt64 -> UInt64 -> UInt128) -> UInt64 -> UInt64 -> UInt128
forall a b c. (a -> b -> c) -> b -> a -> c
flip UInt64 -> UInt64 -> UInt128
Word128)

instance FromChType ChUUID (Word64, Word64) where fromChType :: ChUUID -> (UInt64, UInt64)
fromChType (MkChUUID (Word128 UInt64
w64hi UInt64
w64lo)) = (UInt64
w64hi, UInt64
w64lo)




-- | ClickHouse String column type
newtype ChString = MkChString StrictByteString
  deriving newtype (Int -> ChString -> ShowS
[ChString] -> ShowS
ChString -> String
(Int -> ChString -> ShowS)
-> (ChString -> String) -> ([ChString] -> ShowS) -> Show ChString
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ChString -> ShowS
showsPrec :: Int -> ChString -> ShowS
$cshow :: ChString -> String
show :: ChString -> String
$cshowList :: [ChString] -> ShowS
showList :: [ChString] -> ShowS
Show, ChString -> ChString -> Bool
(ChString -> ChString -> Bool)
-> (ChString -> ChString -> Bool) -> Eq ChString
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ChString -> ChString -> Bool
== :: ChString -> ChString -> Bool
$c/= :: ChString -> ChString -> Bool
/= :: ChString -> ChString -> Bool
Eq, String -> ChString
(String -> ChString) -> IsString ChString
forall a. (String -> a) -> IsString a
$cfromString :: String -> ChString
fromString :: String -> ChString
IsString, ChString -> ()
(ChString -> ()) -> NFData ChString
forall a. (a -> ()) -> NFData a
$crnf :: ChString -> ()
rnf :: ChString -> ()
NFData)

instance IsChType ChString where
  type ToChTypeName ChString = "String"
  defaultValueOfTypeName :: ChString
defaultValueOfTypeName = ChString
""


instance ToChType ChString StrictByteString where toChType :: StrictByteString -> ChString
toChType = StrictByteString -> ChString
MkChString
instance ToChType ChString Builder          where toChType :: Builder -> ChString
toChType = StrictByteString -> ChString
MkChString (StrictByteString -> ChString)
-> (Builder -> StrictByteString) -> Builder -> ChString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> StrictByteString
toStrict (ByteString -> StrictByteString)
-> (Builder -> ByteString) -> Builder -> StrictByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> ByteString
toLazyByteString
instance ToChType ChString String           where toChType :: String -> ChString
toChType = StrictByteString -> ChString
MkChString (StrictByteString -> ChString)
-> (String -> StrictByteString) -> String -> ChString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> StrictByteString
BS8.pack
instance ToChType ChString Text             where toChType :: Text -> ChString
toChType = StrictByteString -> ChString
MkChString (StrictByteString -> ChString)
-> (Text -> StrictByteString) -> Text -> ChString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> StrictByteString
Text.encodeUtf8
instance ToChType ChString Int              where toChType :: Int -> ChString
toChType = StrictByteString -> ChString
MkChString (StrictByteString -> ChString)
-> (Int -> StrictByteString) -> Int -> ChString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> StrictByteString
BS8.pack (String -> StrictByteString)
-> (Int -> String) -> Int -> StrictByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> String
forall a. Show a => a -> String
show

instance FromChType ChString StrictByteString where fromChType :: ChString -> StrictByteString
fromChType (MkChString StrictByteString
string) = StrictByteString
string
instance FromChType ChString Builder where fromChType :: ChString -> Builder
fromChType (MkChString StrictByteString
string) = StrictByteString -> Builder
byteString StrictByteString
string
instance
  ( TypeError
    (     'Text "ChString to Text using FromChType convertion could cause exception"
    ':$$: 'Text "Decode ByteString manually if you are sure it's always can be decoded or replace it with ByteString"
    )
  ) =>
  FromChType ChString Text
  where
  fromChType :: ChString -> Text
fromChType = String -> ChString -> Text
forall a. HasCallStack => String -> a
error String
"Unreachable"




{- |
ClickHouse DateTime column type (paramtrized with timezone)

>>> chTypeName @(DateTime "")
"DateTime"
>>> chTypeName @(DateTime "UTC")
"DateTime('UTC')"
-}
newtype DateTime (tz :: Symbol) = MkDateTime Word32
  deriving newtype (Int -> DateTime tz -> ShowS
[DateTime tz] -> ShowS
DateTime tz -> String
(Int -> DateTime tz -> ShowS)
-> (DateTime tz -> String)
-> ([DateTime tz] -> ShowS)
-> Show (DateTime tz)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall (tz :: Symbol). Int -> DateTime tz -> ShowS
forall (tz :: Symbol). [DateTime tz] -> ShowS
forall (tz :: Symbol). DateTime tz -> String
$cshowsPrec :: forall (tz :: Symbol). Int -> DateTime tz -> ShowS
showsPrec :: Int -> DateTime tz -> ShowS
$cshow :: forall (tz :: Symbol). DateTime tz -> String
show :: DateTime tz -> String
$cshowList :: forall (tz :: Symbol). [DateTime tz] -> ShowS
showList :: [DateTime tz] -> ShowS
Show, DateTime tz -> DateTime tz -> Bool
(DateTime tz -> DateTime tz -> Bool)
-> (DateTime tz -> DateTime tz -> Bool) -> Eq (DateTime tz)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall (tz :: Symbol). DateTime tz -> DateTime tz -> Bool
$c== :: forall (tz :: Symbol). DateTime tz -> DateTime tz -> Bool
== :: DateTime tz -> DateTime tz -> Bool
$c/= :: forall (tz :: Symbol). DateTime tz -> DateTime tz -> Bool
/= :: DateTime tz -> DateTime tz -> Bool
Eq, Integer -> DateTime tz
DateTime tz -> DateTime tz
DateTime tz -> DateTime tz -> DateTime tz
(DateTime tz -> DateTime tz -> DateTime tz)
-> (DateTime tz -> DateTime tz -> DateTime tz)
-> (DateTime tz -> DateTime tz -> DateTime tz)
-> (DateTime tz -> DateTime tz)
-> (DateTime tz -> DateTime tz)
-> (DateTime tz -> DateTime tz)
-> (Integer -> DateTime tz)
-> Num (DateTime tz)
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
forall (tz :: Symbol). Integer -> DateTime tz
forall (tz :: Symbol). DateTime tz -> DateTime tz
forall (tz :: Symbol). DateTime tz -> DateTime tz -> DateTime tz
$c+ :: forall (tz :: Symbol). DateTime tz -> DateTime tz -> DateTime tz
+ :: DateTime tz -> DateTime tz -> DateTime tz
$c- :: forall (tz :: Symbol). DateTime tz -> DateTime tz -> DateTime tz
- :: DateTime tz -> DateTime tz -> DateTime tz
$c* :: forall (tz :: Symbol). DateTime tz -> DateTime tz -> DateTime tz
* :: DateTime tz -> DateTime tz -> DateTime tz
$cnegate :: forall (tz :: Symbol). DateTime tz -> DateTime tz
negate :: DateTime tz -> DateTime tz
$cabs :: forall (tz :: Symbol). DateTime tz -> DateTime tz
abs :: DateTime tz -> DateTime tz
$csignum :: forall (tz :: Symbol). DateTime tz -> DateTime tz
signum :: DateTime tz -> DateTime tz
$cfromInteger :: forall (tz :: Symbol). Integer -> DateTime tz
fromInteger :: Integer -> DateTime tz
Num, Eq (DateTime tz)
DateTime tz
Eq (DateTime tz) =>
(DateTime tz -> DateTime tz -> DateTime tz)
-> (DateTime tz -> DateTime tz -> DateTime tz)
-> (DateTime tz -> DateTime tz -> DateTime tz)
-> (DateTime tz -> DateTime tz)
-> (DateTime tz -> Int -> DateTime tz)
-> (DateTime tz -> Int -> DateTime tz)
-> DateTime tz
-> (Int -> DateTime tz)
-> (DateTime tz -> Int -> DateTime tz)
-> (DateTime tz -> Int -> DateTime tz)
-> (DateTime tz -> Int -> DateTime tz)
-> (DateTime tz -> Int -> Bool)
-> (DateTime tz -> Maybe Int)
-> (DateTime tz -> Int)
-> (DateTime tz -> Bool)
-> (DateTime tz -> Int -> DateTime tz)
-> (DateTime tz -> Int -> DateTime tz)
-> (DateTime tz -> Int -> DateTime tz)
-> (DateTime tz -> Int -> DateTime tz)
-> (DateTime tz -> Int -> DateTime tz)
-> (DateTime tz -> Int -> DateTime tz)
-> (DateTime tz -> Int)
-> Bits (DateTime tz)
Int -> DateTime tz
DateTime tz -> Bool
DateTime tz -> Int
DateTime tz -> Maybe Int
DateTime tz -> DateTime tz
DateTime tz -> Int -> Bool
DateTime tz -> Int -> DateTime tz
DateTime tz -> DateTime tz -> DateTime tz
forall a.
Eq a =>
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> a
-> (Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> Bool)
-> (a -> Maybe Int)
-> (a -> Int)
-> (a -> Bool)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int)
-> Bits a
forall (tz :: Symbol). Eq (DateTime tz)
forall (tz :: Symbol). DateTime tz
forall (tz :: Symbol). Int -> DateTime tz
forall (tz :: Symbol). DateTime tz -> Bool
forall (tz :: Symbol). DateTime tz -> Int
forall (tz :: Symbol). DateTime tz -> Maybe Int
forall (tz :: Symbol). DateTime tz -> DateTime tz
forall (tz :: Symbol). DateTime tz -> Int -> Bool
forall (tz :: Symbol). DateTime tz -> Int -> DateTime tz
forall (tz :: Symbol). DateTime tz -> DateTime tz -> DateTime tz
$c.&. :: forall (tz :: Symbol). DateTime tz -> DateTime tz -> DateTime tz
.&. :: DateTime tz -> DateTime tz -> DateTime tz
$c.|. :: forall (tz :: Symbol). DateTime tz -> DateTime tz -> DateTime tz
.|. :: DateTime tz -> DateTime tz -> DateTime tz
$cxor :: forall (tz :: Symbol). DateTime tz -> DateTime tz -> DateTime tz
xor :: DateTime tz -> DateTime tz -> DateTime tz
$ccomplement :: forall (tz :: Symbol). DateTime tz -> DateTime tz
complement :: DateTime tz -> DateTime tz
$cshift :: forall (tz :: Symbol). DateTime tz -> Int -> DateTime tz
shift :: DateTime tz -> Int -> DateTime tz
$crotate :: forall (tz :: Symbol). DateTime tz -> Int -> DateTime tz
rotate :: DateTime tz -> Int -> DateTime tz
$czeroBits :: forall (tz :: Symbol). DateTime tz
zeroBits :: DateTime tz
$cbit :: forall (tz :: Symbol). Int -> DateTime tz
bit :: Int -> DateTime tz
$csetBit :: forall (tz :: Symbol). DateTime tz -> Int -> DateTime tz
setBit :: DateTime tz -> Int -> DateTime tz
$cclearBit :: forall (tz :: Symbol). DateTime tz -> Int -> DateTime tz
clearBit :: DateTime tz -> Int -> DateTime tz
$ccomplementBit :: forall (tz :: Symbol). DateTime tz -> Int -> DateTime tz
complementBit :: DateTime tz -> Int -> DateTime tz
$ctestBit :: forall (tz :: Symbol). DateTime tz -> Int -> Bool
testBit :: DateTime tz -> Int -> Bool
$cbitSizeMaybe :: forall (tz :: Symbol). DateTime tz -> Maybe Int
bitSizeMaybe :: DateTime tz -> Maybe Int
$cbitSize :: forall (tz :: Symbol). DateTime tz -> Int
bitSize :: DateTime tz -> Int
$cisSigned :: forall (tz :: Symbol). DateTime tz -> Bool
isSigned :: DateTime tz -> Bool
$cshiftL :: forall (tz :: Symbol). DateTime tz -> Int -> DateTime tz
shiftL :: DateTime tz -> Int -> DateTime tz
$cunsafeShiftL :: forall (tz :: Symbol). DateTime tz -> Int -> DateTime tz
unsafeShiftL :: DateTime tz -> Int -> DateTime tz
$cshiftR :: forall (tz :: Symbol). DateTime tz -> Int -> DateTime tz
shiftR :: DateTime tz -> Int -> DateTime tz
$cunsafeShiftR :: forall (tz :: Symbol). DateTime tz -> Int -> DateTime tz
unsafeShiftR :: DateTime tz -> Int -> DateTime tz
$crotateL :: forall (tz :: Symbol). DateTime tz -> Int -> DateTime tz
rotateL :: DateTime tz -> Int -> DateTime tz
$crotateR :: forall (tz :: Symbol). DateTime tz -> Int -> DateTime tz
rotateR :: DateTime tz -> Int -> DateTime tz
$cpopCount :: forall (tz :: Symbol). DateTime tz -> Int
popCount :: DateTime tz -> Int
Bits, Int -> DateTime tz
DateTime tz -> Int
DateTime tz -> [DateTime tz]
DateTime tz -> DateTime tz
DateTime tz -> DateTime tz -> [DateTime tz]
DateTime tz -> DateTime tz -> DateTime tz -> [DateTime tz]
(DateTime tz -> DateTime tz)
-> (DateTime tz -> DateTime tz)
-> (Int -> DateTime tz)
-> (DateTime tz -> Int)
-> (DateTime tz -> [DateTime tz])
-> (DateTime tz -> DateTime tz -> [DateTime tz])
-> (DateTime tz -> DateTime tz -> [DateTime tz])
-> (DateTime tz -> DateTime tz -> DateTime tz -> [DateTime tz])
-> Enum (DateTime tz)
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
forall (tz :: Symbol). Int -> DateTime tz
forall (tz :: Symbol). DateTime tz -> Int
forall (tz :: Symbol). DateTime tz -> [DateTime tz]
forall (tz :: Symbol). DateTime tz -> DateTime tz
forall (tz :: Symbol). DateTime tz -> DateTime tz -> [DateTime tz]
forall (tz :: Symbol).
DateTime tz -> DateTime tz -> DateTime tz -> [DateTime tz]
$csucc :: forall (tz :: Symbol). DateTime tz -> DateTime tz
succ :: DateTime tz -> DateTime tz
$cpred :: forall (tz :: Symbol). DateTime tz -> DateTime tz
pred :: DateTime tz -> DateTime tz
$ctoEnum :: forall (tz :: Symbol). Int -> DateTime tz
toEnum :: Int -> DateTime tz
$cfromEnum :: forall (tz :: Symbol). DateTime tz -> Int
fromEnum :: DateTime tz -> Int
$cenumFrom :: forall (tz :: Symbol). DateTime tz -> [DateTime tz]
enumFrom :: DateTime tz -> [DateTime tz]
$cenumFromThen :: forall (tz :: Symbol). DateTime tz -> DateTime tz -> [DateTime tz]
enumFromThen :: DateTime tz -> DateTime tz -> [DateTime tz]
$cenumFromTo :: forall (tz :: Symbol). DateTime tz -> DateTime tz -> [DateTime tz]
enumFromTo :: DateTime tz -> DateTime tz -> [DateTime tz]
$cenumFromThenTo :: forall (tz :: Symbol).
DateTime tz -> DateTime tz -> DateTime tz -> [DateTime tz]
enumFromThenTo :: DateTime tz -> DateTime tz -> DateTime tz -> [DateTime tz]
Enum, Eq (DateTime tz)
Eq (DateTime tz) =>
(DateTime tz -> DateTime tz -> Ordering)
-> (DateTime tz -> DateTime tz -> Bool)
-> (DateTime tz -> DateTime tz -> Bool)
-> (DateTime tz -> DateTime tz -> Bool)
-> (DateTime tz -> DateTime tz -> Bool)
-> (DateTime tz -> DateTime tz -> DateTime tz)
-> (DateTime tz -> DateTime tz -> DateTime tz)
-> Ord (DateTime tz)
DateTime tz -> DateTime tz -> Bool
DateTime tz -> DateTime tz -> Ordering
DateTime tz -> DateTime tz -> DateTime tz
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall (tz :: Symbol). Eq (DateTime tz)
forall (tz :: Symbol). DateTime tz -> DateTime tz -> Bool
forall (tz :: Symbol). DateTime tz -> DateTime tz -> Ordering
forall (tz :: Symbol). DateTime tz -> DateTime tz -> DateTime tz
$ccompare :: forall (tz :: Symbol). DateTime tz -> DateTime tz -> Ordering
compare :: DateTime tz -> DateTime tz -> Ordering
$c< :: forall (tz :: Symbol). DateTime tz -> DateTime tz -> Bool
< :: DateTime tz -> DateTime tz -> Bool
$c<= :: forall (tz :: Symbol). DateTime tz -> DateTime tz -> Bool
<= :: DateTime tz -> DateTime tz -> Bool
$c> :: forall (tz :: Symbol). DateTime tz -> DateTime tz -> Bool
> :: DateTime tz -> DateTime tz -> Bool
$c>= :: forall (tz :: Symbol). DateTime tz -> DateTime tz -> Bool
>= :: DateTime tz -> DateTime tz -> Bool
$cmax :: forall (tz :: Symbol). DateTime tz -> DateTime tz -> DateTime tz
max :: DateTime tz -> DateTime tz -> DateTime tz
$cmin :: forall (tz :: Symbol). DateTime tz -> DateTime tz -> DateTime tz
min :: DateTime tz -> DateTime tz -> DateTime tz
Ord, Num (DateTime tz)
Ord (DateTime tz)
(Num (DateTime tz), Ord (DateTime tz)) =>
(DateTime tz -> Rational) -> Real (DateTime tz)
DateTime tz -> Rational
forall a. (Num a, Ord a) => (a -> Rational) -> Real a
forall (tz :: Symbol). Num (DateTime tz)
forall (tz :: Symbol). Ord (DateTime tz)
forall (tz :: Symbol). DateTime tz -> Rational
$ctoRational :: forall (tz :: Symbol). DateTime tz -> Rational
toRational :: DateTime tz -> Rational
Real, Enum (DateTime tz)
Real (DateTime tz)
(Real (DateTime tz), Enum (DateTime tz)) =>
(DateTime tz -> DateTime tz -> DateTime tz)
-> (DateTime tz -> DateTime tz -> DateTime tz)
-> (DateTime tz -> DateTime tz -> DateTime tz)
-> (DateTime tz -> DateTime tz -> DateTime tz)
-> (DateTime tz -> DateTime tz -> (DateTime tz, DateTime tz))
-> (DateTime tz -> DateTime tz -> (DateTime tz, DateTime tz))
-> (DateTime tz -> Integer)
-> Integral (DateTime tz)
DateTime tz -> Integer
DateTime tz -> DateTime tz -> (DateTime tz, DateTime tz)
DateTime tz -> DateTime tz -> DateTime tz
forall a.
(Real a, Enum a) =>
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> (a, a))
-> (a -> a -> (a, a))
-> (a -> Integer)
-> Integral a
forall (tz :: Symbol). Enum (DateTime tz)
forall (tz :: Symbol). Real (DateTime tz)
forall (tz :: Symbol). DateTime tz -> Integer
forall (tz :: Symbol).
DateTime tz -> DateTime tz -> (DateTime tz, DateTime tz)
forall (tz :: Symbol). DateTime tz -> DateTime tz -> DateTime tz
$cquot :: forall (tz :: Symbol). DateTime tz -> DateTime tz -> DateTime tz
quot :: DateTime tz -> DateTime tz -> DateTime tz
$crem :: forall (tz :: Symbol). DateTime tz -> DateTime tz -> DateTime tz
rem :: DateTime tz -> DateTime tz -> DateTime tz
$cdiv :: forall (tz :: Symbol). DateTime tz -> DateTime tz -> DateTime tz
div :: DateTime tz -> DateTime tz -> DateTime tz
$cmod :: forall (tz :: Symbol). DateTime tz -> DateTime tz -> DateTime tz
mod :: DateTime tz -> DateTime tz -> DateTime tz
$cquotRem :: forall (tz :: Symbol).
DateTime tz -> DateTime tz -> (DateTime tz, DateTime tz)
quotRem :: DateTime tz -> DateTime tz -> (DateTime tz, DateTime tz)
$cdivMod :: forall (tz :: Symbol).
DateTime tz -> DateTime tz -> (DateTime tz, DateTime tz)
divMod :: DateTime tz -> DateTime tz -> (DateTime tz, DateTime tz)
$ctoInteger :: forall (tz :: Symbol). DateTime tz -> Integer
toInteger :: DateTime tz -> Integer
Integral, DateTime tz
DateTime tz -> DateTime tz -> Bounded (DateTime tz)
forall a. a -> a -> Bounded a
forall (tz :: Symbol). DateTime tz
$cminBound :: forall (tz :: Symbol). DateTime tz
minBound :: DateTime tz
$cmaxBound :: forall (tz :: Symbol). DateTime tz
maxBound :: DateTime tz
Bounded, DateTime tz -> ()
(DateTime tz -> ()) -> NFData (DateTime tz)
forall a. (a -> ()) -> NFData a
forall (tz :: Symbol). DateTime tz -> ()
$crnf :: forall (tz :: Symbol). DateTime tz -> ()
rnf :: DateTime tz -> ()
NFData)

instance KnownSymbol (ToChTypeName (DateTime tz)) => IsChType (DateTime tz)
  where
  type ToChTypeName (DateTime tz) = If (tz == "") ("DateTime") ("DateTime('" `AppendSymbol` tz `AppendSymbol` "')")
  defaultValueOfTypeName :: DateTime tz
defaultValueOfTypeName = UInt32 -> DateTime tz
forall (tz :: Symbol). UInt32 -> DateTime tz
MkDateTime UInt32
0


instance ToChType (DateTime tz) Word32     where toChType :: UInt32 -> DateTime tz
toChType = UInt32 -> DateTime tz
forall (tz :: Symbol). UInt32 -> DateTime tz
MkDateTime
instance ToChType (DateTime tz) UTCTime    where toChType :: UTCTime -> DateTime tz
toChType = UInt32 -> DateTime tz
forall (tz :: Symbol). UInt32 -> DateTime tz
MkDateTime (UInt32 -> DateTime tz)
-> (UTCTime -> UInt32) -> UTCTime -> DateTime tz
forall b c a. (b -> c) -> (a -> b) -> a -> c
. POSIXTime -> UInt32
forall b. Integral b => POSIXTime -> b
forall a b. (RealFrac a, Integral b) => a -> b
floor (POSIXTime -> UInt32)
-> (UTCTime -> POSIXTime) -> UTCTime -> UInt32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UTCTime -> POSIXTime
utcTimeToPOSIXSeconds
instance ToChType (DateTime tz) ZonedTime  where toChType :: ZonedTime -> DateTime tz
toChType = UInt32 -> DateTime tz
forall (tz :: Symbol). UInt32 -> DateTime tz
MkDateTime (UInt32 -> DateTime tz)
-> (ZonedTime -> UInt32) -> ZonedTime -> DateTime tz
forall b c a. (b -> c) -> (a -> b) -> a -> c
. POSIXTime -> UInt32
forall b. Integral b => POSIXTime -> b
forall a b. (RealFrac a, Integral b) => a -> b
floor (POSIXTime -> UInt32)
-> (ZonedTime -> POSIXTime) -> ZonedTime -> UInt32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UTCTime -> POSIXTime
utcTimeToPOSIXSeconds (UTCTime -> POSIXTime)
-> (ZonedTime -> UTCTime) -> ZonedTime -> POSIXTime
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ZonedTime -> UTCTime
zonedTimeToUTC

instance FromChType (DateTime tz) Word32     where fromChType :: DateTime tz -> UInt32
fromChType = DateTime tz -> UInt32
forall a b. Coercible a b => a -> b
coerce
instance FromChType (DateTime tz) UTCTime    where fromChType :: DateTime tz -> UTCTime
fromChType (MkDateTime UInt32
w32) = POSIXTime -> UTCTime
posixSecondsToUTCTime (UInt32 -> POSIXTime
forall a b. (Integral a, Num b) => a -> b
fromIntegral UInt32
w32)




newtype Date = MkChDate Word16
  deriving newtype (Int -> Date -> ShowS
[Date] -> ShowS
Date -> String
(Int -> Date -> ShowS)
-> (Date -> String) -> ([Date] -> ShowS) -> Show Date
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Date -> ShowS
showsPrec :: Int -> Date -> ShowS
$cshow :: Date -> String
show :: Date -> String
$cshowList :: [Date] -> ShowS
showList :: [Date] -> ShowS
Show, Date -> Date -> Bool
(Date -> Date -> Bool) -> (Date -> Date -> Bool) -> Eq Date
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Date -> Date -> Bool
== :: Date -> Date -> Bool
$c/= :: Date -> Date -> Bool
/= :: Date -> Date -> Bool
Eq, Eq Date
Date
Eq Date =>
(Date -> Date -> Date)
-> (Date -> Date -> Date)
-> (Date -> Date -> Date)
-> (Date -> Date)
-> (Date -> Int -> Date)
-> (Date -> Int -> Date)
-> Date
-> (Int -> Date)
-> (Date -> Int -> Date)
-> (Date -> Int -> Date)
-> (Date -> Int -> Date)
-> (Date -> Int -> Bool)
-> (Date -> Maybe Int)
-> (Date -> Int)
-> (Date -> Bool)
-> (Date -> Int -> Date)
-> (Date -> Int -> Date)
-> (Date -> Int -> Date)
-> (Date -> Int -> Date)
-> (Date -> Int -> Date)
-> (Date -> Int -> Date)
-> (Date -> Int)
-> Bits Date
Int -> Date
Date -> Bool
Date -> Int
Date -> Maybe Int
Date -> Date
Date -> Int -> Bool
Date -> Int -> Date
Date -> Date -> Date
forall a.
Eq a =>
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> a
-> (Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> Bool)
-> (a -> Maybe Int)
-> (a -> Int)
-> (a -> Bool)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int)
-> Bits a
$c.&. :: Date -> Date -> Date
.&. :: Date -> Date -> Date
$c.|. :: Date -> Date -> Date
.|. :: Date -> Date -> Date
$cxor :: Date -> Date -> Date
xor :: Date -> Date -> Date
$ccomplement :: Date -> Date
complement :: Date -> Date
$cshift :: Date -> Int -> Date
shift :: Date -> Int -> Date
$crotate :: Date -> Int -> Date
rotate :: Date -> Int -> Date
$czeroBits :: Date
zeroBits :: Date
$cbit :: Int -> Date
bit :: Int -> Date
$csetBit :: Date -> Int -> Date
setBit :: Date -> Int -> Date
$cclearBit :: Date -> Int -> Date
clearBit :: Date -> Int -> Date
$ccomplementBit :: Date -> Int -> Date
complementBit :: Date -> Int -> Date
$ctestBit :: Date -> Int -> Bool
testBit :: Date -> Int -> Bool
$cbitSizeMaybe :: Date -> Maybe Int
bitSizeMaybe :: Date -> Maybe Int
$cbitSize :: Date -> Int
bitSize :: Date -> Int
$cisSigned :: Date -> Bool
isSigned :: Date -> Bool
$cshiftL :: Date -> Int -> Date
shiftL :: Date -> Int -> Date
$cunsafeShiftL :: Date -> Int -> Date
unsafeShiftL :: Date -> Int -> Date
$cshiftR :: Date -> Int -> Date
shiftR :: Date -> Int -> Date
$cunsafeShiftR :: Date -> Int -> Date
unsafeShiftR :: Date -> Int -> Date
$crotateL :: Date -> Int -> Date
rotateL :: Date -> Int -> Date
$crotateR :: Date -> Int -> Date
rotateR :: Date -> Int -> Date
$cpopCount :: Date -> Int
popCount :: Date -> Int
Bits, Date
Date -> Date -> Bounded Date
forall a. a -> a -> Bounded a
$cminBound :: Date
minBound :: Date
$cmaxBound :: Date
maxBound :: Date
Bounded, Int -> Date
Date -> Int
Date -> [Date]
Date -> Date
Date -> Date -> [Date]
Date -> Date -> Date -> [Date]
(Date -> Date)
-> (Date -> Date)
-> (Int -> Date)
-> (Date -> Int)
-> (Date -> [Date])
-> (Date -> Date -> [Date])
-> (Date -> Date -> [Date])
-> (Date -> Date -> Date -> [Date])
-> Enum Date
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
$csucc :: Date -> Date
succ :: Date -> Date
$cpred :: Date -> Date
pred :: Date -> Date
$ctoEnum :: Int -> Date
toEnum :: Int -> Date
$cfromEnum :: Date -> Int
fromEnum :: Date -> Int
$cenumFrom :: Date -> [Date]
enumFrom :: Date -> [Date]
$cenumFromThen :: Date -> Date -> [Date]
enumFromThen :: Date -> Date -> [Date]
$cenumFromTo :: Date -> Date -> [Date]
enumFromTo :: Date -> Date -> [Date]
$cenumFromThenTo :: Date -> Date -> Date -> [Date]
enumFromThenTo :: Date -> Date -> Date -> [Date]
Enum, Date -> ()
(Date -> ()) -> NFData Date
forall a. (a -> ()) -> NFData a
$crnf :: Date -> ()
rnf :: Date -> ()
NFData)

instance IsChType Date where
  type ToChTypeName Date = "Date"
  defaultValueOfTypeName :: Date
defaultValueOfTypeName = UInt16 -> Date
MkChDate UInt16
0

instance ToChType Date Word16 where toChType :: UInt16 -> Date
toChType = UInt16 -> Date
MkChDate

instance FromChType Date Word16 where fromChType :: Date -> UInt16
fromChType = Date -> UInt16
forall a b. Coercible a b => a -> b
coerce




newtype ChArray a = MkChArray [a]
  deriving newtype (Int -> ChArray a -> ShowS
[ChArray a] -> ShowS
ChArray a -> String
(Int -> ChArray a -> ShowS)
-> (ChArray a -> String)
-> ([ChArray a] -> ShowS)
-> Show (ChArray a)
forall a. Show a => Int -> ChArray a -> ShowS
forall a. Show a => [ChArray a] -> ShowS
forall a. Show a => ChArray a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> ChArray a -> ShowS
showsPrec :: Int -> ChArray a -> ShowS
$cshow :: forall a. Show a => ChArray a -> String
show :: ChArray a -> String
$cshowList :: forall a. Show a => [ChArray a] -> ShowS
showList :: [ChArray a] -> ShowS
Show, ChArray a -> ChArray a -> Bool
(ChArray a -> ChArray a -> Bool)
-> (ChArray a -> ChArray a -> Bool) -> Eq (ChArray a)
forall a. Eq a => ChArray a -> ChArray a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => ChArray a -> ChArray a -> Bool
== :: ChArray a -> ChArray a -> Bool
$c/= :: forall a. Eq a => ChArray a -> ChArray a -> Bool
/= :: ChArray a -> ChArray a -> Bool
Eq, ChArray a -> ()
(ChArray a -> ()) -> NFData (ChArray a)
forall a. NFData a => ChArray a -> ()
forall a. (a -> ()) -> NFData a
$crnf :: forall a. NFData a => ChArray a -> ()
rnf :: ChArray a -> ()
NFData)

instance
  KnownSymbol (AppendSymbol (AppendSymbol "Array(" (ToChTypeName chType)) ")")
  =>
  IsChType (ChArray chType)
  where
  type ToChTypeName (ChArray chType) = "Array(" `AppendSymbol` ToChTypeName chType `AppendSymbol` ")"
  defaultValueOfTypeName :: ChArray chType
defaultValueOfTypeName = [chType] -> ChArray chType
forall a. [a] -> ChArray a
MkChArray []




instance FromChType chType inputType => FromChType (ChArray chType) [inputType]
  where
  fromChType :: ChArray chType -> [inputType]
fromChType (MkChArray [chType]
values) = (chType -> inputType) -> [chType] -> [inputType]
forall a b. (a -> b) -> [a] -> [b]
map chType -> inputType
forall chType outputType.
FromChType chType outputType =>
chType -> outputType
fromChType [chType]
values

instance ToChType chType inputType => ToChType (ChArray chType) [inputType]
  where
  toChType :: [inputType] -> ChArray chType
toChType = [chType] -> ChArray chType
forall a. [a] -> ChArray a
MkChArray ([chType] -> ChArray chType)
-> ([inputType] -> [chType]) -> [inputType] -> ChArray chType
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (inputType -> chType) -> [inputType] -> [chType]
forall a b. (a -> b) -> [a] -> [b]
map inputType -> chType
forall chType inputType.
ToChType chType inputType =>
inputType -> chType
toChType




{- |
  Unsigned variable-length quantity encoding
  
  Part of protocol implementation
-}
newtype UVarInt = MkUVarInt Word64
  deriving newtype (Int -> UVarInt -> ShowS
[UVarInt] -> ShowS
UVarInt -> String
(Int -> UVarInt -> ShowS)
-> (UVarInt -> String) -> ([UVarInt] -> ShowS) -> Show UVarInt
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> UVarInt -> ShowS
showsPrec :: Int -> UVarInt -> ShowS
$cshow :: UVarInt -> String
show :: UVarInt -> String
$cshowList :: [UVarInt] -> ShowS
showList :: [UVarInt] -> ShowS
Show, UVarInt -> UVarInt -> Bool
(UVarInt -> UVarInt -> Bool)
-> (UVarInt -> UVarInt -> Bool) -> Eq UVarInt
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: UVarInt -> UVarInt -> Bool
== :: UVarInt -> UVarInt -> Bool
$c/= :: UVarInt -> UVarInt -> Bool
/= :: UVarInt -> UVarInt -> Bool
Eq, Integer -> UVarInt
UVarInt -> UVarInt
UVarInt -> UVarInt -> UVarInt
(UVarInt -> UVarInt -> UVarInt)
-> (UVarInt -> UVarInt -> UVarInt)
-> (UVarInt -> UVarInt -> UVarInt)
-> (UVarInt -> UVarInt)
-> (UVarInt -> UVarInt)
-> (UVarInt -> UVarInt)
-> (Integer -> UVarInt)
-> Num UVarInt
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
$c+ :: UVarInt -> UVarInt -> UVarInt
+ :: UVarInt -> UVarInt -> UVarInt
$c- :: UVarInt -> UVarInt -> UVarInt
- :: UVarInt -> UVarInt -> UVarInt
$c* :: UVarInt -> UVarInt -> UVarInt
* :: UVarInt -> UVarInt -> UVarInt
$cnegate :: UVarInt -> UVarInt
negate :: UVarInt -> UVarInt
$cabs :: UVarInt -> UVarInt
abs :: UVarInt -> UVarInt
$csignum :: UVarInt -> UVarInt
signum :: UVarInt -> UVarInt
$cfromInteger :: Integer -> UVarInt
fromInteger :: Integer -> UVarInt
Num, Eq UVarInt
UVarInt
Eq UVarInt =>
(UVarInt -> UVarInt -> UVarInt)
-> (UVarInt -> UVarInt -> UVarInt)
-> (UVarInt -> UVarInt -> UVarInt)
-> (UVarInt -> UVarInt)
-> (UVarInt -> Int -> UVarInt)
-> (UVarInt -> Int -> UVarInt)
-> UVarInt
-> (Int -> UVarInt)
-> (UVarInt -> Int -> UVarInt)
-> (UVarInt -> Int -> UVarInt)
-> (UVarInt -> Int -> UVarInt)
-> (UVarInt -> Int -> Bool)
-> (UVarInt -> Maybe Int)
-> (UVarInt -> Int)
-> (UVarInt -> Bool)
-> (UVarInt -> Int -> UVarInt)
-> (UVarInt -> Int -> UVarInt)
-> (UVarInt -> Int -> UVarInt)
-> (UVarInt -> Int -> UVarInt)
-> (UVarInt -> Int -> UVarInt)
-> (UVarInt -> Int -> UVarInt)
-> (UVarInt -> Int)
-> Bits UVarInt
Int -> UVarInt
UVarInt -> Bool
UVarInt -> Int
UVarInt -> Maybe Int
UVarInt -> UVarInt
UVarInt -> Int -> Bool
UVarInt -> Int -> UVarInt
UVarInt -> UVarInt -> UVarInt
forall a.
Eq a =>
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> a
-> (Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> Bool)
-> (a -> Maybe Int)
-> (a -> Int)
-> (a -> Bool)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int)
-> Bits a
$c.&. :: UVarInt -> UVarInt -> UVarInt
.&. :: UVarInt -> UVarInt -> UVarInt
$c.|. :: UVarInt -> UVarInt -> UVarInt
.|. :: UVarInt -> UVarInt -> UVarInt
$cxor :: UVarInt -> UVarInt -> UVarInt
xor :: UVarInt -> UVarInt -> UVarInt
$ccomplement :: UVarInt -> UVarInt
complement :: UVarInt -> UVarInt
$cshift :: UVarInt -> Int -> UVarInt
shift :: UVarInt -> Int -> UVarInt
$crotate :: UVarInt -> Int -> UVarInt
rotate :: UVarInt -> Int -> UVarInt
$czeroBits :: UVarInt
zeroBits :: UVarInt
$cbit :: Int -> UVarInt
bit :: Int -> UVarInt
$csetBit :: UVarInt -> Int -> UVarInt
setBit :: UVarInt -> Int -> UVarInt
$cclearBit :: UVarInt -> Int -> UVarInt
clearBit :: UVarInt -> Int -> UVarInt
$ccomplementBit :: UVarInt -> Int -> UVarInt
complementBit :: UVarInt -> Int -> UVarInt
$ctestBit :: UVarInt -> Int -> Bool
testBit :: UVarInt -> Int -> Bool
$cbitSizeMaybe :: UVarInt -> Maybe Int
bitSizeMaybe :: UVarInt -> Maybe Int
$cbitSize :: UVarInt -> Int
bitSize :: UVarInt -> Int
$cisSigned :: UVarInt -> Bool
isSigned :: UVarInt -> Bool
$cshiftL :: UVarInt -> Int -> UVarInt
shiftL :: UVarInt -> Int -> UVarInt
$cunsafeShiftL :: UVarInt -> Int -> UVarInt
unsafeShiftL :: UVarInt -> Int -> UVarInt
$cshiftR :: UVarInt -> Int -> UVarInt
shiftR :: UVarInt -> Int -> UVarInt
$cunsafeShiftR :: UVarInt -> Int -> UVarInt
unsafeShiftR :: UVarInt -> Int -> UVarInt
$crotateL :: UVarInt -> Int -> UVarInt
rotateL :: UVarInt -> Int -> UVarInt
$crotateR :: UVarInt -> Int -> UVarInt
rotateR :: UVarInt -> Int -> UVarInt
$cpopCount :: UVarInt -> Int
popCount :: UVarInt -> Int
Bits, Int -> UVarInt
UVarInt -> Int
UVarInt -> [UVarInt]
UVarInt -> UVarInt
UVarInt -> UVarInt -> [UVarInt]
UVarInt -> UVarInt -> UVarInt -> [UVarInt]
(UVarInt -> UVarInt)
-> (UVarInt -> UVarInt)
-> (Int -> UVarInt)
-> (UVarInt -> Int)
-> (UVarInt -> [UVarInt])
-> (UVarInt -> UVarInt -> [UVarInt])
-> (UVarInt -> UVarInt -> [UVarInt])
-> (UVarInt -> UVarInt -> UVarInt -> [UVarInt])
-> Enum UVarInt
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
$csucc :: UVarInt -> UVarInt
succ :: UVarInt -> UVarInt
$cpred :: UVarInt -> UVarInt
pred :: UVarInt -> UVarInt
$ctoEnum :: Int -> UVarInt
toEnum :: Int -> UVarInt
$cfromEnum :: UVarInt -> Int
fromEnum :: UVarInt -> Int
$cenumFrom :: UVarInt -> [UVarInt]
enumFrom :: UVarInt -> [UVarInt]
$cenumFromThen :: UVarInt -> UVarInt -> [UVarInt]
enumFromThen :: UVarInt -> UVarInt -> [UVarInt]
$cenumFromTo :: UVarInt -> UVarInt -> [UVarInt]
enumFromTo :: UVarInt -> UVarInt -> [UVarInt]
$cenumFromThenTo :: UVarInt -> UVarInt -> UVarInt -> [UVarInt]
enumFromThenTo :: UVarInt -> UVarInt -> UVarInt -> [UVarInt]
Enum, Eq UVarInt
Eq UVarInt =>
(UVarInt -> UVarInt -> Ordering)
-> (UVarInt -> UVarInt -> Bool)
-> (UVarInt -> UVarInt -> Bool)
-> (UVarInt -> UVarInt -> Bool)
-> (UVarInt -> UVarInt -> Bool)
-> (UVarInt -> UVarInt -> UVarInt)
-> (UVarInt -> UVarInt -> UVarInt)
-> Ord UVarInt
UVarInt -> UVarInt -> Bool
UVarInt -> UVarInt -> Ordering
UVarInt -> UVarInt -> UVarInt
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: UVarInt -> UVarInt -> Ordering
compare :: UVarInt -> UVarInt -> Ordering
$c< :: UVarInt -> UVarInt -> Bool
< :: UVarInt -> UVarInt -> Bool
$c<= :: UVarInt -> UVarInt -> Bool
<= :: UVarInt -> UVarInt -> Bool
$c> :: UVarInt -> UVarInt -> Bool
> :: UVarInt -> UVarInt -> Bool
$c>= :: UVarInt -> UVarInt -> Bool
>= :: UVarInt -> UVarInt -> Bool
$cmax :: UVarInt -> UVarInt -> UVarInt
max :: UVarInt -> UVarInt -> UVarInt
$cmin :: UVarInt -> UVarInt -> UVarInt
min :: UVarInt -> UVarInt -> UVarInt
Ord, Num UVarInt
Ord UVarInt
(Num UVarInt, Ord UVarInt) => (UVarInt -> Rational) -> Real UVarInt
UVarInt -> Rational
forall a. (Num a, Ord a) => (a -> Rational) -> Real a
$ctoRational :: UVarInt -> Rational
toRational :: UVarInt -> Rational
Real, Enum UVarInt
Real UVarInt
(Real UVarInt, Enum UVarInt) =>
(UVarInt -> UVarInt -> UVarInt)
-> (UVarInt -> UVarInt -> UVarInt)
-> (UVarInt -> UVarInt -> UVarInt)
-> (UVarInt -> UVarInt -> UVarInt)
-> (UVarInt -> UVarInt -> (UVarInt, UVarInt))
-> (UVarInt -> UVarInt -> (UVarInt, UVarInt))
-> (UVarInt -> Integer)
-> Integral UVarInt
UVarInt -> Integer
UVarInt -> UVarInt -> (UVarInt, UVarInt)
UVarInt -> UVarInt -> UVarInt
forall a.
(Real a, Enum a) =>
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> (a, a))
-> (a -> a -> (a, a))
-> (a -> Integer)
-> Integral a
$cquot :: UVarInt -> UVarInt -> UVarInt
quot :: UVarInt -> UVarInt -> UVarInt
$crem :: UVarInt -> UVarInt -> UVarInt
rem :: UVarInt -> UVarInt -> UVarInt
$cdiv :: UVarInt -> UVarInt -> UVarInt
div :: UVarInt -> UVarInt -> UVarInt
$cmod :: UVarInt -> UVarInt -> UVarInt
mod :: UVarInt -> UVarInt -> UVarInt
$cquotRem :: UVarInt -> UVarInt -> (UVarInt, UVarInt)
quotRem :: UVarInt -> UVarInt -> (UVarInt, UVarInt)
$cdivMod :: UVarInt -> UVarInt -> (UVarInt, UVarInt)
divMod :: UVarInt -> UVarInt -> (UVarInt, UVarInt)
$ctoInteger :: UVarInt -> Integer
toInteger :: UVarInt -> Integer
Integral, UVarInt
UVarInt -> UVarInt -> Bounded UVarInt
forall a. a -> a -> Bounded a
$cminBound :: UVarInt
minBound :: UVarInt
$cmaxBound :: UVarInt
maxBound :: UVarInt
Bounded, UVarInt -> ()
(UVarInt -> ()) -> NFData UVarInt
forall a. (a -> ()) -> NFData a
$crnf :: UVarInt -> ()
rnf :: UVarInt -> ()
NFData)








-- * Versioning

client_version_major, client_version_minor :: UVarInt
client_version_patch :: UVarInt  `SinceRevision` DBMS_MIN_REVISION_WITH_VERSION_PATCH
client_version_major :: UVarInt
client_version_major = case Version -> [Int]
versionBranch Version
version of (Int
x:[Int]
_) -> Int -> UVarInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
x; [Int]
_ -> UVarInt
0
client_version_minor :: UVarInt
client_version_minor = case Version -> [Int]
versionBranch Version
version of (Int
_:Int
x:[Int]
_) -> Int -> UVarInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
x; [Int]
_ -> UVarInt
0
client_version_patch :: SinceRevision UVarInt DBMS_MIN_REVISION_WITH_VERSION_PATCH
client_version_patch = UVarInt
-> SinceRevision UVarInt DBMS_MIN_REVISION_WITH_VERSION_PATCH
forall a (revisionNumber :: Nat).
a -> SinceRevision a revisionNumber
MkSinceRevision (UVarInt
 -> SinceRevision UVarInt DBMS_MIN_REVISION_WITH_VERSION_PATCH)
-> UVarInt
-> SinceRevision UVarInt DBMS_MIN_REVISION_WITH_VERSION_PATCH
forall a b. (a -> b) -> a -> b
$ case Version -> [Int]
versionBranch Version
version of (Int
_:Int
_:Int
x:[Int]
_) -> Int -> UVarInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
x; [Int]
_ -> UVarInt
0

client_name :: ChString
client_name :: ChString
client_name = String -> ChString
forall a. IsString a => String -> a
fromString (String -> ChString) -> String -> ChString
forall a b. (a -> b) -> a -> b
$
  String
"ClickHaskell-"
  String -> ShowS
forall a. Semigroup a => a -> a -> a
<> UVarInt -> String
forall a. Show a => a -> String
show UVarInt
client_version_major String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"."
  String -> ShowS
forall a. Semigroup a => a -> a -> a
<> UVarInt -> String
forall a. Show a => a -> String
show UVarInt
client_version_minor String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"."
  String -> ShowS
forall a. Semigroup a => a -> a -> a
<> SinceRevision UVarInt DBMS_MIN_REVISION_WITH_VERSION_PATCH
-> String
forall a. Show a => a -> String
show SinceRevision UVarInt DBMS_MIN_REVISION_WITH_VERSION_PATCH
client_version_patch

newtype ProtocolRevision = MkProtocolRevision Word64
  deriving newtype (Int -> ProtocolRevision -> ShowS
[ProtocolRevision] -> ShowS
ProtocolRevision -> String
(Int -> ProtocolRevision -> ShowS)
-> (ProtocolRevision -> String)
-> ([ProtocolRevision] -> ShowS)
-> Show ProtocolRevision
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ProtocolRevision -> ShowS
showsPrec :: Int -> ProtocolRevision -> ShowS
$cshow :: ProtocolRevision -> String
show :: ProtocolRevision -> String
$cshowList :: [ProtocolRevision] -> ShowS
showList :: [ProtocolRevision] -> ShowS
Show, ProtocolRevision -> ProtocolRevision -> Bool
(ProtocolRevision -> ProtocolRevision -> Bool)
-> (ProtocolRevision -> ProtocolRevision -> Bool)
-> Eq ProtocolRevision
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ProtocolRevision -> ProtocolRevision -> Bool
== :: ProtocolRevision -> ProtocolRevision -> Bool
$c/= :: ProtocolRevision -> ProtocolRevision -> Bool
/= :: ProtocolRevision -> ProtocolRevision -> Bool
Eq, Integer -> ProtocolRevision
ProtocolRevision -> ProtocolRevision
ProtocolRevision -> ProtocolRevision -> ProtocolRevision
(ProtocolRevision -> ProtocolRevision -> ProtocolRevision)
-> (ProtocolRevision -> ProtocolRevision -> ProtocolRevision)
-> (ProtocolRevision -> ProtocolRevision -> ProtocolRevision)
-> (ProtocolRevision -> ProtocolRevision)
-> (ProtocolRevision -> ProtocolRevision)
-> (ProtocolRevision -> ProtocolRevision)
-> (Integer -> ProtocolRevision)
-> Num ProtocolRevision
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
$c+ :: ProtocolRevision -> ProtocolRevision -> ProtocolRevision
+ :: ProtocolRevision -> ProtocolRevision -> ProtocolRevision
$c- :: ProtocolRevision -> ProtocolRevision -> ProtocolRevision
- :: ProtocolRevision -> ProtocolRevision -> ProtocolRevision
$c* :: ProtocolRevision -> ProtocolRevision -> ProtocolRevision
* :: ProtocolRevision -> ProtocolRevision -> ProtocolRevision
$cnegate :: ProtocolRevision -> ProtocolRevision
negate :: ProtocolRevision -> ProtocolRevision
$cabs :: ProtocolRevision -> ProtocolRevision
abs :: ProtocolRevision -> ProtocolRevision
$csignum :: ProtocolRevision -> ProtocolRevision
signum :: ProtocolRevision -> ProtocolRevision
$cfromInteger :: Integer -> ProtocolRevision
fromInteger :: Integer -> ProtocolRevision
Num, Eq ProtocolRevision
Eq ProtocolRevision =>
(ProtocolRevision -> ProtocolRevision -> Ordering)
-> (ProtocolRevision -> ProtocolRevision -> Bool)
-> (ProtocolRevision -> ProtocolRevision -> Bool)
-> (ProtocolRevision -> ProtocolRevision -> Bool)
-> (ProtocolRevision -> ProtocolRevision -> Bool)
-> (ProtocolRevision -> ProtocolRevision -> ProtocolRevision)
-> (ProtocolRevision -> ProtocolRevision -> ProtocolRevision)
-> Ord ProtocolRevision
ProtocolRevision -> ProtocolRevision -> Bool
ProtocolRevision -> ProtocolRevision -> Ordering
ProtocolRevision -> ProtocolRevision -> ProtocolRevision
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: ProtocolRevision -> ProtocolRevision -> Ordering
compare :: ProtocolRevision -> ProtocolRevision -> Ordering
$c< :: ProtocolRevision -> ProtocolRevision -> Bool
< :: ProtocolRevision -> ProtocolRevision -> Bool
$c<= :: ProtocolRevision -> ProtocolRevision -> Bool
<= :: ProtocolRevision -> ProtocolRevision -> Bool
$c> :: ProtocolRevision -> ProtocolRevision -> Bool
> :: ProtocolRevision -> ProtocolRevision -> Bool
$c>= :: ProtocolRevision -> ProtocolRevision -> Bool
>= :: ProtocolRevision -> ProtocolRevision -> Bool
$cmax :: ProtocolRevision -> ProtocolRevision -> ProtocolRevision
max :: ProtocolRevision -> ProtocolRevision -> ProtocolRevision
$cmin :: ProtocolRevision -> ProtocolRevision -> ProtocolRevision
min :: ProtocolRevision -> ProtocolRevision -> ProtocolRevision
Ord)

instance Deserializable ProtocolRevision where deserialize :: ProtocolRevision -> Get ProtocolRevision
deserialize = Get UVarInt -> Get ProtocolRevision
forall a b. Coercible a b => a -> b
coerce (Get UVarInt -> Get ProtocolRevision)
-> (ProtocolRevision -> Get UVarInt)
-> ProtocolRevision
-> Get ProtocolRevision
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize @UVarInt
instance Serializable ProtocolRevision where serialize :: ProtocolRevision -> ProtocolRevision -> Builder
serialize ProtocolRevision
rev = forall chType.
Serializable chType =>
ProtocolRevision -> chType -> Builder
serialize @UVarInt ProtocolRevision
rev (UVarInt -> Builder)
-> (ProtocolRevision -> UVarInt) -> ProtocolRevision -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ProtocolRevision -> UVarInt
forall a b. Coercible a b => a -> b
coerce

{-# INLINE [0] afterRevision #-}
afterRevision
  :: forall revision monoid
  .  (KnownNat revision, Monoid monoid)
  => ProtocolRevision -> monoid -> monoid
afterRevision :: forall (revision :: Nat) monoid.
(KnownNat revision, Monoid monoid) =>
ProtocolRevision -> monoid -> monoid
afterRevision ProtocolRevision
chosenRevision monoid
monoid =
  if ProtocolRevision
chosenRevision ProtocolRevision -> ProtocolRevision -> Bool
forall a. Ord a => a -> a -> Bool
>= (Integer -> ProtocolRevision
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Integer -> ProtocolRevision)
-> (Proxy revision -> Integer)
-> Proxy revision
-> ProtocolRevision
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Proxy revision -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal) (forall (t :: Nat). Proxy t
forall {k} (t :: k). Proxy t
Proxy @revision)
  then monoid
monoid
  else monoid
forall a. Monoid a => a
mempty

latestSupportedRevision :: ProtocolRevision
latestSupportedRevision :: ProtocolRevision
latestSupportedRevision = (Integer -> ProtocolRevision
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Integer -> ProtocolRevision)
-> (Proxy DBMS_TCP_PROTOCOL_VERSION -> Integer)
-> Proxy DBMS_TCP_PROTOCOL_VERSION
-> ProtocolRevision
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Proxy DBMS_TCP_PROTOCOL_VERSION -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal) (forall (t :: Nat). Proxy t
forall {k} (t :: k). Proxy t
Proxy @DBMS_TCP_PROTOCOL_VERSION)

data SinceRevision a (revisionNumber :: Nat) = MkSinceRevision a | NotPresented
instance Show a => Show (SinceRevision a revisionNumber) where
  show :: SinceRevision a revisionNumber -> String
show (MkSinceRevision a
a) = a -> String
forall a. Show a => a -> String
show a
a
  show SinceRevision a revisionNumber
NotPresented = String
""

instance
  (KnownNat revision, Deserializable chType)
  =>
  Deserializable (SinceRevision chType revision)
  where
  deserialize :: ProtocolRevision -> Get (SinceRevision chType revision)
deserialize ProtocolRevision
rev =
    if ProtocolRevision
rev ProtocolRevision -> ProtocolRevision -> Bool
forall a. Ord a => a -> a -> Bool
>= (Integer -> ProtocolRevision
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Integer -> ProtocolRevision)
-> (Proxy revision -> Integer)
-> Proxy revision
-> ProtocolRevision
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Proxy revision -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal) (forall (t :: Nat). Proxy t
forall {k} (t :: k). Proxy t
Proxy @revision)
    then chType -> SinceRevision chType revision
forall a (revisionNumber :: Nat).
a -> SinceRevision a revisionNumber
MkSinceRevision (chType -> SinceRevision chType revision)
-> Get chType -> Get (SinceRevision chType revision)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize @chType ProtocolRevision
rev
    else SinceRevision chType revision
-> Get (SinceRevision chType revision)
forall a. a -> Get a
forall (f :: * -> *) a. Applicative f => a -> f a
pure SinceRevision chType revision
forall a (revisionNumber :: Nat). SinceRevision a revisionNumber
NotPresented

instance
  (KnownNat revision, Serializable chType)
  =>
  Serializable (SinceRevision chType revision)
  where
  serialize :: ProtocolRevision -> SinceRevision chType revision -> Builder
serialize ProtocolRevision
rev (MkSinceRevision chType
val) = forall (revision :: Nat) monoid.
(KnownNat revision, Monoid monoid) =>
ProtocolRevision -> monoid -> monoid
afterRevision @revision ProtocolRevision
rev (ProtocolRevision -> chType -> Builder
forall chType.
Serializable chType =>
ProtocolRevision -> chType -> Builder
serialize ProtocolRevision
rev chType
val)
  serialize ProtocolRevision
rev SinceRevision chType revision
NotPresented          = forall (revision :: Nat) monoid.
(KnownNat revision, Monoid monoid) =>
ProtocolRevision -> monoid -> monoid
afterRevision @revision ProtocolRevision
rev (String -> Builder
forall a. HasCallStack => String -> a
error String
"Unexpected error")


{-
  Slightly modified C++ sources:
  https://github.com/ClickHouse/ClickHouse/blob/eb4a74d7412a1fcf52727cd8b00b365d6b9ed86c/src/Core/ProtocolDefines.h#L6
-}
type DBMS_TCP_PROTOCOL_VERSION = 54448;

type DBMS_MIN_REVISION_WITH_CLIENT_INFO = 54032;
type DBMS_MIN_REVISION_WITH_SERVER_TIMEZONE = 54058;
type DBMS_MIN_REVISION_WITH_QUOTA_KEY_IN_CLIENT_INFO = 54060;
--type DBMS_MIN_REVISION_WITH_TABLES_STATUS = 54226;
--type DBMS_MIN_REVISION_WITH_TIME_ZONE_PARAMETER_IN_DATETIME_DATA_TYPE = 54337;
type DBMS_MIN_REVISION_WITH_SERVER_DISPLAY_NAME = 54372;
type DBMS_MIN_REVISION_WITH_VERSION_PATCH = 54401;
--type DBMS_MIN_REVISION_WITH_SERVER_LOGS = 54406;
--type DBMS_MIN_REVISION_WITH_CURRENT_AGGREGATION_VARIANT_SELECTION_METHOD = 54448;
--type DBMS_MIN_MAJOR_VERSION_WITH_CURRENT_AGGREGATION_VARIANT_SELECTION_METHOD = 21;
--type DBMS_MIN_MINOR_VERSION_WITH_CURRENT_AGGREGATION_VARIANT_SELECTION_METHOD = 4;
--type DBMS_MIN_REVISION_WITH_COLUMN_DEFAULTS_METADATA = 54410;
--type DBMS_MIN_REVISION_WITH_LOW_CARDINALITY_TYPE = 54405;
type DBMS_MIN_REVISION_WITH_CLIENT_WRITE_INFO = 54420;
--type DBMS_MIN_REVISION_WITH_SETTINGS_SERIALIZED_AS_STRINGS = 54429;
--type DBMS_MIN_REVISION_WITH_SCALARS = 54429;
type DBMS_MIN_REVISION_WITH_OPENTELEMETRY = 54442;
--type DBMS_MIN_REVISION_WITH_AGGREGATE_FUNCTIONS_VERSIONING = 54452;
--type DBMS_CLUSTER_PROCESSING_PROTOCOL_VERSION = 1;
--type DBMS_MIN_SUPPORTED_PARALLEL_REPLICAS_PROTOCOL_VERSION = 3;
--type DBMS_PARALLEL_REPLICAS_MIN_VERSION_WITH_MARK_SEGMENT_SIZE_FIELD = 4;
--type DBMS_PARALLEL_REPLICAS_PROTOCOL_VERSION = 4;
type DBMS_MIN_REVISION_WITH_PARALLEL_REPLICAS = 54453;
--type DBMS_MERGE_TREE_PART_INFO_VERSION = 1;
type DBMS_MIN_REVISION_WITH_INTERSERVER_SECRET = 54441;
--type DBMS_MIN_REVISION_WITH_X_FORWARDED_FOR_IN_CLIENT_INFO = 54443;
--type DBMS_MIN_REVISION_WITH_REFERER_IN_CLIENT_INFO = 54447;
type DBMS_MIN_PROTOCOL_VERSION_WITH_DISTRIBUTED_DEPTH = 54448;
--type DBMS_MIN_PROTOCOL_VERSION_WITH_INCREMENTAL_PROFILE_EVENTS = 54451;
type DBMS_MIN_REVISION_WITH_CUSTOM_SERIALIZATION = 54454;
type DBMS_MIN_PROTOCOL_VERSION_WITH_INITIAL_QUERY_START_TIME = 54449;
--type DBMS_MIN_PROTOCOL_VERSION_WITH_PROFILE_EVENTS_IN_INSERT = 54456;
--type DBMS_MIN_PROTOCOL_VERSION_WITH_VIEW_IF_PERMITTED = 54457;
--type DBMS_MIN_PROTOCOL_VERSION_WITH_ADDENDUM = 54458;
type DBMS_MIN_PROTOCOL_VERSION_WITH_QUOTA_KEY = 54458;
type DBMS_MIN_PROTOCOL_VERSION_WITH_PARAMETERS = 54459;
--type DBMS_MIN_PROTOCOL_VERSION_WITH_SERVER_QUERY_TIME_IN_PROGRESS = 54460;
type DBMS_MIN_PROTOCOL_VERSION_WITH_PASSWORD_COMPLEXITY_RULES = 54461;
type DBMS_MIN_REVISION_WITH_INTERSERVER_SECRET_V2 = 54462;
type DBMS_MIN_PROTOCOL_VERSION_WITH_TOTAL_BYTES_IN_PROGRESS = 54463;
--type DBMS_MIN_PROTOCOL_VERSION_WITH_TIMEZONE_UPDATES = 54464;
--type DBMS_MIN_REVISION_WITH_SPARSE_SERIALIZATION = 54465;
--type DBMS_MIN_REVISION_WITH_SSH_AUTHENTICATION = 54466;
--type DBMS_MIN_REVISION_WITH_TABLE_READ_ONLY_CHECK = 54467;
--type DBMS_MIN_REVISION_WITH_SYSTEM_KEYWORDS_TABLE = 54468;
type DBMS_MIN_REVISION_WITH_ROWS_BEFORE_AGGREGATION = 54469;
type DBMS_MIN_PROTOCOL_VERSION_WITH_CHUNKED_PACKETS = 54470;
type DBMS_MIN_REVISION_WITH_VERSIONED_PARALLEL_REPLICAS_PROTOCOL = 54471;