Skip to content

Commit 4edab71

Browse files
committed
feat: Complete the basic query
1 parent 392e974 commit 4edab71

File tree

9 files changed

+362
-2
lines changed

9 files changed

+362
-2
lines changed

cmd/main.go

Lines changed: 0 additions & 1 deletion
This file was deleted.
File renamed without changes.

constants/keyword.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package constants
2+
3+
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+
)

go.mod

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
11
module gorm-plus
22

33
go 1.18
4+
5+
require (
6+
gorm.io/driver/mysql v1.4.4
7+
gorm.io/gorm v1.24.2
8+
)
9+
10+
require (
11+
github.com/go-sql-driver/mysql v1.6.0 // indirect
12+
github.com/jinzhu/inflection v1.0.0 // indirect
13+
github.com/jinzhu/now v1.1.5 // indirect
14+
)

go.sum

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
2+
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
3+
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
4+
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
5+
github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
6+
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
7+
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
8+
gorm.io/driver/mysql v1.4.4 h1:MX0K9Qvy0Na4o7qSC/YI7XxqUw5KDw01umqgID+svdQ=
9+
gorm.io/driver/mysql v1.4.4/go.mod h1:BCg8cKI+R0j/rZRQxeKis/forqRwRSYOR8OM3Wo6hOM=
10+
gorm.io/gorm v1.23.8/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
11+
gorm.io/gorm v1.24.2 h1:9wR6CFD+G8nOusLdvkZelOEhpJVwwHzpQOUM+REd6U0=
12+
gorm.io/gorm v1.24.2/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA=

mapper/mapper.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package mapper
2+
3+
import (
4+
"gorm.io/gorm"
5+
)
6+
7+
var GormDb *gorm.DB
8+
9+
func Insert[T any](entity *T) *gorm.DB {
10+
resultDb := GormDb.Create(&entity)
11+
return resultDb
12+
}
13+
14+
func InsertMigrate[T any](entity *T) (*gorm.DB, error) {
15+
if err := GormDb.AutoMigrate(new(T)); err != nil {
16+
return nil, err
17+
}
18+
resultDb := GormDb.Create(&entity)
19+
return resultDb, nil
20+
}
21+
22+
func InsertBatch[T any](entities ...*T) *gorm.DB {
23+
resultDb := GormDb.Create(&entities)
24+
return resultDb
25+
}
26+
27+
func InsertBatchMigrate[T any](entities ...*T) (*gorm.DB, error) {
28+
if err := GormDb.AutoMigrate(new(T)); err != nil {
29+
return nil, err
30+
}
31+
resultDb := GormDb.Create(&entities)
32+
return resultDb, nil
33+
}
34+
35+
func DeleteById[T any](id any) *gorm.DB {
36+
resultDb := GormDb.Delete(new(T), id)
37+
return resultDb
38+
}
39+
40+
func DeleteByIds[T any](ids ...any) *gorm.DB {
41+
var entities []T
42+
resultDb := GormDb.Delete(&entities, ids)
43+
return resultDb
44+
}
45+
46+
func Delete[T any](q *Query[T]) *gorm.DB {
47+
var entity T
48+
resultDb := GormDb.Where(q.QueryBuilder.String(), q.Args...).Delete(&entity)
49+
return resultDb
50+
}
51+
52+
func UpdateById[T any](id any, entity *T) *gorm.DB {
53+
var e T
54+
GormDb.First(&e, id)
55+
resultDb := GormDb.Model(&e).Updates(entity)
56+
return resultDb
57+
}
58+
59+
func Update[T any](q *Query[T], entity *T) *gorm.DB {
60+
resultDb := GormDb.Where(q.QueryBuilder.String(), q.Args...).Updates(entity)
61+
return resultDb
62+
}
63+
64+
func SelectById[T any](id any) (*gorm.DB, T) {
65+
var entity T
66+
resultDb := GormDb.First(&entity, id)
67+
return resultDb, entity
68+
}
69+
70+
func SelectByIds[T any](ids ...any) (*gorm.DB, []T) {
71+
var results []T
72+
resultDb := GormDb.Find(&results, ids)
73+
return resultDb, results
74+
}
75+
76+
func SelectOne[T any](q *Query[T]) (*gorm.DB, T) {
77+
var entity T
78+
resultDb := GormDb.Where(q.QueryBuilder.String(), q.Args...).First(&entity)
79+
return resultDb, entity
80+
}
81+
82+
func SelectList[T any](q *Query[T]) (*gorm.DB, []T) {
83+
var results []T
84+
resultDb := GormDb.Where(q.QueryBuilder.String(), q.Args...).Find(&results)
85+
return resultDb, results
86+
}
87+
88+
func SelectCount[T any](q *Query[T]) (*gorm.DB, int64) {
89+
var count int64
90+
resultDb := GormDb.Model(new(T)).Where(q.QueryBuilder.String(), q.Args...).Count(&count)
91+
return resultDb, count
92+
}

