You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.1 KiB

  1. // Copyright 2016 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package builder
  5. import (
  6. "errors"
  7. "fmt"
  8. )
  9. func (b *Builder) selectWriteTo(w Writer) error {
  10. if len(b.tableName) <= 0 {
  11. return errors.New("no table indicated")
  12. }
  13. if _, err := fmt.Fprint(w, "SELECT "); err != nil {
  14. return err
  15. }
  16. if len(b.selects) > 0 {
  17. for i, s := range b.selects {
  18. if _, err := fmt.Fprint(w, s); err != nil {
  19. return err
  20. }
  21. if i != len(b.selects)-1 {
  22. if _, err := fmt.Fprint(w, ","); err != nil {
  23. return err
  24. }
  25. }
  26. }
  27. } else {
  28. if _, err := fmt.Fprint(w, "*"); err != nil {
  29. return err
  30. }
  31. }
  32. if _, err := fmt.Fprintf(w, " FROM %s", b.tableName); err != nil {
  33. return err
  34. }
  35. for _, v := range b.joins {
  36. fmt.Fprintf(w, " %s JOIN %s ON ", v.joinType, v.joinTable)
  37. if err := v.joinCond.WriteTo(w); err != nil {
  38. return err
  39. }
  40. }
  41. if !b.cond.IsValid() {
  42. return nil
  43. }
  44. if _, err := fmt.Fprint(w, " WHERE "); err != nil {
  45. return err
  46. }
  47. return b.cond.WriteTo(w)
  48. }