module Data.ApiDataTypes where
import Control.Applicative
import Control.Monad
import Data.Aeson
import Data.Aeson.Types
import Data.Text as T
import Generics.Deriving
data TableName = TableName Text
deriving (Show, Generic, Eq)
data ColumnName = ColumnName Text
deriving (Show, Generic)
data Operator = Operator Text
deriving(Show, Generic)
data Action = Action Text
deriving (Show, Generic)
data Format = Format
{ getFormat :: Int
} deriving (Show, Eq)
type DateVal = Text
data FieldValue = IntField Int | TextField Text | DateField DateVal deriving (Show, Generic)
data SetField = SetField
{ columnName :: ColumnName
, setFieldValue :: FieldValue
} deriving (Show, Generic)
data JoinTable = JoinTable
{ tablename :: TableName
, field :: ColumnName
, operator :: Operator
, withTable :: TableName
, withField :: ColumnName
} deriving (Show, Generic)
data WhereCondition = WhereCondition
{ whereTableName :: TableName
, whereField :: ColumnName
, whereOperator :: Operator
, whereFieldValue :: FieldValue
} deriving (Show, Generic)
data SqlQuery = SqlQuery
{ format :: Int
, action :: Action
, selectName :: TableName
, set :: Maybe [SetField]
, joinTables :: Maybe [JoinTable]
, whereCondition :: Maybe [WhereCondition]
} deriving (Show, Generic)
data SqlResultQuery = SqlResultQuery
{ getAction :: Action
, getSelectTable :: TableName
, getSetFields :: Maybe [SetField]
, getJoins :: Maybe [JoinTable]
, getWhereCondition :: Maybe [WhereCondition]
} deriving (Show, Generic)
data SqlResponse = SqlResponse
{ response :: Text
, errors :: Text
, warnings :: Text
} deriving (Show, Generic)
instance FromJSON TableName
instance ToJSON TableName
instance FromJSON Action
instance ToJSON Action
instance FromJSON Operator
instance ToJSON Operator
instance ToJSON SetField
instance FromJSON SetField where
parseJSON (Object v)
= SetField
<$> v .: "columnName"
<*> ( IntField <$> (v .: "setFieldValue")
<|> TextField <$> (v .: "setFieldValue")
<|> DateField <$> (v .: "setFieldValue")
)
parseJSON _ = mzero
instance FromJSON JoinTable
instance ToJSON JoinTable
instance FromJSON ColumnName
instance ToJSON ColumnName
instance FromJSON SqlQuery where
parseJSON (Object v)
= SqlQuery
<$> v .: "format"
<*> v .: "action"
<*> v .: "selectName"
<*> v .:? "set"
<*> v .:? "joinTables"
<*> v .:? "whereCondition"
parseJSON _ = mzero
instance ToJSON SqlQuery
instance FromJSON SqlResultQuery
instance ToJSON SqlResultQuery
instance FromJSON SqlResponse
instance ToJSON SqlResponse
instance FromJSON FieldValue
instance ToJSON FieldValue
instance ToJSON WhereCondition
instance FromJSON WhereCondition where
parseJSON (Object v)
= WhereCondition
<$> v .: "whereTableName"
<*> v .: "whereField"
<*> v .: "whereOperator"
<*> ( IntField <$> (v .: "whereFieldValue")
<|> TextField <$> (v .: "whereFieldValue")
<|> DateField <$> (v .: "whereFieldValue")
)
parseJSON _ = mzero