From 830ea6bba526338ca8e4aeec7d6f375285cb55b6 Mon Sep 17 00:00:00 2001 From: Nikitin Aleksandr Date: Fri, 22 Mar 2024 15:08:12 +0300 Subject: [PATCH] =?UTF-8?q?=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20Read=5Fc?= =?UTF-8?q?tx()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/grpc/server_grpc/server_grpc.go_ | 15 +++--- .../pkg/db/crud/crud_table_cache.go_ | 2 +- .../pkg/db/crud/crud_table_update_func.go_ | 4 +- bin/templates/pkg/db/crud/crud_tables.go_ | 42 ++++++++--------- .../pkg/db/crud/crud_tables_rapira.go_ | 46 +++++++++---------- .../grpc_server_tables/grpc_server_tables.go | 3 ++ 6 files changed, 58 insertions(+), 54 deletions(-) diff --git a/bin/templates/internal/app/grpc/server_grpc/server_grpc.go_ b/bin/templates/internal/app/grpc/server_grpc/server_grpc.go_ index 3371481..7b5ba53 100644 --- a/bin/templates/internal/app/grpc/server_grpc/server_grpc.go_ +++ b/bin/templates/internal/app/grpc/server_grpc/server_grpc.go_ @@ -5,6 +5,7 @@ import ( "github.com/ManyakRus/starter/micro" "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/network/grpc/grpc_proto" "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/object_model/entities/lawsuit_status_types" + "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/db/crud/crud_lawsuit_status_types" ) // LawsuitStatusType_Read - читает и возвращает модель из БД @@ -29,7 +30,7 @@ func (s *ServerGRPC) LawsuitStatusType_Read(ctx context.Context, Request *grpc_p //запрос в БД Model := &lawsuit_status_types.LawsuitStatusType{} Model.ID = Request.ID - err = Model.Read() + err = crud_lawsuit_status_types.Read_ctx(ctx, Model) if err != nil { return &Otvet, err } @@ -66,7 +67,7 @@ func (s *ServerGRPC) LawsuitStatusType_Delete(ctx context.Context, Request *grpc //запрос в БД Model := &lawsuit_status_types.LawsuitStatusType{} Model.ID = Request.ID - err = Model.Delete() + err = crud_lawsuit_status_types.Delete_ctx(ctx, Model) if err != nil { return &Otvet, err } @@ -103,7 +104,7 @@ func (s *ServerGRPC) LawsuitStatusType_Restore(ctx context.Context, Request *grp //запрос в БД Model := &lawsuit_status_types.LawsuitStatusType{} Model.ID = Request.ID - err = Model.Restore() + err = crud_lawsuit_status_types.Restore_ctx(ctx, Model) if err != nil { return &Otvet, err } @@ -145,7 +146,7 @@ func (s *ServerGRPC) LawsuitStatusType_Create(ctx context.Context, Request *grpc } //запрос в БД - err = Model.Create() + err = crud_lawsuit_status_types.Create_ctx(ctx, Model) if err != nil { return &Otvet, err } @@ -187,7 +188,7 @@ func (s *ServerGRPC) LawsuitStatusType_Update(ctx context.Context, Request *grpc } //запрос в БД - err = Model.Update() + err = crud_lawsuit_status_types.Update_ctx(ctx, Model) if err != nil { return &Otvet, err } @@ -229,7 +230,7 @@ func (s *ServerGRPC) LawsuitStatusType_Save(ctx context.Context, Request *grpc_p } //запрос в БД - err = Model.Save() + err = crud_lawsuit_status_types.Save_ctx(ctx, &Model) if err != nil { return &Otvet, err } @@ -267,7 +268,7 @@ func (s *ServerGRPC) LawsuitStatusType_FindByExtID(ctx context.Context, Request Model := &lawsuit_status_types.LawsuitStatusType{} Model.ExtID = Request.ExtID Model.ConnectionID = Request.ConnectionId - err = Model.Find_ByExtID() + err = crud_lawsuit_status_types.Find_ByExtID_ctx(ctx, Model) if err != nil { return &Otvet, err } diff --git a/bin/templates/pkg/db/crud/crud_table_cache.go_ b/bin/templates/pkg/db/crud/crud_table_cache.go_ index ee5b57d..d57308f 100644 --- a/bin/templates/pkg/db/crud/crud_table_cache.go_ +++ b/bin/templates/pkg/db/crud/crud_table_cache.go_ @@ -40,7 +40,7 @@ func (crud Crud_DB) ReadFromCache(ID int64) (lawsuit_status_types.LawsuitStatusT defer ctxCancelFunc() Otvet.ID = ID - err = crud.Read_ctx(ctx, &Otvet) + err = Read_ctx(ctx, &Otvet) if err == nil { cache.Add(ID, Otvet) } diff --git a/bin/templates/pkg/db/crud/crud_table_update_func.go_ b/bin/templates/pkg/db/crud/crud_table_update_func.go_ index 7f98116..4f3f222 100644 --- a/bin/templates/pkg/db/crud/crud_table_update_func.go_ +++ b/bin/templates/pkg/db/crud/crud_table_update_func.go_ @@ -6,12 +6,12 @@ func (crud Crud_DB) Read(m *lawsuit_status_types.LawsuitStatusType) error { ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS)) defer ctxCancelFunc() - err = crud.Read_ctx(ctx, m) + err = Read_ctx(ctx, m) return err } // Read_ctx - изменяет 1 поле ColumnName в базе данных -func (crud Crud_DB) Read_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { +func Read_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { var err error if micro.ContextDone(ctx) == true { diff --git a/bin/templates/pkg/db/crud/crud_tables.go_ b/bin/templates/pkg/db/crud/crud_tables.go_ index 0f3cb9a..e6ef23c 100644 --- a/bin/templates/pkg/db/crud/crud_tables.go_ +++ b/bin/templates/pkg/db/crud/crud_tables.go_ @@ -28,12 +28,12 @@ func (crud Crud_DB) Read(m *lawsuit_status_types.LawsuitStatusType) error { ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS)) defer ctxCancelFunc() - err = crud.Read_ctx(ctx, m) + err = Read_ctx(ctx, m) return err } // Read_ctx - находит запись в БД по ID -func (crud Crud_DB) Read_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { +func Read_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { var err error if micro.ContextDone(ctx) == true { @@ -61,12 +61,12 @@ func (crud Crud_DB) Save(m *lawsuit_status_types.LawsuitStatusType) error { ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS)) defer ctxCancelFunc() - err := crud.Save_ctx(ctx, m) + err := Save_ctx(ctx, m) return err } // Save_ctx - записывает новый или существующий объект в базу данных -func (crud Crud_DB) Save_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { +func Save_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { var err error if micro.ContextDone(ctx) == true { err = context.Canceled @@ -74,7 +74,7 @@ func (crud Crud_DB) Save_ctx(ctx context.Context, m *lawsuit_status_types.Lawsui } is_create := !micro.BoolFromInt64(int64(m.ID)) - err = crud.create_update_ctx(ctx, m, is_create) + err = create_update_ctx(ctx, m, is_create) return err } @@ -84,19 +84,19 @@ func (crud Crud_DB) Update(m *lawsuit_status_types.LawsuitStatusType) error { ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS)) defer ctxCancelFunc() - err := crud.Update_ctx(ctx, m) + err := Update_ctx(ctx, m) return err } // Update_ctx - записывает существующий объект в базу данных -func (crud Crud_DB) Update_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { +func Update_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { var err error if micro.ContextDone(ctx) == true { err = context.Canceled return err } - err = crud.create_update_ctx(ctx, m, false) + err = create_update_ctx(ctx, m, false) return err } @@ -106,19 +106,19 @@ func (crud Crud_DB) Create(m *lawsuit_status_types.LawsuitStatusType) error { ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS)) defer ctxCancelFunc() - err := crud.Create_ctx(ctx, m) + err := Create_ctx(ctx, m) return err } // Create_ctx - записывает новый объект в базу данных -func (crud Crud_DB) Create_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { +func Create_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { var err error if micro.ContextDone(ctx) == true { err = context.Canceled return err } - err = crud.create_update_ctx(ctx, m, true) + err = create_update_ctx(ctx, m, true) return err } @@ -130,12 +130,12 @@ func (crud Crud_DB) create_update(m *lawsuit_status_types.LawsuitStatusType, is_ ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS)) defer ctxCancelFunc() - err = crud.create_update_ctx(ctx, m, is_create) + err = create_update_ctx(ctx, m, is_create) return err } // create_update_ctx - записывает объект в базу данных -func (crud Crud_DB) create_update_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType, is_create bool) error { +func create_update_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType, is_create bool) error { var err error // log.Trace("start Save() ", TableName, " id: ", int64(m.ID)) @@ -208,12 +208,12 @@ func (crud Crud_DB) Restore(m *lawsuit_status_types.LawsuitStatusType) error { ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS)) defer ctxCancelFunc() - err = crud.Restore_ctx(ctx, m) + err = Restore_ctx(ctx, m) return err } // Restore_ctx - записывает is_deleted = true -func (crud Crud_DB) Restore_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { +func Restore_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { var err error if micro.ContextDone(ctx) == true { @@ -223,7 +223,7 @@ func (crud Crud_DB) Restore_ctx(ctx context.Context, m *lawsuit_status_types.Law m2 := lawsuit_status_types.LawsuitStatusType{} m2.ID = m.ID - err = crud.Read_ctx(ctx, &m2) + err = Read_ctx(ctx, &m2) if err != nil { return err } @@ -231,7 +231,7 @@ func (crud Crud_DB) Restore_ctx(ctx context.Context, m *lawsuit_status_types.Law m2.IsDeleted = false m.IsDeleted = false - err = crud.Save_ctx(ctx, &m2) + err = Save_ctx(ctx, &m2) if err != nil { err = fmt.Errorf(m.TableNameDB()+" Restore() id: %v, error: %v", m.ID, err) } @@ -253,13 +253,13 @@ func (crud Crud_DB) Find_ByExtID(m *lawsuit_status_types.LawsuitStatusType) erro ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS)) defer ctxCancelFunc() - err = crud.Find_ByExtID_ctx(ctx, m) + err = Find_ByExtID_ctx(ctx, m) return err } // Find_ByExtID_ctx - находит запись в БД по ext_id и connection_id -func (crud Crud_DB) Find_ByExtID_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { +func Find_ByExtID_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { var err error if micro.ContextDone(ctx) == true { @@ -294,12 +294,12 @@ func (crud Crud_DB) Delete(m *lawsuit_status_types.LawsuitStatusType) error { ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS)) defer ctxCancelFunc() - err = crud.Delete_ctx(ctx, m) + err = Delete_ctx(ctx, m) return err } // Delete_ctx - удаляет запись в БД -func (crud Crud_DB) Delete_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { +func Delete_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { var err error if micro.ContextDone(ctx) == true { diff --git a/bin/templates/pkg/db/crud/crud_tables_rapira.go_ b/bin/templates/pkg/db/crud/crud_tables_rapira.go_ index 7be8c4c..fcc0afd 100644 --- a/bin/templates/pkg/db/crud/crud_tables_rapira.go_ +++ b/bin/templates/pkg/db/crud/crud_tables_rapira.go_ @@ -28,12 +28,12 @@ func (crud Crud_DB) Read(m *lawsuit_status_types.LawsuitStatusType) error { ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS)) defer ctxCancelFunc() - err = crud.Read_ctx(ctx, m) + err = Read_ctx(ctx, m) return err } // Read_ctx - находит запись в БД по ID -func (crud Crud_DB) Read_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { +func Read_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { var err error if micro.ContextDone(ctx) == true { @@ -61,12 +61,12 @@ func (crud Crud_DB) Save(m *lawsuit_status_types.LawsuitStatusType) error { ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS)) defer ctxCancelFunc() - err := crud.Save_ctx(ctx, m) + err := Save_ctx(ctx, m) return err } // Save_ctx - записывает новый или существующий объект в базу данных -func (crud Crud_DB) Save_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { +func Save_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { var err error if micro.ContextDone(ctx) == true { err = context.Canceled @@ -74,7 +74,7 @@ func (crud Crud_DB) Save_ctx(ctx context.Context, m *lawsuit_status_types.Lawsui } is_create := !micro.BoolFromInt64(int64(m.ID)) - err = crud.create_update_ctx(ctx, m, is_create) + err = create_update_ctx(ctx, m, is_create) return err } @@ -84,19 +84,19 @@ func (crud Crud_DB) Update(m *lawsuit_status_types.LawsuitStatusType) error { ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS)) defer ctxCancelFunc() - err := crud.Update_ctx(ctx, m) + err := Update_ctx(ctx, m) return err } // Update_ctx - записывает существующий объект в базу данных -func (crud Crud_DB) Update_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { +func Update_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { var err error if micro.ContextDone(ctx) == true { err = context.Canceled return err } - err = crud.create_update_ctx(ctx, m, false) + err = create_update_ctx(ctx, m, false) return err } @@ -106,19 +106,19 @@ func (crud Crud_DB) Create(m *lawsuit_status_types.LawsuitStatusType) error { ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS)) defer ctxCancelFunc() - err := crud.Create_ctx(ctx, m) + err := Create_ctx(ctx, m) return err } // Create_ctx - записывает новый объект в базу данных -func (crud Crud_DB) Create_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { +func Create_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { var err error if micro.ContextDone(ctx) == true { err = context.Canceled return err } - err = crud.create_update_ctx(ctx, m, true) + err = create_update_ctx(ctx, m, true) return err } @@ -130,12 +130,12 @@ func (crud Crud_DB) create_update(m *lawsuit_status_types.LawsuitStatusType, is_ ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS)) defer ctxCancelFunc() - err = crud.create_update_ctx(ctx, m, is_create) + err = create_update_ctx(ctx, m, is_create) return err } // create_update_ctx - записывает объект в базу данных -func (crud Crud_DB) create_update_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType, is_create bool) error { +func create_update_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType, is_create bool) error { var err error // log.Trace("start Save() ", TableName, " id: ", int64(m.ID)) @@ -208,12 +208,12 @@ func (crud Crud_DB) Delete(m *lawsuit_status_types.LawsuitStatusType) error { ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS)) defer ctxCancelFunc() - err = crud.Delete_ctx(ctx, m) + err = Delete_ctx(ctx, m) return err } // Delete_ctx - записывает is_deleted = true -func (crud Crud_DB) Delete_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { +func Delete_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { var err error if micro.ContextDone(ctx) == true { @@ -223,7 +223,7 @@ func (crud Crud_DB) Delete_ctx(ctx context.Context, m *lawsuit_status_types.Laws m2 := lawsuit_status_types.LawsuitStatusType{} m2.ID = m.ID - err = crud.Read_ctx(ctx, &m2) + err = Read_ctx(ctx, &m2) if err != nil { return err } @@ -231,7 +231,7 @@ func (crud Crud_DB) Delete_ctx(ctx context.Context, m *lawsuit_status_types.Laws m2.IsDeleted = true m.IsDeleted = true - err = crud.Save_ctx(ctx, &m2) + err = Save_ctx(ctx, &m2) if err != nil { err = fmt.Errorf(m.TableNameDB()+" Delete() id: %v, error: %v", m.ID, err) } @@ -247,12 +247,12 @@ func (crud Crud_DB) Restore(m *lawsuit_status_types.LawsuitStatusType) error { ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS)) defer ctxCancelFunc() - err = crud.Restore_ctx(ctx, m) + err = Restore_ctx(ctx, m) return err } // Restore_ctx - записывает is_deleted = true -func (crud Crud_DB) Restore_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { +func Restore_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { var err error if micro.ContextDone(ctx) == true { @@ -262,7 +262,7 @@ func (crud Crud_DB) Restore_ctx(ctx context.Context, m *lawsuit_status_types.Law m2 := lawsuit_status_types.LawsuitStatusType{} m2.ID = m.ID - err = crud.Read_ctx(ctx, &m2) + err = Read_ctx(ctx, &m2) if err != nil { return err } @@ -270,7 +270,7 @@ func (crud Crud_DB) Restore_ctx(ctx context.Context, m *lawsuit_status_types.Law m2.IsDeleted = false m.IsDeleted = false - err = crud.Save_ctx(ctx, &m2) + err = Save_ctx(ctx, &m2) if err != nil { err = fmt.Errorf(m.TableNameDB()+" Restore() id: %v, error: %v", m.ID, err) } @@ -292,13 +292,13 @@ func (crud Crud_DB) Find_ByExtID(m *lawsuit_status_types.LawsuitStatusType) erro ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS)) defer ctxCancelFunc() - err = crud.Find_ByExtID_ctx(ctx, m) + err = Find_ByExtID_ctx(ctx, m) return err } // Find_ByExtID_ctx - находит запись в БД по ext_id и connection_id -func (crud Crud_DB) Find_ByExtID_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { +func Find_ByExtID_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { var err error if micro.ContextDone(ctx) == true { diff --git a/internal/create_files/grpc_server_tables/grpc_server_tables.go b/internal/create_files/grpc_server_tables/grpc_server_tables.go index 33f3329..d075d02 100644 --- a/internal/create_files/grpc_server_tables/grpc_server_tables.go +++ b/internal/create_files/grpc_server_tables/grpc_server_tables.go @@ -124,6 +124,9 @@ func CreateFiles(Table1 *types.Table) error { ProtoURL := create_files.FindProtoURL() TextGRPCServer = create_files.AddImport(TextGRPCServer, ProtoURL) + CrudTableURL := create_files.FindCrudTableURL(TableName) + TextGRPCServer = create_files.AddImport(TextGRPCServer, CrudTableURL) + //удалим лишние функции TextGRPCServer = DeleteFuncDelete(TextGRPCServer, Table1) TextGRPCServer = DeleteFuncRestore(TextGRPCServer, Table1)