Skip to content

Commit

Permalink
refactor: refactor build sql (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
afumu authored Sep 4, 2022
1 parent 0b67e59 commit e1251c0
Show file tree
Hide file tree
Showing 16 changed files with 1,041 additions and 183 deletions.
24 changes: 22 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,26 @@ jobs:

build:
runs-on: ubuntu-latest
strategy:
matrix:
db: [ 'MySQL' ]
services:
mysql:
# Docker Hub image
image: mysql:8
env:
MYSQL_ROOT_PASSWORD: test
MYSQL_DATABASE: test
MYSQL_USER: test
MYSQL_PASSWORD: test
# Set health checks to wait until mysql has started
options: >-
--health-cmd="mysqladmin ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 3306:3306
steps:
- uses: actions/checkout@v3

Expand All @@ -21,7 +41,7 @@ jobs:
- name: Build
run: go build -v ./...

- name: Test
run: go test -v ./... -coverprofile=coverage.txt -covermode=atomic
# - name: Test
# run: go test -v ./... -coverprofile=coverage.txt -covermode=atomic

- uses: codecov/codecov-action@v2
1 change: 1 addition & 0 deletions cmd/gobatis-plus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package main

import (
"flag"
"k8s.io/klog/v2"

"github.com/acmestack/gobatis-plus/cmd/gobatis-plus/customargs"
"github.com/acmestack/gobatis-plus/pkg/generator"
Expand Down
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,32 @@ go 1.18

require (
github.com/acmestack/gobatis v0.2.8
github.com/acmestack/godkits v0.0.10
github.com/go-sql-driver/mysql v1.6.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.5.1
k8s.io/gengo v0.0.0-20220613173612-397b4ae3bce7
k8s.io/klog/v2 v2.2.0
)

require (
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.1.1 // indirect
github.com/Masterminds/sprig/v3 v3.2.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-logr/logr v0.2.0 // indirect
github.com/google/uuid v1.1.1 // indirect
github.com/huandu/xstrings v1.3.1 // indirect
github.com/imdario/mergo v0.3.11 // indirect
github.com/mitchellh/copystructure v1.0.0 // indirect
github.com/mitchellh/reflectwalk v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/xfali/loadbalance v0.0.1 // indirect
golang.org/x/crypto v0.0.0-20200414173820-0848c9571904 // indirect
golang.org/x/mod v0.2.0 // indirect
golang.org/x/tools v0.0.0-20200505023115-26f46d2f7ef8 // indirect
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
k8s.io/klog/v2 v2.2.0 // indirect
)
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/Masterminds/sprig/v3 v3.2.2 h1:17jRggJu518dr3QaafizSXOjKYp94wKfABxUmy
github.com/Masterminds/sprig/v3 v3.2.2/go.mod h1:UoaO7Yp8KlPnJIYWTFkMaqPUYKTfGFPhxNuwnnxkKlk=
github.com/acmestack/gobatis v0.2.8 h1:dYA3AUVXLQvHcuGA9bscqq4xw6tEC1E9dlyx4ebCHtk=
github.com/acmestack/gobatis v0.2.8/go.mod h1:vEEXPWzVzeDoFpYD2FoOfGfCyEuLtSiMIbP6jqO44Xg=
github.com/acmestack/godkits v0.0.10 h1:gIVwtJ/ZVSUr4u5NsKq35Hvp+lecTImA9EYkVACUpss=
github.com/acmestack/godkits v0.0.10/go.mod h1:d5kiqEvQl/LpXd8VTy7PZvQ5DDiasCX+QKA3+q8fWos=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -22,15 +24,18 @@ github.com/huandu/xstrings v1.3.1 h1:4jgBlKK6tLKFvO8u5pmYjG91cqytmDCDvGh7ECVFfFs
github.com/huandu/xstrings v1.3.1/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/imdario/mergo v0.3.11 h1:3tnifQM4i+fbajXKBHXWEH+KvNHqojZ778UH75j3bGA=
github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs=
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mitchellh/copystructure v1.0.0 h1:Laisrj+bAB6b/yJwB5Bt3ITZhGJdqmxquMKeZ+mmkFQ=
github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw=
github.com/mitchellh/reflectwalk v1.0.0 h1:9D+8oIskB4VJBN5SFlmc27fSlIBZaov1Wpk/IfikLNY=
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/spf13/cast v1.3.1 h1:nFm6S0SMdyzrzcmThSipiEubIDy8WEXKNZ0UOgiRpng=
Expand Down Expand Up @@ -66,7 +71,9 @@ golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
Expand Down
33 changes: 18 additions & 15 deletions pkg/constants/keyword.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,23 @@ const (
Ge = ">="
Lt = "<"
Le = "<="
IsNull = "is null"
IsNotNull = "is not null"
GroupBy = "group by"
Having = "having"
OrderBy = "order by"
Exists = "exists"
Between = "between"
Asc = "asc"
Desc = "desc"
INSERT = "insert"
SELECT = "select"
UPDATE = "update"
DELETE = "delete"
WHERE = "where"
FROM = "from"
ID = "id"
IsNull = "IS NULL"
IsNotNull = "IS NOT NULL"
GroupBy = "GROUP BY"
Having = "HAVING"
OrderBy = "ORDER BY"
Exists = "EXISTS"
Between = "BETWEEN"
Asc = "ASC"
Desc = "DESC"
INSERT = "INSERT"
SELECT = "SELECT"
UPDATE = "UPDATE"
DELETE = "DELETE"
WHERE = "WHERE"
FROM = "FROM"
INTO = "INTO"
VALUES = "VALUES"
SET = "SET"
)
25 changes: 25 additions & 0 deletions pkg/constants/sql_template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Licensed to the AcmeStack under one or more contributor license
* agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package constants

const SELECT_SQL = "SELECT #{columns} FROM #{tableName} WHERE #{conditions}"

const INSERT_SQL = "INSERT INTO #{tableName} (#{columns}) VALUES (#{columnMapping})"

const UPDATEBYID_SQL = "UPDATE #{tableName} SET #{columnMapping} WHERE #{conditions}"

const DELETEBYID_SQL = "delete from #{tableName} where #{conditions}"
8 changes: 7 additions & 1 deletion pkg/constants/string_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,14 @@ const (
SPACE = " "
ASTERISK = "*"
CONNECTION = "-"
COUNT = "count(*)"
COUNT = "COUNT(*)"
LEFT_BRACKET = "("
RIGHT_BRACKET = ")"
COMMA = ","
COLUMN = "column"

COLUMN_HASH = "#{columns}"
TABLE_NAME_HASH = "#{tableName}"
CONDITIONS_HASH = "#{conditions}"
COLUMN_MAPPING_HASH = "#{columnMapping}"
)
1 change: 1 addition & 0 deletions pkg/generator/gobatis-gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package generator
import (
"fmt"
"io"
"k8s.io/klog/v2"
"path/filepath"
"strings"

Expand Down
8 changes: 4 additions & 4 deletions pkg/mapper/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ type Base[T any] interface {

UpdateById(entity T) int64

DeleteById(id any) int64

DeleteBatchIds(ids []any) int64

SelectById(id any) (T, error)

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

SelectList(queryWrapper *QueryWrapper[T]) ([]T, error)

DeleteById(id any) int64

DeleteBatchIds(ids []any) int64
}
Loading

0 comments on commit e1251c0

Please sign in to comment.