2024-04-08 17:42:13 +03:00
|
|
|
package crud_tables
|
2023-10-25 16:22:30 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/ManyakRus/crud_generator/internal/config"
|
|
|
|
"github.com/ManyakRus/crud_generator/internal/constants"
|
2023-10-26 11:33:12 +03:00
|
|
|
"github.com/ManyakRus/crud_generator/internal/create_files"
|
2023-11-20 17:08:58 +03:00
|
|
|
"github.com/ManyakRus/crud_generator/internal/folders"
|
2023-10-27 11:43:15 +03:00
|
|
|
"github.com/ManyakRus/crud_generator/internal/mini_func"
|
2023-10-25 16:22:30 +03:00
|
|
|
"github.com/ManyakRus/crud_generator/internal/types"
|
|
|
|
"github.com/ManyakRus/starter/log"
|
|
|
|
"github.com/ManyakRus/starter/micro"
|
|
|
|
"os"
|
2024-02-13 17:43:47 +03:00
|
|
|
"sort"
|
2023-10-25 16:22:30 +03:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2023-10-26 11:33:12 +03:00
|
|
|
// CreateAllFiles - создаёт все файлы в папке db
|
|
|
|
func CreateAllFiles(MapAll map[string]*types.Table) error {
|
2023-10-25 16:22:30 +03:00
|
|
|
var err error
|
|
|
|
|
2023-11-02 15:57:13 +03:00
|
|
|
for _, Table1 := range MapAll {
|
2024-05-31 16:09:51 +03:00
|
|
|
////проверка что таблица нормальная
|
|
|
|
//err1 := create_files.IsGoodTable(Table1)
|
|
|
|
//if err1 != nil {
|
|
|
|
// log.Warn(err1)
|
|
|
|
// continue
|
|
|
|
//}
|
2023-11-02 15:57:13 +03:00
|
|
|
|
2024-05-24 16:39:16 +03:00
|
|
|
//проверка что таблица нормальная
|
|
|
|
err2 := create_files.IsGoodTableNamePrefix(Table1)
|
|
|
|
if err2 != nil {
|
|
|
|
log.Warn(err2)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-03-06 15:37:36 +03:00
|
|
|
//файлы crud
|
2023-11-16 12:40:37 +03:00
|
|
|
if config.Settings.NEED_CREATE_DB == true {
|
2023-11-14 17:07:41 +03:00
|
|
|
err = CreateFiles(Table1)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("CreateFiles() table: ", Table1.Name, " error: ", err)
|
|
|
|
return err
|
|
|
|
}
|
2023-10-25 16:22:30 +03:00
|
|
|
}
|
2024-02-13 17:43:47 +03:00
|
|
|
|
2024-03-06 15:37:36 +03:00
|
|
|
//тестовые файлы crud
|
2023-11-13 16:42:54 +03:00
|
|
|
if config.Settings.NEED_CREATE_DB_TEST == true {
|
2024-04-17 10:19:36 +03:00
|
|
|
err = CreateFilesTest(Table1)
|
2023-11-13 16:42:54 +03:00
|
|
|
if err != nil {
|
2024-04-17 10:19:36 +03:00
|
|
|
log.Error("CreateFilesTest() table: ", Table1.Name, " error: ", err)
|
2023-11-13 16:42:54 +03:00
|
|
|
return err
|
|
|
|
}
|
2023-10-25 16:22:30 +03:00
|
|
|
}
|
2024-02-13 17:43:47 +03:00
|
|
|
|
2024-03-06 15:37:36 +03:00
|
|
|
//файлы UpdateEveryColumn
|
2024-02-13 17:43:47 +03:00
|
|
|
if config.Settings.NEED_CREATE_UPDATE_EVERY_COLUMN == true {
|
|
|
|
//файлы db update
|
|
|
|
err = CreateFilesUpdateEveryColumn(Table1)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("CreateFiles() table: ", Table1.Name, " error: ", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
//тестовые файлы db update
|
2024-03-12 17:19:36 +03:00
|
|
|
err = CreateFilesUpdateEveryColumnTest(Table1)
|
2024-02-13 17:43:47 +03:00
|
|
|
if err != nil {
|
2024-04-17 10:19:36 +03:00
|
|
|
log.Error("CreateFilesTest() table: ", Table1.Name, " error: ", err)
|
2024-02-13 17:43:47 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2024-03-06 15:37:36 +03:00
|
|
|
|
|
|
|
//файлы Cache
|
|
|
|
if config.Settings.NEED_CREATE_CACHE_API == true {
|
|
|
|
//файлы cache
|
|
|
|
if config.Settings.NEED_CREATE_CACHE_FILES == true {
|
|
|
|
err = CreateFilesCache(Table1)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("CreateFiles() table: ", Table1.Name, " error: ", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//тестовые файлы cache
|
|
|
|
if config.Settings.NEED_CREATE_CACHE_TEST_FILES == true {
|
|
|
|
err = CreateFilesCacheTest(Table1)
|
|
|
|
if err != nil {
|
2024-04-17 10:19:36 +03:00
|
|
|
log.Error("CreateFilesTest() table: ", Table1.Name, " error: ", err)
|
2024-03-06 15:37:36 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-25 16:22:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-26 11:33:12 +03:00
|
|
|
// CreateFiles - создаёт 1 файл в папке db
|
|
|
|
func CreateFiles(Table1 *types.Table) error {
|
2023-10-25 16:22:30 +03:00
|
|
|
var err error
|
|
|
|
|
|
|
|
//чтение файлов
|
|
|
|
DirBin := micro.ProgramDir_bin()
|
2023-11-01 17:34:57 +03:00
|
|
|
DirTemplates := DirBin + config.Settings.TEMPLATE_FOLDERNAME + micro.SeparatorFile()
|
|
|
|
DirReady := DirBin + config.Settings.READY_FOLDERNAME + micro.SeparatorFile()
|
2023-11-16 15:08:10 +03:00
|
|
|
DirTemplatesDB := DirTemplates + config.Settings.TEMPLATE_FOLDERNAME_CRUD + micro.SeparatorFile()
|
|
|
|
DirReadyDB := DirReady + config.Settings.TEMPLATE_FOLDERNAME_CRUD + micro.SeparatorFile()
|
2023-10-25 16:22:30 +03:00
|
|
|
|
2024-02-09 15:17:57 +03:00
|
|
|
FilenameTemplateDB := DirTemplatesDB + config.Settings.TEMPLATES_CRUD_FILENAME
|
2023-10-25 16:22:30 +03:00
|
|
|
TableName := strings.ToLower(Table1.Name)
|
2023-11-17 14:24:26 +03:00
|
|
|
DirReadyTable := DirReadyDB + config.Settings.PREFIX_CRUD + TableName
|
|
|
|
FilenameReadyDB := DirReadyTable + micro.SeparatorFile() + config.Settings.PREFIX_CRUD + TableName + ".go"
|
2023-10-25 16:22:30 +03:00
|
|
|
|
|
|
|
//создадим каталог
|
2023-11-17 14:24:26 +03:00
|
|
|
ok, err := micro.FileExists(DirReadyTable)
|
2023-10-25 16:22:30 +03:00
|
|
|
if ok == false {
|
2023-11-17 14:24:26 +03:00
|
|
|
err = os.MkdirAll(DirReadyTable, 0777)
|
2023-10-25 16:22:30 +03:00
|
|
|
if err != nil {
|
2023-11-17 14:24:26 +03:00
|
|
|
log.Panic("Mkdir() ", DirReadyTable, " error: ", err)
|
2023-10-25 16:22:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-06 17:45:55 +03:00
|
|
|
//загрузим шаблон файла
|
2023-10-25 16:22:30 +03:00
|
|
|
bytes, err := os.ReadFile(FilenameTemplateDB)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic("ReadFile() ", FilenameTemplateDB, " error: ", err)
|
|
|
|
}
|
|
|
|
TextDB := string(bytes)
|
|
|
|
|
2023-11-17 13:23:15 +03:00
|
|
|
//заменим имя пакета на новое
|
2023-12-01 16:00:11 +03:00
|
|
|
TextDB = create_files.ReplacePackageName(TextDB, DirReadyTable)
|
2023-11-17 14:24:26 +03:00
|
|
|
|
2024-03-06 17:45:55 +03:00
|
|
|
ModelName := Table1.NameGo
|
2023-11-17 14:24:26 +03:00
|
|
|
//заменим импорты
|
|
|
|
if config.Settings.USE_DEFAULT_TEMPLATE == true {
|
|
|
|
TextDB = create_files.DeleteTemplateRepositoryImports(TextDB)
|
|
|
|
|
2023-11-17 16:48:58 +03:00
|
|
|
ModelTableURL := create_files.FindModelTableURL(TableName)
|
|
|
|
TextDB = create_files.AddImport(TextDB, ModelTableURL)
|
2023-11-17 14:24:26 +03:00
|
|
|
|
2023-11-20 17:08:58 +03:00
|
|
|
ConstantsURL := create_files.FindDBConstantsURL()
|
2023-11-17 14:24:26 +03:00
|
|
|
TextDB = create_files.AddImport(TextDB, ConstantsURL)
|
2023-11-24 16:28:45 +03:00
|
|
|
|
|
|
|
CrudFunctionsURL := create_files.FindCrudFunctionsURL()
|
|
|
|
TextDB = create_files.AddImport(TextDB, CrudFunctionsURL)
|
2023-12-01 16:00:11 +03:00
|
|
|
|
|
|
|
//удалим лишние функции
|
|
|
|
TextDB = create_files.DeleteFuncDelete(TextDB, Table1)
|
|
|
|
TextDB = create_files.DeleteFuncRestore(TextDB, Table1)
|
|
|
|
TextDB = create_files.DeleteFuncFind_byExtID(TextDB, Table1)
|
|
|
|
|
|
|
|
//удалим лишние функции ctx
|
|
|
|
TextDB = create_files.DeleteFuncDeleteCtx(TextDB, Table1)
|
|
|
|
TextDB = create_files.DeleteFuncRestoreCtx(TextDB, Table1)
|
|
|
|
TextDB = create_files.DeleteFuncFind_byExtIDCtx(TextDB, Table1)
|
2024-03-06 17:45:55 +03:00
|
|
|
|
|
|
|
//кэш
|
|
|
|
if config.Settings.NEED_CREATE_CACHE_API == true {
|
|
|
|
//исправление Save()
|
|
|
|
TextDB = strings.ReplaceAll(TextDB, `//`+constants.TEXT_CACHE_REMOVE, constants.TEXT_CACHE_REMOVE)
|
|
|
|
}
|
2023-11-17 14:24:26 +03:00
|
|
|
}
|
2023-11-17 13:23:15 +03:00
|
|
|
|
2023-10-25 16:22:30 +03:00
|
|
|
//создание текста
|
|
|
|
TextDB = strings.ReplaceAll(TextDB, config.Settings.TEXT_TEMPLATE_MODEL, ModelName)
|
|
|
|
TextDB = strings.ReplaceAll(TextDB, config.Settings.TEXT_TEMPLATE_TABLENAME, Table1.Name)
|
2023-11-10 15:04:05 +03:00
|
|
|
TextDB = config.Settings.TEXT_MODULE_GENERATED + TextDB
|
2023-10-25 16:22:30 +03:00
|
|
|
|
2023-12-01 16:00:11 +03:00
|
|
|
//TextDB = create_files.DeleteFuncFind_byExtID(TextDB, Table1)
|
|
|
|
//TextDB = create_files.DeleteFuncFind_byExtIDCtx(TextDB, Table1)
|
2023-10-25 17:30:18 +03:00
|
|
|
TextDB = AddTextOmit(TextDB, Table1)
|
2023-11-02 18:19:33 +03:00
|
|
|
TextDB = ReplaceText_modified_at(TextDB, Table1)
|
2023-12-25 17:15:24 +03:00
|
|
|
TextDB = ReplaceText_created_at(TextDB, Table1)
|
2023-11-02 18:19:33 +03:00
|
|
|
TextDB = ReplaceText_is_deleted_deleted_at(TextDB, Table1)
|
|
|
|
TextDB = create_files.DeleteImportModel(TextDB)
|
2024-05-28 13:39:36 +03:00
|
|
|
|
|
|
|
TextDB = create_files.ReplaceCacheRemove_ManyPK(TextDB, Table1)
|
|
|
|
|
2024-04-26 16:45:09 +03:00
|
|
|
TextDB = create_files.ReplacePrimaryKeyM_ID(TextDB, Table1)
|
2023-10-25 16:22:30 +03:00
|
|
|
|
2023-11-15 17:59:21 +03:00
|
|
|
//замена импортов на новые URL
|
|
|
|
TextDB = create_files.ReplaceServiceURLImports(TextDB)
|
|
|
|
|
2024-04-12 16:54:27 +03:00
|
|
|
//uuid
|
|
|
|
TextDB = create_files.CheckAndAddImportUUID_FromText(TextDB)
|
|
|
|
|
2024-05-15 17:12:03 +03:00
|
|
|
//alias
|
|
|
|
TextDB = create_files.CheckAndAddImportAlias(TextDB)
|
|
|
|
|
2023-11-27 11:47:00 +03:00
|
|
|
//удаление пустого импорта
|
|
|
|
TextDB = create_files.DeleteEmptyImport(TextDB)
|
|
|
|
|
2024-01-09 13:48:10 +03:00
|
|
|
//переименование функций
|
|
|
|
TextDB = RenameFunctions(TextDB, Table1)
|
|
|
|
|
2024-02-19 14:55:52 +03:00
|
|
|
//удаление пустых строк
|
|
|
|
TextDB = create_files.DeleteEmptyLines(TextDB)
|
|
|
|
|
2023-10-25 16:22:30 +03:00
|
|
|
//запись файла
|
|
|
|
err = os.WriteFile(FilenameReadyDB, []byte(TextDB), constants.FILE_PERMISSIONS)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-04-17 10:19:36 +03:00
|
|
|
// CreateFilesTest - создаёт 1 файл в папке db
|
|
|
|
func CreateFilesTest(Table1 *types.Table) error {
|
2023-10-25 16:22:30 +03:00
|
|
|
var err error
|
|
|
|
|
2023-11-01 17:34:57 +03:00
|
|
|
TableName := strings.ToLower(Table1.Name)
|
|
|
|
|
2023-10-25 16:22:30 +03:00
|
|
|
//чтение файлов
|
|
|
|
DirBin := micro.ProgramDir_bin()
|
2023-11-01 17:34:57 +03:00
|
|
|
DirTemplates := DirBin + config.Settings.TEMPLATE_FOLDERNAME + micro.SeparatorFile()
|
|
|
|
DirReady := DirBin + config.Settings.READY_FOLDERNAME + micro.SeparatorFile()
|
2023-11-16 15:08:10 +03:00
|
|
|
DirTemplatesDB := DirTemplates + config.Settings.TEMPLATE_FOLDERNAME_CRUD + micro.SeparatorFile()
|
|
|
|
DirReadyDB := DirReady + config.Settings.TEMPLATE_FOLDERNAME_CRUD + micro.SeparatorFile()
|
2023-10-25 16:22:30 +03:00
|
|
|
|
2024-02-09 15:17:57 +03:00
|
|
|
FilenameTemplateDB := DirTemplatesDB + config.Settings.TEMPLATES_CRUD_TEST_FILENAME
|
2023-11-17 14:24:26 +03:00
|
|
|
DirReadyTable := DirReadyDB + config.Settings.PREFIX_CRUD + TableName
|
|
|
|
FilenameReadyDB := DirReadyTable + micro.SeparatorFile() + config.Settings.PREFIX_CRUD + TableName + "_test.go"
|
2023-10-25 16:22:30 +03:00
|
|
|
|
2023-11-20 18:02:26 +03:00
|
|
|
//создадим папку готовых файлов
|
|
|
|
folders.CreateFolder(DirReadyTable)
|
|
|
|
|
2023-10-25 16:22:30 +03:00
|
|
|
bytes, err := os.ReadFile(FilenameTemplateDB)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic("ReadFile() ", FilenameTemplateDB, " error: ", err)
|
|
|
|
}
|
|
|
|
TextDB := string(bytes)
|
|
|
|
|
2023-11-17 13:23:15 +03:00
|
|
|
//заменим имя пакета на новое
|
2023-12-01 16:00:11 +03:00
|
|
|
TextDB = create_files.ReplacePackageName(TextDB, DirReadyTable)
|
2023-11-17 14:24:26 +03:00
|
|
|
|
|
|
|
//заменим импорты
|
|
|
|
if config.Settings.USE_DEFAULT_TEMPLATE == true {
|
|
|
|
TextDB = create_files.DeleteTemplateRepositoryImports(TextDB)
|
|
|
|
|
2023-11-17 16:48:58 +03:00
|
|
|
ModelTableURL := create_files.FindModelTableURL(TableName)
|
|
|
|
TextDB = create_files.AddImport(TextDB, ModelTableURL)
|
2023-11-17 14:24:26 +03:00
|
|
|
|
2024-05-03 15:57:49 +03:00
|
|
|
ConstantsURL := create_files.FindConstantsURL()
|
|
|
|
TextDB = create_files.AddImport(TextDB, ConstantsURL)
|
|
|
|
|
2023-12-01 16:00:11 +03:00
|
|
|
//удалим лишние функции
|
|
|
|
TextDB = create_files.DeleteFuncTestDelete(TextDB, Table1)
|
|
|
|
TextDB = create_files.DeleteFuncTestRestore(TextDB, Table1)
|
|
|
|
TextDB = create_files.DeleteFuncTestFind_byExtID(TextDB, Table1)
|
2024-04-24 15:26:19 +03:00
|
|
|
|
|
|
|
//Postgres_ID_Test = ID Minimum
|
|
|
|
TextDB = create_files.Replace_Postgres_ID_Test(TextDB, Table1)
|
|
|
|
|
|
|
|
//замена ID на PrimaryKey
|
|
|
|
TextDB = create_files.ReplacePrimaryKeyOtvetID(TextDB, Table1)
|
|
|
|
|
|
|
|
//добавим импорт uuid
|
|
|
|
TextDB = create_files.CheckAndAddImportUUID_FromText(TextDB)
|
|
|
|
|
2024-05-03 16:40:56 +03:00
|
|
|
//замена "postgres_gorm.Connect_WithApplicationName("
|
|
|
|
TextDB = create_files.ReplaceConnect_WithApplicationName(TextDB)
|
|
|
|
|
2023-11-17 14:24:26 +03:00
|
|
|
}
|
2023-11-17 13:23:15 +03:00
|
|
|
|
2023-10-25 16:22:30 +03:00
|
|
|
//создание текста
|
|
|
|
ModelName := Table1.NameGo
|
|
|
|
TextDB = strings.ReplaceAll(TextDB, config.Settings.TEXT_TEMPLATE_MODEL, ModelName)
|
|
|
|
TextDB = strings.ReplaceAll(TextDB, config.Settings.TEXT_TEMPLATE_TABLENAME, Table1.Name)
|
2023-11-10 15:04:05 +03:00
|
|
|
TextDB = config.Settings.TEXT_MODULE_GENERATED + TextDB
|
2023-10-25 16:22:30 +03:00
|
|
|
|
|
|
|
if config.Settings.HAS_IS_DELETED == true {
|
2023-12-01 16:00:11 +03:00
|
|
|
TextDB = create_files.DeleteFuncTestDelete(TextDB, Table1)
|
|
|
|
TextDB = create_files.DeleteFuncTestRestore(TextDB, Table1)
|
2023-10-25 16:22:30 +03:00
|
|
|
}
|
2023-12-01 16:00:11 +03:00
|
|
|
TextDB = create_files.DeleteFuncTestFind_byExtID(TextDB, Table1)
|
2023-10-25 16:22:30 +03:00
|
|
|
|
2024-02-15 15:15:16 +03:00
|
|
|
//SkipNow() если нет строк в БД
|
2023-11-02 15:57:13 +03:00
|
|
|
TextDB = create_files.AddSkipNow(TextDB, Table1)
|
|
|
|
|
2023-11-15 17:59:21 +03:00
|
|
|
//замена импортов на новые URL
|
|
|
|
TextDB = create_files.ReplaceServiceURLImports(TextDB)
|
|
|
|
|
2023-11-27 11:47:00 +03:00
|
|
|
//удаление пустого импорта
|
|
|
|
TextDB = create_files.DeleteEmptyImport(TextDB)
|
|
|
|
|
2024-02-19 14:55:52 +03:00
|
|
|
//удаление пустых строк
|
|
|
|
TextDB = create_files.DeleteEmptyLines(TextDB)
|
|
|
|
|
2023-10-25 16:22:30 +03:00
|
|
|
//запись файла
|
|
|
|
err = os.WriteFile(FilenameReadyDB, []byte(TextDB), constants.FILE_PERMISSIONS)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-12-01 16:00:11 +03:00
|
|
|
//// DeleteFuncDelete - удаляет функцию Delete()
|
|
|
|
//func DeleteFuncDelete(Text string, Table1 *types.Table) string {
|
|
|
|
// Otvet := Text
|
|
|
|
//
|
|
|
|
// _, ok := Table1.MapColumns["is_deleted"]
|
|
|
|
// if ok == true {
|
|
|
|
// return Otvet
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// Otvet = create_files.DeleteFuncFromComment(Text, "\n// Delete ")
|
|
|
|
//
|
|
|
|
// return Otvet
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//// DeleteFuncRestore - удаляет функцию Restore()
|
|
|
|
//func DeleteFuncRestore(Text string, Table1 *types.Table) string {
|
|
|
|
// Otvet := Text
|
|
|
|
//
|
|
|
|
// _, ok := Table1.MapColumns["is_deleted"]
|
|
|
|
// if ok == true {
|
|
|
|
// return Otvet
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// Otvet = create_files.DeleteFuncFromComment(Text, "\n// Restore ")
|
|
|
|
//
|
|
|
|
// return Otvet
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//// DeleteFuncDeleteCtx - удаляет функцию Delete_ctx()
|
|
|
|
//func DeleteFuncDeleteCtx(Text string, Table1 *types.Table) string {
|
|
|
|
// Otvet := Text
|
|
|
|
//
|
|
|
|
// _, ok := Table1.MapColumns["is_deleted"]
|
|
|
|
// if ok == true {
|
|
|
|
// return Otvet
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// Otvet = create_files.DeleteFuncFromComment(Text, "\n// Delete_ctx ")
|
|
|
|
//
|
|
|
|
// return Otvet
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//// DeleteFuncRestoreCtx - удаляет функцию Restore_ctx()
|
|
|
|
//func DeleteFuncRestoreCtx(Text string, Table1 *types.Table) string {
|
|
|
|
// Otvet := Text
|
|
|
|
//
|
|
|
|
// _, ok := Table1.MapColumns["is_deleted"]
|
|
|
|
// if ok == true {
|
|
|
|
// return Otvet
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// Otvet = create_files.DeleteFuncFromComment(Text, "\n// Restore_ctx ")
|
|
|
|
//
|
|
|
|
// return Otvet
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//// DeleteFuncFind_byExtID - удаляет функцию Find_ByExtID()
|
|
|
|
//func DeleteFuncFind_byExtID(Text string, Table1 *types.Table) string {
|
|
|
|
// Otvet := Text
|
|
|
|
//
|
|
|
|
// //если есть обе колонки - ничего не делаем
|
|
|
|
// ok := create_files.Has_Column_ExtID_ConnectionID(Table1)
|
|
|
|
// if ok == true {
|
|
|
|
// return Otvet
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// //
|
|
|
|
// Otvet = create_files.DeleteFuncFromComment(Text, "\n// Find_ByExtID ")
|
|
|
|
//
|
|
|
|
// return Otvet
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//// DeleteFuncFind_byExtIDCtx - удаляет функцию Find_ByExtID_ctx()
|
|
|
|
//func DeleteFuncFind_byExtIDCtx(Text string, Table1 *types.Table) string {
|
|
|
|
// Otvet := Text
|
|
|
|
//
|
|
|
|
// //если есть обе колонки - ничего не делаем
|
|
|
|
// ok := create_files.Has_Column_ExtID_ConnectionID(Table1)
|
|
|
|
// if ok == true {
|
|
|
|
// return Otvet
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// //
|
|
|
|
// Otvet = create_files.DeleteFuncFromComment(Text, "\n// Find_ByExtID_ctx ")
|
|
|
|
//
|
|
|
|
// return Otvet
|
|
|
|
//}
|
2023-10-25 17:30:18 +03:00
|
|
|
|
2023-10-26 10:33:18 +03:00
|
|
|
// AddTextOmit - добавляет код для записи null в колонки Nullable
|
2023-10-25 17:30:18 +03:00
|
|
|
func AddTextOmit(TextDB string, Table1 *types.Table) string {
|
|
|
|
Otvet := TextDB
|
|
|
|
|
|
|
|
TextFind := "\t//игнор пустых колонок"
|
|
|
|
pos1 := strings.Index(Otvet, TextFind)
|
|
|
|
if pos1 < 0 {
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
|
|
|
TextOmit := ""
|
2023-11-07 17:58:02 +03:00
|
|
|
NullableCount := 0
|
2023-10-25 17:30:18 +03:00
|
|
|
for _, Column1 := range Table1.MapColumns {
|
2023-11-27 11:26:33 +03:00
|
|
|
ColumnName := Column1.Name
|
2023-11-02 18:19:33 +03:00
|
|
|
ColumnNameGo := Column1.NameGo
|
2023-10-25 17:30:18 +03:00
|
|
|
TypeGo := Column1.TypeGo
|
2023-11-02 18:19:33 +03:00
|
|
|
|
2023-10-26 10:33:18 +03:00
|
|
|
if Column1.IsNullable == false {
|
2023-10-25 17:30:18 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-11-16 12:40:37 +03:00
|
|
|
//ищем в файле настроек nullable.json
|
2023-11-27 11:26:33 +03:00
|
|
|
is_nullable_config, has_is_nullable_config := types.MapNullableFileds[ColumnName]
|
2023-11-16 12:40:37 +03:00
|
|
|
if has_is_nullable_config == true && is_nullable_config == false {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2023-10-26 10:33:18 +03:00
|
|
|
if TypeGo == "time.Time" {
|
2023-11-07 17:58:02 +03:00
|
|
|
NullableCount = NullableCount + 1
|
2023-10-26 10:33:18 +03:00
|
|
|
TextFind := `if m.` + ColumnNameGo + `.IsZero() == true {`
|
|
|
|
pos1 := strings.Index(TextDB, TextFind)
|
|
|
|
if pos1 >= 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
TextOmit = TextOmit + "\t" + `ColumnName = "` + ColumnNameGo + `"
|
2023-10-25 17:30:18 +03:00
|
|
|
if m.` + ColumnNameGo + `.IsZero() == true {
|
|
|
|
MassOmit = append(MassOmit, ColumnName)
|
|
|
|
}
|
|
|
|
|
|
|
|
`
|
2023-11-02 18:19:33 +03:00
|
|
|
} else if mini_func.IsNumberType(TypeGo) == true && (Column1.TableKey != "" || is_nullable_config == true) {
|
2023-11-07 17:58:02 +03:00
|
|
|
NullableCount = NullableCount + 1
|
2023-10-26 10:33:18 +03:00
|
|
|
TextFind := `if m.` + ColumnNameGo + ` == 0 {`
|
|
|
|
pos1 := strings.Index(TextDB, TextFind)
|
|
|
|
if pos1 >= 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
TextOmit = TextOmit + "\t" + `ColumnName = "` + ColumnNameGo + `"
|
|
|
|
if m.` + ColumnNameGo + ` == 0 {
|
|
|
|
MassOmit = append(MassOmit, ColumnName)
|
|
|
|
}
|
|
|
|
|
|
|
|
`
|
|
|
|
}
|
|
|
|
|
2023-10-25 17:30:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Otvet = Otvet[:pos1] + TextOmit + Otvet[pos1:]
|
|
|
|
|
2023-11-07 17:58:02 +03:00
|
|
|
if NullableCount == 0 && config.Settings.USE_DEFAULT_TEMPLATE == true {
|
|
|
|
Otvet = strings.ReplaceAll(Otvet, "\n\tvar ColumnName string", "")
|
|
|
|
}
|
|
|
|
|
2023-10-25 17:30:18 +03:00
|
|
|
return Otvet
|
|
|
|
}
|
2023-11-02 18:19:33 +03:00
|
|
|
|
|
|
|
// ReplaceText_modified_at - заменяет текст "Text_modified_at" на текст из файла
|
|
|
|
func ReplaceText_modified_at(s string, Table1 *types.Table) string {
|
|
|
|
Otvet := s
|
|
|
|
|
|
|
|
TextNew := config.Settings.TEXT_DB_MODIFIED_AT
|
|
|
|
_, ok := Table1.MapColumns["modified_at"]
|
|
|
|
if ok == false {
|
|
|
|
TextNew = ""
|
|
|
|
}
|
|
|
|
|
2023-12-25 17:15:24 +03:00
|
|
|
TextFind := "\t//Text_modified_at\n"
|
2023-11-02 18:19:33 +03:00
|
|
|
Otvet = strings.ReplaceAll(Otvet, TextFind, TextNew)
|
|
|
|
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReplaceText_is_deleted_deleted_at - заменяет текст "Text_is_deleted_deleted_at" на текст из файла
|
|
|
|
func ReplaceText_is_deleted_deleted_at(s string, Table1 *types.Table) string {
|
|
|
|
Otvet := s
|
|
|
|
|
|
|
|
TextNew := config.Settings.TEXT_DB_IS_DELETED
|
|
|
|
_, ok := Table1.MapColumns["is_deleted"]
|
|
|
|
if ok == false {
|
|
|
|
TextNew = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ok = Table1.MapColumns["deleted_at"]
|
|
|
|
if ok == false {
|
|
|
|
TextNew = ""
|
|
|
|
}
|
|
|
|
|
2023-12-25 17:15:24 +03:00
|
|
|
TextFind := "\t//Text_is_deleted_deleted_at\n"
|
|
|
|
Otvet = strings.ReplaceAll(Otvet, TextFind, TextNew)
|
|
|
|
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReplaceText_created_at - заменяет текст "Text_created_at" на текст из файла
|
|
|
|
func ReplaceText_created_at(s string, Table1 *types.Table) string {
|
|
|
|
Otvet := s
|
|
|
|
|
|
|
|
TextNew := config.Settings.TEXT_DB_CREATED_AT
|
|
|
|
_, ok := Table1.MapColumns["created_at"]
|
|
|
|
if ok == false {
|
|
|
|
TextNew = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
TextFind := "\t//Text_created_at\n"
|
2023-11-02 18:19:33 +03:00
|
|
|
Otvet = strings.ReplaceAll(Otvet, TextFind, TextNew)
|
|
|
|
|
|
|
|
return Otvet
|
|
|
|
}
|
2024-01-09 13:48:10 +03:00
|
|
|
|
2024-04-02 11:54:01 +03:00
|
|
|
// RenameFunctions - заменяет названия функций, на названия из файла
|
2024-01-09 13:48:10 +03:00
|
|
|
func RenameFunctions(TextDB string, Table1 *types.Table) string {
|
|
|
|
Otvet := TextDB
|
|
|
|
|
|
|
|
TableName := strings.ToLower(Table1.Name)
|
|
|
|
Rename1, ok := types.MapRenameFunctions[TableName]
|
|
|
|
if ok == false {
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range Rename1 {
|
2024-04-03 10:45:20 +03:00
|
|
|
Otvet = strings.ReplaceAll(Otvet, "func "+v.Old+"(", "func "+v.New+"(")
|
2024-01-09 13:48:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return Otvet
|
|
|
|
}
|
2024-02-13 17:43:47 +03:00
|
|
|
|
2024-03-06 15:37:36 +03:00
|
|
|
// CreateFilesUpdateEveryColumn - создаёт 1 файл в папке crud
|
2024-02-13 17:43:47 +03:00
|
|
|
func CreateFilesUpdateEveryColumn(Table1 *types.Table) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
TableName := strings.ToLower(Table1.Name)
|
|
|
|
|
|
|
|
//чтение файлов
|
|
|
|
DirBin := micro.ProgramDir_bin()
|
|
|
|
DirTemplates := DirBin + config.Settings.TEMPLATE_FOLDERNAME + micro.SeparatorFile()
|
|
|
|
DirReady := DirBin + config.Settings.READY_FOLDERNAME + micro.SeparatorFile()
|
|
|
|
DirTemplatesCrud := DirTemplates + config.Settings.TEMPLATE_FOLDERNAME_CRUD + micro.SeparatorFile()
|
|
|
|
DirReadyCrud := DirReady + config.Settings.TEMPLATE_FOLDERNAME_CRUD + micro.SeparatorFile() + config.Settings.PREFIX_CRUD + TableName + micro.SeparatorFile()
|
|
|
|
|
2024-02-20 16:33:21 +03:00
|
|
|
FilenameTemplateCrudFunc := DirTemplatesCrud + config.Settings.TEMPLATES_CRUD_TABLE_UPDATE_FUNC_FILENAME
|
2024-02-13 17:43:47 +03:00
|
|
|
DirReadyTable := DirReadyCrud
|
2024-02-20 16:33:21 +03:00
|
|
|
FilenameReadyCrudUpdateFunc := DirReadyTable + config.Settings.PREFIX_CRUD + TableName + "_update.go"
|
2024-02-13 17:43:47 +03:00
|
|
|
|
|
|
|
//создадим папку готовых файлов
|
|
|
|
folders.CreateFolder(DirReadyTable)
|
|
|
|
|
2024-02-20 16:33:21 +03:00
|
|
|
//читаем шаблон файла, только функции
|
|
|
|
bytes, err := os.ReadFile(FilenameTemplateCrudFunc)
|
2024-02-13 17:43:47 +03:00
|
|
|
if err != nil {
|
2024-02-20 16:33:21 +03:00
|
|
|
log.Panic("ReadFile() ", FilenameTemplateCrudFunc, " error: ", err)
|
2024-02-13 17:43:47 +03:00
|
|
|
}
|
|
|
|
TextCrudUpdateFunc := string(bytes)
|
|
|
|
|
2024-02-20 16:33:21 +03:00
|
|
|
//читаем шаблон файла, без функций
|
|
|
|
FilenameTemplateCrud := DirTemplatesCrud + config.Settings.TEMPLATES_CRUD_TABLE_UPDATE_FILENAME
|
|
|
|
bytes, err = os.ReadFile(FilenameTemplateCrud)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic("ReadFile() ", FilenameTemplateCrud, " error: ", err)
|
|
|
|
}
|
|
|
|
TextCrud := string(bytes)
|
|
|
|
TextCrud = TextCrud + "\n"
|
2024-02-13 17:43:47 +03:00
|
|
|
|
2024-02-20 16:33:21 +03:00
|
|
|
//заменим имя пакета на новое
|
|
|
|
TextCrud = create_files.ReplacePackageName(TextCrud, DirReadyTable)
|
2024-04-02 11:54:01 +03:00
|
|
|
TextCrud = strings.ReplaceAll(TextCrud, config.Settings.TEXT_TEMPLATE_MODEL, Table1.NameGo)
|
|
|
|
TextCrud = strings.ReplaceAll(TextCrud, config.Settings.TEXT_TEMPLATE_TABLENAME, Table1.Name)
|
2024-02-20 16:33:21 +03:00
|
|
|
|
2024-04-08 17:42:13 +03:00
|
|
|
//создание текста
|
|
|
|
TextUpdateEveryColumn := FindTextUpdateEveryColumn(TextCrudUpdateFunc, Table1)
|
|
|
|
|
|
|
|
// пустой файл не нужен
|
|
|
|
if TextUpdateEveryColumn == "" {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
TextCrud = TextCrud + TextUpdateEveryColumn
|
|
|
|
TextCrud = config.Settings.TEXT_MODULE_GENERATED + TextCrud
|
|
|
|
|
2024-02-13 17:43:47 +03:00
|
|
|
//заменим импорты
|
2024-02-14 16:16:25 +03:00
|
|
|
if config.Settings.USE_DEFAULT_TEMPLATE == true {
|
2024-02-20 17:14:48 +03:00
|
|
|
TextCrud = create_files.DeleteTemplateRepositoryImports(TextCrud)
|
|
|
|
|
2024-02-14 16:16:25 +03:00
|
|
|
DBConstantsURL := create_files.FindDBConstantsURL()
|
|
|
|
TextCrud = create_files.AddImport(TextCrud, DBConstantsURL)
|
2024-02-13 17:43:47 +03:00
|
|
|
|
2024-02-14 16:16:25 +03:00
|
|
|
ModelTableURL := create_files.FindModelTableURL(TableName)
|
|
|
|
TextCrud = create_files.AddImport(TextCrud, ModelTableURL)
|
2024-02-13 17:43:47 +03:00
|
|
|
|
2024-04-27 17:23:34 +03:00
|
|
|
TextCrud = create_files.CheckAndAddImportGorm_FromText(TextCrud)
|
2024-03-12 15:35:08 +03:00
|
|
|
//TextCrud = create_files.ConvertRequestIdToAlias(TextCrud, Table1)
|
2024-04-27 17:23:34 +03:00
|
|
|
//добавим импорт uuid
|
2024-02-14 16:16:25 +03:00
|
|
|
}
|
2024-02-13 17:43:47 +03:00
|
|
|
|
2024-03-06 15:37:36 +03:00
|
|
|
//кэш
|
|
|
|
if config.Settings.NEED_CREATE_CACHE_API == true {
|
|
|
|
TextCrud = strings.ReplaceAll(TextCrud, `//`+constants.TEXT_CACHE_REMOVE, constants.TEXT_CACHE_REMOVE)
|
|
|
|
}
|
|
|
|
|
2024-04-02 11:54:01 +03:00
|
|
|
//переименование функций
|
2024-04-03 09:52:01 +03:00
|
|
|
//TextCrud = RenameFunctions(TextCrud, Table1)
|
2024-04-02 11:54:01 +03:00
|
|
|
|
2024-04-12 16:54:27 +03:00
|
|
|
//заменяет "m.ID" на название колонки PrimaryKey
|
|
|
|
TextCrud = create_files.ReplacePrimaryKeyM_ID(TextCrud, Table1)
|
|
|
|
|
2024-04-27 17:23:34 +03:00
|
|
|
//добавим импорт uuid
|
2024-04-12 16:54:27 +03:00
|
|
|
TextCrud = create_files.CheckAndAddImportUUID_FromText(TextCrud)
|
|
|
|
|
2024-02-13 17:43:47 +03:00
|
|
|
//удаление пустого импорта
|
|
|
|
TextCrud = create_files.DeleteEmptyImport(TextCrud)
|
2024-02-19 14:55:52 +03:00
|
|
|
|
|
|
|
//удаление пустых строк
|
2024-02-13 17:43:47 +03:00
|
|
|
TextCrud = create_files.DeleteEmptyLines(TextCrud)
|
|
|
|
|
|
|
|
//запись файла
|
2024-02-20 16:33:21 +03:00
|
|
|
err = os.WriteFile(FilenameReadyCrudUpdateFunc, []byte(TextCrud), constants.FILE_PERMISSIONS)
|
2024-02-13 17:43:47 +03:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindTextUpdateEveryColumn - возвращает текст для всех таблиц
|
|
|
|
func FindTextUpdateEveryColumn(TextCrudUpdateFunc string, Table1 *types.Table) string {
|
|
|
|
Otvet := ""
|
|
|
|
|
|
|
|
//сортировка по названию таблиц
|
|
|
|
keys := make([]string, 0, len(Table1.MapColumns))
|
|
|
|
for k := range Table1.MapColumns {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
//найдём новый текст для каждой таблицы
|
|
|
|
for _, key1 := range keys {
|
|
|
|
Column1, ok := Table1.MapColumns[key1]
|
|
|
|
if ok == false {
|
|
|
|
log.Panic("FindTextUpdateEveryColumn() Table1.MapColumns[key1] = false")
|
|
|
|
}
|
2024-02-14 16:04:22 +03:00
|
|
|
if create_files.Is_NotNeedUpdate_Сolumn(Column1) == true {
|
2024-02-13 17:43:47 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
TextColumn1 := FindTextUpdateEveryColumn1(TextCrudUpdateFunc, Table1, Column1)
|
|
|
|
Otvet = Otvet + TextColumn1 + "\n\n"
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindTextUpdateEveryColumn1 - возвращает текст для одной таблицы
|
|
|
|
func FindTextUpdateEveryColumn1(TextCrudUpdateFunc string, Table1 *types.Table, Column1 *types.Column) string {
|
|
|
|
Otvet := TextCrudUpdateFunc
|
|
|
|
|
|
|
|
ModelName := Table1.NameGo
|
|
|
|
ColumnName := Column1.NameGo
|
|
|
|
FuncName := "Update_" + ColumnName
|
2024-05-13 18:07:32 +03:00
|
|
|
TextRequest, TextRequestFieldName := create_files.FindTextProtobufRequest(Table1)
|
2024-02-13 17:43:47 +03:00
|
|
|
|
2024-05-28 13:39:36 +03:00
|
|
|
Otvet = create_files.ReplaceCacheRemove_ManyPK(Otvet, Table1)
|
|
|
|
|
2024-05-02 15:39:32 +03:00
|
|
|
//запись null в nullable колонки
|
|
|
|
if Column1.IsNullable == true && (Column1.TableKey != "" || Column1.TypeGo == "time.Time") {
|
|
|
|
} else {
|
|
|
|
TextFind := ` if Value == 0 {
|
|
|
|
tx = db.Model(&m).Update("ColumnName", gorm.Expr("NULL"))
|
|
|
|
} else {
|
|
|
|
tx = db.Model(&m).Update("ColumnName", Value)
|
|
|
|
}`
|
|
|
|
TextReplace := ` tx = db.Model(&m).Update("ColumnName", Value)`
|
|
|
|
Otvet = strings.ReplaceAll(Otvet, TextFind, TextReplace)
|
|
|
|
}
|
|
|
|
|
2024-02-13 17:43:47 +03:00
|
|
|
//заменяем Read_ctx()
|
|
|
|
Otvet = strings.ReplaceAll(Otvet, " Read_ctx ", " "+FuncName+"_ctx ")
|
|
|
|
Otvet = strings.ReplaceAll(Otvet, " Read_ctx(", " "+FuncName+"_ctx(")
|
|
|
|
Otvet = strings.ReplaceAll(Otvet, ".Read_ctx(", "."+FuncName+"_ctx(")
|
|
|
|
|
|
|
|
//заменяем Read()
|
|
|
|
Otvet = strings.ReplaceAll(Otvet, config.Settings.TEXT_TEMPLATE_MODEL+"_Read", ModelName+"_"+FuncName)
|
|
|
|
Otvet = strings.ReplaceAll(Otvet, " Read ", " "+FuncName+" ")
|
|
|
|
Otvet = strings.ReplaceAll(Otvet, " Read(", " "+FuncName+"(")
|
|
|
|
Otvet = strings.ReplaceAll(Otvet, `"Read()`, `"`+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, "ColumnName", ColumnName)
|
|
|
|
Otvet = strings.ReplaceAll(Otvet, "Model.ID", "Model."+ColumnName)
|
|
|
|
Otvet = strings.ReplaceAll(Otvet, "Request.ID", "Request."+TextRequestFieldName)
|
2024-02-14 17:30:09 +03:00
|
|
|
//Otvet = strings.ReplaceAll(Otvet, "ColumnName", ColumnName)
|
2024-02-13 17:43:47 +03:00
|
|
|
//Otvet = strings.ReplaceAll(Otvet, "m.ID", "m."+ColumnName)
|
|
|
|
|
2024-02-14 18:03:08 +03:00
|
|
|
//внешние ключи заменяем 0 на null
|
2024-02-15 15:15:16 +03:00
|
|
|
TextEqualEmpty := create_files.FindTextEqualEmpty(Column1, "Value")
|
|
|
|
Otvet = strings.ReplaceAll(Otvet, "Value == 0", TextEqualEmpty)
|
2024-05-02 15:39:32 +03:00
|
|
|
//if Column1.IsNullable == true && (Column1.TableKey != "" || Column1.TypeGo == "time.Time") {
|
|
|
|
// Otvet = strings.ReplaceAll(Otvet, "0==1 && ", "")
|
|
|
|
//}
|
2024-02-14 18:03:08 +03:00
|
|
|
|
2024-02-13 17:43:47 +03:00
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
2024-03-12 17:19:36 +03:00
|
|
|
// CreateFilesUpdateEveryColumnTest - создаёт 1 файл в папке grpc_client
|
|
|
|
func CreateFilesUpdateEveryColumnTest(Table1 *types.Table) error {
|
2024-02-13 17:43:47 +03:00
|
|
|
var err error
|
|
|
|
|
|
|
|
TableName := strings.ToLower(Table1.Name)
|
|
|
|
|
|
|
|
//чтение файлов
|
|
|
|
DirBin := micro.ProgramDir_bin()
|
|
|
|
DirTemplates := DirBin + config.Settings.TEMPLATE_FOLDERNAME + micro.SeparatorFile()
|
|
|
|
DirReady := DirBin + config.Settings.READY_FOLDERNAME + micro.SeparatorFile()
|
|
|
|
DirTemplatesCrud := DirTemplates + config.Settings.TEMPLATE_FOLDERNAME_CRUD + micro.SeparatorFile()
|
|
|
|
DirReadyCrud := DirReady + config.Settings.TEMPLATE_FOLDERNAME_CRUD + micro.SeparatorFile() + config.Settings.PREFIX_CRUD + TableName + micro.SeparatorFile()
|
|
|
|
|
2024-02-20 16:33:21 +03:00
|
|
|
FilenameTemplateCrudFunc := DirTemplatesCrud + config.Settings.TEMPLATES_CRUD_TABLE_UPDATE_FUNC_TEST_FILENAME
|
2024-02-13 17:43:47 +03:00
|
|
|
DirReadyTable := DirReadyCrud
|
|
|
|
FilenameReadyCrudUpdate := DirReadyTable + config.Settings.PREFIX_CRUD + TableName + "_update_test.go"
|
|
|
|
|
|
|
|
//создадим папку готовых файлов
|
|
|
|
folders.CreateFolder(DirReadyTable)
|
|
|
|
|
2024-02-20 16:33:21 +03:00
|
|
|
//читаем шаблон файла, только функции
|
|
|
|
bytes, err := os.ReadFile(FilenameTemplateCrudFunc)
|
2024-02-13 17:43:47 +03:00
|
|
|
if err != nil {
|
2024-02-20 16:33:21 +03:00
|
|
|
log.Panic("ReadFile() ", FilenameTemplateCrudFunc, " error: ", err)
|
2024-02-13 17:43:47 +03:00
|
|
|
}
|
|
|
|
TextCrudUpdateFunc := string(bytes)
|
|
|
|
|
2024-02-20 16:33:21 +03:00
|
|
|
//читаем шаблон файла, без функций
|
|
|
|
FilenameTemplateCrud := DirTemplatesCrud + config.Settings.TEMPLATES_CRUD_TABLE_UPDATE_TEST_FILENAME
|
|
|
|
bytes, err = os.ReadFile(FilenameTemplateCrud)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic("ReadFile() ", FilenameTemplateCrud, " error: ", err)
|
|
|
|
}
|
|
|
|
TextCrud := string(bytes)
|
|
|
|
TextCrud = TextCrud + "\n"
|
2024-02-13 17:43:47 +03:00
|
|
|
|
2024-02-20 16:33:21 +03:00
|
|
|
//заменим имя пакета на новое
|
|
|
|
TextCrud = create_files.ReplacePackageName(TextCrud, DirReadyTable)
|
2024-04-02 11:54:01 +03:00
|
|
|
TextCrud = strings.ReplaceAll(TextCrud, config.Settings.TEXT_TEMPLATE_MODEL, Table1.NameGo)
|
|
|
|
TextCrud = strings.ReplaceAll(TextCrud, config.Settings.TEXT_TEMPLATE_TABLENAME, Table1.Name)
|
2024-02-13 17:43:47 +03:00
|
|
|
|
|
|
|
//заменим импорты
|
2024-02-20 17:14:48 +03:00
|
|
|
if config.Settings.USE_DEFAULT_TEMPLATE == true {
|
|
|
|
TextCrud = create_files.DeleteTemplateRepositoryImports(TextCrud)
|
2024-02-13 17:43:47 +03:00
|
|
|
|
2024-02-20 17:14:48 +03:00
|
|
|
ModelTableURL := create_files.FindModelTableURL(TableName)
|
|
|
|
TextCrud = create_files.AddImport(TextCrud, ModelTableURL)
|
|
|
|
|
2024-05-31 16:09:51 +03:00
|
|
|
//Postgres_ID_Test = ID Minimum
|
|
|
|
TextCrud = create_files.Replace_Postgres_ID_Test(TextCrud, Table1)
|
|
|
|
|
2024-04-25 16:32:34 +03:00
|
|
|
TextCrud = create_files.ReplacePrimaryKeyM_ID(TextCrud, Table1)
|
|
|
|
TextCrud = create_files.ReplacePrimaryKeyOtvetID(TextCrud, Table1)
|
2024-03-12 17:19:36 +03:00
|
|
|
//TextCrud = create_files.ConvertRequestIdToAlias(TextCrud, Table1)
|
2024-05-03 15:57:49 +03:00
|
|
|
|
|
|
|
ConstantsURL := create_files.FindConstantsURL()
|
|
|
|
TextCrud = create_files.AddImport(TextCrud, ConstantsURL)
|
|
|
|
|
2024-05-03 16:40:56 +03:00
|
|
|
//замена "postgres_gorm.Connect_WithApplicationName("
|
|
|
|
TextCrud = create_files.ReplaceConnect_WithApplicationName(TextCrud)
|
|
|
|
|
2024-02-20 17:14:48 +03:00
|
|
|
}
|
2024-02-13 17:43:47 +03:00
|
|
|
|
|
|
|
//создание текста
|
|
|
|
TextUpdateEveryColumn := FindTextUpdateEveryColumnTest(TextCrudUpdateFunc, Table1)
|
|
|
|
// пустой файл не нужен
|
|
|
|
if TextUpdateEveryColumn == "" {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
TextCrud = TextCrud + TextUpdateEveryColumn
|
|
|
|
|
|
|
|
TextCrud = config.Settings.TEXT_MODULE_GENERATED + TextCrud
|
|
|
|
|
2024-02-15 15:15:16 +03:00
|
|
|
//SkipNow() если нет строк в БД
|
|
|
|
TextCrud = create_files.AddSkipNow(TextCrud, Table1)
|
|
|
|
|
2024-02-13 17:43:47 +03:00
|
|
|
//удаление пустого импорта
|
|
|
|
TextCrud = create_files.DeleteEmptyImport(TextCrud)
|
2024-02-19 14:55:52 +03:00
|
|
|
|
|
|
|
//удаление пустых строк
|
2024-02-13 17:43:47 +03:00
|
|
|
TextCrud = create_files.DeleteEmptyLines(TextCrud)
|
|
|
|
|
|
|
|
//запись файла
|
|
|
|
err = os.WriteFile(FilenameReadyCrudUpdate, []byte(TextCrud), constants.FILE_PERMISSIONS)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindTextUpdateEveryColumnTest - возвращает текст для всех таблиц
|
|
|
|
func FindTextUpdateEveryColumnTest(TextCrudUpdateFunc string, Table1 *types.Table) string {
|
|
|
|
Otvet := ""
|
|
|
|
|
|
|
|
//сортировка по названию таблиц
|
|
|
|
keys := make([]string, 0, len(Table1.MapColumns))
|
|
|
|
for k := range Table1.MapColumns {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
//найдём новый текст для каждой таблицы
|
|
|
|
for _, key1 := range keys {
|
|
|
|
Column1, ok := Table1.MapColumns[key1]
|
|
|
|
if ok == false {
|
|
|
|
log.Panic("FindTextUpdateEveryColumnTest() Table1.MapColumns[key1] = false")
|
|
|
|
}
|
2024-02-14 16:04:22 +03:00
|
|
|
if create_files.Is_NotNeedUpdate_Сolumn(Column1) == true {
|
2024-02-13 17:43:47 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
TextColumn1 := FindTextUpdateEveryColumnTest1(TextCrudUpdateFunc, Table1, Column1)
|
|
|
|
Otvet = Otvet + TextColumn1 + "\n\n"
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindTextUpdateEveryColumnTest1 - возвращает текст для одной таблицы
|
|
|
|
func FindTextUpdateEveryColumnTest1(TextCrudUpdateFunc string, Table1 *types.Table, Column1 *types.Column) string {
|
|
|
|
Otvet := TextCrudUpdateFunc
|
|
|
|
|
|
|
|
ModelName := Table1.NameGo
|
|
|
|
ColumnName := Column1.NameGo
|
|
|
|
FuncName := "Update_" + ColumnName
|
2024-05-13 18:07:32 +03:00
|
|
|
TextRequest, TextRequestFieldName := create_files.FindTextProtobufRequest(Table1)
|
2024-02-13 17:43:47 +03:00
|
|
|
DefaultValue := create_files.FindTextDefaultValue(Column1.TypeGo)
|
|
|
|
|
2024-05-31 16:09:51 +03:00
|
|
|
//Postgres_ID_Test = ID Minimum
|
|
|
|
Otvet = create_files.Replace_Postgres_ID_Test(Otvet, Table1)
|
|
|
|
|
2024-04-25 16:32:34 +03:00
|
|
|
Otvet = create_files.ReplacePrimaryKeyM_ID(Otvet, Table1)
|
|
|
|
Otvet = create_files.ReplacePrimaryKeyOtvetID(Otvet, Table1)
|
|
|
|
|
2024-02-13 17:43:47 +03:00
|
|
|
Otvet = strings.ReplaceAll(Otvet, config.Settings.TEXT_TEMPLATE_TABLENAME, Table1.Name)
|
|
|
|
Otvet = strings.ReplaceAll(Otvet, config.Settings.TEXT_TEMPLATE_MODEL, ModelName)
|
|
|
|
Otvet = strings.ReplaceAll(Otvet, "grpc_proto.RequestId", "grpc_proto."+TextRequest)
|
|
|
|
Otvet = strings.ReplaceAll(Otvet, "ColumnName", ColumnName)
|
|
|
|
Otvet = strings.ReplaceAll(Otvet, "Request.ID", "Request."+TextRequestFieldName)
|
2024-04-26 16:45:09 +03:00
|
|
|
//Otvet = strings.ReplaceAll(Otvet, "Otvet.Name", "Otvet."+ColumnName)
|
2024-02-13 17:43:47 +03:00
|
|
|
//Otvet = strings.ReplaceAll(Otvet, "Postgres_ID_Test", DefaultValue)
|
2024-02-14 11:40:19 +03:00
|
|
|
Otvet = strings.ReplaceAll(Otvet, "TestUpdate(", "Test"+FuncName+"(")
|
|
|
|
Otvet = strings.ReplaceAll(Otvet, ".Update(", "."+FuncName+"(")
|
2024-02-13 17:43:47 +03:00
|
|
|
Otvet = strings.ReplaceAll(Otvet, " DefaultValue", " "+DefaultValue)
|
|
|
|
|
|
|
|
return Otvet
|
|
|
|
}
|
2024-03-06 15:37:36 +03:00
|
|
|
|
|
|
|
// CreateFilesCache - создаёт 1 файл "*_cache.go" в папке crud
|
|
|
|
func CreateFilesCache(Table1 *types.Table) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
TableName := strings.ToLower(Table1.Name)
|
|
|
|
|
|
|
|
//чтение файлов
|
|
|
|
DirBin := micro.ProgramDir_bin()
|
|
|
|
DirTemplates := DirBin + config.Settings.TEMPLATE_FOLDERNAME + micro.SeparatorFile()
|
|
|
|
DirReady := DirBin + config.Settings.READY_FOLDERNAME + micro.SeparatorFile()
|
|
|
|
DirTemplatesCrud := DirTemplates + config.Settings.TEMPLATE_FOLDERNAME_CRUD + micro.SeparatorFile()
|
|
|
|
DirReadyCrud := DirReady + config.Settings.TEMPLATE_FOLDERNAME_CRUD + micro.SeparatorFile() + config.Settings.PREFIX_CRUD + TableName + micro.SeparatorFile()
|
|
|
|
|
|
|
|
FilenameTemplateCache := DirTemplatesCrud + constants.CRUD_TABLES_CACHE_FILENAME
|
|
|
|
DirReadyTable := DirReadyCrud
|
2024-03-06 17:45:55 +03:00
|
|
|
FilenameReadyCache := DirReadyTable + "crud_" + TableName + "_cache.go"
|
2024-03-06 15:37:36 +03:00
|
|
|
|
|
|
|
//создадим папку готовых файлов
|
|
|
|
folders.CreateFolder(DirReadyTable)
|
|
|
|
|
|
|
|
//читаем шаблон файла
|
|
|
|
bytes, err := os.ReadFile(FilenameTemplateCache)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic("ReadFile() ", FilenameTemplateCache, " error: ", err)
|
|
|
|
}
|
|
|
|
TextCache := string(bytes)
|
|
|
|
|
|
|
|
//заменим имя пакета на новое
|
|
|
|
TextCache = create_files.ReplacePackageName(TextCache, DirReadyTable)
|
|
|
|
|
|
|
|
//заменим импорты
|
|
|
|
if config.Settings.USE_DEFAULT_TEMPLATE == true {
|
|
|
|
TextCache = create_files.DeleteTemplateRepositoryImports(TextCache)
|
|
|
|
|
|
|
|
DBConstantsURL := create_files.FindDBConstantsURL()
|
|
|
|
TextCache = create_files.AddImport(TextCache, DBConstantsURL)
|
|
|
|
|
|
|
|
ModelTableURL := create_files.FindModelTableURL(TableName)
|
|
|
|
TextCache = create_files.AddImport(TextCache, ModelTableURL)
|
|
|
|
|
2024-04-25 16:32:34 +03:00
|
|
|
TextCache = create_files.ReplacePrimaryKeyOtvetID(TextCache, Table1)
|
2024-03-12 15:35:08 +03:00
|
|
|
//TextCache = create_files.ConvertRequestIdToAlias(TextCache, Table1)
|
2024-03-06 15:37:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//замена слов
|
|
|
|
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
|
|
|
|
|
2024-03-12 15:35:08 +03:00
|
|
|
//alias
|
|
|
|
TextCache = create_files.ConvertIDToAlias_OtvetID(TextCache, Table1)
|
|
|
|
|
2024-03-21 11:25:12 +03:00
|
|
|
//const CACHE_SIZE = 1000
|
|
|
|
CACHE_ELEMENTS_COUNT_MAX := config.Settings.CACHE_ELEMENTS_COUNT_MAX
|
|
|
|
Count_Now := Table1.RowsCount
|
|
|
|
CACHE_ELEMENTS_COUNT := micro.MinInt64(Count_Now, CACHE_ELEMENTS_COUNT_MAX)
|
|
|
|
sCACHE_ELEMENTS_COUNT := micro.StringFromInt64(CACHE_ELEMENTS_COUNT)
|
|
|
|
TextCache = create_files.FillVariable(TextCache, constants.TEXT_CACHE_SIZE_1000, sCACHE_ELEMENTS_COUNT)
|
|
|
|
|
2024-04-09 18:04:05 +03:00
|
|
|
//тип ID кэша
|
2024-05-24 16:39:16 +03:00
|
|
|
if Table1.PrimaryKeyColumnsCount == 1 {
|
|
|
|
_, ColumnTypeGo := create_files.FindPrimaryKeyNameTypeGo(Table1)
|
|
|
|
TextCache = strings.ReplaceAll(TextCache, "LRU[int64", "LRU["+ColumnTypeGo)
|
|
|
|
TextCache = strings.ReplaceAll(TextCache, "ID int64", "ID "+ColumnTypeGo)
|
|
|
|
} else {
|
|
|
|
TextCache = strings.ReplaceAll(TextCache, "LRU[int64", "LRU[string")
|
|
|
|
TextCache = create_files.ReplacePrimaryKeyOtvetID_Many(TextCache, Table1)
|
|
|
|
TextIDMany := "(ID)"
|
|
|
|
TextIDMany = create_files.ReplaceIDtoID_Many(TextIDMany, Table1)
|
|
|
|
TextCache = strings.ReplaceAll(TextCache, "(ID)", "("+Table1.Name+".StringIdentifier"+TextIDMany+")")
|
|
|
|
TextCache = create_files.ReplaceIDtoID_Many(TextCache, Table1)
|
|
|
|
}
|
2024-04-09 18:04:05 +03:00
|
|
|
|
|
|
|
//uuid
|
|
|
|
TextCache = create_files.CheckAndAddImportUUID_FromText(TextCache)
|
|
|
|
|
2024-03-06 15:37:36 +03:00
|
|
|
//удаление пустого импорта
|
|
|
|
TextCache = create_files.DeleteEmptyImport(TextCache)
|
|
|
|
|
|
|
|
//удаление пустых строк
|
|
|
|
TextCache = create_files.DeleteEmptyLines(TextCache)
|
|
|
|
|
|
|
|
//запись файла
|
|
|
|
err = os.WriteFile(FilenameReadyCache, []byte(TextCache), constants.FILE_PERMISSIONS)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateFilesCacheTest - создаёт 1 файл "*_cache_test.go" в папке crud
|
|
|
|
func CreateFilesCacheTest(Table1 *types.Table) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
TableName := strings.ToLower(Table1.Name)
|
|
|
|
|
|
|
|
//чтение файлов
|
|
|
|
DirBin := micro.ProgramDir_bin()
|
|
|
|
DirTemplates := DirBin + config.Settings.TEMPLATE_FOLDERNAME + micro.SeparatorFile()
|
|
|
|
DirReady := DirBin + config.Settings.READY_FOLDERNAME + micro.SeparatorFile()
|
|
|
|
DirTemplatesCrud := DirTemplates + config.Settings.TEMPLATE_FOLDERNAME_CRUD + micro.SeparatorFile()
|
|
|
|
DirReadyCrud := DirReady + config.Settings.TEMPLATE_FOLDERNAME_CRUD + micro.SeparatorFile() + config.Settings.PREFIX_CRUD + TableName + micro.SeparatorFile()
|
|
|
|
|
|
|
|
FilenameTemplateCache := DirTemplatesCrud + constants.CRUD_TABLES_CACHE_TEST_FILENAME
|
|
|
|
DirReadyTable := DirReadyCrud
|
2024-03-06 17:45:55 +03:00
|
|
|
FilenameReadyCache := DirReadyTable + "crud_" + TableName + "_cache_test.go"
|
2024-03-06 15:37:36 +03:00
|
|
|
|
|
|
|
//создадим папку готовых файлов
|
|
|
|
folders.CreateFolder(DirReadyTable)
|
|
|
|
|
|
|
|
//читаем шаблон файла
|
|
|
|
bytes, err := os.ReadFile(FilenameTemplateCache)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic("ReadFile() ", FilenameTemplateCache, " error: ", err)
|
|
|
|
}
|
|
|
|
TextCache := string(bytes)
|
|
|
|
|
|
|
|
//заменим имя пакета на новое
|
|
|
|
TextCache = create_files.ReplacePackageName(TextCache, DirReadyTable)
|
|
|
|
|
|
|
|
//заменим импорты
|
|
|
|
if config.Settings.USE_DEFAULT_TEMPLATE == true {
|
|
|
|
TextCache = create_files.DeleteTemplateRepositoryImports(TextCache)
|
|
|
|
|
2024-03-12 17:19:36 +03:00
|
|
|
//DBConstantsURL := create_files.FindDBConstantsURL()
|
|
|
|
//TextCache = create_files.AddImport(TextCache, DBConstantsURL)
|
|
|
|
//
|
2024-06-03 10:44:37 +03:00
|
|
|
ModelTableURL := create_files.FindModelTableURL(TableName)
|
|
|
|
TextCache = create_files.AddImport(TextCache, ModelTableURL)
|
2024-03-06 15:37:36 +03:00
|
|
|
|
2024-03-12 15:35:08 +03:00
|
|
|
//TextCache = create_files.ConvertRequestIdToAlias(TextCache, Table1)
|
2024-05-03 15:57:49 +03:00
|
|
|
ConstantsURL := create_files.FindConstantsURL()
|
|
|
|
TextCache = create_files.AddImport(TextCache, ConstantsURL)
|
|
|
|
|
2024-05-03 16:40:56 +03:00
|
|
|
//замена "postgres_gorm.Connect_WithApplicationName("
|
|
|
|
TextCache = create_files.ReplaceConnect_WithApplicationName(TextCache)
|
|
|
|
|
2024-05-31 16:09:51 +03:00
|
|
|
//тип ID кэша
|
|
|
|
if Table1.PrimaryKeyColumnsCount == 1 {
|
2024-06-03 10:44:37 +03:00
|
|
|
//Postgres_ID_Test = ID Minimum
|
|
|
|
TextCache = create_files.Replace_Postgres_ID_Test(TextCache, Table1)
|
|
|
|
|
2024-05-31 16:09:51 +03:00
|
|
|
} else {
|
|
|
|
TextIDMany := "(ID)"
|
|
|
|
TextIDMany = create_files.ReplaceIDtoID_Many(TextIDMany, Table1)
|
|
|
|
//TextCache = strings.ReplaceAll(TextCache, "(ID)", "("+Table1.Name+".StringIdentifier"+TextIDMany+")")
|
|
|
|
//TextCache = create_files.ReplaceIDtoID_Many(TextCache, Table1)
|
|
|
|
//TextIDMany := create_files.FindTextNameTest_ManyPK(Table1)
|
|
|
|
//TextCache = strings.ReplaceAll(TextCache, "ReadFromCache(Postgres_ID_Test)", "ReadFromCache("+TextIDMany+")")
|
|
|
|
//TextCache = create_files.Replace_Postgres_ID_Test(TextCache, Table1)
|
2024-05-31 16:22:52 +03:00
|
|
|
EntityURL := create_files.FindModelTableURL(Table1.Name)
|
|
|
|
TextCache = create_files.AddImport(TextCache, EntityURL)
|
|
|
|
|
2024-05-31 16:09:51 +03:00
|
|
|
TextCache = create_files.Replace_Postgres_ID_Test_ManyPK(TextCache, Table1)
|
|
|
|
}
|
2024-03-06 15:37:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//замена слов
|
|
|
|
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)
|
|
|
|
|
|
|
|
//удаление пустых строк
|
|
|
|
TextCache = create_files.DeleteEmptyLines(TextCache)
|
|
|
|
|
2024-03-12 17:19:36 +03:00
|
|
|
//SkipNow() если нет строк в БД
|
|
|
|
TextCache = create_files.AddSkipNow(TextCache, Table1)
|
|
|
|
|
2024-03-06 15:37:36 +03:00
|
|
|
//запись файла
|
|
|
|
err = os.WriteFile(FilenameReadyCache, []byte(TextCache), constants.FILE_PERMISSIONS)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|