database

package
v1.4.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 29, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CloseDatabase added in v1.3.7

func CloseDatabase(db *sql.DB) error

CloseDatabase closes the database connection with context support.

func ConnectToDatabase

func ConnectToDatabase() (*sql.DB, error)

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

func FormatDatabaseError(err error) string

FormatDatabaseError creates a user-friendly error message from a database error.

func GetDatabaseStats added in v1.3.7

func GetDatabaseStats(db *sql.DB) sql.DBStats

GetDatabaseStats returns database connection pool statistics.

func GetDatabaseVersion added in v1.3.7

func GetDatabaseVersion(db *sql.DB) (string, error)

GetDatabaseVersion returns the PostgreSQL server version.

func GetErrorSeverity added in v1.3.7

func GetErrorSeverity(err error) string

GetErrorSeverity returns the severity level of the database error.

func GetPostgresErrorCode added in v1.3.7

func GetPostgresErrorCode(err error) string

GetPostgresErrorCode returns the PostgreSQL error code if available.

func IsConnectionError added in v1.3.7

func IsConnectionError(err error) bool

IsConnectionError checks if the error is related to database connection.

func IsDatabaseConnected added in v1.3.7

func IsDatabaseConnected(db *sql.DB) bool

IsDatabaseConnected checks if the database is connected and responsive.

func IsDuplicateError added in v1.2.3

func IsDuplicateError(err error) *string

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

func IsForeignKeyError(err error) bool

IsForeignKeyError checks if the error is a foreign key violation.

func IsNotNullError added in v1.3.7

func IsNotNullError(err error) bool

IsNotNullError checks if the error is a not null violation.

func IsPostgresError added in v1.3.7

func IsPostgresError(err error) bool

IsPostgresError checks if the error is a PostgreSQL-specific error.

func IsTimeoutError added in v1.3.7

func IsTimeoutError(err error) bool

IsTimeoutError checks if the error is related to operation timeout.

func LogDatabaseError added in v1.3.7

func LogDatabaseError(err error, operation string)

LogDatabaseError logs a database error with appropriate severity and context.

func PingDatabase added in v1.3.7

func PingDatabase(db *sql.DB) error

PingDatabase pings the database to verify connectivity with context support.

func PrintDatabaseStats added in v1.3.7

func PrintDatabaseStats(db *sql.DB)

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

func ShouldRetryError(err error) bool

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

type TransactionFunction func(transaction *sql.Tx) error

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL