1
0
mirror of https://github.com/ManyakRus/crud_generator.git synced 2025-01-03 01:22:21 +02:00

сделал Read_ctx(ctx context.Context, db *gorm.DB, m *calendars.Calendar)

This commit is contained in:
Nikitin Aleksandr 2024-03-26 15:56:13 +03:00
parent 115c3fa3aa
commit 75692e5ca5
10 changed files with 147 additions and 69 deletions

View File

@ -3,6 +3,7 @@ package server_grpc
import ( import (
"context" "context"
"github.com/ManyakRus/starter/micro" "github.com/ManyakRus/starter/micro"
"github.com/ManyakRus/starter/postgres_gorm"
"gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/network/grpc/grpc_proto" "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/object_model/entities/lawsuit_status_types"
"gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/db/crud/crud_lawsuit_status_types" "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/db/crud/crud_lawsuit_status_types"
@ -28,9 +29,10 @@ func (s *ServerGRPC) LawsuitStatusType_Read(ctx context.Context, Request *grpc_p
} }
//запрос в БД //запрос в БД
db := postgres_gorm.GetConnection()
Model := &lawsuit_status_types.LawsuitStatusType{} Model := &lawsuit_status_types.LawsuitStatusType{}
Model.ID = Request.ID Model.ID = Request.ID
err = crud_lawsuit_status_types.Read_ctx(ctx, Model) err = crud_lawsuit_status_types.Read_ctx(ctx, db, Model)
if err != nil { if err != nil {
return &Otvet, err return &Otvet, err
} }
@ -65,9 +67,10 @@ func (s *ServerGRPC) LawsuitStatusType_Delete(ctx context.Context, Request *grpc
} }
//запрос в БД //запрос в БД
db := postgres_gorm.GetConnection()
Model := &lawsuit_status_types.LawsuitStatusType{} Model := &lawsuit_status_types.LawsuitStatusType{}
Model.ID = Request.ID Model.ID = Request.ID
err = crud_lawsuit_status_types.Delete_ctx(ctx, Model) err = crud_lawsuit_status_types.Delete_ctx(ctx, db, Model)
if err != nil { if err != nil {
return &Otvet, err return &Otvet, err
} }
@ -102,9 +105,10 @@ func (s *ServerGRPC) LawsuitStatusType_Restore(ctx context.Context, Request *grp
} }
//запрос в БД //запрос в БД
db := postgres_gorm.GetConnection()
Model := &lawsuit_status_types.LawsuitStatusType{} Model := &lawsuit_status_types.LawsuitStatusType{}
Model.ID = Request.ID Model.ID = Request.ID
err = crud_lawsuit_status_types.Restore_ctx(ctx, Model) err = crud_lawsuit_status_types.Restore_ctx(ctx, db, Model)
if err != nil { if err != nil {
return &Otvet, err return &Otvet, err
} }
@ -146,7 +150,8 @@ func (s *ServerGRPC) LawsuitStatusType_Create(ctx context.Context, Request *grpc
} }
//запрос в БД //запрос в БД
err = crud_lawsuit_status_types.Create_ctx(ctx, Model) db := postgres_gorm.GetConnection()
err = crud_lawsuit_status_types.Create_ctx(ctx, db, Model)
if err != nil { if err != nil {
return &Otvet, err return &Otvet, err
} }
@ -188,7 +193,8 @@ func (s *ServerGRPC) LawsuitStatusType_Update(ctx context.Context, Request *grpc
} }
//запрос в БД //запрос в БД
err = crud_lawsuit_status_types.Update_ctx(ctx, Model) db := postgres_gorm.GetConnection()
err = crud_lawsuit_status_types.Update_ctx(ctx, db, Model)
if err != nil { if err != nil {
return &Otvet, err return &Otvet, err
} }
@ -230,7 +236,8 @@ func (s *ServerGRPC) LawsuitStatusType_Save(ctx context.Context, Request *grpc_p
} }
//запрос в БД //запрос в БД
err = crud_lawsuit_status_types.Save_ctx(ctx, &Model) db := postgres_gorm.GetConnection()
err = crud_lawsuit_status_types.Save_ctx(ctx, db, &Model)
if err != nil { if err != nil {
return &Otvet, err return &Otvet, err
} }
@ -265,10 +272,11 @@ func (s *ServerGRPC) LawsuitStatusType_FindByExtID(ctx context.Context, Request
} }
//запрос в БД //запрос в БД
db := postgres_gorm.GetConnection()
Model := &lawsuit_status_types.LawsuitStatusType{} Model := &lawsuit_status_types.LawsuitStatusType{}
Model.ExtID = Request.ExtID Model.ExtID = Request.ExtID
Model.ConnectionID = Request.ConnectionId Model.ConnectionID = Request.ConnectionId
err = crud_lawsuit_status_types.Find_ByExtID_ctx(ctx, Model) err = crud_lawsuit_status_types.Find_ByExtID_ctx(ctx, db, Model)
if err != nil { if err != nil {
return &Otvet, err return &Otvet, err
} }

