forked from ebhomengo/niki
ADD | add AllowUseDBGenericFunc interface to generic funcs constraint & debug sth
This commit is contained in:
parent
9a219730a0
commit
27356e028f
|
|
@ -12,6 +12,8 @@ type Transaction struct {
|
|||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
func (T Transaction) UnimplementedAllowUseDBGenericFunc() {}
|
||||
|
||||
type TransactionType string
|
||||
|
||||
const (
|
||||
|
|
|
|||
|
|
@ -11,6 +11,8 @@ type Wallet struct {
|
|||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
func (w Wallet) UnimplementedAllowUseDBGenericFunc() {}
|
||||
|
||||
type WalletStatus string
|
||||
|
||||
const (
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
)
|
||||
|
|
|
|||
Loading…
Reference in New Issue