From 27356e028ff759481d77e7000626d11d06156e06 Mon Sep 17 00:00:00 2001 From: danialasadi Date: Fri, 8 May 2026 17:37:58 +0330 Subject: [PATCH] ADD | add AllowUseDBGenericFunc interface to generic funcs constraint & debug sth --- domain/wallet/entity/transaction.go | 2 ++ domain/wallet/entity/wallet.go | 2 ++ .../repository/postgres/insert_transaction.go | 4 ++-- pkg/database/postgres/db.go | 15 +++++++++------ pkg/database/postgres/pagination.go | 2 +- pkg/database/postgres/transaction_handler.go | 6 +++--- pkg/err_msg/message.go | 4 +--- 7 files changed, 20 insertions(+), 15 deletions(-) diff --git a/domain/wallet/entity/transaction.go b/domain/wallet/entity/transaction.go index 6a9a7de7..48b92a2e 100644 --- a/domain/wallet/entity/transaction.go +++ b/domain/wallet/entity/transaction.go @@ -12,6 +12,8 @@ type Transaction struct { CreatedAt time.Time } +func (T Transaction) UnimplementedAllowUseDBGenericFunc() {} + type TransactionType string const ( diff --git a/domain/wallet/entity/wallet.go b/domain/wallet/entity/wallet.go index 648ba19a..d1640d9a 100644 --- a/domain/wallet/entity/wallet.go +++ b/domain/wallet/entity/wallet.go @@ -11,6 +11,8 @@ type Wallet struct { UpdatedAt time.Time } +func (w Wallet) UnimplementedAllowUseDBGenericFunc() {} + type WalletStatus string const ( diff --git a/domain/wallet/repository/postgres/insert_transaction.go b/domain/wallet/repository/postgres/insert_transaction.go index 407ab888..f780a039 100644 --- a/domain/wallet/repository/postgres/insert_transaction.go +++ b/domain/wallet/repository/postgres/insert_transaction.go @@ -68,12 +68,12 @@ func (db *DB) UpsertBalance(newCtx context.Context, userID uint64, amount float6 tx, txErr := txHolder.Conn() if txErr != nil { - err = richerror.New(op).WithMessage(errmsg.ErrorMsgCantUpsertBalance).WithKind(richerror.KindUnexpected) + err = richerror.New(op).WithMessage(errmsg.ErrorMsgFailedQuery).WithKind(richerror.KindUnexpected) return } if tx == nil { - err = richerror.New(op).WithMessage(errmsg.ErrorMsgCantUpsertBalance).WithKind(richerror.KindUnexpected) + err = richerror.New(op).WithMessage(errmsg.ErrorMsgFailedQuery).WithKind(richerror.KindUnexpected) return } diff --git a/pkg/database/postgres/db.go b/pkg/database/postgres/db.go index 52721b99..485ec2ed 100644 --- a/pkg/database/postgres/db.go +++ b/pkg/database/postgres/db.go @@ -115,11 +115,12 @@ func (db *DB) StmtExecContext(ctx context.Context, stmt *sql.Stmt, args ...any) return result, nil } -///////////////////////// generic query +// /////////////////////// generic query +type AllowUseDBGenericFunc interface { + UnimplementedAllowUseDBGenericFunc() +} -type ScannerFunc[T any] func(scanner Scanner) (T, error) - -func InstantQueryContext[T any](ctx context.Context, stmtKey statementKey, query string, conn *DB, scanner ScannerFunc[T], args ...any) ([]T, error) { +func InstantQueryContext[T AllowUseDBGenericFunc](ctx context.Context, stmtKey statementKey, query string, conn *DB, scanner ScannerFunc[T], args ...any) ([]T, error) { const op = richerror.Op("postgres.InstantQueryContext") readyStmt, err := conn.PrepareStatement(ctx, stmtKey, query) @@ -157,7 +158,7 @@ func InstantQueryContext[T any](ctx context.Context, stmtKey statementKey, query } -func InstantQueryRowContext[T any](ctx context.Context, stmtKey statementKey, query string, conn *DB, scanner ScannerFunc[T], args ...any) (item T, err error) { +func InstantQueryRowContext[T AllowUseDBGenericFunc](ctx context.Context, stmtKey statementKey, query string, conn *DB, scanner ScannerFunc[T], args ...any) (item T, err error) { const op = richerror.Op("postgres.InstantQueryRowContext") readyStmt, sErr := conn.PrepareStatement(ctx, stmtKey, query) @@ -190,7 +191,7 @@ func InstantQueryRowContext[T any](ctx context.Context, stmtKey statementKey, qu } -func InstantExecContext(ctx context.Context, stmtKey statementKey, query string, conn *DB, args ...any) (sql.Result, error) { +func InstantExecContext[T AllowUseDBGenericFunc](ctx context.Context, stmtKey statementKey, query string, conn *DB, args ...any) (sql.Result, error) { const op = richerror.Op("postgres.InstantExecContext") readyStmt, err := conn.PrepareStatement(ctx, stmtKey, query) @@ -207,3 +208,5 @@ func InstantExecContext(ctx context.Context, stmtKey statementKey, query string, return result, nil } + +type ScannerFunc[T any] func(scanner Scanner) (T, error) diff --git a/pkg/database/postgres/pagination.go b/pkg/database/postgres/pagination.go index 3a3ddf99..0f4e4dba 100644 --- a/pkg/database/postgres/pagination.go +++ b/pkg/database/postgres/pagination.go @@ -24,7 +24,7 @@ type DBPagination struct { PageSize int64 } -func PageNumberPagination[T any](ctx context.Context, countQuery string, fetchQuery string, conn *DB, countQueryStmt statementKey, fetchQueryStmt statementKey, op richerror.Op, scanner ScannerFunc[T], countParams []any, fetchParams []any) ([]T, int64, error) { +func PageNumberPagination[T AllowUseDBGenericFunc](ctx context.Context, countQuery string, fetchQuery string, conn *DB, countQueryStmt statementKey, fetchQueryStmt statementKey, op richerror.Op, scanner ScannerFunc[T], countParams []any, fetchParams []any) ([]T, int64, error) { var totalCount int64 diff --git a/pkg/database/postgres/transaction_handler.go b/pkg/database/postgres/transaction_handler.go index cbc52d02..a10659af 100644 --- a/pkg/database/postgres/transaction_handler.go +++ b/pkg/database/postgres/transaction_handler.go @@ -182,7 +182,7 @@ func (tx *TxConn) StmtExecContext(ctx context.Context, stmt *sql.Stmt, args ...a ///////////////////////// generic query -func TXInstantQueryContext[T any](ctx context.Context, txConn *sql.Tx, stmtKey statementKey, query string, conn *DB, scanner ScannerFunc[T], args ...any) ([]T, error) { +func TXInstantQueryContext[T AllowUseDBGenericFunc](ctx context.Context, txConn *sql.Tx, stmtKey statementKey, query string, conn *DB, scanner ScannerFunc[T], args ...any) ([]T, error) { const op = richerror.Op("postgres.TXInstantQueryContext") stmt, err := conn.PrepareStatement(ctx, stmtKey, query) @@ -222,7 +222,7 @@ func TXInstantQueryContext[T any](ctx context.Context, txConn *sql.Tx, stmtKey s } -func TXInstantQueryRowContext[T any](ctx context.Context, txConn *sql.Tx, stmtKey statementKey, query string, conn *DB, scanner ScannerFunc[T], args ...any) (item T, err error) { +func TXInstantQueryRowContext[T AllowUseDBGenericFunc](ctx context.Context, txConn *sql.Tx, stmtKey statementKey, query string, conn *DB, scanner ScannerFunc[T], args ...any) (item T, err error) { const op = richerror.Op("postgres.TXInstantQueryRowContext") stmt, sErr := conn.PrepareStatement(ctx, stmtKey, query) @@ -257,7 +257,7 @@ func TXInstantQueryRowContext[T any](ctx context.Context, txConn *sql.Tx, stmtKe } -func TXInstantExecContext(ctx context.Context, txConn *sql.Tx, stmtKey statementKey, query string, conn *DB, args ...any) (sql.Result, error) { +func TXInstantExecContext[T AllowUseDBGenericFunc](ctx context.Context, txConn *sql.Tx, stmtKey statementKey, query string, conn *DB, args ...any) (sql.Result, error) { const op = richerror.Op("postgres.TXInstantExecContext") stmt, err := conn.PrepareStatement(ctx, stmtKey, query) diff --git a/pkg/err_msg/message.go b/pkg/err_msg/message.go index 2049c292..dec8f22d 100644 --- a/pkg/err_msg/message.go +++ b/pkg/err_msg/message.go @@ -58,7 +58,5 @@ const ( ErrorMsgInvalidRefreshToken = "invalid refresh token" ErrorMsgInvalidBenefactorStatus = "invalid benefactor status" ErrorMsgInvalidAction = "action invalid" - ErrorMsgCantUpsertBalance = "cant update balance" // wallet - ErrorMsgFailedQuery = "query failed" // wallet - + ErrorMsgFailedQuery = "query failed" // wallet )