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

{-# OPTIONS_GHC
  -Wno-orphans
  -Wno-unused-top-binds
#-}

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

  -- * Arbitrary commands
  , command

  -- * 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
  , UUID(..)
  , Array(..)
  , ChString(..)

  , 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 (ByteString, length, take)
import Data.ByteString.Builder
import Data.ByteString.Builder as BS (Builder, byteString)
import Data.ByteString.Lazy as BSL (toStrict)
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 hiding (SocketOption(..))
import Network.Socket qualified as Sock (SocketOption(..))
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
Sock.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
$ UVarInt -> InternalError
UnexpectedPacketType (UVarInt -> InternalError) -> UVarInt -> InternalError
forall a b. (a -> b) -> a -> b
$ ServerPacketType -> UVarInt
serverPacketToNum ServerPacketType
otherPacket)


{- |
  Arbitrary commands wrapper

  For example: `CREATE`
-}
command :: HasCallStack => Connection -> ChString -> IO ()
command :: HasCallStack => Connection -> ChString -> IO ()
command Connection
conn ChString
query = 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)
    ConnectionState -> IO ()
handleCreate ConnectionState
connState
  where
  handleCreate :: ConnectionState -> IO ()
  handleCreate :: ConnectionState -> IO ()
handleCreate 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
..} =
    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 ()) -> IO ()
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
      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
$ UVarInt -> InternalError
UnexpectedPacketType (UVarInt -> InternalError) -> UVarInt -> InternalError
forall a b. (a -> b) -> a -> b
$ ServerPacketType -> UVarInt
serverPacketToNum 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
$ UVarInt -> InternalError
UnexpectedPacketType (UVarInt -> InternalError) -> UVarInt -> InternalError
forall a b. (a -> b) -> a -> b
$ ServerPacketType -> UVarInt
serverPacketToNum 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
<> (ByteString -> Builder
byteString (ByteString -> Builder)
-> (String -> ByteString) -> String -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ByteString
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
<> (ByteString -> Builder
byteString (ByteString -> Builder)
-> (Proxy name -> ByteString) -> Proxy name -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ByteString
BS8.pack (String -> ByteString)
-> (Proxy name -> String) -> Proxy name -> ByteString
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
$ UVarInt -> InternalError
UnexpectedPacketType (UVarInt -> InternalError) -> UVarInt -> InternalError
forall a b. (a -> b) -> a -> b
$ ServerPacketType -> UVarInt
serverPacketToNum 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
<> (ByteString -> Builder
byteString (ByteString -> Builder)
-> (Proxy name -> ByteString) -> Proxy name -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ByteString
BS8.pack (String -> ByteString)
-> (Proxy name -> String) -> Proxy name -> ByteString
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
$ UVarInt -> InternalError
UnexpectedPacketType (UVarInt -> InternalError) -> UVarInt -> InternalError
forall a b. (a -> b) -> a -> b
$ ServerPacketType -> UVarInt
serverPacketToNum ServerPacketType
otherPacket)




-- * Bufferization

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

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

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

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

writeToBuffer :: Buffer -> BS.ByteString -> IO ()
writeToBuffer :: Buffer -> ByteString -> IO ()
writeToBuffer MkBuffer{Int
IORef ByteString
Socket
$sel:bufferSize:MkBuffer :: Buffer -> Int
$sel:bufferSocket:MkBuffer :: Buffer -> Socket
$sel:buff:MkBuffer :: Buffer -> IORef ByteString
bufferSize :: Int
bufferSocket :: Socket
buff :: IORef ByteString
..} ByteString
val = IO ByteString -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IORef ByteString
-> (ByteString -> (ByteString, ByteString)) -> IO ByteString
forall a b. IORef a -> (a -> (a, b)) -> IO b
atomicModifyIORef IORef ByteString
buff (ByteString
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 ByteString -> Decoder packet
decoder) -> Buffer -> IO ByteString
readBuffer Buffer
buffer IO ByteString -> (ByteString -> 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)
-> (ByteString -> Decoder packet) -> ByteString -> IO packet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe ByteString -> Decoder packet
decoder (Maybe ByteString -> Decoder packet)
-> (ByteString -> Maybe ByteString) -> ByteString -> Decoder packet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just
  (Done ByteString
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 -> ByteString -> IO ()
writeToBuffer Buffer
buffer ByteString
leftover
  (Fail ByteString
_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 UVarInt
  | 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)

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 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

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 :: Natural) (proxy :: Natural -> *).
KnownNat n =>
proxy n -> Integer
natVal (Proxy (PacketTypeNumber packetType) -> UVarInt)
-> Proxy (PacketTypeNumber packetType) -> UVarInt
forall a b. (a -> b) -> a -> b
$ forall (t :: Natural). 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
    , $sel:client_name:MkHelloPacket :: ChString
client_name          = ChString
clientName
    , $sel:client_version_major:MkHelloPacket :: UVarInt
client_version_major = UVarInt
major
    , $sel:client_version_minor:MkHelloPacket :: UVarInt
client_version_minor = UVarInt
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 :: Natural).
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 :: Natural).
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 :: Natural).
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
    , $sel:client_name:MkClientInfo :: ChString
