Skip to content

Commit

Permalink
1.丰富Where相关方法
Browse files Browse the repository at this point in the history
2.丰富Where相关方法
  • Loading branch information
leil committed Jan 1, 2024
1 parent 1172ed0 commit 3828ab2
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 0 deletions.
136 changes: 136 additions & 0 deletions tableSet.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,78 @@ func (receiver *TableSet[Table]) Where(query any, args ...any) *TableSet[Table]
return session
}

// WhereEq 条件
func (receiver *TableSet[Table]) WhereEq(columnName any, args any) *TableSet[Table] {
session := receiver.getOrCreateSession()
session.whereList.Add(whereQuery{
query: fmt.Sprintf("%v = ?", columnName),
args: []any{args},
})
return session
}

// WhereGt 大于条件
func (receiver *TableSet[Table]) WhereGt(columnName any, args any) *TableSet[Table] {
session := receiver.getOrCreateSession()
session.whereList.Add(whereQuery{
query: fmt.Sprintf("%v > ?", columnName),
args: []any{args},
})
return session
}

// WhereGte 大于等于条件
func (receiver *TableSet[Table]) WhereGte(columnName any, args any) *TableSet[Table] {
session := receiver.getOrCreateSession()
session.whereList.Add(whereQuery{
query: fmt.Sprintf("%v >= ?", columnName),
args: []any{args},
})
return session
}

// WhereLt 小于条件
func (receiver *TableSet[Table]) WhereLt(columnName any, args any) *TableSet[Table] {
session := receiver.getOrCreateSession()
session.whereList.Add(whereQuery{
query: fmt.Sprintf("%v < ?", columnName),
args: []any{args},
})
return session
} // WhereLte 小于等于条件
func (receiver *TableSet[Table]) WhereLte(columnName any, args any) *TableSet[Table] {
session := receiver.getOrCreateSession()
session.whereList.Add(whereQuery{
query: fmt.Sprintf("%v <= ?", columnName),
args: []any{args},
})
return session
} // WhereIn in条件
func (receiver *TableSet[Table]) WhereIn(columnName any, args ...any) *TableSet[Table] {
session := receiver.getOrCreateSession()
session.whereList.Add(whereQuery{
query: fmt.Sprintf("%v in ?", columnName),
args: args,
})
return session
} // WhereLike like条件("%?%")
func (receiver *TableSet[Table]) WhereLike(columnName any, args any) *TableSet[Table] {
session := receiver.getOrCreateSession()
session.whereList.Add(whereQuery{
query: fmt.Sprintf("%v like ?", columnName),
args: []any{fmt.Sprintf("%%%v%%", args)},
})
return session
} // WhereEq between条件(>= and <=)
func (receiver *TableSet[Table]) WhereBetween(columnName any, min, max any) *TableSet[Table] {
session := receiver.getOrCreateSession()
session.whereList.Add(whereQuery{
query: fmt.Sprintf("%v >= ? and %v <= ?", columnName, columnName),
args: []any{min, max},
})
return session
}

// WhereIf 当conditional==true时,使用条件
func (receiver *TableSet[Table]) WhereIf(conditional bool, query any, args ...any) *TableSet[Table] {
if !conditional {
Expand All @@ -215,6 +287,70 @@ func (receiver *TableSet[Table]) WhereIf(conditional bool, query any, args ...an
return receiver.Where(query, args...)
}

// WhereEqIf 当conditional==true时,使用等于条件
func (receiver *TableSet[Table]) WhereEqIf(conditional bool, columnName any, args any) *TableSet[Table] {
if !conditional {
return receiver
}
return receiver.WhereEq(columnName, args)
}

// WhereGtIf 当conditional==true时,使用大于条件
func (receiver *TableSet[Table]) WhereGtIf(conditional bool, columnName any, args any) *TableSet[Table] {
if !conditional {
return receiver
}
return receiver.WhereGt(columnName, args)
}

// WhereGteIf 当conditional==true时,使用大于等于条件
func (receiver *TableSet[Table]) WhereGteIf(conditional bool, columnName any, args any) *TableSet[Table] {
if !conditional {
return receiver
}
return receiver.WhereGte(columnName, args)
}

// WhereLtIf 当conditional==true时,使用小于条件
func (receiver *TableSet[Table]) WhereLtIf(conditional bool, columnName any, args any) *TableSet[Table] {
if !conditional {
return receiver
}
return receiver.WhereLt(columnName, args)
}

// WhereLteIf 当conditional==true时,使用小于等于条件
func (receiver *TableSet[Table]) WhereLteIf(conditional bool, columnName any, args any) *TableSet[Table] {
if !conditional {
return receiver
}
return receiver.WhereLte(columnName, args)
}

// WhereInIf 当conditional==true时,使用in条件
func (receiver *TableSet[Table]) WhereInIf(conditional bool, columnName any, args ...any) *TableSet[Table] {
if !conditional {
return receiver
}
return receiver.WhereIn(columnName, args)
}

// WhereLikeIf 当conditional==true时,使用like条件("%?%"匹配)
func (receiver *TableSet[Table]) WhereLikeIf(conditional bool, columnName any, args any) *TableSet[Table] {
if !conditional {
return receiver
}
return receiver.WhereLike(columnName, args)
}

// WhereBetweenIf 当conditional==true时,使用between条件(>=and<=)
func (receiver *TableSet[Table]) WhereBetweenIf(conditional bool, columnName any, min, max any) *TableSet[Table] {
if !conditional {
return receiver
}
return receiver.WhereBetween(columnName, min, max)
}

// WhereIgnoreLessZero 条件,自动忽略小于等于0的
func (receiver *TableSet[Table]) WhereIgnoreLessZero(query any, val int) *TableSet[Table] {
session := receiver.getOrCreateSession()
Expand Down
5 changes: 5 additions & 0 deletions test/tableSet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ func TestTableSet(t *testing.T) {
assert.Less(t, 1, lst.First().Id)
})

// 测试where、whereIf
t.Run("where、whereIf", func(t *testing.T) {

})

// 测试排序
t.Run("asc", func(t *testing.T) {
lst := context.User.Asc("Age").ToList()
Expand Down

0 comments on commit 3828ab2

Please sign in to comment.