mapper/mapper_test.go

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package mapper
2+
3+
import (
4+
"fmt"
5+
"gorm.io/driver/mysql"
6+
"gorm.io/gorm"
7+
"testing"
8+
)
9+
10+
func init() {
11+
dsn := "root:root-abcd-1234@tcp(127.0.0.1:3306)/test?charset=utf8mb4&parseTime=True&loc=Local"
12+
var err error
13+
GormDb, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
14+
if err != nil {
15+
fmt.Println(err)
16+
}
17+
}
18+
19+
type Test1 struct {
20+
gorm.Model
21+
Code string
22+
Price uint
23+
}
24+
25+
func TestSave(t *testing.T) {
26+
test1 := Test1{Code: "D455", Price: 100}
27+
resultDb := Insert(&test1)
28+
fmt.Println(resultDb)
29+
fmt.Println(test1)
30+
}
31+
32+
func TestSaveMigrate(t *testing.T) {
33+
test1 := Test1{Code: "D455", Price: 100}
34+
resultDb, err := InsertMigrate(&test1)
35+
if err != nil {
36+
fmt.Println(err)
37+
}
38+
fmt.Println(resultDb)
39+
fmt.Println(test1)
40+
}
41+
42+
func TestBatchSave(t *testing.T) {
43+
test1 := Test1{Code: "D466", Price: 100}
44+
test2 := Test1{Code: "D466", Price: 100}
45+
resultDb := InsertBatch(&test1, &test2)
46+
fmt.Println(resultDb)
47+
fmt.Println(test1)
48+
fmt.Println(test2)
49+
}
50+
51+
func TestSaveBatchMigrate(t *testing.T) {
52+
test1 := Test1{Code: "D477", Price: 100}
53+
test2 := Test1{Code: "D477", Price: 100}
54+
resultDb, err := InsertBatchMigrate(&test1, &test2)
55+
if err != nil {
56+
fmt.Println(err)
57+
}
58+
fmt.Println(resultDb)
59+
fmt.Println(test1)
60+
fmt.Println(test2)
61+
}
62+
63+
func TestDeleteById(t *testing.T) {
64+
resultDb := DeleteById[Test1](1)
65+
fmt.Println(resultDb)
66+
}
67+
68+
func TestDeleteByIds(t *testing.T) {
69+
resultDb := DeleteByIds[Test1](4, 5)
70+
fmt.Println(resultDb)
71+
}
72+
73+
func TestDelete(t *testing.T) {
74+
q := Query[Test1]{}
75+
q.Eq("code", "D1").Eq("price", 100)
76+
resultDb := Delete(&q)
77+
fmt.Println(resultDb)
78+
}
79+
80+
func TestUpdateById(t *testing.T) {
81+
test1 := Test1{Code: "777"}
82+
resultDb := UpdateById(6, &test1)
83+
fmt.Println(resultDb)
84+
}
85+
86+
func TestUpdate(t *testing.T) {
87+
q := Query[Test1]{}
88+
q.Eq("code", "D42").Eq("price", 100)
89+
test1 := Test1{Code: "888"}
90+
resultDb := Update(&q, &test1)
91+
fmt.Println(resultDb)
92+
}
93+
94+
func TestSelectById(t *testing.T) {
95+
db, result := SelectById[Test1](1)
96+
fmt.Println(db)
97+
fmt.Println(result)
98+
}
99+
100+
func TestSelectByIds(t *testing.T) {
101+
db, result := SelectByIds[Test1](1, 2)
102+
fmt.Println(db)
103+
fmt.Println(result)
104+
}
105+
106+
func TestSelectOne(t *testing.T) {
107+
q := Query[Test1]{}
108+
q.Eq("code", "D42").Eq("price", 100)
109+
db, result := SelectOne(&q)
110+
fmt.Println(db)
111+
fmt.Println(result)
112+
}
113+
114+
func TestSelectList(t *testing.T) {
115+
q := Query[Test1]{}
116+
q.Eq("price", 100)
117+
db, result := SelectList(&q)
118+
fmt.Println(db.RowsAffected)
119+
fmt.Println(result)
120+
}
121+
122+
func TestSelectCount(t *testing.T) {
123+
q := Query[Test1]{}
124+
q.Eq("price", 100)
125+
db, count := SelectCount(&q)
126+
fmt.Println(db)
127+
fmt.Println(count)
128+
}

