1
0
mirror of https://github.com/ManyakRus/crud_generator.git synced 2025-01-04 13:23:00 +02:00

сделал find_by_functions.json

This commit is contained in:
Nikitin Aleksandr 2024-08-14 17:31:08 +03:00
parent 12c33e7898
commit a4500dc01a
20 changed files with 628 additions and 84 deletions

View File

@ -0,0 +1 @@
{}

View File

@ -0,0 +1 @@
{"connections":["branch_id","is_legal"]}

View File

@ -0,0 +1 @@
{}

View File

@ -0,0 +1 @@
{"connections":["branch_id"]}

View File

@ -10,11 +10,13 @@ example:
------------------------------------------------------------------------
mapping.json
Mapping Postgres types to Golang types
------------------------------------------------------------------------
name_replace.json
Mapping database field name = golang field name
For tables file.
@ -27,6 +29,7 @@ example:
}
------------------------------------------------------------------------
nullable.json
List of golang field names, which need change 0 to null.
true = need change 0 to null
@ -38,6 +41,7 @@ example:
}
------------------------------------------------------------------------
model_crud_delete_functions.json
Mapping postgres tables to golang function name,
this functions will be deleted from model crud files.
@ -48,6 +52,7 @@ example:
------------------------------------------------------------------------
crud_functions_rename.json
TableName:{old:"",new:""}
example:
@ -59,3 +64,24 @@ example:
}
]
}
------------------------------------------------------------------------
findby_functions.json
Need for automatic create functions searching 1 row in table filtered by column_name
Fill "TableName":["column_name"]
Fill "TableName":["column_name","column2_name"]
example:
{
"lawsuits": ["contract_id"]
}
------------------------------------------------------------------------
findmassby_function.json
Need for automatic create functions searching many rows in table filtered by column_name
Fill "TableName":["column_name"]
Fill "TableName":["column_name","column2_name"]
example:
{
"lawsuits": ["contract_id"]
}

View File

@ -262,4 +262,35 @@ TEMPLATES_README_RUS_FILENAME="readme.rus_"
TEMPLATE_FOLDERNAME_CONSTANTS="pkg/constants"
#TEMPLATES_CONSTANTS_FILENAME - short filename of "constants.go_" file
TEMPLATES_CONSTANTS_FILENAME="constants.go_"
TEMPLATES_CONSTANTS_FILENAME="constants.go_"
#TEMPLATES_FINDBY_FILENAME - short filename of "findby_functions.json" file
TEMPLATES_FINDBY_FILENAME="findby_functions.json"
#TEMPLATES_CRUD_TABLE_FINDBY_FILENAME - short filename of "crud_tables_findby.go_" file
TEMPLATES_CRUD_TABLE_FINDBY_FILENAME="crud_tables_findby.go_"
#TEMPLATES_CRUD_TABLE_FINDBY_TEST_FILENAME - short filename of "crud_tables_findby_test.go_" file
TEMPLATES_CRUD_TABLE_FINDBY_TEST_FILENAME="crud_tables_findby_test.go_"
#TEMPLATES_CRUD_TABLE_FINDBY_FUNCTION_FILENAME - short filename of "crud_tables_findby_function.go_" file
TEMPLATES_CRUD_TABLE_FINDBY_FUNCTION_FILENAME="crud_tables_findby_function.go_"
#TEMPLATES_CRUD_TABLE_FINDBY_FUNCTION_TEST_FILENAME - short filename of "crud_tables_findby_function_test.go_" file
TEMPLATES_CRUD_TABLE_FINDBY_FUNCTION_TEST_FILENAME="crud_tables_findby_function_test.go_"
#TEMPLATES_FINDMASSBY_FILENAME - short filename of "findmassby_functions.json" file
TEMPLATES_FINDMASSBY_FILENAME="findmassby_functions.json"
#TEMPLATES_CRUD_TABLE_FINDMASSBY_FILENAME - short filename of "crud_tables_findmassby.go_" file
TEMPLATES_CRUD_TABLE_FINDMASSBY_FILENAME="crud_tables_findmassby.go_"
#TEMPLATES_CRUD_TABLE_FINDMASSBY_TEST_FILENAME - short filename of "crud_tables_findmassby_test.go_" file
TEMPLATES_CRUD_TABLE_FINDMASSBY_TEST_FILENAME="crud_tables_findmassby_test.go_"
#TEMPLATES_CRUD_TABLE_FINDMASSBY_FUNCTION_FILENAME - short filename of "crud_tables_findmassby_function.go_" file
TEMPLATES_CRUD_TABLE_FINDMASSBY_FUNCTION_FILENAME="crud_tables_findmassby_function.go_"
#TEMPLATES_CRUD_TABLE_FINDMASSBY_FUNCTION_TEST_FILENAME - short filename of "crud_tables_findmassby_function_test.go_" file
TEMPLATES_CRUD_TABLE_FINDMASSBY_FUNCTION_TEST_FILENAME="crud_tables_findmassby_function_test.go_"

