Skip to content

Commit

Permalink
add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
lu-zhengda committed Oct 13, 2023
1 parent 708b62d commit 7e63af8
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions normalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,23 @@ func (n *Normalizer) normalizeSQL(token *Token, lastToken *Token, normalizedSQLB
if !n.config.KeepSQLAlias {
// discard SQL alias
if strings.ToUpper(token.Value) == "AS" {
// if current token is AS, then continue to next token
// because without seeing the next token, we cannot
// determine if the current token is an alias or not
*lastToken = *token
return
}

if strings.ToUpper(lastToken.Value) == "AS" {
if token.Type == IDENT {
// if the last token is AS and the current token is IDENT,
// then the current token is an alias, so we discard it
*lastToken = *token
return
} else {
// if the last token is AS and the current token is not IDENT,
// this could be a CTE like WITH ... AS (...),
// so we do not discard the current token
appendWhitespace(lastToken, token, normalizedSQLBuilder)
n.writeToken(lastToken, normalizedSQLBuilder)
}
Expand Down Expand Up @@ -174,6 +182,9 @@ func (n *Normalizer) writeToken(token *Token, normalizedSQLBuilder *strings.Buil
func (n *Normalizer) isObfuscatedValueGroupable(token *Token, lastToken *Token, groupablePlaceholder *GroupablePlaceholder) bool {
if token.Value == NumberPlaceholder || token.Value == StringPlaceholder {
if lastToken.Value == "(" || lastToken.Value == "[" {
// if the last token is "(" or "[", and the current token is a placeholder,
// we know it's the start of groupable placeholders
// we don't return here because we still need to write the first placeholder
groupablePlaceholder.groupable = true
} else if lastToken.Value == "," && groupablePlaceholder.groupable {
return true
Expand All @@ -185,6 +196,7 @@ func (n *Normalizer) isObfuscatedValueGroupable(token *Token, lastToken *Token,
}

if groupablePlaceholder.groupable && (token.Value == ")" || token.Value == "]") {
// end of groupable placeholders
groupablePlaceholder.groupable = false
}

Expand Down

0 comments on commit 7e63af8

Please sign in to comment.