2019-03-31 09:17:28 +02:00
|
|
|
// Query building functions for expression components
|
|
|
|
|
package sqlbuilder
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type intervalExpression struct {
|
|
|
|
|
expressionInterfaceImpl
|
|
|
|
|
duration time.Duration
|
|
|
|
|
negative bool
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var intervalSep = ":"
|
|
|
|
|
|
2019-04-29 14:39:48 +02:00
|
|
|
func (c *intervalExpression) Serialize(out *queryData, options ...serializeOption) error {
|
2019-03-31 09:17:28 +02:00
|
|
|
hours := c.duration / time.Hour
|
|
|
|
|
minutes := (c.duration % time.Hour) / time.Minute
|
|
|
|
|
sec := (c.duration % time.Minute) / time.Second
|
|
|
|
|
msec := (c.duration % time.Second) / time.Microsecond
|
2019-04-29 14:39:48 +02:00
|
|
|
out.WriteString("INTERVAL '")
|
2019-03-31 09:17:28 +02:00
|
|
|
if c.negative {
|
2019-04-29 14:39:48 +02:00
|
|
|
out.WriteString("-")
|
2019-03-31 09:17:28 +02:00
|
|
|
}
|
2019-04-29 14:39:48 +02:00
|
|
|
out.WriteString(strconv.FormatInt(int64(hours), 10))
|
|
|
|
|
out.WriteString(intervalSep)
|
|
|
|
|
out.WriteString(strconv.FormatInt(int64(minutes), 10))
|
|
|
|
|
out.WriteString(intervalSep)
|
|
|
|
|
out.WriteString(strconv.FormatInt(int64(sec), 10))
|
|
|
|
|
out.WriteString(intervalSep)
|
|
|
|
|
out.WriteString(strconv.FormatInt(int64(msec), 10))
|
|
|
|
|
out.WriteString("' HOUR_MICROSECOND")
|
|
|
|
|
|
2019-03-31 09:17:28 +02:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Interval returns a representation of duration
|
|
|
|
|
// in a form "INTERVAL `hour:min:sec:microsec` HOUR_MICROSECOND"
|
|
|
|
|
func Interval(duration time.Duration) Expression {
|
|
|
|
|
negative := false
|
|
|
|
|
if duration < 0 {
|
|
|
|
|
negative = true
|
|
|
|
|
duration = -duration
|
|
|
|
|
}
|
|
|
|
|
return &intervalExpression{
|
|
|
|
|
duration: duration,
|
|
|
|
|
negative: negative,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var likeEscaper = strings.NewReplacer("_", "\\_", "%", "\\%")
|
|
|
|
|
|
|
|
|
|
func EscapeForLike(s string) string {
|
|
|
|
|
return likeEscaper.Replace(s)
|
|
|
|
|
}
|