Skip to content

Commit 56e6809

Browse files
committed
add: between and not between
1 parent a908abb commit 56e6809

File tree

4 files changed

+30
-13
lines changed

4 files changed

+30
-13
lines changed

constants/keyword.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
package constants
22

33
const (
4-
And = "AND"
5-
Or = "OR"
6-
In = "IN"
7-
Not = "NOT"
8-
Like = " LIKE "
9-
Eq = "="
10-
Ne = "<>"
11-
Gt = ">"
12-
Ge = ">="
13-
Lt = "<"
14-
Le = "<="
15-
Desc = "DESC"
16-
Asc = "ASC"
4+
And = "AND"
5+
Or = "OR"
6+
In = "IN"
7+
Not = "NOT"
8+
Like = " LIKE "
9+
Eq = "="
10+
Ne = "<>"
11+
Gt = ">"
12+
Ge = ">="
13+
Lt = "<"
14+
Le = "<="
15+
Between = "BETWEEN"
16+
Desc = "DESC"
17+
Asc = "ASC"
1718
)

gormplus/mapper.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ 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)
8586

8687
if len(q.DistinctColumns) > 0 {
8788
resultDb.Distinct(q.DistinctColumns)

gormplus/mapper_test.go

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

118118
func TestSelectList(t *testing.T) {
119119
q := Query[Test1]{}
120+
q.NotBetween("price", 100, 200)
120121
db, result := SelectList(&q)
121122
fmt.Println(db.RowsAffected)
122123
for _, v := range result {

gormplus/query.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,20 @@ func (q *Query[T]) In(column string, val ...any) *Query[T] {
7777
return q
7878
}
7979

80+
func (q *Query[T]) Between(column string, start, end any) *Query[T] {
81+
cond := fmt.Sprintf("%s %s ? and ? ", column, constants.Between)
82+
q.QueryBuilder.WriteString(cond)
83+
q.QueryArgs = append(q.QueryArgs, start, end)
84+
return q
85+
}
86+
87+
func (q *Query[T]) NotBetween(column string, start, end any) *Query[T] {
88+
cond := fmt.Sprintf("%s %s %s ? and ? ", column, constants.Not, constants.Between)
89+
q.QueryBuilder.WriteString(cond)
90+
q.QueryArgs = append(q.QueryArgs, start, end)
91+
return q
92+
}
93+
8094
func (q *Query[T]) Distinct(column ...string) *Query[T] {
8195
q.DistinctColumns = column
8296
return q

0 commit comments

Comments
 (0)