@@ -71,6 +71,7 @@ func (f *Formatter) WriteExpr(expr Expr) {
7171 expr .FormatSQL (f )
7272}
7373
74+
7475func (f * Formatter ) NewLine () {
7576 if f .mode != FormatModeBeautify {
7677 return
@@ -107,7 +108,46 @@ func Format(expr Expr) string {
107108 return formatter .String ()
108109}
109110
111+ func (p * BinaryOperation ) isLogicalOp () bool {
112+ switch p .Operation {
113+ case TokenKind (KeywordAnd ), TokenKind (KeywordOr ):
114+ return true
115+ default :
116+ return p .HasGlobal || p .HasNot
117+ }
118+ }
119+
120+ func isLogicalBinaryOp (expr Expr ) bool {
121+ if bin , ok := expr .(* BinaryOperation ); ok {
122+ return bin .isLogicalOp ()
123+ }
124+ return false
125+ }
126+
127+ func (p * BinaryOperation ) writeLogicalOperand (formatter * Formatter , expr Expr ) {
128+ if isLogicalBinaryOp (expr ) {
129+ formatter .WriteExpr (expr )
130+ } else {
131+ formatter .Indent ()
132+ formatter .WriteExpr (expr )
133+ formatter .Dedent ()
134+ }
135+ }
136+
110137func (p * BinaryOperation ) FormatSQL (formatter * Formatter ) {
138+ if p .isLogicalOp () && formatter .mode == FormatModeBeautify {
139+ p .writeLogicalOperand (formatter , p .LeftExpr )
140+ formatter .NewLine ()
141+ if p .HasNot {
142+ formatter .WriteString ("NOT " )
143+ } else if p .HasGlobal {
144+ formatter .WriteString ("GLOBAL " )
145+ }
146+ formatter .WriteString (string (p .Operation ))
147+ formatter .NewLine ()
148+ p .writeLogicalOperand (formatter , p .RightExpr )
149+ return
150+ }
111151 formatter .WriteExpr (p .LeftExpr )
112152 if p .Operation != TokenKindDash {
113153 formatter .WriteByte (whitespace )
@@ -478,8 +518,10 @@ func (a *AuthenticationClause) FormatSQL(formatter *Formatter) {
478518func (f * BetweenClause ) FormatSQL (formatter * Formatter ) {
479519 if f .Expr != nil {
480520 formatter .WriteExpr (f .Expr )
521+ formatter .WriteString (" BETWEEN " )
522+ } else {
523+ formatter .WriteString ("BETWEEN " )
481524 }
482- formatter .WriteString (" BETWEEN " )
483525 formatter .WriteExpr (f .Between )
484526 formatter .WriteString (" AND " )
485527 formatter .WriteExpr (f .And )
@@ -502,21 +544,24 @@ func (c *CTEStmt) FormatSQL(formatter *Formatter) {
502544}
503545
504546func (c * CaseExpr ) FormatSQL (formatter * Formatter ) {
505- formatter .WriteString ("CASE " )
547+ formatter .WriteString ("CASE" )
506548 if c .Expr != nil {
549+ formatter .WriteByte (whitespace )
507550 formatter .WriteExpr (c .Expr )
508551 }
509- for i , when := range c .Whens {
510- if i > 0 {
511- formatter .WriteByte (whitespace )
512- }
552+ formatter .Indent ()
553+ for _ , when := range c .Whens {
554+ formatter .Break ()
513555 formatter .WriteExpr (when )
514556 }
515557 if c .Else != nil {
516- formatter .WriteString (" ELSE " )
558+ formatter .Break ()
559+ formatter .WriteString ("ELSE " )
517560 formatter .WriteExpr (c .Else )
518561 }
519- formatter .WriteString (" END" )
562+ formatter .Dedent ()
563+ formatter .Break ()
564+ formatter .WriteString ("END" )
520565}
521566
522567func (c * CastExpr ) FormatSQL (formatter * Formatter ) {
@@ -1458,12 +1503,35 @@ func (g *GranteesClause) FormatSQL(formatter *Formatter) {
14581503}
14591504
14601505func (g * GroupByClause ) FormatSQL (formatter * Formatter ) {
1461- formatter .WriteString ("GROUP BY " )
1506+ formatter .WriteString ("GROUP BY" )
14621507 if g .AggregateType != "" {
1508+ formatter .WriteByte (whitespace )
14631509 formatter .WriteString (g .AggregateType )
14641510 }
14651511 if g .Expr != nil {
1466- formatter .WriteExpr (g .Expr )
1512+ if g .AggregateType == "" && formatter .mode == FormatModeBeautify {
1513+ if columnList , ok := g .Expr .(* ColumnExprList ); ok && len (columnList .Items ) > 0 {
1514+ formatter .Indent ()
1515+ for i , item := range columnList .Items {
1516+ if i == 0 {
1517+ formatter .Break ()
1518+ } else {
1519+ formatter .WriteByte (',' )
1520+ formatter .Break ()
1521+ }
1522+ formatter .WriteExpr (item )
1523+ }
1524+ formatter .Dedent ()
1525+ } else {
1526+ formatter .WriteByte (whitespace )
1527+ formatter .WriteExpr (g .Expr )
1528+ }
1529+ } else if g .AggregateType == "" {
1530+ formatter .WriteByte (whitespace )
1531+ formatter .WriteExpr (g .Expr )
1532+ } else {
1533+ formatter .WriteExpr (g .Expr )
1534+ }
14671535 }
14681536 if g .WithCube {
14691537 formatter .WriteString (" WITH CUBE" )
@@ -2591,16 +2659,17 @@ func (w *WhenClause) FormatSQL(formatter *Formatter) {
25912659 }
25922660}
25932661
2594- func isLogicalOperation (operation TokenKind ) bool {
2595- return strings .EqualFold (string (operation ), KeywordAnd ) || strings .EqualFold (string (operation ), KeywordOr )
2596- }
2597-
25982662func (w * WhereClause ) FormatSQL (formatter * Formatter ) {
25992663 formatter .WriteString ("WHERE" )
2600- formatter .Indent ()
2601- formatter .Break ()
2602- formatter .WriteExpr (w .Expr )
2603- formatter .Dedent ()
2664+ if isLogicalBinaryOp (w .Expr ) {
2665+ formatter .Break ()
2666+ formatter .WriteExpr (w .Expr )
2667+ } else {
2668+ formatter .Indent ()
2669+ formatter .Break ()
2670+ formatter .WriteExpr (w .Expr )
2671+ formatter .Dedent ()
2672+ }
26042673}
26052674
26062675func (w * WindowDefinition ) FormatSQL (formatter * Formatter ) {
0 commit comments