client_name                  = ChString
clientName
    , $sel:client_version_major:MkClientInfo :: UVarInt
client_version_major         = UVarInt
major
    , $sel:client_version_minor:MkClientInfo :: UVarInt
client_version_minor         = UVarInt
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 :: Natural).
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 :: Natural).
a -> SinceRevision a revisionNumber
MkSinceRevision UVarInt
0
    , $sel:client_version_patch:MkClientInfo :: SinceRevision UVarInt DBMS_MIN_REVISION_WITH_VERSION_PATCH
client_version_patch         = UVarInt
-> SinceRevision UVarInt DBMS_MIN_REVISION_WITH_VERSION_PATCH
forall a (revisionNumber :: Natural).
a -> SinceRevision a revisionNumber
MkSinceRevision UVarInt
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 :: Natural).
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 :: Natural).
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 :: Natural).
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 :: Natural).
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 :: Natural).
a -> SinceRevision a revisionNumber
MkSinceRevision ChString
""
  , $sel:query_stage:MkQueryPacket :: QueryStage
query_stage        = QueryStage
Complete
  , $sel:compression:MkQueryPacket :: UVarInt
compression        = UVarInt
0
  , ChString
query :: ChString
$sel:query:MkQueryPacket :: 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 :: Natural).
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
. Int -> UVarInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> UVarInt) -> (QueryStage -> Int) -> QueryStage -> UVarInt
forall b c a. (b -> c) -> (a -> b) -> a -> c
. QueryStage -> Int
forall a. Enum a => a -> Int
fromEnum


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
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
. (\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)

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)




-- * 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

serverPacketToNum :: ServerPacketType -> UVarInt
serverPacketToNum :: ServerPacketType -> UVarInt
serverPacketToNum = \case
  (HelloResponse HelloResponse
_) -> UVarInt
0; (DataResponse DataPacket
_)       -> UVarInt
1
  (Exception ExceptionPacket
_)     -> UVarInt
2; (Progress ProgressPacket
_)           -> UVarInt
3;
  (ServerPacketType
Pong)            -> UVarInt
4; (ServerPacketType
EndOfStream)          -> UVarInt
5
  (ProfileInfo ProfileInfo
_)   -> UVarInt
6; (ServerPacketType
Totals)               -> UVarInt
7
  (ServerPacketType
Extremes)        -> UVarInt
8; (ServerPacketType
TablesStatusResponse) -> UVarInt
9
  (ServerPacketType
Log)             -> UVarInt
10; (TableColumns TableColumns
_)      -> UVarInt
11;
  (ServerPacketType
UUIDs)           -> UVarInt
12; (ServerPacketType
ReadTaskRequest)     -> UVarInt
13
  (ServerPacketType
ProfileEvents)   -> UVarInt
14; (UnknownPacket UVarInt
num)   -> UVarInt
num


