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 issue #2572 #3301

Merged
merged 1 commit into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions cmd/gf/internal/cmd/cmd_z_unit_gen_dao_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"testing"

"github.com/gogf/gf/cmd/gf/v2/internal/cmd/gendao"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/test/gtest"
"github.com/gogf/gf/v2/text/gstr"
Expand Down Expand Up @@ -209,3 +210,83 @@ func Test_Gen_Dao_TypeMapping(t *testing.T) {
}
})
}

func execSqlFile(db gdb.DB, filePath string, args ...any) error {
sqlContent := fmt.Sprintf(
gfile.GetContents(filePath),
args...,
)
array := gstr.SplitAndTrim(sqlContent, ";")
for _, v := range array {
if _, err := db.Exec(ctx, v); err != nil {
return err
}
}
return nil
}

func Test_Gen_Dao_Issue2572(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
err error
db = testDB
table1 = "user1"
table2 = "user2"
issueDirPath = gtest.DataPath(`issue`, `2572`)
)
t.AssertNil(execSqlFile(db, gtest.DataPath(`issue`, `2572`, `sql1.sql`)))
t.AssertNil(execSqlFile(db, gtest.DataPath(`issue`, `2572`, `sql2.sql`)))
defer dropTableWithDb(db, table1)
defer dropTableWithDb(db, table2)

var (
path = gfile.Temp(guid.S())
group = "test"
in = gendao.CGenDaoInput{
Path: path,
Link: link,
Tables: "",
TablesEx: "",
Group: group,
Prefix: "",
RemovePrefix: "",
JsonCase: "SnakeScreaming",
ImportPrefix: "",
DaoPath: "",
DoPath: "",
EntityPath: "",
TplDaoIndexPath: "",
TplDaoInternalPath: "",
TplDaoDoPath: "",
TplDaoEntityPath: "",
StdTime: false,
WithTime: false,
GJsonSupport: false,
OverwriteDao: false,
DescriptionTag: false,
NoJsonTag: false,
NoModelComment: false,
Clear: false,
TypeMapping: nil,
}
)
err = gutil.FillStructWithDefault(&in)
t.AssertNil(err)

err = gfile.Copy(issueDirPath, path)
t.AssertNil(err)

defer gfile.Remove(path)

pwd := gfile.Pwd()
err = gfile.Chdir(path)
t.AssertNil(err)

defer gfile.Chdir(pwd)

_, err = gendao.CGenDao{}.Dao(ctx, in)
t.AssertNil(err)

fmt.Println(path)
})
}
20 changes: 19 additions & 1 deletion cmd/gf/internal/cmd/gendao/gendao.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ type (
NoModelComment bool `name:"noModelComment" short:"m" brief:"{CGenDaoBriefNoModelComment}" orphan:"true"`
Clear bool `name:"clear" short:"a" brief:"{CGenDaoBriefClear}" orphan:"true"`

TypeMapping map[DBFieldTypeName]CustomAttributeType `name:"typeMapping" short:"y" brief:"{CGenDaoBriefTypeMapping}" orphan:"true"`
TypeMapping map[DBFieldTypeName]CustomAttributeType `name:"typeMapping" short:"y" brief:"{CGenDaoBriefTypeMapping}" orphan:"true"`
generatedFilePaths *CGenDaoInternalGeneratedFilePaths
}
CGenDaoOutput struct{}

Expand All @@ -212,6 +213,13 @@ type (
NewTableNames []string
}

CGenDaoInternalGeneratedFilePaths struct {
DaoFilePaths []string
DaoInternalFilePaths []string
DoFilePaths []string
EntityFilePaths []string
}

