1
0
mirror of https://github.com/ManyakRus/crud_generator.git synced 2025-01-17 08:56:48 +02:00

сделал StringIdentifier()

This commit is contained in:
Nikitin Aleksandr 2024-05-28 13:39:36 +03:00
parent 118503e1ae
commit 35e94c42a6
4 changed files with 83 additions and 9 deletions

View File

@ -11,6 +11,7 @@ import (
"github.com/ManyakRus/starter/micro" "github.com/ManyakRus/starter/micro"
"github.com/iancoleman/strcase" "github.com/iancoleman/strcase"
"github.com/jinzhu/inflection" "github.com/jinzhu/inflection"
"sort"
"strconv" "strconv"
"strings" "strings"
) )
@ -177,7 +178,15 @@ func FindPrimaryKeyColumn(Table1 *types.Table) (Column1 *types.Column) {
func FindPrimaryKeyColumns(Table1 *types.Table) []*types.Column { func FindPrimaryKeyColumns(Table1 *types.Table) []*types.Column {
Otvet := make([]*types.Column, 0) 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 { if Column1.IsPrimaryKey == true {
Otvet = append(Otvet, Column1) 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 { func ReplacePrimaryKeyOtvetID_Many(Text string, Table1 *types.Table) string {
Otvet := Text Otvet := Text
//сортировка по названию таблиц
keys := make([]string, 0, len(Table1.MapColumns))
for k := range Table1.MapColumns {
keys = append(keys, k)
}
sort.Strings(keys)
TextOtvetIDID := "" TextOtvetIDID := ""
for _, Column1 := range Table1.MapColumns { for _, key1 := range keys {
if Column1.IsPrimaryKey == false { Column1, _ := Table1.MapColumns[key1]
if Column1.IsPrimaryKey != true {
continue continue
} }
TextOtvetIDID = TextOtvetIDID + "\tOtvet." + Column1.NameGo + " = " + Column1.NameGo + "\n" TextOtvetIDID = TextOtvetIDID + "\tOtvet." + Column1.NameGo + " = " + Column1.NameGo + "\n"
} }
Otvet = strings.ReplaceAll(Otvet, "\tOtvet.ID = AliasFromInt(ID)", TextOtvetIDID) Otvet = strings.ReplaceAll(Otvet, "\tOtvet.ID = AliasFromInt(ID)", TextOtvetIDID)
//TextNames, TextNamesTypes, TextProtoNames := FindTextIDMany(Table1) //TextNames, TextNamesTypes, TextProtoNames := FindTextIDMany(Table1)
@ -817,7 +834,7 @@ func CheckAndAddImportStrconv(Text string) string {
Otvet := Text Otvet := Text
RepositoryURL := `strconv` RepositoryURL := `strconv`
Otvet = AddImport(Text, RepositoryURL) Otvet = CheckAndAddImport(Text, RepositoryURL)
return Otvet return Otvet
} }
@ -827,7 +844,7 @@ func CheckAndAddImportFmt(Text string) string {
Otvet := Text Otvet := Text
RepositoryURL := `fmt` RepositoryURL := `fmt`
Otvet = AddImport(Text, RepositoryURL) Otvet = CheckAndAddImport(Text, RepositoryURL)
return Otvet return Otvet
} }
@ -2085,6 +2102,36 @@ func FindTextIDMany(Table1 *types.Table) (TextNames, TextNamesTypes, TextProtoNa
//TextNamesTypes := "" //TextNamesTypes := ""
//TextNames := "" //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 := "" Comma := ""
MassPrimaryKey := FindPrimaryKeyColumns(Table1) MassPrimaryKey := FindPrimaryKeyColumns(Table1)
for _, PrimaryKey1 := range MassPrimaryKey { for _, PrimaryKey1 := range MassPrimaryKey {
@ -2095,7 +2142,7 @@ func FindTextIDMany(Table1 *types.Table) (TextNames, TextNamesTypes, TextProtoNa
TextProtoNames = TextProtoNames + Comma + OtvetColumnName TextProtoNames = TextProtoNames + Comma + OtvetColumnName
TextNamesTypes = TextNamesTypes + Comma + PrimaryKey1.NameGo + " " + PrimaryKey1.TypeGo TextNamesTypes = TextNamesTypes + Comma + PrimaryKey1.NameGo + " " + PrimaryKey1.TypeGo
TextNames = TextNames + Comma + PrimaryKey1.NameGo TextNames = TextNames + Comma + VariableName + Dot + PrimaryKey1.NameGo
Comma = ", " Comma = ", "
} }
@ -2212,3 +2259,17 @@ func FindTextConvertToString(Column1 *types.Column, VariableName string) string
return Otvet 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
}

View File

@ -171,6 +171,9 @@ func CreateFiles(Table1 *types.Table) error {
TextDB = ReplaceText_created_at(TextDB, Table1) TextDB = ReplaceText_created_at(TextDB, Table1)
TextDB = ReplaceText_is_deleted_deleted_at(TextDB, Table1) TextDB = ReplaceText_is_deleted_deleted_at(TextDB, Table1)
TextDB = create_files.DeleteImportModel(TextDB) TextDB = create_files.DeleteImportModel(TextDB)
TextDB = create_files.ReplaceCacheRemove_ManyPK(TextDB, Table1)
TextDB = create_files.ReplacePrimaryKeyM_ID(TextDB, Table1) TextDB = create_files.ReplacePrimaryKeyM_ID(TextDB, Table1)
//замена импортов на новые URL //замена импортов на новые URL
@ -643,6 +646,8 @@ func FindTextUpdateEveryColumn1(TextCrudUpdateFunc string, Table1 *types.Table,
FuncName := "Update_" + ColumnName FuncName := "Update_" + ColumnName
TextRequest, TextRequestFieldName := create_files.FindTextProtobufRequest(Table1) TextRequest, TextRequestFieldName := create_files.FindTextProtobufRequest(Table1)
Otvet = create_files.ReplaceCacheRemove_ManyPK(Otvet, Table1)
//запись null в nullable колонки //запись null в nullable колонки
if Column1.IsNullable == true && (Column1.TableKey != "" || Column1.TypeGo == "time.Time") { if Column1.IsNullable == true && (Column1.TableKey != "" || Column1.TypeGo == "time.Time") {
} else { } else {

View File

@ -738,7 +738,15 @@ func StringIdentifier(` + TextNamesTypes + `) string {
Otvet := "" 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 { if Column1.IsPrimaryKey == false {
continue continue
} }

View File

@ -179,7 +179,7 @@ func LastWord(StringFrom string) string {
} }
r := []rune(StringFrom) r := []rune(StringFrom)
for f := len(r); f >= 0; f-- { for f := len(r); f > 0; f-- {
r1 := r[f-1] r1 := r[f-1]
if r1 == '_' { if r1 == '_' {
} else if unicode.IsLetter(r1) == false && unicode.IsDigit(r1) == false { } else if unicode.IsLetter(r1) == false && unicode.IsDigit(r1) == false {