-- ** 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, ProtocolRevision -> Get HelloResponse
(ProtocolRevision -> Get HelloResponse)
-> Deserializable HelloResponse
forall chType.
(ProtocolRevision -> Get chType) -> Deserializable chType
$cdeserialize :: ProtocolRevision -> Get HelloResponse
deserialize :: ProtocolRevision -> Get HelloResponse
Deserializable)

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)

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, 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, ProtocolRevision -> Get ExceptionPacket
(ProtocolRevision -> Get ExceptionPacket)
-> Deserializable ExceptionPacket
forall chType.
(ProtocolRevision -> Get chType) -> Deserializable chType
$cdeserialize :: ProtocolRevision -> Get ExceptionPacket
deserialize :: ProtocolRevision -> Get ExceptionPacket
Deserializable)

-- ** 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)

-- ** 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)

-- ** 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)








-- * 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
  , 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 (Array chType))
  , Deserializable chType
  , TypeError ('Text "Arrays deserialization still unsupported")
  )
  => DeserializableColumn (Column name (Array chType)) where
  {-# INLINE deserializeColumn #-}
  deserializeColumn :: ProtocolRevision
-> Bool -> UVarInt -> Get (Column name (Array chType))
deserializeColumn ProtocolRevision
rev Bool
isCheckRequired UVarInt
_rows = do
    forall column.
KnownColumn column =>
ProtocolRevision -> Bool -> Get ()
handleColumnHeader @(Column name (Array 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 (Array chType)))
  (GetColumnType (Column name (Array chType)))
-> Get
     (Column
        (GetColumnName (Column name (Array chType)))
        (GetColumnType (Column name (Array chType))))
forall a. a -> Get a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Column
   (GetColumnName (Column name (Array chType)))
   (GetColumnType (Column name (Array chType)))
 -> Get
      (Column
         (GetColumnName (Column name (Array chType)))
         (GetColumnType (Column name (Array chType)))))
-> Column
     (GetColumnName (Column name (Array chType)))
     (GetColumnType (Column name (Array chType)))
-> Get
     (Column
        (GetColumnName (Column name (Array chType)))
        (GetColumnType (Column name (Array chType))))
