Skip to content

Commit e1251c0

Browse files
authored
refactor: refactor build sql (#10)
1 parent 0b67e59 commit e1251c0

16 files changed

+1041
-183
lines changed

.github/workflows/go.yml

+22-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,26 @@ jobs:
1010

1111
build:
1212
runs-on: ubuntu-latest
13+
strategy:
14+
matrix:
15+
db: [ 'MySQL' ]
16+
services:
17+
mysql:
18+
# Docker Hub image
19+
image: mysql:8
20+
env:
21+
MYSQL_ROOT_PASSWORD: test
22+
MYSQL_DATABASE: test
23+
MYSQL_USER: test
24+
MYSQL_PASSWORD: test
25+
# Set health checks to wait until mysql has started
26+
options: >-
27+
--health-cmd="mysqladmin ping"
28+
--health-interval 10s
29+
--health-timeout 5s
30+
--health-retries 5
31+
ports:
32+
- 3306:3306
1333
steps:
1434
- uses: actions/checkout@v3
1535

@@ -21,7 +41,7 @@ jobs:
2141
- name: Build
2242
run: go build -v ./...
2343

24-
- name: Test
25-
run: go test -v ./... -coverprofile=coverage.txt -covermode=atomic
44+
# - name: Test
45+
# run: go test -v ./... -coverprofile=coverage.txt -covermode=atomic
2646

2747
- uses: codecov/codecov-action@v2

cmd/gobatis-plus/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package main
1919

2020
import (
2121
"flag"
22+
"k8s.io/klog/v2"
2223

2324
"github.com/acmestack/gobatis-plus/cmd/gobatis-plus/customargs"
2425
"github.com/acmestack/gobatis-plus/pkg/generator"

go.mod

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,32 @@ go 1.18
44

55
require (
66
github.com/acmestack/gobatis v0.2.8
7+
github.com/acmestack/godkits v0.0.10
78
github.com/go-sql-driver/mysql v1.6.0
89
github.com/spf13/pflag v1.0.5
10+
github.com/stretchr/testify v1.5.1
911
k8s.io/gengo v0.0.0-20220613173612-397b4ae3bce7
10-
k8s.io/klog/v2 v2.2.0
1112
)
1213

1314
require (
1415
github.com/Masterminds/goutils v1.1.1 // indirect
1516
github.com/Masterminds/semver/v3 v3.1.1 // indirect
1617
github.com/Masterminds/sprig/v3 v3.2.2 // indirect
18+
github.com/davecgh/go-spew v1.1.1 // indirect
1719
github.com/go-logr/logr v0.2.0 // indirect
1820
github.com/google/uuid v1.1.1 // indirect
1921
github.com/huandu/xstrings v1.3.1 // indirect
2022
github.com/imdario/mergo v0.3.11 // indirect
2123
github.com/mitchellh/copystructure v1.0.0 // indirect
2224
github.com/mitchellh/reflectwalk v1.0.0 // indirect
25+
github.com/pmezard/go-difflib v1.0.0 // indirect
2326
github.com/shopspring/decimal v1.2.0 // indirect
2427
github.com/spf13/cast v1.3.1 // indirect
2528
github.com/xfali/loadbalance v0.0.1 // indirect
2629
golang.org/x/crypto v0.0.0-20200414173820-0848c9571904 // indirect
2730
golang.org/x/mod v0.2.0 // indirect
2831
golang.org/x/tools v0.0.0-20200505023115-26f46d2f7ef8 // indirect
2932
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect
33+
gopkg.in/yaml.v2 v2.3.0 // indirect
34+
k8s.io/klog/v2 v2.2.0 // indirect
3035
)

go.sum

+7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ github.com/Masterminds/sprig/v3 v3.2.2 h1:17jRggJu518dr3QaafizSXOjKYp94wKfABxUmy
66
github.com/Masterminds/sprig/v3 v3.2.2/go.mod h1:UoaO7Yp8KlPnJIYWTFkMaqPUYKTfGFPhxNuwnnxkKlk=
77
github.com/acmestack/gobatis v0.2.8 h1:dYA3AUVXLQvHcuGA9bscqq4xw6tEC1E9dlyx4ebCHtk=
88
github.com/acmestack/gobatis v0.2.8/go.mod h1:vEEXPWzVzeDoFpYD2FoOfGfCyEuLtSiMIbP6jqO44Xg=
9+
github.com/acmestack/godkits v0.0.10 h1:gIVwtJ/ZVSUr4u5NsKq35Hvp+lecTImA9EYkVACUpss=
10+
github.com/acmestack/godkits v0.0.10/go.mod h1:d5kiqEvQl/LpXd8VTy7PZvQ5DDiasCX+QKA3+q8fWos=
911
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1012
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1113
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -22,15 +24,18 @@ github.com/huandu/xstrings v1.3.1 h1:4jgBlKK6tLKFvO8u5pmYjG91cqytmDCDvGh7ECVFfFs
2224
github.com/huandu/xstrings v1.3.1/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
2325
github.com/imdario/mergo v0.3.11 h1:3tnifQM4i+fbajXKBHXWEH+KvNHqojZ778UH75j3bGA=
2426
github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
27+
github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs=
2528
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
2629
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
30+
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
2731
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
2832
github.com/mitchellh/copystructure v1.0.0 h1:Laisrj+bAB6b/yJwB5Bt3ITZhGJdqmxquMKeZ+mmkFQ=
2933
github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw=
3034
github.com/mitchellh/reflectwalk v1.0.0 h1:9D+8oIskB4VJBN5SFlmc27fSlIBZaov1Wpk/IfikLNY=
3135
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
3236
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
3337
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
38+
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
3439
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
3540
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
3641
github.com/spf13/cast v1.3.1 h1:nFm6S0SMdyzrzcmThSipiEubIDy8WEXKNZ0UOgiRpng=
@@ -66,7 +71,9 @@ golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8T
6671
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
6772
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
6873
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
74+
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
6975
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
76+
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA=
7077
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
7178
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
7279
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=

pkg/constants/keyword.go

+18-15
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,23 @@ const (
2929
Ge = ">="
3030
Lt = "<"
3131
Le = "<="
32-
IsNull = "is null"
33-
IsNotNull = "is not null"
34-
GroupBy = "group by"
35-
Having = "having"
36-
OrderBy = "order by"
37-
Exists = "exists"
38-
Between = "between"
39-
Asc = "asc"
40-
Desc = "desc"
41-
INSERT = "insert"
42-
SELECT = "select"
43-
UPDATE = "update"
44-
DELETE = "delete"
45-
WHERE = "where"
46-
FROM = "from"
4732
ID = "id"
33+
IsNull = "IS NULL"
34+
IsNotNull = "IS NOT NULL"
35+
GroupBy = "GROUP BY"
36+
Having = "HAVING"
37+
OrderBy = "ORDER BY"
38+
Exists = "EXISTS"
39+
Between = "BETWEEN"
40+
Asc = "ASC"
41+
Desc = "DESC"
42+
INSERT = "INSERT"
43+
SELECT = "SELECT"
44+
UPDATE = "UPDATE"
45+
DELETE = "DELETE"
46+
WHERE = "WHERE"
47+
FROM = "FROM"
48+
INTO = "INTO"
49+
VALUES = "VALUES"
50+
SET = "SET"
4851
)

pkg/constants/sql_template.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Licensed to the AcmeStack under one or more contributor license
3+
* agreements. See the NOTICE file distributed with this work for
4+
* additional information regarding copyright ownership.
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+
package constants
18+
19+
const SELECT_SQL = "SELECT #{columns} FROM #{tableName} WHERE #{conditions}"
20+
21+
const INSERT_SQL = "INSERT INTO #{tableName} (#{columns}) VALUES (#{columnMapping})"
22+
23+
const UPDATEBYID_SQL = "UPDATE #{tableName} SET #{columnMapping} WHERE #{conditions}"
24+
25+
const DELETEBYID_SQL = "delete from #{tableName} where #{conditions}"

pkg/constants/string_pool.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,14 @@ const (
2424
SPACE = " "
2525
ASTERISK = "*"
2626
CONNECTION = "-"
27-
COUNT = "count(*)"
27+
COUNT = "COUNT(*)"
2828
LEFT_BRACKET = "("
2929
RIGHT_BRACKET = ")"
3030
COMMA = ","
31+
COLUMN = "column"
32+
33+
COLUMN_HASH = "#{columns}"
34+
TABLE_NAME_HASH = "#{tableName}"
35+
CONDITIONS_HASH = "#{conditions}"
36+
COLUMN_MAPPING_HASH = "#{columnMapping}"
3137
)

pkg/generator/gobatis-gen.go

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package generator
2020
import (
2121
"fmt"
2222
"io"
23+
"k8s.io/klog/v2"
2324
"path/filepath"
2425
"strings"
2526

pkg/mapper/base.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ type Base[T any] interface {
2424

2525
UpdateById(entity T) int64
2626

27+
DeleteById(id any) int64
28+
29+
DeleteBatchIds(ids []any) int64
30+
2731
SelectById(id any) (T, error)
2832

2933
SelectBatchIds(queryWrapper *QueryWrapper[T]) ([]T, error)
@@ -33,8 +37,4 @@ type Base[T any] interface {
3337
SelectCount(queryWrapper *QueryWrapper[T]) (int64, error)
3438

3539
SelectList(queryWrapper *QueryWrapper[T]) ([]T, error)
36-
37-
DeleteById(id any) int64
38-
39-
DeleteBatchIds(ids []any) int64
4040
}

0 commit comments

Comments
 (0)