Skip to content

Commit

Permalink
replace scanAll to avoid creating a slice of tokens in memory
Browse files Browse the repository at this point in the history
  • Loading branch information
lu-zhengda committed Oct 17, 2023
1 parent 7efea94 commit 7a55cf7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
6 changes: 5 additions & 1 deletion normalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ func (n *Normalizer) Normalize(input string, lexerOpts ...lexerOption) (normaliz
var lastToken Token // The last token that is not whitespace or comment
var groupablePlaceholder groupablePlaceholder

for _, token := range lexer.ScanAll() {
for {
token := lexer.Scan()
if token.Type == EOF {
break
}
n.collectMetadata(&token, &lastToken, statementMetadata)
n.normalizeSQL(&token, &lastToken, &normalizedSQLBuilder, &groupablePlaceholder)
}
Expand Down
6 changes: 5 additions & 1 deletion obfuscate_and_normalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ func ObfuscateAndNormalize(input string, obfuscator *Obfuscator, normalizer *Nor
var lastToken Token // The last token that is not whitespace or comment
var groupablePlaceholder groupablePlaceholder

for _, token := range lexer.ScanAll() {
for {
token := lexer.Scan()
if token.Type == EOF {
break
}
token.Value = obfuscator.ObfuscateTokenValue(token, lexerOpts...)
normalizer.collectMetadata(&token, &lastToken, statementMetadata)
normalizer.normalizeSQL(&token, &lastToken, &normalizedSQLBuilder, &groupablePlaceholder)
Expand Down
6 changes: 5 additions & 1 deletion obfuscator.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ func (o *Obfuscator) Obfuscate(input string, lexerOpts ...lexerOption) string {
input,
lexerOpts...,
)
for _, token := range lexer.ScanAll() {
for {
token := lexer.Scan()
if token.Type == EOF {
break
}
obfuscatedSQL.WriteString(o.ObfuscateTokenValue(token, lexerOpts...))
}

Expand Down

0 comments on commit 7a55cf7

Please sign in to comment.