Query returns query representation of a `SELECT` statement.
()
| 2493 | |
| 2494 | // Query returns query representation of a `SELECT` statement. |
| 2495 | func (s *Selector) Query() (string, []any) { |
| 2496 | b := s.Builder.clone() |
| 2497 | s.joinPrefix(&b) |
| 2498 | b.WriteString("SELECT ") |
| 2499 | if s.distinct { |
| 2500 | b.WriteString("DISTINCT ") |
| 2501 | } |
| 2502 | if len(s.selection) > 0 { |
| 2503 | s.joinSelect(&b) |
| 2504 | } else { |
| 2505 | b.WriteString("*") |
| 2506 | } |
| 2507 | if len(s.from) > 0 { |
| 2508 | b.WriteString(" FROM ") |
| 2509 | } |
| 2510 | for i, from := range s.from { |
| 2511 | if i > 0 { |
| 2512 | b.Comma() |
| 2513 | } |
| 2514 | switch t := from.(type) { |
| 2515 | case *SelectTable: |
| 2516 | t.SetDialect(s.dialect) |
| 2517 | b.WriteString(t.ref()) |
| 2518 | case *Selector: |
| 2519 | t.SetDialect(s.dialect) |
| 2520 | b.Wrap(func(b *Builder) { |
| 2521 | b.Join(t) |
| 2522 | }) |
| 2523 | if t.as != "" { |
| 2524 | b.WriteString(" AS ") |
| 2525 | b.Ident(t.as) |
| 2526 | } |
| 2527 | case *WithBuilder: |
| 2528 | t.SetDialect(s.dialect) |
| 2529 | b.Ident(t.Name()) |
| 2530 | case *queryView: |
| 2531 | b.Join(t.Querier) |
| 2532 | } |
| 2533 | } |
| 2534 | for _, join := range s.joins { |
| 2535 | b.WriteString(" " + join.kind + " ") |
| 2536 | switch view := join.table.(type) { |
| 2537 | case *SelectTable: |
| 2538 | view.SetDialect(s.dialect) |
| 2539 | b.WriteString(view.ref()) |
| 2540 | case *Selector: |
| 2541 | view.SetDialect(s.dialect) |
| 2542 | b.Wrap(func(b *Builder) { |
| 2543 | b.Join(view) |
| 2544 | }) |
| 2545 | b.WriteString(" AS ") |
| 2546 | b.Ident(view.as) |
| 2547 | case *WithBuilder: |
| 2548 | view.SetDialect(s.dialect) |
| 2549 | b.Ident(view.Name()) |
| 2550 | } |
| 2551 | if join.on != nil { |
| 2552 | b.WriteString(" ON ") |
nothing calls this directly
no test coverage detected