{-# OPTIONS_GHC -fno-warn-unused-binds #-}
{-# LANGUAGE GADTs, TypeOperators, TypeFamilies, FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving, MultiParamTypeClasses #-}
{-# LANGUAGE UndecidableInstances, DeriveGeneric, OverloadedStrings #-}
{-# LANGUAGE CPP #-}
module Database.Selda.Types
( (:*:)(..), Head, Tup (..)
, first, second, third, fourth, fifth
, ColName, TableName
, modColName, mkColName, mkTableName, addColSuffix, addColPrefix
, fromColName, fromTableName, rawTableName, intercalateColNames
) where
import Data.Dynamic
import Data.String
import Data.Text (Text, replace, append, intercalate)
import GHC.Generics (Generic)
newtype ColName = ColName { unColName :: Text }
deriving (Ord, Eq, Show, IsString)
newtype TableName = TableName Text
deriving (Ord, Eq, Show, IsString)
modColName :: ColName -> (Text -> Text) -> ColName
modColName (ColName cn) f = ColName (f cn)
addColPrefix :: ColName -> Text -> ColName
addColPrefix (ColName cn) s = ColName $ Data.Text.append s cn
addColSuffix :: ColName -> Text -> ColName
addColSuffix (ColName cn) s = ColName $ Data.Text.append cn s
fromColName :: ColName -> Text
fromColName (ColName cn) = mconcat ["\"", escapeQuotes cn, "\""]
intercalateColNames :: Text -> [ColName] -> Text
intercalateColNames inter cs = intercalate inter (escapeQuotes . unColName <$> cs)
fromTableName :: TableName -> Text
fromTableName (TableName tn) = mconcat ["\"", escapeQuotes tn, "\""]
rawTableName :: TableName -> Text
rawTableName (TableName tn) = escapeQuotes tn
mkColName :: Text -> ColName
mkColName = ColName
mkTableName :: Text -> TableName
mkTableName = TableName
escapeQuotes :: Text -> Text
escapeQuotes = Data.Text.replace "\"" "\"\""
data a :*: b where
(:*:) :: a -> b -> a :*: b
deriving (Typeable, Generic)
infixr 1 :*:
instance (Show a, Show b) => Show (a :*: b) where
show (a :*: b) = show a ++ " :*: " ++ show b
instance (Eq a, Eq b) => Eq (a :*: b) where
(a :*: b) == (a' :*: b') = a == a' && b == b'
instance (Ord a, Ord b) => Ord (a :*: b) where
(a :*: b) `compare` (a' :*: b') =
case a `compare` a' of
EQ -> b `compare` b'
o -> o
type family Head a where
Head (a :*: b) = a
Head a = a
class Tup a where
tupHead :: a -> Head a
instance {-# OVERLAPPING #-} Tup (a :*: b) where
tupHead (a :*: _) = a
instance Head a ~ a => Tup a where
tupHead a = a
first :: Tup a => a -> Head a
first = tupHead
second :: Tup b => (a :*: b) -> Head b
second (_ :*: b) = tupHead b
third :: Tup c => (a :*: b :*: c) -> Head c
third (_ :*: _ :*: c) = tupHead c
fourth :: Tup d => (a :*: b :*: c :*: d) -> Head d
fourth (_ :*: _ :*: _ :*: d) = tupHead d
fifth :: Tup e => (a :*: b :*: c :*: d :*: e) -> Head e
fifth (_ :*: _ :*: _ :*: _ :*: e) = tupHead e