chore: refactor for sonarqube issues
SonarQube Analysis / sonarqube (pull_request) Successful in 6m3s
SonarQube Analysis / sonarqube (pull_request) Successful in 6m3s
This commit is contained in:
@@ -89,10 +89,7 @@ public class SqlExpressionClause : SqlClause, ISqlExpressionClause
|
||||
{
|
||||
var items = new List<string>();
|
||||
var current = new StringBuilder();
|
||||
var parenDepth = 0;
|
||||
var inSingleQuote = false;
|
||||
var inDoubleQuote = false;
|
||||
var inBracket = false;
|
||||
var state = new SplitScanState();
|
||||
|
||||
for (int i = 0; i < clause.Length; i++)
|
||||
{
|
||||
@@ -106,39 +103,10 @@ public class SqlExpressionClause : SqlClause, ISqlExpressionClause
|
||||
continue;
|
||||
}
|
||||
|
||||
// Toggle quote states
|
||||
if (ch == '\'' && !inDoubleQuote && !inBracket)
|
||||
{
|
||||
inSingleQuote = !inSingleQuote;
|
||||
}
|
||||
else if (ch == '"' && !inSingleQuote && !inBracket)
|
||||
{
|
||||
inDoubleQuote = !inDoubleQuote;
|
||||
}
|
||||
else if (ch == '[' && !inSingleQuote && !inDoubleQuote)
|
||||
{
|
||||
inBracket = true;
|
||||
}
|
||||
else if (ch == ']' && inBracket && !inSingleQuote && !inDoubleQuote)
|
||||
{
|
||||
inBracket = false;
|
||||
}
|
||||
|
||||
// Track parenthesis depth
|
||||
if (!inSingleQuote && !inDoubleQuote && !inBracket)
|
||||
{
|
||||
if (ch == '(')
|
||||
{
|
||||
parenDepth++;
|
||||
}
|
||||
else if (ch == ')')
|
||||
{
|
||||
parenDepth--;
|
||||
}
|
||||
}
|
||||
state.UpdateForCharacter(ch);
|
||||
|
||||
// Split on comma only when not inside quotes, brackets, or parentheses
|
||||
if (ch == ',' && !inSingleQuote && !inDoubleQuote && !inBracket && parenDepth == 0)
|
||||
if (ch == ',' && !state.InsideDelimiter && state.ParenDepth == 0)
|
||||
{
|
||||
items.Add(current.ToString());
|
||||
current.Clear();
|
||||
@@ -157,5 +125,55 @@ public class SqlExpressionClause : SqlClause, ISqlExpressionClause
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tracks quote, bracket, and parenthesis nesting while scanning a clause character by character,
|
||||
/// so the splitter knows when a comma is at the top level.
|
||||
/// </summary>
|
||||
private struct SplitScanState
|
||||
{
|
||||
public int ParenDepth;
|
||||
public bool InSingleQuote;
|
||||
public bool InDoubleQuote;
|
||||
public bool InBracket;
|
||||
|
||||
/// <summary>Gets whether the scanner is currently inside a quoted string or bracketed identifier.</summary>
|
||||
public readonly bool InsideDelimiter => InSingleQuote || InDoubleQuote || InBracket;
|
||||
|
||||
/// <summary>Updates the quote/bracket states and parenthesis depth for the given character.</summary>
|
||||
public void UpdateForCharacter(char ch)
|
||||
{
|
||||
// Toggle quote/bracket states
|
||||
if (ch == '\'' && !InDoubleQuote && !InBracket)
|
||||
{
|
||||
InSingleQuote = !InSingleQuote;
|
||||
}
|
||||
else if (ch == '"' && !InSingleQuote && !InBracket)
|
||||
{
|
||||
InDoubleQuote = !InDoubleQuote;
|
||||
}
|
||||
else if (ch == '[' && !InSingleQuote && !InDoubleQuote)
|
||||
{
|
||||
InBracket = true;
|
||||
}
|
||||
else if (ch == ']' && InBracket && !InSingleQuote && !InDoubleQuote)
|
||||
{
|
||||
InBracket = false;
|
||||
}
|
||||
|
||||
// Track parenthesis depth only when outside quotes/brackets
|
||||
if (!InsideDelimiter)
|
||||
{
|
||||
if (ch == '(')
|
||||
{
|
||||
ParenDepth++;
|
||||
}
|
||||
else if (ch == ')')
|
||||
{
|
||||
ParenDepth--;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
using Strata.SqlTools.SqlBreakdown.Interfaces.Core;
|
||||
|
||||
using Strata.SqlTools.SqlBreakdown.Interfaces.Core;
|
||||
|
||||
namespace Strata.SqlTools.SqlBreakdown.Expressions.Literals;
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -48,7 +48,7 @@ public static class StringExtensions
|
||||
|
||||
if (aCharCount < 0)
|
||||
{
|
||||
throw new ArgumentException();
|
||||
throw new ArgumentException("Character count cannot be negative.", nameof(aCharCount));
|
||||
}
|
||||
|
||||
if (aCharCount == 0)
|
||||
@@ -75,7 +75,7 @@ public static class StringExtensions
|
||||
|
||||
if (aCharCount < 0)
|
||||
{
|
||||
throw new ArgumentException();
|
||||
throw new ArgumentException("Character count cannot be negative.", nameof(aCharCount));
|
||||
}
|
||||
|
||||
if (aCharCount == 0)
|
||||
|
||||
@@ -44,12 +44,6 @@ public static partial class SqlUtils
|
||||
try
|
||||
{
|
||||
// Would need to execute SQL here - skipping for this conversion
|
||||
// var cmd = new StrataSimpleSQLRunner.SimpleSQLCommand("select * from sys.schemas where principal_id=1");
|
||||
// var dt = StrataSimpleSQLRunner.ExecuteDatatable(cmd);
|
||||
// foreach (DataRow row in dt.Rows)
|
||||
// {
|
||||
// list.Add(row[0].ToString());
|
||||
// }
|
||||
|
||||
// Remove system schemas
|
||||
return list.Except(GetSystemSchemas()).ToList();
|
||||
|
||||
Reference in New Issue
Block a user