From 9f2716f678250fe8aa827608be81c1b79bb8e3fb Mon Sep 17 00:00:00 2001 From: Nikitin Aleksandr Date: Wed, 25 Oct 2023 16:22:30 +0300 Subject: [PATCH] =?UTF-8?q?=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20db?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + .../{db_lawsuit_status_types.go_ => db.go_} | 0 ...suit_status_types_test.go_ => db_test.go_} | 0 internal/constants/constants.go | 2 +- internal/create_files/db/db.go | 359 ++++++++++++++++++ internal/create_files/db/db_test.go | 1 + internal/create_files/model/model.go | 57 ++- internal/logic/logic.go | 11 + internal/types/types.go | 2 + 9 files changed, 429 insertions(+), 4 deletions(-) rename bin/templates/db/{db_lawsuit_status_types.go_ => db.go_} (100%) rename bin/templates/db/{db_lawsuit_status_types_test.go_ => db_test.go_} (100%) create mode 100644 internal/create_files/db/db.go create mode 100644 internal/create_files/db/db_test.go diff --git a/.gitignore b/.gitignore index cbeebe3..d080d33 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ /database.graphml /database.graphml0 /bin/ready/pkg/model/ +/bin/ready/ diff --git a/bin/templates/db/db_lawsuit_status_types.go_ b/bin/templates/db/db.go_ similarity index 100% rename from bin/templates/db/db_lawsuit_status_types.go_ rename to bin/templates/db/db.go_ diff --git a/bin/templates/db/db_lawsuit_status_types_test.go_ b/bin/templates/db/db_test.go_ similarity index 100% rename from bin/templates/db/db_lawsuit_status_types_test.go_ rename to bin/templates/db/db_test.go_ diff --git a/internal/constants/constants.go b/internal/constants/constants.go index 28b2751..f2e6eb3 100644 --- a/internal/constants/constants.go +++ b/internal/constants/constants.go @@ -8,7 +8,7 @@ const FolderTemplates string = "templates" const FolderReady string = "ready" -const FILE_PERMISSIONS = 0600 +const FILE_PERMISSIONS = 0666 const TEXT_GENERATED = `//File generated automatic with crud_generator app //Do not change anything here. diff --git a/internal/create_files/db/db.go b/internal/create_files/db/db.go new file mode 100644 index 0000000..21c2dbb --- /dev/null +++ b/internal/create_files/db/db.go @@ -0,0 +1,359 @@ +package db + +import ( + "github.com/ManyakRus/crud_generator/internal/config" + "github.com/ManyakRus/crud_generator/internal/constants" + "github.com/ManyakRus/crud_generator/internal/create_files/model" + "github.com/ManyakRus/crud_generator/internal/types" + "github.com/ManyakRus/starter/log" + "github.com/ManyakRus/starter/micro" + "os" + "strings" +) + +// CreateDBFiles - создаёт все файлы в папке db +func CreateDBFiles(MapAll map[string]*types.Table) error { + var err error + + for _, table1 := range MapAll { + //файлы db + err = CreateDBFiles1(table1) + if err != nil { + log.Error("CreateDBFiles1() table: ", table1.Name, " error: ", err) + return err + } + + //тестовые файлы db + err = CreateDBTestFiles1(table1) + if err != nil { + log.Error("CreateDBTestFiles1() table: ", table1.Name, " error: ", err) + return err + } + } + + return err +} + +// CreateDBFiles1 - создаёт 1 файл в папке db +func CreateDBFiles1(Table1 *types.Table) error { + var err error + + //чтение файлов + DirBin := micro.ProgramDir_bin() + DirTemplates := DirBin + constants.FolderTemplates + micro.SeparatorFile() + DirReady := DirBin + constants.FolderReady + micro.SeparatorFile() + DirTemplatesDB := DirTemplates + config.Settings.TEMPLATE_FOLDERNAME_DB + micro.SeparatorFile() + DirReadyDB := DirReady + "pkg" + micro.SeparatorFile() + "db" + micro.SeparatorFile() + + FilenameTemplateDB := DirTemplatesDB + "db.go_" + TableName := strings.ToLower(Table1.Name) + FilenameReadyDB := DirReadyDB + TableName + micro.SeparatorFile() + TableName + ".go" + + //создадим каталог + DirTable := DirReadyDB + TableName + ok, err := micro.FileExists(DirTable) + if ok == false { + err = os.Mkdir(DirTable, 0777) + if err != nil { + log.Panic("Mkdir() ", DirTable, " error: ", err) + } + } + + bytes, err := os.ReadFile(FilenameTemplateDB) + if err != nil { + log.Panic("ReadFile() ", FilenameTemplateDB, " error: ", err) + } + TextDB := string(bytes) + + //создание текста + ModelName := Table1.NameGo + TextDB = strings.ReplaceAll(TextDB, config.Settings.TEXT_TEMPLATE_MODEL, ModelName) + TextDB = strings.ReplaceAll(TextDB, config.Settings.TEXT_TEMPLATE_TABLENAME, Table1.Name) + TextDB = constants.TEXT_GENERATED + TextDB + + if config.Settings.HAS_IS_DELETED == true { + TextDB = DeleteFuncDelete(TextDB, ModelName, Table1) + TextDB = DeleteFuncDeleteCtx(TextDB, ModelName, Table1) + TextDB = DeleteFuncRestore(TextDB, ModelName, Table1) + TextDB = DeleteFuncRestoreCtx(TextDB, ModelName, Table1) + } + TextDB = model.DeleteFuncFind_byExtID(TextDB, ModelName, Table1) + + //запись файла + err = os.WriteFile(FilenameReadyDB, []byte(TextDB), constants.FILE_PERMISSIONS) + + return err +} + +// CreateDBTestFiles1 - создаёт 1 файл в папке db +func CreateDBTestFiles1(Table1 *types.Table) error { + var err error + + //чтение файлов + DirBin := micro.ProgramDir_bin() + DirTemplates := DirBin + constants.FolderTemplates + micro.SeparatorFile() + DirReady := DirBin + constants.FolderReady + micro.SeparatorFile() + DirTemplatesDB := DirTemplates + config.Settings.TEMPLATE_FOLDERNAME_DB + micro.SeparatorFile() + DirReadyDB := DirReady + "pkg" + micro.SeparatorFile() + "db" + micro.SeparatorFile() + + FilenameTemplateDB := DirTemplatesDB + "db_test.go_" + TableName := strings.ToLower(Table1.Name) + FilenameReadyDB := DirReadyDB + TableName + micro.SeparatorFile() + TableName + "_test.go" + + //создадим каталог + DirTable := DirReadyDB + TableName + ok, err := micro.FileExists(DirTable) + if ok == false { + err = os.Mkdir(DirTable, 0777) + if err != nil { + log.Panic("Mkdir() ", DirTable, " error: ", err) + } + } + + bytes, err := os.ReadFile(FilenameTemplateDB) + if err != nil { + log.Panic("ReadFile() ", FilenameTemplateDB, " error: ", err) + } + TextDB := string(bytes) + + //создание текста + ModelName := Table1.NameGo + TextDB = strings.ReplaceAll(TextDB, config.Settings.TEXT_TEMPLATE_MODEL, ModelName) + TextDB = strings.ReplaceAll(TextDB, config.Settings.TEXT_TEMPLATE_TABLENAME, Table1.Name) + TextDB = constants.TEXT_GENERATED + TextDB + + if config.Settings.HAS_IS_DELETED == true { + TextDB = DeleteFuncDelete(TextDB, ModelName, Table1) + TextDB = DeleteFuncDeleteCtx(TextDB, ModelName, Table1) + TextDB = DeleteFuncRestore(TextDB, ModelName, Table1) + TextDB = DeleteFuncRestoreCtx(TextDB, ModelName, Table1) + } + TextDB = model.DeleteFuncFind_byExtID(TextDB, ModelName, Table1) + + //запись файла + err = os.WriteFile(FilenameReadyDB, []byte(TextDB), constants.FILE_PERMISSIONS) + + return err +} + +// DeleteFuncDelete - удаляет функцию Delete() +func DeleteFuncDelete(TextModel, ModelName string, Table1 *types.Table) string { + Otvet := TextModel + + _, ok := Table1.MapColumns["is_deleted"] + if ok == true { + return Otvet + } + + TextFind := "\n// Delete " + pos1 := strings.Index(Otvet, TextFind) + if pos1 < 0 { + return Otvet + } + s2 := Otvet[pos1+1:] + + posEnd := strings.Index(s2, "\n}") + if posEnd < 0 { + return Otvet + } + + Otvet = Otvet[:pos1-1] + Otvet[pos1+posEnd+3:] + + return Otvet +} + +// DeleteFuncRestore - удаляет функцию Restore() +func DeleteFuncRestore(TextModel, Modelname string, Table1 *types.Table) string { + Otvet := TextModel + + _, ok := Table1.MapColumns["is_deleted"] + if ok == true { + return Otvet + } + + TextFind := "\n// Restore " + pos1 := strings.Index(Otvet, TextFind) + if pos1 < 0 { + return Otvet + } + s2 := Otvet[pos1+1:] + + posEnd := strings.Index(s2, "\n}") + if posEnd < 0 { + return Otvet + } + + Otvet = Otvet[:pos1-1] + Otvet[pos1+posEnd+3:] + + return Otvet +} + +// DeleteFuncDeleteCtx - удаляет функцию Delete_ctx() +func DeleteFuncDeleteCtx(TextModel, ModelName string, Table1 *types.Table) string { + Otvet := TextModel + + _, ok := Table1.MapColumns["is_deleted"] + if ok == true { + return Otvet + } + + TextFind := "\n// Delete_ctx " + pos1 := strings.Index(Otvet, TextFind) + if pos1 < 0 { + return Otvet + } + s2 := Otvet[pos1+1:] + + posEnd := strings.Index(s2, "\n}") + if posEnd < 0 { + return Otvet + } + + Otvet = Otvet[:pos1-1] + Otvet[pos1+posEnd+3:] + + return Otvet +} + +// DeleteFuncRestoreCtx - удаляет функцию Restore_ctx() +func DeleteFuncRestoreCtx(TextModel, Modelname string, Table1 *types.Table) string { + Otvet := TextModel + + _, ok := Table1.MapColumns["is_deleted"] + if ok == true { + return Otvet + } + + TextFind := "\n// Restore_ctx " + pos1 := strings.Index(Otvet, TextFind) + if pos1 < 0 { + return Otvet + } + s2 := Otvet[pos1+1:] + + posEnd := strings.Index(s2, "\n}") + if posEnd < 0 { + return Otvet + } + + Otvet = Otvet[:pos1-1] + Otvet[pos1+posEnd+3:] + + return Otvet +} + +// DeleteFuncFind_byExtID - удаляет функцию Find_ByExtID() +func DeleteFuncFind_byExtID(TextModel, Modelname string, Table1 *types.Table) string { + Otvet := TextModel + + // + _, ok := Table1.MapColumns["ext_id"] + if ok == true { + return Otvet + } + + // + _, ok = Table1.MapColumns["connection_id"] + if ok == true { + return Otvet + } + + TextFind := "\n// Find_ByExtID " + pos1 := strings.Index(Otvet, TextFind) + if pos1 < 0 { + return Otvet + } + s2 := Otvet[pos1+1:] + + posEnd := strings.Index(s2, "\n}") + if posEnd < 0 { + return Otvet + } + + Otvet = Otvet[:pos1-1] + Otvet[pos1+posEnd+3:] + + return Otvet +} + +// DeleteFuncTestDelete - удаляет функцию Delete() +func DeleteFuncTestDelete(TextModel, ModelName string, Table1 *types.Table) string { + Otvet := TextModel + + _, ok := Table1.MapColumns["is_deleted"] + if ok == true { + return Otvet + } + + TextFind := "\nfunc TestDelete(" + pos1 := strings.Index(Otvet, TextFind) + if pos1 < 0 { + return Otvet + } + s2 := Otvet[pos1+1:] + + posEnd := strings.Index(s2, "\n}") + if posEnd < 0 { + return Otvet + } + + Otvet = Otvet[:pos1-1] + Otvet[pos1+posEnd+3:] + + return Otvet +} + +// DeleteFuncTestRestore - удаляет функцию Restore() +func DeleteFuncTestRestore(TextModel, Modelname string, Table1 *types.Table) string { + Otvet := TextModel + + _, ok := Table1.MapColumns["is_deleted"] + if ok == true { + return Otvet + } + + TextFind := "\nfunc TestRestore(" + pos1 := strings.Index(Otvet, TextFind) + if pos1 < 0 { + return Otvet + } + s2 := Otvet[pos1+1:] + + posEnd := strings.Index(s2, "\n}") + if posEnd < 0 { + return Otvet + } + + Otvet = Otvet[:pos1-1] + Otvet[pos1+posEnd+3:] + + return Otvet +} + +// DeleteFuncFind_byExtID - удаляет функцию Find_ByExtID() +func DeleteFuncTestFind_byExtID(TextModel, Modelname string, Table1 *types.Table) string { + Otvet := TextModel + + // + _, ok := Table1.MapColumns["ext_id"] + if ok == true { + return Otvet + } + + // + _, ok = Table1.MapColumns["connection_id"] + if ok == true { + return Otvet + } + + TextFind := "func TestFind_ByExtID(" + pos1 := strings.Index(Otvet, TextFind) + if pos1 < 0 { + return Otvet + } + s2 := Otvet[pos1+1:] + + posEnd := strings.Index(s2, "\n}") + if posEnd < 0 { + return Otvet + } + + Otvet = Otvet[:pos1-1] + Otvet[pos1+posEnd+3:] + + return Otvet +} diff --git a/internal/create_files/db/db_test.go b/internal/create_files/db/db_test.go new file mode 100644 index 0000000..3a49c63 --- /dev/null +++ b/internal/create_files/db/db_test.go @@ -0,0 +1 @@ +package db diff --git a/internal/create_files/model/model.go b/internal/create_files/model/model.go index af4bb76..ce8202a 100644 --- a/internal/create_files/model/model.go +++ b/internal/create_files/model/model.go @@ -9,6 +9,7 @@ import ( "github.com/ManyakRus/starter/log" "github.com/ManyakRus/starter/micro" "os" + "sort" "strconv" "strings" ) @@ -69,6 +70,7 @@ func CreateModelFiles1(Table1 *types.Table) error { TextModel = DeleteFuncDelete(TextModel, ModelName, Table1) TextModel = DeleteFuncRestore(TextModel, ModelName, Table1) } + TextModel = DeleteFuncFind_byExtID(TextModel, ModelName, Table1) TextModel = AddImportTime(TextModel, Table1) @@ -86,12 +88,23 @@ func FindTextModelStruct(Table1 *types.Table) (string, string, error) { TableName := Table1.Name ModelName = create_files.FindSingularName(TableName) ModelName = create_files.FormatName(ModelName) + Table1.NameGo = ModelName Otvet = `// ` + ModelName + ` - model from table ` + TableName + ` type ` + ModelName + ` struct { ` - for _, Column1 := range Table1.MapColumns { - TextColumn := FindTextColumn(Column1) + + //сортировка + 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] + TextColumn := FindTextColumn(&Column1) Otvet = Otvet + TextColumn + "\n" } @@ -99,12 +112,13 @@ type ` + ModelName + ` struct { return Otvet, ModelName, err } -func FindTextColumn(Column1 types.Column) string { +func FindTextColumn(Column1 *types.Column) string { Otvet := "" // Code string `json:"code" gorm:"column:code;default:0"` ColumnName := Column1.Name ColumnModelName := create_files.FormatName(Column1.Name) + Column1.NameGo = ColumnModelName SQLMapping1, ok := dbmeta.GetMappings()[Column1.Type] if ok == false { log.Panic("GetMappings() ", Column1.Type, " error: not found") @@ -287,3 +301,40 @@ func FindHasTimeColumn(Table1 *types.Table) bool { return Otvet } + +// DeleteFuncFind_byExtID - удаляет функцию Find_ByExtID() +func DeleteFuncFind_byExtID(TextModel, Modelname string, Table1 *types.Table) string { + Otvet := TextModel + + // + _, ok := Table1.MapColumns["ext_id"] + if ok == true { + return Otvet + } + + // + _, ok = Table1.MapColumns["connection_id"] + if ok == true { + return Otvet + } + + //FirstSymbol := strings.ToLower(Modelname)[:1] + TextFind := "Find_ByExtID(*" + Modelname + ") error" + Otvet = strings.ReplaceAll(Otvet, TextFind, "") + + TextFind = "\n// Find_ByExtID " + pos1 := strings.Index(Otvet, TextFind) + if pos1 < 0 { + return Otvet + } + s2 := Otvet[pos1+1:] + + posEnd := strings.Index(s2, "\n}") + if posEnd < 0 { + return Otvet + } + + Otvet = Otvet[:pos1-1] + Otvet[pos1+posEnd+3:] + + return Otvet +} diff --git a/internal/logic/logic.go b/internal/logic/logic.go index 8c5c5ae..361bf4f 100644 --- a/internal/logic/logic.go +++ b/internal/logic/logic.go @@ -1,6 +1,7 @@ package logic import ( + "github.com/ManyakRus/crud_generator/internal/create_files/db" "github.com/ManyakRus/crud_generator/internal/create_files/model" "github.com/ManyakRus/crud_generator/internal/postgres" "github.com/ManyakRus/starter/log" @@ -22,7 +23,17 @@ func StartFillAll() error { return err } + //модель err = model.CreateModelFiles(MapAll) + if err != nil { + return err + } + + //db crud + err = db.CreateDBFiles(MapAll) + if err != nil { + return err + } return err } diff --git a/internal/types/types.go b/internal/types/types.go index 5c5e847..01f9bd1 100644 --- a/internal/types/types.go +++ b/internal/types/types.go @@ -8,6 +8,7 @@ type Column struct { OrderNumber int TableKey string `json:"table_key" gorm:"column:table_key;default:''"` ColumnKey string `json:"column_key" gorm:"column:column_key;default:''"` + NameGo string `gorm:-` } type Table struct { @@ -16,4 +17,5 @@ type Table struct { MapColumns map[string]Column //Columns []Column OrderNumber int + NameGo string }