forall a b. (a -> b) -> a -> b
$ forall column.
KnownColumn column =>
[GetColumnType column]
-> Column (GetColumnName column) (GetColumnType column)
mkColumn @(Column name (Array 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


-- ** Generics

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

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 {-# OVERLAPPING #-}
  (GDeserializable right)
  =>
  (GDeserializable (S1 metaSel (Rec0 ProtocolRevision) :*: right))
  where
  {-# INLINE gDeserialize #-}
  gDeserialize :: forall p.
ProtocolRevision
-> Get ((:*:) (S1 metaSel (Rec0 ProtocolRevision)) right p)
gDeserialize ProtocolRevision
rev = do
    ProtocolRevision
chosenRev <- ProtocolRevision -> ProtocolRevision -> ProtocolRevision
forall a. Ord a => a -> a -> a
min ProtocolRevision
rev (ProtocolRevision -> ProtocolRevision)
-> (UVarInt -> ProtocolRevision) -> UVarInt -> ProtocolRevision
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UVarInt -> ProtocolRevision
forall a b. Coercible a b => a -> b
coerce (UVarInt -> ProtocolRevision)
-> Get UVarInt -> Get ProtocolRevision
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall chType.
Deserializable chType =>
ProtocolRevision -> Get chType
deserialize @UVarInt ProtocolRevision
rev
    S1 metaSel (Rec0 ProtocolRevision) p
-> right p -> (:*:) (S1 metaSel (Rec0 ProtocolRevision)) right p
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
(:*:) (Rec0 ProtocolRevision p -> S1 metaSel (Rec0 ProtocolRevision) p
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (Rec0 ProtocolRevision p -> S1 metaSel (Rec0 ProtocolRevision) p)
-> Rec0 ProtocolRevision p -> S1 metaSel (Rec0 ProtocolRevision) p
forall a b. (a -> b) -> a -> b
$ ProtocolRevision -> Rec0 ProtocolRevision p
forall k i c (p :: k). c -> K1 i c p
K1 ProtocolRevision
chosenRev) (right p -> (:*:) (S1 metaSel (Rec0 ProtocolRevision)) right p)
-> Get (right p)
-> Get ((:*:) (S1 metaSel (Rec0 ProtocolRevision)) right p)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (f :: * -> *) p.
GDeserializable f =>
ProtocolRevision -> Get (f p)
gDeserialize @right ProtocolRevision
chosenRev

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 UUID where deserialize :: ProtocolRevision -> Get UUID
deserialize ProtocolRevision
_ = UInt128 -> UUID
MkChUUID (UInt128 -> UUID) -> Get UInt128 -> Get UUID
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 -> ByteString -> ChString
forall chType inputType.
ToChType chType inputType =>
inputType -> chType
toChType (ByteString -> ChString) -> Get ByteString -> Get ChString
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> (ByteString -> ByteString) -> Get ByteString
forall a. Int -> (ByteString -> a) -> Get a
readN Int
n (Int -> ByteString -> ByteString
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"

-- ** FromChType

class FromChType chType outputType where fromChType  :: chType -> outputType

instance FromChType UUID (Word64, Word64) where fromChType :: UUID -> (UInt64, UInt64)
fromChType (MkChUUID (Word128 UInt64
w64hi UInt64
w64lo)) = (UInt64
w64hi, UInt64
w64lo)
instance {-# OVERLAPPABLE #-} (IsChType chType, chType ~ inputType) => FromChType chType inputType where fromChType :: chType -> inputType
fromChType = chType -> chType
chType -> inputType
forall a. a -> a
id
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)
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)
instance FromChType chType (LowCardinality chType) where
  fromChType :: chType -> LowCardinality chType
fromChType = chType -> LowCardinality chType
forall chType. chType -> LowCardinality chType
MkLowCardinality
instance FromChType Date Word16 where fromChType :: Date -> UInt16
fromChType = Date -> UInt16
forall a b. Coercible a b => a -> b
coerce
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
instance FromChType ChString BS.ByteString where fromChType :: ChString -> ByteString
fromChType (MkChString ByteString
string) = ByteString
string
instance FromChType ChString Builder where fromChType :: ChString -> Builder
fromChType (MkChString ByteString
string) = ByteString -> Builder
byteString ByteString
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"
instance FromChType chType inputType => FromChType (Array chType) [inputType]
  where
  fromChType :: Array 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








-- * 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;   Int8Column   :: [Int8]   -> Column name Int8
  UInt16Column  :: [UInt16]  -> Column name UInt16;  Int16Column  :: [Int16]  -> Column name Int16
  UInt32Column  :: [UInt32]  -> Column name UInt32;  Int32Column  :: [Int32]  -> Column name Int32
  UInt64Column  :: [UInt64]  -> Column name UInt64;  Int64Column  :: [Int64]  -> Column name Int64
  UInt128Column :: [UInt128] -> Column name UInt128; Int128Column :: [Int128] -> Column name Int128
  DateTimeColumn :: [DateTime tz] -> Column name (DateTime tz)
  DateColumn :: [Date] -> Column name Date
  UUIDColumn :: [UUID] -> Column name UUID
  StringColumn :: [ChString] -> Column name ChString
  ArrayColumn :: [Array chType] -> Column name (Array 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

{-# 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 [UUID]
values) -> [chType]
[UUID]
values; (StringColumn [ChString]
values) -> [chType]
[ChString]
values
  (ArrayColumn [Array chType]
values) -> [chType]
[Array chType]
values; (NullableColumn [Maybe chType]
values) ->  [chType]
[Maybe chType]
values
  (LowCardinalityColumn [chType]
values) -> (chType -> chType) -> [chType] -> [chType]
forall a b. (a -> b) -> [a] -> [b]
map chType -> chType
forall chType outputType.
FromChType chType outputType =>
chType -> outputType
fromChType [chType]
values

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)

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 UUID) where mkColumn :: [GetColumnType (Column name UUID)]
-> Column
     (GetColumnName (Column name UUID))
     (GetColumnType (Column name UUID))
mkColumn = [UUID] -> Column name UUID
[GetColumnType (Column name UUID)]
-> Column
     (GetColumnName (Column name UUID))
     (GetColumnType (Column name UUID))
forall (name :: Symbol). [UUID] -> Column name UUID
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 (Array ChString)) where mkColumn :: [GetColumnType (Column name (Array ChString))]
-> Column
     (GetColumnName (Column name (Array ChString)))
     (GetColumnType (Column name (Array ChString)))
mkColumn = [Array ChString] -> Column name (Array ChString)
[GetColumnType (Column name (Array ChString))]
-> Column
     (GetColumnName (Column name (Array ChString)))
     (GetColumnType (Column name (Array ChString)))
forall name (name :: Symbol).
[Array name] -> Column name (Array 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 :: Natural) 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 :: Natural) 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 :: Natural) 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) = (ByteString -> Builder
byteString (ByteString -> Builder)
-> (Proxy name -> ByteString) -> Proxy name -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ByteString
BS8.pack (String -> ByteString)
-> (Proxy name -> String) -> Proxy name -> ByteString
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 Int8 where toQueryPart :: Int8 -> Builder
toQueryPart = ByteString -> Builder
BS.byteString (ByteString -> Builder) -> (Int8 -> ByteString) -> Int8 -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ByteString
BS8.pack (String -> ByteString) -> (Int8 -> String) -> Int8 -> ByteString
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 = ByteString -> Builder
BS.byteString (ByteString -> Builder)
-> (Int16 -> ByteString) -> Int16 -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ByteString
BS8.pack (String -> ByteString) -> (Int16 -> String) -> Int16 -> ByteString
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 = ByteString -> Builder
BS.byteString (ByteString -> Builder)
-> (Int32 -> ByteString) -> Int32 -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ByteString
BS8.pack (String -> ByteString) -> (Int32 -> String) -> Int32 -> ByteString
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 = ByteString -> Builder
BS.byteString (ByteString -> Builder)
-> (Int64 -> ByteString) -> Int64 -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ByteString
BS8.pack (String -> ByteString) -> (Int64 -> String) -> Int64 -> ByteString
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 = ByteString -> Builder
BS.byteString (ByteString -> Builder)
-> (Int128 -> ByteString) -> Int128 -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ByteString
BS8.pack (String -> ByteString)
-> (Int128 -> String) -> Int128 -> ByteString
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 = ByteString -> Builder
BS.byteString (ByteString -> Builder)
-> (UInt8 -> ByteString) -> UInt8 -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ByteString
BS8.pack (String -> ByteString) -> (UInt8 -> String) -> UInt8 -> ByteString
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 = ByteString -> Builder
BS.byteString (ByteString -> Builder)
-> (UInt16 -> ByteString) -> UInt16 -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ByteString
BS8.pack (String -> ByteString)
-> (UInt16 -> String) -> UInt16 -> ByteString
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 = ByteString -> Builder
BS.byteString (ByteString -> Builder)
-> (UInt32 -> ByteString) -> UInt32 -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ByteString
BS8.pack (String -> ByteString)
-> (UInt32 -> String) -> UInt32 -> ByteString
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 = ByteString -> Builder
BS.byteString (ByteString -> Builder)
-> (UInt64 -> ByteString) -> UInt64 -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ByteString
BS8.pack (String -> ByteString)
-> (UInt64 -> String) -> UInt64 -> ByteString
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 = ByteString -> Builder
BS.byteString (ByteString -> Builder)
-> (UInt128 -> ByteString) -> UInt128 -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ByteString
BS8.pack (String -> ByteString)
-> (UInt128 -> String) -> UInt128 -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UInt128 -> String
forall a. Show a => a -> String
show
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 UUID where
  toQueryPart :: UUID -> 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 ByteString
string) =  Builder
"'" Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
escapeQuery ByteString
string Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
"'"
    where
    escapeQuery :: BS.ByteString -> Builder
    escapeQuery :: ByteString -> Builder