mapper/query.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package mapper
2+
3+
import (
4+
"fmt"
5+
"gorm-plus/constants"
6+
"strings"
7+
)
8+
9+
type Query[T any] struct {
10+
Columns []string
11+
QueryBuilder strings.Builder
12+
Args []any
13+
LastCond string
14+
}
15+
16+
func (q *Query[T]) Eq(column string, val any) *Query[T] {
17+
q.addCond(column, val, constants.Eq)
18+
return q
19+
}
20+
21+
func (q *Query[T]) Ne(column string, val any) *Query[T] {
22+
q.addCond(column, val, constants.Ne)
23+
return q
24+
}
25+
26+
func (q *Query[T]) Gt(column string, val any) *Query[T] {
27+
q.addCond(column, val, constants.Gt)
28+
return q
29+
}
30+
31+
func (q *Query[T]) Ge(column string, val any) *Query[T] {
32+
q.addCond(column, val, constants.Ge)
33+
return q
34+
}
35+
36+
func (q *Query[T]) Lt(column string, val any) *Query[T] {
37+
q.addCond(column, val, constants.Lt)
38+
return q
39+
}
40+
41+
func (q *Query[T]) Le(column string, val any) *Query[T] {
42+
q.addCond(column, val, constants.Le)
43+
return q
44+
}
45+
46+
func (q *Query[T]) Like(column string, val any) *Query[T] {
47+
s := val.(string)
48+
q.addCond(column, "%"+s+"%", constants.Like)
49+
return q
50+
}
51+
52+
func (q *Query[T]) NotLike(column string, val any) *Query[T] {
53+
s := val.(string)
54+
q.addCond(column, "%"+s+"%", constants.Not+constants.Like)
55+
return q
56+
}
57+
58+
func (q *Query[T]) LikeLeft(column string, val any) *Query[T] {
59+
s := val.(string)
60+
q.addCond(column, "%"+s, constants.Like)
61+
return q
62+
}
63+
64+
func (q *Query[T]) LikeRight(column string, val any) *Query[T] {
65+
s := val.(string)
66+
q.addCond(column, s+"%", constants.Like)
67+
return q
68+
}
69+
70+
func (q *Query[T]) In(column string, val ...any) *Query[T] {
71+
q.addCond(column, val, constants.In)
72+
return q
73+
}
74+
75+
func (q *Query[T]) And() *Query[T] {
76+
q.QueryBuilder.WriteString(constants.And)
77+
q.QueryBuilder.WriteString(" ")
78+
q.LastCond = constants.And
79+
return q
80+
}
81+
82+
func (q *Query[T]) Or() *Query[T] {
83+
q.QueryBuilder.WriteString(constants.Or)
84+
q.QueryBuilder.WriteString(" ")
85+
q.LastCond = constants.Or
86+
return q
87+
}
88+
89+
func (q *Query[T]) Select(columns ...string) *Query[T] {
90+
q.Columns = append(q.Columns, columns...)
91+
return q
92+
}
93+
94+
func (q *Query[T]) addCond(column string, val any, condType string) {
95+
if q.LastCond != constants.And && q.LastCond != constants.Or && q.QueryBuilder.Len() > 0 {
96+
q.QueryBuilder.WriteString(constants.And)
97+
q.QueryBuilder.WriteString(" ")
98+
}
99+
cond := fmt.Sprintf("%s %s ?", column, condType)
100+
q.QueryBuilder.WriteString(cond)
101+
q.QueryBuilder.WriteString(" ")
102+
q.LastCond = ""
103+
q.Args = append(q.Args, val)
104+
}

pkg/mapper/mapper.go

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)