2023-10-24 17:03:04 +02:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/ManyakRus/crud_generator/internal/config"
|
|
|
|
"github.com/ManyakRus/crud_generator/internal/constants"
|
|
|
|
"github.com/ManyakRus/crud_generator/internal/create_files"
|
|
|
|
"github.com/ManyakRus/crud_generator/internal/types"
|
2023-10-25 10:41:26 +02:00
|
|
|
"github.com/ManyakRus/crud_generator/pkg/dbmeta"
|
2023-10-25 13:52:49 +02:00
|
|
|
"github.com/ManyakRus/starter/log"
|
2023-10-24 17:03:04 +02:00
|
|
|
"github.com/ManyakRus/starter/micro"
|
|
|
|
"os"
|
2023-10-25 15:22:30 +02:00
|
|
|
"sort"
|
2023-10-25 13:52:49 +02:00
|
|
|
"strconv"
|
2023-10-24 17:03:04 +02:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2023-10-26 10:33:12 +02:00
|
|
|
func CreateAllFiles(MapAll map[string]*types.Table) error {
|
2023-10-24 17:03:04 +02:00
|
|
|
var err error
|
|
|
|
|
|
|
|
for _, table1 := range MapAll {
|
2023-10-26 10:33:12 +02:00
|
|
|
err = CreateFiles(table1)
|
2023-10-24 17:03:04 +02:00
|
|
|
if err != nil {
|
2023-10-26 10:33:12 +02:00
|
|
|
log.Error("CreateFiles() table: ", table1.Name, " error: ", err)
|
2023-10-24 17:03:04 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-26 10:33:12 +02:00
|
|
|
func CreateFiles(Table1 *types.Table) error {
|
2023-10-24 17:03:04 +02:00
|
|
|
var err error
|
|
|
|
|
2023-10-25 13:52:49 +02:00
|
|
|
//чтение файлов
|
2023-10-24 17:03:04 +02:00
|
|
|
DirBin := micro.ProgramDir_bin()
|
|
|
|
DirTemplates := DirBin + constants.FolderTemplates + micro.SeparatorFile()
|
|
|
|
DirReady := DirBin + constants.FolderReady + micro.SeparatorFile()
|
|
|
|
DirTemplatesModel := DirTemplates + config.Settings.TEMPLATE_FOLDERNAME_MODEL + micro.SeparatorFile()
|
|
|
|
DirReadyModel := DirReady + "pkg" + micro.SeparatorFile() + "model" + micro.SeparatorFile()
|
|
|
|
|
|
|
|
FilenameTemplateModel := DirTemplatesModel + "model.go_"
|
|
|
|
FilenameReadyModel := DirReadyModel + strings.ToLower(Table1.Name) + ".go"
|
|
|
|
|
|
|
|
bytes, err := os.ReadFile(FilenameTemplateModel)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic("ReadFile() ", FilenameTemplateModel, " error: ", err)
|
|
|
|
}
|
2023-10-25 13:52:49 +02:00
|
|
|
TextModel := string(bytes)
|
2023-10-24 17:03:04 +02:00
|
|
|
|
2023-10-25 13:52:49 +02:00
|
|
|
//создание текста
|
2023-10-25 10:41:26 +02:00
|
|
|
TextModelStruct, ModelName, err := FindTextModelStruct(Table1)
|
2023-10-25 13:52:49 +02:00
|
|
|
TextModel = ReplaceModelStruct(TextModel, TextModelStruct)
|
|
|
|
|
|
|
|
////(l LawsuitStatusType) = (b Branch)
|
|
|
|
//TextTemplateVarModel := "(" + strings.ToLower(config.Settings.TEXT_TEMPLATE_MODEL[:1]) + " " + config.Settings.TEXT_TEMPLATE_MODEL
|
|
|
|
//TextVarModel := "(" + strings.ToLower(ModelName[:1]) + " " + ModelName
|
|
|
|
//TextModel = strings.ReplaceAll(TextModel, TextTemplateVarModel, TextVarModel)
|
|
|
|
//
|
|
|
|
////(l *LawsuitStatusType) = (b *Branch)
|
|
|
|
//TextTemplateVarModel = "(" + strings.ToLower(config.Settings.TEXT_TEMPLATE_MODEL[:1]) + " *" + config.Settings.TEXT_TEMPLATE_MODEL
|
|
|
|
//TextVarModel = "(" + strings.ToLower(ModelName[:1]) + " *" + ModelName
|
|
|
|
//TextModel = strings.ReplaceAll(TextModel, TextTemplateVarModel, TextVarModel)
|
|
|
|
|
|
|
|
//
|
|
|
|
TextModel = strings.ReplaceAll(TextModel, config.Settings.TEXT_TEMPLATE_MODEL, ModelName)
|
|
|
|
TextModel = strings.ReplaceAll(TextModel, config.Settings.TEXT_TEMPLATE_TABLENAME, Table1.Name)
|
|
|
|
TextModel = constants.TEXT_GENERATED + TextModel
|
|
|
|
|
|
|
|
if config.Settings.HAS_IS_DELETED == true {
|
|
|
|
TextModel = DeleteFuncDelete(TextModel, ModelName, Table1)
|
|
|
|
TextModel = DeleteFuncRestore(TextModel, ModelName, Table1)
|
|
|
|
}
|
2023-10-25 15:22:30 +02:00
|
|
|
TextModel = DeleteFuncFind_byExtID(TextModel, ModelName, Table1)
|
2023-10-25 13:52:49 +02:00
|
|
|
|
|
|
|
TextModel = AddImportTime(TextModel, Table1)
|
|
|
|
|
|
|
|
//запись файла
|
|
|
|
err = os.WriteFile(FilenameReadyModel, []byte(TextModel), constants.FILE_PERMISSIONS)
|
2023-10-24 17:03:04 +02:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-25 10:41:26 +02:00
|
|
|
func FindTextModelStruct(Table1 *types.Table) (string, string, error) {
|
2023-10-24 17:03:04 +02:00
|
|
|
var Otvet string
|
2023-10-25 10:41:26 +02:00
|
|
|
var ModelName string
|
2023-10-24 17:03:04 +02:00
|
|
|
var err error
|
|
|
|
|
|
|
|
TableName := Table1.Name
|
2023-10-25 10:41:26 +02:00
|
|
|
ModelName = create_files.FindSingularName(TableName)
|
|
|
|
ModelName = create_files.FormatName(ModelName)
|
2023-10-25 15:22:30 +02:00
|
|
|
Table1.NameGo = ModelName
|
2023-10-24 17:03:04 +02:00
|
|
|
|
|
|
|
Otvet = `// ` + ModelName + ` - model from table ` + TableName + `
|
|
|
|
type ` + ModelName + ` struct {
|
|
|
|
`
|
2023-10-25 15:22:30 +02:00
|
|
|
|
|
|
|
//сортировка
|
|
|
|
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)
|
2023-10-25 10:41:26 +02:00
|
|
|
Otvet = Otvet + TextColumn + "\n"
|
2023-10-25 16:30:18 +02:00
|
|
|
Table1.MapColumns[key1] = Column1
|
2023-10-25 10:41:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Otvet = Otvet + "\n}"
|
|
|
|
return Otvet, ModelName, err
|
|
|
|
}
|
|
|
|
|
2023-10-25 15:22:30 +02:00
|
|
|
func FindTextColumn(Column1 *types.Column) string {
|
2023-10-25 10:41:26 +02:00
|
|
|
Otvet := ""
|
|
|
|
// Code string `json:"code" gorm:"column:code;default:0"`
|
|
|
|
|
|
|
|
ColumnName := Column1.Name
|
|
|
|
ColumnModelName := create_files.FormatName(Column1.Name)
|
2023-10-25 15:22:30 +02:00
|
|
|
Column1.NameGo = ColumnModelName
|
2023-10-25 10:41:26 +02:00
|
|
|
SQLMapping1, ok := dbmeta.GetMappings()[Column1.Type]
|
|
|
|
if ok == false {
|
|
|
|
log.Panic("GetMappings() ", Column1.Type, " error: not found")
|
|
|
|
}
|
|
|
|
Type_go := SQLMapping1.GoType
|
2023-10-25 16:30:18 +02:00
|
|
|
Column1.TypeGo = Type_go
|
2023-10-25 10:41:26 +02:00
|
|
|
TextDefaultValue := FindTextDefaultValue(Type_go)
|
2023-10-26 09:33:18 +02:00
|
|
|
TextPrimaryKey := FindTextPrimaryKey(Column1.IsIdentity)
|
2023-10-25 13:52:49 +02:00
|
|
|
Description := Column1.Description
|
|
|
|
Description = strconv.Quote(Description) //экранирование символов
|
2023-10-25 10:41:26 +02:00
|
|
|
|
2023-10-25 13:52:49 +02:00
|
|
|
Otvet = Otvet + "\t" + ColumnModelName + " " + Type_go + "\t"
|
|
|
|
Otvet = Otvet + "`json:\"" + ColumnName + "\""
|
|
|
|
Otvet = Otvet + "\tgorm:\"column:" + ColumnName + TextPrimaryKey + TextDefaultValue + "\""
|
|
|
|
Otvet = Otvet + "\tdb:\"" + ColumnName + "\""
|
2023-10-25 10:41:26 +02:00
|
|
|
Otvet = Otvet + "`"
|
2023-10-25 13:52:49 +02:00
|
|
|
Otvet = Otvet + "\t//" + Description
|
2023-10-25 10:41:26 +02:00
|
|
|
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
|
|
|
func FindTextDefaultValue(Type_go string) string {
|
|
|
|
var Otvet string
|
|
|
|
|
|
|
|
sValue := ""
|
|
|
|
switch Type_go {
|
|
|
|
case "string":
|
2023-10-25 13:52:49 +02:00
|
|
|
sValue = "\\\"\\\""
|
2023-10-25 10:41:26 +02:00
|
|
|
case "int", "int32", "int64", "float32", "float64", "uint", "uint32", "uint64":
|
|
|
|
sValue = "0"
|
|
|
|
case "time.Time":
|
|
|
|
sValue = "null"
|
|
|
|
}
|
|
|
|
|
|
|
|
if sValue != "" {
|
|
|
|
Otvet = ";default:" + sValue
|
|
|
|
}
|
|
|
|
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
|
|
|
func FindTextPrimaryKey(Is_identity bool) string {
|
|
|
|
Otvet := ""
|
|
|
|
|
|
|
|
if Is_identity == true {
|
|
|
|
Otvet = ";primaryKey;autoIncrement:true"
|
|
|
|
}
|
|
|
|
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReplaceModelStruct(TextTemplateModel, TextModelStruct string) string {
|
|
|
|
Otvet := ""
|
|
|
|
|
|
|
|
ModelName := config.Settings.TEXT_TEMPLATE_MODEL
|
|
|
|
|
2023-10-25 13:52:49 +02:00
|
|
|
//найдём начало и конец
|
2023-10-25 10:41:26 +02:00
|
|
|
TextFind1 := "// " + ModelName
|
|
|
|
pos1 := strings.Index(TextTemplateModel, TextFind1)
|
2023-10-25 13:52:49 +02:00
|
|
|
if pos1 < 0 {
|
2023-10-25 10:41:26 +02:00
|
|
|
TextFind1 := "type " + ModelName + " struct {"
|
|
|
|
pos1 = strings.Index(TextTemplateModel, TextFind1)
|
|
|
|
}
|
|
|
|
|
2023-10-25 13:52:49 +02:00
|
|
|
if pos1 < 0 {
|
2023-10-25 10:41:26 +02:00
|
|
|
log.Panic("ReplaceModelStruct() error: in model.go_ not found text: ", TextFind1)
|
|
|
|
}
|
2023-10-24 17:03:04 +02:00
|
|
|
|
2023-10-25 13:52:49 +02:00
|
|
|
s2 := TextTemplateModel[pos1:]
|
|
|
|
TextFind1 = "}\n"
|
|
|
|
posEnd := strings.Index(s2, TextFind1)
|
|
|
|
if posEnd < 0 {
|
|
|
|
log.Panic("ReplaceModelStruct() error: in model.go_ not found text: ", TextFind1)
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
Otvet = TextTemplateModel[:pos1] + TextModelStruct + TextTemplateModel[pos1+posEnd+1:]
|
|
|
|
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteFuncDelete - удаляет функцию Delete()
|
|
|
|
func DeleteFuncDelete(TextModel, ModelName string, Table1 *types.Table) string {
|
|
|
|
Otvet := TextModel
|
|
|
|
|
|
|
|
_, ok := Table1.MapColumns["is_deleted"]
|
|
|
|
if ok == true {
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
|
|
|
//FirstSymbol := strings.ToLower(ModelName)[:1]
|
|
|
|
TextFind := "Delete(*" + ModelName + ") error"
|
|
|
|
Otvet = strings.ReplaceAll(Otvet, TextFind, "")
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
//FirstSymbol := strings.ToLower(Modelname)[:1]
|
|
|
|
TextFind := "Restore(*" + Modelname + ") error"
|
|
|
|
Otvet = strings.ReplaceAll(Otvet, TextFind, "")
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func AddImportTime(TextModel string, Table1 *types.Table) string {
|
|
|
|
Otvet := TextModel
|
|
|
|
|
|
|
|
//если уже есть импорт
|
|
|
|
pos1 := strings.Index(Otvet, `"time"`)
|
|
|
|
if pos1 >= 0 {
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
2023-10-25 13:59:28 +02:00
|
|
|
HasTimeColumn := FindHasTimeColumn(Table1)
|
|
|
|
if HasTimeColumn == false {
|
|
|
|
return Otvet
|
|
|
|
}
|
|
|
|
|
2023-10-25 13:52:49 +02:00
|
|
|
//
|
|
|
|
pos1 = strings.Index(Otvet, "import (")
|
|
|
|
if pos1 < 0 {
|
|
|
|
log.Error("not found word: import (")
|
|
|
|
return TextModel
|
|
|
|
}
|
|
|
|
|
|
|
|
Otvet = Otvet[:pos1+8] + "\n\t" + `"time"` + Otvet[pos1+8:]
|
|
|
|
|
2023-10-25 10:41:26 +02:00
|
|
|
return Otvet
|
2023-10-24 17:03:04 +02:00
|
|
|
}
|
2023-10-25 13:59:28 +02:00
|
|
|
|
|
|
|
func FindHasTimeColumn(Table1 *types.Table) bool {
|
|
|
|
Otvet := false
|
|
|
|
|
|
|
|
for _, Column1 := range Table1.MapColumns {
|
|
|
|
SQLMapping1, ok := dbmeta.GetMappings()[Column1.Type]
|
|
|
|
if ok == false {
|
|
|
|
log.Panic("GetMappings() ", Column1.Type, " error: not found")
|
|
|
|
}
|
|
|
|
if SQLMapping1.GoType == "time.Time" {
|
|
|
|
Otvet = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Otvet
|
|
|
|
}
|
2023-10-25 15:22:30 +02:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|