Documentation
¶
Index ¶
Constants ¶
const Author = "github.com/lordofthemind"
Author of the gopherpostgres package
const Description = "" /* 134-byte string literal not displayed */
Description of the gopherpostgres package
const Version = "1.0.1"
Version of the gopherpostgres package
Variables ¶
This section is empty.
Functions ¶
func CheckAndEnableUUIDExtension ¶
CheckAndEnableUUIDExtension checks if the 'uuid-ossp' extension is enabled in PostgreSQL and enables it if it is not.
The 'uuid-ossp' extension is required for generating UUIDs using the `uuid_generate_v4()` function in PostgreSQL. This function queries the PostgreSQL system catalog to check whether the extension is already enabled. If the extension is not found, it attempts to create and enable the extension.
Params:
db - The GORM database connection (*gorm.DB) used to interact with the PostgreSQL database.
Returns:
error - Returns an error if the check or enabling the extension fails.
Example usage:
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{}) if err != nil { log.Fatalf("Failed to connect to PostgreSQL: %v", err) } err = CheckAndEnableUUIDExtension(db) if err != nil { log.Fatalf("Error enabling UUID extension: %v", err) }
This function is useful when working with GORM and PostgreSQL to ensure the 'uuid-ossp' extension is available for UUID generation in your database schema.
func ConnectPostgresDB ¶
func ConnectPostgresDB(ctx context.Context, dsn string, timeout time.Duration, maxRetries int) (*sql.DB, error)
ConnectPostgresDB connects to a PostgreSQL database using the sql package with retries.
This function attempts to connect to a PostgreSQL database using the provided Data Source Name (DSN), retrying the connection up to 'maxRetries' times. It uses a context with a timeout to ensure that the connection does not hang indefinitely. If the connection is successful, it returns a *sql.DB instance for database operations.
Params:
ctx - The context for managing connection timeout and cancellation. dsn - The PostgreSQL connection string (Data Source Name). timeout - The timeout duration for the connection attempt. maxRetries - The maximum number of retries before giving up.
Returns:
*sql.DB - The connected PostgreSQL database instance on success. error - An error message if the connection fails after the retries.
Example usage:
ctx := context.Background() db, err := ConnectPostgresDB(ctx, "postgres://user:password@localhost:5432/mydb", 10*time.Second, 3) if err != nil { log.Fatalf("Failed to connect to PostgreSQL: %v", err) } defer db.Close()
Once connected, you can perform SQL operations like querying or executing statements.
func ConnectToPostgresGORM ¶
ConnectToPostgresGORM connects to a PostgreSQL database using GORM with retries.
This function attempts to connect to a PostgreSQL database using the GORM ORM library. It applies the provided configuration and tries to connect up to 'MaxRetries' times. The function uses a context with a timeout to control how long the connection attempt lasts. If successful, a *gorm.DB instance is returned, which allows performing ORM-based operations.
Params:
ctx - The context for managing connection timeout and cancellation. config - The configuration struct containing DSN, Timeout, and MaxRetries.
Returns:
*gorm.DB - The connected GORM PostgreSQL database instance on success. error - An error message if the connection fails after the retries.
Example:
config := gopherpostgres.PostgresConfig{ DSN: "postgres://user:password@localhost:5432/mydb", Timeout: 10 * time.Second, MaxRetries: 3, } ctx := context.Background() db, err := ConnectToPostgresGORM(ctx, config) if err != nil { log.Fatalf("Failed to connect to PostgreSQL using GORM: %v", err) }
Types ¶
type PostgresConfig ¶
type PostgresConfig struct { DSN string // PostgreSQL connection string (Data Source Name) Timeout time.Duration // Timeout duration for the connection attempt MaxRetries int // Maximum number of retries before giving up }
PostgresConfig encapsulates the configuration parameters for connecting to a PostgreSQL database.
Fields:
- DSN: PostgreSQL connection string (Data Source Name). - Timeout: Timeout duration for the connection attempt. - MaxRetries: Maximum number of retries before giving up.
Example:
config := PostgresConfig{ DSN: "postgres://user:password@localhost:5432/mydb", Timeout: 10 * time.Second, MaxRetries: 3, }