| 1 |
package clause |
| 2 |
|
| 3 |
// Select select attrs when querying, updating, creating |
| 4 |
type Select struct { |
| 5 |
Distinct bool |
| 6 |
Columns []Column |
| 7 |
Expression Expression |
| 8 |
} |
| 9 |
|
| 10 |
func (s Select) Name() string { |
| 11 |
return "SELECT" |
| 12 |
} |
| 13 |
|
| 14 |
func (s Select) Build(builder Builder) { |
| 15 |
if len(s.Columns) > 0 { |
| 16 |
if s.Distinct { |
| 17 |
builder.WriteString("DISTINCT ") |
| 18 |
} |
| 19 |
|
| 20 |
for idx, column := range s.Columns { |
| 21 |
if idx > 0 { |
| 22 |
builder.WriteByte(',') |
| 23 |
} |
| 24 |
builder.WriteQuoted(column) |
| 25 |
} |
| 26 |
} else { |
| 27 |
builder.WriteByte('*') |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
func (s Select) MergeClause(clause *Clause) { |
| 32 |
if s.Expression != nil { |
| 33 |
if s.Distinct { |
| 34 |
if expr, ok := s.Expression.(Expr); ok { |
| 35 |
expr.SQL = "DISTINCT " + expr.SQL |
| 36 |
clause.Expression = expr |
| 37 |
return |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
clause.Expression = s.Expression |
| 42 |
} else { |
| 43 |
clause.Expression = s |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
// CommaExpression represents a group of expressions separated by commas. |
| 48 |
type CommaExpression struct { |
| 49 |
Exprs []Expression |
| 50 |
} |
| 51 |
|
| 52 |
func (comma CommaExpression) Build(builder Builder) { |
| 53 |
for idx, expr := range comma.Exprs { |
| 54 |
if idx > 0 { |
| 55 |
_, _ = builder.WriteString(", ") |
| 56 |
} |
| 57 |
expr.Build(builder) |
| 58 |
} |
| 59 |
} |