mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-20 22:11:45 +03:00
56db8ec358
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/8732 GitOrigin-RevId: e11a0ef6979d1d58a0b39dcd8fff48d446d3420f
43 lines
1.8 KiB
Haskell
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) => 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]
|