View File

@ -0,0 +1,12 @@
package crud_lawsuit_status_types
import (
"context"
"errors"
"github.com/ManyakRus/starter/postgres_gorm"
"gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/db/db_constants"
"gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/object_model/entities/lawsuit_status_types"
"gorm.io/gorm"
"time"
)

View File

@ -0,0 +1,28 @@
// FindBy_FieldNamesWithUnderline - находит запись в БД по FieldNamesWithPlus
func (crud Crud_DB) FindBy_FieldNamesWithUnderline(m *lawsuit_status_types.LawsuitStatusType) error {
var err error
ctxMain := context.Background()
ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(db_constants.TIMEOUT_DB_SECONDS))
defer ctxCancelFunc()
db := postgres_gorm.GetConnection()
err = FindBy_FieldNamesWithUnderline_ctx(ctx, db, m)
return err
}
// FindBy_FieldNamesWithUnderline_ctx - находит запись в БД по FieldNamesWithPlus
func FindBy_FieldNamesWithUnderline_ctx(ctx context.Context, db *gorm.DB, m *lawsuit_status_types.LawsuitStatusType) error {
var err error
tx := db.WithContext(ctx)
tx := tx.Where("ColumnName = ?", m.FieldName)
tx = tx.First(m)
err = tx.Error
return err
}

View File

@ -0,0 +1,15 @@
func TestFindBy_FieldNamesWithUnderline(t *testing.T) {
config_main.LoadEnv()
postgres_gorm.Connect()
defer postgres_gorm.CloseConnection()
crud := Crud_DB{}
Otvet := lawsuit_status_types.LawsuitStatusType{}
Otvet.FieldName = 0
err := crud.FindBy_FieldNamesWithUnderline(&Otvet)
if err != nil {
t.Log("crud_"+TableName+"_test.TestFindBy_FieldNamesWithUnderline() FieldNamesWithUnderline=", FieldNames_TEST, " error: ", err)
}
}

View File

@ -0,0 +1,9 @@
package crud_lawsuit_status_types
import (
"github.com/ManyakRus/starter/config_main"
"github.com/ManyakRus/starter/postgres_gorm"
"gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/object_model/entities/lawsuit_status_types"
"testing"
)

View File

@ -0,0 +1 @@
package crud_lawsuit_status_types

View File

@ -0,0 +1 @@
package crud_lawsuit_status_types

View File