escapeQuery -- [ClickHaskell.DbTypes.ToDo.1]: Optimize
      = ByteString -> Builder
BS.byteString (ByteString -> Builder)
-> (ByteString -> ByteString) -> ByteString -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> ByteString) -> ByteString -> ByteString
BS8.concatMap (\case Char
'\'' -> ByteString
"\\\'"; Char
'\\' -> ByteString
"\\\\"; Char
sym -> Char -> ByteString
BS8.singleton Char
sym;)
instance ToQueryPart (DateTime tz)
  where
  toQueryPart :: DateTime tz -> Builder
toQueryPart DateTime tz
chDateTime = let time :: ByteString
time = String -> ByteString
BS8.pack (String -> ByteString)
-> (DateTime tz -> String) -> DateTime tz -> ByteString
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 -> ByteString) -> DateTime tz -> ByteString
forall a b. (a -> b) -> a -> b
$ DateTime tz
chDateTime
    in ByteString -> Builder
BS.byteString (Int -> Char -> ByteString
BS8.replicate (Int
10 Int -> Int -> Int
forall a. Num a => a -> a -> a
- ByteString -> Int
BS8.length ByteString
time) Char
'0' ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
time)
instance (IsChType chType, ToQueryPart chType) => ToQueryPart (Array chType)
  where
  toQueryPart :: Array 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)
