Skip to content

Commit 9766434

Browse files
committed
feat: add in and not in
1 parent 56e6809 commit 9766434

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

constants/keyword.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const (
55
Or = "OR"
66
In = "IN"
77
Not = "NOT"
8-
Like = " LIKE "
8+
Like = "LIKE"
99
Eq = "="
1010
Ne = "<>"
1111
Gt = ">"

gormplus/mapper.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ func SelectOne[T any](q *Query[T]) (*gorm.DB, T) {
8282
func SelectList[T any](q *Query[T]) (*gorm.DB, []T) {
8383
var results []T
8484
resultDb := GormDb.Model(new(T))
85-
//resultDb.Where("price between ? and ?", 100, 200)
8685

8786
if len(q.DistinctColumns) > 0 {
8887
resultDb.Distinct(q.DistinctColumns)

gormplus/mapper_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func TestSelectOne(t *testing.T) {
117117

118118
func TestSelectList(t *testing.T) {
119119
q := Query[Test1]{}
120-
q.NotBetween("price", 100, 200)
120+
q.In("code", "D455")
121121
db, result := SelectList(&q)
122122
fmt.Println(db.RowsAffected)
123123
for _, v := range result {

gormplus/query.go

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (q *Query[T]) Like(column string, val any) *Query[T] {
5656

5757
func (q *Query[T]) NotLike(column string, val any) *Query[T] {
5858
s := val.(string)
59-
q.addCond(column, "%"+s+"%", constants.Not+constants.Like)
59+
q.addCond(column, "%"+s+"%", constants.Not+" "+constants.Like)
6060
return q
6161
}
6262

@@ -72,19 +72,40 @@ func (q *Query[T]) LikeRight(column string, val any) *Query[T] {
7272
return q
7373
}
7474

75+
func (q *Query[T]) IsNull(column string) *Query[T] {
76+
q.buildAndIfNeed()
77+
cond := fmt.Sprintf("%s is null", column)
78+
q.QueryBuilder.WriteString(cond)
79+
return q
80+
}
81+
82+
func (q *Query[T]) IsNotNull(column string) *Query[T] {
83+
q.buildAndIfNeed()
84+
cond := fmt.Sprintf("%s is not null", column)
85+
q.QueryBuilder.WriteString(cond)
86+
return q
87+
}
88+
7589
func (q *Query[T]) In(column string, val ...any) *Query[T] {
7690
q.addCond(column, val, constants.In)
7791
return q
7892
}
7993

94+
func (q *Query[T]) NotIn(column string, val ...any) *Query[T] {
95+
q.addCond(column, val, constants.Not+" "+constants.In)
96+
return q
97+
}
98+
8099
func (q *Query[T]) Between(column string, start, end any) *Query[T] {
100+
q.buildAndIfNeed()
81101
cond := fmt.Sprintf("%s %s ? and ? ", column, constants.Between)
82102
q.QueryBuilder.WriteString(cond)
83103
q.QueryArgs = append(q.QueryArgs, start, end)
84104
return q
85105
}
86106

87107
func (q *Query[T]) NotBetween(column string, start, end any) *Query[T] {
108+
q.buildAndIfNeed()
88109
cond := fmt.Sprintf("%s %s %s ? and ? ", column, constants.Not, constants.Between)
89110
q.QueryBuilder.WriteString(cond)
90111
q.QueryArgs = append(q.QueryArgs, start, end)
@@ -142,17 +163,21 @@ func (q *Query[T]) Having(having string, args ...any) *Query[T] {
142163
}
143164

144165
func (q *Query[T]) addCond(column string, val any, condType string) {
145-
if q.LastCond != constants.And && q.LastCond != constants.Or && q.QueryBuilder.Len() > 0 {
146-
q.QueryBuilder.WriteString(constants.And)
147-
q.QueryBuilder.WriteString(" ")
148-
}
166+
q.buildAndIfNeed()
149167
cond := fmt.Sprintf("%s %s ?", column, condType)
150168
q.QueryBuilder.WriteString(cond)
151169
q.QueryBuilder.WriteString(" ")
152170
q.LastCond = ""
153171
q.QueryArgs = append(q.QueryArgs, val)
154172
}
155173

174+
func (q *Query[T]) buildAndIfNeed() {
175+
if q.LastCond != constants.And && q.LastCond != constants.Or && q.QueryBuilder.Len() > 0 {
176+
q.QueryBuilder.WriteString(constants.And)
177+
q.QueryBuilder.WriteString(" ")
178+
}
179+
}
180+
156181
func (q *Query[T]) buildOrder(orderType string, columns ...string) {
157182
for _, v := range columns {
158183
if q.OrderBuilder.Len() > 0 {

0 commit comments

Comments
 (0)