1
0
mirror of https://github.com/ManyakRus/crud_generator.git synced 2024-11-27 08:40:53 +02:00

сделал many pk

This commit is contained in:
Nikitin Aleksandr 2024-06-04 16:53:15 +03:00
parent 526c16aa23
commit ca5f45e478
6 changed files with 218 additions and 59 deletions

View File

@ -277,13 +277,16 @@ func ReplacePrimaryKeyOtvetID1(Text string, Table1 *types.Table) string {
func ReplacePrimaryKeyOtvetID_Many(Text string, Table1 *types.Table) string {
Otvet := Text
Otvet = ReplacePrimaryKeyOtvetID_Many1(Text, Table1, "Otvet")
Otvet = ReplacePrimaryKeyOtvetID_ManyPK1(Otvet, Table1, "Otvet")
Otvet = ReplacePrimaryKeyOtvetID_ManyPK1(Otvet, Table1, "Request")
Otvet = ReplacePrimaryKeyOtvetID1(Otvet, Table1) //для тестов
return Otvet
}
// ReplacePrimaryKeyOtvetID_Many1 - заменяет "Otvet.ID" на название колонки PrimaryKey
func ReplacePrimaryKeyOtvetID_Many1(Text string, Table1 *types.Table, VariableName string) string {
// ReplacePrimaryKeyOtvetID_ManyPK1 - заменяет "Otvet.ID" на название колонки PrimaryKey
func ReplacePrimaryKeyOtvetID_ManyPK1(Text string, Table1 *types.Table, VariableName string) string {
Otvet := Text
//сортировка по названию таблиц
@ -295,6 +298,9 @@ func ReplacePrimaryKeyOtvetID_Many1(Text string, Table1 *types.Table, VariableNa
TextIDRequestID := ""
TextOtvetIDID := ""
TextRequestIDmID := ""
TextRequestIDInt64ID := ""
TextOtvetIDmID := ""
for _, key1 := range keys {
Column1, _ := Table1.MapColumns[key1]
if Column1.IsPrimaryKey != true {
@ -303,16 +309,21 @@ func ReplacePrimaryKeyOtvetID_Many1(Text string, Table1 *types.Table, VariableNa
TextOtvetIDID = TextOtvetIDID + "\t" + VariableName + "." + Column1.NameGo + " = " + Column1.NameGo + "\n"
RequestColumnName := FindRequestColumnName(Table1, Column1)
TextIDRequestID = TextIDRequestID + "\t" + Column1.NameGo + " := Request." + RequestColumnName + "\n"
TextM := ConvertColumnToAlias(Otvet, Table1, Column1, "m")
TextRequestIDmID = TextRequestIDmID + "\t" + VariableName + "." + RequestColumnName + " = " + TextM + "\n"
TextInt64ID := FindTextConvertGolangTypeToProtobufType(Table1, Column1, "")
TextRequestIDInt64ID = TextRequestIDInt64ID + "\t" + VariableName + "." + RequestColumnName + " = " + TextInt64ID + "\n"
TextOtvetIDmID = TextOtvetIDmID + "\t" + "Otvet." + Column1.NameGo + " = " + VariableName + "." + Column1.NameGo + "\n"
}
Otvet = strings.ReplaceAll(Otvet, "\t"+VariableName+".ID = AliasFromInt(ID)", TextOtvetIDID)
Otvet = strings.ReplaceAll(Otvet, "\t"+VariableName+".ID = IntFromAlias(m.ID)", TextRequestIDmID)
Otvet = strings.ReplaceAll(Otvet, "\t"+VariableName+".ID = int64(ID)", TextRequestIDInt64ID)
Otvet = strings.ReplaceAll(Otvet, "\tOtvet.ID = "+VariableName+".ID\n", TextOtvetIDmID)
//заменим ID := Request.ID
Otvet = strings.ReplaceAll(Otvet, "\tID := Request.ID\n", TextIDRequestID)
//заменим int64(m.ID)
Otvet = ReplacePrimaryKeyM_ID1(Otvet, Table1)
return Otvet
}
@ -334,7 +345,11 @@ func ReplacePrimaryKeyM_ID(Text string, Table1 *types.Table) string {
func ReplacePrimaryKeyM_ManyPK(Text string, Table1 *types.Table) string {
Otvet := Text
Otvet = ReplacePrimaryKeyOtvetID_Many1(Text, Table1, "m")
Otvet = ReplacePrimaryKeyOtvetID_ManyPK1(Otvet, Table1, "m")
Otvet = ReplacePrimaryKeyOtvetID_ManyPK1(Otvet, Table1, "Request")
//заменим int64(m.ID)
Otvet = ReplacePrimaryKeyM_ID1(Otvet, Table1)
return Otvet
}
@ -1654,12 +1669,7 @@ func ConvertRequestIdToAlias(Text string, Table1 *types.Table) string {
return Otvet
}
URL := FindURL_Alias()
if URL == "" {
return Otvet
}
Otvet = AddImport(Otvet, URL)
Otvet = CheckAndAddImportAlias(Otvet)
return Otvet
}
@ -1694,6 +1704,29 @@ func ConvertIDToAlias_OtvetID(Text string, Table1 *types.Table) string {
return Otvet
}
// ConvertColumnToAlias - заменяет "Otvet.ID = ID" на "Otvet.ID = alias.Name(ID)"
func ConvertColumnToAlias(Text string, Table1 *types.Table, Column1 *types.Column, VariableName string) string {
Otvet := VariableName + "." + Column1.NameGo
TableName := Table1.Name
IDName := Column1.Name
TextConvert, ok := types.MapConvertID[TableName+"."+IDName]
if ok == false {
return Otvet
}
if TextConvert[:6] != "alias." {
return Otvet
}
Otvet = TextConvert + "(" + VariableName + "." + Column1.NameGo + ")"
//добавим испорт
Text = CheckAndAddImportAlias(Text)
return Otvet
}
// DeleteCommentFromString - удаляет комментарий из строки //
func DeleteCommentFromString(TextFrom string) string {
Otvet := TextFrom
@ -2380,6 +2413,19 @@ func ReplaceIDtoID_Many(Text string, Table1 *types.Table) string {
func ReplaceOtvetIDEqual1(Text string, Table1 *types.Table) string {
Otvet := Text
if Table1.PrimaryKeyColumnsCount == 1 {
Otvet = ReplaceOtvetIDEqual1_1(Text, Table1)
} else {
Otvet = ReplaceOtvetIDEqual1_ManyPK(Text, Table1)
}
return Otvet
}
// ReplaceOtvetIDEqual1_1 - заменяет Otvet.ID = -1
func ReplaceOtvetIDEqual1_1(Text string, Table1 *types.Table) string {
Otvet := Text
PrimaryKeyColumn := FindPrimaryKeyColumn(Table1)
if PrimaryKeyColumn == nil {
return Otvet
@ -2392,6 +2438,48 @@ func ReplaceOtvetIDEqual1(Text string, Table1 *types.Table) string {
return Otvet
}
// ReplaceOtvetIDEqual1_ManyPK - заменяет Otvet.ID = -1
func ReplaceOtvetIDEqual1_ManyPK(Text string, Table1 *types.Table) string {
Otvet := Text
PrimaryKeyColumns := FindPrimaryKeyColumns(Table1)
if len(PrimaryKeyColumns) == 0 {
return Otvet
}
TextFind := "\tOtvet.ID = -1\n"
TextNew := ""
for _, ColumnPK1 := range PrimaryKeyColumns {
Value := FindNegativeValue(ColumnPK1.TypeGo)
TextNew = TextNew + "\tOtvet." + ColumnPK1.NameGo + " = " + Value + "\n"
}
Otvet = strings.ReplaceAll(Otvet, TextFind, TextNew)
return Otvet
}
// ReplaceOtvetIDEqual0 - заменяет Otvet.ID = -1
func ReplaceOtvetIDEqual0(Text string, Table1 *types.Table) string {
Otvet := Text
PrimaryKeyColumns := FindPrimaryKeyColumns(Table1)
if len(PrimaryKeyColumns) == 0 {
return Otvet
}
TextFind := "\tOtvet.ID = 0\n"
TextNew := ""
for _, ColumnPK1 := range PrimaryKeyColumns {
Value := FindTextDefaultValue(ColumnPK1.TypeGo)
TextNew = TextNew + "\tOtvet." + ColumnPK1.NameGo + " = " + Value + "\n"
}
Otvet = strings.ReplaceAll(Otvet, TextFind, TextNew)
return Otvet
}
// ReplaceModelIDEqual1 - заменяет Otvet.ID = -1
func ReplaceModelIDEqual1(Text string, Table1 *types.Table) string {
Otvet := Text

View File

@ -118,10 +118,17 @@ import (`
log.Panic("MapAll[key1] not found, key: ", key1)
}
////проверка что таблица нормальная
//err1 := create_files.IsGoodTable(Table1)
//if err1 != nil {
// log.Warn(err1)
// continue
//}
//проверка что таблица нормальная
err1 := create_files.IsGoodTable(Table1)
if err1 != nil {
log.Warn(err1)
err2 := create_files.IsGoodTableNamePrefix(Table1)
if err2 != nil {
log.Warn(err2)
continue
}
@ -208,10 +215,17 @@ func InitCrudTransport_DB() {`
log.Panic("MapAll[key1] not found, key: ", key1)
}
////проверка что таблица нормальная
//err1 := create_files.IsGoodTable(Table1)
//if err1 != nil {
// log.Warn(err1)
// continue
//}
//проверка что таблица нормальная
err1 := create_files.IsGoodTable(Table1)
if err1 != nil {
log.Warn(err1)
err2 := create_files.IsGoodTableNamePrefix(Table1)
if err2 != nil {
log.Warn(err2)
continue
}
@ -256,10 +270,17 @@ func InitCrudTransport_GRPC() {`
log.Panic("MapAll[key1] not found, key: ", key1)
}
////проверка что таблица нормальная
//err1 := create_files.IsGoodTable(Table1)
//if err1 != nil {
// log.Warn(err1)
// continue
//}
//проверка что таблица нормальная
err1 := create_files.IsGoodTable(Table1)
if err1 != nil {
log.Warn(err1)
err2 := create_files.IsGoodTableNamePrefix(Table1)
if err2 != nil {
log.Warn(err2)
continue
}
@ -303,10 +324,17 @@ func InitCrudTransport_NRPC() {`
log.Panic("MapAll[key1] not found, key: ", key1)
}
////проверка что таблица нормальная
//err1 := create_files.IsGoodTable(Table1)
//if err1 != nil {
// log.Warn(err1)
// continue
//}
//проверка что таблица нормальная
err1 := create_files.IsGoodTable(Table1)
if err1 != nil {
log.Warn(err1)
err2 := create_files.IsGoodTableNamePrefix(Table1)
if err2 != nil {
log.Warn(err2)
continue
}
@ -408,10 +436,17 @@ func initCrudTransport_manual_DB() {`
log.Panic("MapAll[key1] not found, key: ", key1)
}
////проверка что таблица нормальная
//err1 := create_files.IsGoodTable(Table1)
//if err1 != nil {
// log.Warn(err1)
// continue
//}
//проверка что таблица нормальная
err1 := create_files.IsGoodTable(Table1)
if err1 != nil {
log.Warn(err1)
err2 := create_files.IsGoodTableNamePrefix(Table1)
if err2 != nil {
log.Warn(err2)
continue
}
@ -455,10 +490,17 @@ func initCrudTransport_manual_GRPC() {`
log.Panic("MapAll[key1] not found, key: ", key1)
}
////проверка что таблица нормальная
//err1 := create_files.IsGoodTable(Table1)
//if err1 != nil {
// log.Warn(err1)
// continue
//}
//проверка что таблица нормальная
err1 := create_files.IsGoodTable(Table1)
if err1 != nil {
log.Warn(err1)
err2 := create_files.IsGoodTableNamePrefix(Table1)
if err2 != nil {
log.Warn(err2)
continue
}
@ -501,10 +543,17 @@ func initCrudTransport_manual_NRPC() {`
log.Panic("MapAll[key1] not found, key: ", key1)
}
////проверка что таблица нормальная
//err1 := create_files.IsGoodTable(Table1)
//if err1 != nil {
// log.Warn(err1)
// continue
//}
//проверка что таблица нормальная
err1 := create_files.IsGoodTable(Table1)
if err1 != nil {
log.Warn(err1)
err2 := create_files.IsGoodTableNamePrefix(Table1)
if err2 != nil {
log.Warn(err2)
continue
}

View File

@ -17,10 +17,17 @@ func CreateAllFiles(MapAll map[string]*types.Table) error {
var err error
for _, Table1 := range MapAll {
////проверка что таблица нормальная
//err1 := create_files.IsGoodTable(Table1)
//if err1 != nil {
// log.Warn(err1)
// continue
//}
//проверка что таблица нормальная
err1 := create_files.IsGoodTable(Table1)
if err1 != nil {
log.Warn(err1)
err2 := create_files.IsGoodTableNamePrefix(Table1)
if err2 != nil {
log.Warn(err2)
continue
}

View File

@ -876,16 +876,9 @@ func CreateFilesCache(Table1 *types.Table) error {
ModelTableURL := create_files.FindModelTableURL(TableName)
TextCache = create_files.AddImport(TextCache, ModelTableURL)
TextCache = create_files.ReplacePrimaryKeyOtvetID(TextCache, Table1)
//TextCache = create_files.ConvertRequestIdToAlias(TextCache, Table1)
}
//замена слов
ModelName := Table1.NameGo
TextCache = strings.ReplaceAll(TextCache, config.Settings.TEXT_TEMPLATE_MODEL, ModelName)
TextCache = strings.ReplaceAll(TextCache, config.Settings.TEXT_TEMPLATE_TABLENAME, Table1.Name)
TextCache = config.Settings.TEXT_MODULE_GENERATED + TextCache
//alias
TextCache = create_files.ConvertIDToAlias_OtvetID(TextCache, Table1)
@ -901,6 +894,7 @@ func CreateFilesCache(Table1 *types.Table) error {
_, ColumnTypeGo := create_files.FindPrimaryKeyNameTypeGo(Table1)
TextCache = strings.ReplaceAll(TextCache, "LRU[int64", "LRU["+ColumnTypeGo)
TextCache = strings.ReplaceAll(TextCache, "ID int64", "ID "+ColumnTypeGo)
TextCache = create_files.ReplacePrimaryKeyOtvetID(TextCache, Table1)
} else {
TextCache = strings.ReplaceAll(TextCache, "LRU[int64", "LRU[string")
TextCache = create_files.ReplacePrimaryKeyOtvetID_Many(TextCache, Table1)
@ -913,6 +907,12 @@ func CreateFilesCache(Table1 *types.Table) error {
//uuid
TextCache = create_files.CheckAndAddImportUUID_FromText(TextCache)
//замена слов
ModelName := Table1.NameGo
TextCache = strings.ReplaceAll(TextCache, config.Settings.TEXT_TEMPLATE_MODEL, ModelName)
TextCache = strings.ReplaceAll(TextCache, config.Settings.TEXT_TEMPLATE_TABLENAME, Table1.Name)
TextCache = config.Settings.TEXT_MODULE_GENERATED + TextCache
//удаление пустого импорта
TextCache = create_files.DeleteEmptyImport(TextCache)

View File

@ -18,10 +18,17 @@ func CreateAllFiles(MapAll map[string]*types.Table) error {
var err error
for _, Table1 := range MapAll {
////проверка что таблица нормальная
//err1 := create_files.IsGoodTable(Table1)
//if err1 != nil {
// log.Warn(err1)
// continue
//}
//проверка что таблица нормальная
err1 := create_files.IsGoodTable(Table1)
if err1 != nil {
log.Warn(err1)
err2 := create_files.IsGoodTableNamePrefix(Table1)
if err2 != nil {
log.Warn(err2)
continue
}
@ -113,11 +120,6 @@ func CreateFiles(Table1 *types.Table) error {
TextGRPCClient = create_files.ReplacePackageName(TextGRPCClient, DirReadyTable)
//создание текста
ModelName := Table1.NameGo
TextGRPCClient = strings.ReplaceAll(TextGRPCClient, config.Settings.TEXT_TEMPLATE_MODEL, ModelName)
TextGRPCClient = strings.ReplaceAll(TextGRPCClient, config.Settings.TEXT_TEMPLATE_TABLENAME, Table1.Name)
TextGRPCClient = config.Settings.TEXT_MODULE_GENERATED + TextGRPCClient
//заменим импорты
if config.Settings.USE_DEFAULT_TEMPLATE == true {
//TextGRPCClient = create_files.ReplaceServiceURLImports(TextGRPCClient)
@ -166,6 +168,12 @@ func CreateFiles(Table1 *types.Table) error {
TextGRPCClient = create_files.DeleteFuncRestore(TextGRPCClient, Table1)
TextGRPCClient = create_files.DeleteFuncFind_byExtID(TextGRPCClient, Table1)
//замена имени таблицы
ModelName := Table1.NameGo
TextGRPCClient = strings.ReplaceAll(TextGRPCClient, config.Settings.TEXT_TEMPLATE_MODEL, ModelName)
TextGRPCClient = strings.ReplaceAll(TextGRPCClient, config.Settings.TEXT_TEMPLATE_TABLENAME, Table1.Name)
TextGRPCClient = config.Settings.TEXT_MODULE_GENERATED + TextGRPCClient
//удаление пустого импорта
TextGRPCClient = create_files.DeleteEmptyImport(TextGRPCClient)
@ -222,6 +230,9 @@ func CreateFilesTest(Table1 *types.Table) error {
//замена Otvet.ID = -1
TextGRPCClient = create_files.ReplaceOtvetIDEqual1(TextGRPCClient, Table1)
//замена Otvet.ID = 0
TextGRPCClient = create_files.ReplaceOtvetIDEqual0(TextGRPCClient, Table1)
//замена ID на PrimaryKey
TextGRPCClient = create_files.ReplacePrimaryKeyOtvetID(TextGRPCClient, Table1)
@ -514,6 +525,8 @@ func FindTextUpdateEveryColumn1(TextGRPC_ClientUpdateFunc string, Table1 *types.
ColumnName := Column1.NameGo
FuncName := "Update_" + ColumnName
_, TextRequestFieldName, _, _ := create_files.FindTextProtobufRequest_ID_Type(Table1, Column1, "Request.")
TextRequest := create_files.FindTextProtobufRequest_Column_ManyPK(Table1, Column1)
Otvet = strings.ReplaceAll(Otvet, "grpc_proto.RequestId", "grpc_proto."+TextRequest)
//замена RequestId{}
Otvet = create_files.ReplaceTextRequestID_and_Column(Otvet, Table1, Column1)
@ -532,7 +545,6 @@ func FindTextUpdateEveryColumn1(TextGRPC_ClientUpdateFunc string, Table1 *types.
Otvet = strings.ReplaceAll(Otvet, " Update(", " "+FuncName+"(")
Otvet = strings.ReplaceAll(Otvet, config.Settings.TEXT_TEMPLATE_MODEL, ModelName)
Otvet = strings.ReplaceAll(Otvet, config.Settings.TEXT_TEMPLATE_TABLENAME, Table1.Name)
//Otvet = strings.ReplaceAll(Otvet, "grpc_proto.RequestId", "grpc_proto."+TextRequest)
Otvet = strings.ReplaceAll(Otvet, "m.ColumnName", ColumnNameGolang)
Otvet = strings.ReplaceAll(Otvet, "int64(m.ID)", " "+IDTypeGo+"(m.ID)")
Otvet = strings.ReplaceAll(Otvet, "ColumnName", ColumnName)
@ -772,6 +784,9 @@ func CreateFiles_GRPC_Client_Cache(Table1 *types.Table) error {
GRPC_NRPC_URL := create_files.Find_GRPC_NRPC_URL()
TextGRPCClient = create_files.AddImport(TextGRPCClient, GRPC_NRPC_URL)
//замена Request.ID = Int64(ID)
TextGRPCClient = create_files.ReplacePrimaryKeyM_ID(TextGRPCClient, Table1)
//замена RequestId{}
TextGRPCClient = create_files.ReplaceTextRequestID_PrimaryKey(TextGRPCClient, Table1)

View File

@ -77,12 +77,12 @@ func CreateFileProto(MapAll map[string]*types.Table) error {
log.Panic("MapAll[key1] not found")
}
//проверка что таблица нормальная
err1 := create_files.IsGoodTable(Table1)
if err1 != nil {
log.Warn(err1)
continue
}
////проверка что таблица нормальная
//err1 := create_files.IsGoodTable(Table1)
//if err1 != nil {
// log.Warn(err1)
// continue
//}
//проверка что таблица нормальная
err2 := create_files.IsGoodTableNamePrefix(Table1)