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--;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user