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

Function renderSelect

pkg/formatter/render.go:197–330  ·  view source on GitHub ↗

─── statement renderers ─────────────────────────────────────────────────────

(s *ast.SelectStatement, opts ast.FormatOptions)

Source from the content-addressed store, hash-verified

195// ─── statement renderers ─────────────────────────────────────────────────────
196
197func renderSelect(s *ast.SelectStatement, opts ast.FormatOptions) string {
198 if s == nil {
199 return ""
200 }
201
202 // Dialect normalization: work on a shallow copy so the original AST is not mutated.
203 stmt := *s
204 s = &stmt
205 normalizeSelectForDialect(s, opts.Dialect)
206
207 f := newNodeFormatter(opts)
208 sb := f.sb
209
210 if s.With != nil {
211 sb.WriteString(renderWith(s.With, f))
212 sb.WriteString(f.clauseSep())
213 }
214
215 sb.WriteString(f.kw("SELECT"))
216 sb.WriteString(" ")
217
218 if len(s.DistinctOnColumns) > 0 {
219 sb.WriteString(f.kw("DISTINCT ON"))
220 sb.WriteString(" (")
221 sb.WriteString(exprListSQL(s.DistinctOnColumns))
222 sb.WriteString(") ")
223 } else if s.Distinct {
224 sb.WriteString(f.kw("DISTINCT"))
225 sb.WriteString(" ")
226 }
227
228 // Render TOP clause (SQL Server). Emitted between SELECT [DISTINCT] and columns.
229 if s.Top != nil {
230 sb.WriteString(f.kw("TOP"))
231 sb.WriteString(" ")
232 sb.WriteString(FormatExpression(s.Top.Count, opts))
233 if s.Top.IsPercent {
234 sb.WriteString(" ")
235 sb.WriteString(f.kw("PERCENT"))
236 }
237 if s.Top.WithTies {
238 sb.WriteString(" ")
239 sb.WriteString(f.kw("WITH TIES"))
240 }
241 sb.WriteString(" ")
242 }
243
244 sb.WriteString(exprListSQL(s.Columns))
245
246 if len(s.From) > 0 {
247 sb.WriteString(f.clauseSep())
248 sb.WriteString(f.kw("FROM"))
249 sb.WriteString(" ")
250 froms := make([]string, len(s.From))
251 for i := range s.From {
252 froms[i] = tableRefSQL(&s.From[i], f)
253 }
254 sb.WriteString(strings.Join(froms, ", "))

Callers 1

FormatStatementFunction · 0.85

Calls 15

newNodeFormatterFunction · 0.85
renderWithFunction · 0.85
FormatExpressionFunction · 0.85
sampleSQLFunction · 0.85
clauseSepMethod · 0.80
kwMethod · 0.80
resultMethod · 0.80
exprListSQLFunction · 0.70
tableRefSQLFunction · 0.70
joinSQLFunction · 0.70
windowSpecSQLFunction · 0.70

Tested by

no test coverage detected