Files
sql-utilities/src/Strata.SqlTools.SqlBreakdown/Utilities/SqlSchemaHelpers.cs
T
Thom LambandClaude Opus 4.7 0f8d505616
SonarQube Analysis / sonarqube (pull_request) Successful in 2m47s
chore(sonar): apply collection-expression syntax across all sites (IDE0028)
Manual sweep of all 42 IDE0028 sites flagged by SonarQube — `dotnet format
analyzers --diagnostics IDE0028` declined to fix these (no .editorconfig
opt-in for `dotnet_style_prefer_collection_expression`), so applied by
hand. The repo already targets `<LangVersion>latest</LangVersion>` on
net8.0, so C# 12 collection expressions are available.

Pattern: `new List<T>()` / `new Dictionary<K,V>()` / `new ArrayList()` /
`new()` -> `[]` for empty; `new List<T> { ... }` -> `[...]` for literal.

24 files touched in src/{EFCore, LinqToSql, Query, Snowflake, SqlBreakdown,
SqlServer}; tests untouched (no IDE0028 sites in test code).

Build clean (35 warnings unchanged from baseline, 0 errors). All tests
remain green.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-26 17:01:30 -05:00

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()
=>
[
DEFAULT_SCHEMA,
"int", // integration
"perf",
"audit",
"log",
"sqlgen",
"upgrade",
"upg",
"irc", // irc chat rooms
"migration"
];
}