From 35e94c42a6193c90f0eadedf5642503ae0e8b5b8 Mon Sep 17 00:00:00 2001 From: Nikitin Aleksandr Date: Tue, 28 May 2024 13:39:36 +0300 Subject: [PATCH] =?UTF-8?q?=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20StringId?= =?UTF-8?q?entifier()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/create_files/create_files.go | 75 +++++++++++++++++-- .../create_files/crud_tables/crud_tables.go | 5 ++ .../entities_tables/entities_tables.go | 10 ++- .../ManyakRus/starter/micro/microfunctions.go | 2 +- 4 files changed, 83 insertions(+), 9 deletions(-) diff --git a/internal/create_files/create_files.go b/internal/create_files/create_files.go index afdd0b4..c4bc76a 100644 --- a/internal/create_files/create_files.go +++ b/internal/create_files/create_files.go @@ -11,6 +11,7 @@ import ( "github.com/ManyakRus/starter/micro" "github.com/iancoleman/strcase" "github.com/jinzhu/inflection" + "sort" "strconv" "strings" ) @@ -177,7 +178,15 @@ func FindPrimaryKeyColumn(Table1 *types.Table) (Column1 *types.Column) { func FindPrimaryKeyColumns(Table1 *types.Table) []*types.Column { Otvet := make([]*types.Column, 0) - for _, Column1 := range Table1.MapColumns { + //сортировка по названию таблиц + keys := make([]string, 0, len(Table1.MapColumns)) + for k := range Table1.MapColumns { + keys = append(keys, k) + } + sort.Strings(keys) + + for _, key1 := range keys { + Column1, _ := Table1.MapColumns[key1] if Column1.IsPrimaryKey == true { Otvet = append(Otvet, Column1) } @@ -268,14 +277,22 @@ func ReplacePrimaryKeyOtvetID1(Text string, Table1 *types.Table) string { func ReplacePrimaryKeyOtvetID_Many(Text string, Table1 *types.Table) string { Otvet := Text + //сортировка по названию таблиц + keys := make([]string, 0, len(Table1.MapColumns)) + for k := range Table1.MapColumns { + keys = append(keys, k) + } + sort.Strings(keys) + TextOtvetIDID := "" - for _, Column1 := range Table1.MapColumns { - if Column1.IsPrimaryKey == false { + for _, key1 := range keys { + Column1, _ := Table1.MapColumns[key1] + if Column1.IsPrimaryKey != true { continue } - TextOtvetIDID = TextOtvetIDID + "\tOtvet." + Column1.NameGo + " = " + Column1.NameGo + "\n" } + Otvet = strings.ReplaceAll(Otvet, "\tOtvet.ID = AliasFromInt(ID)", TextOtvetIDID) //TextNames, TextNamesTypes, TextProtoNames := FindTextIDMany(Table1) @@ -817,7 +834,7 @@ func CheckAndAddImportStrconv(Text string) string { Otvet := Text RepositoryURL := `strconv` - Otvet = AddImport(Text, RepositoryURL) + Otvet = CheckAndAddImport(Text, RepositoryURL) return Otvet } @@ -827,7 +844,7 @@ func CheckAndAddImportFmt(Text string) string { Otvet := Text RepositoryURL := `fmt` - Otvet = AddImport(Text, RepositoryURL) + Otvet = CheckAndAddImport(Text, RepositoryURL) return Otvet } @@ -2085,6 +2102,36 @@ func FindTextIDMany(Table1 *types.Table) (TextNames, TextNamesTypes, TextProtoNa //TextNamesTypes := "" //TextNames := "" + TextNames, TextNamesTypes, TextProtoNames = FindTextID_VariableName_Many(Table1, "") + + //Comma := "" + //MassPrimaryKey := FindPrimaryKeyColumns(Table1) + //for _, PrimaryKey1 := range MassPrimaryKey { + // OtvetColumnName := FindTextConvertGolangTypeToProtobufType(Table1, PrimaryKey1, "") + // if OtvetColumnName == "" { + // continue + // } + // + // TextProtoNames = TextProtoNames + Comma + OtvetColumnName + // TextNamesTypes = TextNamesTypes + Comma + PrimaryKey1.NameGo + " " + PrimaryKey1.TypeGo + // TextNames = TextNames + Comma + PrimaryKey1.NameGo + // + // Comma = ", " + //} + + return +} + +// FindTextID_VariableName_Many - находит все PrimaryKey строкой +func FindTextID_VariableName_Many(Table1 *types.Table, VariableName string) (TextNames, TextNamesTypes, TextProtoNames string) { + //TextProtoNames := "" + //TextNamesTypes := "" + //TextNames := "" + + Dot := "" + if VariableName != "" { + Dot = "." + } Comma := "" MassPrimaryKey := FindPrimaryKeyColumns(Table1) for _, PrimaryKey1 := range MassPrimaryKey { @@ -2095,7 +2142,7 @@ func FindTextIDMany(Table1 *types.Table) (TextNames, TextNamesTypes, TextProtoNa TextProtoNames = TextProtoNames + Comma + OtvetColumnName TextNamesTypes = TextNamesTypes + Comma + PrimaryKey1.NameGo + " " + PrimaryKey1.TypeGo - TextNames = TextNames + Comma + PrimaryKey1.NameGo + TextNames = TextNames + Comma + VariableName + Dot + PrimaryKey1.NameGo Comma = ", " } @@ -2212,3 +2259,17 @@ func FindTextConvertToString(Column1 *types.Column, VariableName string) string return Otvet } + +// ReplaceCacheRemove_ManyPK - заменяет cache.Remove(IntFromAlias(m.ID)) на cache.Remove(m.StringIdentifier()) +func ReplaceCacheRemove_ManyPK(Text string, Table1 *types.Table) string { + Otvet := Text + + if Table1.PrimaryKeyColumnsCount > 1 { + TextOld := "cache.Remove(IntFromAlias(m.ID))" + TextNames, _, _ := FindTextID_VariableName_Many(Table1, "m") + TextNew := "cache.Remove(" + Table1.Name + ".StringIdentifier(" + TextNames + "))" + Otvet = strings.ReplaceAll(Otvet, TextOld, TextNew) + } + + return Otvet +} diff --git a/internal/create_files/crud_tables/crud_tables.go b/internal/create_files/crud_tables/crud_tables.go index 50a1eee..f435b85 100644 --- a/internal/create_files/crud_tables/crud_tables.go +++ b/internal/create_files/crud_tables/crud_tables.go @@ -171,6 +171,9 @@ func CreateFiles(Table1 *types.Table) error { TextDB = ReplaceText_created_at(TextDB, Table1) TextDB = ReplaceText_is_deleted_deleted_at(TextDB, Table1) TextDB = create_files.DeleteImportModel(TextDB) + + TextDB = create_files.ReplaceCacheRemove_ManyPK(TextDB, Table1) + TextDB = create_files.ReplacePrimaryKeyM_ID(TextDB, Table1) //замена импортов на новые URL @@ -643,6 +646,8 @@ func FindTextUpdateEveryColumn1(TextCrudUpdateFunc string, Table1 *types.Table, FuncName := "Update_" + ColumnName TextRequest, TextRequestFieldName := create_files.FindTextProtobufRequest(Table1) + Otvet = create_files.ReplaceCacheRemove_ManyPK(Otvet, Table1) + //запись null в nullable колонки if Column1.IsNullable == true && (Column1.TableKey != "" || Column1.TypeGo == "time.Time") { } else { diff --git a/internal/create_files/entities_tables/entities_tables.go b/internal/create_files/entities_tables/entities_tables.go index 4db5e03..453e090 100644 --- a/internal/create_files/entities_tables/entities_tables.go +++ b/internal/create_files/entities_tables/entities_tables.go @@ -738,7 +738,15 @@ func StringIdentifier(` + TextNamesTypes + `) string { Otvet := "" ` - for _, Column1 := range Table1.MapColumns { + //сортировка по названию таблиц + keys := make([]string, 0, len(Table1.MapColumns)) + for k := range Table1.MapColumns { + keys = append(keys, k) + } + sort.Strings(keys) + + for _, key1 := range keys { + Column1, _ := Table1.MapColumns[key1] if Column1.IsPrimaryKey == false { continue } diff --git a/vendor/github.com/ManyakRus/starter/micro/microfunctions.go b/vendor/github.com/ManyakRus/starter/micro/microfunctions.go index 78216aa..779b102 100644 --- a/vendor/github.com/ManyakRus/starter/micro/microfunctions.go +++ b/vendor/github.com/ManyakRus/starter/micro/microfunctions.go @@ -179,7 +179,7 @@ func LastWord(StringFrom string) string { } r := []rune(StringFrom) - for f := len(r); f >= 0; f-- { + for f := len(r); f > 0; f-- { r1 := r[f-1] if r1 == '_' { } else if unicode.IsLetter(r1) == false && unicode.IsDigit(r1) == false {