ADD | add AllowUseDBGenericFunc interface to generic funcs constraint & debug sth

This commit is contained in:
danialasadi 2026-05-08 17:37:58 +03:30
parent 9a219730a0
commit 27356e028f
7 changed files with 20 additions and 15 deletions

View File

@ -12,6 +12,8 @@ type Transaction struct {
CreatedAt time.Time CreatedAt time.Time
} }
func (T Transaction) UnimplementedAllowUseDBGenericFunc() {}
type TransactionType string type TransactionType string
const ( const (

View File

@ -11,6 +11,8 @@ type Wallet struct {
UpdatedAt time.Time UpdatedAt time.Time
} }
func (w Wallet) UnimplementedAllowUseDBGenericFunc() {}
type WalletStatus string type WalletStatus string
const ( const (

View File

@ -68,12 +68,12 @@ func (db *DB) UpsertBalance(newCtx context.Context, userID uint64, amount float6
tx, txErr := txHolder.Conn() tx, txErr := txHolder.Conn()
if txErr != nil { if txErr != nil {
err = richerror.New(op).WithMessage(errmsg.ErrorMsgCantUpsertBalance).WithKind(richerror.KindUnexpected) err = richerror.New(op).WithMessage(errmsg.ErrorMsgFailedQuery).WithKind(richerror.KindUnexpected)
return return
} }
if tx == nil { if tx == nil {
err = richerror.New(op).WithMessage(errmsg.ErrorMsgCantUpsertBalance).WithKind(richerror.KindUnexpected) err = richerror.New(op).WithMessage(errmsg.ErrorMsgFailedQuery).WithKind(richerror.KindUnexpected)
return return
} }

View File

@ -116,10 +116,11 @@ func (db *DB) StmtExecContext(ctx context.Context, stmt *sql.Stmt, args ...any)
} }
// /////////////////////// generic query // /////////////////////// generic query
type AllowUseDBGenericFunc interface {
UnimplementedAllowUseDBGenericFunc()
}
type ScannerFunc[T any] func(scanner Scanner) (T, error) func InstantQueryContext[T AllowUseDBGenericFunc](ctx context.Context, stmtKey statementKey, query string, conn *DB, scanner ScannerFunc[T], args ...any) ([]T, error) {
func InstantQueryContext[T any](ctx context.Context, stmtKey statementKey, query string, conn *DB, scanner ScannerFunc[T], args ...any) ([]T, error) {
const op = richerror.Op("postgres.InstantQueryContext") const op = richerror.Op("postgres.InstantQueryContext")
readyStmt, err := conn.PrepareStatement(ctx, stmtKey, query) 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") const op = richerror.Op("postgres.InstantQueryRowContext")
readyStmt, sErr := conn.PrepareStatement(ctx, stmtKey, query) 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") const op = richerror.Op("postgres.InstantExecContext")
readyStmt, err := conn.PrepareStatement(ctx, stmtKey, query) readyStmt, err := conn.PrepareStatement(ctx, stmtKey, query)
@ -207,3 +208,5 @@ func InstantExecContext(ctx context.Context, stmtKey statementKey, query string,
return result, nil return result, nil
} }
type ScannerFunc[T any] func(scanner Scanner) (T, error)

View File

@ -24,7 +24,7 @@ type DBPagination struct {
PageSize int64 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 var totalCount int64

View File

@ -182,7 +182,7 @@ func (tx *TxConn) StmtExecContext(ctx context.Context, stmt *sql.Stmt, args ...a
///////////////////////// generic query ///////////////////////// 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") const op = richerror.Op("postgres.TXInstantQueryContext")
stmt, err := conn.PrepareStatement(ctx, stmtKey, query) 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") const op = richerror.Op("postgres.TXInstantQueryRowContext")
stmt, sErr := conn.PrepareStatement(ctx, stmtKey, query) 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") const op = richerror.Op("postgres.TXInstantExecContext")
stmt, err := conn.PrepareStatement(ctx, stmtKey, query) stmt, err := conn.PrepareStatement(ctx, stmtKey, query)

View File

@ -58,7 +58,5 @@ const (
ErrorMsgInvalidRefreshToken = "invalid refresh token" ErrorMsgInvalidRefreshToken = "invalid refresh token"
ErrorMsgInvalidBenefactorStatus = "invalid benefactor status" ErrorMsgInvalidBenefactorStatus = "invalid benefactor status"
ErrorMsgInvalidAction = "action invalid" ErrorMsgInvalidAction = "action invalid"
ErrorMsgCantUpsertBalance = "cant update balance" // wallet
ErrorMsgFailedQuery = "query failed" // wallet ErrorMsgFailedQuery = "query failed" // wallet
) )