============================================================ Statements ============================================================ SQL returns the full SQL string for this SELECT statement, including all clauses: WITH, DISTINCT ON/DISTINCT, SELECT list, FROM, JOIN, WHERE, GROUP BY, HAVING, WINDOW,
()
| 524 | // GROUP BY, HAVING, WINDOW, ORDER BY, LIMIT, OFFSET, FETCH, and FOR. |
| 525 | // This enables round-trip serialization of parsed queries. |
| 526 | func (s *SelectStatement) SQL() string { |
| 527 | if s == nil { |
| 528 | return "" |
| 529 | } |
| 530 | sb := getBuilder() |
| 531 | defer putBuilder(sb) |
| 532 | |
| 533 | if s.With != nil { |
| 534 | sb.WriteString(s.With.SQL()) |
| 535 | sb.WriteString(" ") |
| 536 | } |
| 537 | |
| 538 | sb.WriteString("SELECT ") |
| 539 | |
| 540 | if len(s.DistinctOnColumns) > 0 { |
| 541 | sb.WriteString("DISTINCT ON (") |
| 542 | sb.WriteString(exprListSQL(s.DistinctOnColumns)) |
| 543 | sb.WriteString(") ") |
| 544 | } else if s.Distinct { |
| 545 | sb.WriteString("DISTINCT ") |
| 546 | } |
| 547 | |
| 548 | sb.WriteString(exprListSQL(s.Columns)) |
| 549 | |
| 550 | if len(s.From) > 0 { |
| 551 | sb.WriteString(" FROM ") |
| 552 | froms := make([]string, len(s.From)) |
| 553 | for i := range s.From { |
| 554 | froms[i] = tableRefSQL(&s.From[i]) |
| 555 | } |
| 556 | sb.WriteString(strings.Join(froms, ", ")) |
| 557 | } |
| 558 | |
| 559 | for _, j := range s.Joins { |
| 560 | j := j // G601: Create local copy to avoid memory aliasing |
| 561 | sb.WriteString(" ") |
| 562 | sb.WriteString(joinSQL(&j)) |
| 563 | } |
| 564 | |
| 565 | if s.PrewhereClause != nil { |
| 566 | sb.WriteString(" PREWHERE ") |
| 567 | sb.WriteString(exprSQL(s.PrewhereClause)) |
| 568 | } |
| 569 | |
| 570 | if s.Where != nil { |
| 571 | sb.WriteString(" WHERE ") |
| 572 | sb.WriteString(exprSQL(s.Where)) |
| 573 | } |
| 574 | |
| 575 | if len(s.GroupBy) > 0 { |
| 576 | sb.WriteString(" GROUP BY ") |
| 577 | sb.WriteString(exprListSQL(s.GroupBy)) |
| 578 | } |
| 579 | |
| 580 | if s.Having != nil { |
| 581 | sb.WriteString(" HAVING ") |
| 582 | sb.WriteString(exprSQL(s.Having)) |
| 583 | } |