graphql-engine/server/src-lib/Hasura/Backends/MSSQL/Schema/Introspection.hs
Tom Harding 3cef692dd7 feature(server): Get _all_ source tables
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/8649
GitOrigin-RevId: 4bb9311d1d7ab4e8ee641bde5df2babcb1b8b306
2023-04-05 20:16:14 +00:00

43 lines
1.8 KiB
Haskell

{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE ViewPatterns #-}
module Hasura.Backends.MSSQL.Schema.Introspection
( listAllTables,
)
where
import Control.Monad.Trans.Control (MonadBaseControl)
import Data.String.Interpolate (i)
import Database.MSSQL.Transaction (multiRowQueryE)
import Database.ODBC.SQLServer qualified as ODBC
import Hasura.Backends.MSSQL.Connection (runMSSQLSourceWriteTx)
import Hasura.Backends.MSSQL.SQL.Error (defaultMSSQLTxErrorHandler)
import Hasura.Backends.MSSQL.Types (SchemaName (..), TableName (..))
import Hasura.Base.Error (QErr, prefixQErr)
import Hasura.Prelude
import Hasura.RQL.Types.Common (SourceName)
import Hasura.RQL.Types.Metadata (MetadataM)
import Hasura.RQL.Types.SchemaCache (CacheRM, askSourceConfig)
import Hasura.SQL.Backend (BackendType (MSSQL))
-- | List all tables, tracked or untracked, on a given data source.
listAllTables :: (CacheRM m, MetadataM m, MonadBaseControl IO m, MonadError QErr m, MonadIO m) => env -> SourceName -> m [TableName]
listAllTables _ sourceName = do
sourceConfig <- askSourceConfig @'MSSQL sourceName
let listAllTablesSql :: ODBC.Query
listAllTablesSql =
[i|
select table_name, table_schema
from information_schema.tables
where table_schema not in (
'guest', 'INFORMATION_SCHEMA', 'sys', 'db_owner', 'db_securityadmin', 'db_accessadmin', 'db_backupoperator', 'db_ddladmin', 'db_datawriter', 'db_datareader', 'db_denydatawriter', 'db_denydatareader', 'hdb_catalog'
);
|]
results :: [(Text, Text)] <-
runMSSQLSourceWriteTx sourceConfig (multiRowQueryE defaultMSSQLTxErrorHandler listAllTablesSql)
`onLeftM` \err -> throwError (prefixQErr "failed to fetch source tables: " err)
pure [TableName {..} | (tableName, SchemaName -> tableSchema) <- results]