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

Method SQL

pkg/sql/ast/sql.go:526–627  ·  view source on GitHub ↗

============================================================ 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,

()

Source from the content-addressed store, hash-verified

524// GROUP BY, HAVING, WINDOW, ORDER BY, LIMIT, OFFSET, FETCH, and FOR.
525// This enables round-trip serialization of parsed queries.
526func (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 }

Callers 11

TestWindowFrameSQLFunction · 0.95
TestFetchSQLFunction · 0.95
TestForSQLFunction · 0.95
TestNilSQLFunction · 0.95
TestLateralTableRefFunction · 0.95
TestOrderByNullsLastFunction · 0.95
TestWindowFunctionSQLFunction · 0.95
TestCTESQLFunction · 0.95

Calls 13

getBuilderFunction · 0.85
putBuilderFunction · 0.85
exprListSQLFunction · 0.70
tableRefSQLFunction · 0.70
joinSQLFunction · 0.70
exprSQLFunction · 0.70
windowSpecSQLFunction · 0.70
orderBySQLFunction · 0.70
fetchSQLFunction · 0.70
forSQLFunction · 0.70
SQLMethod · 0.45
ToSQLMethod · 0.45

Tested by 11

TestWindowFrameSQLFunction · 0.76
TestFetchSQLFunction · 0.76
TestForSQLFunction · 0.76
TestNilSQLFunction · 0.76
TestLateralTableRefFunction · 0.76
TestOrderByNullsLastFunction · 0.76
TestWindowFunctionSQLFunction · 0.76
TestCTESQLFunction · 0.76