1
0
mirror of https://github.com/ManyakRus/crud_generator.git synced 2024-12-22 00:36:41 +02:00

сделал db

This commit is contained in:
Nikitin Aleksandr 2023-10-25 16:22:30 +03:00
parent b276b75b0f
commit 9f2716f678
9 changed files with 429 additions and 4 deletions

1
.gitignore vendored
View File

@ -14,3 +14,4 @@
/database.graphml
/database.graphml0
/bin/ready/pkg/model/
/bin/ready/

View File

@ -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.

View File

@ -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
}

View File

@ -0,0 +1 @@
package db

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}