mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-19 13:31:43 +03:00
3cef692dd7
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/8649 GitOrigin-RevId: 4bb9311d1d7ab4e8ee641bde5df2babcb1b8b306
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) => 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]
|