aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/go-xorm/builder/builder_select.go
blob: deaacbd024313e2e9096201b23883426d9e4bcd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2016 The Xorm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package builder

import (
	"errors"
	"fmt"
)

func (b *Builder) selectWriteTo(w Writer) error {
	if len(b.tableName) <= 0 {
		return errors.New("no table indicated")
	}

	if _, err := fmt.Fprint(w, "SELECT "); err != nil {
		return err
	}
	if len(b.selects) > 0 {
		for i, s := range b.selects {
			if _, err := fmt.Fprint(w, s); err != nil {
				return err
			}
			if i != len(b.selects)-1 {
				if _, err := fmt.Fprint(w, ","); err != nil {
					return err
				}
			}
		}
	} else {
		if _, err := fmt.Fprint(w, "*"); err != nil {
			return err
		}
	}

	if _, err := fmt.Fprint(w, " FROM ", b.tableName); err != nil {
		return err
	}

	for _, v := range b.joins {
		if _, err := fmt.Fprintf(w, " %s JOIN %s ON ", v.joinType, v.joinTable); err != nil {
			return err
		}

		if err := v.joinCond.WriteTo(w); err != nil {
			return err
		}
	}

	if b.cond.IsValid() {
		if _, err := fmt.Fprint(w, " WHERE "); err != nil {
			return err
		}

		if err := b.cond.WriteTo(w); err != nil {
			return err
		}
	}

	if len(b.groupBy) > 0 {
		if _, err := fmt.Fprint(w, " GROUP BY ", b.groupBy); err != nil {
			return err
		}
	}

	if len(b.having) > 0 {
		if _, err := fmt.Fprint(w, " HAVING ", b.having); err != nil {
			return err
		}
	}

	if len(b.orderBy) > 0 {
		if _, err := fmt.Fprint(w, " ORDER BY ", b.orderBy); err != nil {
			return err
		}
	}

	return nil
}

// OrderBy orderBy SQL
func (b *Builder) OrderBy(orderBy string) *Builder {
	b.orderBy = orderBy
	return b
}

// GroupBy groupby SQL
func (b *Builder) GroupBy(groupby string) *Builder {
	b.groupBy = groupby
	return b
}

// Having having SQL
func (b *Builder) Having(having string) *Builder {
	b.having = having
	return b
}