Documentation
¶
Index ¶
- func ExecuteInTransaction[T any](ctx context.Context, db IDbConnection, tsf TransactionScopeFunction[T], ...) (T, error)
- func ExecuteInTransactionAsync[T any](ctx context.Context, db IDbConnection, tsf TransactionScopeFunction[T], ...) async.Result[T]
- func NewErrInvalidDataType(format string, args ...any) error
- func Query[T any](ctx context.Context, conn IDbSession, query string, args ...any) ([]T, error)
- func QueryAsync[T any](ctx context.Context, conn IDbSession, query string, args ...any) async.Result[[]T]
- type ErrInvalidDataType
- type IDbConnection
- type IDbSession
- type TransactionScopeFunction
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExecuteInTransaction ¶
func ExecuteInTransaction[T any](ctx context.Context, db IDbConnection, tsf TransactionScopeFunction[T], opts ...sql.TxOptions) (T, error)
ExecuteInTransaction executes the provided function within a database transaction.
This function creates a new transaction using the provided database connection and executes the given TransactionScopeFunction within that transaction context. If the function completes successfully, the transaction is committed; otherwise, it is rolled back. The transaction is also rolled back if a panic occurs during execution (via deferred rollback).
Type parameter T represents the return type of the transaction function, allowing for flexible return values based on the specific business logic requirements.
Parameters:
- ctx: Context for cancellation and timeout control, propagated to the transaction
- db: Database connection to use for creating the transaction
- tsf: Function to execute within the transaction scope
- opts: Optional transaction options (isolation level, read-only mode, etc.). If not provided, default transaction options are used.
Returns:
- T: The result returned by the transaction function
- error: Non-nil if transaction creation, execution, or commit fails
func ExecuteInTransactionAsync ¶
func ExecuteInTransactionAsync[T any](ctx context.Context, db IDbConnection, tsf TransactionScopeFunction[T], opts ...sql.TxOptions) async.Result[T]
ExecuteInTransactionAsync executes a database transaction asynchronously and returns the result.
This function wraps the synchronous ExecuteInTransaction function in an asynchronous execution context, allowing the transaction to run concurrently without blocking the caller. The result is returned as an async.Result that can be awaited or processed later.
The function leverages Go's concurrency model to execute the entire transaction lifecycle (begin, execute, commit/rollback) in a separate goroutine, making it suitable for scenarios where: - You want to execute multiple independent transactions in parallel - You need to avoid blocking the main execution flow while waiting for transaction completion - You're implementing non-blocking data persistence patterns - You want to improve throughput by overlapping transaction execution with other work
Type parameter T represents the return type of the transaction function, allowing for flexible return values based on the specific business logic requirements.
Parameters:
- ctx: Context for cancellation and timeout control, propagated to the underlying transaction
- db: Database connection to use for creating the transaction
- tsf: Function to execute within the transaction scope
- opts: Optional transaction options (isolation level, read-only mode, etc.). If not provided, default transaction options are used.
Returns:
- async.Result[T]: An async result object containing either:
- The result returned by the transaction function
- An error if transaction creation, execution, or commit fails
func NewErrInvalidDataType ¶ added in v1.0.4
func Query ¶
Query executes a SQL query and returns the results as a slice of type T.
The function performs the following operations: 1. Executes the provided SQL query with the given arguments using the database session 2. Parses the returned rows into a slice of the specified type T 3. Ensures proper resource cleanup by closing the rows
Type parameter T must be a type that can be populated from database rows. The actual mapping from database rows to type T is handled by parseDbResult.
Parameters:
- ctx: Context for cancellation and timeout control
- conn: Database session (connection or transaction) to execute the query on
- query: SQL query string to execute
- args: Variadic arguments to be used as query parameters (prevents SQL injection)
Returns:
- []T: Slice of results parsed from the query, empty slice if no rows match
- error: Non-nil if query execution or result parsing fails
func QueryAsync ¶
func QueryAsync[T any](ctx context.Context, conn IDbSession, query string, args ...any) async.Result[[]T]
QueryAsync executes a SQL query asynchronously and returns the results as a slice of type T.
This function wraps the synchronous Query function in an asynchronous execution context, allowing the query to run concurrently without blocking the caller. The result is returned as an async.Result that can be awaited or processed later.
The function leverages Go's concurrency model to execute the database query in a separate goroutine, making it suitable for scenarios where you want to: - Execute multiple independent queries in parallel - Avoid blocking the main execution flow while waiting for database results - Implement non-blocking data fetching patterns
Types ¶
type ErrInvalidDataType ¶ added in v1.0.4
type ErrInvalidDataType struct {
Message string
}
---------------------------------------------------------------------- ErrInvalidDataType ----------------------------------------------------------------------
func (ErrInvalidDataType) Error ¶ added in v1.0.4
func (e ErrInvalidDataType) Error() string
Error implements error.
type IDbConnection ¶
type IDbSession ¶
type TransactionScopeFunction ¶
TransactionScopeFunction is a function type that executes database operations within a transaction context.
This function type is designed to encapsulate business logic that requires transactional guarantees. It receives a context for cancellation/timeout control and a transaction object for executing database operations, and returns a result of type T along with any error that occurred.
Type parameter T represents the return type of the transaction function, allowing for flexible return values based on the specific use case.
Parameters:
- ctx: Context for cancellation and timeout control during transaction execution
- tx: Active database transaction to use for executing queries and commands
Returns:
- T: Result of the transaction operations (type determined by caller)
- error: Non-nil if any operation within the transaction fails