formatSelect formats SELECT statements with proper indentation and alignment
(stmt *ast.SelectStatement)
| 116 | |
| 117 | // formatSelect formats SELECT statements with proper indentation and alignment |
| 118 | func (f *SQLFormatter) formatSelect(stmt *ast.SelectStatement) error { |
| 119 | // WITH clause |
| 120 | if stmt.With != nil { |
| 121 | if err := f.formatWithClause(stmt.With); err != nil { |
| 122 | return err |
| 123 | } |
| 124 | f.writeNewline() |
| 125 | } |
| 126 | |
| 127 | // SELECT keyword and columns |
| 128 | f.writeKeyword("SELECT") |
| 129 | if stmt.Distinct { |
| 130 | f.builder.WriteString(" ") |
| 131 | f.writeKeyword("DISTINCT") |
| 132 | } |
| 133 | |
| 134 | if f.compact { |
| 135 | f.builder.WriteString(" ") |
| 136 | f.formatExpressionList(stmt.Columns, ", ") |
| 137 | } else { |
| 138 | f.writeNewline() |
| 139 | f.increaseIndent() |
| 140 | f.builder.WriteString(f.currentIndent()) |
| 141 | f.formatExpressionList(stmt.Columns, ",\n"+f.currentIndent()) |
| 142 | f.decreaseIndent() |
| 143 | } |
| 144 | |
| 145 | // FROM clause |
| 146 | if len(stmt.From) > 0 { |
| 147 | f.writeNewline() |
| 148 | f.writeKeyword("FROM") |
| 149 | f.builder.WriteString(" ") |
| 150 | f.formatTableReferences(stmt.From) |
| 151 | } |
| 152 | |
| 153 | // JOINs |
| 154 | for _, join := range stmt.Joins { |
| 155 | join := join // G601: Create local copy to avoid memory aliasing |
| 156 | f.writeNewline() |
| 157 | if err := f.formatJoin(&join); err != nil { |
| 158 | return err |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // WHERE clause |
| 163 | if stmt.Where != nil { |
| 164 | f.writeNewline() |
| 165 | f.writeKeyword("WHERE") |
| 166 | f.builder.WriteString(" ") |
| 167 | if err := f.formatExpression(stmt.Where); err != nil { |
| 168 | return err |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | // GROUP BY clause |
| 173 | if len(stmt.GroupBy) > 0 { |
| 174 | f.writeNewline() |
| 175 | f.writeKeyword("GROUP BY") |
no test coverage detected