2021-03-15 16:02:58 +03:00
|
|
|
module Hasura.SQL.Backend
|
2021-09-24 01:56:37 +03:00
|
|
|
( PostgresKind (..),
|
|
|
|
BackendType (..),
|
|
|
|
backendShortName,
|
|
|
|
supportedBackends,
|
|
|
|
)
|
|
|
|
where
|
2021-04-22 00:44:37 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
import Data.Aeson
|
|
|
|
import Data.List.Extended (uniques)
|
|
|
|
import Data.Proxy
|
|
|
|
import Data.Text (unpack)
|
|
|
|
import Data.Text.Extended
|
|
|
|
import Hasura.Incremental
|
|
|
|
import Hasura.Prelude
|
2021-04-22 00:44:37 +03:00
|
|
|
|
|
|
|
-- | Argument to Postgres; we represent backends which are variations on Postgres as sub-types of
|
|
|
|
-- Postgres. This value indicates which "flavour" of Postgres a backend is.
|
|
|
|
data PostgresKind
|
|
|
|
= Vanilla
|
2021-05-21 05:46:58 +03:00
|
|
|
| Citus
|
2021-07-07 04:43:42 +03:00
|
|
|
deriving (Show, Eq, Ord)
|
2021-02-14 09:07:52 +03:00
|
|
|
|
|
|
|
-- | An enum that represents each backend we support.
|
2021-03-18 23:34:11 +03:00
|
|
|
data BackendType
|
2021-04-22 00:44:37 +03:00
|
|
|
= Postgres PostgresKind
|
2021-03-18 23:34:11 +03:00
|
|
|
| MSSQL
|
2021-04-12 13:18:29 +03:00
|
|
|
| BigQuery
|
2021-07-15 15:44:26 +03:00
|
|
|
| MySQL
|
2022-02-25 19:08:18 +03:00
|
|
|
| DataWrapper
|
2021-07-07 04:43:42 +03:00
|
|
|
deriving (Show, Eq, Ord)
|
2021-04-22 00:44:37 +03:00
|
|
|
|
2021-03-18 23:34:11 +03:00
|
|
|
-- | The name of the backend, as we expect it to appear in our metadata and API.
|
|
|
|
instance ToTxt BackendType where
|
2021-04-22 00:44:37 +03:00
|
|
|
toTxt (Postgres Vanilla) = "postgres"
|
2021-09-24 01:56:37 +03:00
|
|
|
toTxt (Postgres Citus) = "citus"
|
|
|
|
toTxt MSSQL = "mssql"
|
|
|
|
toTxt BigQuery = "bigquery"
|
|
|
|
toTxt MySQL = "mysql"
|
2022-02-25 19:08:18 +03:00
|
|
|
toTxt DataWrapper = "data-wrapper"
|
2021-03-18 23:34:11 +03:00
|
|
|
|
2021-07-07 04:43:42 +03:00
|
|
|
-- | The FromJSON instance uses this lookup mechanism to avoid having to duplicate and hardcode the
|
|
|
|
-- backend string. We accept both the short form and the long form of the backend's name.
|
2021-03-18 23:34:11 +03:00
|
|
|
instance FromJSON BackendType where
|
2021-07-07 04:43:42 +03:00
|
|
|
parseJSON = withText "backend type" \name -> do
|
2021-09-24 01:56:37 +03:00
|
|
|
let knownBackends =
|
|
|
|
supportedBackends >>= \b ->
|
|
|
|
[ (toTxt b, b), -- long form
|
|
|
|
(backendShortName b, b) -- short form
|
|
|
|
]
|
2021-07-07 04:43:42 +03:00
|
|
|
uniqueBackends = commaSeparated $ fst <$> uniques knownBackends
|
2021-09-24 01:56:37 +03:00
|
|
|
lookup name knownBackends
|
|
|
|
`onNothing` fail ("got: " <> unpack name <> ", expected one of: " <> unpack uniqueBackends)
|
2021-03-18 23:34:11 +03:00
|
|
|
|
|
|
|
instance ToJSON BackendType where
|
|
|
|
toJSON = String . toTxt
|
|
|
|
|
2021-04-22 00:44:37 +03:00
|
|
|
instance Cacheable (Proxy (b :: BackendType))
|
|
|
|
|
2021-07-07 04:43:42 +03:00
|
|
|
-- | Some generated APIs use a shortened version of the backend's name rather than its full
|
|
|
|
-- name. This function returns the "short form" of a backend, if any.
|
|
|
|
backendShortName :: BackendType -> Text
|
|
|
|
backendShortName = \case
|
|
|
|
Postgres Vanilla -> "pg"
|
2021-09-24 01:56:37 +03:00
|
|
|
b -> toTxt b
|
2021-07-07 04:43:42 +03:00
|
|
|
|
2021-03-18 23:34:11 +03:00
|
|
|
supportedBackends :: [BackendType]
|
2021-04-22 00:44:37 +03:00
|
|
|
supportedBackends =
|
2021-09-24 01:56:37 +03:00
|
|
|
[ Postgres Vanilla,
|
|
|
|
Postgres Citus,
|
|
|
|
MSSQL,
|
|
|
|
BigQuery,
|
2021-12-22 03:10:28 +03:00
|
|
|
MySQL,
|
2022-02-25 19:08:18 +03:00
|
|
|
DataWrapper
|
2021-04-22 00:44:37 +03:00
|
|
|
]
|