Documentation
¶
Index ¶
- func CloseDatabase(db *sql.DB) error
- func ConnectToDatabase() (*sql.DB, error)
- func ExecWithContext(ctx context.Context, db *sql.DB, query string, args ...interface{}) (sql.Result, error)
- func FormatDatabaseError(err error) string
- func GetDatabaseStats(db *sql.DB) sql.DBStats
- func GetDatabaseVersion(db *sql.DB) (string, error)
- func GetErrorSeverity(err error) string
- func GetPostgresErrorCode(err error) string
- func IsConnectionError(err error) bool
- func IsDatabaseConnected(db *sql.DB) bool
- func IsDuplicateError(err error) *string
- func IsForeignKeyError(err error) bool
- func IsNotNullError(err error) bool
- func IsPostgresError(err error) bool
- func IsTimeoutError(err error) bool
- func LogDatabaseError(err error, operation string)
- func PingDatabase(db *sql.DB) error
- func PrintDatabaseStats(db *sql.DB)
- func QueryRowWithContext(ctx context.Context, db *sql.DB, query string, args ...interface{}) *sql.Row
- func QueryWithContext(ctx context.Context, db *sql.DB, query string, args ...interface{}) (*sql.Rows, error)
- func ShouldRetryError(err error) bool
- func Transaction(database *sql.DB, operation TransactionFunction) (err error)
- func TransactionWithIsolation(database *sql.DB, operation TransactionFunction, ...) error
- func TransactionWithRetry(database *sql.DB, operation TransactionFunction, maxRetries int) error
- type DatabaseConfig
- type DatabaseError
- type TransactionFunction
- type TransactionMetrics
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CloseDatabase ¶ added in v1.3.7
CloseDatabase closes the database connection with context support.
func ConnectToDatabase ¶
ConnectToDatabase establishes a connection to a PostgreSQL database. Configures connection pooling and verifies connectivity. Returns the database handle or an error if the connection fails.
func ExecWithContext ¶ added in v1.3.7
func ExecWithContext(ctx context.Context, db *sql.DB, query string, args ...interface{}) (sql.Result, error)
ExecWithContext is a convenience function for executing queries with context.
func FormatDatabaseError ¶ added in v1.3.7
FormatDatabaseError creates a user-friendly error message from a database error.
func GetDatabaseStats ¶ added in v1.3.7
GetDatabaseStats returns database connection pool statistics.
func GetDatabaseVersion ¶ added in v1.3.7
GetDatabaseVersion returns the PostgreSQL server version.
func GetErrorSeverity ¶ added in v1.3.7
GetErrorSeverity returns the severity level of the database error.
func GetPostgresErrorCode ¶ added in v1.3.7
GetPostgresErrorCode returns the PostgreSQL error code if available.
func IsConnectionError ¶ added in v1.3.7
IsConnectionError checks if the error is related to database connection.
func IsDatabaseConnected ¶ added in v1.3.7
IsDatabaseConnected checks if the database is connected and responsive.
func IsDuplicateError ¶ added in v1.2.3
IsDuplicateError checks if the error is a Postgres duplicate entry error (unique_violation). Returns the constraint/field name if duplicate, otherwise nil.
func IsForeignKeyError ¶ added in v1.3.7
IsForeignKeyError checks if the error is a foreign key violation.
func IsNotNullError ¶ added in v1.3.7
IsNotNullError checks if the error is a not null violation.
func IsPostgresError ¶ added in v1.3.7
IsPostgresError checks if the error is a PostgreSQL-specific error.
func IsTimeoutError ¶ added in v1.3.7
IsTimeoutError checks if the error is related to operation timeout.
func LogDatabaseError ¶ added in v1.3.7
LogDatabaseError logs a database error with appropriate severity and context.
func PingDatabase ¶ added in v1.3.7
PingDatabase pings the database to verify connectivity with context support.
func PrintDatabaseStats ¶ added in v1.3.7
PrintDatabaseStats logs database connection pool statistics.
func QueryRowWithContext ¶ added in v1.3.7
func QueryRowWithContext(ctx context.Context, db *sql.DB, query string, args ...interface{}) *sql.Row
QueryRowWithContext is a convenience function for querying a single row with context.
func QueryWithContext ¶ added in v1.3.7
func QueryWithContext(ctx context.Context, db *sql.DB, query string, args ...interface{}) (*sql.Rows, error)
QueryWithContext is a convenience function for querying multiple rows with context.
func ShouldRetryError ¶ added in v1.3.7
ShouldRetryError checks if the error is transient and the operation can be retried.
func Transaction ¶ added in v1.1.4
func Transaction(database *sql.DB, operation TransactionFunction) (err error)
Transaction executes a database transaction with robust panic handling, consistent timestamps, and enhanced logging for debugging. It returns an error if the transaction fails or panics.
func TransactionWithIsolation ¶ added in v1.3.7
func TransactionWithIsolation(database *sql.DB, operation TransactionFunction, isolationLevel sql.IsolationLevel) error
TransactionWithIsolation executes a database transaction with a specific isolation level.
func TransactionWithRetry ¶ added in v1.3.7
func TransactionWithRetry(database *sql.DB, operation TransactionFunction, maxRetries int) error
TransactionWithRetry executes a database transaction with retry logic for transient errors.
Types ¶
type DatabaseConfig ¶ added in v1.3.7
type DatabaseConfig struct {
MaxIdleConns int // MaxIdleConns sets the maximum number of connections in the idle connection pool
MaxOpenConns int // MaxOpenConns sets the maximum number of open connections to the database
ConnMaxLifetime time.Duration // ConnMaxLifetime sets the maximum amount of time a connection may be reused
ConnMaxIdleTime time.Duration // ConnMaxIdleTime sets the maximum amount of time a connection may be idle
ConnectTimeout time.Duration // ConnectTimeout sets the maximum time for establishing connection
PingTimeout time.Duration // PingTimeout sets the maximum time for ping operations
}
DatabaseConfig holds configuration for database connection and connection pooling.
func LoadDatabaseConfig ¶ added in v1.3.7
func LoadDatabaseConfig() DatabaseConfig
LoadDatabaseConfig loads database configuration with defaults from environment variables.
type DatabaseError ¶ added in v1.3.7
type DatabaseError struct {
OriginalError error // OriginalError is the underlying database error
ErrorType string // ErrorType categorizes the error (duplicate, foreign_key, etc.)
Constraint string // Constraint is the database constraint that was violated
Message string // Message is a user-friendly error message
Table string // Table is the database table where the error occurred
Column string // Column is the database column where the error occurred
}
DatabaseError represents a structured database error with context.
func AnalyzeDatabaseError ¶ added in v1.3.7
func AnalyzeDatabaseError(err error) *DatabaseError
AnalyzeDatabaseError comprehensively analyzes database errors and returns structured information.
func (*DatabaseError) Error ¶ added in v1.3.7
func (e *DatabaseError) Error() string
Error returns the string representation of the database error.
func (*DatabaseError) Unwrap ¶ added in v1.3.7
func (e *DatabaseError) Unwrap() error
Unwrap returns the original error for error wrapping compatibility.
type TransactionFunction ¶ added in v1.1.3
TransactionFunction defines the signature for the transactional operation. It accepts a transaction and returns an error if the operation fails.
type TransactionMetrics ¶ added in v1.3.7
type TransactionMetrics struct {
StartTime time.Time
EndTime time.Time
Duration time.Duration
Success bool
ErrorType string
RetryCount int
IsolationLevel string
}
TransactionMetrics holds metrics about transaction execution.
func TransactionWithMetrics ¶ added in v1.3.7
func TransactionWithMetrics(database *sql.DB, operation TransactionFunction) (*TransactionMetrics, error)
TransactionWithMetrics executes a transaction and returns detailed metrics.