Skip to content

Commit 6740768

Browse files
authored
refactor: refactor baseMapper (#4)
* feat: add queryWrapper * feat: update gobatis version v0.2.6 to v0.2.7 * refactor: refactor baseMapper * refactor: delete redundant code
1 parent ad10920 commit 6740768

File tree

11 files changed

+429
-151
lines changed

11 files changed

+429
-151
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/acmestack/gobatis-plus
33
go 1.18
44

55
require (
6-
github.com/acmestack/gobatis v0.2.7
6+
github.com/acmestack/gobatis v0.2.8
77
github.com/go-sql-driver/mysql v1.6.0
88
github.com/spf13/pflag v1.0.5
99
k8s.io/gengo v0.0.0-20220613173612-397b4ae3bce7

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030I
44
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
55
github.com/Masterminds/sprig/v3 v3.2.2 h1:17jRggJu518dr3QaafizSXOjKYp94wKfABxUmyxvxX8=
66
github.com/Masterminds/sprig/v3 v3.2.2/go.mod h1:UoaO7Yp8KlPnJIYWTFkMaqPUYKTfGFPhxNuwnnxkKlk=
7-
github.com/acmestack/gobatis v0.2.7 h1:aec/toZ5lZTmccI1WQ5jPPQjdITrwbIEPkt5dSajeaE=
8-
github.com/acmestack/gobatis v0.2.7/go.mod h1:vEEXPWzVzeDoFpYD2FoOfGfCyEuLtSiMIbP6jqO44Xg=
7+
github.com/acmestack/gobatis v0.2.8 h1:dYA3AUVXLQvHcuGA9bscqq4xw6tEC1E9dlyx4ebCHtk=
8+
github.com/acmestack/gobatis v0.2.8/go.mod h1:vEEXPWzVzeDoFpYD2FoOfGfCyEuLtSiMIbP6jqO44Xg=
99
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1010
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1111
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

pkg/constants/keyword.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
/*
2+
* Copyright (c) 2022, AcmeStack
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
118
package constants
219

320
const (
@@ -21,4 +38,11 @@ const (
2138
Between = "between"
2239
Asc = "asc"
2340
Desc = "desc"
41+
INSERT = "insert"
42+
SELECT = "select"
43+
UPDATE = "update"
44+
DELETE = "delete"
45+
WHERE = "where"
46+
FROM = "from"
47+
ID = "id"
2448
)

pkg/constants/string_pool.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (c) 2022, AcmeStack
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package constants
19+
20+
const (
21+
RIGHT_BRACE = "}"
22+
HASH_LEFT_BRACE = "#{"
23+
MAPPING = "mapping"
24+
SPACE = " "
25+
ASTERISK = "*"
26+
CONNECTION = "-"
27+
COUNT = "count(*)"
28+
LEFT_BRACKET = "("
29+
RIGHT_BRACKET = ")"
30+
COMMA = ","
31+
)

pkg/mapper/base.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,23 @@
1818
package mapper
1919

2020
type Base[T any] interface {
21-
Insert(entity T) int64
21+
Save(entity T) int64
2222

23-
InsertBatch(entities ...T) (int64, int64)
23+
SaveBatch(entities ...T) (int64, int64)
2424

25-
DeleteById(id any) int64
25+
UpdateById(entity T) int64
2626

27-
DeleteBatchIds(ids []any) int64
27+
SelectById(id any) (T, error)
2828

29-
UpdateById(entity T) int64
29+
SelectBatchIds(queryWrapper *QueryWrapper[T]) ([]T, error)
3030

31-
SelectById(id any) T
31+
SelectOne(queryWrapper *QueryWrapper[T]) (T, error)
3232

33-
SelectBatchIds(ids []any) []T
33+
SelectCount(queryWrapper *QueryWrapper[T]) (int64, error)
3434

35-
SelectOne(entity T) T
35+
SelectList(queryWrapper *QueryWrapper[T]) ([]T, error)
3636

37-
SelectCount(entity T) int64
37+
DeleteById(id any) int64
3838

39-
SelectList(queryWrapper QueryWrapper[T]) ([]T, error)
39+
DeleteBatchIds(ids []any) int64
4040
}

pkg/mapper/base_mapper.go

Lines changed: 217 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,46 @@
1+
/*
2+
* Copyright (c) 2022, AcmeStack
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
118
package mapper
219

320
import (
421
"context"
522
"github.com/acmestack/gobatis"
23+
"github.com/acmestack/gobatis-plus/pkg/constants"
24+
"reflect"
25+
"strconv"
26+
"strings"
27+
"time"
628
)
729

830
type BaseMapper[T any] struct {
9-
SessMgr *gobatis.SessionManager
10-
Ctx context.Context
11-
Columns []string
31+
SessMgr *gobatis.SessionManager
32+
Ctx context.Context
33+
Columns []string
34+
ParamNameSeq int
1235
}
1336

14-
func (userMapper *BaseMapper[T]) Insert(entity T) int64 {
37+
type BuildSqlFunc func(columns string, tableName string) string
38+
39+
func (userMapper *BaseMapper[T]) Save(entity T) int64 {
1540
return 0
1641
}
1742

18-
func (userMapper *BaseMapper[T]) InsertBatch(entities ...T) (int64, int64) {
43+
func (userMapper *BaseMapper[T]) SaveBatch(entities ...T) (int64, int64) {
1944
return 0, 0
2045
}
2146
func (userMapper *BaseMapper[T]) DeleteById(id any) int64 {
@@ -27,30 +52,205 @@ func (userMapper *BaseMapper[T]) DeleteBatchIds(ids []any) int64 {
2752
func (userMapper *BaseMapper[T]) UpdateById(entity T) int64 {
2853
return 0
2954
}
30-
func (userMapper *BaseMapper[T]) SelectById(id any) T {
31-
return *new(T)
55+
func (userMapper *BaseMapper[T]) SelectById(id any) (T, error) {
56+
queryWrapper := userMapper.init(nil)
57+
queryWrapper.Eq(constants.ID, strconv.Itoa(id.(int)))
58+
columns := userMapper.buildSelectColumns(queryWrapper)
59+
60+
sqlId, sql, paramMap := userMapper.buildSelectSql(queryWrapper, columns, buildSelectSqlFirstPart)
61+
62+
var entity T
63+
err := gobatis.RegisterSql(sqlId, sql)
64+
if err != nil {
65+
return entity, err
66+
}
67+
68+
sess := userMapper.SessMgr.NewSession()
69+
70+
err = sess.Select(sqlId).Param(paramMap).Result(&entity)
71+
if err != nil {
72+
return entity, err
73+
}
74+
75+
// delete sqlId
76+
gobatis.UnregisterSql(sqlId)
77+
78+
return entity, nil
3279
}
33-
func (userMapper *BaseMapper[T]) SelectBatchIds(ids []any) []T {
80+
func (userMapper *BaseMapper[T]) SelectBatchIds(ids []any) ([]T, error) {
81+
tableName := userMapper.getTableName()
82+
sqlFirstPart := buildSelectSqlFirstPart(constants.ASTERISK, tableName)
83+
var paramMap = map[string]any{}
84+
build := strings.Builder{}
85+
build.WriteString(constants.SPACE + constants.WHERE + constants.SPACE + constants.ID +
86+
constants.SPACE + constants.In + constants.LEFT_BRACKET + constants.SPACE)
87+
for index, id := range ids {
88+
mapping := userMapper.getMappingSeq()
89+
paramMap[mapping] = strconv.Itoa(id.(int))
90+
if index == len(ids)-1 {
91+
build.WriteString(constants.HASH_LEFT_BRACE + mapping + constants.RIGHT_BRACE)
92+
} else {
93+
build.WriteString(constants.HASH_LEFT_BRACE + mapping + constants.RIGHT_BRACE + constants.COMMA)
94+
}
95+
}
96+
build.WriteString(constants.SPACE + constants.RIGHT_BRACKET)
97+
sqlId := buildSqlId(constants.SELECT)
98+
sql := sqlFirstPart + build.String()
99+
100+
err := gobatis.RegisterSql(sqlId, sql)
101+
if err != nil {
102+
return nil, err
103+
}
104+
105+
sess := userMapper.SessMgr.NewSession()
34106
var arr []T
35-
return arr
107+
err = sess.Select(sqlId).Param(paramMap).Result(&arr)
108+
if err != nil {
109+
return nil, err
110+
}
111+
return arr, nil
36112
}
37-
func (userMapper *BaseMapper[T]) SelectOne(entity T) T {
38-
return *new(T)
113+
114+
func (userMapper *BaseMapper[T]) getMappingSeq() string {
115+
userMapper.ParamNameSeq = userMapper.ParamNameSeq + 1
116+
mapping := constants.MAPPING + strconv.Itoa(userMapper.ParamNameSeq)
117+
return mapping
39118
}
40-
func (userMapper *BaseMapper[T]) SelectCount(entity T) int64 {
41-
return 0
119+
120+
func (userMapper *BaseMapper[T]) SelectOne(queryWrapper *QueryWrapper[T]) (T, error) {
121+
queryWrapper = userMapper.init(queryWrapper)
122+
123+
columns := userMapper.buildSelectColumns(queryWrapper)
124+
125+
sqlId, sql, paramMap := userMapper.buildSelectSql(queryWrapper, columns, buildSelectSqlFirstPart)
126+
127+
var entity T
128+
err := gobatis.RegisterSql(sqlId, sql)
129+
if err != nil {
130+
return entity, err
131+
}
132+
133+
sess := userMapper.SessMgr.NewSession()
134+
135+
err = sess.Select(sqlId).Param(paramMap).Result(&entity)
136+
if err != nil {
137+
return entity, err
138+
}
139+
140+
// delete sqlId
141+
gobatis.UnregisterSql(sqlId)
142+
return entity, nil
143+
}
144+
145+
func (userMapper *BaseMapper[T]) SelectCount(queryWrapper *QueryWrapper[T]) (int64, error) {
146+
queryWrapper = userMapper.init(queryWrapper)
147+
148+
sqlId, sql, paramMap := userMapper.buildSelectSql(queryWrapper, constants.COUNT, buildSelectSqlFirstPart)
149+
150+
err := gobatis.RegisterSql(sqlId, sql)
151+
if err != nil {
152+
return 0, err
153+
}
154+
155+
sess := userMapper.SessMgr.NewSession()
156+
var count int64
157+
err = sess.Select(sqlId).Param(paramMap).Result(&count)
158+
if err != nil {
159+
return 0, err
160+
}
161+
162+
return count, nil
42163
}
43164

44165
func (userMapper *BaseMapper[T]) SelectList(queryWrapper *QueryWrapper[T]) ([]T, error) {
45-
if queryWrapper == nil {
46-
queryWrapper = &QueryWrapper[T]{}
47-
queryWrapper.init()
166+
queryWrapper = userMapper.init(queryWrapper)
167+
168+
columns := userMapper.buildSelectColumns(queryWrapper)
169+
170+
sqlId, sql, paramMap := userMapper.buildSelectSql(queryWrapper, columns, buildSelectSqlFirstPart)
171+
172+
err := gobatis.RegisterSql(sqlId, sql)
173+
if err != nil {
174+
return nil, err
48175
}
176+
49177
sess := userMapper.SessMgr.NewSession()
50178
var arr []T
51-
err := sess.Select(queryWrapper.SqlBuild.String()).Param(queryWrapper.Entity).Result(&arr)
179+
err = sess.Select(sqlId).Param(paramMap).Result(&arr)
52180
if err != nil {
53181
return nil, err
54182
}
183+
184+
// delete sqlId
185+
gobatis.UnregisterSql(sqlId)
55186
return arr, nil
56187
}
188+
189+
func (userMapper *BaseMapper[T]) buildSelectColumns(queryWrapper *QueryWrapper[T]) string {
190+
var columns string
191+
if len(queryWrapper.Columns) > 0 {
192+
columns = strings.Join(queryWrapper.Columns, ",")
193+
} else {
194+
columns = constants.ASTERISK
195+
}
196+
return columns
197+
}
198+
199+
func (userMapper *BaseMapper[T]) init(queryWrapper *QueryWrapper[T]) *QueryWrapper[T] {
200+
if queryWrapper == nil {
201+
queryWrapper = &QueryWrapper[T]{}
202+
}
203+
return queryWrapper
204+
}
205+
206+
func (userMapper *BaseMapper[T]) buildCondition(queryWrapper *QueryWrapper[T]) (string, map[string]any) {
207+
var paramMap = map[string]any{}
208+
expression := queryWrapper.Expression
209+
build := strings.Builder{}
210+
for _, v := range expression {
211+
if paramValue, ok := v.(ParamValue); ok {
212+
mapping := userMapper.getMappingSeq()
213+
paramMap[mapping] = paramValue.value
214+
build.WriteString(constants.HASH_LEFT_BRACE + mapping + constants.RIGHT_BRACE + constants.SPACE)
215+
} else {
216+
build.WriteString(v.(string) + constants.SPACE)
217+
}
218+
}
219+
return build.String(), paramMap
220+
}
221+
222+
func (userMapper *BaseMapper[T]) buildSelectSql(queryWrapper *QueryWrapper[T], columns string, buildSqlFunc BuildSqlFunc) (string, string, map[string]any) {
223+
224+
sqlCondition, paramMap := userMapper.buildCondition(queryWrapper)
225+
226+
tableName := userMapper.getTableName()
227+
228+
sqlId := buildSqlId(constants.SELECT)
229+
230+
sqlFirstPart := buildSqlFunc(columns, tableName)
231+
232+
var sql string
233+
if len(queryWrapper.Expression) > 0 {
234+
sql = sqlFirstPart + constants.SPACE + constants.WHERE + constants.SPACE + sqlCondition
235+
} else {
236+
sql = sqlFirstPart
237+
}
238+
239+
return sqlId, sql, paramMap
240+
}
241+
242+
func (userMapper *BaseMapper[T]) getTableName() string {
243+
entityRef := reflect.TypeOf(new(T)).Elem()
244+
tableNameTag := entityRef.Field(0).Tag
245+
tableName := string(tableNameTag)
246+
return tableName
247+
}
248+
249+
func buildSqlId(sqlType string) string {
250+
sqlId := sqlType + constants.CONNECTION + strconv.Itoa(time.Now().Nanosecond())
251+
return sqlId
252+
}
253+
254+
func buildSelectSqlFirstPart(columns string, tableName string) string {
255+
return constants.SELECT + constants.SPACE + columns + constants.SPACE + constants.FROM + constants.SPACE + tableName
256+
}

0 commit comments

Comments
 (0)