DBFieldTypeName = string
CustomAttributeType struct {
Type string `brief:"custom attribute type name"`
Expand All @@ -220,6 +228,12 @@ type (
)

func (c CGenDao) Dao(ctx context.Context, in CGenDaoInput) (out *CGenDaoOutput, err error) {
in.generatedFilePaths = &CGenDaoInternalGeneratedFilePaths{
DaoFilePaths: make([]string, 0),
DaoInternalFilePaths: make([]string, 0),
DoFilePaths: make([]string, 0),
EntityFilePaths: make([]string, 0),
}
if g.Cfg().Available(ctx) {
v := g.Cfg().MustGet(ctx, CGenDaoConfig)
if v.IsSlice() {
Expand Down Expand Up @@ -333,6 +347,10 @@ func doGenDaoForArray(ctx context.Context, index int, in CGenDaoInput) {
TableNames: tableNames,
NewTableNames: newTableNames,
})

if in.Clear {
doClear(ctx, in)
}
}

func getImportPartContent(ctx context.Context, source string, isDo bool, appendImports []string) string {
Expand Down
17 changes: 11 additions & 6 deletions cmd/gf/internal/cmd/gendao/gendao_clear.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,24 @@ import (
"context"

"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/text/gstr"

"github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog"
"github.com/gogf/gf/cmd/gf/v2/internal/utility/utils"
)

func doClear(ctx context.Context, dirPath string, force bool) {
files, err := gfile.ScanDirFile(dirPath, "*.go", true)
func doClear(ctx context.Context, in CGenDaoInput) {
filePaths, err := gfile.ScanDirFile(in.Path, "*.go", true)
if err != nil {
mlog.Fatal(err)
}
for _, file := range files {
if force || utils.IsFileDoNotEdit(file) {
if err = gfile.Remove(file); err != nil {
var allGeneratedFilePaths = make([]string, 0)
allGeneratedFilePaths = append(allGeneratedFilePaths, in.generatedFilePaths.DaoFilePaths...)
allGeneratedFilePaths = append(allGeneratedFilePaths, in.generatedFilePaths.DaoInternalFilePaths...)
allGeneratedFilePaths = append(allGeneratedFilePaths, in.generatedFilePaths.EntityFilePaths...)
allGeneratedFilePaths = append(allGeneratedFilePaths, in.generatedFilePaths.DoFilePaths...)
for _, filePath := range filePaths {
if !gstr.InArray(allGeneratedFilePaths, filePath) {
if err = gfile.Remove(filePath); err != nil {
mlog.Print(err)
}
}
Expand Down
11 changes: 8 additions & 3 deletions cmd/gf/internal/cmd/gendao/gendao_dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ func generateDao(ctx context.Context, in CGenDaoInternalInput) {
dirPathDao = gfile.Join(in.Path, in.DaoPath)
dirPathDaoInternal = gfile.Join(dirPathDao, "internal")
)
if in.Clear {
doClear(ctx, dirPathDao, true)
}
for i := 0; i < len(in.TableNames); i++ {
generateDaoSingle(ctx, generateDaoSingleInput{
CGenDaoInternalInput: in,
Expand Down Expand Up @@ -118,6 +115,10 @@ func generateDaoIndex(in generateDaoIndexInput) {
tplVarTableNameCamelLowerCase: in.TableNameCamelLowerCase,
})
indexContent = replaceDefaultVar(in.CGenDaoInternalInput, indexContent)
in.generatedFilePaths.DaoFilePaths = append(
in.generatedFilePaths.DaoFilePaths,
path,
)
if err := gfile.PutContents(path, strings.TrimSpace(indexContent)); err != nil {
mlog.Fatalf("writing content to '%s' failed: %v", path, err)
} else {
Expand Down Expand Up @@ -151,6 +152,10 @@ func generateDaoInternal(in generateDaoInternalInput) {
tplVarColumnNames: gstr.Trim(generateColumnNamesForDao(in.FieldMap, removeFieldPrefixArray)),
})
modelContent = replaceDefaultVar(in.CGenDaoInternalInput, modelContent)
in.generatedFilePaths.DaoInternalFilePaths = append(
in.generatedFilePaths.DaoInternalFilePaths,
path,
)
if err := gfile.PutContents(path, strings.TrimSpace(modelContent)); err != nil {
mlog.Fatalf("writing content to '%s' failed: %v", path, err)
} else {
Expand Down
7 changes: 4 additions & 3 deletions cmd/gf/internal/cmd/gendao/gendao_do.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ import (

func generateDo(ctx context.Context, in CGenDaoInternalInput) {
var dirPathDo = filepath.FromSlash(gfile.Join(in.Path, in.DoPath))
if in.Clear {
doClear(ctx, dirPathDo, false)
}
in.NoJsonTag = true
in.DescriptionTag = false
in.NoModelComment = false
Expand Down Expand Up @@ -66,6 +63,10 @@ func generateDo(ctx context.Context, in CGenDaoInternalInput) {
gstr.CaseCamel(newTableName),
structDefinition,
)
in.generatedFilePaths.DoFilePaths = append(
in.generatedFilePaths.DoFilePaths,
doFilePath,
)
err = gfile.PutContents(doFilePath, strings.TrimSpace(modelContent))
if err != nil {
mlog.Fatalf(`writing content to "%s" failed: %v`, doFilePath, err)
Expand Down
8 changes: 4 additions & 4 deletions cmd/gf/internal/cmd/gendao/gendao_entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ import (

func generateEntity(ctx context.Context, in CGenDaoInternalInput) {
var dirPathEntity = gfile.Join(in.Path, in.EntityPath)
if in.Clear {
doClear(ctx, dirPathEntity, false)
}
// Model content.
for i, tableName := range in.TableNames {
fieldMap, err := in.DB.TableFields(ctx, tableName)
Expand All @@ -51,7 +48,10 @@ func generateEntity(ctx context.Context, in CGenDaoInternalInput) {
appendImports,
)
)

in.generatedFilePaths.EntityFilePaths = append(
in.generatedFilePaths.EntityFilePaths,
entityFilePath,
)
err = gfile.PutContents(entityFilePath, strings.TrimSpace(entityContent))
if err != nil {
mlog.Fatalf("writing content to '%s' failed: %v", entityFilePath, err)
Expand Down
20 changes: 20 additions & 0 deletions cmd/gf/internal/cmd/testdata/issue/2572/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
gfcli:
gen:
dao:
- link: "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
tables: "user1"
descriptionTag: true
noModelComment: true
group: "sys"
clear: true
overwriteDao: true
- link: "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
tables: "user2"
descriptionTag: true
noModelComment: true
group: "book"
clear: true
overwriteDao: true



85 changes: 85 additions & 0 deletions cmd/gf/internal/cmd/testdata/issue/2572/dao/internal/user_3.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading