Skip to content

Commit 36a1bad

Browse files
committed
feat: add select example
1 parent 1d0c57b commit 36a1bad

File tree

5 files changed

+221
-102
lines changed

5 files changed

+221
-102
lines changed

example/example_insert.go

Lines changed: 0 additions & 70 deletions
This file was deleted.

example/example_insert_test.go

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,71 @@
11
package example
22

3-
import "testing"
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"github.com/gorm-plus/gorm-plus/gormplus"
7+
"testing"
8+
)
49

510
func TestInsert(t *testing.T) {
6-
Insert()
11+
user := &User{Username: "zhangsan", Password: "123456", Age: 18, Score: 100, Dept: "A部门"}
12+
result := gormplus.Insert(user)
13+
if result.Error != nil {
14+
fmt.Println(result.Error)
15+
}
16+
fmt.Println(result.RowsAffected)
717
}
818

919
func TestInsertBatch(t *testing.T) {
10-
InsertBatch()
20+
user1 := &User{Username: "zhangsan1", Password: "123456", Age: 18, Score: 12, Dept: "导弹部门"}
21+
user2 := &User{Username: "lisi", Password: "123456", Age: 16, Score: 34, Dept: "投诉部门"}
22+
user3 := &User{Username: "wangwu", Password: "123456", Age: 26, Score: 33, Dept: "研发部门"}
23+
user4 := &User{Username: "zhangsan4", Password: "123456", Age: 30, Score: 11, Dept: "产品部门"}
24+
user5 := &User{Username: "zhangsan5", Password: "123456", Age: 12, Score: 34, Dept: "产品部门1"}
25+
user6 := &User{Username: "zhangsan6", Password: "123456", Age: 45, Score: 123, Dept: "产品部门12"}
26+
27+
var users []*User
28+
users = append(users, user1)
29+
users = append(users, user2)
30+
users = append(users, user3)
31+
users = append(users, user4)
32+
users = append(users, user5)
33+
users = append(users, user6)
34+
35+
result := gormplus.InsertBatch[User](users)
36+
if result.Error != nil {
37+
fmt.Println(result.Error)
38+
}
39+
fmt.Println(result.RowsAffected)
40+
for _, u := range users {
41+
marshal, _ := json.Marshal(u)
42+
fmt.Println(string(marshal))
43+
}
1144
}
1245

1346
func TestInsertBatchSize(t *testing.T) {
14-
InsertBatchSize()
47+
user1 := &User{Username: "zhangsan1", Password: "123456", Age: 18, Score: 12, Dept: "导弹部门"}
48+
user2 := &User{Username: "lisi", Password: "123456", Age: 16, Score: 34, Dept: "投诉部门"}
49+
user3 := &User{Username: "wangwu", Password: "123456", Age: 26, Score: 33, Dept: "研发部门"}
50+
user4 := &User{Username: "zhangsan4", Password: "123456", Age: 30, Score: 11, Dept: "产品部门"}
51+
user5 := &User{Username: "zhangsan5", Password: "123456", Age: 12, Score: 34, Dept: "产品部门1"}
52+
user6 := &User{Username: "zhangsan6", Password: "123456", Age: 45, Score: 123, Dept: "产品部门12"}
53+
54+
var users []*User
55+
users = append(users, user1)
56+
users = append(users, user2)
57+
users = append(users, user3)
58+
users = append(users, user4)
59+
users = append(users, user5)
60+
users = append(users, user6)
61+
62+
result := gormplus.InsertBatchSize[User](users, 3)
63+
if result.Error != nil {
64+
fmt.Println(result.Error)
65+
}
66+
fmt.Println(result.RowsAffected)
67+
for _, u := range users {
68+
marshal, _ := json.Marshal(u)
69+
fmt.Println(string(marshal))
70+
}
1571
}

