builder_select.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 _, err := fmt.Fprint(w, " WHERE "); err != nil {
  42. return err
  43. }
  44. return b.cond.WriteTo(w)
  45. }