79 lines
2.9 KiB
C#
79 lines
2.9 KiB
C#
using Strata.SqlTools.SqlBreakdown.Extensions;
|
|
|
|
namespace Strata.SqlTools.SqlBreakdown.Utilities;
|
|
|
|
/// <summary>
|
|
/// SQL schema and table helper methods.
|
|
/// </summary>
|
|
public static partial class SqlUtils
|
|
{
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
/// <returns>A SQL query string for retrieving table or view schema information.</returns>
|
|
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();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets a list of client-usable schemas (excluding system schemas).
|
|
/// </summary>
|
|
/// <returns>A list of schema names available to clients.</returns>
|
|
public static List<string> GetClientUsableSchemas()
|
|
{
|
|
var list = new List<string>();
|
|
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets a list of system schema names that should not be exposed to clients.
|
|
/// </summary>
|
|
/// <returns>A list of system schema names.</returns>
|
|
public static List<string> GetSystemSchemas()
|
|
=> new List<string>
|
|
{
|
|
DEFAULT_SCHEMA,
|
|
"int", // integration
|
|
"perf",
|
|
"audit",
|
|
"log",
|
|
"sqlgen",
|
|
"upgrade",
|
|
"upg",
|
|
"irc", // irc chat rooms
|
|
"migration"
|
|
};
|
|
}
|
|
|