example/example_select_test.go

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package example
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"github.com/gorm-plus/gorm-plus/gormplus"
7+
"gorm.io/gorm"
8+
"log"
9+
"testing"
10+
)
11+
12+
func TestSelectById(t *testing.T) {
13+
user, resultDb := gormplus.SelectById[User](1)
14+
if resultDb.Error != nil {
15+
if errors.Is(resultDb.Error, gorm.ErrRecordNotFound) {
16+
log.Fatalln("SelectById Data not found:", resultDb.Error)
17+
}
18+
log.Fatalln("SelectById error:", resultDb.Error)
19+
}
20+
log.Println("RowsAffected:", resultDb.RowsAffected)
21+
marshal, _ := json.Marshal(user)
22+
log.Println(string(marshal))
23+
}
24+
25+
func TestSelectByIds(t *testing.T) {
26+
var ids []int
27+
ids = append(ids, 1)
28+
ids = append(ids, 2)
29+
users, resultDb := gormplus.SelectByIds[User](ids)
30+
if resultDb.Error != nil {
31+
log.Fatalln(resultDb.Error)
32+
}
33+
log.Println("RowsAffected:", resultDb.RowsAffected)
34+
marshal, _ := json.Marshal(users)
35+
log.Println(string(marshal))
36+
}
37+
38+
func TestSelectOne1(t *testing.T) {
39+
q := &gormplus.Query[User]{}
40+
q.Eq(UserColumn.Username, "zhangsan1")
41+
user, resultDb := gormplus.SelectOne(q)
42+
43+
if resultDb.Error != nil {
44+
if errors.Is(resultDb.Error, gorm.ErrRecordNotFound) {
45+
log.Fatalln("SelectOne Data not found:", resultDb.Error)
46+
}
47+
log.Fatalln("SelectOne error:", resultDb.Error)
48+
}
49+
50+
log.Println("RowsAffected:", resultDb.RowsAffected)
51+
marshal, _ := json.Marshal(user)
52+
log.Println(string(marshal))
53+
}
54+
55+
func TestSelectOne2(t *testing.T) {
56+
q := &gormplus.Query[User]{}
57+
q.Eq(UserColumn.Username, "zhangsan").
58+
Select(UserColumn.Username, UserColumn.Password)
59+
user, resultDb := gormplus.SelectOne(q)
60+
61+
if resultDb.Error != nil {
62+
if errors.Is(resultDb.Error, gorm.ErrRecordNotFound) {
63+
log.Fatalln("SelectOne Data not found:", resultDb.Error)
64+
}
65+
log.Fatalln("SelectOne error:", resultDb.Error)
66+
}
67+
68+
log.Println("RowsAffected:", resultDb.RowsAffected)
69+
marshal, _ := json.Marshal(user)
70+
log.Println(string(marshal))
71+
}
72+
73+
func TestSelectList1(t *testing.T) {
74+
q := &gormplus.Query[User]{}
75+
q.Eq(UserColumn.Username, "zhangsan")
76+
users, resultDb := gormplus.SelectList(q)
77+
if resultDb.Error != nil {
78+
log.Fatalln("error:", resultDb.Error)
79+
}
80+
for _, u := range users {
81+
marshal, _ := json.Marshal(u)
82+
log.Println(string(marshal))
83+
}
84+
}
85+
86+
func TestSelectTableList(t *testing.T) {
87+
type deptCount struct {
88+
Dept string
89+
Count string
90+
}
91+
q := &gormplus.Query[User]{}
92+
q.Group(UserColumn.Dept).Select(UserColumn.Dept, "count(*) as count")
93+
users, resultDb := gormplus.SelectModelList[User, deptCount](q)
94+
if resultDb.Error != nil {
95+
log.Fatalln("error:", resultDb.Error)
96+
}
97+
for _, u := range users {
98+
marshal, _ := json.Marshal(u)
99+
log.Println(string(marshal))
100+
}
101+
}
102+
103+
func TestSelectPage(t *testing.T) {
104+
q := &gormplus.Query[User]{}
105+
q.Eq(UserColumn.Age, 18)
106+
page := &gormplus.Page[User]{Current: 1, Size: 10}
107+
pageResult, resultDb := gormplus.SelectPage(page, q)
108+
if resultDb.Error != nil {
109+
log.Fatalln("error:", resultDb.Error)
110+
}
111+
log.Println("total", pageResult.Total)
112+
for _, u := range pageResult.Records {
113+
marshal, _ := json.Marshal(u)
114+
log.Println(string(marshal))
115+
}
116+
}
117+
118+
func TestSelectTablePage(t *testing.T) {
119+
type deptCount struct {
120+
Dept string
121+
Count string
122+
}
123+
q := gormplus.NewQuery[User]()
124+
q.Group(UserColumn.Dept).Select(UserColumn.Dept, "count(*) as count")
125+
page := gormplus.NewPage[deptCount](1, 2)
126+
pageResult, resultDb := gormplus.SelectModelPage[User, deptCount](page, q)
127+
if resultDb.Error != nil {
128+
log.Fatalln("error:", resultDb.Error)
129+
}
130+
log.Println("total:", pageResult.Total)
131+
for _, u := range pageResult.Records {
132+
marshal, _ := json.Marshal(u)
133+
log.Println(string(marshal))
134+
}
135+
}
136+
137+
func TestSelectCount(t *testing.T) {
138+
q := gormplus.NewQuery[User]()
139+
q.Eq(UserColumn.Age, 18)
140+
count, resultDb := gormplus.SelectCount(q)
141+
if resultDb.Error != nil {
142+
log.Fatalln("error:", resultDb.Error)
143+
}
144+
log.Println("count:", count)
145+
}

example/example_update.go

Lines changed: 0 additions & 26 deletions
This file was deleted.

example/example_update_test.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
package example
22

33
import (
4+
"fmt"
5+
"github.com/gorm-plus/gorm-plus/gormplus"
46
"testing"
57
)
68

79
func TestUpdateById(t *testing.T) {
8-
UpdateById()
10+
user := &User{ID: 1, Username: "zhangsan", Password: "123456", Age: 18, Score: 100, Dept: "A部门asdfasdf"}
11+
result := gormplus.UpdateById(user)
12+
if result.Error != nil {
13+
fmt.Println(result.Error)
14+
}
15+
fmt.Println(result.RowsAffected)
916
}
1017

1118
func TestUpdate(t *testing.T) {
12-
Update()
19+
q := gormplus.Query[User]{}
20+
q.Eq(UserColumn.Username, "zhangsan").Set(UserColumn.Dept, "相关部门123123").
21+
Set(UserColumn.Phone, 12312)
22+
result := gormplus.Update(&q)
23+
if result.Error != nil {
24+
fmt.Println(result.Error)
25+
}
26+
fmt.Println(result.RowsAffected)
1327
}

0 commit comments

Comments
 (0)