@ -14,89 +14,99 @@ var Settings SettingsINI
// SettingsINI - структура для хранения всех нужных переменных окружения
type SettingsINI struct {
INCLUDE_TABLES string
EXCLUDE_TABLES string
TEMPLATE_FOLDERNAME string
TEMPLATE_FOLDERNAME_MODEL string
TEMPLATE_FOLDERNAME_DB string
TEMPLATE_FOLDERNAME_CRUD string
TEMPLATE_FOLDERNAME_TABLES string
TEMPLATE_FOLDERNAME_GRPC string
TEMPLATE_FOLDERNAME_GRPC_PROTO string
TEMPLATE_FOLDERNAME_GRPC_SERVER string
TEMPLATE_FOLDERNAME_GRPC_CLIENT string
TEMPLATE_FOLDERNAME_NRPC string
TEMPLATE_FOLDERNAME_NRPC_SERVER string
TEMPLATE_FOLDERNAME_NRPC_CLIENT string
TEMPLATE_FOLDERNAME_GRPC_NRPC string
TEMPLATE_FOLDERNAME_GRPC_CLIENT_FUNC string
TEMPLATE_SERVICE_NAME string
TEMPLATE_FOLDERNAME_CRUD_STARTER string
TEMPLATE_FOLDERNAME_ALIAS string
TEMPLATE_FOLDER_CRUD_FUNCTIONS string
TEMPLATE_FOLDERNAME_MAIN string
TEMPLATE_REPOSITORY_URL string
TEMPLATE_EXTERNAL_PROTO_FILENAME string
TEMPLATES_CRUD_FILENAME string
TEMPLATES_CRUD_TEST_FILENAME string
TEMPLATES_ALIAS_FILENAME string
TEMPLATES_CRUD_TABLE_UPDATE_FUNC_FILENAME string
TEMPLATES_CRUD_TABLE_UPDATE_FUNC_TEST_FILENAME string
TEMPLATES_CONVERT_ID_FILENAME string
TEMPLATES_CRUD_FUNCTIONS_RENAME_FILENAME string
TEMPLATES_MODEL_CRUD_DELETE_FUNCTIONS_FILENAME string
TEMPLATES_NAME_REPLACE_FILENAME string
TEMPLATES_NULLABLE_FILENAME string
TEMPLATES_CRUD_TABLE_UPDATE_FILENAME string
TEMPLATES_CRUD_TABLE_UPDATE_TEST_FILENAME string
TEMPLATES_GRPC_CLIENT_TABLE_UPDATE_FILENAME string
TEMPLATES_GRPC_CLIENT_TABLE_UPDATE_TEST_FILENAME string
TEMPLATES_GRPC_SERVER_TABLE_UPDATE_FILENAME string
TEMPLATES_GRPC_SERVER_TABLE_UPDATE_TEST_FILENAME string
TEMPLATES_GRPC_CLIENT_TABLES_CACHE_FILENAME string
TEMPLATES_GRPC_CLIENT_TABLES_CACHE_TEST_FILENAME string
TEMPLATE_GRPC_CLIENT_FUNC_FILENAME string
TEMPLATE_GRPC_CLIENT_FUNC_TEST_FILENAME string
TEMPLATES_README_MD_FILENAME string
TEMPLATES_README_RUS_FILENAME string
TEMPLATE_FOLDERNAME_CONSTANTS string
TEMPLATES_CONSTANTS_FILENAME string
NEED_CREATE_MODEL_STRUCT bool
NEED_CREATE_MODEL_CRUD bool
NEED_CREATE_DB bool
NEED_CREATE_DB_TEST bool
NEED_CREATE_DB_TABLES bool
NEED_CREATE_GRPC bool
NEED_CREATE_GRPC_SERVER_TEST bool
NEED_CREATE_GRPC_CLIENT_TEST bool
NEED_CREATE_NRPC bool
NEED_CREATE_NRPC_SERVER_TEST bool
NEED_CREATE_NRPC_CLIENT_TEST bool
NEED_CREATE_MANUAL_FILES bool
NEED_CREATE_CACHE_API bool
NEED_CREATE_CACHE_FILES bool
NEED_CREATE_CACHE_TEST_FILES bool
SERVICE_NAME string
SERVICE_REPOSITORY_URL string
TEXT_TEMPLATE_MODEL string
TEXT_TEMPLATE_TABLENAME string
HAS_IS_DELETED bool
READY_FOLDERNAME string
TEXT_DB_MODIFIED_AT string
TEXT_DB_CREATED_AT string
TEXT_DB_IS_DELETED string
USE_DEFAULT_TEMPLATE bool
PREFIX_SERVER_GRPC string
PREFIX_CLIENT_GRPC string
PREFIX_CRUD string
PREFIX_MODEL string
PREFIX_TABLE string
COMMENT_MODEL_STRUCT string
TEXT_MODULE_GENERATED string
READY_ALIAS_FILENAME string
NEED_CREATE_UPDATE_EVERY_COLUMN bool
CACHE_ELEMENTS_COUNT_MAX int64
SINGULAR_TABLE_NAMES bool
INCLUDE_TABLES string
EXCLUDE_TABLES string
TEMPLATE_FOLDERNAME string
TEMPLATE_FOLDERNAME_MODEL string
TEMPLATE_FOLDERNAME_DB string
TEMPLATE_FOLDERNAME_CRUD string
TEMPLATE_FOLDERNAME_TABLES string
TEMPLATE_FOLDERNAME_GRPC string
TEMPLATE_FOLDERNAME_GRPC_PROTO string
TEMPLATE_FOLDERNAME_GRPC_SERVER string
TEMPLATE_FOLDERNAME_GRPC_CLIENT string
TEMPLATE_FOLDERNAME_NRPC string
TEMPLATE_FOLDERNAME_NRPC_SERVER string
TEMPLATE_FOLDERNAME_NRPC_CLIENT string
TEMPLATE_FOLDERNAME_GRPC_NRPC string
TEMPLATE_FOLDERNAME_GRPC_CLIENT_FUNC string
TEMPLATE_SERVICE_NAME string
TEMPLATE_FOLDERNAME_CRUD_STARTER string
TEMPLATE_FOLDERNAME_ALIAS string
TEMPLATE_FOLDER_CRUD_FUNCTIONS string
TEMPLATE_FOLDERNAME_MAIN string
TEMPLATE_REPOSITORY_URL string
TEMPLATE_EXTERNAL_PROTO_FILENAME string
TEMPLATES_CRUD_FILENAME string
TEMPLATES_CRUD_TEST_FILENAME string
TEMPLATES_ALIAS_FILENAME string
TEMPLATES_CRUD_TABLE_UPDATE_FUNC_FILENAME string
TEMPLATES_CRUD_TABLE_UPDATE_FUNC_TEST_FILENAME string
TEMPLATES_CONVERT_ID_FILENAME string
TEMPLATES_CRUD_FUNCTIONS_RENAME_FILENAME string
TEMPLATES_MODEL_CRUD_DELETE_FUNCTIONS_FILENAME string
TEMPLATES_NAME_REPLACE_FILENAME string
TEMPLATES_NULLABLE_FILENAME string
TEMPLATES_CRUD_TABLE_UPDATE_FILENAME string
TEMPLATES_CRUD_TABLE_UPDATE_TEST_FILENAME string
TEMPLATES_GRPC_CLIENT_TABLE_UPDATE_FILENAME string
TEMPLATES_GRPC_CLIENT_TABLE_UPDATE_TEST_FILENAME string
TEMPLATES_GRPC_SERVER_TABLE_UPDATE_FILENAME string
TEMPLATES_GRPC_SERVER_TABLE_UPDATE_TEST_FILENAME string
TEMPLATES_GRPC_CLIENT_TABLES_CACHE_FILENAME string
TEMPLATES_GRPC_CLIENT_TABLES_CACHE_TEST_FILENAME string
TEMPLATE_GRPC_CLIENT_FUNC_FILENAME string
TEMPLATE_GRPC_CLIENT_FUNC_TEST_FILENAME string
TEMPLATES_README_MD_FILENAME string
TEMPLATES_README_RUS_FILENAME string
TEMPLATE_FOLDERNAME_CONSTANTS string
TEMPLATES_CONSTANTS_FILENAME string
NEED_CREATE_MODEL_STRUCT bool
NEED_CREATE_MODEL_CRUD bool
NEED_CREATE_DB bool
NEED_CREATE_DB_TEST bool
NEED_CREATE_DB_TABLES bool
NEED_CREATE_GRPC bool
NEED_CREATE_GRPC_SERVER_TEST bool
NEED_CREATE_GRPC_CLIENT_TEST bool
NEED_CREATE_NRPC bool
NEED_CREATE_NRPC_SERVER_TEST bool
NEED_CREATE_NRPC_CLIENT_TEST bool
NEED_CREATE_MANUAL_FILES bool
NEED_CREATE_CACHE_API bool
NEED_CREATE_CACHE_FILES bool
NEED_CREATE_CACHE_TEST_FILES bool
SERVICE_NAME string
SERVICE_REPOSITORY_URL string
TEXT_TEMPLATE_MODEL string
TEXT_TEMPLATE_TABLENAME string
HAS_IS_DELETED bool
READY_FOLDERNAME string
TEXT_DB_MODIFIED_AT string
TEXT_DB_CREATED_AT string
TEXT_DB_IS_DELETED string
USE_DEFAULT_TEMPLATE bool
PREFIX_SERVER_GRPC string
PREFIX_CLIENT_GRPC string
PREFIX_CRUD string
PREFIX_MODEL string
PREFIX_TABLE string
COMMENT_MODEL_STRUCT string
TEXT_MODULE_GENERATED string
READY_ALIAS_FILENAME string
NEED_CREATE_UPDATE_EVERY_COLUMN bool
CACHE_ELEMENTS_COUNT_MAX int64
SINGULAR_TABLE_NAMES bool
TEMPLATES_FINDBY_FILENAME string
TEMPLATES_FINDMASSBY_FILENAME string
TEMPLATES_CRUD_TABLE_FINDBY_FILENAME string
TEMPLATES_CRUD_TABLE_FINDBY_TEST_FILENAME string
TEMPLATES_CRUD_TABLE_FINDBY_FUNCTION_FILENAME string
TEMPLATES_CRUD_TABLE_FINDBY_FUNCTION_TEST_FILENAME string
TEMPLATES_CRUD_TABLE_FINDMASSBY_FILENAME string
TEMPLATES_CRUD_TABLE_FINDMASSBY_TEST_FILENAME string
TEMPLATES_CRUD_TABLE_FINDMASSBY_FUNCTION_FILENAME string
TEMPLATES_CRUD_TABLE_FINDMASSBY_FUNCTION_TEST_FILENAME string
}
func Getenv(Name string, IsRequired bool) string {
@ -538,6 +548,56 @@ func FillSettings() {
s = Getenv(Name, true)
Settings.TEMPLATE_GRPC_CLIENT_FUNC_TEST_FILENAME = s
//
Name = "TEMPLATES_FINDBY_FILENAME"
s = Getenv(Name, true)
Settings.TEMPLATES_FINDBY_FILENAME = s
//
Name = "TEMPLATES_FINDMASSBY_FILENAME"
s = Getenv(Name, true)
Settings.TEMPLATES_FINDMASSBY_FILENAME = s
//
Name = "TEMPLATES_CRUD_TABLE_FINDBY_FILENAME"
s = Getenv(Name, true)
Settings.TEMPLATES_CRUD_TABLE_FINDBY_FILENAME = s
//
Name = "TEMPLATES_CRUD_TABLE_FINDBY_TEST_FILENAME"
s = Getenv(Name, true)
Settings.TEMPLATES_CRUD_TABLE_FINDBY_TEST_FILENAME = s
//
Name = "TEMPLATES_CRUD_TABLE_FINDBY_FUNCTION_FILENAME"
s = Getenv(Name, true)
Settings.TEMPLATES_CRUD_TABLE_FINDBY_FUNCTION_FILENAME = s
//
Name = "TEMPLATES_CRUD_TABLE_FINDBY_FUNCTION_TEST_FILENAME"
s = Getenv(Name, true)
Settings.TEMPLATES_CRUD_TABLE_FINDBY_FUNCTION_TEST_FILENAME = s
//
Name = "TEMPLATES_CRUD_TABLE_FINDMASSBY_FILENAME"
s = Getenv(Name, true)
Settings.TEMPLATES_CRUD_TABLE_FINDMASSBY_FILENAME = s
//
Name = "TEMPLATES_CRUD_TABLE_FINDMASSBY_TEST_FILENAME"
s = Getenv(Name, true)
Settings.TEMPLATES_CRUD_TABLE_FINDMASSBY_TEST_FILENAME = s
//
Name = "TEMPLATES_CRUD_TABLE_FINDMASSBY_FUNCTION_FILENAME"
s = Getenv(Name, true)
Settings.TEMPLATES_CRUD_TABLE_FINDMASSBY_FUNCTION_FILENAME = s
//
Name = "TEMPLATES_CRUD_TABLE_FINDMASSBY_FUNCTION_TEST_FILENAME"
s = Getenv(Name, true)
Settings.TEMPLATES_CRUD_TABLE_FINDMASSBY_FUNCTION_TEST_FILENAME = s
}
// CurrentDirectory - возвращает текущую директорию ОС

View File

@ -80,6 +80,18 @@ func CreateAllFiles(MapAll map[string]*types.Table) error {
return err
}
}
err = CreateFilesFindBy(Table1)
if err != nil {
log.Error("CreateFilesFindBy() table: ", Table1.Name, " error: ", err)
return err
}
err = CreateFilesFindByTest(Table1)
if err != nil {
log.Error("CreateFilesFindByTest() table: ", Table1.Name, " error: ", err)
return err
}
}
}

