gorm/clause/query.go

251 lines
4.8 KiB
Go
Raw Normal View History

2020-01-29 14:22:44 +03:00
package clause
import "strings"
2020-01-30 10:14:48 +03:00
////////////////////////////////////////////////////////////////////////////////
// Query Expressions
////////////////////////////////////////////////////////////////////////////////
2020-01-29 14:22:44 +03:00
2020-01-30 10:14:48 +03:00
type AddConditions []Expression
func (cs AddConditions) Build(builder Builder) {
2020-01-29 14:22:44 +03:00
for idx, c := range cs {
if idx > 0 {
builder.Write(" AND ")
}
c.Build(builder)
}
}
2020-01-30 10:14:48 +03:00
type ORConditions []Expression
2020-01-29 14:22:44 +03:00
2020-01-30 10:14:48 +03:00
func (cs ORConditions) Build(builder Builder) {
2020-01-29 14:22:44 +03:00
for idx, c := range cs {
if idx > 0 {
builder.Write(" OR ")
}
c.Build(builder)
}
}
2020-01-30 10:14:48 +03:00
type NotConditions []Expression
2020-01-29 14:22:44 +03:00
2020-01-30 10:14:48 +03:00
func (cs NotConditions) Build(builder Builder) {
2020-01-29 14:22:44 +03:00
for idx, c := range cs {
if idx > 0 {
builder.Write(" AND ")
}
2020-01-30 10:14:48 +03:00
if negationBuilder, ok := c.(NegationExpressionBuilder); ok {
2020-01-29 14:22:44 +03:00
negationBuilder.NegationBuild(builder)
} else {
builder.Write(" NOT ")
c.Build(builder)
}
}
}
2020-01-30 10:14:48 +03:00
// String raw sql for where
type String struct {
2020-01-29 14:22:44 +03:00
SQL string
Values []interface{}
}
2020-01-30 10:14:48 +03:00
func (str String) Build(builder Builder) {
sql := str.SQL
for _, v := range str.Values {
2020-01-29 14:22:44 +03:00
sql = strings.Replace(sql, " ? ", " "+builder.AddVar(v)+" ", 1)
}
builder.Write(sql)
}
// IN Whether a value is within a set of values
type IN struct {
Column interface{}
Values []interface{}
}
2020-01-30 10:14:48 +03:00
func (in IN) Build(builder Builder) {
2020-01-29 14:22:44 +03:00
builder.WriteQuoted(in.Column)
2020-01-29 22:03:06 +03:00
switch len(in.Values) {
case 0:
2020-01-29 14:22:44 +03:00
builder.Write(" IN (NULL)")
2020-01-29 22:03:06 +03:00
case 1:
builder.Write(" = ", builder.AddVar(in.Values...))
default:
2020-01-29 14:22:44 +03:00
builder.Write(" IN (", builder.AddVar(in.Values...), ")")
}
}
2020-01-30 10:14:48 +03:00
func (in IN) NegationBuild(builder Builder) {
2020-01-29 22:03:06 +03:00
switch len(in.Values) {
case 0:
case 1:
builder.Write(" <> ", builder.AddVar(in.Values...))
default:
2020-01-29 14:22:44 +03:00
builder.Write(" NOT IN (", builder.AddVar(in.Values...), ")")
}
}
// Eq equal to for where
type Eq struct {
Column interface{}
Value interface{}
}
2020-01-30 10:14:48 +03:00
func (eq Eq) Build(builder Builder) {
2020-01-29 14:22:44 +03:00
builder.WriteQuoted(eq.Column)
if eq.Value == nil {
builder.Write(" IS NULL")
} else {
builder.Write(" = ", builder.AddVar(eq.Value))
}
}
2020-01-30 10:14:48 +03:00
func (eq Eq) NegationBuild(builder Builder) {
2020-01-29 14:22:44 +03:00
Neq{eq.Column, eq.Value}.Build(builder)
}
// Neq not equal to for where
type Neq struct {
Column interface{}
Value interface{}
}
2020-01-30 10:14:48 +03:00
func (neq Neq) Build(builder Builder) {
2020-01-29 14:22:44 +03:00
builder.WriteQuoted(neq.Column)
if neq.Value == nil {
builder.Write(" IS NOT NULL")
} else {
builder.Write(" <> ", builder.AddVar(neq.Value))
}
}
2020-01-30 10:14:48 +03:00
func (neq Neq) NegationBuild(builder Builder) {
2020-01-29 14:22:44 +03:00
Eq{neq.Column, neq.Value}.Build(builder)
}
// Gt greater than for where
type Gt struct {
Column interface{}
Value interface{}
}
2020-01-30 10:14:48 +03:00
func (gt Gt) Build(builder Builder) {
2020-01-29 14:22:44 +03:00
builder.WriteQuoted(gt.Column)
builder.Write(" > ", builder.AddVar(gt.Value))
}
2020-01-30 10:14:48 +03:00
func (gt Gt) NegationBuild(builder Builder) {
2020-01-29 14:22:44 +03:00
Lte{gt.Column, gt.Value}.Build(builder)
}
// Gte greater than or equal to for where
type Gte struct {
Column interface{}
Value interface{}
}
2020-01-30 10:14:48 +03:00
func (gte Gte) Build(builder Builder) {
2020-01-29 14:22:44 +03:00
builder.WriteQuoted(gte.Column)
builder.Write(" >= ", builder.AddVar(gte.Value))
}
2020-01-30 10:14:48 +03:00
func (gte Gte) NegationBuild(builder Builder) {
2020-01-29 14:22:44 +03:00
Lt{gte.Column, gte.Value}.Build(builder)
}
// Lt less than for where
type Lt struct {
Column interface{}
Value interface{}
}
2020-01-30 10:14:48 +03:00
func (lt Lt) Build(builder Builder) {
2020-01-29 14:22:44 +03:00
builder.WriteQuoted(lt.Column)
builder.Write(" < ", builder.AddVar(lt.Value))
}
2020-01-30 10:14:48 +03:00
func (lt Lt) NegationBuild(builder Builder) {
2020-01-29 14:22:44 +03:00
Gte{lt.Column, lt.Value}.Build(builder)
}
// Lte less than or equal to for where
type Lte struct {
Column interface{}
Value interface{}
}
2020-01-30 10:14:48 +03:00
func (lte Lte) Build(builder Builder) {
2020-01-29 14:22:44 +03:00
builder.WriteQuoted(lte.Column)
builder.Write(" <= ", builder.AddVar(lte.Value))
}
2020-01-30 10:14:48 +03:00
func (lte Lte) NegationBuild(builder Builder) {
2020-01-29 14:22:44 +03:00
Gt{lte.Column, lte.Value}.Build(builder)
}
// Like whether string matches regular expression
type Like struct {
Column interface{}
Value interface{}
}
2020-01-30 10:14:48 +03:00
func (like Like) Build(builder Builder) {
2020-01-29 14:22:44 +03:00
builder.WriteQuoted(like.Column)
builder.Write(" LIKE ", builder.AddVar(like.Value))
}
2020-01-30 10:14:48 +03:00
func (like Like) NegationBuild(builder Builder) {
2020-01-29 14:22:44 +03:00
builder.WriteQuoted(like.Column)
builder.Write(" NOT LIKE ", builder.AddVar(like.Value))
}
2020-01-29 22:03:06 +03:00
// Map
type Map map[interface{}]interface{}
2020-01-30 10:14:48 +03:00
func (m Map) Build(builder Builder) {
2020-01-29 22:03:06 +03:00
// TODO
}
2020-01-30 10:14:48 +03:00
func (m Map) NegationBuild(builder Builder) {
2020-01-29 22:03:06 +03:00
// TODO
}
// Attrs
type Attrs struct {
Value interface{}
Select []string
Omit []string
}
2020-01-30 10:14:48 +03:00
func (attrs Attrs) Build(builder Builder) {
2020-01-29 22:03:06 +03:00
// TODO
// builder.WriteQuoted(like.Column)
// builder.Write(" LIKE ", builder.AddVar(like.Value))
}
2020-01-30 10:14:48 +03:00
func (attrs Attrs) NegationBuild(builder Builder) {
2020-01-29 22:03:06 +03:00
// TODO
}
// ID
type ID struct {
Value []interface{}
}
2020-01-30 10:14:48 +03:00
func (id ID) Build(builder Builder) {
2020-01-29 22:03:06 +03:00
if len(id.Value) == 1 {
}
// TODO
// builder.WriteQuoted(like.Column)
// builder.Write(" LIKE ", builder.AddVar(like.Value))
}
2020-01-30 10:14:48 +03:00
func (id ID) NegationBuild(builder Builder) {
2020-01-29 22:03:06 +03:00
// TODO
}