1
0
mirror of https://github.com/ManyakRus/crud_generator.git synced 2024-12-18 23:47:38 +02:00
crud_generator/internal/create_files/db/db.go

331 lines
8.9 KiB
Go
Raw Normal View History

2023-10-25 15:22:30 +02:00
package db
import (
"github.com/ManyakRus/crud_generator/internal/config"
"github.com/ManyakRus/crud_generator/internal/constants"
2023-10-26 10:33:12 +02:00
"github.com/ManyakRus/crud_generator/internal/create_files"
2023-10-27 10:43:15 +02:00
"github.com/ManyakRus/crud_generator/internal/mini_func"
2023-10-25 15:22:30 +02:00
"github.com/ManyakRus/crud_generator/internal/types"
"github.com/ManyakRus/starter/log"
"github.com/ManyakRus/starter/micro"
"os"
"strings"
)
2023-10-26 10:33:12 +02:00
// CreateAllFiles - создаёт все файлы в папке db
func CreateAllFiles(MapAll map[string]*types.Table) error {
2023-10-25 15:22:30 +02:00
var err error
for _, table1 := range MapAll {
//файлы db
2023-10-26 10:33:12 +02:00
err = CreateFiles(table1)
2023-10-25 15:22:30 +02:00
if err != nil {
2023-10-26 10:33:12 +02:00
log.Error("CreateFiles() table: ", table1.Name, " error: ", err)
2023-10-25 15:22:30 +02:00
return err
}
//тестовые файлы db
2023-10-26 10:33:12 +02:00
err = CreateTestFiles(table1)
2023-10-25 15:22:30 +02:00
if err != nil {
2023-10-26 10:33:12 +02:00
log.Error("CreateTestFiles() table: ", table1.Name, " error: ", err)
2023-10-25 15:22:30 +02:00
return err
}
}
return err
}
2023-10-26 10:33:12 +02:00
// CreateFiles - создаёт 1 файл в папке db
func CreateFiles(Table1 *types.Table) error {
2023-10-25 15:22:30 +02:00
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)
2023-10-25 15:38:35 +02:00
DirTable := DirReadyDB + "db_" + TableName
FilenameReadyDB := DirTable + micro.SeparatorFile() + "db_" + TableName + ".go"
2023-10-25 15:22:30 +02:00
//создадим каталог
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 {
2023-10-25 16:30:18 +02:00
TextDB = DeleteFuncDelete(TextDB, Table1)
TextDB = DeleteFuncDeleteCtx(TextDB, Table1)
TextDB = DeleteFuncRestore(TextDB, Table1)
TextDB = DeleteFuncRestoreCtx(TextDB, Table1)
2023-10-25 15:22:30 +02:00
}
2023-10-25 16:30:18 +02:00
TextDB = DeleteFuncFind_byExtID(TextDB, Table1)
2023-10-26 14:23:47 +02:00
TextDB = DeleteFuncFind_byExtIDCtx(TextDB, Table1)
2023-10-25 16:30:18 +02:00
TextDB = AddTextOmit(TextDB, Table1)
2023-10-25 15:22:30 +02:00
//запись файла
err = os.WriteFile(FilenameReadyDB, []byte(TextDB), constants.FILE_PERMISSIONS)
return err
}
2023-10-26 10:33:12 +02:00
// CreateTestFiles - создаёт 1 файл в папке db
func CreateTestFiles(Table1 *types.Table) error {
2023-10-25 15:22:30 +02:00
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)
2023-10-25 15:38:35 +02:00
DirTable := DirReadyDB + "db_" + TableName
FilenameReadyDB := DirTable + micro.SeparatorFile() + "db_" + TableName + "_test.go"
2023-10-25 15:22:30 +02:00
//создадим каталог
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 {
2023-10-26 14:23:47 +02:00
TextDB = DeleteFuncTestDelete(TextDB, Table1)
TextDB = DeleteFuncTestRestore(TextDB, Table1)
2023-10-25 15:22:30 +02:00
}
2023-10-26 14:23:47 +02:00
TextDB = DeleteFuncTestFind_byExtID(TextDB, Table1)
2023-10-25 15:22:30 +02:00
2023-10-27 10:43:15 +02:00
//ID Minimum
if Table1.IDMinimum != "" {
TextFind := "const Postgres_ID_Test = "
TextDB = strings.ReplaceAll(TextDB, TextFind+"1", TextFind+Table1.IDMinimum)
}
2023-10-25 15:22:30 +02:00
//запись файла
err = os.WriteFile(FilenameReadyDB, []byte(TextDB), constants.FILE_PERMISSIONS)
return err
}
// DeleteFuncDelete - удаляет функцию Delete()
2023-10-26 10:33:12 +02:00
func DeleteFuncDelete(Text string, Table1 *types.Table) string {
Otvet := Text
2023-10-25 15:22:30 +02:00
_, ok := Table1.MapColumns["is_deleted"]
if ok == true {
return Otvet
}
2023-10-26 10:33:12 +02:00
Otvet = create_files.DeleteFuncFromComment(Text, "\n// Delete ")
2023-10-25 15:22:30 +02:00
return Otvet
}
// DeleteFuncRestore - удаляет функцию Restore()
2023-10-26 10:33:12 +02:00
func DeleteFuncRestore(Text string, Table1 *types.Table) string {
Otvet := Text
2023-10-25 15:22:30 +02:00
_, ok := Table1.MapColumns["is_deleted"]
if ok == true {
return Otvet
}
2023-10-26 10:33:12 +02:00
Otvet = create_files.DeleteFuncFromComment(Text, "\n// Restore ")
2023-10-25 15:22:30 +02:00
return Otvet
}
// DeleteFuncDeleteCtx - удаляет функцию Delete_ctx()
2023-10-26 10:33:12 +02:00
func DeleteFuncDeleteCtx(Text string, Table1 *types.Table) string {
Otvet := Text
2023-10-25 15:22:30 +02:00
_, ok := Table1.MapColumns["is_deleted"]
if ok == true {
return Otvet
}
2023-10-26 10:33:12 +02:00
Otvet = create_files.DeleteFuncFromComment(Text, "\n// Delete_ctx ")
2023-10-25 15:22:30 +02:00
return Otvet
}
// DeleteFuncRestoreCtx - удаляет функцию Restore_ctx()
2023-10-26 10:33:12 +02:00
func DeleteFuncRestoreCtx(Text string, Table1 *types.Table) string {
Otvet := Text
2023-10-25 15:22:30 +02:00
_, ok := Table1.MapColumns["is_deleted"]
if ok == true {
return Otvet
}
2023-10-26 10:33:12 +02:00
Otvet = create_files.DeleteFuncFromComment(Text, "\n// Restore_ctx ")
2023-10-25 15:22:30 +02:00
return Otvet
}
// DeleteFuncFind_byExtID - удаляет функцию Find_ByExtID()
2023-10-26 10:33:12 +02:00
func DeleteFuncFind_byExtID(Text string, Table1 *types.Table) string {
Otvet := Text
2023-10-25 15:22:30 +02:00
2023-10-26 14:23:47 +02:00
//если есть обе колонки - ничего не делаем
ok := create_files.Has_Column_ExtID_ConnectionID(Table1)
2023-10-25 15:22:30 +02:00
if ok == true {
return Otvet
}
//
2023-10-26 14:23:47 +02:00
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)
2023-10-25 15:22:30 +02:00
if ok == true {
return Otvet
}
2023-10-26 14:23:47 +02:00
//
Otvet = create_files.DeleteFuncFromComment(Text, "\n// Find_ByExtID_ctx ")
2023-10-25 15:22:30 +02:00
return Otvet
}
// DeleteFuncTestDelete - удаляет функцию Delete()
2023-10-26 10:33:12 +02:00
func DeleteFuncTestDelete(Text string, Table1 *types.Table) string {
Otvet := Text
2023-10-25 15:22:30 +02:00
_, ok := Table1.MapColumns["is_deleted"]
if ok == true {
return Otvet
}
2023-10-26 10:33:12 +02:00
Otvet = create_files.DeleteFuncFromFuncName(Otvet, "TestDelete")
2023-10-25 15:22:30 +02:00
return Otvet
}
// DeleteFuncTestRestore - удаляет функцию Restore()
2023-10-26 10:33:12 +02:00
func DeleteFuncTestRestore(Text string, Table1 *types.Table) string {
Otvet := Text
2023-10-25 15:22:30 +02:00
_, ok := Table1.MapColumns["is_deleted"]
if ok == true {
return Otvet
}
2023-10-26 10:33:12 +02:00
Otvet = create_files.DeleteFuncFromFuncName(Otvet, "TestRestore")
2023-10-25 15:22:30 +02:00
return Otvet
}
// DeleteFuncFind_byExtID - удаляет функцию Find_ByExtID()
2023-10-26 10:33:12 +02:00
func DeleteFuncTestFind_byExtID(Text string, Table1 *types.Table) string {
Otvet := Text
2023-10-25 15:22:30 +02:00
2023-10-26 14:23:47 +02:00
//если есть обе колонки - ничего не делаем
ok := create_files.Has_Column_ExtID_ConnectionID(Table1)
2023-10-25 15:22:30 +02:00
if ok == true {
return Otvet
}
//
2023-10-26 10:33:12 +02:00
Otvet = create_files.DeleteFuncFromFuncName(Otvet, "TestFind_ByExtID")
2023-10-25 15:22:30 +02:00
return Otvet
}
2023-10-25 16:30:18 +02:00
2023-10-26 09:33:18 +02:00
// AddTextOmit - добавляет код для записи null в колонки Nullable
2023-10-25 16:30:18 +02:00
func AddTextOmit(TextDB string, Table1 *types.Table) string {
Otvet := TextDB
TextFind := "\t//игнор пустых колонок"
pos1 := strings.Index(Otvet, TextFind)
if pos1 < 0 {
return Otvet
}
TextOmit := ""
for _, Column1 := range Table1.MapColumns {
TypeGo := Column1.TypeGo
2023-10-26 09:33:18 +02:00
if Column1.IsNullable == false {
2023-10-25 16:30:18 +02:00
continue
}
ColumnNameGo := Column1.NameGo
2023-10-26 09:33:18 +02:00
if TypeGo == "time.Time" {
TextFind := `if m.` + ColumnNameGo + `.IsZero() == true {`
pos1 := strings.Index(TextDB, TextFind)
if pos1 >= 0 {
continue
}
TextOmit = TextOmit + "\t" + `ColumnName = "` + ColumnNameGo + `"
2023-10-25 16:30:18 +02:00
if m.` + ColumnNameGo + `.IsZero() == true {
MassOmit = append(MassOmit, ColumnName)
}
`
2023-10-27 10:43:15 +02:00
} else if mini_func.IsNumberType(TypeGo) == true && Column1.TableKey != "" {
2023-10-26 09:33:18 +02: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 16:30:18 +02:00
}
Otvet = Otvet[:pos1] + TextOmit + Otvet[pos1:]
return Otvet
}