You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ims/util/db/clause.go

140 lines
3.0 KiB

package db
import (
"gorm.io/gorm/clause"
)
type null struct {
Column any
}
func (n null) Build(builder clause.Builder) {
builder.WriteQuoted(n.Column)
builder.WriteString(" IS NULL")
}
func (n null) NegationBuild(builder clause.Builder) {
builder.WriteQuoted(n.Column)
builder.WriteString(" IS NOT NULL")
}
type between struct {
Column any
Less any
More any
}
func (b between) Build(builder clause.Builder) {
b.build(builder, " BETWEEN ")
}
func (b between) NegationBuild(builder clause.Builder) {
b.build(builder, " NOT BETWEEN ")
}
func (b between) build(builder clause.Builder, op string) {
builder.WriteQuoted(b.Column)
builder.WriteString(op)
builder.AddVar(builder, b.Less)
builder.WriteString(" And ")
builder.AddVar(builder, b.More)
}
type exists struct {
expr any
}
func (e exists) Build(builder clause.Builder) {
e.build(builder, "EXISTS")
}
func (e exists) NegationBuild(builder clause.Builder) {
e.build(builder, "NOT EXISTS")
}
func (e exists) build(builder clause.Builder, op string) {
builder.WriteString(op)
builder.WriteString(" (")
builder.AddVar(builder, e.expr)
builder.WriteString(")")
}
func Eq(col string, val any) clause.Expression {
return clause.Eq{Column: col, Value: val}
}
func Neq(col string, val any) clause.Expression {
return clause.Neq{Column: col, Value: val}
}
func Lt(col string, val any) clause.Expression {
return clause.Lt{Column: col, Value: val}
}
func Lte(col string, val any) clause.Expression {
return clause.Lte{Column: col, Value: val}
}
func Gt(col string, val any) clause.Expression {
return clause.Gt{Column: col, Value: val}
}
func Gte(col string, val any) clause.Expression {
return clause.Gte{Column: col, Value: val}
}
func Between(col string, less, more any) clause.Expression {
return between{col, less, more}
}
func NotBetween(col string, less, more any) clause.Expression {
return clause.Not(between{col, less, more})
}
func IsNull(col string) clause.Expression {
return null{col}
}
func NotNull(col string) clause.Expression {
return clause.Not(null{col})
}
func Like(col, tpl string) clause.Expression {
return clause.Like{Column: col, Value: "%" + tpl + "%"}
}
func NotLike(col, tpl string) clause.Expression {
return clause.Not(clause.Like{Column: col, Value: "%" + tpl + "%"})
}
func HasPrefix(col, prefix string) clause.Expression {
return clause.Like{Column: col, Value: prefix + "%"}
}
func NotPrefix(col, prefix string) clause.Expression {
return clause.Not(HasPrefix(col, prefix))
}
func HasSuffix(col, suffix string) clause.Expression {
return clause.Like{Column: col, Value: "%" + suffix}
}
func NotSuffix(col, suffix string) clause.Expression {
return clause.Not(HasSuffix(col, suffix))
}
func In(col string, values ...any) clause.Expression {
return clause.IN{Column: col, Values: values}
}
func NotIn(col string, values ...any) clause.Expression {
return clause.Not(clause.IN{Column: col, Values: values})
}
func Exists(expr any) clause.Expression {
return exists{expr}
}
func NotExists(expr any) clause.Expression {
return clause.Not(exists{expr})
}