gorm/clause/select.go

46 lines
794 B
Go
Raw Permalink Normal View History

2020-02-02 09:40:44 +03:00
package clause
// Select select attrs when querying, updating, creating
type Select struct {
2020-06-05 14:19:08 +03:00
Distinct bool
2020-02-16 08:45:27 +03:00
Columns []Column
Expression Expression
2020-02-02 09:40:44 +03:00
}
2020-02-04 03:56:15 +03:00
func (s Select) Name() string {
return "SELECT"
2020-02-02 09:40:44 +03:00
}
func (s Select) Build(builder Builder) {
2020-02-07 18:45:35 +03:00
if len(s.Columns) > 0 {
2020-06-05 14:19:08 +03:00
if s.Distinct {
2020-07-05 17:12:52 +03:00
builder.WriteString("DISTINCT ")
2020-06-05 14:19:08 +03:00
}
2020-02-07 18:45:35 +03:00
for idx, column := range s.Columns {
2020-02-02 09:40:44 +03:00
if idx > 0 {
builder.WriteByte(',')
}
builder.WriteQuoted(column)
}
} else {
builder.WriteByte('*')
}
}
2020-02-07 18:45:35 +03:00
func (s Select) MergeClause(clause *Clause) {
2020-02-16 08:45:27 +03:00
if s.Expression != nil {
if s.Distinct {
if expr, ok := s.Expression.(Expr); ok {
expr.SQL = "DISTINCT " + expr.SQL
clause.Expression = expr
return
}
}
2020-02-16 08:45:27 +03:00
clause.Expression = s.Expression
} else {
clause.Expression = s
2020-02-02 09:40:44 +03:00
}
}