View File

@ -2,7 +2,10 @@ package server_grpc
import ( import (
"context" "context"
"time"
"github.com/ManyakRus/starter/micro" "github.com/ManyakRus/starter/micro"
"github.com/ManyakRus/starter/contextmain"
"github.com/ManyakRus/starter/postgres_gorm"
"gitlab.aescorp.ru/dsp_dev/claim/sync_service/api/grpc_proto" "gitlab.aescorp.ru/dsp_dev/claim/sync_service/api/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/object_model/entities/lawsuit_status_types"
) )
@ -26,9 +29,14 @@ func (s *ServerGRPC) LawsuitStatusType_ReadFromCache(ctx context.Context, Reques
return &Otvet, err return &Otvet, err
} }
ctxMain := contextmain.GetContext()
ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS))
defer ctxCancelFunc()
//запрос в БД //запрос в БД
db := postgres_gorm.GetConnection()
Model := lawsuit_status_types.LawsuitStatusType{} Model := lawsuit_status_types.LawsuitStatusType{}
Model, err = Model.ReadFromCache(Request.ID) Model, err = crud_lawsuit_status_types.ReadFromCache_ctx(ctx, db, Request.ID)
if err != nil { if err != nil {
return &Otvet, err return &Otvet, err
} }

View File

@ -2,7 +2,10 @@ package server_grpc
import ( import (
"context" "context"
"time"
"github.com/ManyakRus/starter/micro" "github.com/ManyakRus/starter/micro"
"github.com/ManyakRus/starter/contextmain"
"gitlab.aescorp.ru/dsp_dev/claim/sync_service/api/grpc_proto" "gitlab.aescorp.ru/dsp_dev/claim/sync_service/api/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/object_model/entities/lawsuit_status_types"
"github.com/ManyakRus/starter/postgres_gorm"
) )

View File

@ -17,11 +17,16 @@ func (s *ServerGRPC) LawsuitStatusType_Update(ctx context.Context, Request *grpc
return &Otvet, err return &Otvet, err
} }
ctxMain := contextmain.GetContext()
ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS))
defer ctxCancelFunc()
//запрос в БД //запрос в БД
db := postgres_gorm.GetConnection()
Model := &lawsuit_status_types.LawsuitStatusType{} Model := &lawsuit_status_types.LawsuitStatusType{}
Model.ID = Request.ID Model.ID = Request.ID
Model.ColumnName = Request.FieldName Model.ColumnName = Request.FieldName
err = Model.Update() err = crud_lawsuit_status_types.Update_ColumnName_ctx(ctx, db, Model)
if err != nil { if err != nil {
return &Otvet, err return &Otvet, err
} }

View File

