modify model

master
jager 10 months ago
parent c08e741d7a
commit 4e4ae034f1

@ -1,6 +1,8 @@
module github.com/zeromicro/go-zero
go 1.19
go 1.21
toolchain go1.21.5
require (
github.com/DATA-DOG/go-sqlmock v1.5.2

@ -7,10 +7,13 @@ github.com/alicebob/gopher-json v0.0.0-20230218143504-906a9b012302/go.mod h1:SGn
github.com/alicebob/miniredis/v2 v2.31.1 h1:7XAt0uUg3DtwEKW5ZAGa+K7FZV2DdKQo5K/6TTnfX8Y=
github.com/alicebob/miniredis/v2 v2.31.1/go.mod h1:UB/T2Uztp7MlFSDakaX1sTXUv5CASoprx0wulRT6HBg=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
github.com/bufbuild/protocompile v0.7.1 h1:Kd8fb6EshOHXNNRtYAmLAwy/PotlyFoN0iMbuwGNh0M=
github.com/bufbuild/protocompile v0.7.1/go.mod h1:+Etjg4guZoAqzVk2czwEQP12yaxLJ8DxuqCJ9qHdH94=
github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM=
@ -51,12 +54,14 @@ github.com/go-openapi/swag v0.22.4/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg=
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
github.com/golang/glog v1.1.2 h1:DVjP2PbBOzHyzA+dn3WhHIq4NdVu3Q+pvivFICf/7fo=
github.com/golang/glog v1.1.2/go.mod h1:zR+okUeTbrL6EL3xHUDxZuEtGv04p5shwip1+mL/rLQ=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
@ -78,6 +83,7 @@ github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/
github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 h1:K6RDEckDVWvDI9JAJYCmNdQXq6neHJOYx3V6jnqNEec=
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.0 h1:RtRsiaGvWxcwd8y3BiRZxsylPT8hLWZ5SPcfI+3IDNk=
@ -106,6 +112,7 @@ github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGC
github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
@ -136,15 +143,19 @@ github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uY
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/onsi/ginkgo/v2 v2.13.0 h1:0jY9lJquiL8fcf3M4LAXN5aMlS/b2BV86HFFPCPMgE4=
github.com/onsi/ginkgo/v2 v2.13.0/go.mod h1:TE309ZR8s5FsKKpuB1YAQYBzCaAfUgatB/xlT/ETL/o=
github.com/onsi/gomega v1.29.0 h1:KIA/t2t5UBzoirT4H9tsML45GEbo3ouUnBHsCfD2tVg=
github.com/onsi/gomega v1.29.0/go.mod h1:9sxs+SwGrKI0+PWe4Fxa9tFQQBG5xSsSbMXOI8PPpoQ=
github.com/openzipkin/zipkin-go v0.4.2 h1:zjqfqHjUpPmB3c1GlCvvgsM1G4LkvqQbBDueDOCg/jA=
github.com/openzipkin/zipkin-go v0.4.2/go.mod h1:ZeVkFjuuBiSy13y8vpSDCjMi9GoI3hPpCJSBx/EYFhY=
github.com/pelletier/go-toml/v2 v2.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI=
github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
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/prashantv/gostub v1.1.0 h1:BTyx3RfQjRHnUWaGF9oQos79AlQ5k8WNktv7VGvVH4g=
github.com/prashantv/gostub v1.1.0/go.mod h1:A5zLQHz7ieHGG7is6LLXLz7I8+3LZzsrV0P1IAHhP5U=
github.com/prometheus/client_golang v1.18.0 h1:HzFfmkOzH5Q8L8G+kSJKUx5dtG87sewO+FoDDqP5Tbk=
github.com/prometheus/client_golang v1.18.0/go.mod h1:T+GXkCk5wSJyOqMIzVgvvjFDlkOQntgjkJWKrN5txjA=
github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw=
@ -158,9 +169,11 @@ github.com/redis/go-redis/v9 v9.4.0/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
@ -294,6 +307,7 @@ golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4f
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.12.0 h1:YW6HUoUmYBpwSgyaGaZq1fHjrBjX1rlpZ54T6mu2kss=
golang.org/x/tools v0.12.0/go.mod h1:Sc0INKfu04TlqNoRA1hgpFZbhYXHPr4V5DzpSBTPqQM=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

@ -19,24 +19,24 @@ var commonMysqlDataTypeMapInt = map[int]string{
// For consistency, all integer types are converted to int64
// number
parser.Bit: "byte",
parser.TinyInt: "int64",
parser.SmallInt: "int64",
parser.MediumInt: "int64",
parser.Int: "int64",
parser.MiddleInt: "int64",
parser.Int1: "int64",
parser.Int2: "int64",
parser.Int3: "int64",
parser.Int4: "int64",
parser.Int8: "int64",
parser.Integer: "int64",
parser.TinyInt: "int8",
parser.SmallInt: "int16",
parser.MediumInt: "int32",
parser.Int: "int",
parser.MiddleInt: "int32",
parser.Int1: "int",
parser.Int2: "int",
parser.Int3: "int",
parser.Int4: "int",
parser.Int8: "int8",
parser.Integer: "int",
parser.BigInt: "int64",
parser.Float: "float64",
parser.Float4: "float64",
parser.Float8: "float64",
parser.Double: "float64",
parser.Decimal: "float64",
parser.Dec: "float64",
parser.Decimal: "decimal.Decimal",
parser.Dec: "decimal.Decimal",
parser.Fixed: "float64",
parser.Numeric: "float64",
parser.Real: "float64",
@ -55,8 +55,8 @@ var commonMysqlDataTypeMapInt = map[int]string{
parser.LongVarChar: "string",
parser.LineString: "string",
parser.MultiLineString: "string",
parser.Binary: "string",
parser.VarBinary: "string",
parser.Binary: "[]byte",
parser.VarBinary: "[]byte",
parser.TinyText: "string",
parser.Text: "string",
parser.MediumText: "string",
@ -64,10 +64,10 @@ var commonMysqlDataTypeMapInt = map[int]string{
parser.Enum: "string",
parser.Set: "string",
parser.Json: "string",
parser.Blob: "string",
parser.LongBlob: "string",
parser.MediumBlob: "string",
parser.TinyBlob: "string",
parser.Blob: "[]byte",
parser.LongBlob: "[]byte",
parser.MediumBlob: "[]byte",
parser.TinyBlob: "[]byte",
// bool
parser.Bool: "bool",
parser.Boolean: "bool",
@ -80,19 +80,19 @@ var commonMysqlDataTypeMapString = map[string]string{
"_bool": "pq.BoolArray",
"boolean": "bool",
// number
"tinyint": "int64",
"smallint": "int64",
"mediumint": "int64",
"int": "int64",
"int1": "int64",
"int2": "int64",
"tinyint": "int8",
"smallint": "int16",
"mediumint": "int32",
"int": "int",
"int1": "int",
"int2": "int",
"_int2": "pq.Int64Array",
"int3": "int64",
"int4": "int64",
"int3": "int",
"int4": "int",
"_int4": "pq.Int64Array",
"int8": "int64",
"int8": "int8",
"_int8": "pq.Int64Array",
"integer": "int64",
"integer": "int",
"_integer": "pq.Int64Array",
"bigint": "int64",
"float": "float64",
@ -101,8 +101,8 @@ var commonMysqlDataTypeMapString = map[string]string{
"float8": "float64",
"_float8": "pq.Float64Array",
"double": "float64",
"decimal": "float64",
"dec": "float64",
"decimal": "decimal.Decimal",
"dec": "decimal.Decimal",
"fixed": "float64",
"real": "float64",
"bit": "byte",
@ -123,10 +123,10 @@ var commonMysqlDataTypeMapString = map[string]string{
"character": "string",
"varchar": "string",
"_varchar": "pq.StringArray",
"binary": "string",
"bytea": "string",
"longvarbinary": "string",
"varbinary": "string",
"binary": "[]byte",
"bytea": "[]byte",
"longvarbinary": "[]byte",
"varbinary": "[]byte",
"tinytext": "string",
"text": "string",
"_text": "pq.StringArray",
@ -136,10 +136,10 @@ var commonMysqlDataTypeMapString = map[string]string{
"set": "string",
"json": "string",
"jsonb": "string",
"blob": "string",
"longblob": "string",
"mediumblob": "string",
"tinyblob": "string",
"blob": "[]byte",
"longblob": "[]byte",
"mediumblob": "[]byte",
"tinyblob": "[]byte",
"ltree": "[]byte",
}
@ -192,6 +192,8 @@ func mayConvertNullType(goDataType string, isDefaultNull, unsigned, strict bool)
return "sql.NullString"
case "time.Time":
return "sql.NullTime"
case "decimal":
return "*decimal.Decimal"
default:
if unsigned {
ret, ok := unsignedTypeMap[goDataType]

@ -268,7 +268,7 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er
table.ContainsUniqueCacheKey = len(uniqueKey) > 0
table.ignoreColumns = g.ignoreColumns
importsCode, err := genImports(table, withCache, in.ContainsTime())
importsCode, err := genImports(table, withCache, in.ContainsTime(), in.ContainsDecimal())
if err != nil {
return "", err
}

@ -6,7 +6,7 @@ import (
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
)
func genImports(table Table, withCache, timeImport bool) (string, error) {
func genImports(table Table, withCache, timeImport, decimalImport bool) (string, error) {
if withCache {
text, err := pathx.LoadTemplate(category, importsTemplateFile, template.Imports)
if err != nil {
@ -17,6 +17,7 @@ func genImports(table Table, withCache, timeImport bool) (string, error) {
"time": timeImport,
"containsPQ": table.ContainsPQ,
"data": table,
"decimal": decimalImport,
})
if err != nil {
return "", err
@ -34,6 +35,7 @@ func genImports(table Table, withCache, timeImport bool) (string, error) {
"time": timeImport,
"containsPQ": table.ContainsPQ,
"data": table,
"decimal": decimalImport,
})
if err != nil {
return "", err

@ -15,7 +15,11 @@ import (
"github.com/zeromicro/go-zero/tools/goctl/util/stringx"
)
const timeImport = "time.Time"
const (
timeImport = "time.Time"
decimalImport = "decimal.Decimal"
decimalImportPtr = "*decimal.Decimal"
)
type (
// Table describes a mysql table
@ -263,6 +267,16 @@ func (t *Table) ContainsTime() bool {
return false
}
// ContainsDecimal returns true if contains golang type decimal.Decimal
func (t *Table) ContainsDecimal() bool {
for _, item := range t.Fields {
if item.DataType == decimalImport || item.DataType == decimalImportPtr {
return true
}
}
return false
}
// ConvertDataType converts mysql data type into golang data type
func ConvertDataType(table *model.Table, strict bool) (*Table, error) {
isPrimaryDefaultNull := table.PrimaryKey.ColumnDefault == nil && table.PrimaryKey.IsNullAble == "YES"

@ -6,6 +6,7 @@ import (
{{if .time}}"time"{{end}}
{{if .containsPQ}}"github.com/lib/pq"{{end}}
{{if .decimal}}"github.com/shopspring/decimal"{{end}}
"github.com/zeromicro/go-zero/core/stores/builder"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"github.com/zeromicro/go-zero/core/stringx"

@ -6,6 +6,7 @@ import (
{{if .time}}"time"{{end}}
{{if .containsPQ}}"github.com/lib/pq"{{end}}
{{if .decimal}}"github.com/shopspring/decimal"{{end}}
"github.com/zeromicro/go-zero/core/stores/builder"
"github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/core/stores/sqlc"

@ -1 +1 @@
`db:"{{.field}}"`
`db:"{{.field}}" json:"{{.field}}"`
Loading…
Cancel
Save