-> (Array chType -> Builder) -> Array 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)
-> (Array chType -> [chType]) -> Array chType -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall chType outputType.
FromChType chType outputType =>
chType -> outputType
fromChType @(Array 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 [(:*:) (S1 ('MetaSel ('Just name) a b f) rec) right p]
xs
    = let ([S1 ('MetaSel ('Just name) a b f) rec p]
ls, [right p]
rs) = ((:*:) (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 p], [right p]))
-> ([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])
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\(S1 ('MetaSel ('Just name) a b f) rec p
l :*: right p
r) ([S1 ('MetaSel ('Just name) a b f) rec p]
accL, [right p]
accR) -> (S1 ('MetaSel ('Just name) a b f) rec p
lS1 ('MetaSel ('Just name) a b f) rec p
-> [S1 ('MetaSel ('Just name) a b f) rec p]
-> [S1 ('MetaSel ('Just name) a b f) rec p]
forall a. a -> [a] -> [a]
:[S1 ('MetaSel ('Just name) a b f) rec p]
accL, right p
rright p -> [right p] -> [right p]
forall a. a -> [a] -> [a]
:[right p]
accR)) ([], []) [(:*:) (S1 ('MetaSel ('Just name) a b f) rec) right p]
xs
      in forall (columns :: [*]) (f :: * -> *) p.