@ -27,8 +27,8 @@ func Test_server_LawsuitStatusType_Update(t *testing.T) {
Request2.ID = LawsuitStatusType_ID_Test Request2.ID = LawsuitStatusType_ID_Test
Request2.ColumnName = Model.ColumnName Request2.ColumnName = Model.ColumnName
Request2.VersionModel = lawsuit_status_types.LawsuitStatusType{}.GetStructVersion() Request2.VersionModel = lawsuit_status_types.LawsuitStatusType{}.GetStructVersion()
_, err = server1.LawsuitStatusType_Update(ctx, &Request2) _, err = server1.LawsuitStatusType_Update_ColumnName(ctx, &Request2)
if err != nil { if err != nil {
t.Error("Test_server_LawsuitStatusType_Update() Update() error: ", err) t.Error("Test_server_LawsuitStatusType_Update() Update_ColumnName() error: ", err)
} }
} }

View File

@ -2,11 +2,13 @@ package crud_lawsuit_status_types
import ( import (
"context" "context"
"time"
"github.com/ManyakRus/starter/contextmain" "github.com/ManyakRus/starter/contextmain"
"github.com/hashicorp/golang-lru/v2/expirable" "github.com/hashicorp/golang-lru/v2/expirable"
"gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/db/constants" "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/db/constants"
"gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/object_model/entities/lawsuit_status_types" "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/object_model/entities/lawsuit_status_types"
"time" "github.com/ManyakRus/starter/postgres_gorm"
"gorm.io/gorm"
) )
// cache - кэш с данными // cache - кэш с данными
@ -39,11 +41,26 @@ func (crud Crud_DB) ReadFromCache(ID int64) (lawsuit_status_types.LawsuitStatusT
ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS)) ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS))
defer ctxCancelFunc() defer ctxCancelFunc()
db := postgres_gorm.GetConnection()
Otvet, err = ReadFromCache_ctx(ctx, db, ID)
return Otvet, err
}
// ReadFromCache_ctx - находит запись в кеше или в БД по ID
func ReadFromCache_ctx(ctx context.Context, db *gorm.DB, ID int64) (lawsuit_status_types.LawsuitStatusType, error) {
var Otvet lawsuit_status_types.LawsuitStatusType
var err error
Otvet.ID = ID Otvet.ID = ID
err = Read_ctx(ctx, &Otvet) err = Read_ctx(ctx, db, &Otvet)
if err == nil { if err == nil {
cache.Add(ID, Otvet) cache.Add(ID, Otvet)
} }
return Otvet, err return Otvet, err
} }

View File