View File

@ -0,0 +1,292 @@
package crud_tables
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"
"github.com/ManyakRus/starter/log"
"github.com/ManyakRus/starter/micro"
"os"
"strings"
)
// CreateFilesFindBy - создаёт 1 файл в папке crud
func CreateFilesFindBy(Table1 *types.Table) error {
var err error
if len(types.MapFindBy) == 0 {
return err
}
//чтение файлов
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()
FilenameTemplateCrud := DirTemplatesCrud + config.Settings.TEMPLATES_CRUD_TABLE_FINDBY_FILENAME
TableName := strings.ToLower(Table1.Name)
DirReadyTable := DirReadyCrud + config.Settings.PREFIX_CRUD + TableName
FilenameReady := DirReadyTable + micro.SeparatorFile() + config.Settings.PREFIX_CRUD + TableName + "_findby.go"
//создадим каталог
ok, err := micro.FileExists(DirReadyTable)
if ok == false {
err = os.MkdirAll(DirReadyTable, 0777)
if err != nil {
log.Panic("Mkdir() ", DirReadyTable, " error: ", err)
}
}
//загрузим шаблон файла
bytes, err := os.ReadFile(FilenameTemplateCrud)
if err != nil {
log.Panic("ReadFile() ", FilenameTemplateCrud, " error: ", err)
}
TextCrud := string(bytes)
//загрузим шаблон файла функции
FilenameTemplateCrudFunction := DirTemplatesCrud + config.Settings.TEMPLATES_CRUD_TABLE_FINDBY_FUNCTION_FILENAME
bytes, err = os.ReadFile(FilenameTemplateCrudFunction)
if err != nil {
log.Panic("ReadFile() ", FilenameTemplateCrudFunction, " error: ", err)
}
TextTemplatedFunction := string(bytes)
//заменим имя пакета на новое
TextCrud = create_files.ReplacePackageName(TextCrud, DirReadyTable)
ModelName := Table1.NameGo
//заменим импорты
if config.Settings.USE_DEFAULT_TEMPLATE == true {
TextCrud = create_files.DeleteTemplateRepositoryImports(TextCrud)
ModelTableURL := create_files.FindModelTableURL(TableName)
TextCrud = create_files.AddImport(TextCrud, ModelTableURL)
ConstantsURL := create_files.FindDBConstantsURL()
TextCrud = create_files.AddImport(TextCrud, ConstantsURL)
}
//создание функций
TextCrudFunc := CreateFilesFindByTable(Table1, TextTemplatedFunction)
if TextCrudFunc == "" {
return err
}
TextCrud = TextCrud + TextCrudFunc
//создание текста
TextCrud = strings.ReplaceAll(TextCrud, config.Settings.TEXT_TEMPLATE_MODEL, ModelName)
TextCrud = strings.ReplaceAll(TextCrud, config.Settings.TEXT_TEMPLATE_TABLENAME, Table1.Name)
TextCrud = config.Settings.TEXT_MODULE_GENERATED + TextCrud
//замена импортов на новые URL
TextCrud = create_files.ReplaceServiceURLImports(TextCrud)
//uuid
TextCrud = create_files.CheckAndAddImportUUID_FromText(TextCrud)
//alias
TextCrud = create_files.CheckAndAddImportAlias(TextCrud)
//удаление пустого импорта
TextCrud = create_files.DeleteEmptyImport(TextCrud)
//удаление пустых строк
TextCrud = create_files.DeleteEmptyLines(TextCrud)
//запись файла
err = os.WriteFile(FilenameReady, []byte(TextCrud), constants.FILE_PERMISSIONS)
return err
}
// CreateFilesFindByTable - создаёт текст всех функций
func CreateFilesFindByTable(Table1 *types.Table, TextTemplateFunction string) string {
Otvet := ""
for TableName1, MassColumns1 := range types.MapFindBy {
if TableName1 != Table1.Name {
continue
}
Otvet1 := CreateFilesFindByTable1(Table1, TextTemplateFunction, MassColumns1)
Otvet = Otvet + Otvet1
}
return Otvet
}
// CreateFilesFindByTable1 - создаёт текст всех функций
func CreateFilesFindByTable1(Table1 *types.Table, TextTemplateFunction string, MassColumns1 []string) string {
Otvet := TextTemplateFunction
//
FieldNamesWithUnderline := ""
//
FieldNamesWithComma := ""
//
TextFind := "\t" + `tx := tx.Where("ColumnName = ?", m.FieldName)` + "\n"
TextWhere := ""
Underline := ""
Plus := ""
for _, ColumnName1 := range MassColumns1 {
Column1, ok := Table1.MapColumns[ColumnName1]
if ok == false {
log.Panic(Table1.Name + " .MapColumns[" + ColumnName1 + "] = false")
}
TextWhere = TextWhere + "\t" + `tx := tx.Where("` + ColumnName1 + ` = ?", m.` + Column1.NameGo + `)` + "\n"
FieldNamesWithUnderline = FieldNamesWithUnderline + Underline + Column1.NameGo
FieldNamesWithComma = FieldNamesWithComma + Plus + Column1.NameGo
Underline = "_"
Plus = "+"
}
Otvet = strings.ReplaceAll(Otvet, TextFind, TextWhere)
Otvet = strings.ReplaceAll(Otvet, "FieldNamesWithUnderline", FieldNamesWithUnderline)
Otvet = strings.ReplaceAll(Otvet, "FieldNamesWithPlus", FieldNamesWithComma)
return Otvet
}
// CreateFilesFindByTest - создаёт 1 файл в папке crud
func CreateFilesFindByTest(Table1 *types.Table) error {
var err error
if len(types.MapFindBy) == 0 {
return err
}
//чтение файлов
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()
FilenameTemplateCrud := DirTemplatesCrud + config.Settings.TEMPLATES_CRUD_TABLE_FINDBY_FILENAME
TableName := strings.ToLower(Table1.Name)
DirReadyTable := DirReadyCrud + config.Settings.PREFIX_CRUD + TableName
FilenameReady := DirReadyTable + micro.SeparatorFile() + config.Settings.PREFIX_CRUD + TableName + "_findby_test.go"
//создадим каталог
ok, err := micro.FileExists(DirReadyTable)
if ok == false {
err = os.MkdirAll(DirReadyTable, 0777)
if err != nil {
log.Panic("Mkdir() ", DirReadyTable, " error: ", err)
}
}
//загрузим шаблон файла
bytes, err := os.ReadFile(FilenameTemplateCrud)
if err != nil {
log.Panic("ReadFile() ", FilenameTemplateCrud, " error: ", err)
}
TextCrud := string(bytes)
//загрузим шаблон файла функции
FilenameTemplateCrudFunction := DirTemplatesCrud + config.Settings.TEMPLATES_CRUD_TABLE_FINDBY_FUNCTION_TEST_FILENAME
bytes, err = os.ReadFile(FilenameTemplateCrudFunction)
if err != nil {
log.Panic("ReadFile() ", FilenameTemplateCrudFunction, " error: ", err)
}
TextTemplatedFunction := string(bytes)
//заменим имя пакета на новое
TextCrud = create_files.ReplacePackageName(TextCrud, DirReadyTable)
ModelName := Table1.NameGo
//заменим импорты
if config.Settings.USE_DEFAULT_TEMPLATE == true {
TextCrud = create_files.DeleteTemplateRepositoryImports(TextCrud)
ModelTableURL := create_files.FindModelTableURL(TableName)
TextCrud = create_files.AddImport(TextCrud, ModelTableURL)
}
//создание функций
TextCrudFunc := CreateFilesFindByTestTable(Table1, TextTemplatedFunction)
if TextCrudFunc == "" {
return err
}
TextCrud = TextCrud + TextCrudFunc
//создание текста
TextCrud = strings.ReplaceAll(TextCrud, config.Settings.TEXT_TEMPLATE_MODEL, ModelName)
TextCrud = strings.ReplaceAll(TextCrud, config.Settings.TEXT_TEMPLATE_TABLENAME, Table1.Name)
TextCrud = config.Settings.TEXT_MODULE_GENERATED + TextCrud
//замена импортов на новые URL
TextCrud = create_files.ReplaceServiceURLImports(TextCrud)
//uuid
TextCrud = create_files.CheckAndAddImportUUID_FromText(TextCrud)
//alias
TextCrud = create_files.CheckAndAddImportAlias(TextCrud)
//удаление пустого импорта
TextCrud = create_files.DeleteEmptyImport(TextCrud)
//удаление пустых строк
TextCrud = create_files.DeleteEmptyLines(TextCrud)
//запись файла
err = os.WriteFile(FilenameReady, []byte(TextCrud), constants.FILE_PERMISSIONS)
return err
}
// CreateFilesFindByTestTable - создаёт текст всех функций
func CreateFilesFindByTestTable(Table1 *types.Table, TextTemplateFunction string) string {
Otvet := ""
for TableName1, MassColumns1 := range types.MapFindBy {
if TableName1 != Table1.Name {
continue
}
Otvet1 := CreateFilesFindByTestTable1(Table1, TextTemplateFunction, MassColumns1)
Otvet = Otvet + Otvet1
}
return Otvet
}
// CreateFilesFindByTestTable1 - создаёт текст всех функций
func CreateFilesFindByTestTable1(Table1 *types.Table, TextTemplateFunction string, MassColumns1 []string) string {
Otvet := TextTemplateFunction
//
FieldNamesWithUnderline := ""
//
TextAssignFind := "\t" + `Otvet.FieldName = 0` + "\n"
TextAssign := ""
TextFieldName_TEST := ""
Underline := ""
Comma := ""
for _, ColumnName1 := range MassColumns1 {
Column1, ok := Table1.MapColumns[ColumnName1]
if ok == false {
log.Panic(Table1.Name + " .MapColumns[" + ColumnName1 + "] = false")
}
DefaultValue := create_files.FindTextDefaultValue(Column1.TypeGo)
TextAssign = TextAssign + "\t" + `Otvet.` + Column1.NameGo + ` = ` + DefaultValue + "\n"
FieldNamesWithUnderline = FieldNamesWithUnderline + Underline + Column1.NameGo
TextFieldName_TEST = TextFieldName_TEST + Comma + DefaultValue
Underline = "_"
Comma = ", "
}
Otvet = strings.ReplaceAll(Otvet, TextAssignFind, TextAssign)
Otvet = strings.ReplaceAll(Otvet, "FieldNamesWithUnderline", FieldNamesWithUnderline)
Otvet = strings.ReplaceAll(Otvet, "FieldNames_TEST", TextFieldName_TEST)
return Otvet
}

