{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}

-- | This module provides constants the

module Database.ClickHouseDriver.ServerProtocol where

import           Data.ByteString (ByteString)
import           Data.Vector     (Vector, fromList, (!?))


-- Name, version, revision
_HELLO :: Word
_HELLO :: Word
_HELLO = Word
0 :: Word

-- A block of data
_DATA :: Word
_DATA :: Word
_DATA = Word
1 :: Word

-- The exception during query execution
_EXCEPTION :: Word
_EXCEPTION :: Word
_EXCEPTION = Word
2 :: Word

-- Query execution process rows read, bytes read
_PROGRESS :: Word
_PROGRESS :: Word
_PROGRESS = Word
3 :: Word

-- ping response
_PONG :: Word
_PONG :: Word
_PONG = Word
4 :: Word

-- All packets were transimitted
_END_OF_STREAM :: Word
_END_OF_STREAM :: Word
_END_OF_STREAM = Word
5 :: Word

-- Packet with profiling info
_PROFILE_INFO :: Word
_PROFILE_INFO :: Word
_PROFILE_INFO = Word
6 :: Word

-- A block with totals
_TOTAL :: Word
_TOTAL :: Word
_TOTAL = Word
7 :: Word

-- A block with minimums and maximums
_EXTREMES :: Word
_EXTREMES :: Word
_EXTREMES = Word
8 :: Word

-- A response to TableStatus request
_TABLES_STATUS_RESPONSE :: Word
_TABLES_STATUS_RESPONSE :: Word
_TABLES_STATUS_RESPONSE = Word
9 :: Word

-- A System logs of the query execution
_LOG :: Word
_LOG :: Word
_LOG = Word
10 :: Word

-- Columns' description for default values calculation
_TABLE_COLUMNS :: Word
_TABLE_COLUMNS :: Word
_TABLE_COLUMNS = Word
11 :: Word

typeStr :: Vector ByteString
typeStr :: Vector ByteString
typeStr =
  [ByteString] -> Vector ByteString
forall a. [a] -> Vector a
fromList
    [ ByteString
"Hello",
      ByteString
"Data",
      ByteString
"Exception",
      ByteString
"Progress",
      ByteString
"Pong",
      ByteString
"EndOfStream",
      ByteString
"ProfileInfo",
      ByteString
"Totals",
      ByteString
"Extremes",
      ByteString
"TablesStatusResponse",
      ByteString
"Log",
      ByteString
"TableColumns"
    ]

toString :: Int -> ByteString
toString :: Int -> ByteString
toString Int
n = 
  case Vector ByteString
typeStr Vector ByteString -> Int -> Maybe ByteString
forall a. Vector a -> Int -> Maybe a
!? Int
n of
    Maybe ByteString
Nothing -> ByteString
"Unknown Packet"
    Just ByteString
t -> ByteString
t