using Strata.SqlTools.SqlBreakdown.Extensions; namespace Strata.SqlTools.SqlBreakdown.Utilities; /// /// SQL schema and table helper methods. /// public static partial class SqlUtils { /// /// Returns SQL that will give you column information from a view or table. /// Uses Parameters @ObjectName and @SchemaName and returns columns: SQLColumnName (varchar), SQLDataType, ColumnID, SQLDataTypeName, IsIdentity. /// /// A SQL query string for retrieving table or view schema information. public static string GetTableOrViewSchema() { var sql = new StringBuilderEx(); sql.Append("select C.name as SQLColumnName, C.system_type_id as SQLDataType, C.column_id as ColumnID, ST.name AS SQLDataTypeName, C.is_identity AS IsIdentity from sys.tables T "); sql.AppendLine(" inner join sys.columns C on C.object_id = T.object_id "); sql.AppendLine(" inner join sys.schemas S on S.schema_id = T.schema_id "); sql.AppendLine(" inner join sys.types st on st.user_type_id = c.user_type_id"); sql.AppendLine(" where T.name = @ObjectName and S.name = @SchemaName "); sql.AppendLine("UNION "); sql.Append("select C.name as SQLColumnName, C.system_type_id as SQLDataType, C.column_id as ColumnID, ST.name AS SQLDataTypeName, C.is_identity AS IsIdentity from sys.views V "); sql.AppendLine(" inner join sys.columns C on C.object_id = V.object_id "); sql.AppendLine(" inner join sys.schemas S on S.schema_id = V.schema_id "); sql.AppendLine(" inner join sys.types st on st.user_type_id = c.user_type_id"); sql.AppendLine(" where V.name = @ObjectName and S.name = @SchemaName "); sql.AppendLine(" order by ColumnID"); return sql.ToString(); } /// /// Gets a list of client-usable schemas (excluding system schemas). /// /// A list of schema names available to clients. public static List GetClientUsableSchemas() { var list = new List(); try { // Would need to execute SQL here - skipping for this conversion // Remove system schemas return list.Except(GetSystemSchemas()).ToList(); } catch { // Ignore error and return empty list } return list; } /// /// Gets a list of system schema names that should not be exposed to clients. /// /// A list of system schema names. public static List GetSystemSchemas() => new List { DEFAULT_SCHEMA, "int", // integration "perf", "audit", "log", "sqlgen", "upgrade", "upg", "irc", // irc chat rooms "migration" }; }