2019-04-29 14:39:48 +02:00
|
|
|
package sqlbuilder
|
|
|
|
|
|
2019-06-05 17:15:20 +02:00
|
|
|
import "errors"
|
2019-04-29 14:39:48 +02:00
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
type OrderByClause interface {
|
2019-05-08 13:47:01 +02:00
|
|
|
serializeAsOrderBy(statement statementType, out *queryData) error
|
2019-04-29 14:39:48 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
type orderByClauseImpl struct {
|
2019-06-04 12:10:23 +02:00
|
|
|
expression Expression
|
2019-04-29 14:39:48 +02:00
|
|
|
ascent bool
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-08 13:47:01 +02:00
|
|
|
func (o *orderByClauseImpl) serializeAsOrderBy(statement statementType, out *queryData) error {
|
2019-04-29 14:39:48 +02:00
|
|
|
if o.expression == nil {
|
2019-06-05 17:15:20 +02:00
|
|
|
return errors.New("nil orderBy by clause.")
|
2019-04-29 14:39:48 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-08 13:47:01 +02:00
|
|
|
if err := o.expression.serializeAsOrderBy(statement, out); err != nil {
|
2019-04-29 14:39:48 +02:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if o.ascent {
|
2019-05-08 13:47:01 +02:00
|
|
|
out.writeString(" ASC")
|
2019-04-29 14:39:48 +02:00
|
|
|
} else {
|
2019-05-08 13:47:01 +02:00
|
|
|
out.writeString(" DESC")
|
2019-04-29 14:39:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
func ASC(expression Expression) OrderByClause {
|
2019-05-07 19:06:21 +02:00
|
|
|
return &orderByClauseImpl{expression: expression, ascent: true}
|
2019-04-29 14:39:48 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
func DESC(expression Expression) OrderByClause {
|
2019-05-07 19:06:21 +02:00
|
|
|
return &orderByClauseImpl{expression: expression, ascent: false}
|
2019-04-29 14:39:48 +02:00
|
|
|
}
|