@ -6,12 +6,14 @@ func (crud Crud_DB) Read(m *lawsuit_status_types.LawsuitStatusType) error {
ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS)) ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS))
defer ctxCancelFunc() defer ctxCancelFunc()
err = Read_ctx(ctx, m) db := postgres_gorm.GetConnection()
err = Read_ctx(ctx, db, m)
return err return err
} }
// Read_ctx - изменяет 1 поле ColumnName в базе данных // Read_ctx - изменяет 1 поле ColumnName в базе данных
func Read_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { func Read_ctx(ctx context.Context, db *gorm.DB, m *lawsuit_status_types.LawsuitStatusType) error {
var err error var err error
if micro.ContextDone(ctx) == true { if micro.ContextDone(ctx) == true {
@ -26,7 +28,6 @@ func Read_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) er
} }
// //
db := postgres_gorm.GetConnection()
db.WithContext(ctx) db.WithContext(ctx)
// //

View File

@ -11,6 +11,7 @@ import (
"time" "time"
"github.com/ManyakRus/starter/contextmain" "github.com/ManyakRus/starter/contextmain"
"gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/db/crud_functions" "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/db/crud_functions"
"gorm.io/gorm"
) )
// TableName - имя таблицы в БД Postgres // TableName - имя таблицы в БД Postgres
@ -28,12 +29,14 @@ func (crud Crud_DB) Read(m *lawsuit_status_types.LawsuitStatusType) error {
ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS)) ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS))
defer ctxCancelFunc() defer ctxCancelFunc()
err = Read_ctx(ctx, m) db := postgres_gorm.GetConnection()
err = Read_ctx(ctx, db, m)
return err return err
} }
// Read_ctx - находит запись в БД по ID // Read_ctx - находит запись в БД по ID
func Read_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { func Read_ctx(ctx context.Context, db *gorm.DB, m *lawsuit_status_types.LawsuitStatusType) error {
var err error var err error
if micro.ContextDone(ctx) == true { if micro.ContextDone(ctx) == true {
@ -43,7 +46,6 @@ func Read_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) er
id := int64(m.ID) id := int64(m.ID)
db := postgres_gorm.GetConnection()
db.WithContext(ctx) db.WithContext(ctx)
tx := db.First(m, id) tx := db.First(m, id)
@ -61,12 +63,14 @@ func (crud Crud_DB) Save(m *lawsuit_status_types.LawsuitStatusType) error {
ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS)) ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS))
defer ctxCancelFunc() defer ctxCancelFunc()
err := Save_ctx(ctx, m) db := postgres_gorm.GetConnection()
err := Save_ctx(ctx, db, m)
return err return err
} }
// Save_ctx - записывает новый или существующий объект в базу данных // Save_ctx - записывает новый или существующий объект в базу данных
func Save_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { func Save_ctx(ctx context.Context, db *gorm.DB, m *lawsuit_status_types.LawsuitStatusType) error {
var err error var err error
if micro.ContextDone(ctx) == true { if micro.ContextDone(ctx) == true {
err = context.Canceled err = context.Canceled
@ -74,7 +78,7 @@ func Save_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) er
} }
is_create := !micro.BoolFromInt64(int64(m.ID)) is_create := !micro.BoolFromInt64(int64(m.ID))
err = create_update_ctx(ctx, m, is_create) err = create_update_ctx(ctx, db, m, is_create)
return err return err
} }
@ -84,19 +88,21 @@ func (crud Crud_DB) Update(m *lawsuit_status_types.LawsuitStatusType) error {
ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS)) ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS))
defer ctxCancelFunc() defer ctxCancelFunc()
err := Update_ctx(ctx, m) db := postgres_gorm.GetConnection()
err := Update_ctx(ctx, db, m)
return err return err
} }
// Update_ctx - записывает существующий объект в базу данных // Update_ctx - записывает существующий объект в базу данных
func Update_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { func Update_ctx(ctx context.Context, db *gorm.DB, m *lawsuit_status_types.LawsuitStatusType) error {
var err error var err error
if micro.ContextDone(ctx) == true { if micro.ContextDone(ctx) == true {
err = context.Canceled err = context.Canceled
return err return err
} }
err = create_update_ctx(ctx, m, false) err = create_update_ctx(ctx, db, m, false)
return err return err
} }
@ -106,19 +112,21 @@ func (crud Crud_DB) Create(m *lawsuit_status_types.LawsuitStatusType) error {
ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS)) ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS))
defer ctxCancelFunc() defer ctxCancelFunc()
err := Create_ctx(ctx, m) db := postgres_gorm.GetConnection()
err := Create_ctx(ctx, db, m)
return err return err
} }
// Create_ctx - записывает новый объект в базу данных // Create_ctx - записывает новый объект в базу данных
func Create_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { func Create_ctx(ctx context.Context, db *gorm.DB, m *lawsuit_status_types.LawsuitStatusType) error {
var err error var err error
if micro.ContextDone(ctx) == true { if micro.ContextDone(ctx) == true {
err = context.Canceled err = context.Canceled
return err return err
} }
err = create_update_ctx(ctx, m, true) err = create_update_ctx(ctx, db, m, true)
return err return err
} }
@ -130,12 +138,14 @@ 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)) ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS))
defer ctxCancelFunc() defer ctxCancelFunc()
err = create_update_ctx(ctx, m, is_create) db := postgres_gorm.GetConnection()
err = create_update_ctx(ctx, db, m, is_create)
return err return err
} }
// create_update_ctx - записывает объект в базу данных // create_update_ctx - записывает объект в базу данных
func create_update_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType, is_create bool) error { func create_update_ctx(ctx context.Context, db *gorm.DB, m *lawsuit_status_types.LawsuitStatusType, is_create bool) error {
var err error var err error
// log.Trace("start Save() ", TableName, " id: ", int64(m.ID)) // log.Trace("start Save() ", TableName, " id: ", int64(m.ID))
@ -159,7 +169,6 @@ func create_update_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatu
} }
// //
db := postgres_gorm.GetConnection()
db.WithContext(ctx) db.WithContext(ctx)
// заполним даты // заполним даты
@ -208,12 +217,14 @@ func (crud Crud_DB) Restore(m *lawsuit_status_types.LawsuitStatusType) error {
ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS)) ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS))
defer ctxCancelFunc() defer ctxCancelFunc()
err = Restore_ctx(ctx, m) db := postgres_gorm.GetConnection()
err = Restore_ctx(ctx, db, m)
return err return err
} }
// Restore_ctx - записывает is_deleted = true // Restore_ctx - записывает is_deleted = true
func Restore_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { func Restore_ctx(ctx context.Context, db *gorm.DB, m *lawsuit_status_types.LawsuitStatusType) error {
var err error var err error
if micro.ContextDone(ctx) == true { if micro.ContextDone(ctx) == true {
@ -223,7 +234,7 @@ func Restore_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType)
m2 := lawsuit_status_types.LawsuitStatusType{} m2 := lawsuit_status_types.LawsuitStatusType{}
m2.ID = m.ID m2.ID = m.ID
err = Read_ctx(ctx, &m2) err = Read_ctx(ctx, db, &m2)
if err != nil { if err != nil {
return err return err
} }
@ -231,7 +242,7 @@ func Restore_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType)
m2.IsDeleted = false m2.IsDeleted = false
m.IsDeleted = false m.IsDeleted = false
err = Save_ctx(ctx, &m2) err = Save_ctx(ctx, db, &m2)
if err != nil { if err != nil {
err = fmt.Errorf(m.TableNameDB()+" Restore() id: %v, error: %v", m.ID, err) err = fmt.Errorf(m.TableNameDB()+" Restore() id: %v, error: %v", m.ID, err)
} }
@ -253,13 +264,15 @@ 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)) ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS))
defer ctxCancelFunc() defer ctxCancelFunc()
err = Find_ByExtID_ctx(ctx, m) db := postgres_gorm.GetConnection()
err = Find_ByExtID_ctx(ctx, db, m)
return err return err
} }
// Find_ByExtID_ctx - находит запись в БД по ext_id и connection_id // Find_ByExtID_ctx - находит запись в БД по ext_id и connection_id
func Find_ByExtID_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { func Find_ByExtID_ctx(ctx context.Context, db *gorm.DB, m *lawsuit_status_types.LawsuitStatusType) error {
var err error var err error
if micro.ContextDone(ctx) == true { if micro.ContextDone(ctx) == true {
@ -272,7 +285,6 @@ func Find_ByExtID_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatus
return err return err
} }
db := postgres_gorm.GetConnection()
db.WithContext(ctx) db.WithContext(ctx)
tx := db.Where("ext_id = ?", m.ExtID).Where("connection_id = ?", m.ConnectionID).First(m) tx := db.Where("ext_id = ?", m.ExtID).Where("connection_id = ?", m.ConnectionID).First(m)
@ -294,12 +306,14 @@ func (crud Crud_DB) Delete(m *lawsuit_status_types.LawsuitStatusType) error {
ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS)) ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS))
defer ctxCancelFunc() defer ctxCancelFunc()
err = Delete_ctx(ctx, m) db := postgres_gorm.GetConnection()
err = Delete_ctx(ctx, db, m)
return err return err
} }
// Delete_ctx - удаляет запись в БД // Delete_ctx - удаляет запись в БД
func Delete_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { func Delete_ctx(ctx context.Context, db *gorm.DB, m *lawsuit_status_types.LawsuitStatusType) error {
var err error var err error
if micro.ContextDone(ctx) == true { if micro.ContextDone(ctx) == true {
@ -307,7 +321,6 @@ func Delete_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType)
return err return err
} }
db := postgres_gorm.GetConnection()
db.WithContext(ctx) db.WithContext(ctx)
tx := db.Delete(m) tx := db.Delete(m)

View File

@ -11,6 +11,7 @@ import (
"time" "time"
"github.com/ManyakRus/starter/contextmain" "github.com/ManyakRus/starter/contextmain"
"gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/db/crud_functions" "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/db/crud_functions"
"gorm.io/gorm"
) )
// TableName - имя таблицы в БД Postgres // TableName - имя таблицы в БД Postgres
@ -28,12 +29,14 @@ func (crud Crud_DB) Read(m *lawsuit_status_types.LawsuitStatusType) error {
ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS)) ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS))
defer ctxCancelFunc() defer ctxCancelFunc()
err = Read_ctx(ctx, m) db := postgres_gorm.GetConnection()
err = Read_ctx(ctx, db, m)
return err return err
} }
// Read_ctx - находит запись в БД по ID // Read_ctx - находит запись в БД по ID
func Read_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { func Read_ctx(ctx context.Context, db *gorm.DB, m *lawsuit_status_types.LawsuitStatusType) error {
var err error var err error
if micro.ContextDone(ctx) == true { if micro.ContextDone(ctx) == true {
@ -43,7 +46,6 @@ func Read_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) er
id := int64(m.ID) id := int64(m.ID)
db := postgres_gorm.GetConnection()
db.WithContext(ctx) db.WithContext(ctx)
tx := db.First(m, id) tx := db.First(m, id)
@ -61,12 +63,14 @@ func (crud Crud_DB) Save(m *lawsuit_status_types.LawsuitStatusType) error {
ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS)) ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS))
defer ctxCancelFunc() defer ctxCancelFunc()
err := Save_ctx(ctx, m) db := postgres_gorm.GetConnection()
err := Save_ctx(ctx, db, m)
return err return err
} }
// Save_ctx - записывает новый или существующий объект в базу данных // Save_ctx - записывает новый или существующий объект в базу данных
func Save_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { func Save_ctx(ctx context.Context, db *gorm.DB, m *lawsuit_status_types.LawsuitStatusType) error {
var err error var err error
if micro.ContextDone(ctx) == true { if micro.ContextDone(ctx) == true {
err = context.Canceled err = context.Canceled
@ -74,7 +78,7 @@ func Save_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) er
} }
is_create := !micro.BoolFromInt64(int64(m.ID)) is_create := !micro.BoolFromInt64(int64(m.ID))
err = create_update_ctx(ctx, m, is_create) err = create_update_ctx(ctx, db, m, is_create)
return err return err
} }
@ -84,19 +88,21 @@ func (crud Crud_DB) Update(m *lawsuit_status_types.LawsuitStatusType) error {
ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS)) ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS))
defer ctxCancelFunc() defer ctxCancelFunc()
err := Update_ctx(ctx, m) db := postgres_gorm.GetConnection()
err := Update_ctx(ctx, db, m)
return err return err
} }
// Update_ctx - записывает существующий объект в базу данных // Update_ctx - записывает существующий объект в базу данных
func Update_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { func Update_ctx(ctx context.Context, db *gorm.DB, m *lawsuit_status_types.LawsuitStatusType) error {
var err error var err error
if micro.ContextDone(ctx) == true { if micro.ContextDone(ctx) == true {
err = context.Canceled err = context.Canceled
return err return err
} }
err = create_update_ctx(ctx, m, false) err = create_update_ctx(ctx, db, m, false)
return err return err
} }
@ -106,19 +112,21 @@ func (crud Crud_DB) Create(m *lawsuit_status_types.LawsuitStatusType) error {
ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS)) ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS))
defer ctxCancelFunc() defer ctxCancelFunc()
err := Create_ctx(ctx, m) db := postgres_gorm.GetConnection()
err := Create_ctx(ctx, db, m)
return err return err
} }
// Create_ctx - записывает новый объект в базу данных // Create_ctx - записывает новый объект в базу данных
func Create_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { func Create_ctx(ctx context.Context, db *gorm.DB, m *lawsuit_status_types.LawsuitStatusType) error {
var err error var err error
if micro.ContextDone(ctx) == true { if micro.ContextDone(ctx) == true {
err = context.Canceled err = context.Canceled
return err return err
} }
err = create_update_ctx(ctx, m, true) err = create_update_ctx(ctx, db, m, true)
return err return err
} }
@ -130,12 +138,14 @@ 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)) ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS))
defer ctxCancelFunc() defer ctxCancelFunc()
err = create_update_ctx(ctx, m, is_create) db := postgres_gorm.GetConnection()
err = create_update_ctx(ctx, db, m, is_create)
return err return err
} }
// create_update_ctx - записывает объект в базу данных // create_update_ctx - записывает объект в базу данных
func create_update_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType, is_create bool) error { func create_update_ctx(ctx context.Context, db *gorm.DB, m *lawsuit_status_types.LawsuitStatusType, is_create bool) error {
var err error var err error
// log.Trace("start Save() ", TableName, " id: ", int64(m.ID)) // log.Trace("start Save() ", TableName, " id: ", int64(m.ID))
@ -159,7 +169,6 @@ func create_update_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatu
} }
// //
db := postgres_gorm.GetConnection()
db.WithContext(ctx) db.WithContext(ctx)
// заполним даты // заполним даты
@ -208,12 +217,14 @@ func (crud Crud_DB) Delete(m *lawsuit_status_types.LawsuitStatusType) error {
ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS)) ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS))
defer ctxCancelFunc() defer ctxCancelFunc()
err = Delete_ctx(ctx, m) db := postgres_gorm.GetConnection()
err = Delete_ctx(ctx, db, m)
return err return err
} }
// Delete_ctx - записывает is_deleted = true // Delete_ctx - записывает is_deleted = true
func Delete_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { func Delete_ctx(ctx context.Context, db *gorm.DB, m *lawsuit_status_types.LawsuitStatusType) error {
var err error var err error
if micro.ContextDone(ctx) == true { if micro.ContextDone(ctx) == true {
@ -223,7 +234,7 @@ func Delete_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType)
m2 := lawsuit_status_types.LawsuitStatusType{} m2 := lawsuit_status_types.LawsuitStatusType{}
m2.ID = m.ID m2.ID = m.ID
err = Read_ctx(ctx, &m2) err = Read_ctx(ctx, db, &m2)
if err != nil { if err != nil {
return err return err
} }
@ -231,7 +242,7 @@ func Delete_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType)
m2.IsDeleted = true m2.IsDeleted = true
m.IsDeleted = true m.IsDeleted = true
err = Save_ctx(ctx, &m2) err = Save_ctx(ctx, db, &m2)
if err != nil { if err != nil {
err = fmt.Errorf(m.TableNameDB()+" Delete() id: %v, error: %v", m.ID, err) err = fmt.Errorf(m.TableNameDB()+" Delete() id: %v, error: %v", m.ID, err)
} }
@ -247,12 +258,14 @@ func (crud Crud_DB) Restore(m *lawsuit_status_types.LawsuitStatusType) error {
ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS)) ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS))
defer ctxCancelFunc() defer ctxCancelFunc()
err = Restore_ctx(ctx, m) db := postgres_gorm.GetConnection()
err = Restore_ctx(ctx, db, m)
return err return err
} }
// Restore_ctx - записывает is_deleted = true // Restore_ctx - записывает is_deleted = true
func Restore_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { func Restore_ctx(ctx context.Context, db *gorm.DB, m *lawsuit_status_types.LawsuitStatusType) error {
var err error var err error
if micro.ContextDone(ctx) == true { if micro.ContextDone(ctx) == true {
@ -262,7 +275,7 @@ func Restore_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType)
m2 := lawsuit_status_types.LawsuitStatusType{} m2 := lawsuit_status_types.LawsuitStatusType{}
m2.ID = m.ID m2.ID = m.ID
err = Read_ctx(ctx, &m2) err = Read_ctx(ctx, db, &m2)
if err != nil { if err != nil {
return err return err
} }
@ -270,7 +283,7 @@ func Restore_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType)
m2.IsDeleted = false m2.IsDeleted = false
m.IsDeleted = false m.IsDeleted = false
err = Save_ctx(ctx, &m2) err = Save_ctx(ctx, db, &m2)
if err != nil { if err != nil {
err = fmt.Errorf(m.TableNameDB()+" Restore() id: %v, error: %v", m.ID, err) err = fmt.Errorf(m.TableNameDB()+" Restore() id: %v, error: %v", m.ID, err)
} }
@ -292,13 +305,15 @@ 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)) ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_DB_SECONDS))
defer ctxCancelFunc() defer ctxCancelFunc()
err = Find_ByExtID_ctx(ctx, m) db := postgres_gorm.GetConnection()
err = Find_ByExtID_ctx(ctx, db, m)
return err return err
} }
// Find_ByExtID_ctx - находит запись в БД по ext_id и connection_id // Find_ByExtID_ctx - находит запись в БД по ext_id и connection_id
func Find_ByExtID_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatusType) error { func Find_ByExtID_ctx(ctx context.Context, db *gorm.DB, m *lawsuit_status_types.LawsuitStatusType) error {
var err error var err error
if micro.ContextDone(ctx) == true { if micro.ContextDone(ctx) == true {
@ -311,7 +326,6 @@ func Find_ByExtID_ctx(ctx context.Context, m *lawsuit_status_types.LawsuitStatus
return err return err
} }
db := postgres_gorm.GetConnection()
db.WithContext(ctx) db.WithContext(ctx)
tx := db.Where("ext_id = ?", m.ExtID).Where("connection_id = ?", m.ConnectionID).First(m) tx := db.Where("ext_id = ?", m.ExtID).Where("connection_id = ?", m.ConnectionID).First(m)

View File

@ -411,8 +411,11 @@ func CreateFilesUpdateEveryColumn(Table1 *types.Table) error {
ProtoURL := create_files.FindProtoURL() ProtoURL := create_files.FindProtoURL()
TextGRPCServer = create_files.AddImport(TextGRPCServer, ProtoURL) TextGRPCServer = create_files.AddImport(TextGRPCServer, ProtoURL)
//ModelURL := create_files.FindModelURL() DBConstantsURL := create_files.FindDBConstantsURL()
//TextGRPCServer = create_files.AddImport(TextGRPCServer, ModelURL) TextGRPCServer = create_files.AddImport(TextGRPCServer, DBConstantsURL)
CrudTableURL := create_files.FindCrudTableURL(TableName)
TextGRPCServer = create_files.AddImport(TextGRPCServer, CrudTableURL)
} }
@ -684,6 +687,12 @@ func CreateFilesCache(Table1 *types.Table) error {
ProtoURL := create_files.FindProtoURL() ProtoURL := create_files.FindProtoURL()
TextGRPCServer = create_files.AddImport(TextGRPCServer, ProtoURL) TextGRPCServer = create_files.AddImport(TextGRPCServer, ProtoURL)
DBConstantsURL := create_files.FindDBConstantsURL()
TextGRPCServer = create_files.AddImport(TextGRPCServer, DBConstantsURL)
CrudTableURL := create_files.FindCrudTableURL(TableName)
TextGRPCServer = create_files.AddImport(TextGRPCServer, CrudTableURL)
} }
//создание текста //создание текста