Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(goctl): Fix issues #2543

Merged
merged 5 commits into from Oct 22, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions tools/goctl/model/sql/converter/types.go
Expand Up @@ -77,6 +77,7 @@ var commonMysqlDataTypeMapString = map[string]string{
// For consistency, all integer types are converted to int64
// bool
"bool": "bool",
"_bool": "pq.BoolArray",
"boolean": "bool",
// number
"tinyint": "int64",
Expand All @@ -85,14 +86,20 @@ var commonMysqlDataTypeMapString = map[string]string{
"int": "int64",
"int1": "int64",
"int2": "int64",
"_int2": "pq.Int64Array",
"int3": "int64",
"int4": "int64",
"_int4": "pq.Int64Array",
"int8": "int64",
"_int8": "pq.Int64Array",
"integer": "int64",
"_integer": "pq.Int64Array",
"bigint": "int64",
"float": "float64",
"float4": "float64",
"_float4": "pq.Float64Array",
"float8": "float64",
"_float8": "pq.Float64Array",
"double": "float64",
"decimal": "float64",
"dec": "float64",
Expand All @@ -111,14 +118,17 @@ var commonMysqlDataTypeMapString = map[string]string{
"nvarchar": "string",
"nchar": "string",
"char": "string",
"_char": "pq.StringArray",
"character": "string",
"varchar": "string",
"_varchar": "pq.StringArray",
"binary": "string",
"bytea": "string",
"longvarbinary": "string",
"varbinary": "string",
"tinytext": "string",
"text": "string",
"_text": "pq.StringArray",
"mediumtext": "string",
"longtext": "string",
"enum": "string",
Expand All @@ -129,6 +139,7 @@ var commonMysqlDataTypeMapString = map[string]string{
"longblob": "string",
"mediumblob": "string",
"tinyblob": "string",
"ltree": "[]byte",
}

// ConvertDataType converts mysql column type into golang type
Expand All @@ -143,11 +154,16 @@ func ConvertDataType(dataBaseType int, isDefaultNull, unsigned, strict bool) (st

// ConvertStringDataType converts mysql column type into golang type
func ConvertStringDataType(dataBaseType string, isDefaultNull, unsigned, strict bool) (string, error) {
var isArray = strings.HasPrefix(dataBaseType, "_")
tp, ok := commonMysqlDataTypeMapString[strings.ToLower(dataBaseType)]
if !ok {
return "", fmt.Errorf("unsupported database type: %s", dataBaseType)
}

if isArray {
kesonan marked this conversation as resolved.
Show resolved Hide resolved
return tp, nil
}

return mayConvertNullType(tp, isDefaultNull, unsigned, strict), nil
}

Expand Down
7 changes: 6 additions & 1 deletion tools/goctl/model/sql/template/model.go
Expand Up @@ -10,11 +10,16 @@ import (
const ModelCustom = `package {{.pkg}}
{{if .withCache}}
import (
"github.com/lib/pq"
"github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/core/stores/sqlx"
)
{{else}}
import "github.com/zeromicro/go-zero/core/stores/sqlx"

import (
"github.com/lib/pq"
"github.com/zeromicro/go-zero/core/stores/sqlx"
)
{{end}}
var _ {{.upperStartCamelObject}}Model = (*custom{{.upperStartCamelObject}}Model)(nil)

Expand Down