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 {
|
2020-07-17 06:24:24 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2021-07-14 10:51:24 +03:00
|
|
|
|
|
|
|
// CommaExpression represents a group of expressions separated by commas.
|
|
|
|
type CommaExpression struct {
|
|
|
|
Exprs []Expression
|
|
|
|
}
|
|
|
|
|
|
|
|
func (comma CommaExpression) Build(builder Builder) {
|
|
|
|
for idx, expr := range comma.Exprs {
|
|
|
|
if idx > 0 {
|
|
|
|
_, _ = builder.WriteString(", ")
|
|
|
|
}
|
|
|
|
expr.Build(builder)
|
|
|
|
}
|
|
|
|
}
|