Documentation
¶
Index ¶
- func CreateTransaction(ctx context.Context, db *sql.DB, fn TransactionFn, opts *sql.TxOptions) error
- func RunQueriesConcurrently(db *sql.DB, queries ...func(db *sql.DB) error) []error
- func RunQueriesConcurrentlyWithCancel(db *sql.DB, queries ...func(db *sql.DB, ctx context.Context) error) []error
- type Config
- type DefaultHandler
- type DefaultService
- func (d *DefaultService) CreateTransaction(ctx context.Context, fn TransactionFn, opts *sql.TxOptions) error
- func (d *DefaultService) Exec(query *string, params ...any) (sql.Result, error)
- func (d *DefaultService) ExecWithCtx(ctx context.Context, query *string, params ...any) (sql.Result, error)
- func (d *DefaultService) QueryRow(query *string, params ...any) (*sql.Row, error)
- func (d *DefaultService) QueryRowWithCtx(ctx context.Context, query *string, params ...any) (*sql.Row, error)
- func (d *DefaultService) ScanRow(row *sql.Row, destinations ...any) error
- type Handler
- type Service
- type TransactionFn
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CreateTransaction ¶
func CreateTransaction( ctx context.Context, db *sql.DB, fn TransactionFn, opts *sql.TxOptions, ) error
CreateTransaction creates a transaction for the database
Parameters:
- ctx: The context for the transaction
- db: The database connection
- fn: The function to execute within the transaction
- opts: The transaction options
Returns:
- error: An error if the transaction fails
func RunQueriesConcurrently ¶ added in v0.4.8
RunQueriesConcurrently runs multiple queries concurrently
Parameters:
db: The database connection queries: The queries to run
Returns:
[]error: A slice of errors, or nil if no errors occurred
func RunQueriesConcurrentlyWithCancel ¶ added in v0.4.12
func RunQueriesConcurrentlyWithCancel( db *sql.DB, queries ...func(db *sql.DB, ctx context.Context) error, ) []error
RunQueriesConcurrentlyWithCancel runs multiple queries concurrently with a cancel context
Parameters:
db: The database connection queries: The queries to run
Returns:
[]error: A slice of errors, or nil if no errors occurred
Types ¶
type Config ¶ added in v0.4.6
type Config struct {
DriverName string
DataSourceName string
MaxOpenConnections int
MaxIdleConnections int
ConnectionMaxLifetime time.Duration
ConnectionMaxIdleTime time.Duration
}
Config struct
func NewConfig ¶ added in v0.4.6
func NewConfig( driverName, dataSourceName string, maxOpenConnections, maxIdleConnections int, connectionMaxIdleTime, connectionMaxLifetime time.Duration, ) (*Config, error)
NewConfig creates a new configuration for the connection
Parameters:
- driverName: the name of the driver
- dataSourceName: the data source name
- maxOpenConnections: the maximum number of open connections
- maxIdleConnections: the maximum number of idle connections
- connectionMaxIdleTime: the maximum idle time for a connection
- connectionMaxLifetime: the maximum lifetime for a connection
Returns:
- *Config: the connection configuration
- error: if any error occurs
type DefaultHandler ¶ added in v0.7.1
type DefaultHandler struct {
// contains filtered or unexported fields
}
DefaultHandler struct
func NewDefaultHandler ¶ added in v0.7.1
func NewDefaultHandler( config *Config, ) (*DefaultHandler, error)
NewDefaultHandler creates a new connection
Parameters:
- config: the configuration for the connection
Returns:
- *DefaultHandler: the connection handler
func (*DefaultHandler) Connect ¶ added in v0.7.1
func (d *DefaultHandler) Connect() (*sql.DB, error)
Connect returns a new SQL connection
Returns:
- *sql.DB: the SQL connection
- error: if any error occurred
func (*DefaultHandler) DB ¶ added in v0.7.1
func (d *DefaultHandler) DB() (*sql.DB, error)
DB returns the SQL connection
Returns:
- *sql.DB: the SQL connection
- error: if any error occurred
func (*DefaultHandler) Disconnect ¶ added in v0.7.1
func (d *DefaultHandler) Disconnect() error
Disconnect closes the SQL connection
Returns:
- error: if any error occurred
func (*DefaultHandler) IsConnected ¶ added in v0.7.5
func (d *DefaultHandler) IsConnected() bool
IsConnected checks if the SQL connection is established
Returns:
- bool: true if the connection is established, false otherwise
type DefaultService ¶ added in v0.5.0
type DefaultService struct {
Handler
}
DefaultService is the default service struct
func NewDefaultService ¶ added in v0.5.0
func NewDefaultService(config *Config) ( instance *DefaultService, err error, )
NewDefaultService creates a new default service
Parameters:
- config: the configuration for the connection
Returns:
*DefaultService: the default service error: if there was an error creating the service
func (*DefaultService) CreateTransaction ¶ added in v0.5.0
func (d *DefaultService) CreateTransaction( ctx context.Context, fn TransactionFn, opts *sql.TxOptions, ) error
CreateTransaction creates a transaction for the database
Parameters:
- ctx: The context for the transaction - fn: The function to execute within the transaction - opts: The transaction options
Returns:
- error: An error if the transaction fails
func (*DefaultService) Exec ¶ added in v0.5.0
Exec executes a query with parameters and returns the result
Parameters:
- query: the query to execute
- params: the parameters for the query
Returns:
- sql.Result: the result of the execution
func (*DefaultService) ExecWithCtx ¶ added in v0.7.5
func (d *DefaultService) ExecWithCtx( ctx context.Context, query *string, params ...any, ) ( sql.Result, error, )
ExecWithCtx executes a query with parameters and returns the result with a context
Parameters:
- ctx: the context to use - query: the query to execute - params: the parameters for the query
Returns:
- sql.Result: the result of the execution - error: if any error occurs
func (*DefaultService) QueryRow ¶ added in v0.5.0
QueryRow runs a query row with parameters and returns the result row
Parameters:
- query: the query to execute - params: the parameters for the query
Returns:
- *sql.Row: the result row - error: if any error occurs
func (*DefaultService) QueryRowWithCtx ¶ added in v0.7.5
func (d *DefaultService) QueryRowWithCtx( ctx context.Context, query *string, params ...any, ) (*sql.Row, error)
QueryRowWithCtx runs a query row with parameters and returns the result row with a context
Parameters:
- ctx: the context to use - query: the query to execute - params: the parameters for the query
Returns:
- *sql.Row: the result row - error: if any error occurs
type Handler ¶ added in v0.7.1
type Handler interface {
Connect() (*sql.DB, error)
IsConnected() bool
DB() (*sql.DB, error)
Disconnect() error
}
Handler interface
type Service ¶ added in v0.5.0
type Service interface {
Handler
CreateTransaction(
ctx context.Context,
fn TransactionFn,
opts *sql.TxOptions,
) error
Exec(query *string, params ...any) (sql.Result, error)
ExecWithCtx(
ctx context.Context,
query *string,
params ...any,
) (sql.Result, error)
QueryRow(query *string, params ...any) (*sql.Row, error)
QueryRowWithCtx(
ctx context.Context,
query *string,
params ...any,
) (*sql.Row, error)
ScanRow(row *sql.Row, destinations ...any) error
}
Service is the interface for the service
type TransactionFn ¶ added in v0.6.1
TransactionFn is the function type for transactions