GWritable columns f =>
ProtocolRevision -> [f p] -> Builder
gSerializeRecords @'[Column name chType] ProtocolRevision
rev [S1 ('MetaSel ('Just name) a b f) rec p]
ls 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]
rs
  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

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
. ByteString -> Int
BS.length (ByteString -> Int) -> (ChString -> ByteString) -> ChString -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ChString -> ByteString
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 UUID where serialize :: ProtocolRevision -> UUID -> 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)
-> (UUID -> (UInt64, UInt64)) -> UUID -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UUID -> (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

-- ** ToChType

class ToChType chType inputType    where toChType    :: inputType -> chType

instance {-# OVERLAPPABLE #-} (IsChType chType, chType ~ inputType) => ToChType chType inputType where toChType :: inputType -> chType
toChType = inputType -> chType
inputType -> 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
instance ToChType ChString BS.ByteString where toChType :: ByteString -> ChString
toChType = ByteString -> ChString
MkChString
instance ToChType ChString Builder       where toChType :: Builder -> ChString
toChType = ByteString -> ChString
MkChString (ByteString -> ChString)
-> (Builder -> ByteString) -> Builder -> ChString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
toStrict (ByteString -> ByteString)
-> (Builder -> ByteString) -> Builder -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> ByteString
toLazyByteString
instance ToChType ChString String        where toChType :: String -> ChString
toChType = ByteString -> ChString
MkChString (ByteString -> ChString)
-> (String -> ByteString) -> String -> ChString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ByteString
BS8.pack
instance ToChType ChString Text          where toChType :: Text -> ChString
toChType = ByteString -> ChString
MkChString (ByteString -> ChString)
-> (Text -> ByteString) -> Text -> ChString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
Text.encodeUtf8
instance ToChType ChString Int           where toChType :: Int -> ChString
toChType = ByteString -> ChString
MkChString (ByteString -> ChString) -> (Int -> ByteString) -> Int -> ChString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ByteString
BS8.pack (String -> ByteString) -> (Int -> String) -> Int -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> String
forall a. Show a => a -> String
show
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 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 ToChType UUID Word64 where toChType :: UInt64 -> UUID
toChType = UInt128 -> UUID
MkChUUID (UInt128 -> UUID) -> (UInt64 -> UInt128) -> UInt64 -> UUID
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 UUID (Word64, Word64) where toChType :: (UInt64, UInt64) -> UUID
toChType = UInt128 -> UUID
MkChUUID (UInt128 -> UUID)
-> ((UInt64, UInt64) -> UInt128) -> (UInt64, UInt64) -> UUID
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 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 ToChType Date Word16 where toChType :: UInt16 -> Date
toChType = UInt16 -> Date
MkChDate
instance ToChType chType inputType => ToChType (Array chType) [inputType]
  where
  toChType :: [inputType] -> Array chType
toChType = [chType] -> Array chType
forall a. [a] -> Array a
MkChArray ([chType] -> Array chType)
-> ([inputType] -> [chType]) -> [inputType] -> Array 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








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 = ByteString -> Builder
byteString (ByteString -> Builder)
-> (Proxy (ToChTypeName chType) -> ByteString)
-> Proxy (ToChTypeName chType)
-> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ByteString
BS8.pack (String -> ByteString)
-> (Proxy (ToChTypeName chType) -> String)
-> Proxy (ToChTypeName chType)
-> ByteString
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

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

{- |
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

-- | ClickHouse String column type
newtype ChString = MkChString BS.ByteString
  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
""

-- | ClickHouse Date column type
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

-- | ClickHouse Array column type
newtype Array a = MkChArray [a]
  deriving newtype (Int -> Array a -> ShowS
[Array a] -> ShowS
Array a -> String
(Int -> Array a -> ShowS)
-> (Array a -> String) -> ([Array a] -> ShowS) -> Show (Array a)
forall a. Show a => Int -> Array a -> ShowS
forall a. Show a => [Array a] -> ShowS
forall a. Show a => Array a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> Array a -> ShowS
showsPrec :: Int -> Array a -> ShowS
$cshow :: forall a. Show a => Array a -> String
show :: Array a -> String
$cshowList :: forall a. Show a => [Array a] -> ShowS
showList :: [Array a] -> ShowS
Show, Array a -> Array a -> Bool
(Array a -> Array a -> Bool)
-> (Array a -> Array a -> Bool) -> Eq (Array a)
forall a. Eq a => Array a -> Array a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => Array a -> Array a -> Bool
== :: Array a -> Array a -> Bool
$c/= :: forall a. Eq a => Array a -> Array a -> Bool
/= :: Array a -> Array a -> Bool
Eq, Array a -> ()
(Array a -> ()) -> NFData (Array a)
forall a. NFData a => Array a -> ()
forall a. (a -> ()) -> NFData a
$crnf :: forall a. NFData a => Array a -> ()
rnf :: Array a -> ()
NFData)
instance
  KnownSymbol (AppendSymbol (AppendSymbol "Array(" (ToChTypeName chType)) ")")
  =>
  IsChType (Array chType)
  where
  type ToChTypeName (Array chType) = "Array(" `AppendSymbol` ToChTypeName chType `AppendSymbol` ")"
  defaultValueOfTypeName :: Array chType
defaultValueOfTypeName = [chType] -> Array chType
forall a. [a] -> Array a
MkChArray []


-- | 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


-- | 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 (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




{- |
  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

major, minor, patch :: UVarInt
major :: UVarInt
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
minor :: UVarInt
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
patch :: UVarInt
patch = 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

clientName :: ChString
clientName :: ChString
clientName = 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
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
minor 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
patch

newtype ProtocolRevision = MkProtocolRevision Word64
  deriving newtype (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 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 :: Natural) 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 :: Natural) (proxy :: Natural -> *).
KnownNat n =>
proxy n -> Integer
natVal) (forall (t :: Natural). 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 :: Natural) (proxy :: Natural -> *).
KnownNat n =>
proxy n -> Integer
natVal) (forall (t :: Natural). 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 :: Natural) (proxy :: Natural -> *).
KnownNat n =>
proxy n -> Integer
natVal) (forall (t :: Natural). Proxy t
forall {k} (t :: k). Proxy t
Proxy @revision)
    then chType -> SinceRevision chType revision
forall a (revisionNumber :: Natural).
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 :: Natural).
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 :: Natural) 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 :: Natural) 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;