2019-04-29 14:39:48 +02:00
|
|
|
package sqlbuilder
|
|
|
|
|
|
|
|
|
|
import "github.com/dropbox/godropbox/errors"
|
|
|
|
|
|
2019-05-07 19:06:21 +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 {
|
|
|
|
|
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 {
|
|
|
|
|
return errors.Newf("nil orderBy by clause.")
|
|
|
|
|
}
|
|
|
|
|
|
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-05-07 19:06:21 +02:00
|
|
|
func ASC(expression expression) orderByClause {
|
|
|
|
|
return &orderByClauseImpl{expression: expression, ascent: true}
|
2019-04-29 14:39:48 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
func DESC(expression expression) orderByClause {
|
|
|
|
|
return &orderByClauseImpl{expression: expression, ascent: false}
|
2019-04-29 14:39:48 +02:00
|
|
|
}
|