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
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-06 14:01:50 +02:00
|
|
|
const intervalSep = ":"
|
2019-03-31 09:17:28 +02:00
|
|
|
|
2019-05-08 13:47:01 +02:00
|
|
|
func (c *intervalExpression) serialize(statement statementType, out *queryData) error {
|
|
|
|
|
out.writeString("INTERVAL '")
|
2019-05-06 14:01:50 +02:00
|
|
|
|
|
|
|
|
duration := c.duration
|
|
|
|
|
|
|
|
|
|
if duration < 0 {
|
|
|
|
|
duration = -duration
|
2019-05-08 13:47:01 +02:00
|
|
|
out.writeString("-")
|
2019-03-31 09:17:28 +02:00
|
|
|
}
|
2019-05-06 14:01:50 +02:00
|
|
|
|
|
|
|
|
hours := duration / time.Hour
|
|
|
|
|
minutes := (duration % time.Hour) / time.Minute
|
|
|
|
|
sec := (duration % time.Minute) / time.Second
|
|
|
|
|
msec := (duration % time.Second) / time.Microsecond
|
|
|
|
|
|
2019-05-08 13:47:01 +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-04-29 14:39:48 +02:00
|
|
|
|
2019-03-31 09:17:28 +02:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-06 14:01:50 +02:00
|
|
|
//// Interval returns a representation of duration
|
2019-05-07 19:06:21 +02:00
|
|
|
//func Interval(duration time.Duration) expression {
|
2019-05-06 14:01:50 +02:00
|
|
|
// intervalExp := &intervalExpression{
|
|
|
|
|
// duration: duration,
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// intervalExp.expressionInterfaceImpl.parent = intervalExp
|
|
|
|
|
//
|
|
|
|
|
// return intervalExp
|
|
|
|
|
//}
|
2019-03-31 09:17:28 +02:00
|
|
|
|
|
|
|
|
var likeEscaper = strings.NewReplacer("_", "\\_", "%", "\\%")
|
|
|
|
|
|
|
|
|
|
func EscapeForLike(s string) string {
|
|
|
|
|
return likeEscaper.Replace(s)
|
|
|
|
|
}
|