@@ -23,7 +23,7 @@ func calcHowMuchNextStmtWillTake(tokens []sqllexer.Token) int {
2323 result := 0
2424 stack := []string {}
2525 for _ , token := range tokens {
26- if token .Type == sqllexer .WS {
26+ if token .Type == sqllexer .SPACE {
2727 result += 1
2828 continue
2929 }
@@ -48,19 +48,30 @@ func calcHowMuchNextStmtWillTake(tokens []sqllexer.Token) int {
4848}
4949
5050func SqlPrettyPrint (sqlData []byte ) string {
51- lexer := sqllexer .New (string (sqlData ))
52- tokens := lexer .ScanAll ()
53- var sb strings.Builder
54- lineLength := 0
55- subQueryIndent := 0
56- isBreakIndent := false
57- stack := []string {}
51+ var (
52+ sb strings.Builder
53+ stack []string
54+ isBreakIndent bool
55+ lineLength , subQueryIndent int
56+ tokens []sqllexer.Token
57+ lexer = sqllexer .New (string (sqlData ))
58+ )
59+
60+ // below: replacement for deprecated lexer.ScanAll()
61+ for {
62+ tok := lexer .Scan ()
63+ if tok == nil || tok .Type == sqllexer .EOF {
64+ break
65+ }
66+ tokens = append (tokens , * tok )
67+ }
68+
5869 for tokenIdx , token := range tokens {
5970 // Super useful, uncomment to debug and run go test ./...
6071 // fmt.Print(token, ", ")
6172
6273 // Skip original whitespace
63- if token .Type == sqllexer .WS {
74+ if token .Type == sqllexer .SPACE {
6475 token .Value = " "
6576 if tokenIdx > 0 && tokens [tokenIdx - 1 ].Value == "(" {
6677 continue
@@ -124,7 +135,7 @@ func SqlPrettyPrint(sqlData []byte) string {
124135
125136 // Break line if needed
126137 if lineLength > 0 && len (token .Value )+ lineLength > lineLengthLimit {
127- if token .Type == sqllexer .WS && tokenIdx + 1 < len (tokens ) && newLineKeywords [tokens [tokenIdx + 1 ].Value ] {
138+ if token .Type == sqllexer .SPACE && tokenIdx + 1 < len (tokens ) && newLineKeywords [tokens [tokenIdx + 1 ].Value ] {
128139 continue // we will break line in next token anyway, no need to double break
129140 }
130141 lineLength = 0
@@ -142,7 +153,7 @@ func SqlPrettyPrint(sqlData []byte) string {
142153 sb .WriteString (" " )
143154 }
144155 lineLength += currentIndentLevel
145- if token .Type == sqllexer .WS {
156+ if token .Type == sqllexer .SPACE {
146157 continue
147158 }
148159 }
@@ -165,7 +176,7 @@ func SqlPrettyPrint(sqlData []byte) string {
165176 lineLength = 0
166177 isBreakIndent = false
167178 } else {
168- if tokenIdx + 1 < len (tokens ) && tokens [tokenIdx + 1 ].Type != sqllexer .WS {
179+ if tokenIdx + 1 < len (tokens ) && tokens [tokenIdx + 1 ].Type != sqllexer .SPACE {
169180 sb .WriteString (" " )
170181 lineLength += 1
171182 }
0 commit comments