{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
module Database.InfluxDB.Format
(
Format
, makeFormat
, (%)
, formatQuery
, formatDatabase
, formatMeasurement
, formatKey
, database
, key
, keys
, measurement
, measurements
, field
, decimal
, realFloat
, text
, string
, byteString8
, time
, fromQuery
) where
import Control.Category
import Data.Monoid
import Data.String
import Prelude hiding ((.), id)
import Data.Time
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as BL
import qualified Data.ByteString.Builder as BB
import qualified Data.List as L
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Builder as TL
import qualified Data.Text.Lazy.Builder.Int as TL
import qualified Data.Text.Lazy.Builder.RealFloat as TL
import Database.InfluxDB.Internal.Text
import Database.InfluxDB.Types hiding (database)
fromQuery :: Query -> B.ByteString
fromQuery :: Query -> ByteString
fromQuery (Query Text
q) =
ByteString -> ByteString
BL.toStrict forall a b. (a -> b) -> a -> b
$ Builder -> ByteString
BB.toLazyByteString forall a b. (a -> b) -> a -> b
$ Text -> Builder
T.encodeUtf8Builder Text
q
newtype Format a r = Format { forall a r. Format a r -> (Builder -> a) -> r
runFormat :: (TL.Builder -> a) -> r }
instance Category Format where
id :: forall a. Format a a
id = forall a r. ((Builder -> a) -> r) -> Format a r
Format (\Builder -> a
k -> Builder -> a
k Builder
"")
Format b c
fmt1 . :: forall b c a. Format b c -> Format a b -> Format a c
. Format a b
fmt2 = forall a r. ((Builder -> a) -> r) -> Format a r
Format forall a b. (a -> b) -> a -> b
$ \Builder -> a
k ->
forall a r. Format a r -> (Builder -> a) -> r
runFormat Format b c
fmt1 forall a b. (a -> b) -> a -> b
$ \Builder
a ->
forall a r. Format a r -> (Builder -> a) -> r
runFormat Format a b
fmt2 forall a b. (a -> b) -> a -> b
$ \Builder
b ->
Builder -> a
k (Builder
a forall a. Semigroup a => a -> a -> a
<> Builder
b)
instance a ~ r => IsString (Format a r) where
fromString :: String -> Format a r
fromString String
xs = forall a r. ((Builder -> a) -> r) -> Format a r
Format forall a b. (a -> b) -> a -> b
$ \Builder -> a
k -> Builder -> a
k forall a b. (a -> b) -> a -> b
$ forall a. IsString a => String -> a
fromString String
xs
(%) :: Format b c -> Format a b -> Format a c
% :: forall b c a. Format b c -> Format a b -> Format a c
(%) = forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
(.)
runFormatWith :: (T.Text -> a) -> Format a r -> r
runFormatWith :: forall a r. (Text -> a) -> Format a r -> r
runFormatWith Text -> a
f Format a r
fmt = forall a r. Format a r -> (Builder -> a) -> r
runFormat Format a r
fmt (Text -> a
f forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Text -> Text
TL.toStrict forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Builder -> Text
TL.toLazyText)
formatQuery :: Format Query r -> r
formatQuery :: forall r. Format Query r -> r
formatQuery = forall a r. (Text -> a) -> Format a r -> r
runFormatWith Text -> Query
Query
formatDatabase :: Format Database r -> r
formatDatabase :: forall r. Format Database r -> r
formatDatabase = forall a r. (Text -> a) -> Format a r -> r
runFormatWith Text -> Database
Database
formatMeasurement :: Format Measurement r -> r
formatMeasurement :: forall r. Format Measurement r -> r
formatMeasurement = forall a r. (Text -> a) -> Format a r -> r
runFormatWith Text -> Measurement
Measurement
formatKey :: Format Key r -> r
formatKey :: forall r. Format Key r -> r
formatKey Format Key r
fmt = forall a r. Format a r -> (Builder -> a) -> r
runFormat Format Key r
fmt (Text -> Key
Key forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Text -> Text
TL.toStrict forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Builder -> Text
TL.toLazyText)
makeFormat :: (a -> TL.Builder) -> Format r (a -> r)
makeFormat :: forall a r. (a -> Builder) -> Format r (a -> r)
makeFormat a -> Builder
build = forall a r. ((Builder -> a) -> r) -> Format a r
Format forall a b. (a -> b) -> a -> b
$ \Builder -> r
k a
a -> Builder -> r
k forall a b. (a -> b) -> a -> b
$ a -> Builder
build a
a
doubleQuote :: T.Text -> TL.Builder
doubleQuote :: Text -> Builder
doubleQuote Text
name = Builder
"\"" forall a. Semigroup a => a -> a -> a
<> Text -> Builder
TL.fromText Text
name forall a. Semigroup a => a -> a -> a
<> Builder
"\""
singleQuote :: T.Text -> TL.Builder
singleQuote :: Text -> Builder
singleQuote Text
name = Builder
"'" forall a. Semigroup a => a -> a -> a
<> Text -> Builder
TL.fromText Text
name forall a. Semigroup a => a -> a -> a
<> Builder
"'"
identifierBuilder :: T.Text -> TL.Builder
identifierBuilder :: Text -> Builder
identifierBuilder = Text -> Builder
doubleQuote forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Text -> Text
escapeDoubleQuotes
stringBuilder :: T.Text -> TL.Builder
stringBuilder :: Text -> Builder
stringBuilder = Text -> Builder
singleQuote forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Text -> Text
escapeSingleQuotes
database :: Format r (Database -> r)
database :: forall r. Format r (Database -> r)
database = forall a r. (a -> Builder) -> Format r (a -> r)
makeFormat forall a b. (a -> b) -> a -> b
$ \(Database Text
name) -> Text -> Builder
identifierBuilder Text
name
key :: Format r (Key -> r)
key :: forall r. Format r (Key -> r)
key = forall a r. (a -> Builder) -> Format r (a -> r)
makeFormat forall a b. (a -> b) -> a -> b
$ \(Key Text
name) -> Text -> Builder
identifierBuilder Text
name
keys :: Format r ([Key] -> r)
keys :: forall r. Format r ([Key] -> r)
keys = forall a r. (a -> Builder) -> Format r (a -> r)
makeFormat forall a b. (a -> b) -> a -> b
$
forall a. Monoid a => [a] -> a
mconcat forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a. a -> [a] -> [a]
L.intersperse Builder
"," forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a b. (a -> b) -> [a] -> [b]
map (\(Key Text
name) -> Text -> Builder
identifierBuilder Text
name)
measurement :: Format r (Measurement -> r)
measurement :: forall r. Format r (Measurement -> r)
measurement = forall a r. (a -> Builder) -> Format r (a -> r)
makeFormat forall a b. (a -> b) -> a -> b
$ \(Measurement Text
name) -> Text -> Builder
identifierBuilder Text
name
measurements :: Format r ([Measurement] -> r)
measurements :: forall r. Format r ([Measurement] -> r)
measurements = forall a r. (a -> Builder) -> Format r (a -> r)
makeFormat forall a b. (a -> b) -> a -> b
$
forall a. Monoid a => [a] -> a
mconcat forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a. a -> [a] -> [a]
L.intersperse Builder
","
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a b. (a -> b) -> [a] -> [b]
map (\(Measurement Text
name) -> Text -> Builder
identifierBuilder Text
name)
field :: Format r (QueryField -> r)
field :: forall r. Format r (QueryField -> r)
field = forall a r. (a -> Builder) -> Format r (a -> r)
makeFormat forall a b. (a -> b) -> a -> b
$ \case
FieldInt Int64
n -> forall a. Integral a => a -> Builder
TL.decimal Int64
n
FieldFloat Double
d -> forall a. RealFloat a => a -> Builder
TL.realFloat Double
d
FieldString Text
s -> Text -> Builder
stringBuilder Text
s
FieldBool Bool
b -> if Bool
b then Builder
"true" else Builder
"false"
QueryField
FieldNull -> Builder
"null"
decimal :: Integral a => Format r (a -> r)
decimal :: forall a r. Integral a => Format r (a -> r)
decimal = forall a r. (a -> Builder) -> Format r (a -> r)
makeFormat forall a. Integral a => a -> Builder
TL.decimal
realFloat :: RealFloat a => Format r (a -> r)
realFloat :: forall a r. RealFloat a => Format r (a -> r)
realFloat = forall a r. (a -> Builder) -> Format r (a -> r)
makeFormat forall a. RealFloat a => a -> Builder
TL.realFloat
text :: Format r (T.Text -> r)
text :: forall r. Format r (Text -> r)
text = forall a r. (a -> Builder) -> Format r (a -> r)
makeFormat Text -> Builder
TL.fromText
string :: Format r (String -> r)
string :: forall r. Format r (String -> r)
string = forall a r. (a -> Builder) -> Format r (a -> r)
makeFormat String -> Builder
TL.fromString
byteString8 :: Format r (B.ByteString -> r)
byteString8 :: forall r. Format r (ByteString -> r)
byteString8 = forall a r. (a -> Builder) -> Format r (a -> r)
makeFormat forall a b. (a -> b) -> a -> b
$ Text -> Builder
TL.fromText forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ByteString -> Text
T.decodeUtf8
time :: FormatTime time => Format r (time -> r)
time :: forall time r. FormatTime time => Format r (time -> r)
time = forall a r. (a -> Builder) -> Format r (a -> r)
makeFormat forall a b. (a -> b) -> a -> b
$ \time
t ->
Builder
"'" forall a. Semigroup a => a -> a -> a
<> String -> Builder
TL.fromString (forall t. FormatTime t => TimeLocale -> String -> t -> String
formatTime TimeLocale
defaultTimeLocale String
fmt time
t) forall a. Semigroup a => a -> a -> a
<> Builder
"'"
where
fmt :: String
fmt = String
"%F %X%Q"