MCPcopy Create free account
hub / github.com/ajitpratap0/GoSQLX / formatSelect

Method formatSelect

cmd/gosqlx/cmd/sql_formatter.go:118–234  ·  view source on GitHub ↗

formatSelect formats SELECT statements with proper indentation and alignment

(stmt *ast.SelectStatement)

Source from the content-addressed store, hash-verified

116
117// formatSelect formats SELECT statements with proper indentation and alignment
118func (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")

Callers 4

formatStatementMethod · 0.95
formatInsertMethod · 0.95
formatExpressionMethod · 0.95
formatTableReferenceMethod · 0.95

Calls 10

formatWithClauseMethod · 0.95
writeNewlineMethod · 0.95
writeKeywordMethod · 0.95
formatExpressionListMethod · 0.95
increaseIndentMethod · 0.95
currentIndentMethod · 0.95
decreaseIndentMethod · 0.95
formatTableReferencesMethod · 0.95
formatJoinMethod · 0.95
formatExpressionMethod · 0.95

Tested by

no test coverage detected