View File

@ -0,0 +1 @@
package crud_tables

View File

@ -23,6 +23,8 @@ func LoadConfigsAll() {
LoadConvertID()
LoadMapModelCrudDeleteFunctions()
LoadCrudFunctionsRename()
LoadFindBy()
LoadFindMassBy()
}
// LoadMappings - загружает маппинг ТипБД = ТипGolang, из файла .json
@ -203,3 +205,47 @@ func LoadCrudFunctionsRename() {
}
}
// LoadFindBy - загружает из файла .json список функций FindBy которые надо создать
func LoadFindBy() {
dir := micro.ProgramDir_bin()
FileName := dir + config.Settings.TEMPLATE_FOLDERNAME + micro.SeparatorFile() + constants.CONFIG_FOLDER_NAME + micro.SeparatorFile() + config.Settings.TEMPLATES_FINDBY_FILENAME
var err error
//чтение файла
bytes, err := os.ReadFile(FileName)
if err != nil {
TextError := fmt.Sprint("ReadFile() error: ", err)
log.Panic(TextError)
}
//json в map
err = json.Unmarshal(bytes, &types.MapFindBy)
if err != nil {
log.Panic("Unmarshal() error: ", err)
}
}
// LoadFindMassBy - загружает из файла .json список функций FindMassBy которые надо создать
func LoadFindMassBy() {
dir := micro.ProgramDir_bin()
FileName := dir + config.Settings.TEMPLATE_FOLDERNAME + micro.SeparatorFile() + constants.CONFIG_FOLDER_NAME + micro.SeparatorFile() + config.Settings.TEMPLATES_FINDMASSBY_FILENAME
var err error
//чтение файла
bytes, err := os.ReadFile(FileName)
if err != nil {
TextError := fmt.Sprint("ReadFile() error: ", err)
log.Panic(TextError)
}
//json в map
err = json.Unmarshal(bytes, &types.MapFindMassBy)
if err != nil {
log.Panic("Unmarshal() error: ", err)
}
}

View File

@ -51,3 +51,9 @@ var MapModelCrudDeleteFunctions = make(map[string]string, 0)
// MapRenameFunctions - карта функций которые будут переименованый в файлах crud
var MapRenameFunctions = make(map[string][]ReplaceStruct, 0)
// MapFindBy - карта функций которые будут созданы для поиска 1 строки в таблице
var MapFindBy = make(map[string][]string, 0)
// MapFindMassBy - карта функций которые будут созданы для поиска много строк в таблице
var MapFindMassBy = make(map[string][]string, 0)