Skip to content

Commit

Permalink
Support collecting stored procedure name
Browse files Browse the repository at this point in the history
  • Loading branch information
lu-zhengda committed Oct 19, 2023
1 parent 8d68006 commit 31856d0
Show file tree
Hide file tree
Showing 5 changed files with 397 additions and 233 deletions.
35 changes: 25 additions & 10 deletions normalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ type normalizerConfig struct {
// CollectComments specifies whether the normalizer should extract and return comments as SQL metadata
CollectComments bool

// CollectProcedure specifies whether the normalizer should extract and return procedure name as SQL metadata
CollectProcedure bool

// KeepSQLAlias reports whether SQL aliases ("AS") should be truncated.
KeepSQLAlias bool

Expand Down Expand Up @@ -53,11 +56,18 @@ func WithUppercaseKeywords(uppercaseKeywords bool) normalizerOption {
}
}

func WithCollectProcedures(collectProcedure bool) normalizerOption {
return func(c *normalizerConfig) {
c.CollectProcedure = collectProcedure
}
}

type StatementMetadata struct {
Size int
Tables []string
Comments []string
Commands []string
Size int
Tables []string
Comments []string
Commands []string
Procedures []string
}

type groupablePlaceholder struct {
Expand Down Expand Up @@ -92,9 +102,10 @@ func (n *Normalizer) Normalize(input string, lexerOpts ...lexerOption) (normaliz
var normalizedSQLBuilder strings.Builder

statementMetadata = &StatementMetadata{
Tables: []string{},
Comments: []string{},
Commands: []string{},
Tables: []string{},
Comments: []string{},
Commands: []string{},
Procedures: []string{},
}

var lastToken Token // The last token that is not whitespace or comment
Expand Down Expand Up @@ -128,6 +139,9 @@ func (n *Normalizer) collectMetadata(token *Token, lastToken *Token, statementMe
} else if n.config.CollectTables && isTableIndicator(strings.ToUpper(lastToken.Value)) {
// Collect table names
statementMetadata.Tables = append(statementMetadata.Tables, token.Value)
} else if n.config.CollectProcedure && isProcedure(lastToken) {
// Collect procedure names
statementMetadata.Procedures = append(statementMetadata.Procedures, token.Value)
}
}
}
Expand All @@ -145,7 +159,7 @@ func (n *Normalizer) normalizeSQL(token *Token, lastToken *Token, normalizedSQLB
}

if strings.ToUpper(lastToken.Value) == "AS" {
if token.Type == IDENT {
if token.Type == IDENT && !isSQLKeyword(token) {
// 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
Expand Down Expand Up @@ -223,11 +237,12 @@ func dedupeCollectedMetadata(metadata []string) (dedupedMetadata []string, size
}

func dedupeStatementMetadata(info *StatementMetadata) {
var tablesSize, commentsSize, commandsSize int
var tablesSize, commentsSize, commandsSize, procedureSize int
info.Tables, tablesSize = dedupeCollectedMetadata(info.Tables)
info.Comments, commentsSize = dedupeCollectedMetadata(info.Comments)
info.Commands, commandsSize = dedupeCollectedMetadata(info.Commands)
info.Size += tablesSize + commentsSize + commandsSize
info.Procedures, procedureSize = dedupeCollectedMetadata(info.Procedures)
info.Size += tablesSize + commentsSize + commandsSize + procedureSize
}

func appendWhitespace(lastToken *Token, token *Token, normalizedSQLBuilder *strings.Builder) {
Expand Down
Loading

0 comments on commit 31856d0

Please sign in to comment.