modify model
This commit is contained in:
@@ -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}}"`
|
||||
Reference in New Issue
Block a user